Krita Source Code Documentation
Loading...
Searching...
No Matches
KisPaletteComboBox.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2018 Michael Zhou <simeirxh@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7// Qt
8#include <QPainter>
9#include <QPen>
10#include <QVector>
11#include <QCompleter>
12
13// STL
14#include <algorithm>
15
16#include "kis_palette_view.h"
17#include "KisPaletteComboBox.h"
18
20 : KisSqueezedComboBox(parent)
21 , m_model(0)
22{
23 setEditable(true);
24 setInsertPolicy(NoInsert);
25 completer()->setCompletionMode(QCompleter::PopupCompletion);
26 completer()->setCaseSensitivity(Qt::CaseInsensitive);
27 completer()->setFilterMode(Qt::MatchContains);
28 connect(this, SIGNAL(currentIndexChanged(int)), SLOT(slotIndexUpdated(int)));
29}
30
33
35{
36 if (!m_model.isNull()) {
37 m_model->disconnect(this);
38 }
39 m_model = paletteModel;
40 if (m_model.isNull()) { return; }
42 connect(m_model, SIGNAL(sigPaletteChanged()), SLOT(slotPaletteChanged()));
43 connect(m_model, SIGNAL(sigPaletteModified()), SLOT(slotPaletteChanged()));
44}
45
47{
48 if (!m_view.isNull()) {
49 m_view->disconnect(this);
50 disconnect(m_view.data());
51 }
52 m_view = view;
54 connect(view, SIGNAL(sigIndexSelected(QModelIndex)), SLOT(slotSwatchSelected(QModelIndex)));
55}
56
58{
59 clear();
60 m_groupMapMap.clear();
61 m_idxSwatchMap.clear();
62
63 if (QSharedPointer<KoColorSet>(m_model->colorSet()).isNull()) { return; }
64
65 for (const QString &groupName : m_model->colorSet()->swatchGroupNames()) {
67 PosIdxMapType posIdxMap;
68 const KisSwatchGroupSP group = m_model->colorSet()->getGroup(groupName);
69 for (const KisSwatchGroup::SwatchInfo &info : group->infoList()) {
70 infoList.append(info);
71 }
72 std::sort(infoList.begin(), infoList.end(), swatchInfoLess);
73 for (const KisSwatchGroup::SwatchInfo &info : infoList) {
74 const KisSwatch &swatch = info.swatch;
75 QString name = swatch.name();
76 if (!swatch.id().isEmpty()){
77 name = swatch.id() + " - " + swatch.name();
78 }
79 addSqueezedItem(QIcon(createColorSquare(swatch)), name);
80 posIdxMap[SwatchPosType(info.column, info.row)] = count() - 1;
81 m_idxSwatchMap.push_back(swatch);
82 }
83 m_groupMapMap[group->name()] = posIdxMap;
84 }
85 if (m_view.isNull()) {
86 setCurrentIndex(0);
87 }
88 QModelIndex idx = m_view->currentIndex();
89 if (!idx.isValid()) { return; }
90 // FIXME! checkIndex() is just a workaround to not trigger ASSERT failures amidst model resets/row changes.
91 // This class really needs a rewrite to conform with Qt's MVC patterns...
92 if (!m_model->checkIndex(idx, QAbstractItemModel::CheckIndexOption::IndexIsValid)) { return; }
93 if (qvariant_cast<bool>(idx.data(KisPaletteModel::IsGroupNameRole))) { return; }
94 if (!qvariant_cast<bool>(idx.data(KisPaletteModel::CheckSlotRole))) { return; }
95
96 blockSignals(true); // this is a passive selection; this shouldn't make others change
98 blockSignals(false);
99}
100
101bool KisPaletteComboBox::swatchInfoLess(const KisSwatchGroup::SwatchInfo &first, const KisSwatchGroup::SwatchInfo &second)
102{
103 return first.swatch.name() < second.swatch.name();
104}
105
107{
108 QPixmap colorSquare(32, 32);
109 if (swatch.spotColor()) {
110 QImage img = QImage(32, 32, QImage::Format_ARGB32);
111 QPainter circlePainter;
112 img.fill(Qt::transparent);
113 circlePainter.begin(&img);
114 QBrush brush = QBrush(Qt::SolidPattern);
115 brush.setColor(swatch.color().toQColor());
116 circlePainter.setBrush(brush);
117 QPen pen = circlePainter.pen();
118 pen.setColor(Qt::transparent);
119 pen.setWidth(0);
120 circlePainter.setPen(pen);
121 circlePainter.drawEllipse(0, 0, 32, 32);
122 circlePainter.end();
123 colorSquare = QPixmap::fromImage(img);
124 } else {
125 colorSquare.fill(swatch.color().toQColor());
126 }
127 return colorSquare;
128}
129
130void KisPaletteComboBox::slotSwatchSelected(const QModelIndex &index)
131{
132 if (!qvariant_cast<bool>(index.data(KisPaletteModel::CheckSlotRole))) {
133 return;
134 }
135 if (qvariant_cast<bool>(index.data(KisPaletteModel::IsGroupNameRole))) {
136 return;
137 }
138 QString gName = qvariant_cast<QString>(index.data(KisPaletteModel::GroupNameRole));
139 int rowInGroup = qvariant_cast<int>(index.data(KisPaletteModel::RowInGroupRole));
140 setCurrentIndex(m_groupMapMap[gName][SwatchPosType(index.column(), rowInGroup)]);
141}
142
144{
145 if (idx >= 0 && idx < m_idxSwatchMap.size()) {
146 Q_EMIT sigColorSelected(m_idxSwatchMap[idx].color());
147 }
148}
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
QPointer< const KisPaletteModel > m_model
KisPaletteComboBox(QWidget *parent=0)
void slotSwatchSelected(const QModelIndex &index)
QPointer< KisPaletteView > m_view
QPair< int, int > SwatchPosType
QHash< QString, PosIdxMapType > m_groupMapMap
void sigColorSelected(const KoColor &)
void setPaletteModel(const KisPaletteModel *)
static bool swatchInfoLess(const KisSwatchGroup::SwatchInfo &, const KisSwatchGroup::SwatchInfo &)
void setCompanionView(KisPaletteView *)
QHash< SwatchPosType, int > PosIdxMapType
QPixmap createColorSquare(const KisSwatch &swatch) const
QVector< KisSwatch > m_idxSwatchMap
The KisPaletteModel class This, together with KisPaletteView and KisPaletteDelegate forms a mvc way t...
KisPaletteModel * paletteModel() const
void addSqueezedItem(const QString &newItem, QVariant userData=QVariant())
KoColor color() const
Definition KisSwatch.h:30
QString name() const
Definition KisSwatch.h:24
bool spotColor() const
Definition KisSwatch.h:33
QString id() const
Definition KisSwatch.h:27
void toQColor(QColor *c) const
a convenience method for the above.
Definition KoColor.cpp:198