Krita Source Code Documentation
Loading...
Searching...
No Matches
KisPaletteView Class Reference

#include <kis_palette_view.h>

+ Inheritance diagram for KisPaletteView:

Classes

struct  Private
 

Public Slots

void slotFGColorChanged (const KoColor &)
 
void slotScrollerStateChanged (QScroller::State state)
 

Signals

void sigColorSelected (const KoColor &)
 
void sigIndexSelected (const QModelIndex &index)
 

Public Member Functions

bool addEntryWithDialog (KoColor color)
 
bool addGroupWithDialog ()
 
const KoColor closestColor (const KoColor &color) const
 closestColor determines closest swatch in the active palette and returns it's color as KoColor
 
 KisPaletteView (QWidget *parent=0)
 
KisPaletteModel * paletteModel () const
 
bool removeEntryWithDialog (QModelIndex index)
 
void removeSelectedEntry ()
 
void scrollTo (const QModelIndex &index, ScrollHint hint=EnsureVisible) override
 
void selectClosestColor (const KoColor &color)
 selectClosestColor select a color that's closest to parameter color
 
void setAllowModification (bool allow)
 setAllowModification Set whether doubleclick calls up a modification window. This is to prevent users from editing the palette when the palette is intended to be a list of items.
 
void setCrossedKeyword (const QString &value)
 setCrossedKeyword This allows you to set keywords that can cross out colors. This is implemented to mark the lazybrush "transparent" color.
 
void setDisplayRenderer (const KoColorDisplayRendererInterface *displayRenderer)
 
void setPaletteModel (KisPaletteModel *model)
 
 ~KisPaletteView () override
 

Private Slots

void slotAdditionalGuiUpdate ()
 
void slotCurrentSelectionChanged (const QModelIndex &newCurrent)
 
void slotHorizontalHeaderResized (int, int, int newSize)
 

Private Member Functions

void resizeRows (int newSize)
 
void saveModification ()
 

Private Attributes

const QScopedPointer< Private > d
 

Static Private Attributes

static int MINIMUM_ROW_HEIGHT = 10
 

Detailed Description

Definition at line 24 of file kis_palette_view.h.

Constructor & Destructor Documentation

◆ KisPaletteView()

KisPaletteView::KisPaletteView ( QWidget * parent = 0)
explicit

Definition at line 45 of file kis_palette_view.cpp.

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}
const QScopedPointer< Private > d
void slotHorizontalHeaderResized(int, int, int newSize)
void slotCurrentSelectionChanged(const QModelIndex &newCurrent)
void slotScrollerStateChanged(QScroller::State state)
static int MINIMUM_ROW_HEIGHT
KRITAWIDGETUTILS_EXPORT QScroller * createPreconfiguredScroller(QAbstractScrollArea *target)

References KisKineticScroller::createPreconfiguredScroller(), MINIMUM_ROW_HEIGHT, slotCurrentSelectionChanged(), slotHorizontalHeaderResized(), and slotScrollerStateChanged().

◆ ~KisPaletteView()

KisPaletteView::~KisPaletteView ( )
override

Definition at line 113 of file kis_palette_view.cpp.

114{
115}

Member Function Documentation

◆ addEntryWithDialog()

bool KisPaletteView::addEntryWithDialog ( KoColor color)

add an entry with a dialog window.

Warning
deprecated. kept for compatibility with PaletteView in libkis

Definition at line 126 of file kis_palette_view.cpp.

127{
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}
A pushbutton to display or allow user selection of a color.
void setColor(const KoColor &c)
void setSpotColor(bool spotColor)
Definition KisSwatch.cpp:38
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

References KisColorButton::color, d, saveModification(), KisColorButton::setColor(), KisSwatch::setColor(), KisSwatch::setId(), KisSwatch::setName(), and KisSwatch::setSpotColor().

◆ addGroupWithDialog()

bool KisPaletteView::addGroupWithDialog ( )

add entry with a dialog window.

Warning
deprecated. kept for compatibility with PaletteView in libkis

Definition at line 170 of file kis_palette_view.cpp.

171{
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}

References d, and saveModification().

◆ closestColor()

const KoColor KisPaletteView::closestColor ( const KoColor & color) const

closestColor determines closest swatch in the active palette and returns it's color as KoColor

Parameters
color
Returns
KoColor

Definition at line 232 of file kis_palette_view.cpp.

233{
234 QModelIndex index = d->model->indexForClosest(color);
235 KisSwatch swatch = d->model->getSwatch(index);
236 return swatch.color();
237}
KoColor color() const
Definition KisSwatch.h:30

References KisSwatch::color(), and d.

◆ paletteModel()

KisPaletteModel * KisPaletteView::paletteModel ( ) const

Definition at line 259 of file kis_palette_view.cpp.

260{
261 return d->model;
262}

References d.

◆ removeEntryWithDialog()

bool KisPaletteView::removeEntryWithDialog ( QModelIndex index)

