libkipi from trunk (KDE 4.3) : add support of kipi host settings "file timestamp...
[kdegraphics.git] / gwenview / lib / semanticinfo / tagitemdelegate.cpp
blob131b046bb9e074e6c9af1a403a011768021fa072
1 // vim: set tabstop=4 shiftwidth=4 noexpandtab:
2 /*
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.
21 // Self
22 #include <tagitemdelegate.moc>
24 // Qt
25 #include <QAbstractItemView>
26 #include <QPainter>
27 #include <QToolButton>
29 // KDE
30 #include <kdebug.h>
31 #include <kdialog.h>
32 #include <kiconloader.h>
33 #include <klocale.h>
35 // Local
36 #include <lib/semanticinfo/tagmodel.h>
38 namespace Gwenview {
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);
46 #undef pm
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()));
73 #undef initButton
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);
92 if (fullyAssigned) {
93 assignToAllButton->hide();
94 } else {
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()) {
102 return;
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,
117 selected
118 ? QPalette::HighlightedText
119 : QPalette::Text));
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()";
135 return;
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()";
145 return;
147 emit assignTagToAllRequested(index.data(TagModel::TagRole).toString());
151 } // namespace