~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy

« back to all changes in this revision

Viewing changes to libs/plasma/appletbrowser/kcategorizeditemsviewdelegate.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-10-11 14:04:48 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071011140448-v0eb7lxbb24zagca
Tags: 3.94.0-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright (C) 2007 Ivan Cukic <ivan.cukic+kde@gmail.com>
 
3
 *
 
4
 *   This program is free software; you can redistribute it and/or modify
 
5
 *   it under the terms of the GNU General Public License version 2 (or,
 
6
 *   at your option, any later version) as published by the Free Software
 
7
 *   Foundation
 
8
 *
 
9
 *   This program is distributed in the hope that it will be useful,
 
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *   GNU General Public License for more details
 
13
 *
 
14
 *   You should have received a copy of the GNU Library General Public
 
15
 *   License along with this program; if not, write to the
 
16
 *   Free Software Foundation, Inc.,
 
17
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
18
 */
 
19
 
 
20
#include "kcategorizeditemsviewdelegate_p.h"
 
21
 
 
22
#include <cmath>
 
23
 
 
24
#include <QtCore>
 
25
 
 
26
#include <KIconLoader>
 
27
 
 
28
#include "kcategorizeditemsview_p.h"
 
29
 
 
30
#define FAV_ICON_SIZE 24
 
31
#define EMBLEM_ICON_SIZE 16
 
32
#define UNIVERSAL_PADDING 6
 
33
#define FADE_LENGTH 32
 
34
#define MAIN_ICON_SIZE 48
 
35
#define DROPDOWN_PADDING 2
 
36
#define DROPDOWN_SEPARATOR_HEIGHT 32
 
37
 
 
38
KCategorizedItemsViewDelegate::KCategorizedItemsViewDelegate(QObject * parent)
 
39
        : QItemDelegate(parent), m_favoriteIcon("bookmark"), 
 
40
        m_favoriteAddIcon("edit-add"), m_favoriteRemoveIcon("edit-delete"),
 
41
        m_onFavoriteIconItem(NULL)
 
42
{
 
43
    m_parent = (KCategorizedItemsView *) parent;
 
44
}
 
45
 
 
46
void KCategorizedItemsViewDelegate::paint(QPainter *painter,
 
47
        const QStyleOptionViewItem &option, const QModelIndex &index) const
 
