Subversion Repositories Scribus

Rev

Rev 24137 | Rev 24892 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
7747 jghali 1
/*
2
For general Scribus (>=1.3.2) copyright and licensing information please refer
3
to the COPYING file provided with the program. Following this notice may exist
4
a copyright and/or license notice that predates the release of Scribus 1.3.2
5
for which a new license (GPL+exception) is in place.
6
*/
7
#include "colorlistbox.h"
21837 jghali 8
 
9
#include <cstdlib>
10223 cbradney 10
#include <QBitmap>
21837 jghali 11
#include <QCursor>
9575 fschmid 12
#include <QEvent>
13
#include <QHelpEvent>
15662 fschmid 14
#include <QMenu>
21837 jghali 15
#include <QPainter>
21855 jghali 16
#include <QPersistentModelIndex>
21837 jghali 17
#include <QPixmap>
18
#include <QSignalBlocker>
19
#include <QToolTip>
7747 jghali 20
 
21855 jghali 21
#include "colorlistmodel.h"
10203 cbradney 22
#include "commonstrings.h"
21837 jghali 23
#include "iconmanager.h"
10203 cbradney 24
#include "sccolorengine.h"
7747 jghali 25
#include "scconfig.h"
23474 jghali 26
#include "scribusapp.h"
7747 jghali 27
#include "scribusdoc.h"
10203 cbradney 28
#include "util_color.h"
7747 jghali 29
 
23423 jghali 30
class SCRIBUS_API ColorSmallItemDelegate : public ScListBoxPixmap<15, 15>
9874 avox 31
{
32
public:
23423 jghali 33
        ColorSmallItemDelegate(): ScListBoxPixmap<15, 15>() {};
24736 jghali 34
        ~ColorSmallItemDelegate() = default;
9874 avox 35
 
23423 jghali 36
        void redraw(const QVariant&) const override;
37
        QString text(const QVariant&) const override;
9874 avox 38
};
39
 
23423 jghali 40
class SCRIBUS_API ColorWideItemDelegate : public ScListBoxPixmap<30, 15>
9874 avox 41
{
42
public:
23423 jghali 43
        ColorWideItemDelegate(): ScListBoxPixmap<30, 15>() {};
24736 jghali 44
        ~ColorWideItemDelegate() = default;
9874 avox 45
 
23423 jghali 46
        void redraw(const QVariant&) const override;
47
        QString text(const QVariant&) const override;
9874 avox 48
};
49
 
23423 jghali 50
class SCRIBUS_API ColorFancyItemDelegate : public ScListBoxPixmap<60, 15>
9874 avox 51
{
52
public:
23474 jghali 53
        ColorFancyItemDelegate();
24736 jghali 54
        ~ColorFancyItemDelegate() = default;
9874 avox 55
 
23474 jghali 56
        void iconSetChange();
23423 jghali 57
        void redraw(const QVariant&) const override;
58
        QString text(const QVariant&) const override;
23474 jghali 59
 
60
private:
61
        QPixmap alertIcon;
62
        QPixmap cmykIcon;
63
        QPixmap rgbIcon;
64
        QPixmap labIcon;
65
        QPixmap spotIcon;
66
        QPixmap regIcon;
9874 avox 67
};
68
 
23474 jghali 69
ColorFancyItemDelegate::ColorFancyItemDelegate() : ScListBoxPixmap<60, 15>()
70
{
71
        iconSetChange();
72
}
9874 avox 73
 
74
void ColorSmallItemDelegate::redraw(const QVariant& data) const
75
{
23423 jghali 76
        QPixmap* pPixmap = ScListBoxPixmap<15, 15>::pmap.data();
10674 fschmid 77
        pPixmap->fill(Qt::transparent);
78
        if (data.canConvert<ColorPixmapValue>())
79
        {
80
                ColorPixmapValue item(data.value<ColorPixmapValue>());
81
                QColor rgb = ScColorEngine::getDisplayColor(item.m_color, item.m_doc);
82
                pPixmap->fill(rgb);
83
                QPainter painter(pPixmap);
84
                painter.setBrush(Qt::NoBrush);
85
                QPen b(Qt::black, 1);
86
                painter.setPen(b);
87
                painter.drawRect(0, 0, 15, 15);
88
                painter.end();
89
        }
7747 jghali 90
}
91
 
9874 avox 92
void ColorWideItemDelegate::redraw(const QVariant& data) const
7747 jghali 93
{
23423 jghali 94
        QPixmap* pPixmap = ScListBoxPixmap<30, 15>::pmap.data();
10674 fschmid 95
        pPixmap->fill(Qt::transparent);
96
        if (data.canConvert<ColorPixmapValue>())
97
        {
98
                ColorPixmapValue item(data.value<ColorPixmapValue>());
99
                QColor rgb = ScColorEngine::getDisplayColor(item.m_color, item.m_doc);
100
                pPixmap->fill(rgb);
101
        }
7747 jghali 102
}
103
 
9874 avox 104
void ColorFancyItemDelegate::redraw(const QVariant& data) const
7747 jghali 105
{
106
        static QPixmap smallPix(15, 15);
107
 
23423 jghali 108
        QPixmap* pPixmap = ScListBoxPixmap<60, 15>::pmap.data();
9874 avox 109
        pPixmap->fill(Qt::transparent);
7747 jghali 110
 
9874 avox 111
        if (data.canConvert<ColorPixmapValue>())
7747 jghali 112
        {
9874 avox 113
                ColorPixmapValue item(data.value<ColorPixmapValue>());
114
 
115
                QColor rgb = ScColorEngine::getDisplayColor(item.m_color, item.m_doc);
116
                smallPix.fill(rgb);
117
                QPainter painter(&smallPix);
118
                painter.setBrush(Qt::NoBrush);
119
                QPen b(Qt::black, 1);
120
                painter.setPen(b);
121
                painter.drawRect(0, 0, 15, 15);
122
                painter.end();
123
 
124
                paintAlert(smallPix, *pPixmap, 0, 0);
125
                bool isOutOfGamut = ScColorEngine::isOutOfGamut(item.m_color, item.m_doc);
126
                if (isOutOfGamut)
127
                        paintAlert(alertIcon, *pPixmap, 15, 0);
11258 jghali 128
                if (item.m_color.getColorModel() == colorModelCMYK)
9874 avox 129
                        paintAlert(cmykIcon, *pPixmap, 30, 0);
20145 fschmid 130
                else if (item.m_color.getColorModel() == colorModelRGB)
9874 avox 131
                        paintAlert(rgbIcon, *pPixmap, 30, 0);
20145 fschmid 132
                else if (item.m_color.getColorModel() == colorModelLab)
133
                        paintAlert(labIcon, *pPixmap, 30, 0);
11258 jghali 134
                if (item.m_color.isSpotColor())
135
                        paintAlert(spotIcon, *pPixmap, 45, 0);
136
                if (item.m_color.isRegistrationColor())
137
                        paintAlert(regIcon, *pPixmap, 46, 0);
7747 jghali 138
        }
139
}
140
 
9874 avox 141
 
142
QString ColorSmallItemDelegate::text(const QVariant& data) const
7747 jghali 143
{
9874 avox 144
        if (data.canConvert<ColorPixmapValue>())
145
                return data.value<ColorPixmapValue>().m_name;
22632 craig 146
        return data.toString();
9874 avox 147
}
148
 
149
QString ColorWideItemDelegate::text(const QVariant& data) const
150
{
151
        if (data.canConvert<ColorPixmapValue>())
152
                return data.value<ColorPixmapValue>().m_name;
22632 craig 153
        return data.toString();
9874 avox 154
}
155
 
156
QString ColorFancyItemDelegate::text(const QVariant& data) const
157
{
158
        if (data.canConvert<ColorPixmapValue>())
159
                return data.value<ColorPixmapValue>().m_name;
22632 craig 160
        return data.toString();
9874 avox 161
}
162
 
23474 jghali 163
void ColorFancyItemDelegate::iconSetChange()
164
{
165
        IconManager& iconManager = IconManager::instance();
9874 avox 166
 
23474 jghali 167
        alertIcon = iconManager.loadPixmap("alert.png", true);
168
        cmykIcon = iconManager.loadPixmap("cmyk.png", true);
169
        rgbIcon = iconManager.loadPixmap("rgb.png", true);
170
        labIcon = iconManager.loadPixmap("lab.png", true);
171
        spotIcon = iconManager.loadPixmap("spot.png", true);
172
        regIcon = iconManager.loadPixmap("register.png", true);
173
}
174
 
15695 fschmid 175
int ColorListBox::initialized;
176
int ColorListBox::sortRule;
177
 
10469 cbradney 178
ColorListBox::ColorListBox(QWidget * parent)
21855 jghali 179
        : QListView(parent)
9874 avox 180
{
15695 fschmid 181
        if (initialized != 12345)
182
                sortRule = 0;
183
        initialized = 12345;
21855 jghali 184
        QListView::setModel(new ColorListModel(this));
21664 jghali 185
        setPixmapType(ColorListBox::widePixmap);
21855 jghali 186
 
187
        connect(this, SIGNAL(clicked(QModelIndex)),       this, SLOT(emitItemClicked(QModelIndex)));
188
        connect(this, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(emitItemDoubleClicked(QModelIndex)));
189
        connect(this->selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)),
190
            this, SLOT(emitCurrentChanged(QModelIndex, QModelIndex)));
191
        connect(this->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
192
            this, SIGNAL(itemSelectionChanged()));
21837 jghali 193
        connect(this, SIGNAL(contextMenuRequested()), this, SLOT(slotRightClick()));
7747 jghali 194
}
195
 
21664 jghali 196
ColorListBox::ColorListBox(ColorListBox::PixmapType type, QWidget * parent)
21855 jghali 197
        : QListView(parent)
21664 jghali 198
{
22521 craig 199
        cList = nullptr;
21664 jghali 200
        if (initialized != 12345)
201
                sortRule = 0;
202
        initialized = 12345;
21855 jghali 203
        QListView::setModel(new ColorListModel(this));
21664 jghali 204
        setPixmapType(type);
21855 jghali 205
 
23474 jghali 206
        connect(ScQApp, SIGNAL(iconSetChanged()), this, SLOT(iconSetChange()));
207
 
21855 jghali 208
        connect(this, SIGNAL(clicked(QModelIndex)),       this, SLOT(emitItemClicked(QModelIndex)));
209
        connect(this, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(emitItemDoubleClicked(QModelIndex)));
210
        connect(this->selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)),
211
            this, SLOT(emitCurrentChanged(QModelIndex, QModelIndex)));
212
        connect(this->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
213
            this, SIGNAL(itemSelectionChanged()));
21837 jghali 214
        connect(this, SIGNAL(contextMenuRequested()), this, SLOT(slotRightClick()));
21664 jghali 215
}
216
 
21563 jghali 217
ColorListBox::~ColorListBox()
218
{
219
        if (itemDelegate())
220
                delete itemDelegate();
221
        clear();
222
}
223
 
21855 jghali 224
void ColorListBox::clear()
225
{
226
        QAbstractItemModel* itemModel = model();
227
        itemModel->removeRows(0, itemModel->rowCount());
228
}
229
 
230
int ColorListBox::count() const
231
{
232
        return this->model()->rowCount();
233
}
234
 
16861 jghali 235
void ColorListBox::changeEvent(QEvent *e)
236
{
237
        if (e->type() == QEvent::LanguageChange)
238
        {
239
                languageChange();
240
                return;
241
        }
21855 jghali 242
        QListView::changeEvent(e);
16861 jghali 243
}
9874 avox 244
 
