@@ -33,7 +33,9 @@ QgsDetailedItemDelegate::QgsDetailedItemDelegate(QObject * parent) :
33
33
34
34
{
35
35
// mpWidget->setFixedHeight(80);
36
- mpCheckBox->resize (16 ,16 );
36
+ mpCheckBox->resize (mpCheckBox->sizeHint ().height (),mpCheckBox->sizeHint ().height ());
37
+ setVerticalSpacing (3 );
38
+ setHorizontalSpacing (5 );
37
39
}
38
40
39
41
QgsDetailedItemDelegate::~QgsDetailedItemDelegate ()
@@ -55,138 +57,18 @@ void QgsDetailedItemDelegate::paint(QPainter * thepPainter,
55
57
bool myCheckState = theIndex.model ()->data (theIndex, Qt::CheckStateRole).toBool ();
56
58
if (myData.isRenderedAsWidget ())
57
59
{
58
- mpWidget->setChecked (myCheckState);
59
- mpWidget->setData (myData);
60
- mpWidget->resize (theOption.rect .width (),mpWidget->height ());
61
- mpWidget->setAutoFillBackground (false );
62
- mpWidget->repaint ();
63
-
64
- if (theOption.state & QStyle::State_Selected)
65
- {
66
- QColor myColor1 = theOption.palette .highlight ().color ();
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
60
+ paintAsWidget (thepPainter,theOption,myData);
61
+ }
83
62
else // render by manually painting
84
63
{
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;
106
- int myHeight = myTitleMetrics.height () + myVerticalSpacer;
107
-
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 ().color ();
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
64
+ paintManually (thepPainter,theOption,myData);
65
+ }
186
66
} // can convert item data
187
67
thepPainter->restore ();
188
68
}
189
69
70
+
71
+
190
72
QSize QgsDetailedItemDelegate::sizeHint (
191
73
const QStyleOptionViewItem & theOption,
192
74
const QModelIndex & theIndex ) const
@@ -201,22 +83,8 @@ QSize QgsDetailedItemDelegate::sizeHint(
201
83
}
202
84
else // fall back to hand calculated & hand drawn item
203
85
{
204
- QFont myFont = theOption.font ;
205
- QFont myTitleFont = myFont;
206
- myTitleFont.setBold (true );
207
- myTitleFont.setPointSize (myFont.pointSize () + 3 );
208
- QFontMetrics myTitleMetrics (myTitleFont);
209
- QFontMetrics myDetailMetrics (myFont);
210
- int myVerticalSpacer = 3 ; // spacing between title and description
211
- int myHorizontalSpacer = 5 ; // spacing between checkbox / icon and description
212
- int myHeight = myTitleMetrics.height () + myVerticalSpacer;
213
- QString myDetailString = theIndex.model ()->data (theIndex, Qt::UserRole).toString ();
214
- QStringList myList = wordWrap ( myDetailString,
215
- myDetailMetrics,
216
- theOption.rect .width () - (mpCheckBox->width () + myHorizontalSpacer));
217
- myHeight += (myList.count () + 1 ) * (myDetailMetrics.height () - myVerticalSpacer);
218
86
// for some reason itmes are non selectable if using rect.width() on osx and win
219
- return QSize (50 , myHeight + myVerticalSpacer );
87
+ return QSize (50 , height (theOption,myData) );
220
88
// return QSize(theOption.rect.width(), myHeight + myVerticalSpacer);
221
89
}
222
90
}
@@ -226,6 +94,162 @@ QSize QgsDetailedItemDelegate::sizeHint(
226
94
}
227
95
}
228
96
97
+ void QgsDetailedItemDelegate::paintManually (QPainter * thepPainter,
98
+ const QStyleOptionViewItem & theOption,
99
+ const QgsDetailedItemData theData) const
100
+ {
101
+ //
102
+ // Get the strings and check box properties
103
+ //
104
+ // bool myCheckState = theIndex.model()->data(theIndex, Qt::CheckStateRole).toBool();
105
+ mpCheckBox->setChecked (theData.isChecked ());
106
+ QPixmap myCbxPixmap (mpCheckBox->size ());
107
+ mpCheckBox->render (&myCbxPixmap); // we will draw this onto the widget further down
108
+
109
+ //
110
+ // Calculate the widget height and other metrics
111
+ //
112
+
113
+ QFontMetrics myTitleMetrics (titleFont (theOption));
114
+ QFontMetrics myDetailMetrics (detailFont (theOption));
115
+ int myTextStartX = theOption.rect .x () + horizontalSpacing ();
116
+ int myTextStartY= theOption.rect .y () + verticalSpacing ();
117
+ int myHeight = myTitleMetrics.height () + verticalSpacing ();
118
+
119
+ //
120
+ // Draw the item background with a gradient if its highlighted
121
+ //
122
+ if (theOption.state & QStyle::State_Selected)
123
+ {
124
+ drawHighlight (theOption,thepPainter, height (theOption, theData));
125
+ }
126
+
127
+ //
128
+ // Draw the checkbox
129
+ //
130
+ if (theData.isCheckable ())
131
+ {
132
+ thepPainter->drawPixmap (theOption.rect .x (),
133
+ theOption.rect .y () + mpCheckBox->height (),
134
+ myCbxPixmap);
135
+ myTextStartX = theOption.rect .x () + myCbxPixmap.width () + horizontalSpacing ();
136
+ }
137
+ //
138
+ // Draw the decoration (pixmap)
139
+ //
140
+ bool myIconFlag = false ;
141
+ QPixmap myDecoPixmap = theData.icon ();
142
+ if (!myDecoPixmap.isNull ())
143
+ {
144
+ thepPainter->drawPixmap (myTextStartX,
145
+ myTextStartY + (myDecoPixmap.height () / 2 ),
146
+ myDecoPixmap);
147
+ myTextStartX += myDecoPixmap.width () + horizontalSpacing ();
148
+ }
149
+ //
150
+ // Draw the title
151
+ //
152
+ myTextStartY += myHeight/2 ;
153
+ thepPainter->setFont (titleFont (theOption));
154
+ thepPainter->drawText ( myTextStartX ,
155
+ myTextStartY ,
156
+ theData.title ());
157
+ //
158
+ // Draw the description with word wrapping if needed
159
+ //
160
+ thepPainter->setFont (detailFont (theOption)); // return to original font set by client
161
+ if (myIconFlag)
162
+ {
163
+ myTextStartY += verticalSpacing ();
164
+ }
165
+ else
166
+ {
167
+ myTextStartY += myDetailMetrics.height () + verticalSpacing ();
168
+ }
169
+ QStringList myList =
170
+ wordWrap ( theData.detail (), myDetailMetrics, theOption.rect .width () - myTextStartX );
171
+ QStringListIterator myLineWrapIterator (myList);
172
+ while (myLineWrapIterator.hasNext ())
173
+ {
174
+ QString myLine = myLineWrapIterator.next ();
175
+ thepPainter->drawText ( myTextStartX,
176
+ myTextStartY,
177
+ myLine);
178
+ myTextStartY += myDetailMetrics.height () - verticalSpacing ();
179
+ }
180
+ } // render by manual painting
181
+
182
+
183
+ void QgsDetailedItemDelegate::paintAsWidget (QPainter * thepPainter,
184
+ const QStyleOptionViewItem & theOption,
185
+ const QgsDetailedItemData theData) const
186
+ {
187
+ QApplication::style ()->drawPrimitive (QStyle::PE_PanelItemViewItem, &theOption, thepPainter, 0 );
188
+ mpWidget->setChecked (theData.isChecked ());
189
+ mpWidget->setData (theData);
190
+ mpWidget->resize (theOption.rect .width (),mpWidget->height ());
191
+ mpWidget->setAutoFillBackground (true );
192
+ // mpWidget->setAttribute(Qt::WA_OpaquePaintEvent);
193
+ mpWidget->repaint ();
194
+
195
+ if (theOption.state & QStyle::State_Selected)
196
+ {
197
+ drawHighlight (theOption,thepPainter,height (theOption,theData));
198
+ }
199
+ QPixmap myPixmap = QPixmap::grabWidget (mpWidget);
200
+ thepPainter->drawPixmap (theOption.rect .x (),
201
+ theOption.rect .y (),
202
+ myPixmap);
203
+ }// render as widget
204
+
205
+ void QgsDetailedItemDelegate::drawHighlight (const QStyleOptionViewItem &theOption,
206
+ QPainter * thepPainter,
207
+ int theHeight) const
208
+ {
209
+ QColor myColor1 = theOption.palette .highlight ().color ();
210
+ QColor myColor2 = myColor1;
211
+ myColor2 = myColor2.lighter (110 ); // 10% lighter
212
+ QLinearGradient myGradient (QPointF (0 ,theOption.rect .y ()),
213
+ QPointF (0 ,theOption.rect .y () + theHeight));
214
+ myGradient.setColorAt (0 , myColor1);
215
+ myGradient.setColorAt (0.1 , myColor2);
216
+ myGradient.setColorAt (0.5 , myColor1);
217
+ myGradient.setColorAt (0.9 , myColor2);
218
+ myGradient.setColorAt (1 , myColor2);
219
+ thepPainter->fillRect (theOption.rect , QBrush (myGradient));
220
+ }
221
+
222
+ int QgsDetailedItemDelegate::height (const QStyleOptionViewItem & theOption,
223
+ const QgsDetailedItemData theData) const
224
+ {
225
+ QFontMetrics myTitleMetrics ( titleFont ( theOption ) );
226
+ QFontMetrics myDetailMetrics ( detailFont ( theOption ) );
227
+ // we dont word wrap the title so its easy to measure
228
+ int myHeight = myTitleMetrics.height () + verticalSpacing ();
229
+ // the detail needs to be measured though
230
+ QStringList myList = wordWrap ( theData.detail (),
231
+ myDetailMetrics,
232
+ theOption.rect .width () - ( mpCheckBox->width () + horizontalSpacing () ) );
233
+ myHeight += ( myList.count () + 1 ) * ( myDetailMetrics.height () - verticalSpacing () );
234
+ return myHeight;
235
+ }
236
+
237
+
238
+ QFont QgsDetailedItemDelegate::detailFont (const QStyleOptionViewItem &theOption) const
239
+ {
240
+ QFont myFont = theOption.font ;
241
+ return myFont;
242
+ }
243
+
244
+ QFont QgsDetailedItemDelegate::titleFont (const QStyleOptionViewItem &theOption) const
245
+ {
246
+ QFont myTitleFont = detailFont (theOption);
247
+ myTitleFont.setBold (true );
248
+ myTitleFont.setPointSize (myTitleFont.pointSize () + 1 );
249
+ return myTitleFont;
250
+ }
251
+
252
+
229
253
QStringList QgsDetailedItemDelegate::wordWrap (QString theString,
230
254
QFontMetrics theMetrics,
231
255
int theWidth) const
@@ -272,3 +296,27 @@ QStringList QgsDetailedItemDelegate::wordWrap(QString theString,
272
296
273
297
}
274
298
299
+
300
+
301
+ int QgsDetailedItemDelegate::verticalSpacing () const
302
+ {
303
+ return mVerticalSpacing ;
304
+ }
305
+
306
+
307
+ void QgsDetailedItemDelegate::setVerticalSpacing ( int theValue )
308
+ {
309
+ mVerticalSpacing = theValue;
310
+ }
311
+
312
+
313
+ int QgsDetailedItemDelegate::horizontalSpacing () const
314
+ {
315
+ return mHorizontalSpacing ;
316
+ }
317
+
318
+
319
+ void QgsDetailedItemDelegate::setHorizontalSpacing ( int theValue )
320
+ {
321
+ mHorizontalSpacing = theValue;
322
+ }
0 commit comments