dtkWidgetsTagCloudList.cpp 10.1 KB
Newer Older
1
/* dtkWidgetsTagCloudList.cpp ---
2
 *
3
4
 * Author: Julien Wintz
 * Created: Mon Apr 15 14:15:07 2013 (+0200)
5
 * Version:
6
7
8
9
10
11
 * Last-Updated: Mon Apr 15 15:48:15 2013 (+0200)
 *           By: Julien Wintz
 *     Update #: 40
 */

/* Change Log:
12
 *
13
14
15
16
17
18
19
20
21
22
 */

#include "dtkWidgetsTagCloudList.h"

// ///////////////////////////////////////////////////////////////////
// dtkWidgetsTagCloudListLightDelegate
// ///////////////////////////////////////////////////////////////////

class dtkWidgetsTagCloudListLightDelegate: public QStyledItemDelegate
{
23
public:
24
25
    dtkWidgetsTagCloudListLightDelegate(dtkWidgetsTagCloudList *list);

26
public:
27
28
    virtual void paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;

29
public:
30
31
    virtual QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;

32
protected:
33
34
35
36
37
38
39
40
41
    dtkWidgetsTagCloudList *list;
};

// /////////////////////////////////////////////////////////////////
// dtkWidgetsTagCloudListDarkDelegate
// /////////////////////////////////////////////////////////////////

class dtkWidgetsTagCloudListDarkDelegate: public QStyledItemDelegate
{
42
public:
43
44
    dtkWidgetsTagCloudListDarkDelegate(dtkWidgetsTagCloudList *list);

45
public:
46
47
    virtual void paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;

48
public:
49
50
    virtual QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;

51
protected:
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    dtkWidgetsTagCloudList *list;
};

// ///////////////////////////////////////////////////////////////////
// dtkWidgetsTagCloudListPrivate
// ///////////////////////////////////////////////////////////////////

class dtkWidgetsTagCloudListPrivate
{
public:
    QList<dtkWidgetsTagCloudItem *> items;
};

// ///////////////////////////////////////////////////////////////////
// dtkWidgetsTagCloudList
// ///////////////////////////////////////////////////////////////////

dtkWidgetsTagCloudList::dtkWidgetsTagCloudList(QWidget *parent) : QListWidget(parent)
{
    d = new dtkWidgetsTagCloudListPrivate;
72

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    this->setAttribute(Qt::WA_MacShowFocusRect, false);
    this->setFrameShape(QFrame::NoFrame);
    this->setDragEnabled(true);
    this->setItemDelegate(new dtkWidgetsTagCloudListLightDelegate(this));
    this->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);

    connect(this, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(onItemClicked(QListWidgetItem *)));
}

dtkWidgetsTagCloudList::~dtkWidgetsTagCloudList(void)
{
    delete d;
}

void dtkWidgetsTagCloudList::addItem(QString name)
{
    d->items << new dtkWidgetsTagCloudItem(name, QString(), QStringList());

    QListWidget::addItem(d->items.last());
}

void dtkWidgetsTagCloudList::addItem(QString name, QString description)
{
    d->items << new dtkWidgetsTagCloudItem(name, description, QStringList());

    QListWidget::addItem(d->items.last());
}

void dtkWidgetsTagCloudList::addItem(QString name, QString description, QStringList tags)
{
    d->items << new dtkWidgetsTagCloudItem(name, description, tags);

    QListWidget::addItem(d->items.last());
}

void dtkWidgetsTagCloudList::addItem(QString name, QString description, QStringList tags, QString kind, QString type)
{
    d->items << new dtkWidgetsTagCloudItem(name, description, tags, kind, type);

    QListWidget::addItem(d->items.last());
}

void dtkWidgetsTagCloudList::addItem(dtkWidgetsTagCloudItem item)
{
    d->items << new dtkWidgetsTagCloudItem(item);

    QListWidget::addItem(d->items.last());
}

void dtkWidgetsTagCloudList::clear(void)
{
    d->items.clear();

    QListWidget::clear();
}