21855 jghali 245
void ColorListBox::emitCurrentChanged(const QModelIndex &current, const QModelIndex &previous)
246
{
247
        QPersistentModelIndex persistentCurrent = current;
248
 
249
        QString text = model()->data(current, Qt::DisplayRole).toString();
250
        emit currentTextChanged(text);
251
        emit currentRowChanged(persistentCurrent.row());
252
}
253
 
254
void ColorListBox::emitItemClicked(const QModelIndex &current)
255
{
256
        QPersistentModelIndex persistentCurrent = current;
257
        emit itemClicked(persistentCurrent.row());
258
}
259
 
260
void ColorListBox::emitItemDoubleClicked(const QModelIndex &current)
261
{
262
        QPersistentModelIndex persistentCurrent = current;
263
        emit itemDoubleClicked(persistentCurrent.row());
264
}
265
 
23474 jghali 266
void ColorListBox::iconSetChange()
267
{
268
        if (m_type == ColorListBox::fancyPixmap)
269
        {
270
                QAbstractItemDelegate* curDelegate = itemDelegate();
271
                ColorFancyItemDelegate* colorDelegate = dynamic_cast<ColorFancyItemDelegate*>(curDelegate);
272
                if (colorDelegate)
273
                {
274
                        colorDelegate->iconSetChange();
275
                        this->update();
276
                }
277
        }
278
}
279
 
16861 jghali 280
void ColorListBox::languageChange()
281
{
21855 jghali 282
        // Not needed anymore normally: on language change a paintEvent is sent to widget
283
        // and model will return the new translated string for None color
284
        /*if (this->count() > 0)
16861 jghali 285
        {
286
                QModelIndexList result;
287
                QModelIndex start = model()->index(0, 0, this->rootIndex());
288
                result =  model()->match(start, Qt::UserRole, CommonStrings::None, 1, Qt::MatchExactly | Qt::MatchCaseSensitive);
289
                if (result.isEmpty())
290
                        return;
291
                int index = result.first().row();
292
                QListWidgetItem* item = this->item(index);
293
                item->setText(CommonStrings::tr_NoneColor);
21855 jghali 294
        }*/
16861 jghali 295
}
296
 
9874 avox 297
QString ColorListBox::currentColor() const
298
{
299
        if (currentRow() >= 0)
21855 jghali 300
        {
301
                QAbstractItemModel* itemModel = model();
302
                return itemModel->data(currentIndex(), Qt::DisplayRole).toString();
303
        }
22632 craig 304
        return CommonStrings::tr_NoneColor;
9874 avox 305
}
306
 
21855 jghali 307
int ColorListBox::currentRow() const
308
{
309
        return currentIndex().row();
310
}
311
 
312
QVariant ColorListBox::data(int row, int role) const
313
{
314
        QModelIndex index = model()->index(row, 0);
315
        return model()->data(index, role);
316
}
317
 
318
QStringList ColorListBox::findColors(const QString &name, Qt::MatchFlags flags) const
319
{
320
        QStringList foundColors;
321
        QAbstractItemModel* currentModel = model();
322
 
323
        QModelIndex firstIndex = currentModel->index(0, 0, QModelIndex());
324
        QModelIndexList indexes = currentModel->match(firstIndex, Qt::DisplayRole, name, -1, flags);
325
        for (int i = 0; i < indexes.count(); ++i)
326
        {
327
                QModelIndex modelIndex = indexes.at(i);
328
                QVariant modelData = currentModel->data(modelIndex, Qt::DisplayRole);
329
                foundColors.append(modelData.toString());
330
        }
331
 
332
        return foundColors;
333
}
334
 
335
bool ColorListBox::hasSelection() const
336
{
337
        return this->selectionModel()->hasSelection();
338
}
339
 
