Skip to content

Commit 9b3f088

Browse files
author
timlinux
committedMay 19, 2008
Created a custom view delegate for displaying in lists like the plugin manager and the grass toolbox.
The delegate allows displaying a title (in bold) and a description underneath it (in normal font), without needing to resort to using tables & columns. The delegate currently works in two modes: 1) Direct rendering of item onto a painter 2) Renderng the item via a qgsdetaileditemwidget (disabled for not as its experimental) THe qgsdetailed item data class is a new qvariant type that can be used to pass title, description etc between the model and the view. git-svn-id: http://svn.osgeo.org/qgis/trunk@8463 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 172c20e commit 9b3f088

File tree

6 files changed

+381
-0
lines changed

6 files changed

+381
-0
lines changed
 

‎src/gui/qgsdetaileditemdata.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/***************************************************************************
2+
qgsdetailedlistdata.cpp - A data represenation for a rich QItemData subclass
3+
-------------------
4+
begin : Sat May 17 2008
5+
copyright : (C) 2008 Tim Sutton
6+
email : tim@linfiniti.com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
/* $Id:$ */
18+
19+
#include "qgsdetaileditemdata.h"
20+
QgsDetailedItemData::QgsDetailedItemData()
21+
{
22+
}
23+
24+
QgsDetailedItemData::~QgsDetailedItemData()
25+
{
26+
}
27+
28+
void QgsDetailedItemData::setTitle(QString theTitle)
29+
{
30+
mTitle=theTitle;
31+
}
32+
33+
void QgsDetailedItemData::setDetail(QString theDetail)
34+
{
35+
mDetail=theDetail;
36+
}
37+
38+
QString QgsDetailedItemData::title()
39+
{
40+
return mTitle;
41+
}
42+
43+
QString QgsDetailedItemData::detail()
44+
{
45+
return mDetail;
46+
}

‎src/gui/qgsdetaileditemdata.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/***************************************************************************
2+
qgsdetaileditemdata.h - A data represenation for a rich QItemData subclass
3+
-------------------
4+
begin : Sat May 17 2008
5+
copyright : (C) 2008 Tim Sutton
6+
email : tim@linfiniti.com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
/* $Id:$ */
18+
#ifndef QGSDETAILEDITEMDATA_H
19+
#define QGSDETAILEDITEMDATA_H
20+
21+
22+
#include <QMetaType>
23+
#include <QString>
24+
25+
/** This class is the data only representation of a
26+
* QgsDetailedItemWidget, designed to be used in custom views.
27+
*/
28+
class QgsDetailedItemData
29+
{
30+
public:
31+
QgsDetailedItemData();
32+
~QgsDetailedItemData();
33+
void setTitle(QString theTitle);
34+
void setDetail(QString theDetail);
35+
QString title();
36+
QString detail();
37+
private:
38+
QString mTitle;
39+
QString mDetail;
40+
QString mLibraryName;
41+
bool mCheckBoxEnabled;
42+
};
43+
44+
// Make QVariant aware of this data type (see qtdocs star
45+
// rating delegate example for more details)
46+
Q_DECLARE_METATYPE(QgsDetailedItemData)
47+
#endif //QGSDETAILEDITEMDATA_H

‎src/gui/qgsdetaileditemdelegate.cpp

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/***************************************************************************
2+
qgsdetailedlistwidget.cpp - A rich QItemDelegate subclass
3+
-------------------
4+
begin : Sat May 17 2008
5+
copyright : (C) 2008 Tim Sutton
6+
email : tim@linfiniti.com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
/* $Id:$ */
18+
19+
#include "qgsdetaileditemdelegate.h"
20+
#include "qgsdetaileditemwidget.h"
21+
#include "qgsdetaileditemdata.h"
22+
#include <QPainter>
23+
#include <QFont>
24+
#include <QFontMetrics>
25+
#include <QStyleOptionViewItem>
26+
#include <QModelIndex>
27+
#include <QCheckBox>
28+
#include <QLinearGradient>
29+
QgsDetailedItemDelegate::QgsDetailedItemDelegate(QObject * parent) :
30+
QAbstractItemDelegate(parent),
31+
mpWidget(new QgsDetailedItemWidget()),
32+
mpCheckBox(new QCheckBox())
33+
34+
{
35+
mpCheckBox->resize(16,16);
36+
}
37+
38+
QgsDetailedItemDelegate::~QgsDetailedItemDelegate()
39+
{
40+
delete mpCheckBox;
41+
delete mpWidget;
42+
}
43+
44+
void QgsDetailedItemDelegate::paint(QPainter * thepPainter,
45+
const QStyleOptionViewItem & theOption,
46+
const QModelIndex & theIndex) const
47+
{
48+
if (qVariantCanConvert<QgsDetailedItemData>(theIndex.data(Qt::UserRole)))
49+
{
50+
QgsDetailedItemData myData =
51+
qVariantValue<QgsDetailedItemData>(theIndex.data(Qt::UserRole));
52+
bool myCheckState = theIndex.model()->data(theIndex, Qt::CheckStateRole).toBool();
53+
mpWidget->setChecked(myCheckState);
54+
mpWidget->setData(myData);
55+
mpWidget->resize(theOption.rect.width(),mpWidget->height());
56+
mpWidget->setAutoFillBackground(false);
57+
mpWidget->show();
58+
mpWidget->repaint();
59+
60+
if (theOption.state & QStyle::State_Selected)
61+
{
62+
QColor myColor1 = theOption.palette.highlight();
63+
QColor myColor2 = myColor1;
64+
myColor2 = myColor2.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, myColor2);
72+
thepPainter->fillRect(theOption.rect, QBrush(myGradient));
73+
}
74+
QPixmap myPixmap(mpWidget->size());
75+
mpWidget->render(&myPixmap);
76+
thepPainter->drawPixmap(theOption.rect.x(),
77+
theOption.rect.y(),
78+
myPixmap);
79+
}
80+
else
81+
{
82+
// After painting we need to restore the painter to its original state
83+
thepPainter->save();
84+
//
85+
// Get the strings and check box properties
86+
//
87+
QString myString = theIndex.model()->data(theIndex, Qt::DisplayRole).toString();
88+
QString myDetailString = theIndex.model()->data(theIndex, Qt::UserRole).toString();
89+
bool myCheckState = theIndex.model()->data(theIndex, Qt::CheckStateRole).toBool();
90+
mpCheckBox->setChecked(myCheckState);
91+
QPixmap myPixmap(mpCheckBox->size());
92+
mpCheckBox->render(&myPixmap); //we will draw this onto the widget further down
93+
94+
//
95+
// Calculate the widget height and other metrics
96+
//
97+
QFont myFont = theOption.font;
98+
QFont myBoldFont = myFont;
99+
myBoldFont.setBold(true);
100+
myBoldFont.setPointSize(myFont.pointSize() + 3);
101+
QFontMetrics myMetrics(myBoldFont);
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() + myPixmap.width() + myHorizontalSpacer;
105+
int myHeight = myMetrics.height() + myVerticalSpacer;
106+
107+
//
108+
// Draw the item background with a gradient if its highlighted
109+
//
110+
if (theOption.state & QStyle::State_Selected)
111+
{
112+
QColor myColor1 = theOption.palette.highlight();
113+
QColor myColor2 = myColor1;
114+
myColor2 = myColor2.lighter();
115+
int myHeight = myMetrics.height() + myVerticalSpacer;
116+
QLinearGradient myGradient(QPointF(0,theOption.rect.y()),
117+
QPointF(0,theOption.rect.y() + myHeight*2));
118+
myGradient.setColorAt(0, myColor1);
119+
myGradient.setColorAt(0.1, myColor2);
120+
myGradient.setColorAt(0.5, myColor1);
121+
myGradient.setColorAt(0.9, myColor2);
122+
myGradient.setColorAt(1, myColor2);
123+
thepPainter->fillRect(theOption.rect, QBrush(myGradient));
124+
}
125+
126+
//
127+
// Draw the checkbox
128+
//
129+
thepPainter->drawPixmap(theOption.rect.x(),
130+
theOption.rect.y() + mpCheckBox->height(),
131+
myPixmap);
132+
133+
//
134+
// Draw the title and description
135+
//
136+
thepPainter->setFont(myBoldFont);
137+
thepPainter->drawText( myTextStartX ,theOption.rect.y() + myHeight, myString);
138+
thepPainter->setFont(myFont); //return to original font set by client
139+
thepPainter->drawText( myTextStartX,
140+
theOption.rect.y() + (myHeight *2) - myVerticalSpacer,
141+
myDetailString);
142+
thepPainter->restore();
143+
}
144+
}
145+
146+
QSize QgsDetailedItemDelegate::sizeHint(
147+
const QStyleOptionViewItem & theOption,
148+
const QModelIndex & theIndex ) const
149+
{
150+
if (qVariantCanConvert<QgsDetailedItemData>(theIndex.data(Qt::UserRole)))
151+
{
152+
return QSize(378,mpWidget->height());
153+
}
154+
else // fall back to hand calculated & hand drawn item
155+
{
156+
QFont myFont = theOption.font;
157+
QFont myBoldFont = myFont;
158+
myBoldFont.setBold(true);
159+
myBoldFont.setPointSize(myFont.pointSize() + 3);
160+
QFontMetrics myMetrics(myBoldFont);
161+
int myVerticalSpacer = 3; //spacing between title and description
162+
int myHorizontalSpacer = 5; //spacing between checkbox / icon and description
163+
int myHeight = myMetrics.height() + myVerticalSpacer;
164+
return QSize(50,
165+
myHeight *2 + myVerticalSpacer);
166+
}
167+
}

‎src/gui/qgsdetaileditemdelegate.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/***************************************************************************
2+
qgsdetaileditemdelegate.h - A rich QItemDelegate subclass
3+
-------------------
4+
begin : Sat May 17 2008
5+
copyright : (C) 2008 Tim Sutton
6+
email : tim@linfiniti.com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
/* $Id:$ */
18+
#ifndef QGSDETAILEDITEMDELEGATE_H
19+
#define QGSDETAILEDITEMDELEGATE_H
20+
21+
#include <QAbstractItemDelegate>
22+
#include <QString>
23+
24+
class QCheckBox;
25+
class QgsDetailedItemWidget;
26+
27+
class QgsDetailedItemDelegate :
28+
public QAbstractItemDelegate
29+
{
30+
Q_OBJECT;
31+
public:
32+
QgsDetailedItemDelegate(QObject * parent = 0);
33+
~QgsDetailedItemDelegate();
34+
void paint(QPainter * thePainter,
35+
const QStyleOptionViewItem & theOption,
36+
const QModelIndex & theIndex) const;
37+
QSize sizeHint( const QStyleOptionViewItem & theOption,
38+
const QModelIndex & theIndex ) const;
39+
private:
40+
QgsDetailedItemWidget * mpWidget;
41+
QCheckBox * mpCheckBox;
42+
};
43+
44+
#endif //QGSDETAILEDITEMDELEGATE_H

‎src/gui/qgsdetaileditemwidget.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/***************************************************************************
2+
qgsdetailedlistwidget.cpp - A rich QItemWidget subclass
3+
-------------------
4+
begin : Sat May 17 2008
5+
copyright : (C) 2008 Tim Sutton
6+
email : tim@linfiniti.com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
/* $Id:$ */
18+
19+
#include "qgsdetaileditemwidget.h"
20+
QgsDetailedItemWidget::QgsDetailedItemWidget(QWidget * parent) :
21+
QWidget(parent)
22+
23+
{
24+
setupUi(this);
25+
}
26+
27+
QgsDetailedItemWidget::~QgsDetailedItemWidget()
28+
{
29+
}
30+
31+
void QgsDetailedItemWidget::setData(QgsDetailedItemData theData)
32+
{
33+
lblTitle->setText(theData.title());
34+
tbDetail->setText(theData.detail());
35+
}
36+
37+
void QgsDetailedItemWidget::setChecked(bool theFlag)
38+
{
39+
cbx->setChecked(theFlag);
40+
}

‎src/gui/qgsdetaileditemwidget.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/***************************************************************************
2+
qgsdetaileditemwidget.h - A rich QItemWidget subclass
3+
-------------------
4+
begin : Sat May 17 2008
5+
copyright : (C) 2008 Tim Sutton
6+
email : tim@linfiniti.com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
/* $Id:$ */
18+
#ifndef QGSDETAILEDITEMWIDGET_H
19+
#define QGSDETAILEDITEMWIDGET_H
20+
21+
#include <ui_qgsdetaileditemwidgetbase.h>
22+
#include <qgsdetaileditemdata.h>
23+
24+
class QgsDetailedItemWidget :
25+
public QWidget, private Ui::QgsDetailedItemWidgetBase
26+
{
27+
Q_OBJECT;
28+
public:
29+
QgsDetailedItemWidget(QWidget * parent = 0);
30+
~QgsDetailedItemWidget();
31+
void setData(QgsDetailedItemData theData);
32+
void setChecked(bool theFlag);
33+
private:
34+
QgsDetailedItemData mData;
35+
};
36+
37+
#endif //QGSDETAILEDITEMWIDGET_H

0 commit comments

Comments
 (0)
Please sign in to comment.