48
{
 
49
 
 
50
    // Preparing needed data for painting
 
51
    int left = option.rect.left();
 
52
    int top = option.rect.top();
 
53
    int width = option.rect.width();
 
54
    int height = option.rect.height();
 
55
    bool leftToRight = (painter->layoutDirection() == Qt::LeftToRight);
 
56
 
 
57
    QIcon::Mode iconMode = QIcon::Normal;
 
58
 
 
59
    QColor backgroundColor = (option.state.testFlag(QStyle::State_Selected))?
 
60
            option.palette.color(QPalette::Highlight):option.palette.color(QPalette::Base);
 
61
    QColor foregroundColor = (option.state.testFlag(QStyle::State_Selected))?
 
62
            option.palette.color(QPalette::HighlightedText):option.palette.color(QPalette::Text);
 
63
 
 
64
    KCategorizedItemsViewModels::AbstractItem * item =
 
65
        getItemByProxyIndex(index);
 
66
    if (!item) return;
 
67
 
 
68
    // Base Background
 
69
    painter->fillRect(option.rect, QBrush(backgroundColor));
 
70
 
 
71
    if (index.column() == 0) {
 
72
        // Painting main column             
 
73
        QStyleOptionViewItem local_option_title(option);
 
74
        QStyleOptionViewItem local_option_normal(option);
 
75
 
 
76
        local_option_title.font.setBold(true);
 
77
        local_option_title.font.setPointSize(local_option_title.font.pointSize() + 2);
 
78
 
 
79
        QLinearGradient gradient;
 
80
 
 
81
        QString title = item->name();
 
82
        QString description = item->description();
 
83
 
 
84
        // Painting
 
85
 
 
86
        // Text
 
87
        int textInner = 2 * UNIVERSAL_PADDING + MAIN_ICON_SIZE;
 
88
 
 
89
        painter->setPen(foregroundColor);
 
90
        painter->setFont(local_option_title.font);
 
91
        painter->drawText(
 
92
            left + (leftToRight ? textInner : 0),
 
93
            top + UNIVERSAL_PADDING,
 
94
            width - textInner, MAIN_ICON_SIZE / 2,
 
95
            Qt::AlignBottom | Qt::AlignLeft, title);
 
96
        painter->setFont(local_option_normal.font);
 
97
        painter->drawText(
 
98
            left + (leftToRight ? textInner : 0),
 
99
            top + UNIVERSAL_PADDING + MAIN_ICON_SIZE / 2,
 
100
            width - textInner, MAIN_ICON_SIZE / 2,
 
101
            Qt::AlignTop | Qt::AlignLeft, description);
 
102
 
 
103
        // Main icon
 
104
        item->icon().paint(painter, 
 
105
                leftToRight ? left + UNIVERSAL_PADDING : left + width - UNIVERSAL_PADDING - MAIN_ICON_SIZE,
 
106
            top + UNIVERSAL_PADDING, 
 
107
            MAIN_ICON_SIZE, MAIN_ICON_SIZE, Qt::AlignCenter, iconMode);
 
108
 
 
109
        // Counting the number of emblems for this item
 
110
        int emblemCount = 0;
 
111
        QPair < Filter, QIcon * > emblem;
 
112
        foreach (emblem, m_parent->m_emblems) {
 
113
            if (item->passesFiltering(emblem.first)) ++emblemCount;
 
114
        }
 
115
 
 
116
        // Gradient part of the background - fading of the text at the end
 
117
        if (leftToRight) {
 
118
            gradient = QLinearGradient(left + width - UNIVERSAL_PADDING - FADE_LENGTH, 0, 
 
119
                left + width - UNIVERSAL_PADDING, 0);
 
120
            gradient.setColorAt(1, backgroundColor);
 
121
            backgroundColor.setAlpha(0);
 
122
            gradient.setColorAt(0, backgroundColor);
 
123
        } else {
 
124
            gradient = QLinearGradient(left + UNIVERSAL_PADDING, 0, 
 
125
                left + UNIVERSAL_PADDING + FADE_LENGTH, 0);
 
126
            gradient.setColorAt(0, backgroundColor);
 
127
            backgroundColor.setAlpha(0);
 
128
            gradient.setColorAt(1, backgroundColor);
 
129
 
 
130
        }
 
131
 
 
132
        QRect paintRect = option.rect;
 
133
        painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
 
134
        painter->fillRect(paintRect, gradient);
 
135
 
 
136
        if (leftToRight) {
 
137
            gradient.setStart(left + width
 
138
                - emblemCount * (UNIVERSAL_PADDING + EMBLEM_ICON_SIZE) - FADE_LENGTH, 0);
 
139
            gradient.setFinalStop(left + width
 
140
                - emblemCount * (UNIVERSAL_PADDING + EMBLEM_ICON_SIZE), 0);
 
141
        } else {
 
142
            gradient.setStart(left + UNIVERSAL_PADDING
 
143
                + emblemCount * (UNIVERSAL_PADDING + EMBLEM_ICON_SIZE), 0);
 
144
            gradient.setFinalStop(left + UNIVERSAL_PADDING
 
145
                + emblemCount * (UNIVERSAL_PADDING + EMBLEM_ICON_SIZE) + FADE_LENGTH, 0);
 
146
        }
 
147
        paintRect.setHeight(UNIVERSAL_PADDING + MAIN_ICON_SIZE / 2);
 
148
        painter->fillRect(paintRect, gradient);
 
149
 
 
150
        // Emblems icons
 
151
        int emblemLeft = leftToRight ? (left + width - EMBLEM_ICON_SIZE) : left; // - FAV_ICON_SIZE - 2 * UNIVERSAL_PADDING
 
152
        foreach (emblem, m_parent->m_emblems) {
 
153
            if (item->passesFiltering(emblem.first)) {
 
154
                emblem.second->paint(painter, 
 
155
                    emblemLeft, top + UNIVERSAL_PADDING, 
 
156
                    EMBLEM_ICON_SIZE, EMBLEM_ICON_SIZE, Qt::AlignCenter, iconMode);
 
157
                if (leftToRight) {
 
158
                    emblemLeft -= UNIVERSAL_PADDING + EMBLEM_ICON_SIZE;
 
159
                } else {
 
160
                    emblemLeft += UNIVERSAL_PADDING + EMBLEM_ICON_SIZE;
 
161
                }
 
162
            }
 
163
        }
 
164
 
 
165
    } else {
 
166
        // Painting favorite icon column
 
167
 
 
168
        KCategorizedItemsViewModels::AbstractItem * item =
 
169
            getItemByProxyIndex(index);
 
170
 
 
171
        if (! (option.state & QStyle::State_MouseOver) && m_onFavoriteIconItem == item)
 
172
            m_onFavoriteIconItem = NULL;
 
173
 
 
174
        QIcon::Mode iconMode = QIcon::Normal;
 
175
        if (!item->isFavorite()) {
 
176
          iconMode = QIcon::Disabled;
 
177
        } else if (option.state & QStyle::State_MouseOver) {
 
178
          iconMode = QIcon::Active;
 
179
        } 
 
180
 
 
181
        m_favoriteIcon.paint(painter, 
 
182
            left + width - FAV_ICON_SIZE - UNIVERSAL_PADDING, top + UNIVERSAL_PADDING, 
 
183
            FAV_ICON_SIZE, FAV_ICON_SIZE, Qt::AlignCenter, iconMode);
 
184
 
 
185
        const KIcon * icon = (item->isFavorite())? & m_favoriteRemoveIcon : & m_favoriteAddIcon;
 
186
 
 
187
        if ((option.state & QStyle::State_MouseOver) && (m_onFavoriteIconItem != item))
 
188
            icon->paint(painter, 
 
189
                left + width - EMBLEM_ICON_SIZE - UNIVERSAL_PADDING, top + UNIVERSAL_PADDING, 
 
190
                EMBLEM_ICON_SIZE, EMBLEM_ICON_SIZE, Qt::AlignCenter, iconMode);
 
191
 
 
192
    }
 
193
 
 
194
    // Dividing line 
 
195
    backgroundColor = option.palette.color(QPalette::Highlight);
 
196
    backgroundColor.setAlpha(100);
 
197
    painter->setPen(backgroundColor);
 
198
    painter->drawLine(left, top + height - 1, left + width, top + height - 1);
 
199
 
 
200
}
 
