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
23#include <KisKineticScroller.h>
24
25#include <KoDialog.h>
27
28#include "KisPaletteDelegate.h"
29#include "KisPaletteModel.h"
30#include "kis_color_button.h"
31#include <KisSwatch.h>
32#include <KisResourceModel.h>
33#include <kis_debug.h>
35
37
43
45 : QTableView(parent)
46 , d(new Private)
47{
48 setItemDelegate(new KisPaletteDelegate(this));
49
50 setShowGrid(true);
51 setDropIndicatorShown(true);
52 setDragDropMode(QAbstractItemView::DragDrop);
53 setSelectionMode(QAbstractItemView::SingleSelection);
54 setDragEnabled(true);
55 setAcceptDrops(false);
56
57 /*
58 * without this, a cycle might be created:
59 * the view stretches to right border, and this make it need a scroll bar;
60 * after the bar is added, the view shrinks to the bar, and this makes it
61 * no longer need the bar any more, and the bar is removed again
62 */
63 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
64
65 // set the size of swatches
66 horizontalHeader()->setVisible(false);
67 verticalHeader()->setVisible(false);
68 horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
69 horizontalHeader()->setMinimumSectionSize(MINIMUM_ROW_HEIGHT);
70 verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
71 verticalHeader()->setMinimumSectionSize(MINIMUM_ROW_HEIGHT);
72
73 connect(horizontalHeader(), SIGNAL(sectionResized(int,int,int)),
74 SLOT(slotHorizontalHeaderResized(int,int,int)));
75 setAutoFillBackground(true);
76
77 QScroller *scroller = KisKineticScroller::createPreconfiguredScroller(this);
78 if (scroller) {
79 connect(scroller, SIGNAL(stateChanged(QScroller::State)),
80 this, SLOT(slotScrollerStateChanged(QScroller::State)));
81 }
82
83 connect(this, SIGNAL(clicked(QModelIndex)), SLOT(slotCurrentSelectionChanged(QModelIndex)));
84}
85
89
91{
92 KisPaletteDelegate *delegate =
93 dynamic_cast<KisPaletteDelegate*>(itemDelegate());
95
96 delegate->setCrossedKeyword(value);
97}
98
100{
101 KoDialog dialog;
102 dialog.setWindowTitle(i18nc("@title:dialog", "Add a new Color Swatch"));
103 QFormLayout *editableItems = new QFormLayout(dialog.mainWidget());
104
105 QComboBox *cmbGroups = new QComboBox;
106 QString defaultGroupName = i18nc("Name for default swatch group", "Default");
107 cmbGroups->addItem(defaultGroupName);
108 cmbGroups->addItems(d->model->colorSet()->swatchGroupNames());
109 QLineEdit *lnIDName = new QLineEdit;
110 QLineEdit *lnName = new QLineEdit;
111 KisColorButton *bnColor = new KisColorButton;
112 QCheckBox *chkSpot = new QCheckBox;
113 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."));
114 editableItems->addRow(i18n("Swatch Group:"), cmbGroups);
115 editableItems->addRow(i18n("Swatch ID:"), lnIDName);
116 editableItems->addRow(i18n("Color swatch name:"), lnName);
117 editableItems->addRow(i18nc("Color as the Color of a Swatch in a Palette", "Color:"), bnColor);
118 editableItems->addRow(i18n("Spot color:"), chkSpot);
119 cmbGroups->setCurrentIndex(0);
120 lnName->setText(i18nc("Prefix of a color swatch default name, as in Color 1","Color")+" " + QString::number(d->model->colorSet()->colorCount()+1));
121 lnIDName->setText(QString::number(d->model->colorSet()->colorCount() + 1));
122 bnColor->setColor(color);
123 chkSpot->setChecked(false);
124
125 if (dialog.exec() == KoDialog::Accepted) {
126 QString groupName = cmbGroups->currentText();
127 if (groupName == defaultGroupName) {
128 groupName = QString();
129 }
130 KisSwatch newEntry;
131 newEntry.setColor(bnColor->color());
132 newEntry.setName(lnName->text());
133 newEntry.setId(lnIDName->text());
134 newEntry.setSpotColor(chkSpot->isChecked());
135 d->model->addSwatch(newEntry, groupName);
137 return true;
138 }
139
140 return false;
141}
142
144{
145 KoDialog dialog;
146 dialog.setWindowTitle(i18nc("@title:dialog","Add a new group"));
147 QFormLayout *editableItems = new QFormLayout(dialog.mainWidget());
148 QLineEdit *lnName = new QLineEdit();
149 lnName->setText(i18nc("Part of default name for a new group", "Color Group")+""+QString::number(d->model->colorSet()->swatchGroupNames().size()+1));
150 editableItems->addRow(i18nc("Name for a group", "Name"), lnName);
151
152 if (dialog.exec() == KoDialog::Accepted) {
153 d->model->addGroup(lnName->text());
155 return true;
156 }
157 return false;
158}
159
160void KisPaletteView::scrollTo(const QModelIndex &index, ScrollHint hint)
161{
162 if(!d->foregroundColorChangeInProgress) {
163 QTableView::scrollTo(index, hint);
164 }
165}
166
168{
169 bool keepColors = false;
170 if (qvariant_cast<bool>(index.data(KisPaletteModel::IsGroupNameRole))) {
171 KoDialog dialog;
172 dialog.setWindowTitle(i18nc("@title:dialog","Removing Group"));
173 QFormLayout *editableItems = new QFormLayout(dialog.mainWidget());
174 QCheckBox *chkKeep = new QCheckBox();
175 editableItems->addRow(i18nc("Shows up when deleting a swatch group", "Keep the Colors"), chkKeep);
176
177 if (dialog.exec() != KoDialog::Accepted) { return false; }
178 keepColors = chkKeep->isChecked();
179 }
180 d->model->removeSwatch(index, keepColors);
181
183
184 return true;
185}
186
188{
189 KoColorSetSP colorSet = d->model->colorSet();
190 if (!colorSet || !colorSet->valid() || currentIndex().row() < 0) {
191 return;
192 }
193
194 //also don't select if the color is the same as the current selection
195 if (d->model->getSwatch(currentIndex()).color() == color) {
196 return;
197 }
198
199 selectionModel()->clearSelection();
200 QModelIndex index = d->model->indexForClosest(color);
201
202 selectionModel()->setCurrentIndex(index, QItemSelectionModel::Select);
203}
204
206{
207 QModelIndex index = d->model->indexForClosest(color);
208 KisSwatch swatch = d->model->getSwatch(index);
209 return swatch.color();
210}
211
213{
214 QScopedValueRollback<bool> rollback(d->foregroundColorChangeInProgress, true);
215 selectClosestColor(color);
216}
217
219{
220 if (d->model) {
221 disconnect(d->model, 0, this, 0);
222 }
223 d->model = model;
224 setModel(model);
226
227 connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)), SLOT(slotAdditionalGuiUpdate()));
228 connect(model, SIGNAL(rowsMoved(QModelIndex,int,int,QModelIndex,int)), SLOT(slotAdditionalGuiUpdate()));
229 connect(model, SIGNAL(modelReset()), SLOT(slotAdditionalGuiUpdate()));
230}
231
233{
234 return d->model;
235}
236
238{
239 setAcceptDrops(allow);
240}
241
243{
244 resizeRows(newSize);
246}
247
249{
250 verticalHeader()->setDefaultSectionSize(newSize);
251 verticalHeader()->resizeSections(QHeaderView::Fixed);
252}
253
258
260{
261 if (selectedIndexes().size() <= 0) {
262 return;
263 }
264 d->model->removeSwatch(currentIndex());
265}
266
268{
269 /*
270 * Note: QTableView (Qt 5.15) does not clear spans on model resets.
271 * But it does move spans on row inserts/removals, so incremental updates
272 * would be possible.
273 * Moving rows on the other hand does NOT update row spans accordingly...
274 */
275 if (!d->model->colorSet()) return;
276
277 clearSpans();
278 resizeRows(verticalHeader()->defaultSectionSize());
279
280// int row = -1;
281
282 for (const QString &groupName : d->model->colorSet()->swatchGroupNames()) {
283 if (groupName.isEmpty()) continue;
284
285// KisSwatchGroupSP group = d->model->colorSet()->getGroup(groupName);
286// row += group->rowCount() + 1;
287// setSpan(row, 0, 1, d->model->columnCount());
288// setRowHeight(row, fontMetrics().lineSpacing() + 6);
289// verticalHeader()->resizeSection(row, fontMetrics().lineSpacing() + 6);
290
291
292 int rowNumber = d->model->colorSet()->startRowForGroup(groupName);
293 setSpan(rowNumber, 0, 1, d->model->columnCount());
294 setRowHeight(rowNumber, fontMetrics().lineSpacing() + 6);
295 verticalHeader()->resizeSection(rowNumber, fontMetrics().lineSpacing() + 6);
296 }
297}
298
299void KisPaletteView::slotCurrentSelectionChanged(const QModelIndex &newCurrent)
300{
301 if (!newCurrent.isValid()) { return; }
302
303 const bool isGroupName = newCurrent.data(KisPaletteModel::IsGroupNameRole).toBool();
304 const bool isCheckSlot = newCurrent.data(KisPaletteModel::CheckSlotRole).toBool();
305
306 const KisSwatch newEntry = d->model->getSwatch(newCurrent);
307
308 Q_EMIT sigIndexSelected(newCurrent);
309 if (isGroupName) {
310 return;
311 }
312
313 if (isCheckSlot) {
314 Q_EMIT sigColorSelected(newEntry.color());
315 }
316}
317
319{
320 Q_ASSERT(d->model);
321 d->model->setDisplayRenderer(displayRenderer);
322}
323
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