22610 jghali 340
void ColorListBox::insertItem(int row, const ScColor& color, const QString& colorName)
21855 jghali 341
{
24137 craig 342
        ColorListModel* colorListModel = qobject_cast<ColorListModel*>(model());
21855 jghali 343
        if (!colorListModel)
344
                return;
345
 
22632 craig 346
        ScribusDoc* doc = nullptr;
21855 jghali 347
        if (cList)
348
                doc = cList->document();
349
 
350
        ColorPixmapValue value(color, doc, colorName);
351
        colorListModel->insert(row, value);
352
}
353
 
354
bool ColorListBox::isNoneColorShown() const
355
{
24137 craig 356
        ColorListModel* colorListModel = qobject_cast<ColorListModel*>(model());
21855 jghali 357
        if (colorListModel)
358
                return colorListModel->isNoneColorShown();
359
        return false;
360
}
361
 
362
void ColorListBox::removeItem(int i)
363
{
364
        // None color item cannot be removed
365
        if (isNoneColorShown() && (i == 0))
366
                return;
367
 
368
        model()->removeRow(i);
369
}
370
 
22610 jghali 371
int ColorListBox::row(const QString& colorName)
21855 jghali 372
{
373
        QAbstractItemModel* currentModel = model();
374
 
375
        QModelIndex firstIndex = currentModel->index(0, 0, QModelIndex());
376
        QModelIndexList indexes = currentModel->match(firstIndex, Qt::DisplayRole, colorName, -1, Qt::MatchExactly);
377
        if (indexes.count() > 0)
378
        {
379
                const QModelIndex& first = indexes.at(0);
380
                return first.row();
381
        }
382
        return -1;
383
}
384
 
385
void ColorListBox::setCurrentColor(QString colorName)
386
{
387
        if (colorName == CommonStrings::None)
21933 craig 388
                colorName = CommonStrings::tr_NoneColor;
21855 jghali 389
 
390
        QModelIndex firstIndex = model()->index(0, 0, QModelIndex());
391
        QModelIndexList indexes = this->model()->match(firstIndex, Qt::DisplayRole, colorName, -1, Qt::MatchExactly);
392
        if (indexes.count() > 0)
393
                this->selectionModel()->setCurrentIndex(indexes[0], QItemSelectionModel::ClearAndSelect);
394
}
395
 
396
void ColorListBox::setColors(ColorList& list, bool insertNone)
397
{
398
        ColorList::Iterator it;
399
 
24137 craig 400
        ColorListModel* colorModel = qobject_cast<ColorListModel*>(this->model());
21855 jghali 401
        if (!colorModel)
402
                return;
403
 
404
        cList = &list;
405
 
406
        colorModel->setColorList(list, insertNone);
407
}
408
 
409
void ColorListBox::setCurrentRow(int row)
410
{
411
        QModelIndex index = this->model()->index(row, 0);
412
        selectionModel()->setCurrentIndex(index, QItemSelectionModel::ClearAndSelect);
413
}
414
 
21664 jghali 415
void ColorListBox::setPixmapType(ColorListBox::PixmapType type)
416
{
417
        if (type == ColorListBox::fancyPixmap)
418
        {
21665 jghali 419
                QAbstractItemDelegate* oldDelegate = itemDelegate();
420
                ColorFancyItemDelegate* colorDelegate = dynamic_cast<ColorFancyItemDelegate*>(oldDelegate);
21664 jghali 421
                if (!colorDelegate)
422
                {
423
                        setItemDelegate(new ColorFancyItemDelegate());
22632 craig 424
                        delete oldDelegate;
21665 jghali 425
                        m_type = type;
21664 jghali 426
                }
427
        }
428
        else if (type == ColorListBox::widePixmap)
429
        {
21665 jghali 430
                QAbstractItemDelegate* oldDelegate = itemDelegate();
431
                ColorWideItemDelegate* colorDelegate = dynamic_cast<ColorWideItemDelegate*>(oldDelegate);
21664 jghali 432
                if (!colorDelegate)
433
                {
434
                        setItemDelegate(new ColorWideItemDelegate());
22632 craig 435
                        delete oldDelegate;
21665 jghali 436
                        m_type = type;
21664 jghali 437
                }
438
        }
439
        else if (type == ColorListBox::smallPixmap)
440
        {
21665 jghali 441
                QAbstractItemDelegate* oldDelegate = itemDelegate();
442
                ColorSmallItemDelegate* colorDelegate = dynamic_cast<ColorSmallItemDelegate*>(oldDelegate);
21664 jghali 443
                if (!colorDelegate)
444
                {
445
                        setItemDelegate(new ColorSmallItemDelegate());
22632 craig 446
                        delete oldDelegate;
21665 jghali 447
                        m_type = type;
21664 jghali 448
                }
449
        }
450
}
451
 
