Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_color_patches.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2010 Adam Celarek <kdedev at xibo dot at>
3 * SPDX-FileCopyrightText: 2023 Sharaf Zaman <shzam@sdf.org>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8#include "kis_color_patches.h"
9
10#include <QApplication>
11#include <QLayout>
12#include <QPainter>
13#include <QWheelEvent>
14#include <QMouseEvent>
15#include <QDrag>
16#include <QMimeData>
17
18#include <kconfig.h>
19#include <kconfiggroup.h>
20#include <ksharedconfig.h>
21
22#include "kis_canvas2.h"
26
27KisColorPatches::KisColorPatches(QString configPrefix, QWidget *parent)
28 : KisColorSelectorBase(parent)
29 , m_configPrefix(configPrefix)
30 , m_colorPatchesView(new KisColorPatchesTableView(configPrefix, parent))
31{
33}
34
35void KisColorPatches::mouseReleaseEvent(QMouseEvent* event)
36{
37 event->ignore();
39 if (event->isAccepted() || !rect().contains(event->pos()))
40 return;
41
42 if (!m_canvas) return;
43
44 boost::optional<KoColor> isColor = m_colorPatchesView->colorPatchAt(event->globalPos());
45 if (!isColor) {
46 return;
47 }
48
49 KoColor color = *isColor;
50 if (event->button() == Qt::LeftButton) {
51 m_canvas->resourceManager()->setForegroundColor(color);
52 } else if (event->button() == Qt::RightButton) {
53 m_canvas->resourceManager()->setBackgroundColor(color);
54 }
55 event->accept();
56}
57
58void KisColorPatches::mousePressEvent(QMouseEvent *event)
59{
60 boost::optional<KoColor> isColor = m_colorPatchesView->colorPatchAt(event->globalPos());
61 if (!isColor) {
62 return;
63 }
64
65 KoColor color = *isColor;
66
68 if (event->isAccepted())
69 return;
70
71 updateColorPreview(color);
72 event->accept();
73}
74
79
84
89
94
96{
97 for (int i = 0; i < buttonList.size(); i++) {
98 buttonList.at(i)->setParent(this);
99 }
100 m_buttonList = buttonList;
101}
102
104{
106
107 KConfigGroup cfg = KSharedConfig::openConfig()->group("advancedColorSelector");
109
110 QBoxLayout::Direction layoutDirection;
111 if (cfg.readEntry(m_configPrefix + "Alignment", false)) {
113 layoutDirection = QBoxLayout::TopToBottom;
114 } else {
116 layoutDirection = QBoxLayout::LeftToRight;
117 }
118
119 QBoxLayout *boxLayout = dynamic_cast<QBoxLayout*>(layout());
120 if (!boxLayout) {
121 boxLayout = new QBoxLayout(layoutDirection, this);
122 boxLayout->setContentsMargins(0, 0, 0, 0);
123 setLayout(boxLayout);
124 layout()->addWidget(m_colorPatchesView);
125 } else if (boxLayout->direction() != layoutDirection) {
126 boxLayout->setDirection(layoutDirection);
127 }
128
129
130 if (isPopup()) {
131 if (m_direction == Vertical) {
132 setMinimumWidth(m_colorPatchesView->width());
133 setMaximumWidth(m_colorPatchesView->width());
134 } else {
135 setMinimumHeight(m_colorPatchesView->height());
136 setMaximumHeight(m_colorPatchesView->height());
137 }
138 }
139
140 for (int i = 0; i < m_buttonList.size(); i++) {
141 m_buttonList.at(i)->setGeometry(QRect(QPoint(0, 0), m_colorPatchesView->cellSize()));
142 }
143
144 setPopupBehaviour(false, false);
145 update();
146}
147
148
153
void setColors(const QList< KoColor > &colors)
boost::optional< KoColor > colorPatchAt(const QPoint &pos) const
void addColorPatch(const KoColor &color)
void updateSettings() override
void mousePressEvent(QMouseEvent *) override
void setCanvas(KisCanvas2 *canvas) override
void mouseReleaseEvent(QMouseEvent *) override
KisColorPatchesTableView * m_colorPatchesView
QList< KoColor > colors() const
void setAdditionalButtons(QList< QWidget * > buttonList)
QList< QWidget * > m_buttonList
KisColorPatches(QString configPrefix, QWidget *parent=0)
void addColorPatch(const KoColor &color)
void unsetCanvas() override
void setColors(const QList< KoColor > &colors)
Base class for all color selectors, that should support color management and zooming.
void updateColorPreview(const KoColor &color)
virtual void setCanvas(KisCanvas2 *canvas)
void mouseReleaseEvent(QMouseEvent *) override
void mousePressEvent(QMouseEvent *) override
QPointer< KisCanvas2 > m_canvas
void setPopupBehaviour(bool onMouseOver, bool onMouseClick)