remove entry with a dialog window.(Necessary for groups.

Warning
deprecated. kept for compatibility with PaletteView in libkis

Definition at line 194 of file kis_palette_view.cpp.

195{
196 bool keepColors = false;
197 if (qvariant_cast<bool>(index.data(KisPaletteModel::IsGroupNameRole))) {
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}

References d, KisPaletteModel::IsGroupNameRole, and saveModification().

◆ removeSelectedEntry()

void KisPaletteView::removeSelectedEntry ( )

Definition at line 286 of file kis_palette_view.cpp.

287{
288 if (selectedIndexes().size() <= 0) {
289 return;
290 }
291 d->model->removeSwatch(currentIndex());
292}
int size(const Forest< T > &forest)
Definition KisForest.h:1232

References d.

◆ resizeRows()

void KisPaletteView::resizeRows ( int newSize)
private

Definition at line 275 of file kis_palette_view.cpp.

276{
277 verticalHeader()->setDefaultSectionSize(newSize);
278 verticalHeader()->resizeSections(QHeaderView::Fixed);
279}

◆ saveModification()

void KisPaletteView::saveModification ( )
private

Definition at line 281 of file kis_palette_view.cpp.

282{
284}
static bool updateResourceWithUserInput(QWidget *widgetParent, KoResourceSP resource)

References d, and KisResourceUserOperations::updateResourceWithUserInput().

◆ scrollTo()

void KisPaletteView::scrollTo ( const QModelIndex & index,
ScrollHint hint = EnsureVisible )
override

Definition at line 187 of file kis_palette_view.cpp.

188{
189 if(!d->foregroundColorChangeInProgress) {
190 QTableView::scrollTo(index, hint);
191 }
192}

References d.

◆ selectClosestColor()

void KisPaletteView::selectClosestColor ( const KoColor & color)

selectClosestColor select a color that's closest to parameter color

Parameters
color

Definition at line 214 of file kis_palette_view.cpp.

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}

References d.

◆ setAllowModification()

void KisPaletteView::setAllowModification ( bool allow)

setAllowModification Set whether doubleclick calls up a modification window. This is to prevent users from editing the palette when the palette is intended to be a list of items.

Definition at line 264 of file kis_palette_view.cpp.

265{
266 setAcceptDrops(allow);
267}

◆ setCrossedKeyword()

void KisPaletteView::setCrossedKeyword ( const QString & value)

setCrossedKeyword This allows you to set keywords that can cross out colors. This is implemented to mark the lazybrush "transparent" color.

Parameters
value

Definition at line 117 of file kis_palette_view.cpp.

118{
119 KisPaletteDelegate *delegate =
120 dynamic_cast<KisPaletteDelegate*>(itemDelegate());
122
123 delegate->setCrossedKeyword(value);
124}
float value(const T *src, size_t ch)
void setCrossedKeyword(const QString &value)
#define KIS_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:75

References KIS_ASSERT_RECOVER_RETURN, KisPaletteDelegate::setCrossedKeyword(), and value().

◆ setDisplayRenderer()

void KisPaletteView::setDisplayRenderer ( const KoColorDisplayRendererInterface * displayRenderer)

Definition at line 335 of file kis_palette_view.cpp.

336{
337 Q_ASSERT(d->model);
338 d->model->setDisplayRenderer(displayRenderer);
339}

References d.

◆ setPaletteModel()

void KisPaletteView::setPaletteModel ( KisPaletteModel * model)

Definition at line 245 of file kis_palette_view.cpp.

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}

References d, and slotAdditionalGuiUpdate().

◆ sigColorSelected

void KisPaletteView::sigColorSelected ( const KoColor & )
signal

◆ sigIndexSelected

void KisPaletteView::sigIndexSelected ( const QModelIndex & index)
signal

◆ slotAdditionalGuiUpdate

void KisPaletteView::slotAdditionalGuiUpdate ( )
privateslot

Definition at line 294 of file kis_palette_view.cpp.

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}
void resizeRows(int newSize)

References d, and resizeRows().

◆ slotCurrentSelectionChanged

void KisPaletteView::slotCurrentSelectionChanged ( const QModelIndex & newCurrent)
privateslot

Definition at line 316 of file kis_palette_view.cpp.

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}
void sigColorSelected(const KoColor &)
void sigIndexSelected(const QModelIndex &index)

References KisPaletteModel::CheckSlotRole, KisSwatch::color(), d, KisPaletteModel::IsGroupNameRole, sigColorSelected(), and sigIndexSelected().

◆ slotFGColorChanged

void KisPaletteView::slotFGColorChanged ( const KoColor & color)
slot

This tries to select the closest color in the palette. This doesn't update the foreground color, just the visual selection.

Definition at line 239 of file kis_palette_view.cpp.

240{
241 QScopedValueRollback<bool> rollback(d->foregroundColorChangeInProgress, true);
242 selectClosestColor(color);
243}
void selectClosestColor(const KoColor &color)
selectClosestColor select a color that's closest to parameter color

References d, and selectClosestColor().

◆ slotHorizontalHeaderResized

void KisPaletteView::slotHorizontalHeaderResized ( int ,
int ,
int newSize )
privateslot

Definition at line 269 of file kis_palette_view.cpp.

270{
271 resizeRows(newSize);
273}

References resizeRows(), and slotAdditionalGuiUpdate().

◆ slotScrollerStateChanged

void KisPaletteView::slotScrollerStateChanged ( QScroller::State state)
inlineslot

Definition at line 106 of file kis_palette_view.h.

KRITAWIDGETUTILS_EXPORT void updateCursor(QWidget *source, QScroller::State state)

References KisKineticScroller::updateCursor().

Member Data Documentation

◆ d

const QScopedPointer<Private> KisPaletteView::d
private

Definition at line 119 of file kis_palette_view.h.

◆ MINIMUM_ROW_HEIGHT

int KisPaletteView::MINIMUM_ROW_HEIGHT = 10
staticprivate

Definition at line 28 of file kis_palette_view.h.


The documentation for this class was generated from the following files: