@@ -46,145 +46,145 @@ void QgsDetailedItemDelegate::paint(QPainter * thepPainter,
46
46
const QStyleOptionViewItem & theOption,
47
47
const QModelIndex & theIndex) const
48
48
{
49
+ // After painting we need to restore the painter to its original state
50
+ thepPainter->save ();
49
51
if (qVariantCanConvert<QgsDetailedItemData>(theIndex.data (Qt::UserRole)))
50
52
{
51
53
QgsDetailedItemData myData =
52
54
qVariantValue<QgsDetailedItemData>(theIndex.data (Qt::UserRole));
53
55
bool myCheckState = theIndex.model ()->data (theIndex, Qt::CheckStateRole).toBool ();
54
- mpWidget->setChecked (myCheckState);
55
- mpWidget->setData (myData);
56
- mpWidget->resize (theOption.rect .width (),mpWidget->height ());
57
- mpWidget->setAutoFillBackground (false );
58
- mpWidget->repaint ();
59
-
60
- if (theOption.state & QStyle::State_Selected)
56
+ if (myData.isRenderedAsWidget ())
61
57
{
62
- QColor myColor1 = theOption.palette .highlight ();
63
- QColor myColor2 = myColor1;
64
- myColor2 = myColor2.lighter (110 ); // 10% lighter
65
- QLinearGradient myGradient (QPointF (0 ,theOption.rect .y ()),
66
- QPointF (0 ,theOption.rect .y () + mpWidget->height ()));
67
- myGradient.setColorAt (0 , myColor1);
68
- myGradient.setColorAt (0.1 , myColor2);
69
- myGradient.setColorAt (0.5 , myColor1);
70
- myGradient.setColorAt (0.9 , myColor2);
71
- myGradient.setColorAt (1 , myColor1);
72
- thepPainter->fillRect (theOption.rect , QBrush (myGradient));
73
- }
74
- QPixmap myPixmap = QPixmap::grabWidget (mpWidget);
75
- thepPainter->drawPixmap (theOption.rect .x (),
76
- theOption.rect .y (),
77
- myPixmap);
78
- }
79
- else
80
- {
81
- // After painting we need to restore the painter to its original state
82
- thepPainter->save ();
83
- //
84
- // Get the strings and check box properties
85
- //
86
- QString myString = theIndex.model ()->data (theIndex, Qt::DisplayRole).toString ();
87
- QString myDetailString = theIndex.model ()->data (theIndex, Qt::UserRole).toString ();
88
- bool myCheckState = theIndex.model ()->data (theIndex, Qt::CheckStateRole).toBool ();
89
- mpCheckBox->setChecked (myCheckState);
90
- QPixmap myCbxPixmap (mpCheckBox->size ());
91
- mpCheckBox->render (&myCbxPixmap); // we will draw this onto the widget further down
92
- QPixmap myDecoPixmap;
58
+ mpWidget->setChecked (myCheckState);
59
+ mpWidget->setData (myData);
60
+ mpWidget->resize (theOption.rect .width (),mpWidget->height ());
61
+ mpWidget->setAutoFillBackground (false );
62
+ mpWidget->repaint ();
93
63
94
- //
95
- // Calculate the widget height and other metrics
96
- //
97
- QFont myFont = theOption.font ;
98
- QFont myTitleFont = myFont;
99
- myTitleFont.setBold (true );
100
- myTitleFont.setPointSize (myFont.pointSize () + 3 );
101
- QFontMetrics myTitleMetrics (myTitleFont);
102
- QFontMetrics myDetailMetrics (myFont);
103
- int myVerticalSpacer = 3 ; // spacing between title and description
104
- int myHorizontalSpacer = 5 ; // spacing between checkbox / icon and description
105
- int myTextStartX = theOption.rect .x () + myHorizontalSpacer;
106
- int myTextStartY= theOption.rect .y () + myVerticalSpacer;
107
- int myHeight = myTitleMetrics.height () + myVerticalSpacer;
108
-
109
- //
110
- // Draw the item background with a gradient if its highlighted
111
- //
112
- if (theOption.state & QStyle::State_Selected)
64
+ if (theOption.state & QStyle::State_Selected)
65
+ {
66
+ QColor myColor1 = theOption.palette .highlight ();
67
+ QColor myColor2 = myColor1;
68
+ myColor2 = myColor2.lighter (110 ); // 10% lighter
69
+ QLinearGradient myGradient (QPointF (0 ,theOption.rect .y ()),
70
+ QPointF (0 ,theOption.rect .y () + mpWidget->height ()));
71
+ myGradient.setColorAt (0 , myColor1);
72
+ myGradient.setColorAt (0.1 , myColor2);
73
+ myGradient.setColorAt (0.5 , myColor1);
74
+ myGradient.setColorAt (0.9 , myColor2);
75
+ myGradient.setColorAt (1 , myColor1);
76
+ thepPainter->fillRect (theOption.rect , QBrush (myGradient));
77
+ }
78
+ QPixmap myPixmap = QPixmap::grabWidget (mpWidget);
79
+ thepPainter->drawPixmap (theOption.rect .x (),
80
+ theOption.rect .y (),
81
+ myPixmap);
82
+ } // render as widget
83
+ else // render by manually painting
113
84
{
114
- QColor myColor1 = theOption.palette .highlight ();
115
- QColor myColor2 = myColor1;
116
- myColor2 = myColor2.lighter (110 ); // 10% lighter
85
+ //
86
+ // Get the strings and check box properties
87
+ //
88
+ bool myCheckState = theIndex.model ()->data (theIndex, Qt::CheckStateRole).toBool ();
89
+ mpCheckBox->setChecked (myCheckState);
90
+ QPixmap myCbxPixmap (mpCheckBox->size ());
91
+ mpCheckBox->render (&myCbxPixmap); // we will draw this onto the widget further down
92
+
93
+ //
94
+ // Calculate the widget height and other metrics
95
+ //
96
+ QFont myFont = theOption.font ;
97
+ QFont myTitleFont = myFont;
98
+ myTitleFont.setBold (true );
99
+ myTitleFont.setPointSize (myFont.pointSize () + 3 );
100
+ QFontMetrics myTitleMetrics (myTitleFont);
101
+ QFontMetrics myDetailMetrics (myFont);
102
+ int myVerticalSpacer = 3 ; // spacing between title and description
103
+ int myHorizontalSpacer = 5 ; // spacing between checkbox / icon and description
104
+ int myTextStartX = theOption.rect .x () + myHorizontalSpacer;
105
+ int myTextStartY= theOption.rect .y () + myVerticalSpacer;
117
106
int myHeight = myTitleMetrics.height () + myVerticalSpacer;
118
- QLinearGradient myGradient (QPointF (0 ,theOption.rect .y ()),
119
- QPointF (0 ,theOption.rect .y () + myHeight*2 ));
120
- myGradient.setColorAt (0 , myColor1);
121
- myGradient.setColorAt (0.1 , myColor2);
122
- myGradient.setColorAt (0.5 , myColor1);
123
- myGradient.setColorAt (0.9 , myColor2);
124
- myGradient.setColorAt (1 , myColor2);
125
- thepPainter->fillRect (theOption.rect , QBrush (myGradient));
126
- }
127
107
128
- //
129
- // Draw the checkbox
130
- //
131
- bool myCheckableFlag = true ;
132
- if (theIndex.flags () == Qt::ItemIsUserCheckable)
133
- {
134
- myCheckableFlag = false ;
135
- }
136
- if (myCheckableFlag)
137
- {
138
- thepPainter->drawPixmap (theOption.rect .x (),
139
- theOption.rect .y () + mpCheckBox->height (),
140
- myCbxPixmap);
141
- myTextStartX = theOption.rect .x () + myCbxPixmap.width () + myHorizontalSpacer;
142
- }
143
- //
144
- // Draw the decoration (pixmap)
145
- //
146
- bool myIconFlag = false ;
147
- if (!theIndex.model ()->data (theIndex, Qt::DecorationRole).isNull ())
148
- {
149
- myDecoPixmap = theIndex.model ()->data (theIndex, Qt::DecorationRole).value <QPixmap>();
150
- thepPainter->drawPixmap (myTextStartX,
151
- myTextStartY + (myDecoPixmap.height () / 2 ),
152
- myDecoPixmap);
153
- myTextStartX += myDecoPixmap.width () + myHorizontalSpacer;
154
- }
155
- //
156
- // Draw the title
157
- //
158
- myTextStartY += myHeight/2 ;
159
- thepPainter->setFont (myTitleFont);
160
- thepPainter->drawText ( myTextStartX ,
161
- myTextStartY ,
162
- myString);
163
- //
164
- // Draw the description with word wrapping if needed
165
- //
166
- thepPainter->setFont (myFont); // return to original font set by client
167
- if (myIconFlag)
168
- {
169
- myTextStartY += myVerticalSpacer;
170
- }
171
- else
172
- {
173
- myTextStartY += myDetailMetrics.height () + myVerticalSpacer;
174
- }
175
- QStringList myList =
176
- wordWrap ( myDetailString, myDetailMetrics, theOption.rect .width () - myTextStartX );
177
- QStringListIterator myLineWrapIterator (myList);
178
- while (myLineWrapIterator.hasNext ())
179
- {
180
- QString myLine = myLineWrapIterator.next ();
181
- thepPainter->drawText ( myTextStartX,
182
- myTextStartY,
183
- myLine);
184
- myTextStartY += myDetailMetrics.height () - myVerticalSpacer;
185
- }
186
- thepPainter->restore ();
187
- }
108
+ //
109
+ // Draw the item background with a gradient if its highlighted
110
+ //
111
+ if (theOption.state & QStyle::State_Selected)
112
+ {
113
+ QColor myColor1 = theOption.palette .highlight ();
114
+ QColor myColor2 = myColor1;
115
+ myColor2 = myColor2.lighter (110 ); // 10% lighter
116
+ int myHeight = myTitleMetrics.height () + myVerticalSpacer;
117
+ QLinearGradient myGradient (QPointF (0 ,theOption.rect .y ()),
118
+ QPointF (0 ,theOption.rect .y () + myHeight*2 ));
119
+ myGradient.setColorAt (0 , myColor1);
120
+ myGradient.setColorAt (0.1 , myColor2);
121
+ myGradient.setColorAt (0.5 , myColor1);
122
+ myGradient.setColorAt (0.9 , myColor2);
123
+ myGradient.setColorAt (1 , myColor2);
124
+ thepPainter->fillRect (theOption.rect , QBrush (myGradient));
125
+ }
126
+
127
+ //
128
+ // Draw the checkbox
129
+ //
130
+ bool myCheckableFlag = true ;
131
+ if (theIndex.flags () == Qt::ItemIsUserCheckable)
132
+ {
133
+ myCheckableFlag = false ;
134
+ }
135
+ if (myCheckableFlag)
136
+ {
137
+ thepPainter->drawPixmap (theOption.rect .x (),
138
+ theOption.rect .y () + mpCheckBox->height (),
139
+ myCbxPixmap);
140
+ myTextStartX = theOption.rect .x () + myCbxPixmap.width () + myHorizontalSpacer;
141
+ }
142
+ //
143
+ // Draw the decoration (pixmap)
144
+ //
145
+ bool myIconFlag = false ;
146
+ QPixmap myDecoPixmap = myData.icon ();
147
+ if (!myDecoPixmap.isNull ())
148
+ {
149
+ thepPainter->drawPixmap (myTextStartX,
150
+ myTextStartY + (myDecoPixmap.height () / 2 ),
151
+ myDecoPixmap);
152
+ myTextStartX += myDecoPixmap.width () + myHorizontalSpacer;
153
+ }
154
+ //
155
+ // Draw the title
156
+ //
157
+ myTextStartY += myHeight/2 ;
158
+ thepPainter->setFont (myTitleFont);
159
+ thepPainter->drawText ( myTextStartX ,
160
+ myTextStartY ,
161
+ myData.title ());
162
+ //
163
+ // Draw the description with word wrapping if needed
164
+ //
165
+ thepPainter->setFont (myFont); // return to original font set by client
166
+ if (myIconFlag)
167
+ {
168
+ myTextStartY += myVerticalSpacer;
169
+ }
170
+ else
171
+ {
172
+ myTextStartY += myDetailMetrics.height () + myVerticalSpacer;
173
+ }
174
+ QStringList myList =
175
+ wordWrap ( myData.detail (), myDetailMetrics, theOption.rect .width () - myTextStartX );
176
+ QStringListIterator myLineWrapIterator (myList);
177
+ while (myLineWrapIterator.hasNext ())
178
+ {
179
+ QString myLine = myLineWrapIterator.next ();
180
+ thepPainter->drawText ( myTextStartX,
181
+ myTextStartY,
182
+ myLine);
183
+ myTextStartY += myDetailMetrics.height () - myVerticalSpacer;
184
+ }
185
+ } // render by manual painting
186
+ } // can convert item data
187
+ thepPainter->restore ();
188
188
}
189
189
190
190
QSize QgsDetailedItemDelegate::sizeHint (
0 commit comments