• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • krita API Reference
  • KDE Home
  • Contact Us
 

KritaWidgets

  • sources
  • kfour-appscomplete
  • krita
  • libs
  • widgets
KisPaletteComboBox.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2018 Michael Zhou <[email protected]>
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 
19 KisPaletteComboBox::KisPaletteComboBox(QWidget *parent)
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 
31 KisPaletteComboBox::~KisPaletteComboBox()
32 { }
33 
34 void KisPaletteComboBox::setPaletteModel(const KisPaletteModel *paletteModel)
35 {
36  if (!m_model.isNull()) {
37  m_model->disconnect(this);
38  }
39  m_model = paletteModel;
40  if (m_model.isNull()) { return; }
41  slotPaletteChanged();
42  connect(m_model, SIGNAL(sigPaletteChanged()),
43  SLOT(slotPaletteChanged()));
44  connect(m_model, SIGNAL(sigPaletteModified()),
45  SLOT(slotPaletteChanged()));
46 }
47 
48 void KisPaletteComboBox::setCompanionView(KisPaletteView *view)
49 {
50  if (!m_view.isNull()) {
51  m_view->disconnect(this);
52  disconnect(m_view.data());
53  }
54  m_view = view;
55  setPaletteModel(view->paletteModel());
56  connect(view, SIGNAL(sigIndexSelected(QModelIndex)),
57  SLOT(slotSwatchSelected(QModelIndex)));
58 }
59 
60 void KisPaletteComboBox::slotPaletteChanged()
61 {
62  clear();
63  m_groupMapMap.clear();
64  m_idxSwatchMap.clear();
65 
66  if (QSharedPointer<KoColorSet>(m_model->colorSet()).isNull()) { return; }
67 
68  for (const QString &groupName : m_model->colorSet()->getGroupNames()) {
69  QVector<SwatchInfoType> infoList;
70  PosIdxMapType posIdxMap;
71  const KisSwatchGroup *group = m_model->colorSet()->getGroup(groupName);
72  for (const SwatchInfoType &info : group->infoList()) {
73  infoList.append(info);
74  }
75  std::sort(infoList.begin(), infoList.end(), swatchInfoLess);
76  for (const SwatchInfoType &info : infoList) {
77  const KisSwatch &swatch = info.swatch;
78  QString name = swatch.name();
79  if (!swatch.id().isEmpty()){
80  name = swatch.id() + " - " + swatch.name();
81  }
82  addSqueezedItem(QIcon(createColorSquare(swatch)), name);
83  posIdxMap[SwatchPosType(info.column, info.row)] = count() - 1;
84  m_idxSwatchMap.push_back(swatch);
85  }
86  m_groupMapMap[group->name()] = posIdxMap;
87  }
88  if (m_view.isNull()) {
89  setCurrentIndex(0);
90  }
91  QModelIndex idx = m_view->currentIndex();
92  if (!idx.isValid()) { 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
97  slotSwatchSelected(idx);
98  blockSignals(false);
99 }
100 
101 bool KisPaletteComboBox::swatchInfoLess(const SwatchInfoType &first, const SwatchInfoType &second)
102 {
103  return first.swatch.name() < second.swatch.name();
104 }
105 
106 QPixmap KisPaletteComboBox::createColorSquare(const KisSwatch &swatch) const
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 
130 void 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 
143 void KisPaletteComboBox::slotIndexUpdated(int idx)
144 {
145  if (idx >= 0 && idx < m_idxSwatchMap.size()) {
146  emit sigColorSelected(m_idxSwatchMap[idx].color());
147  }
148 }
KisPaletteComboBox::sigColorSelected
void sigColorSelected(const KoColor &)
KisPaletteModel::IsGroupNameRole
Definition: KisPaletteModel.h:36
QPainter::setPen
void setPen(const QColor &color)
QPixmap::fromImage
QPixmap fromImage(const QImage &image, QFlags< Qt::ImageConversionFlag > flags)
QImage::fill
void fill(uint pixelValue)
QPainter::drawEllipse
void drawEllipse(const QRectF &rectangle)
QVector::begin
iterator begin()
KisPaletteComboBox::KisPaletteComboBox
KisPaletteComboBox(QWidget *parent=0)
Definition: KisPaletteComboBox.cpp:19
QModelIndex::column
int column() const
QVector::append
void append(const T &value)
QVector::push_back
void push_back(const T &value)
QWidget
KisPaletteComboBox::~KisPaletteComboBox
~KisPaletteComboBox()
Definition: KisPaletteComboBox.cpp:31
QPainter::pen
const QPen & pen() const
QSharedPointer
QPainter
KisPaletteModel::GroupNameRole
Definition: KisPaletteModel.h:38
kis_palette_view.h
QPen
KisPaletteView
Definition: kis_palette_view.h:27
QPainter::begin
bool begin(QPaintDevice *device)
QModelIndex::data
QVariant data(int role) const
QPixmap
QPen::setColor
void setColor(const QColor &color)
QPainter::end
bool end()
QVector::clear
void clear()
QPointer::isNull
bool isNull() const
QString
QPen::setWidth
void setWidth(int width)
KisPaletteModel
The KisPaletteModel class This, together with KisPaletteView and KisPaletteDelegate forms a mvc way t...
Definition: KisPaletteModel.h:28
QIcon
KisSqueezedComboBox
QModelIndex::isValid
bool isValid() const
QBrush
QPainter::setBrush
void setBrush(const QBrush &brush)
QVector::end
iterator end()
KisPaletteModel::CheckSlotRole
Definition: KisPaletteModel.h:37
KisPaletteComboBox.h
QBrush::setColor
void setColor(const QColor &color)
QVector::size
int size() const
QModelIndex
QVector
QPointer::data
T * data() const
KisPaletteModel::RowInGroupRole
Definition: KisPaletteModel.h:39
QImage
KisPaletteView::paletteModel
KisPaletteModel * paletteModel() const
Definition: kis_palette_view.cpp:223
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Jan 23 2021 11:48:22 by doxygen 1.8.16 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KritaWidgets

Skip menu "KritaWidgets"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

krita API Reference

Skip menu "krita API Reference"
  • libs
  •   KritaBasicFlakes
  •   brush
  •   KritaUndo2
  •   KritaFlake
  •   image
  •   KritaPlugin
  •   Krita
  •   KritaPigment
  •   KritaResources
  •   KritaStore
  •   ui
  •   KritaWidgets
  •   KritaWidgetUtils
  • plugins
  •   Assitants
  •   Extensions
  •   Filters
  •   Generators
  •   Formats
  •           src
  •   PaintOps
  •     libpaintop

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal