]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/imagedelegate.cpp
f7dfa9b1d5603fbb49a0ea8507ea25522845b2fe
[virtualjaguar] / src / gui / imagedelegate.cpp
1 //
2 // imagedelegate.cpp - Qt Model/View rendering class
3 //
4 // by James L. Hammons
5 // (C) 2010 Underground Software
6 //
7 // JLH = James L. Hammons <jlhamm@acm.org>
8 //
9 // Who  When        What
10 // ---  ----------  -------------------------------------------------------------
11 // JLH  02/04/2010  Created this file
12 //
13
14 // This class takes care of rendering items in our custom model in the ListView
15 // class utilized in FilePicker.
16
17 #include "imagedelegate.h"
18
19 #include "filedb.h"
20 //#include "filelistmodel.h"
21
22
23 ImageDelegate::ImageDelegate(QObject * parent): QAbstractItemDelegate(parent), pixelSize(12)
24 {
25 }
26
27 /*
28 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:
29 */
30
31 void ImageDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
32 {
33         if (option.state & QStyle::State_Selected)
34                 painter->fillRect(option.rect, option.palette.highlight());
35
36 /*
37 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.
38
39 The radius of each circle is calculated in the following lines of code:
40 */
41
42 #if 0
43         int size = qMin(option.rect.width(), option.rect.height());
44         int brightness = index.model()->data(index, Qt::DisplayRole).toInt();
45         double radius = (size/2.0) - (brightness/255.0 * size/2.0);
46         if (radius == 0.0)
47                 return;
48 #endif
49
50 /*
51 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.
52 */
53
54         painter->save();
55 #if 0
56         painter->setRenderHint(QPainter::Antialiasing, true);
57         painter->setPen(Qt::NoPen);
58
59 /*
60 We save the painter's state, turn on antialiasing (to obtain smoother curves), and turn off the pen.
61 */
62
63         if (option.state & QStyle::State_Selected)
64                 painter->setBrush(option.palette.highlightedText());
65         else
66                 painter->setBrush(QBrush(Qt::black));
67
68 /*
69 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.
70 */
71
72         painter->drawEllipse(QRectF(option.rect.x() + option.rect.width()/2 - radius,
73                 option.rect.y() + option.rect.height()/2 - radius, 2*radius, 2*radius));
74 #else
75 //      painter->drawPixmap(option.rect.x()+8, option.rect.y()+8, 200, 94, QPixmap(":/res/labels/rayman.jpg"));
76 //      painter->drawPixmap(option.rect.x()+13, option.rect.y()+51, 433/2, 203/2, QPixmap(":/res/labels/rayman.jpg"));
77 //      painter->drawPixmap(option.rect.x(), option.rect.y(), 488/2, 395/2, QPixmap(":/res/cart-blank.png"));
78         painter->drawPixmap(option.rect.x(), option.rect.y(), 488/4, 395/4, QPixmap(":/res/cart-blank.png"));
79 //      unsigned long i = index.model()->data(index, Qt::DisplayRole).toUInt();
80         unsigned long i = index.model()->data(index, Qt::DisplayRole).toUInt();
81         QString filename = index.model()->data(index, Qt::EditRole).toString();
82         QImage label = index.model()->data(index, Qt::DecorationRole).value<QImage>();
83
84 #if 0
85         if (role == Qt::DecorationRole)
86                 return list.at(index.row()).label;
87         else if (role == Qt::DisplayRole)
88                 return (uint)list.at(index.row()).dbIndex;
89         else if (role == Qt::EditRole)
90                 return list.at(index.row()).filename;
91 #endif
92
93 //      if (romList[i].file[0] == 0)
94         if (label.isNull())
95         {
96 //      painter->drawPixmap(option.rect.x()+14, option.rect.y()+50, 433/2, 203/2, QPixmap(":/res/label-blank.png"));
97                 painter->drawPixmap(option.rect.x()+7, option.rect.y()+25, 433/4, 203/4, QPixmap(":/res/label-blank.png"));
98 //Need to query the model for the data we're supposed to draw here...
99 //      painter->drawText(17, 73, QString(romList[i].name));
100 //      painter->setPen(Qt::white);
101                 painter->setPen(QColor(255, 128, 0, 255));
102 //      painter->drawText(QRect(option.rect.x()+20, option.rect.y()+73, 196, 70), Qt::TextWordWrap | Qt::AlignHCenter, QString(romList[i].name));
103                 painter->drawText(QRect(option.rect.x()+10, option.rect.y()+36, 196/2, 70/2), Qt::TextWordWrap | Qt::AlignHCenter, QString(romList[i].name));
104         }
105         else
106         {
107 #if 0
108                 QString filename(romList[i].file);
109                 filename.prepend("./label/");
110                 QImage img(filename);
111                 painter->drawImage(QRect(option.rect.x()+7, option.rect.y()+25, 433/4, 203/4), img);
112 #else
113                 painter->drawImage(QRect(option.rect.x()+7, option.rect.y()+25, 433/4, 203/4), label);
114 #endif
115         }
116 //26x100
117 #endif
118         painter->restore();
119 }
120
121 /*
122 Finally, we paint the circle within the rectangle specified by the style option and we call restore() on the painter.
123
124 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().
125
126 The delegate's sizeHint() function returns a size for the item based on the predefined pixel size, initially set up in the constructor:
127 */
128
129 QSize ImageDelegate::sizeHint(const QStyleOptionViewItem & /* option */, const QModelIndex & /* index */) const
130 {
131         // 488x395 --> blank cart (full size)
132         // 400x188 --> label (full size) 433x203 <-- (actually, it's this)
133
134         // 200x94 is shrunk dimension...
135 //      return QSize(100, 47);
136 //      return QSize(216, 110);
137 //      return QSize(488/2, 395/2);
138         return QSize(488/4, 395/4);
139 }
140
141 /*
142 The delegate's size is updated whenever the pixel size is changed. We provide a custom slot to do this:
143 */
144
145 //void ImageDelegate::setPixelSize(int size)
146 //{
147 //      pixelSize = size;
148 //}