Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_palette_view.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2016 Dmitry Kazakov <dimula73@gmail.com>
3 * SPDX-FileCopyrightText: 2022 Halla Rempt <halla@valdyas.org>
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#include "kis_palette_view.h"
8
9#include <QWheelEvent>
10#include <QHeaderView>
11#include <QFormLayout>
12#include <QLineEdit>
13#include <QCheckBox>
14#include <QComboBox>
15#include <QScopedValueRollback>
16
17#include <KConfigGroup>
18#include <KSharedConfig>
19#include <KLocalizedString>
20
21#include <kis_icon_utils.h>
22#include <config-qt-patches-present.h>
23
24#include <KisKineticScroller.h>
25
26#include <KoDialog.h>
28
29#include "KisPaletteDelegate.h"
30#include "KisPaletteModel.h"
31#include "kis_color_button.h"
32#include <KisSwatch.h>
33#include <KisResourceModel.h>
34#include <kis_debug.h>
36
38
44
46 : QTableView(parent)
47 , d(new Private)
48{
49 setItemDelegate(new KisPaletteDelegate(this));
50
51 setShowGrid(true);
52 setDropIndicatorShown(true);
53 setDragDropMode(QAbstractItemView::DragDrop);
54 setSelectionMode(QAbstractItemView::SingleSelection);
55 setDragEnabled(true);
56 setAcceptDrops(false);
57
58 /*
59 * without this, a cycle might be created:
60 * the view stretches to right border, and this make it need a scroll bar;
61 * after the bar is added, the view shrinks to the bar, and this makes it
62 * no longer need the bar any more, and the bar is removed again
63 */
64 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
65
66 // set the size of swatches
67 horizontalHeader()->setVisible(false);
68 verticalHeader()->setVisible(false);
69 horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
70 horizontalHeader()->setMinimumSectionSize(MINIMUM_ROW_HEIGHT);
71 verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
72 verticalHeader()->setMinimumSectionSize(MINIMUM_ROW_HEIGHT);
73
74#if QT_VERSION >= QT_VERSION_CHECK(6, 9, 0) && !KRITA_QT_HEADER_VIEW_MEMORY_MODE_INITIALIZATION_FIX
75 // Qt 6.9.0 got a "feature" that lets QHeaderView avoid
76 // memory allocations until some custom row/column size
77 // request comes from the user. This feature has a problem.
78 // When calling resizeSections(QHeaderView::Fixed) on a non-
79 // empty header view, it half-initialized the header,
80 // leaving it in a broken state. All the following
81 // initialization requests will not be able to complete
82 // or fix this state.
83 //
84 // Hence we should switch the header into "memory" mode
85 // explicitly, while it has zero sections in it. It will
86 // properly initialize it.
87 //
88 // Offending commit:
89 // https://github.com/qt/qtbase/commit/9e3a96189d9db8a458e65cd5078509afe7a160db
90 //
91 // Krita's copy has a fix for this issue, which is declared by
92 // a special definition.
93
94 verticalHeader()->setStretchLastSection(true);
95 verticalHeader()->setStretchLastSection(false);
96 horizontalHeader()->setStretchLastSection(true);
97 horizontalHeader()->setStretchLastSection(false);
98#endif
99
100 connect(horizontalHeader(), SIGNAL(sectionResized(int,int,int)),
101 SLOT(slotHorizontalHeaderResized(int,int,int)));
102 setAutoFillBackground(true);
103
104 QScroller *scroller = KisKineticScroller::createPreconfiguredScroller(this);
105 if (scroller) {
106 connect(scroller, SIGNAL(stateChanged(QScroller::State)),
107 this, SLOT(slotScrollerStateChanged(QScroller::State)));
108 }
109
110 connect(this, SIGNAL(clicked(QModelIndex)), SLOT(slotCurrentSelectionChanged(QModelIndex)));
111}
112
116
118{
119 KisPaletteDelegate *delegate =
120 dynamic_cast<KisPaletteDelegate*>(itemDelegate());
122
123 delegate->setCrossedKeyword(value);
124}
125
127{
128 KoDialog dialog;
129 dialog.setWindowTitle(i18nc("@title:dialog", "Add a new Color Swatch"));
130 QFormLayout *editableItems = new QFormLayout(dialog.mainWidget());
131
132 QComboBox *cmbGroups = new QComboBox;
133 QString defaultGroupName = i18nc("Name for default swatch group", "Default");
134 cmbGroups->addItem(defaultGroupName);
135 cmbGroups->addItems(d->model->colorSet()->swatchGroupNames());
136 QLineEdit *lnIDName = new QLineEdit;
137 QLineEdit *lnName = new QLineEdit;
138 KisColorButton *bnColor = new KisColorButton;
139 QCheckBox *chkSpot = new QCheckBox;
140 chkSpot->setToolTip(i18nc("@info:tooltip", "A spot color is a color that the printer is able to print without mixing the paints it has available to it. The opposite is called a process color."));
141 editableItems->addRow(i18n("Swatch Group:"), cmbGroups);
142 editableItems->addRow(i18n("Swatch ID:"), lnIDName);
143 editableItems->addRow(i18n("Color swatch name:"), lnName);
144 editableItems->addRow(i18nc("Color as the Color of a Swatch in a Palette", "Color:"), bnColor);
145 editableItems->addRow(i18n("Spot color:"), chkSpot);
146 cmbGroups->setCurrentIndex(0);
147 lnName->setText(i18nc("Prefix of a color swatch default name, as in Color 1","Color")+" " + QString::number(d->model->colorSet()->colorCount()+1));
148 lnIDName->setText(QString::number(d->model->colorSet()->colorCount() + 1));
149 bnColor->setColor(color);
150 chkSpot->setChecked(false);
151
152 if (dialog.exec() == KoDialog::Accepted) {
153 QString groupName = cmbGroups->currentText();
154 if (groupName == defaultGroupName) {
155 groupName = QString();
156 }
157 KisSwatch newEntry;
158 newEntry.setColor(bnColor->color());
159 newEntry.setName(lnName->text());
160 newEntry.setId(lnIDName->text());
161 newEntry.setSpotColor(chkSpot->isChecked());
162 d->model->addSwatch(newEntry, groupName);
164 return true;
165 }
166
167 return false;
168}
169
171{
172 KoDialog dialog;
173 dialog.setWindowTitle(i18nc("@title:dialog","Add a new group"));
174 QFormLayout *editableItems = new QFormLayout(dialog.mainWidget());
175 QLineEdit *lnName = new QLineEdit();
176 lnName->setText(i18nc("Part of default name for a new group", "Color Group")+""+QString::number(d->model->colorSet()->swatchGroupNames().size()+1));
177 editableItems->addRow(i18nc("Name for a group", "Name"), lnName);
178
179 if (dialog.exec() == KoDialog::Accepted) {
180 d->model->addGroup(lnName->text());
182 return true;
183 }
184 return false;
185}
186
187void KisPaletteView::scrollTo(const QModelIndex &index, ScrollHint hint)
188{
189 if(!d->foregroundColorChangeInProgress) {
190 QTableView::scrollTo(index, hint);
191 }
192}
193
195{
196 bool keepColors = false;
197 if (qvariant_cast<bool>(index.data(KisPaletteModel::IsGroupNameRole))) {
198 KoDialog dialog;
199 dialog.setWindowTitle(i18nc("@title:dialog","Removing Group"));
200 QFormLayout *editableItems = new QFormLayout(dialog.mainWidget());
201 QCheckBox *chkKeep = new QCheckBox();
202 editableItems->addRow(i18nc("Shows up when deleting a swatch group", "Keep the Colors"), chkKeep);
203
204 if (dialog.exec() != KoDialog::Accepted) { return false; }
205 keepColors = chkKeep->isChecked();
206 }
207 d->model->removeSwatch(index, keepColors);
208
210
211 return true;
212}
213
215{
216 KoColorSetSP colorSet = d->model->colorSet();
217 if (!colorSet || !colorSet->valid() || currentIndex().row() < 0) {
218 return;
219 }
220
221 //also don't select if the color is the same as the current selection
222 if (d->model->getSwatch(currentIndex()).color() == color) {
223 return;
224 }
225
226 selectionModel()->clearSelection();
227 QModelIndex index = d->model->indexForClosest(color);
228
229 selectionModel()->setCurrentIndex(index, QItemSelectionModel::Select);
230}
231
233{
234 QModelIndex index = d->model->indexForClosest(color);
235 KisSwatch swatch = d->model->getSwatch(index);
236 return swatch.color();
237}
238
240{
241 QScopedValueRollback<bool> rollback(d->foregroundColorChangeInProgress, true);
242 selectClosestColor(color);
243}
244
246{
247 if (d->model) {
248 disconnect(d->model, 0, this, 0);
249 }
250 d->model = model;
251 setModel(model);
253
254 connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(slotAdditionalGuiUpdate()));
255 connect(model, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)), SLOT(slotAdditionalGuiUpdate()));
256 connect(model, SIGNAL(modelReset()), SLOT(slotAdditionalGuiUpdate()));
257}
258
260{
261 return d->model;
262}
263
265{
266 setAcceptDrops(allow);
267}
268
270{
271 resizeRows(newSize);
273}
274
276{
277 verticalHeader()->setDefaultSectionSize(newSize);
278 verticalHeader()->resizeSections(QHeaderView::Fixed);
279}
280
285
287{
288 if (selectedIndexes().size() <= 0) {
289 return;
290 }
291 d->model->removeSwatch(currentIndex());
292}
293
295{
296 /*
297 * Note: QTableView (Qt 5.15) does not clear spans on model resets.
298 * But it does move spans on row inserts/removals, so incremental updates
299 * would be possible.
300 * Moving rows on the other hand does NOT update row spans accordingly...
301 */
302 if (!d->model->colorSet()) return;
303
304 clearSpans();
305 resizeRows(verticalHeader()->defaultSectionSize());
306
307 for (const QString &groupName : d->model->colorSet()->swatchGroupNames()) {
308 if (groupName.isEmpty()) continue;
309
310 int rowNumber = d->model->colorSet()->startRowForGroup(groupName);
311 setSpan(rowNumber, 0, 1, d->model->columnCount());
312 setRowHeight(rowNumber, fontMetrics().lineSpacing() + 6);
313 }
314}
315
316void KisPaletteView::slotCurrentSelectionChanged(const QModelIndex &newCurrent)
317{
318 if (!newCurrent.isValid()) { return; }
319
320 const bool isGroupName = newCurrent.data(KisPaletteModel::IsGroupNameRole).toBool();
321 const bool isCheckSlot = newCurrent.data(KisPaletteModel::CheckSlotRole).toBool();
322
323 const KisSwatch newEntry = d->model->getSwatch(newCurrent);
324
325 Q_EMIT sigIndexSelected(newCurrent);
326 if (isGroupName) {
327 return;
328 }
329
330 if (isCheckSlot) {
331 Q_EMIT sigColorSelected(newEntry.color());
332 }
333}
334
336{
337 Q_ASSERT(d->model);
338 d->model->setDisplayRenderer(displayRenderer);
339}
340
float value(const T *src, size_t ch)
A pushbutton to display or allow user selection of a color.
void setColor(const KoColor &c)
void setCrossedKeyword(const QString &value)
The KisPaletteModel class This, together with KisPaletteView and KisPaletteDelegate forms a mvc way t...
void setCrossedKeyword(const QString &value)
setCrossedKeyword This allows you to set keywords that can cross out colors. This is implemented to m...
void scrollTo(const QModelIndex &index, ScrollHint hint=EnsureVisible) override
void setAllowModification(bool allow)
setAllowModification Set whether doubleclick calls up a modification window. This is to prevent users...
bool removeEntryWithDialog(QModelIndex index)
void sigColorSelected(const KoColor &)
const KoColor closestColor(const KoColor &color) const
closestColor determines closest swatch in the active palette and returns it's color as KoColor
void slotFGColorChanged(const KoColor &)
const QScopedPointer< Private > d
void slotHorizontalHeaderResized(int, int, int newSize)
void slotCurrentSelectionChanged(const QModelIndex &newCurrent)
void sigIndexSelected(const QModelIndex &index)
KisPaletteModel * paletteModel() const
void setPaletteModel(KisPaletteModel *model)
~KisPaletteView() override
void slotScrollerStateChanged(QScroller::State state)
bool addEntryWithDialog(KoColor color)
KisPaletteView(QWidget *parent=0)
void resizeRows(int newSize)
static int MINIMUM_ROW_HEIGHT
void selectClosestColor(const KoColor &color)
selectClosestColor select a color that's closest to parameter color
void setDisplayRenderer(const KoColorDisplayRendererInterface *displayRenderer)
static bool updateResourceWithUserInput(QWidget *widgetParent, KoResourceSP resource)
void setSpotColor(bool spotColor)
Definition KisSwatch.cpp:38
KoColor color() const
Definition KisSwatch.h:30
void setColor(const KoColor &color)
Definition KisSwatch.cpp:32
void setId(const QString &id)
Definition KisSwatch.cpp:26
void setName(const QString &name)
Definition KisSwatch.cpp:20
A dialog base class with standard buttons and predefined layouts.
Definition KoDialog.h:116
#define KIS_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:75
KRITAWIDGETUTILS_EXPORT QScroller * createPreconfiguredScroller(QAbstractScrollArea *target)
QPointer< KisPaletteModel > model