Skip to content

Commit 0b4876f

Browse files
author
timlinux
committedJun 6, 2008
Refactored grass tools and plugin manager to always use qgsdetaileditemdata to represent the info to be display in items. This will let us switch between widget based and manually drawn implementations easily.
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@8603 c8812cc2-4d05-0410-92ff-de0c093fc19c

File tree

5 files changed

+224
-212
lines changed

5 files changed

+224
-212
lines changed
 

‎src/app/qgspluginmanager.cpp

Lines changed: 66 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@
5050
#include <dlfcn.h>
5151
#endif
5252
#endif
53+
54+
55+
const int PLUGIN_DATA_ROLE=Qt::UserRole;
56+
const int PLUGIN_LIBRARY_ROLE=Qt::UserRole + 1;
57+
const int PLUGIN_LIBRARY_NAME_ROLE=Qt::UserRole + 2;
58+
const int PLUGIN_FILTER_ROLE=Qt::UserRole + 3;
59+
5360
QgsPluginManager::QgsPluginManager(QgsPythonUtils* pythonUtils, QWidget * parent, Qt::WFlags fl)
5461
: QDialog(parent, fl)
5562
{
@@ -98,7 +105,6 @@ void QgsPluginManager::setTable()
98105
mModelPlugins= new QStandardItemModel(0,1);
99106
mModelProxy = new QSortFilterProxyModel(this);
100107
mModelProxy->setSourceModel(mModelPlugins);
101-
//mModelProxy->setFilterKeyColumn(0);
102108
vwPlugins->setModel(mModelProxy);
103109
vwPlugins->setFocus();
104110
vwPlugins->setItemDelegateForColumn(0,new QgsDetailedItemDelegate());
@@ -138,31 +144,28 @@ void QgsPluginManager::getPythonPluginDescriptions()
138144
QString description = mPythonUtils->getPluginMetadata(packageName, "description");
139145
QString version = mPythonUtils->getPluginMetadata(packageName, "version");
140146

141-
if (pluginName == "???" || description == "???" || version == "???")
142-
continue;
147+
if (pluginName == "???" || description == "???" || version == "???") continue;
143148

144-
// We will create two items for each row:
145-
// The first has the following roles :
146-
// - DisplayRole : the name and version of the version of the plugin e.g. Plugin Foo (version 1.0)
147-
// - UserRole : the description of the plugin.
148-
// And the second has these roles :
149-
// - DisplayRole : the library name (without path) for the plugin e.g. libfoo.so
150-
// - UserRole : the name of the plugin without version e.g. plugin foor
151-
// The plugin name without version is used elsewhere in this class to match
152-
// aganst plugins loaded in the plugin registry
153-
QStandardItem * mypDetailItem = new QStandardItem(
154-
pluginName + " (" + version + ")");
155-
mypDetailItem->setData(description,Qt::UserRole);
149+
QStandardItem * mypDetailItem = new QStandardItem( pluginName );
156150
QString myLibraryName = "python:" + packageName;;
157-
QStandardItem * mypLibraryNameItem = new QStandardItem(myLibraryName);
158-
mypLibraryNameItem->setData(pluginName,Qt::UserRole);
159-
// myName have a checkbox
160-
mypDetailItem->setCheckable(true);
151+
mypDetailItem->setData(myLibraryName, PLUGIN_LIBRARY_ROLE); //for loading libs later
152+
mypDetailItem->setData(pluginName, PLUGIN_LIBRARY_NAME_ROLE); //for matching in registry later
153+
QString mySearchText = pluginName + " - " + description;
154+
mypDetailItem->setData(mySearchText , PLUGIN_FILTER_ROLE); //for filtering later
155+
mypDetailItem->setCheckable(false);
161156
mypDetailItem->setEditable(false);
157+
// setData in the delegate with a variantised QgsDetailedItemData
158+
QgsDetailedItemData myData;
159+
myData.setTitle(pluginName + " (" + version + ")");
160+
myData.setDetail(description);
161+
//myData.setIcon(pixmap); //todo use a python logo here
162+
myData.setCheckable(true);
163+
myData.setRenderAsWidget(false);
164+
QVariant myVariant = qVariantFromValue(myData);
165+
mypDetailItem->setData(myVariant,PLUGIN_DATA_ROLE);
162166

163167
// check to see if the plugin is loaded and set the checkbox accordingly
164168
QgsPluginRegistry *pRegistry = QgsPluginRegistry::instance();
165-
166169
QString libName = pRegistry->library(pluginName);
167170
if (libName.length() == 0 || !pRegistry->isPythonPlugin(pluginName))
168171
{
@@ -177,10 +180,8 @@ void QgsPluginManager::getPythonPluginDescriptions()
177180
mypDetailItem->setCheckState(Qt::Checked);
178181
}
179182
}
180-
// Add items to model
181-
QList<QStandardItem *> myItems;
182-
myItems << mypDetailItem << mypLibraryNameItem;
183-
mModelPlugins->appendRow(myItems);
183+
// Add item to model
184+
mModelPlugins->appendRow(mypDetailItem);
184185
}
185186
}
186187

