~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to lib/semanticinfo/tagitemdelegate.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
Import upstream version 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
 
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
2
2
/*
3
3
Gwenview: an image viewer
4
4
Copyright 2008 Aurélien Gâteau <agateau@kde.org>
35
35
// Local
36
36
#include <lib/semanticinfo/tagmodel.h>
37
37
 
38
 
namespace Gwenview {
 
38
namespace Gwenview
 
39
{
39
40
 
40
41
TagItemDelegate::TagItemDelegate(QAbstractItemView* view)
41
42
: KWidgetItemDelegate(view, view)
42
43
{
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());
50
 
}
51
 
 
52
 
 
53
 
QList<QWidget*> TagItemDelegate::createItemWidgets() const {
54
 
 
55
 
        #define initButton(x) \
56
 
                (x)->setAutoRaise(true); \
57
 
                setBlockedEventTypes((x), QList<QEvent::Type>() \
58
 
                        << QEvent::MouseButtonPress \
59
 
                        << QEvent::MouseButtonRelease \
60
 
                        << QEvent::MouseButtonDblClick);
61
 
 
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()));
67
 
 
68
 
        QToolButton* removeButton = new QToolButton;
69
 
        initButton(removeButton);
70
 
        removeButton->setIcon(KIcon("list-remove"));
71
 
        connect(removeButton, SIGNAL(clicked()), SLOT(slotRemoveButtonClicked()));
72
 
 
73
 
        #undef initButton
74
 
 
75
 
        return QList<QWidget*>() << removeButton << assignToAllButton;
76
 
}
77
 
 
78
 
 
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);
81
 
 
82
 
        QToolButton* removeButton = static_cast<QToolButton*>(widgets[0]);
83
 
        QToolButton* assignToAllButton = static_cast<QToolButton*>(widgets[1]);
84
 
 
85
 
        QSize buttonSize(mButtonSize, option.rect.height() - 2 * mMargin);
86
 
 
87
 
        removeButton->resize(buttonSize);
88
 
        assignToAllButton->resize(buttonSize);
89
 
 
90
 
        removeButton->move(option.rect.width() - mButtonSize - mMargin, mMargin);
91
 
 
92
 
        if (fullyAssigned) {
93
 
                assignToAllButton->hide();
94
 
        } else {
95
 
                assignToAllButton->move(removeButton->x() - mButtonSize - mSpacing, mMargin);
96
 
        }
97
 
}
98
 
 
99
 
 
100
 
void TagItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
101
 
        if (!index.isValid()) {
102
 
                return;
103
 
        }
104
 
        const bool selected = option.state & QStyle::State_Selected;
105
 
        const bool fullyAssigned = index.data(TagModel::AssignmentStatusRole).toInt() == int(TagModel::FullyAssigned);
106
 
 
107
 
        itemView()->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
108
 
 
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);
114
 
        }
115
 
 
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());
121
 
}
122
 
 
123
 
 
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);
128
 
}
129
 
 
130
 
 
131
 
void TagItemDelegate::slotRemoveButtonClicked() {
132
 
        const QModelIndex index = focusedIndex();
133
 
        if (!index.isValid()) {
134
 
                kWarning() << "!index.isValid()";
135
 
                return;
136
 
        }
137
 
        emit removeTagRequested(index.data(TagModel::TagRole).toString());
138
 
}
139
 
 
140
 
 
141
 
void TagItemDelegate::slotAssignToAllButtonClicked() {
142
 
        const QModelIndex index = focusedIndex();
143
 
        if (!index.isValid()) {
144
 
                kWarning() << "!index.isValid()";
145
 
                return;
146
 
        }
147
 
        emit assignTagToAllRequested(index.data(TagModel::TagRole).toString());
148
 
}
149
 
 
 
44
#define pm(x) view->style()->pixelMetric(QStyle::x)
 
45
    mMargin     = pm(PM_ToolBarItemMargin);
 
46
    mSpacing    = pm(PM_ToolBarItemSpacing);
 
47
#undef pm
 
48
    const int iconSize = KIconLoader::global()->currentSize(KIconLoader::Toolbar);
 
49
    const QSize sz = view->style()->sizeFromContents(QStyle::CT_ToolButton, 0, QSize(iconSize, iconSize));
 
50
    mButtonSize = qMax(sz.width(), sz.height());
 
51
}
 
52
 
 
53
QList<QWidget*> TagItemDelegate::createItemWidgets() const
 
54
{
 
55
 
 
56
#define initButton(x) \
 
57
    (x)->setAutoRaise(true); \
 
58
    setBlockedEventTypes((x), QList<QEvent::Type>() \
 
59
                         << QEvent::MouseButtonPress \
 
60
                         << QEvent::MouseButtonRelease \
 
61
                         << QEvent::MouseButtonDblClick);
 
62
 
 
63
    QToolButton* assignToAllButton = new QToolButton;
 
64
    initButton(assignToAllButton);
 
65
    assignToAllButton->setIcon(KIcon("fill-color")); /* FIXME: Probably not the appropriate icon */
 