void dtkWidgetsTagCloudList::setDark(void)
{
    this->setItemDelegate(new dtkWidgetsTagCloudListDarkDelegate(this));
}

void dtkWidgetsTagCloudList::onItemClicked(QListWidgetItem *item)
{
    dtkWidgetsTagCloudItem *i = dynamic_cast<dtkWidgetsTagCloudItem *>(item);

138
    if (!i)
139
140
141
142
143
144
145
146
147
148
149
        return;

    emit itemClicked(i->description());
}

QMimeData *dtkWidgetsTagCloudList::mimeData(const QList<QListWidgetItem *> items) const
{
    QMimeData *data = NULL;

    dtkWidgetsTagCloudItem *i = dynamic_cast<dtkWidgetsTagCloudItem *>(items.first());

150
    if (i) {
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
        data = new QMimeData;
        data->setUrls(QList<QUrl>() << QUrl(QString("%1:%2").arg(i->kind()).arg(i->type())));
    }

    return data;
}

QStringList dtkWidgetsTagCloudList::mimeTypes(void) const
{
    return QStringList() << "text/uri-list";
}

// ///////////////////////////////////////////////////////////////////
// Helper functions
// ///////////////////////////////////////////////////////////////////

167
QString dtkWidgetsTagCloudListDelegateUnhtmlize(const QString& htmlString)
168
169
170
171
{
    QString textString;

    QXmlStreamReader xml(htmlString);
172
173
174

    while (!xml.atEnd())
        if (xml.readNext() == QXmlStreamReader::Characters)
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
            textString += xml.text();

    return textString;
}

// ///////////////////////////////////////////////////////////////////
// dtkWidgetsTagCloudListLightDelegate
// ///////////////////////////////////////////////////////////////////

dtkWidgetsTagCloudListLightDelegate::dtkWidgetsTagCloudListLightDelegate(dtkWidgetsTagCloudList *list) : QStyledItemDelegate(list)
{
    this->list = list;
}

void dtkWidgetsTagCloudListLightDelegate::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    dtkWidgetsTagCloudItem *item = dynamic_cast<dtkWidgetsTagCloudItem *>(list->itemFromIndex(index));

193
    if (!item)
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
        return;

    QLinearGradient gradiant(option.rect.left(), option.rect.top(), option.rect.left(), option.rect.bottom());
    gradiant.setColorAt(0.0, QColor(247, 247, 247));
    gradiant.setColorAt(0.3, QColor(240, 240, 240));
    gradiant.setColorAt(1.0, QColor(220, 220, 220));

    painter->fillRect(option.rect, gradiant);

    painter->setPen(Qt::white);
    painter->drawLine(option.rect.topLeft() + QPoint(0, 1), option.rect.topRight() + QPoint(0, 1));

    static QPixmap arrow = QPixmap(":dtkWidgetsTagCloud/dtkWidgetsTagCloudList-arrow.png");
    static QPixmap tags = QPixmap(":dtkWidgetsTagCloud/dtkWidgetsTagCloudList-tags.png");

    static int m  =  5;
    static int h1 = 20;
    static int h2 = 20;
    static int h3 = 20;
213

214
215
216
217
218
    int w = option.rect.width();
    int h = option.rect.height();
    int t = option.rect.top();
    int r = option.rect.right();

219
220
221
    QRect name_rect = QRect(m, t + 1 * m,       w - 2 * m, h1);
    QRect desc_rect = QRect(m, t + 2 * m + h1,    w - 6 * m, h2);
    QRect tags_rect = QRect(m, t + 3 * m + h1 + h2, w - 2 * m, h3);
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236

    QFontMetrics metrics = QFontMetrics(painter->font());

    painter->setPen(Qt::black);
    painter->drawText(name_rect, Qt::AlignLeft | Qt::AlignTop, item->name());

    painter->setPen(Qt::gray);
    painter->drawText(desc_rect, Qt::AlignLeft | Qt::AlignTop, metrics.elidedText(dtkWidgetsTagCloudListDelegateUnhtmlize(item->description()), Qt::ElideRight, desc_rect.width()));

    painter->setPen(QColor("#6a769d"));
    painter->drawText(tags_rect.adjusted(m + tags.width(), 0, -tags.width(), 0), Qt::AlignLeft | Qt::AlignTop, item->tags().join(", "));

    painter->setPen(Qt::darkGray);
    painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());

