1 // vim: set tabstop=4 shiftwidth=4 noexpandtab:
3 Gwenview: an image viewer
4 Copyright 2008 Aurélien Gâteau <aurelien.gateau@free.fr>
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
22 #include <tagitemdelegate.moc>
25 #include <QAbstractItemView>
27 #include <QToolButton>
32 #include <kiconloader.h>
36 #include <lib/semanticinfo/tagmodel.h>
40 TagItemDelegate::TagItemDelegate(QAbstractItemView
* view
)
41 : KWidgetItemDelegate(view
, view
)
43 #define pm(x) view->style()->pixelMetric(QStyle::x)
44 mMargin
= pm(PM_ToolBarItemMargin
);
45 mSpacing
= pm(PM_ToolBarItemSpacing
);
47 const int iconSize
= KIconLoader::global()->currentSize(KIconLoader::Toolbar
);
48 const QSize sz
= view
->style()->sizeFromContents(QStyle::CT_ToolButton
, 0, QSize(iconSize
, iconSize
));
49 mButtonSize
= qMax(sz
.width(), sz
.height());
53 QList
<QWidget
*> TagItemDelegate::createItemWidgets() const {
55 #define initButton(x) \
56 (x)->setAutoRaise(true); \
57 setBlockedEventTypes((x), QList<QEvent::Type>() \
58 << QEvent::MouseButtonPress \
59 << QEvent::MouseButtonRelease \
60 << QEvent::MouseButtonDblClick);
62 QToolButton
* assignToAllButton
= new QToolButton
;
63 initButton(assignToAllButton
);
64 assignToAllButton
->setIcon(KIcon("fill-color")); /* FIXME: Probably not the appropriate icon */
65 assignToAllButton
->setToolTip(i18n("Assign this tag to all selected images"));
66 connect(assignToAllButton
, SIGNAL(clicked()), SLOT(slotAssignToAllButtonClicked()));
68 QToolButton
* removeButton
= new QToolButton
;
69 initButton(removeButton
);
70 removeButton
->setIcon(KIcon("list-remove"));
71 connect(removeButton
, SIGNAL(clicked()), SLOT(slotRemoveButtonClicked()));
75 return QList
<QWidget
*>() << removeButton
<< assignToAllButton
;
79 void TagItemDelegate::updateItemWidgets(const QList
<QWidget
*> widgets
, const QStyleOptionViewItem
& option
, const QPersistentModelIndex
& index
) const {
80 const bool fullyAssigned
= index
.data(TagModel::AssignmentStatusRole
).toInt() == int(TagModel::FullyAssigned
);
82 QToolButton
* removeButton
= static_cast<QToolButton
*>(widgets
[0]);
83 QToolButton
* assignToAllButton
= static_cast<QToolButton
*>(widgets
[1]);
85 QSize
buttonSize(mButtonSize
, option
.rect
.height() - 2 * mMargin
);
87 removeButton
->resize(buttonSize
);
88 assignToAllButton
->resize(buttonSize
);
90 removeButton
->move(option
.rect
.width() - mButtonSize
- mMargin
, mMargin
);
93 assignToAllButton
->hide();
95 assignToAllButton
->move(removeButton
->x() - mButtonSize
- mSpacing
, mMargin
);
100 void TagItemDelegate::paint(QPainter
* painter
, const QStyleOptionViewItem
& option
, const QModelIndex
& index
) const {
101 if (!index
.isValid()) {
104 const bool selected
= option
.state
& QStyle::State_Selected
;
105 const bool fullyAssigned
= index
.data(TagModel::AssignmentStatusRole
).toInt() == int(TagModel::FullyAssigned
);
107 itemView()->style()->drawPrimitive(QStyle::PE_PanelItemViewItem
, &option
, painter
, 0);
109 QRect textRect
= option
.rect
;
110 textRect
.setLeft(textRect
.left() + mMargin
);
111 textRect
.setWidth(textRect
.width() - mButtonSize
- mMargin
- mSpacing
);
112 if (!fullyAssigned
) {
113 textRect
.setWidth(textRect
.width() - mButtonSize
- mSpacing
);
116 painter
->setPen(option
.palette
.color(QPalette::Normal
,
118 ? QPalette::HighlightedText
120 painter
->drawText(textRect
, Qt::AlignLeft
| Qt::AlignVCenter
, index
.data().toString());
124 QSize
TagItemDelegate::sizeHint(const QStyleOptionViewItem
& option
, const QModelIndex
& index
) const {
125 const int width
= option
.fontMetrics
.width(index
.data().toString());
126 const int height
= qMax(mButtonSize
, option
.fontMetrics
.height());
127 return QSize(width
+ 2 * mMargin
, height
+ 2 * mMargin
);
131 void TagItemDelegate::slotRemoveButtonClicked() {
132 const QModelIndex index
= focusedIndex();
133 if (!index
.isValid()) {
134 kWarning() << "!index.isValid()";
137 emit
removeTagRequested(index
.data(TagModel::TagRole
).toString());
141 void TagItemDelegate::slotAssignToAllButtonClicked() {
142 const QModelIndex index
= focusedIndex();
143 if (!index
.isValid()) {
144 kWarning() << "!index.isValid()";
147 emit
assignTagToAllRequested(index
.data(TagModel::TagRole
).toString());