21855 jghali 452
void ColorListBox::setShowNoneColor(bool showNone)
453
{
24137 craig 454
        ColorListModel* colorListModel = qobject_cast<ColorListModel*>(model());
21855 jghali 455
        if (colorListModel)
456
                colorListModel->setShowNoneColor(showNone);
457
}
458
 
15662 fschmid 459
void ColorListBox::slotRightClick()
460
{
21837 jghali 461
        QSignalBlocker sigBlocker(this);
21855 jghali 462
        QString currentSel = currentColor();
463
        if (currentSel.isEmpty())
21837 jghali 464
                return;
465
 
466
        QMenu *pmen = new QMenu();
467
        pmen->addAction( tr("Sort by Name"));
468
        pmen->addAction( tr("Sort by Color"));
469
        pmen->addAction( tr("Sort by Type"));
470
        sortRule = pmen->actions().indexOf(pmen->exec(QCursor::pos()));
471
        delete pmen;
21855 jghali 472
 
24137 craig 473
        ColorListModel* colorListModel = qobject_cast<ColorListModel*>(model());
21855 jghali 474
        if (!colorListModel)
475
                return;
476
 
477
        if (sortRule == 0)
478
                colorListModel->setSortRule(ColorListModel::SortByName);
479
        else if (sortRule == 1)
480
                colorListModel->setSortRule(ColorListModel::SortByValues);
481
        else if (sortRule == 2)
482
                colorListModel->setSortRule(ColorListModel::SortByType);
483
 
21837 jghali 484
        if (!currentSel.isEmpty())
21855 jghali 485
                setCurrentColor(currentSel);
15662 fschmid 486
}
9874 avox 487
 
24736 jghali 488
QString ColorListBox::text(int row) const
7747 jghali 489
{
21855 jghali 490
        QVariant varText = data(row, Qt::DisplayRole);
491
        return varText.toString();
7850 jghali 492
}
493
 
21855 jghali 494
void ColorListBox::updateBox(ColorList& list)
7850 jghali 495
{
21855 jghali 496
        bool showNoneColor = false;
21664 jghali 497
 
21855 jghali 498
        clear();
499
        reset();
7747 jghali 500
 
24137 craig 501
        ColorListModel* colorModel = qobject_cast<ColorListModel*>(this->model());
21855 jghali 502
        if (colorModel)
503
                showNoneColor = colorModel->isNoneColorShown();
504
        setColors(list, showNoneColor);
7747 jghali 505
}
506
 
15662 fschmid 507
bool ColorListBox::viewportEvent(QEvent *event)
9575 fschmid 508
{
22521 craig 509
        if (event != nullptr)
16729 fschmid 510
        {
21837 jghali 511
                if (event->type() == QEvent::MouseButtonPress)
9575 fschmid 512
                {
21837 jghali 513
                        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
514
                        if (mouseEvent->button() == Qt::RightButton)
515
                                return true;
516
                }
517
                else if (event->type() == QEvent::MouseButtonRelease)
518
                {
519
                        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
520
                        if (mouseEvent->button() == Qt::RightButton)
9575 fschmid 521
                        {
21837 jghali 522
                                emit contextMenuRequested();
15583 fschmid 523
                                return true;
9575 fschmid 524
                        }
525
                }
526
        }
21855 jghali 527
        return QListView::viewportEvent(event);
9575 fschmid 528
}