Attention une mise à jour du service Gitlab va être effectuée le mardi 30 novembre entre 17h30 et 18h00. Cette mise à jour va générer une interruption du service dont nous ne maîtrisons pas complètement la durée mais qui ne devrait pas excéder quelques minutes. Cette mise à jour intermédiaire en version 14.0.12 nous permettra de rapidement pouvoir mettre à votre disposition une version plus récente.

dtkWidgetsTagCloudList.cpp 12.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// Version: $Id$
//
//

// Commentary:
//
//

// Change Log:
//
//

// Code:
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
    dtkWidgetsTagCloudList *list;
};

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
// /////////////////////////////////////////////////////////////////
// dtkWidgetsTagCloudListDoomDelegate
// /////////////////////////////////////////////////////////////////

class dtkWidgetsTagCloudListDoomDelegate: public QStyledItemDelegate
{
public:
    dtkWidgetsTagCloudListDoomDelegate(dtkWidgetsTagCloudList *list);

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

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

protected:
    dtkWidgetsTagCloudList *list;
};

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
// ///////////////////////////////////////////////////////////////////
// dtkWidgetsTagCloudListPrivate
// ///////////////////////////////////////////////////////////////////

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

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

dtkWidgetsTagCloudList::dtkWidgetsTagCloudList(QWidget *parent) : QListWidget(parent)
{
    d = new dtkWidgetsTagCloudListPrivate;
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    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));
}

153 154 155 156 157
void dtkWidgetsTagCloudList::setDoom(void)
{
    this->setItemDelegate(new dtkWidgetsTagCloudListDoomDelegate(this));
}

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

162
    if (!i)
163 164 165 166 167 168 169 170 171 172 173
        return;

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

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

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

174
    if (i) {
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
        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
// ///////////////////////////////////////////////////////////////////

191
QString dtkWidgetsTagCloudListDelegateUnhtmlize(const QString& htmlString)
192 193 194 195
{
    QString textString;

    QXmlStreamReader xml(htmlString);
196 197 198

    while (!xml.atEnd())
        if (xml.readNext() == QXmlStreamReader::Characters)
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
            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));

217
    if (!item)
218 219
        return;

220
    painter->fillRect(option.rect, QColor("#efefef"));
221 222 223 224 225 226 227 228 229 230
    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;
231

232 233 234 235 236
    int w = option.rect.width();
    int h = option.rect.height();
    int t = option.rect.top();
    int r = option.rect.right();

Julien Wintz's avatar
Julien Wintz committed
237 238
    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);
239
    QRect tags_rect = QRect(m, t + 3 * m + h1 + h2, w - 2 * m, h3);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

    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());

255
    QPointF arrow_pos = QPointF(r - m - arrow.width(), t + h / 2 - arrow.height() / 2);
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    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));

281
    if (!item)
282 283
        return;

284
    painter->fillRect(option.rect, QColor(020, 020, 020));
285 286 287 288 289 290 291 292 293 294
    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;
295

296 297 298 299 300
    int w = option.rect.width();
    int h = option.rect.height();
    int t = option.rect.top();
    int r = option.rect.right();

301 302
    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);
303
    QRect tags_rect = QRect(m, t + 3 * m + h1 + h2, w - 2 * m, h3);
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318

    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());

319
    QPointF arrow_pos = QPointF(r - m - arrow.width(), t + h / 2 - arrow.height() / 2);
320 321 322 323 324 325 326 327 328 329 330 331
    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);
}

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
// ///////////////////////////////////////////////////////////////////
// dtkWidgetsTagCloudListDoomDelegate
// ///////////////////////////////////////////////////////////////////

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

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

    if (!item)
        return;

    painter->fillRect(option.rect, QColor("#282C34"));
    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;

    int w = option.rect.width();
    int h = option.rect.height();
    int t = option.rect.top();
    int r = option.rect.right();

    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);

    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());

    QPointF arrow_pos = QPointF(r - m - arrow.width(), t + h / 2 - arrow.height() / 2);
    painter->drawPixmap(arrow_pos, arrow);
    painter->drawPixmap(tags_rect.topLeft(), tags);
}

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

    return QSize(100, 80);
}

//
// dtkWidgetsTagCloudList.cpp ends here