66
    assignToAllButton->setToolTip(i18n("Assign this tag to all selected images"));
 
67
    connect(assignToAllButton, SIGNAL(clicked()), SLOT(slotAssignToAllButtonClicked()));
 
68
 
 
69
    QToolButton* removeButton = new QToolButton;
 
70
    initButton(removeButton);
 
71
    removeButton->setIcon(KIcon("list-remove"));
 
72
    connect(removeButton, SIGNAL(clicked()), SLOT(slotRemoveButtonClicked()));
 
73
 
 
74
#undef initButton
 
75
 
 
76
    return QList<QWidget*>() << removeButton << assignToAllButton;
 
77
}
 
78
 
 
79
void TagItemDelegate::updateItemWidgets(const QList<QWidget*> widgets, const QStyleOptionViewItem& option, const QPersistentModelIndex& index) const
 
80
{
 
81
    const bool fullyAssigned = index.data(TagModel::AssignmentStatusRole).toInt() == int(TagModel::FullyAssigned);
 
82
 
 
83
    QToolButton* removeButton = static_cast<QToolButton*>(widgets[0]);
 
84
    QToolButton* assignToAllButton = static_cast<QToolButton*>(widgets[1]);
 
85
 
 
86
    QSize buttonSize(mButtonSize, option.rect.height() - 2 * mMargin);
 
87
 
 
88
    removeButton->resize(buttonSize);
 
89
    assignToAllButton->resize(buttonSize);
 
90
 
 
91
    removeButton->move(option.rect.width() - mButtonSize - mMargin, mMargin);
 
92
 
 
93
    if (fullyAssigned) {
 
94
        assignToAllButton->hide();
 
95
    } else {
 
96
        assignToAllButton->move(removeButton->x() - mButtonSize - mSpacing, mMargin);
 
97
    }
 
98
}
 
99
 
 
100
void TagItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
 
101
{
 
102
    if (!index.isValid()) {
 
103
        return;
 
104
    }
 
105
    const bool selected = option.state & QStyle::State_Selected;
 
106
    const bool fullyAssigned = index.data(TagModel::AssignmentStatusRole).toInt() == int(TagModel::FullyAssigned);
 
107
 
 
108
    itemView()->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);
 
109
 
 
110
    QRect textRect = option.rect;
 
111
    textRect.setLeft(textRect.left() + mMargin);
 
112
    textRect.setWidth(textRect.width() - mButtonSize - mMargin - mSpacing);
 
113
    if (!fullyAssigned) {
 
114
        textRect.setWidth(textRect.width() - mButtonSize - mSpacing);
 
115
    }
 
116
 
 
117
    painter->setPen(option.palette.color(QPalette::Normal,
 
118
                                         selected
 
119
                                         ? QPalette::HighlightedText
 
120
                                         : QPalette::Text));
 
121
    painter->drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, index.data().toString());
 
122
}
 
123
 
 
124
QSize TagItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
 
125
{
 
126
    const int width  = option.fontMetrics.width(index.data().toString());
 
127
    const int height = qMax(mButtonSize, option.fontMetrics.height());
 
128
    return QSize(width + 2 * mMargin, height + 2 * mMargin);
 
129
}
 
130
 
 
131
void TagItemDelegate::slotRemoveButtonClicked()
 
132
{
 
133
    const QModelIndex index = focusedIndex();
 
134
    if (!index.isValid()) {
 
135
        kWarning() << "!index.isValid()";
 
136
        return;
 
137
    }
 
138
    emit removeTagRequested(index.data(TagModel::TagRole).toString());
 
139
}
 
140
 
 
141
void TagItemDelegate::slotAssignToAllButtonClicked()
 
142
{
 
143
    const QModelIndex index = focusedIndex();
 
144
    if (!index.isValid()) {
 
145
        kWarning() << "!index.isValid()";
 
146
        return;
 
147
    }
 
148
    emit assignTagToAllRequested(index.data(TagModel::TagRole).toString());
 
149
}
150
150
 
151
151
} // namespace