1 // AircraftItemDelegate.cxx - part of GUI launcher using Qt5
3 // Written by James Turner, started March 2015.
5 // Copyright (C) 2014 James Turner <zakalawe@mac.com>
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "AircraftItemDelegate.hxx"
26 #include <QLinearGradient>
28 #include <QMouseEvent>
29 #include <QFontMetrics>
31 #include "AircraftModel.hxx"
33 AircraftItemDelegate::AircraftItemDelegate(QListView* view) :
36 view->viewport()->installEventFilter(this);
37 view->viewport()->setMouseTracking(true);
39 m_leftArrowIcon.load(":/left-arrow-icon");
40 m_rightArrowIcon.load(":/right-arrow-icon");
43 void AircraftItemDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option,
44 const QModelIndex & index) const
46 painter->setRenderHint(QPainter::Antialiasing);
47 // selection feedback rendering
48 if (option.state & QStyle::State_Selected) {
49 QLinearGradient grad(option.rect.topLeft(), option.rect.bottomLeft());
50 grad.setColorAt(0.0, QColor(152, 163, 180));
51 grad.setColorAt(1.0, QColor(90, 107, 131));
53 QBrush backgroundBrush(grad);
54 painter->fillRect(option.rect, backgroundBrush);
56 painter->setPen(QColor(90, 107, 131));
57 painter->drawLine(option.rect.topLeft(), option.rect.topRight());
60 QRect contentRect = option.rect.adjusted(MARGIN, MARGIN, -MARGIN, -MARGIN);
62 QPixmap thumbnail = index.data(Qt::DecorationRole).value<QPixmap>();
63 quint32 yPos = contentRect.center().y() - (thumbnail.height() / 2);
64 painter->drawPixmap(contentRect.left(), yPos, thumbnail);
67 painter->setPen(QColor(0x7f, 0x7f, 0x7f));
68 painter->setBrush(Qt::NoBrush);
69 painter->drawRect(contentRect.left(), yPos, thumbnail.width(), thumbnail.height());
71 // draw bottom dividing line
72 painter->drawLine(contentRect.left(), contentRect.bottom() + MARGIN,
73 contentRect.right(), contentRect.bottom() + MARGIN);
75 int variantCount = index.data(AircraftVariantCountRole).toInt();
76 int currentVariant =index.data(AircraftVariantRole).toInt();
77 QString description = index.data(Qt::DisplayRole).toString();
78 contentRect.setLeft(contentRect.left() + MARGIN + thumbnail.width());
80 painter->setPen(Qt::black);
85 QRect descriptionRect = contentRect.adjusted(ARROW_SIZE, 0, -ARROW_SIZE, 0),
88 if (variantCount > 0) {
89 bool canLeft = (currentVariant > 0);
90 bool canRight = (currentVariant < variantCount );
92 QRect leftArrowRect = leftCycleArrowRect(option.rect, index);
94 painter->drawPixmap(leftArrowRect.topLeft() + QPoint(2, 2), m_leftArrowIcon);
97 QRect rightArrowRect = rightCycleArrowRect(option.rect, index);
99 painter->drawPixmap(rightArrowRect.topLeft() + QPoint(2, 2), m_rightArrowIcon);
103 painter->drawText(descriptionRect, Qt::TextWordWrap, description, &actualBounds);
105 QString authors = index.data(AircraftAuthorsRole).toString();
110 if (!authors.isEmpty()) {
111 QRect authorsRect = descriptionRect;
112 authorsRect.moveTop(actualBounds.bottom() + MARGIN);
113 painter->drawText(authorsRect, Qt::TextWordWrap,
114 QString("by: %1").arg(authors),
118 QString longDescription = index.data(AircraftLongDescriptionRole).toString();
119 if (!longDescription.isEmpty()) {
120 QRect longDescriptionRect = descriptionRect;
121 longDescriptionRect.moveTop(actualBounds.bottom() + MARGIN);
122 painter->drawText(longDescriptionRect, Qt::TextWordWrap,
123 longDescription, &actualBounds);
126 QRect r = contentRect;
127 r.setWidth(contentRect.width() / 3);
128 r.moveTop(actualBounds.bottom() + MARGIN);
129 r.moveLeft(r.right());
132 if (index.data(AircraftHasRatingsRole).toBool()) {
133 drawRating(painter, "Flight model:", r, index.data(AircraftRatingRole).toInt());
134 r.moveTop(r.bottom());
135 drawRating(painter, "Systems:", r, index.data(AircraftRatingRole + 1).toInt());
137 r.moveTop(actualBounds.bottom() + MARGIN);
138 r.moveLeft(r.right());
139 drawRating(painter, "Cockpit:", r, index.data(AircraftRatingRole + 2).toInt());
140 r.moveTop(r.bottom());
141 drawRating(painter, "Exterior model:", r, index.data(AircraftRatingRole + 3).toInt());
144 QVariant v = index.data(AircraftPackageStatusRole);
145 AircraftItemStatus status = static_cast<AircraftItemStatus>(v.toInt());
146 double downloadFraction = 0.0;
148 if (status != PackageInstalled) {
149 QString buttonText, infoText;
150 QColor buttonColor(27, 122, 211);
152 double sizeInMBytes = index.data(AircraftPackageSizeRole).toInt();
153 sizeInMBytes /= 0x100000;
155 if (status == PackageDownloading) {
156 buttonText = tr("Cancel");
157 double downloadedMB = index.data(AircraftInstallDownloadedSizeRole).toInt();
158 downloadedMB /= 0x100000;
159 infoText = QStringLiteral("%1 MB of %2 MB").arg(downloadedMB, 0, 'f', 1).arg(sizeInMBytes, 0, 'f', 1);
160 buttonColor = QColor(0xcf, 0xcf, 0xcf);
161 downloadFraction = downloadedMB / sizeInMBytes;
162 } else if (status == PackageQueued) {
163 buttonText = tr("Cancel");
164 infoText = tr("Waiting to download %1 MB").arg(sizeInMBytes, 0, 'f', 1);
165 buttonColor = QColor(0xcf, 0xcf, 0xcf);
167 infoText = QStringLiteral("%1MB").arg(sizeInMBytes, 0, 'f', 1);
168 if (status == PackageNotInstalled) {
169 buttonText = "Install";
170 } else if (status == PackageUpdateAvailable) {
171 buttonText = "Update";
175 painter->setBrush(Qt::NoBrush);
176 QRect buttonRect = packageButtonRect(option.rect, index);
177 painter->setPen(Qt::NoPen);
178 painter->setBrush(buttonColor);
179 painter->drawRoundedRect(buttonRect, 5, 5);
180 painter->setPen(Qt::white);
181 painter->drawText(buttonRect, Qt::AlignCenter, buttonText);
183 QRect infoTextRect = buttonRect;
184 infoTextRect.setLeft(buttonRect.right() + MARGIN);
185 infoTextRect.setWidth(200);
187 if (status == PackageDownloading) {
188 QRect progressRect = infoTextRect;
189 progressRect.setHeight(6);
190 painter->setPen(QPen(QColor(0xcf, 0xcf, 0xcf), 0));
191 painter->setBrush(Qt::NoBrush);
192 painter->drawRoundedRect(progressRect, 3, 3);
193 infoTextRect.setTop(progressRect.bottom() + 1);
195 QRect progressBarRect = progressRect.marginsRemoved(QMargins(2, 2, 2, 2));
197 progressBarRect.setWidth(static_cast<int>(progressBarRect.width() * downloadFraction));
199 painter->setBrush(QColor(27, 122, 211));
200 painter->setPen(Qt::NoPen);
201 painter->drawRoundedRect(progressBarRect, 2, 2);
204 painter->setPen(Qt::black);
205 painter->drawText(infoTextRect, Qt::AlignLeft | Qt::AlignVCenter, infoText);
206 } // of update / install / download status
209 QSize AircraftItemDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
211 QRect contentRect = option.rect.adjusted(MARGIN, MARGIN, -MARGIN, -MARGIN);
213 const int THUMBNAIL_WIDTH = 172;
214 // don't request the thumbnail here for remote sources. Assume the default
215 //QPixmap thumbnail = index.data(Qt::DecorationRole).value<QPixmap>();
216 //contentRect.setLeft(contentRect.left() + MARGIN + thumbnail.width());
217 contentRect.setLeft(contentRect.left() + MARGIN + THUMBNAIL_WIDTH);
221 QFontMetrics metrics(f);
223 int textHeight = metrics.boundingRect(contentRect, Qt::TextWordWrap,
224 index.data().toString()).height();
227 QFontMetrics smallMetrics(f);
229 QString authors = index.data(AircraftAuthorsRole).toString();
230 if (!authors.isEmpty()) {
231 textHeight += MARGIN;
232 textHeight += smallMetrics.boundingRect(contentRect, Qt::TextWordWrap, authors).height();
235 QString desc = index.data(AircraftLongDescriptionRole).toString();
236 if (!desc.isEmpty()) {
237 textHeight += MARGIN;
238 textHeight += smallMetrics.boundingRect(contentRect, Qt::TextWordWrap, desc).height();
241 if (index.data(AircraftHasRatingsRole).toBool()) {
243 textHeight += 48; // (24px per rating box)
245 // just the button height
246 textHeight += BUTTON_HEIGHT;
249 textHeight = qMax(textHeight, 128);
251 return QSize(option.rect.width(), textHeight + (MARGIN * 2));
254 bool AircraftItemDelegate::eventFilter( QObject*, QEvent* event )
256 if ( event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease )
258 QMouseEvent* me = static_cast< QMouseEvent* >( event );
259 QModelIndex index = m_view->indexAt( me->pos() );
260 int variantCount = index.data(AircraftVariantCountRole).toInt();
261 int variantIndex = index.data(AircraftVariantRole).toInt();
262 QRect vr = m_view->visualRect(index);
264 if ( (event->type() == QEvent::MouseButtonRelease) && (variantCount > 0) )
266 QRect leftCycleRect = leftCycleArrowRect(vr, index),
267 rightCycleRect = rightCycleArrowRect(vr, index);
269 if ((variantIndex > 0) && leftCycleRect.contains(me->pos())) {
270 m_view->model()->setData(index, variantIndex - 1, AircraftVariantRole);
271 emit variantChanged(index);
273 } else if ((variantIndex < variantCount) && rightCycleRect.contains(me->pos())) {
274 m_view->model()->setData(index, variantIndex + 1, AircraftVariantRole);
275 emit variantChanged(index);
280 if ((event->type() == QEvent::MouseButtonRelease) &&
281 packageButtonRect(vr, index).contains(me->pos()))
283 QVariant v = index.data(AircraftPackageStatusRole);
284 AircraftItemStatus status = static_cast<AircraftItemStatus>(v.toInt());
285 if (status == PackageNotInstalled) {
286 emit requestInstall(index);
287 } else if ((status == PackageDownloading) || (status == PackageQueued)) {
288 emit cancelDownload(index);
289 } else if (status == PackageUpdateAvailable) {
290 emit requestInstall(index);
295 } else if ( event->type() == QEvent::MouseMove ) {
296 QMouseEvent* me = static_cast< QMouseEvent* >( event );
297 QModelIndex index = m_view->indexAt( me->pos() );
298 QRect vr = m_view->visualRect(index);
300 if (packageButtonRect(vr, index).contains(me->pos())) {
301 qDebug() << "mouse inside button";
310 QRect AircraftItemDelegate::leftCycleArrowRect(const QRect& visualRect, const QModelIndex& index) const
312 QRect contentRect = visualRect.adjusted(MARGIN, MARGIN, -MARGIN, -MARGIN);
313 QPixmap thumbnail = index.data(Qt::DecorationRole).value<QPixmap>();
314 contentRect.setLeft(contentRect.left() + MARGIN + thumbnail.width());
316 QRect r = contentRect;
317 r.setRight(r.left() + ARROW_SIZE);
318 r.setBottom(r.top() + ARROW_SIZE);
323 QRect AircraftItemDelegate::rightCycleArrowRect(const QRect& visualRect, const QModelIndex& index) const
325 QRect contentRect = visualRect.adjusted(MARGIN, MARGIN, -MARGIN, -MARGIN);
326 QPixmap thumbnail = index.data(Qt::DecorationRole).value<QPixmap>();
327 contentRect.setLeft(contentRect.left() + MARGIN + thumbnail.width());
329 QRect r = contentRect;
330 r.setLeft(r.right() - ARROW_SIZE);
331 r.setBottom(r.top() + ARROW_SIZE);
336 QRect AircraftItemDelegate::packageButtonRect(const QRect& visualRect, const QModelIndex& index) const
338 QRect contentRect = visualRect.adjusted(MARGIN, MARGIN, -MARGIN, -MARGIN);
339 QPixmap thumbnail = index.data(Qt::DecorationRole).value<QPixmap>();
340 contentRect.setLeft(contentRect.left() + MARGIN + thumbnail.width());
342 return QRect(contentRect.left() + ARROW_SIZE, contentRect.bottom() - 24,
343 BUTTON_WIDTH, BUTTON_HEIGHT);
346 void AircraftItemDelegate::drawRating(QPainter* painter, QString label, const QRect& box, int value) const
348 const int DOT_SIZE = 10;
349 const int DOT_MARGIN = 2;
352 dotBox.setLeft(box.right() - (DOT_MARGIN * 6 + DOT_SIZE * 5));
354 painter->setPen(Qt::black);
356 textBox.setRight(dotBox.left() - DOT_MARGIN);
357 painter->drawText(textBox, Qt::AlignVCenter | Qt::AlignRight, label);
359 painter->setPen(Qt::NoPen);
360 QRect dot(dotBox.left() + DOT_MARGIN,
361 dotBox.center().y() - (DOT_SIZE / 2),
364 for (int i=0; i<5; ++i) {
365 painter->setBrush((i < value) ? QColor(0x3f, 0x3f, 0x3f) : QColor(0xaf, 0xaf, 0xaf));
366 painter->drawEllipse(dot);
367 dot.moveLeft(dot.right() + DOT_MARGIN);