]> git.mxchange.org Git - flightgear.git/blob - src/GUI/AircraftItemDelegate.cxx
Disable testing hacks for package installs.
[flightgear.git] / src / GUI / AircraftItemDelegate.cxx
1 // AircraftItemDelegate.cxx - part of GUI launcher using Qt5
2 //
3 // Written by James Turner, started March 2015.
4 //
5 // Copyright (C) 2014 James Turner <zakalawe@mac.com>
6 //
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.
11 //
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.
16 //
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.
20
21
22 #include "AircraftItemDelegate.hxx"
23
24 #include <QDebug>
25 #include <QPainter>
26 #include <QLinearGradient>
27 #include <QListView>
28 #include <QMouseEvent>
29 #include <QFontMetrics>
30
31 #include "AircraftModel.hxx"
32
33 AircraftItemDelegate::AircraftItemDelegate(QListView* view) :
34     m_view(view)
35 {
36     view->viewport()->installEventFilter(this);
37     view->viewport()->setMouseTracking(true);
38
39     m_leftArrowIcon.load(":/left-arrow-icon");
40     m_rightArrowIcon.load(":/right-arrow-icon");
41 }
42
43 void AircraftItemDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option,
44     const QModelIndex & index) const
45 {
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));
52
53         QBrush backgroundBrush(grad);
54         painter->fillRect(option.rect, backgroundBrush);
55
56         painter->setPen(QColor(90, 107, 131));
57         painter->drawLine(option.rect.topLeft(), option.rect.topRight());
58     }
59
60     QRect contentRect = option.rect.adjusted(MARGIN, MARGIN, -MARGIN, -MARGIN);
61
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);
65
66     // draw 1px frame
67     painter->setPen(QColor(0x7f, 0x7f, 0x7f));
68     painter->setBrush(Qt::NoBrush);
69     painter->drawRect(contentRect.left(), yPos, thumbnail.width(), thumbnail.height());
70
71     // draw bottom dividing line
72     painter->drawLine(contentRect.left(), contentRect.bottom() + MARGIN,
73                       contentRect.right(), contentRect.bottom() + MARGIN);
74
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());
79
80     painter->setPen(Qt::black);
81     QFont f;
82     f.setPointSize(18);
83     painter->setFont(f);
84
85     QRect descriptionRect = contentRect.adjusted(ARROW_SIZE, 0, -ARROW_SIZE, 0),
86         actualBounds;
87
88     if (variantCount > 0) {
89         bool canLeft = (currentVariant > 0);
90         bool canRight =  (currentVariant < variantCount );
91
92         QRect leftArrowRect = leftCycleArrowRect(option.rect, index);
93         if (canLeft) {
94             painter->drawPixmap(leftArrowRect.topLeft() + QPoint(2, 2), m_leftArrowIcon);
95         }
96
97         QRect rightArrowRect = rightCycleArrowRect(option.rect, index);
98         if (canRight) {
99             painter->drawPixmap(rightArrowRect.topLeft() + QPoint(2, 2), m_rightArrowIcon);
100         }
101     }
102
103     painter->drawText(descriptionRect, Qt::TextWordWrap, description, &actualBounds);
104
105     QString authors = index.data(AircraftAuthorsRole).toString();
106
107     f.setPointSize(12);
108     painter->setFont(f);
109
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),
115                           &actualBounds);
116     }
117
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);
124     }
125
126     QRect r = contentRect;
127     r.setWidth(contentRect.width() / 3);
128     r.moveTop(actualBounds.bottom() + MARGIN);
129     r.moveLeft(r.right());
130     r.setHeight(24);
131
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());
136
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());
142     }
143
144     QVariant v = index.data(AircraftPackageStatusRole);
145     AircraftItemStatus status = static_cast<AircraftItemStatus>(v.toInt());
146    // status = PackageNotInstalled;
147     if (status != PackageInstalled) {
148         painter->setBrush(Qt::NoBrush);
149         QRect buttonRect = packageButtonRect(option.rect, index);
150         painter->setPen(Qt::NoPen);
151         painter->setBrush(QColor(27, 122, 211));
152         painter->drawRoundedRect(buttonRect, 5, 5);
153         painter->setPen(Qt::white);
154
155         if (status == PackageNotInstalled) {
156             painter->drawText(buttonRect, Qt::AlignCenter, "Install");
157         } else if (status == PackageUpdateAvailable) {
158             painter->drawText(buttonRect, Qt::AlignCenter, "Update");
159         }
160     }
161 }
162
163 QSize AircraftItemDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
164 {
165     QRect contentRect = option.rect.adjusted(MARGIN, MARGIN, -MARGIN, -MARGIN);
166     QPixmap thumbnail = index.data(Qt::DecorationRole).value<QPixmap>();
167     contentRect.setLeft(contentRect.left() + MARGIN + thumbnail.width());
168
169     QFont f;
170     f.setPointSize(18);
171     QFontMetrics metrics(f);
172
173     int textHeight = metrics.boundingRect(contentRect, Qt::TextWordWrap,
174                                           index.data().toString()).height();
175
176     f.setPointSize(12);
177     QFontMetrics smallMetrics(f);
178
179     QString authors = index.data(AircraftAuthorsRole).toString();
180     if (!authors.isEmpty()) {
181         textHeight += MARGIN;
182         textHeight += smallMetrics.boundingRect(contentRect, Qt::TextWordWrap, authors).height();
183     }
184
185     QString desc = index.data(AircraftLongDescriptionRole).toString();
186     if (!desc.isEmpty()) {
187         textHeight += MARGIN;
188         textHeight += smallMetrics.boundingRect(contentRect, Qt::TextWordWrap, desc).height();
189     }
190
191     if (index.data(AircraftHasRatingsRole).toBool()) {
192         // ratings
193         textHeight += 48; // (24px per rating box)
194     } else {
195         // just the button height
196         textHeight += BUTTON_HEIGHT;
197     }
198
199     textHeight = qMax(textHeight, 128);
200
201     return QSize(option.rect.width(), textHeight + (MARGIN * 2));
202 }
203
204 bool AircraftItemDelegate::eventFilter( QObject*, QEvent* event )
205 {
206     if ( event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease )
207     {
208         QMouseEvent* me = static_cast< QMouseEvent* >( event );
209         QModelIndex index = m_view->indexAt( me->pos() );
210         int variantCount = index.data(AircraftVariantCountRole).toInt();
211         int variantIndex = index.data(AircraftVariantRole).toInt();
212
213         if ( (event->type() == QEvent::MouseButtonRelease) && (variantCount > 0) )
214         {
215             QRect vr = m_view->visualRect(index);
216             QRect leftCycleRect = leftCycleArrowRect(vr, index),
217                 rightCycleRect = rightCycleArrowRect(vr, index);
218
219             if ((variantIndex > 0) && leftCycleRect.contains(me->pos())) {
220                 m_view->model()->setData(index, variantIndex - 1, AircraftVariantRole);
221                 emit variantChanged(index);
222                 return true;
223             } else if ((variantIndex < variantCount) && rightCycleRect.contains(me->pos())) {
224                 m_view->model()->setData(index, variantIndex + 1, AircraftVariantRole);
225                 emit variantChanged(index);
226                 return true;
227             }
228         }
229     } else if ( event->type() == QEvent::MouseMove ) {
230         QMouseEvent* me = static_cast< QMouseEvent* >( event );
231         QModelIndex index = m_view->indexAt( me->pos() );
232         QRect vr = m_view->visualRect(index);
233
234         if (packageButtonRect(vr, index).contains(me->pos())) {
235             qDebug() << "mouse inside button";
236         }
237
238     }
239     
240     return false;
241 }
242
243
244 QRect AircraftItemDelegate::leftCycleArrowRect(const QRect& visualRect, const QModelIndex& index) const
245 {
246     QRect contentRect = visualRect.adjusted(MARGIN, MARGIN, -MARGIN, -MARGIN);
247     QPixmap thumbnail = index.data(Qt::DecorationRole).value<QPixmap>();
248     contentRect.setLeft(contentRect.left() + MARGIN + thumbnail.width());
249
250     QRect r = contentRect;
251     r.setRight(r.left() + ARROW_SIZE);
252     r.setBottom(r.top() + ARROW_SIZE);
253     return r;
254
255 }
256
257 QRect AircraftItemDelegate::rightCycleArrowRect(const QRect& visualRect, const QModelIndex& index) const
258 {
259     QRect contentRect = visualRect.adjusted(MARGIN, MARGIN, -MARGIN, -MARGIN);
260     QPixmap thumbnail = index.data(Qt::DecorationRole).value<QPixmap>();
261     contentRect.setLeft(contentRect.left() + MARGIN + thumbnail.width());
262
263     QRect r = contentRect;
264     r.setLeft(r.right() - ARROW_SIZE);
265     r.setBottom(r.top() + ARROW_SIZE);
266     return r;
267
268 }
269
270 QRect AircraftItemDelegate::packageButtonRect(const QRect& visualRect, const QModelIndex& index) const
271 {
272     QRect contentRect = visualRect.adjusted(MARGIN, MARGIN, -MARGIN, -MARGIN);
273     QPixmap thumbnail = index.data(Qt::DecorationRole).value<QPixmap>();
274     contentRect.setLeft(contentRect.left() + MARGIN + thumbnail.width());
275
276     return QRect(contentRect.left() + ARROW_SIZE, contentRect.bottom() - 24, 60, BUTTON_HEIGHT);
277 }
278
279 void AircraftItemDelegate::drawRating(QPainter* painter, QString label, const QRect& box, int value) const
280 {
281     const int DOT_SIZE = 10;
282     const int DOT_MARGIN = 2;
283
284     QRect dotBox = box;
285     dotBox.setLeft(box.right() - (DOT_MARGIN * 6 + DOT_SIZE * 5));
286
287     painter->setPen(Qt::black);
288     QRect textBox = box;
289     textBox.setRight(dotBox.left() - DOT_MARGIN);
290     painter->drawText(textBox, Qt::AlignVCenter | Qt::AlignRight, label);
291
292     painter->setPen(Qt::NoPen);
293     QRect dot(dotBox.left() + DOT_MARGIN,
294               dotBox.center().y() - (DOT_SIZE / 2),
295               DOT_SIZE,
296               DOT_SIZE);
297     for (int i=0; i<5; ++i) {
298         painter->setBrush((i < value) ? QColor(0x3f, 0x3f, 0x3f) : QColor(0xaf, 0xaf, 0xaf));
299         painter->drawEllipse(dot);
300         dot.moveLeft(dot.right() + DOT_MARGIN);
301     }
302 }