Commit | Line | Data |
---|---|---|
cf76e892 JPM |
1 | // |
2 | // imagedelegate.cpp - Qt Model/View rendering class | |
3 | // | |
4 | // by James Hammons | |
5 | // (C) 2010 Underground Software | |
6 | // | |
7 | // JLH = James Hammons <jlhamm@acm.org> | |
8 | // JPM = Jean-Paul Mari <djipi.mari@gmail.com> | |
9 | ||
10 | // | |
11 | // Who When What | |
12 | // --- ---------- ------------------------------------------------------------- | |
13 | // JLH 02/04/2010 Created this file | |
14 | // JPM 06/06/2016 Visual Studio support | |
15 | // | |
16 | ||
17 | // This class takes care of rendering items in our custom model in the ListView | |
18 | // class utilized in FilePicker. | |
19 | ||
20 | #include "imagedelegate.h" | |
21 | ||
22 | #include "filedb.h" | |
23 | #include "filelistmodel.h" | |
24 | ||
25 | //ImageDelegate::ImageDelegate(QObject * parent): QAbstractItemDelegate(parent)//, pixelSize(12) | |
26 | //{ | |
27 | //} | |
28 | ||
29 | ImageDelegate::ImageDelegate() | |
30 | { | |
31 | QImage cartImg(":/res/cart-blank.png"); | |
32 | QPainter painter(&cartImg); | |
33 | painter.drawPixmap(23, 87, QPixmap(":/res/label-blank.png")); | |
34 | painter.end(); | |
35 | cartSmall = cartImg.scaled(488/4, 395/4, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); | |
36 | } | |
37 | ||
38 | /* | |
39 | Each item is rendered by the delegate's paint() function. The view calls this function with a ready-to-use QPainter object, style information that the delegate should use to correctly draw the item, and an index to the item in the model: | |
40 | */ | |
41 | ||
42 | void ImageDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const | |
43 | { | |
44 | if (option.state & QStyle::State_Selected) | |
45 | painter->fillRect(option.rect, option.palette.highlight()); | |
46 | ||
47 | /* | |
48 | The first task the delegate has to perform is to draw the item's background correctly. Usually, selected items appear differently to non-selected items, so we begin by testing the state passed in the style option and filling the background if necessary. | |
49 | ||
50 | The radius of each circle is calculated in the following lines of code: | |
51 | */ | |
52 | ||
53 | #if 0 | |
54 | int size = qMin(option.rect.width(), option.rect.height()); | |
55 | int brightness = index.model()->data(index, Qt::DisplayRole).toInt(); | |
56 | double radius = (size/2.0) - (brightness/255.0 * size/2.0); | |
57 | if (radius == 0.0) | |
58 | return; | |
59 | #endif | |
60 | ||
61 | /* | |
62 | First, the largest possible radius of the circle is determined by taking the smallest dimension of the style option's rect attribute. Using the model index supplied, we obtain a value for the brightness of the relevant pixel in the image. The radius of the circle is calculated by scaling the brightness to fit within the item and subtracting it from the largest possible radius. | |
63 | */ | |
64 | ||
65 | painter->save(); | |
66 | #if 0 | |
67 | painter->setRenderHint(QPainter::Antialiasing, true); | |
68 | painter->setPen(Qt::NoPen); | |
69 | ||
70 | /* | |
71 | We save the painter's state, turn on antialiasing (to obtain smoother curves), and turn off the pen. | |
72 | */ | |
73 | ||
74 | if (option.state & QStyle::State_Selected) | |
75 | painter->setBrush(option.palette.highlightedText()); | |
76 | else | |
77 | painter->setBrush(QBrush(Qt::black)); | |
78 | ||
79 | /* | |
80 | The foreground of the item (the circle representing a pixel) must be rendered using an appropriate brush. For unselected items, we will use a solid black brush; selected items are drawn using a predefined brush from the style option's palette. | |
81 | */ | |
82 | ||
83 | painter->drawEllipse(QRectF(option.rect.x() + option.rect.width()/2 - radius, | |
84 | option.rect.y() + option.rect.height()/2 - radius, 2*radius, 2*radius)); | |
85 | #else | |
86 | // painter->drawPixmap(option.rect.x()+8, option.rect.y()+8, 200, 94, QPixmap(":/res/labels/rayman.jpg")); | |
87 | // painter->drawPixmap(option.rect.x()+13, option.rect.y()+51, 433/2, 203/2, QPixmap(":/res/labels/rayman.jpg")); | |
88 | // painter->drawPixmap(option.rect.x(), option.rect.y(), 488/2, 395/2, QPixmap(":/res/cart-blank.png")); | |
89 | ||
90 | ||
91 | // This is crappy. We really should have a properly scaled image ready to go so we | |
92 | // don't get Qt's default ugly looking fast scaling... | |
93 | ||
94 | #ifdef _MSC_VER | |
95 | #pragma message("Warning: !!! FIX !!! Need to create properly scaled down cart/label images!") | |
96 | #else | |
97 | #warning "!!! FIX !!! Need to create properly scaled down cart/label images!" | |
98 | #endif // _MSC_VER | |
99 | //We've got the carts, now just need to do the labels... | |
100 | ||
101 | unsigned long i = index.model()->data(index, FLM_INDEX).toUInt(); | |
102 | QString filename = index.model()->data(index, FLM_FILENAME).toString(); | |
103 | QImage label = index.model()->data(index, FLM_LABEL).value<QImage>(); | |
104 | QString nameToDraw; | |
105 | ||
106 | if (i == 0xFFFFFFFF) // Not found... | |
107 | { | |
108 | int lastSlashPos = filename.lastIndexOf('/'); | |
109 | nameToDraw = "\"" + filename.mid(lastSlashPos + 1) + "\""; | |
110 | } | |
111 | else | |
112 | nameToDraw = romList[i].name; | |
113 | ||
114 | if (label.isNull()) | |
115 | { | |
116 | // painter->drawPixmap(option.rect.x()+14, option.rect.y()+50, 433/2, 203/2, QPixmap(":/res/label-blank.png")); | |
117 | // painter->drawPixmap(option.rect.x()+7, option.rect.y()+25, 433/4, 203/4, QPixmap(":/res/label-blank.png")); | |
118 | painter->drawImage(option.rect.x() + 2, option.rect.y() + 2, cartSmall); | |
119 | //Need to query the model for the data we're supposed to draw here... | |
120 | // painter->drawText(17, 73, QString(romList[i].name)); | |
121 | // painter->setPen(Qt::white); | |
122 | painter->setPen(QColor(255, 128, 0, 255)); | |
123 | // painter->drawText(QRect(option.rect.x()+20, option.rect.y()+73, 196, 70), Qt::TextWordWrap | Qt::AlignHCenter, QString(romList[i].name)); | |
124 | painter->drawText(QRect(option.rect.x()+10, option.rect.y()+36, 196/2, 70/2), | |
125 | Qt::TextWordWrap | Qt::AlignHCenter, nameToDraw); | |
126 | } | |
127 | else | |
128 | { | |
129 | #if 0 | |
130 | QString filename(romList[i].file); | |
131 | filename.prepend("./label/"); | |
132 | QImage img(filename); | |
133 | painter->drawImage(QRect(option.rect.x()+7, option.rect.y()+25, 433/4, 203/4), img); | |
134 | #else | |
135 | painter->drawPixmap(option.rect.x() + 2, option.rect.y() + 2, 488/4, 395/4, QPixmap(":/res/cart-blank.png")); | |
136 | painter->drawImage(QRect(option.rect.x()+2+7, option.rect.y()+2+25, 433/4, 203/4), label); | |
137 | #endif | |
138 | } | |
139 | //26x100 | |
140 | #endif | |
141 | painter->restore(); | |
142 | } | |
143 | ||
144 | /* | |
145 | Finally, we paint the circle within the rectangle specified by the style option and we call restore() on the painter. | |
146 | ||
147 | The paint() function does not have to be particularly complicated; it is only necessary to ensure that the state of the painter when the function returns is the same as it was when it was called. This usually means that any transformations applied to the painter must be preceded by a call to QPainter::save() and followed by a call to QPainter::restore(). | |
148 | ||
149 | The delegate's sizeHint() function returns a size for the item based on the predefined pixel size, initially set up in the constructor: | |
150 | */ | |
151 | ||
152 | QSize ImageDelegate::sizeHint(const QStyleOptionViewItem & /* option */, const QModelIndex & /* index */) const | |
153 | { | |
154 | // 488x395 --> blank cart (full size) | |
155 | // 400x188 --> label (full size) 433x203 <-- (actually, it's this) | |
156 | ||
157 | // 200x94 is shrunk dimension... | |
158 | // return QSize(100, 47); | |
159 | // return QSize(216, 110); | |
160 | // return QSize(488/2, 395/2); | |
161 | return QSize((488/4) + 4, (395/4) + 4); | |
162 | } | |
163 | ||
164 | /* | |
165 | The delegate's size is updated whenever the pixel size is changed. We provide a custom slot to do this: | |
166 | */ | |
167 | ||
168 | //void ImageDelegate::setPixelSize(int size) | |
169 | //{ | |
170 | // pixelSize = size; | |
171 | //} |