Changeset View
Changeset View
Standalone View
Standalone View
libs/widgets/KisPaletteComboBox.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2018 Michael Zhou <simeirxh@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 as published by | ||||
6 | * the Free Software Foundation; either version 2 of the License, or | ||||
7 | * (at your option) any later version. | ||||
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 General Public License | ||||
15 | * along with this program; if not, write to the Free Software | ||||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
17 | */ | ||||
18 | | ||||
19 | // Qt | ||||
20 | #include <QPainter> | ||||
21 | #include <QPen> | ||||
22 | #include <QVector> | ||||
23 | #include <QCompleter> | ||||
24 | | ||||
25 | // STL | ||||
26 | #include <algorithm> | ||||
27 | | ||||
28 | #include "kis_palette_view.h" | ||||
29 | #include "KisPaletteComboBox.h" | ||||
30 | | ||||
31 | KisPaletteComboBox::KisPaletteComboBox(QWidget *parent) | ||||
32 | : QComboBox(parent) | ||||
33 | , m_model(Q_NULLPTR) | ||||
34 | { | ||||
35 | setEditable(true); | ||||
36 | setInsertPolicy(NoInsert); | ||||
37 | completer()->setCompletionMode(QCompleter::PopupCompletion); | ||||
38 | completer()->setCaseSensitivity(Qt::CaseInsensitive); | ||||
39 | completer()->setFilterMode(Qt::MatchContains); | ||||
40 | connect(this, SIGNAL(currentIndexChanged(int)), SLOT(slotIndexUpdated(int))); | ||||
41 | } | ||||
42 | | ||||
43 | KisPaletteComboBox::~KisPaletteComboBox() | ||||
44 | { } | ||||
45 | | ||||
46 | void KisPaletteComboBox::setPaletteModel(const KisPaletteModel *paletteModel) | ||||
47 | { | ||||
48 | if (!m_model.isNull()) { | ||||
49 | m_model->disconnect(this); | ||||
50 | } | ||||
51 | m_model = paletteModel; | ||||
52 | if (m_model.isNull()) { return; } | ||||
53 | slotPaletteChanged(); | ||||
54 | connect(m_model, SIGNAL(sigPaletteChanged()), | ||||
55 | SLOT(slotPaletteChanged())); | ||||
56 | connect(m_model, SIGNAL(sigPaletteModified()), | ||||
57 | SLOT(slotPaletteChanged())); | ||||
58 | } | ||||
59 | | ||||
60 | void KisPaletteComboBox::setCompanionView(KisPaletteView *view) | ||||
61 | { | ||||
62 | if (!m_view.isNull()) { | ||||
63 | m_view->disconnect(this); | ||||
64 | disconnect(m_view.data()); | ||||
65 | } | ||||
66 | m_view = view; | ||||
67 | setPaletteModel(view->paletteModel()); | ||||
68 | connect(view, SIGNAL(sigIndexSelected(QModelIndex)), | ||||
69 | SLOT(slotSwatchSelected(QModelIndex))); | ||||
70 | connect(this, SIGNAL(sigColorSelected(KoColor)), | ||||
71 | view, SLOT(slotFGColorChanged(KoColor))); | ||||
72 | } | ||||
73 | | ||||
74 | void KisPaletteComboBox::slotPaletteChanged() | ||||
75 | { | ||||
76 | clear(); | ||||
77 | m_groupMapMap.clear(); | ||||
78 | m_idxSwatchMap.clear(); | ||||
79 | | ||||
80 | if (QPointer<KoColorSet>(m_model->colorSet()).isNull()) { return; } | ||||
81 | | ||||
82 | for (const QString &groupName : m_model->colorSet()->getGroupNames()) { | ||||
83 | QVector<SwatchInfoType> infoList; | ||||
84 | PosIdxMapType posIdxMap; | ||||
85 | const KisSwatchGroup *group = m_model->colorSet()->getGroup(groupName); | ||||
86 | for (const SwatchInfoType &info : group->infoList()) { | ||||
87 | infoList.append(info); | ||||
88 | } | ||||
89 | std::sort(infoList.begin(), infoList.end(), swatchInfoLess); | ||||
90 | for (const SwatchInfoType &info : infoList) { | ||||
91 | const KisSwatch &swatch = info.swatch; | ||||
92 | QString name = swatch.name(); | ||||
93 | if (!swatch.id().isEmpty()){ | ||||
94 | name = swatch.id() + " - " + swatch.name(); | ||||
95 | } | ||||
96 | addItem(QIcon(createColorSquare(swatch)), name); | ||||
97 | posIdxMap[SwatchPosType(info.column, info.row)] = count() - 1; | ||||
98 | m_idxSwatchMap.push_back(swatch); | ||||
99 | } | ||||
100 | m_groupMapMap[group->name()] = posIdxMap; | ||||
101 | } | ||||
102 | if (m_view.isNull()) { | ||||
103 | setCurrentIndex(0); | ||||
104 | } | ||||
105 | QModelIndex idx = m_view->currentIndex(); | ||||
106 | if (!idx.isValid()) { return; } | ||||
107 | if (qvariant_cast<bool>(idx.data(KisPaletteModel::IsGroupNameRole))) { return; } | ||||
108 | if (!qvariant_cast<bool>(idx.data(KisPaletteModel::CheckSlotRole))) { return; } | ||||
109 | | ||||
110 | blockSignals(true); // this is a passive selection; this shouldn't make others change | ||||
111 | slotSwatchSelected(idx); | ||||
112 | blockSignals(false); | ||||
113 | } | ||||
114 | | ||||
115 | bool KisPaletteComboBox::swatchInfoLess(const SwatchInfoType &first, const SwatchInfoType &second) | ||||
116 | { | ||||
117 | return first.swatch.name() < second.swatch.name(); | ||||
118 | } | ||||
119 | | ||||
120 | QPixmap KisPaletteComboBox::createColorSquare(const KisSwatch &swatch) const | ||||
121 | { | ||||
122 | QPixmap colorSquare(32, 32); | ||||
123 | if (swatch.spotColor()) { | ||||
124 | QImage img = QImage(32, 32, QImage::Format_ARGB32); | ||||
125 | QPainter circlePainter; | ||||
126 | img.fill(Qt::transparent); | ||||
127 | circlePainter.begin(&img); | ||||
128 | QBrush brush = QBrush(Qt::SolidPattern); | ||||
129 | brush.setColor(swatch.color().toQColor()); | ||||
130 | circlePainter.setBrush(brush); | ||||
131 | QPen pen = circlePainter.pen(); | ||||
132 | pen.setColor(Qt::transparent); | ||||
133 | pen.setWidth(0); | ||||
134 | circlePainter.setPen(pen); | ||||
135 | circlePainter.drawEllipse(0, 0, 32, 32); | ||||
136 | circlePainter.end(); | ||||
137 | colorSquare = QPixmap::fromImage(img); | ||||
138 | } else { | ||||
139 | colorSquare.fill(swatch.color().toQColor()); | ||||
140 | } | ||||
141 | return colorSquare; | ||||
142 | } | ||||
143 | | ||||
144 | void KisPaletteComboBox::slotSwatchSelected(const QModelIndex &index) | ||||
145 | { | ||||
146 | if (!qvariant_cast<bool>(index.data(KisPaletteModel::CheckSlotRole))) { | ||||
147 | return; | ||||
148 | } | ||||
149 | if (qvariant_cast<bool>(index.data(KisPaletteModel::IsGroupNameRole))) { | ||||
150 | return; | ||||
151 | } | ||||
152 | QString gName = qvariant_cast<QString>(index.data(KisPaletteModel::GroupNameRole)); | ||||
153 | int rowInGroup = qvariant_cast<int>(index.data(KisPaletteModel::RowInGroupRole)); | ||||
154 | setCurrentIndex(m_groupMapMap[gName][SwatchPosType(index.column(), rowInGroup)]); | ||||
155 | } | ||||
156 | | ||||
157 | void KisPaletteComboBox::slotIndexUpdated(int idx) | ||||
158 | { | ||||
159 | if (idx >= 0 && idx < m_idxSwatchMap.size()) { | ||||
160 | emit sigColorSelected(m_idxSwatchMap[idx].color()); | ||||
161 | } | ||||
162 | } |