34
34
#include < QComboBox>
35
35
#include < QSignalMapper>
36
36
#include < QToolBar>
37
+ #include < QItemDelegate>
37
38
38
39
#include " mymodel.h"
39
40
#include " bookmarkmodel.h"
@@ -65,28 +66,26 @@ class QAction;
65
66
class QMenu ;
66
67
QT_END_NAMESPACE
67
68
68
-
69
- #include < QItemDelegate>
70
-
71
- class Delegate : public QItemDelegate
69
+ // ---------------------------------------------------------------------------------
70
+ class IconViewDelegate : public QItemDelegate
72
71
{
73
72
public:
74
73
QSize sizeHint (const QStyleOptionViewItem &option, const QModelIndex &index) const
75
74
{
76
75
QIcon icon = qvariant_cast<QIcon>(index .data (Qt::DecorationRole));
77
76
QSize iconsize = icon.actualSize (option.decorationSize );
78
77
QRect item = option.rect ;
79
- QRect txtRect (item.left (), item.top ()+(iconsize.height ()/2 ), item.width ()-10 , item.height ()-10 );
78
+ QRect txtRect (item.left (), item.top ()+(iconsize.height ()/2 )+ 5 , item.width ()-5 , item.height ()-5 );
80
79
QSize txtsize = option.fontMetrics .boundingRect (txtRect, Qt::AlignCenter|Qt::TextWrapAnywhere, index .data ().toString ()).size ();
81
- return QSize (txtsize.width (),txtsize.height ()+iconsize.height ());
80
+ return QSize (txtsize.width ()+ 10 ,txtsize.height ()+iconsize.height ()+ 10 );
82
81
}
83
82
void paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
84
83
{
85
84
QIcon icon = qvariant_cast<QIcon>(index .data (Qt::DecorationRole));
86
85
QSize iconsize = icon.actualSize (option.decorationSize );
87
86
QRect item = option.rect ;
88
87
QRect iconRect (item.left ()+(item.width ()/2 )-(iconsize.width ()/2 ), item.top ()+3 , iconsize.width (), iconsize.height ());
89
- QRect txtRect (item.left (), item.top ()+(iconsize.height ()/2 )+5 , item.width (), item.height ());
88
+ QRect txtRect (item.left (), item.top ()+(iconsize.height ()/2 )+5 , item.width ()- 5 , item.height ()- 10 );
90
89
91
90
if (option.state & QStyle::State_Selected) {
92
91
painter->fillRect (option.rect , option.palette .highlight ());
@@ -99,7 +98,37 @@ class Delegate : public QItemDelegate
99
98
painter->drawText (txtRect, Qt::AlignCenter|Qt::TextWrapAnywhere, index .data ().toString ());
100
99
}
101
100
};
101
+ class IconListDelegate : public QItemDelegate
102
+ {
103
+ public:
104
+ QSize sizeHint (const QStyleOptionViewItem &option, const QModelIndex &index) const
105
+ {
106
+ QIcon icon = qvariant_cast<QIcon>(index .data (Qt::DecorationRole));
107
+ QSize iconsize = icon.actualSize (option.decorationSize );
108
+ QRect item = option.rect ;
109
+ QRect txtRect (item.left ()+iconsize.width ()+5 , item.top (), item.width (), item.height ());
110
+ QSize txtsize = option.fontMetrics .boundingRect (txtRect, Qt::AlignLeft|Qt::AlignVCenter, index .data ().toString ()).size ();
111
+ return QSize (txtsize.width ()+iconsize.width ()+10 ,iconsize.height ());
112
+ }
113
+ void paint (QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
114
+ {
115
+ QIcon icon = qvariant_cast<QIcon>(index .data (Qt::DecorationRole));
116
+ QSize iconsize = icon.actualSize (option.decorationSize );
117
+ QRect item = option.rect ;
118
+ QRect iconRect (item.left (), item.top (), iconsize.width (), iconsize.height ());
119
+ QRect txtRect (item.left ()+iconsize.width ()+5 , item.top (), item.width (), item.height ());
120
+
121
+ if (option.state & QStyle::State_Selected) {
122
+ painter->fillRect (option.rect , option.palette .highlight ());
123
+ painter->setPen (option.palette .highlightedText ().color ());
124
+ } else {
125
+ painter->setPen (option.palette .text ().color ());
126
+ }
102
127
128
+ painter->drawPixmap (iconRect, icon.pixmap (iconsize.width (),iconsize.height ()));
129
+ painter->drawText (txtRect, Qt::AlignLeft|Qt::AlignVCenter, index .data ().toString ());
130
+ }
131
+ };
103
132
// ---------------------------------------------------------------------------------
104
133
105
134
/* *
@@ -348,6 +377,9 @@ private slots:
348
377
349
378
bool pathHistory;
350
379
bool showPathInWindowTitle;
380
+
381
+ IconViewDelegate *ivdelegate;
382
+ IconListDelegate *ildelegate;
351
383
};
352
384
353
385
// ---------------------------------------------------------------------------------
0 commit comments