@@ -262,19 +263,28 @@ sharedLibExtension = "*.so*";
262263
version_t *pVersion = (version_t *) myLib->resolve("version");
263264

264265
// show the values (or lack of) for each function
265-
if(pName){
266+
if(pName)
267+
{
266268
QgsDebugMsg("Plugin name: " + pName());
267-
}else{
269+
}
270+
else
271+
{
268272
QgsDebugMsg("Plugin name not returned when queried\n");
269273
}
270-
if(pDesc){
274+
if(pDesc)
275+
{
271276
QgsDebugMsg("Plugin description: " + pDesc());
272-
}else{
277+
}
278+
else
279+
{
273280
QgsDebugMsg("Plugin description not returned when queried\n");
274281
}
275-
if(pVersion){
282+
if(pVersion)
283+
{
276284
QgsDebugMsg("Plugin version: " + pVersion());
277-
}else{
285+
}
286+
else
287+
{
278288
QgsDebugMsg("Plugin version not returned when queried\n");
279289
}
280290

@@ -285,39 +295,20 @@ sharedLibExtension = "*.so*";
285295
continue;
286296
}
287297

288-
// We will create two items for each row:
289-
// The first has the following roles :
290-
// - DisplayRole : the name and version of the version of the plugin e.g. Plugin Foo (version 1.0)
291-
// - UserRole : the description of the plugin.
292-
// And the second has these roles :
293-
// - DisplayRole : the library name (without path) for the plugin e.g. libfoo.so
294-
// - UserRole : the name of the plugin without version e.g. plugin foor
295-
// The plugin name without version is used elsewhere in this class to match
296-
// aganst plugins loaded in the plugin registry
297-
298-
QStandardItem * mypDetailItem = new QStandardItem(
299-
pName() + " (" + pVersion() + ")");
300-
//
301-
// Uncomment this block to render item using simple painter technique
302-
//
303-
mypDetailItem->setData(pDesc(),Qt::UserRole);
304-
//
305-
//Uncomment this block to used widget based detail items (experimental)
306-
//
307-
/*
298+
QString myLibraryName = pluginDir[i];
299+
QStandardItem * mypDetailItem = new QStandardItem(myLibraryName);
300+
mypDetailItem->setData(myLibraryName,PLUGIN_LIBRARY_ROLE);
301+
mypDetailItem->setData(pName(), PLUGIN_LIBRARY_NAME_ROLE); //for matching in registry later
308302
QgsDetailedItemData myData;
309303
myData.setTitle(pName());
310304
myData.setDetail(pDesc());
305+
myData.setRenderAsWidget(false);
311306
QVariant myVariant = qVariantFromValue(myData);
312307
//round trip test - delete this...no need to uncomment
313308
//QgsDetailedItemData myData2 = qVariantValue<QgsDetailedItemData>(myVariant);
314309
//Q_ASSERT(myData.title() == myData2.title());
315310
//round trip test ends
316-
mypDetailItem->setData(myVariant,Qt::UserRole);
317-
*/
318-
QString myLibraryName = pluginDir[i];
319-
QStandardItem * mypLibraryNameItem = new QStandardItem(myLibraryName);
320-
mypLibraryNameItem->setData(pName(),Qt::UserRole);
311+
mypDetailItem->setData(myVariant,PLUGIN_DATA_ROLE);
321312
// Let the first col have a checkbox
322313
mypDetailItem->setCheckable(true);
323314
mypDetailItem->setEditable(false);
@@ -326,9 +317,9 @@ sharedLibExtension = "*.so*";
326317

327318
// check to see if the plugin is loaded and set the checkbox accordingly
328319
QgsPluginRegistry *pRegistry = QgsPluginRegistry::instance();
320+
QString libName = pRegistry->library(pName());
329321

330322
// get the library using the plugin description
331-
QString libName = pRegistry->library(pName());
332323
if (libName.length() == 0)
333324
{
334325
QgsDebugMsg("Couldn't find library name in the registry");
@@ -343,9 +334,7 @@ sharedLibExtension = "*.so*";
343334
}
344335
}
345336
// Add items to model
346-
QList<QStandardItem *> myItems;
347-
myItems << mypDetailItem << mypLibraryNameItem;
348-
mModelPlugins->appendRow(myItems);
337+
mModelPlugins->appendRow(mypDetailItem);
349338

350339
delete myLib;
351340
}
@@ -369,15 +358,16 @@ void QgsPluginManager::unload()
369358
QModelIndex myIndex=mModelPlugins->index(row,0);
370359
if (mModelPlugins->data(myIndex,Qt::CheckStateRole).toInt() == 0)
371360
{
372-
// Get the second col index now since it stores the
373-
// plugin name without version string in its data UserRole [ts]
374-
myIndex=mModelPlugins->index(row,1);
361+
// iThe plugin name without version string in its data PLUGIN_LIB [ts]
362+
myIndex=mModelPlugins->index(row,0);
375363
// its off -- see if it is loaded and if so, unload it
376364
QgsPluginRegistry *pRegistry = QgsPluginRegistry::instance();
377365
#ifdef QGISDEBUG
378-
std::cout << "Checking to see if " << mModelPlugins->data(myIndex, Qt::UserRole).toString().toLocal8Bit().data() << " is loaded" << std::endl;
366+
std::cout << "Checking to see if " <<
367+
mModelPlugins->data(myIndex, PLUGIN_LIBRARY_NAME_ROLE).toString().toLocal8Bit().data() <<
368+
" is loaded" << std::endl;
379369
#endif
380-
QString pluginName = mModelPlugins->data(myIndex,Qt::UserRole).toString();
370+
QString pluginName = mModelPlugins->data(myIndex,PLUGIN_LIBRARY_NAME_ROLE).toString();
381371
if (pRegistry->isPythonPlugin(pluginName))
382372
{
383373
if (mPythonUtils && mPythonUtils->isEnabled())
@@ -412,10 +402,10 @@ std::vector < QgsPluginItem > QgsPluginManager::getSelectedPlugins()
412402
{
413403
if (mModelPlugins->item(row,0)->checkState() == Qt::Checked)
414404
{
415-
QString pluginName = mModelPlugins->item(row,1)->data(Qt::UserRole).toString();
405+
QString pluginName = mModelPlugins->item(row,0)->data(PLUGIN_LIBRARY_NAME_ROLE).toString();
416406
bool pythonic = false;
417407

418-
QString library = mModelPlugins->item(row,1)->text();
408+
QString library = mModelPlugins->item(row,0)->data(PLUGIN_LIBRARY_ROLE).toString();
419409
if (library.left(7) == "python:")
420410
{
421411
library = library.mid(7);
@@ -425,7 +415,15 @@ std::vector < QgsPluginItem > QgsPluginManager::getSelectedPlugins()
425415
{
426416
library = lblPluginDir->text() + QDir::separator() + library;
427417
}
428-
pis.push_back(QgsPluginItem(pluginName, mModelPlugins->item(row,0)->text(), library, 0, pythonic));
418+
// Ctor params for plugin item:
419+
//QgsPluginItem(QString name=0,
420+
// QString description=0,
421+
// QString fullPath=0,
422+
// QString type=0,
423+
// bool python=false);
424+
pis.push_back(QgsPluginItem(pluginName,
425+
mModelPlugins->item(row,0)->data(PLUGIN_FILTER_ROLE).toString(),
426+
library, 0, pythonic));
429427
}
430428

431429
}
@@ -462,7 +460,7 @@ void QgsPluginManager::on_vwPlugins_clicked(const QModelIndex &theIndex )
462460
// little hoop to get the correct item
463461
//
464462
QStandardItem * mypItem =
465-
mModelPlugins->findItems(theIndex.data(Qt::DisplayRole).toString()).first();
463+
mModelPlugins->findItems(theIndex.data(Qt ::DisplayRole).toString()).first();
466464
if ( mypItem->checkState() == Qt::Checked )
467465
{
468466
mypItem->setCheckState(Qt::Unchecked);

‎src/gui/qgsdetaileditemdata.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "qgsdetaileditemdata.h"
2020
QgsDetailedItemData::QgsDetailedItemData()
2121
{
22+
mRenderAsWidgetFlag=false;
2223
}
2324

2425
QgsDetailedItemData::~QgsDetailedItemData()
@@ -47,6 +48,10 @@ void QgsDetailedItemData::setChecked(bool theFlag)
4748
{
4849
mCheckedFlag = theFlag;
4950
}
51+
void QgsDetailedItemData::setRenderAsWidget(bool theFlag)
52+
{
53+
mRenderAsWidgetFlag = theFlag;
54+
}
5055

5156
QString QgsDetailedItemData::title()
5257
{
@@ -72,3 +77,9 @@ bool QgsDetailedItemData::isChecked()
7277
{
7378
return mCheckedFlag;
7479
}
80+
81+
bool QgsDetailedItemData::isRenderedAsWidget()
82+
{
83+
return mRenderAsWidgetFlag;
84+
}
85+

‎src/gui/qgsdetaileditemdata.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,20 @@ class QgsDetailedItemData
3636
void setIcon(QPixmap theIcon);
3737
void setCheckable(bool theFlag);
3838
void setChecked(bool theFlag);
39+
/** This is a hint to the delegate to render using
40+
* a widget rather than manually painting every
41+
* part of the list item.
42+
* @note the delegate may completely ignore this
43+
* depending on the delegate implementation.
44+
*/
45+
void setRenderAsWidget(bool theFlag);
3946

4047
QString title();
4148
QString detail();
4249
QPixmap icon();
4350
bool isCheckable();
4451
bool isChecked();
52+
bool isRenderedAsWidget();
4553

4654
private:
4755
QString mTitle;
@@ -50,6 +58,7 @@ class QgsDetailedItemData
5058
QPixmap mPixmap;
5159
bool mCheckableFlag;
5260
bool mCheckedFlag;
61+
bool mRenderAsWidgetFlag;
5362
};
5463

5564
// Make QVariant aware of this data type (see qtdocs star

‎src/gui/qgsdetaileditemdelegate.cpp

Lines changed: 129 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -46,145 +46,145 @@ void QgsDetailedItemDelegate::paint(QPainter * thepPainter,
4646
const QStyleOptionViewItem & theOption,
4747
const QModelIndex & theIndex) const
4848
{
49+
// After painting we need to restore the painter to its original state
50+
thepPainter->save();
4951
if (qVariantCanConvert<QgsDetailedItemData>(theIndex.data(Qt::UserRole)))
5052
{
5153
QgsDetailedItemData myData =
5254
qVariantValue<QgsDetailedItemData>(theIndex.data(Qt::UserRole));
5355
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())
6157
{
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();
9363

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
11384
{
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;
117106
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-
}
127107

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();
188188
}
189189

190190
QSize QgsDetailedItemDelegate::sizeHint(

‎src/plugins/grass/qgsgrasstools.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -442,21 +442,15 @@ void QgsGrassTools::addModules ( QTreeWidgetItem *parent, QDomElement &element
442442
mypDetailItem->setData(pixmap,Qt::DecorationRole);
443443
mypDetailItem->setCheckable(false);
444444
mypDetailItem->setEditable(false);
445-
446-
447-
// Render items using widget based detail items (experimental)
448-
// Calling setData in the delegate with a variantised QgsDetailedItemData
449-
// will cause the widget based mode to be enabled
450-
//QgsDetailedItemData myData;
451-
//myData.setTitle(name);
452-
//myData.setDetail(label);
453-
//myData.setIcon(pixmap);
454-
//myData.setCheckable(false);
455-
//QVariant myVariant = qVariantFromValue(myData);
456-
//mypDetailItem->setData(myVariant,Qt::UserRole);
457-
458-
//alternate invocation method using simple drawing code
459-
mypDetailItem->setData(label,Qt::UserRole);
445+
// setData in the delegate with a variantised QgsDetailedItemData
446+
QgsDetailedItemData myData;
447+
myData.setTitle(name);
448+
myData.setDetail(label);
449+
myData.setIcon(pixmap);
450+
myData.setCheckable(false);
451+
myData.setRenderAsWidget(true);
452+
QVariant myVariant = qVariantFromValue(myData);
453+
mypDetailItem->setData(myVariant,Qt::UserRole);
460454
mModelTools->appendRow(mypDetailItem);
461455
//
462456
// End of experimental work by Tim

0 commit comments

Comments
 (0)
Please sign in to comment.