2 * This file is part of the System Settings package
3 * Copyright (C) 2005 Benjamin C Meyer
4 * <ben+systempreferences at meyerhome dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 #include "moduleiconitem.h"
23 #include <kiconloader.h>
25 #include <kcmoduleinfo.h>
29 #include <QApplication>
33 #define ICON_WIDTH 100
35 ModuleIconItemDelegate::ModuleIconItemDelegate(QObject
*parent
) : QItemDelegate(parent
)
39 void ModuleIconItemDelegate::paint(QPainter
*painter
, const QStyleOptionViewItem
&option
, const QModelIndex
&index
) const
42 painter
->setRenderHints(QPainter::Antialiasing
| QPainter::TextAntialiasing
);
45 bool selected
= option
.state
& QStyle::State_Selected
|| option
.state
& QStyle::State_HasFocus
;
46 if (const QStyleOptionViewItemV3
*v3
= qstyleoption_cast
<const QStyleOptionViewItemV3
*>(&option
)) {
47 style
= v3
->widget
->style();
48 if (!v3
->widget
->hasFocus()) selected
= false;
50 style
= QApplication::style();
54 painter
->fillPath(roundedRectangle(option
.rect
, 10), option
.palette
.brush(QPalette::Highlight
));
55 painter
->setPen(option
.palette
.color(QPalette::HighlightedText
));
58 if (!selected
&& (option
.state
& QStyle::State_MouseOver
)) {
59 QColor hover
= option
.palette
.color(QPalette::Highlight
);
61 painter
->fillPath(roundedRectangle(option
.rect
, 10), hover
);
64 if( index
.data( Qt::UserRole
).toInt() == KIconLoader::DisabledState
) {
65 painter
->setPen( option
.palette
.color( QPalette::Disabled
, QPalette::Text
) );
68 const QSize
&decorationSize
= option
.decorationSize
;
69 QIcon::Mode iconMode
= QIcon::Normal
;
70 if (selected
) iconMode
= QIcon::Selected
;
71 const QPixmap
&pixmap
= qvariant_cast
<QIcon
>(index
.data(Qt::DecorationRole
)).pixmap(option
.decorationSize
, iconMode
);
72 int iconX
= option
.rect
.left() + (option
.rect
.width() - decorationSize
.width()) / 2;
73 painter
->drawPixmap(iconX
, option
.rect
.top() + style
->pixelMetric(QStyle::PM_FocusFrameVMargin
), decorationSize
.width(), decorationSize
.height(), pixmap
);
75 QRect textRectangle
= option
.rect
;
76 textRectangle
.setTop(textRectangle
.top() + decorationSize
.height() + style
->pixelMetric(QStyle::PM_FocusFrameVMargin
));
77 painter
->drawText(textRectangle
, Qt::AlignHCenter
| Qt::TextWordWrap
, index
.data(Qt::DisplayRole
).toString());
81 // Method taken from KFileItemDelegate. Check whether it has been moved to
82 // kdefx/kdrawutil.cpp as the comment says on Fredrik's code. If so, remove
83 // this code (duplication), and use the library one.
84 QPainterPath
ModuleIconItemDelegate::roundedRectangle(const QRectF
&rect
, qreal radius
) const
86 QPainterPath
path(QPointF(rect
.left(), rect
.top() + radius
));
87 path
.quadTo(rect
.left(), rect
.top(), rect
.left() + radius
, rect
.top()); // Top left corner
88 path
.lineTo(rect
.right() - radius
, rect
.top()); // Top side
89 path
.quadTo(rect
.right(), rect
.top(), rect
.right(), rect
.top() + radius
); // Top right corner
90 path
.lineTo(rect
.right(), rect
.bottom() - radius
); // Right side
91 path
.quadTo(rect
.right(), rect
.bottom(), rect
.right() - radius
, rect
.bottom()); // Bottom right corner
92 path
.lineTo(rect
.left() + radius
, rect
.bottom()); // Bottom side
93 path
.quadTo(rect
.left(), rect
.bottom(), rect
.left(), rect
.bottom() - radius
); // Bottom left corner
99 ModuleIconItem::ModuleIconItem( QListWidget
* parent
, const KCModuleInfo
& module
)
100 : QListWidgetItem(SmallIcon( module
.icon(), IMAGE_SIZE
), module
.moduleName(), parent
),
101 imageName(module
.icon())
103 setData( Qt::UserRole
, KIconLoader::DefaultState
);
104 modules
.append(module
);
108 ModuleIconItem::ModuleIconItem( QListWidget
* parent
, const QString
&text
,
109 const QString
&_imageName
)
110 : QListWidgetItem( SmallIcon( _imageName
, IMAGE_SIZE
), text
, parent
),
111 imageName(_imageName
)
113 setData( Qt::UserRole
, KIconLoader::DefaultState
);
117 void ModuleIconItem::loadIcon( bool enabled
)
119 int newState
= enabled
? KIconLoader::DefaultState
: KIconLoader::DisabledState
;
120 if( newState
== data( Qt::UserRole
).toInt() )
123 setData( Qt::UserRole
, newState
);
124 setIcon( DesktopIcon( imageName
, IMAGE_SIZE
, newState
) );
127 void ModuleIconItem::setSize()
129 QStyle
*style
= listWidget()->style();
130 QFontMetrics
fm(font());
131 const QRect
&rect
= fm
.boundingRect(0, 0, ICON_WIDTH
, INT_MAX
, Qt::TextWordWrap
, text());
132 setData(Qt::SizeHintRole
, QSize(ICON_WIDTH
, IMAGE_SIZE
+ style
->pixelMetric(QStyle::PM_FocusFrameVMargin
) + rect
.height()));