201
 
 
202
bool KCategorizedItemsViewDelegate::editorEvent(QEvent *event,
 
203
                               QAbstractItemModel *model,
 
204
                               const QStyleOptionViewItem &option,
 
205
                               const QModelIndex &index)
 
206
{
 
207
    if (index.column() == 1 && event->type() == QEvent::MouseButtonPress) {
 
208
        (m_onFavoriteIconItem = getItemByProxyIndex(index))
 
209
             ->setFavorite(!getItemByProxyIndex(index)->isFavorite());
 
210
 
 
211
        return true;
 
212
    }
 
213
 
 
214
    return QItemDelegate::editorEvent(event, model, option, index);
 
215
}
 
216
 
 
217
QSize KCategorizedItemsViewDelegate::sizeHint(const QStyleOptionViewItem &option,
 
218
        const QModelIndex &index ) const
 
219
{
 
220
    Q_UNUSED(option);
 
221
 
 
222
    //Q_UNUSED(index);
 
223
    //option.
 
224
    int width = (index.column() == 0) ? 0 : FAV_ICON_SIZE;
 
225
    return QSize(width, MAIN_ICON_SIZE + 2 * UNIVERSAL_PADDING);
 
226
}
 
227
 
 
228
int KCategorizedItemsViewDelegate::columnWidth (int column, int viewWidth) const {
 
229
    if (column == 1) {
 
230
        return FAV_ICON_SIZE + 2 * UNIVERSAL_PADDING;
 
231
    } else return viewWidth - columnWidth(1, viewWidth);
 
232
}
 
233
 
 
234
 
 
235
KCategorizedItemsViewModels::AbstractItem * KCategorizedItemsViewDelegate::getItemByProxyIndex(const QModelIndex & index) const { 
 
236
    return (AbstractItem *) m_parent->m_modelItems->itemFromIndex(
 
237
        m_parent->m_modelFilterItems->mapToSource(index)
 
238
    );
 
239
}
 
240
 
 
241
//     KCategorizedItemsViewFilterDelegate
 
242
 
 
243
 
 
244
KCategorizedItemsViewFilterDelegate::KCategorizedItemsViewFilterDelegate(QObject *parent)
 
245
    : QItemDelegate(parent) {
 
246
    kDebug() << "KCategorizedItemsViewFilterDelegate(QObject *parent)\n";
 
247
 
 
248
}
 
249
 
 
250
void KCategorizedItemsViewFilterDelegate::paint(QPainter *painter,
 
251
        const QStyleOptionViewItem &option, const QModelIndex &index) const
 
252
{
 
253
    if (index.flags() & Qt::ItemIsEnabled) {
 
254
        QItemDelegate::paint(painter, option, index);
 
255
    } else {
 
256
        QStyleOptionViewItem separatorOption(option);
 
257
        int height = QItemDelegate::sizeHint(option, index).height() + 2 * DROPDOWN_PADDING;
 
258
 
 
259
        separatorOption.state &= ~(QStyle::State_Selected
 
260
                | QStyle::State_MouseOver | QStyle::State_HasFocus);
 
261
        separatorOption.rect.setTop(separatorOption.rect.top() + separatorOption.rect.height() - height);
 
262
        separatorOption.rect.setHeight(height);
 
263
        QItemDelegate::paint(painter, separatorOption, index);
 
264
        /*painter->drawLine(
 
265
                option.rect.left(), 
 
266
                option.rect.top() + 1,
 
267
                option.rect.left() + option.rect.width(),
 
268
                option.rect.top() + 1);*/
 
269
    }
 
270
}
 
271
 
 
272
QSize KCategorizedItemsViewFilterDelegate::sizeHint(const QStyleOptionViewItem &option,
 
273
        const QModelIndex &index ) const
 
274
{
 
275
    QSize size = QItemDelegate::sizeHint(option, index);
 
276
    if (index.flags() & Qt::ItemIsEnabled) {
 
277
        size.setHeight(size.height() + 2 * DROPDOWN_PADDING);
 
278
    } else {
 
279
        size.setHeight(DROPDOWN_SEPARATOR_HEIGHT);
 
280
    }
 
281
    return size;
 
282
}
 
283
 
 
284
#include "kcategorizeditemsviewdelegate_p.moc"
 
285