237
    QPointF arrow_pos = QPointF(r - m - arrow.width(), t + h / 2 - arrow.height() / 2);
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
    painter->drawPixmap(arrow_pos, arrow);
    painter->drawPixmap(tags_rect.topLeft(), tags);
}

QSize dtkWidgetsTagCloudListLightDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    Q_UNUSED(option);
    Q_UNUSED(index);

    return QSize(100, 80);
}

// ///////////////////////////////////////////////////////////////////
// dtkWidgetsTagCloudListDarkDelegate
// ///////////////////////////////////////////////////////////////////

dtkWidgetsTagCloudListDarkDelegate::dtkWidgetsTagCloudListDarkDelegate(dtkWidgetsTagCloudList *list) : QStyledItemDelegate(list)
{
    this->list = list;
}

void dtkWidgetsTagCloudListDarkDelegate::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    dtkWidgetsTagCloudItem *item = dynamic_cast<dtkWidgetsTagCloudItem *>(list->itemFromIndex(index));

263
    if (!item)
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
        return;

    QLinearGradient gradiant(option.rect.left(), option.rect.top(), option.rect.left(), option.rect.bottom());
    gradiant.setColorAt(0.0, QColor(047, 047, 047));
    gradiant.setColorAt(0.3, QColor(040, 040, 040));
    gradiant.setColorAt(1.0, QColor(020, 020, 020));

    painter->fillRect(option.rect, gradiant);

    painter->setPen(QColor(055, 055, 055));
    painter->drawLine(option.rect.topLeft() + QPoint(0, 1), option.rect.topRight() + QPoint(0, 1));

    static QPixmap arrow = QPixmap(":dtkWidgetsTagCloud/dtkWidgetsTagCloudList-arrow.png");
    static QPixmap tags = QPixmap(":dtkWidgetsTagCloud/dtkWidgetsTagCloudList-tags.png");

    static int m  =  5;
    static int h1 = 20;
    static int h2 = 20;
    static int h3 = 20;
283

284
285
286
287
288
    int w = option.rect.width();
    int h = option.rect.height();
    int t = option.rect.top();
    int r = option.rect.right();

289
290
291
    QRect name_rect = QRect(m, t + 1 * m,       w - 2 * m, h1);
    QRect desc_rect = QRect(m, t + 2 * m + h1,    w - 6 * m, h2);
    QRect tags_rect = QRect(m, t + 3 * m + h1 + h2, w - 2 * m, h3);
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306

    QFontMetrics metrics = QFontMetrics(painter->font());

    painter->setPen(Qt::lightGray);
    painter->drawText(name_rect, Qt::AlignLeft | Qt::AlignTop, item->name());

    painter->setPen(Qt::darkGray);
    painter->drawText(desc_rect, Qt::AlignLeft | Qt::AlignTop, metrics.elidedText(dtkWidgetsTagCloudListDelegateUnhtmlize(item->description()), Qt::ElideRight, desc_rect.width()));

    painter->setPen(QColor("#6a769d"));
    painter->drawText(tags_rect.adjusted(m + tags.width(), 0, -tags.width(), 0), Qt::AlignLeft | Qt::AlignTop, item->tags().join(", "));

    painter->setPen(Qt::black);
    painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());

307
    QPointF arrow_pos = QPointF(r - m - arrow.width(), t + h / 2 - arrow.height() / 2);
308
309
310
311
312
313
314
315
316
317
318
319
    painter->drawPixmap(arrow_pos, arrow);
    painter->drawPixmap(tags_rect.topLeft(), tags);
}

QSize dtkWidgetsTagCloudListDarkDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    Q_UNUSED(option);
    Q_UNUSED(index);

    return QSize(100, 80);
}