swift
Loading...
Searching...
No Matches
colorselector.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include <QColor>
7#include <QColorDialog>
8#include <QCompleter>
9#include <QDrag>
10#include <QDragEnterEvent>
11#include <QDragLeaveEvent>
12#include <QDragMoveEvent>
13#include <QDropEvent>
14#include <QLabel>
15#include <QLineEdit>
16#include <QMimeData>
17#include <QMouseEvent>
18#include <QPalette>
19#include <QPixmap>
20#include <QToolButton>
21#include <QVariant>
22#include <Qt>
23#include <QtGlobal>
24
25#include "ui_colorselector.h"
26
27#include "gui/guiutility.h"
28#include "misc/icons.h"
30#include "misc/rgbcolor.h"
31#include "misc/variant.h"
32
33using namespace swift::misc;
34
36{
38 {
39 ui->setupUi(this);
40 ui->tb_ColorDialog->setIcon(CIcons::color16());
41 this->setAcceptDrops(true);
42 connect(ui->tb_ColorDialog, &QToolButton::clicked, this, &CColorSelector::colorDialog);
43 connect(ui->le_Color, &QLineEdit::editingFinished, this, &CColorSelector::onReturnPressed);
44 connect(ui->le_Color, &QLineEdit::returnPressed, this, &CColorSelector::onReturnPressed);
45
46 auto *completer = new QCompleter(QColor::colorNames(), this);
47 completer->setCaseSensitivity(Qt::CaseInsensitive);
48 completer->setMaxVisibleItems(10);
49 completer->setCompletionMode(QCompleter::PopupCompletion);
50 ui->le_Color->setCompleter(completer);
51 connect(completer, qOverload<const QString &>(&QCompleter::activated), this, &CColorSelector::setColorByName);
52 }
53
55
57 {
58 if (color == m_lastColor)
59 {
60 return;
61 }
62 if (!color.isValid())
63 {
64 this->clear();
65 m_lastColor = CRgbColor();
66 }
67 else
68 {
69 ui->le_Color->setText(color.hex());
70 ui->lbl_ColorIcon->setPixmap(color.toPixmap());
71 m_lastColor = color;
72 }
73 emit colorChanged(color);
74 }
75
76 void CColorSelector::setColor(const QColor &color) { this->setColor(CRgbColor(color)); }
77
79 {
80 CRgbColor q(ui->le_Color->text());
81 return q;
82 }
83
84 void CColorSelector::setReadOnly(bool readOnly)
85 {
86 ui->le_Color->setReadOnly(readOnly);
87 ui->tb_ColorDialog->setVisible(!readOnly);
88 this->setAcceptDrops(!readOnly);
89 }
90
92 {
93 ui->le_Color->clear();
94 ui->lbl_ColorIcon->setPixmap(QPixmap());
95 }
96
98 {
99 if (!event)
100 {
101 return;
102 }
104 event->acceptProposedAction();
105 }
106
108 {
109 if (!event)
110 {
111 return;
112 }
113 event->acceptProposedAction();
114 }
115
117
119 {
120 if (!event)
121 {
122 return;
123 }
124 const QMimeData *mime = event->mimeData();
125 if (!mime)
126 {
127 return;
128 }
129
130 if (mime->hasColor())
131 {
132 const auto color = qvariant_cast<QColor>(event->mimeData()->colorData());
133 if (!color.isValid())
134 {
135 return;
136 }
137 this->setColor(color);
138 }
140 {
141 const CVariant valueVariant(CGuiUtility::fromSwiftDragAndDropData(mime));
142 if (valueVariant.isValid())
143 {
144 if (valueVariant.canConvert<CRgbColor>())
145 {
146 const auto rgb(valueVariant.value<CRgbColor>());
147 if (!rgb.isValid())
148 {
149 return;
150 }
151 this->setColor(rgb);
152 }
153 else if (valueVariant.canConvert<QColor>())
154 {
155 const auto qColor(valueVariant.value<QColor>());
156 if (!qColor.isValid())
157 {
158 return;
159 }
160 this->setColor(qColor);
161 }
162 }
163 }
164 else if (mime->hasText())
165 {
166 const QString t = mime->text();
167 if (t.isEmpty())
168 {
169 return;
170 }
171 if (!t.contains("{"))
172 {
173 // we assume a color string, not an object (because there are no {})
174 const CRgbColor c(t);
175 if (!c.isValid())
176 {
177 return;
178 }
179 this->setColor(c);
180 }
181 }
182 }
183
185 {
186 if (!event || event->button() != Qt::LeftButton)
187 {
189 }
190 const CRgbColor c(this->getColor());
191 if (!c.isValid())
192 {
193 return;
194 }
195
196 auto *drag = new QDrag(this);
197 auto *mimeData = new QMimeData;
198
200 drag->setMimeData(mimeData);
201 drag->setPixmap(c.toPixmap());
202
203 Qt::DropAction dropAction = drag->exec(Qt::CopyAction);
204 Q_UNUSED(dropAction);
205 }
206
207 void CColorSelector::colorDialog()
208 {
209 QColor q = this->getColor().toQColor();
210 if (!q.isValid())
211 {
212 q = m_lastColor.toQColor();
213 }
214 QColor newColor = QColorDialog::getColor(q, this, "Color picker");
215 if (!newColor.isValid())
216 {
217 return;
218 }
219 this->setColor(newColor);
220 }
221
222 void CColorSelector::onReturnPressed()
223 {
224 const CRgbColor color = this->getColor();
225 if (color.isValid())
226 {
227 this->setColor(color);
228 }
229 }
230
231 void CColorSelector::setColorByName(const QString &colorName)
232 {
233 if (colorName.isEmpty())
234 {
235 return;
236 }
237 const CRgbColor c(colorName, true);
238 if (c.isValid())
239 {
240 this->setColor(c);
241 }
242 }
243
244 void CColorSelector::resetToLastValidColor()
245 {
246 if (!m_lastColor.isValid())
247 {
248 return;
249 }
250 this->setColor(m_lastColor);
251 }
252} // namespace swift::gui::components
static swift::misc::CVariant fromSwiftDragAndDropData(const QMimeData *mime)
From text dropped.
static bool hasSwiftVariantMimeType(const QMimeData *mime)
Mime data with swift type.
void setReadOnly(bool readOnly)
Read only.
void colorChanged(const swift::misc::CRgbColor &color)
Color has been changed.
void mousePressEvent(QMouseEvent *event)
For dragging the color.
void dragEnterEvent(QDragEnterEvent *event)
void dragLeaveEvent(QDragLeaveEvent *event)
swift::misc::CRgbColor getColor() const
Selected color.
CColorSelector(QWidget *parent=nullptr)
Constructor.
void dragMoveEvent(QDragMoveEvent *event)
void setColor(const swift::misc::CRgbColor &color)
Color.
static const QPixmap & color16()
Color.
QString hex(bool withHash=false) const
Hex value.
Definition rgbcolor.cpp:100
QPixmap toPixmap() const
Icon as pixmap.
Definition rgbcolor.cpp:30
QColor toQColor() const
To QColor.
Definition rgbcolor.cpp:52
bool isValid() const
Valid?
Definition rgbcolor.cpp:153
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition variant.h:69
T value() const
Return the value converted to the type T.
Definition variant.h:154
bool isValid() const
True if this variant is valid.
Definition variant.h:230
bool canConvert(int typeId) const
True if this variant can be converted to the type with the given metatype ID.
High level reusable GUI components.
Free functions in swift::misc.
void clicked(bool checked)
QStringList colorNames()
bool isValid() const const
QColor getColor(const QColor &initial, QWidget *parent, const QString &title, QColorDialog::ColorDialogOptions options)
void activated(const QModelIndex &index)
QFrame(QWidget *parent, Qt::WindowFlags f)
virtual bool event(QEvent *e) override
void editingFinished()
void returnPressed()
bool hasColor() const const
bool hasText() const const
void setColorData(const QVariant &color)
QString text() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
CaseInsensitive
DropAction
LeftButton
QVariant fromValue(T &&value)
QWidget(QWidget *parent, Qt::WindowFlags f)
void setAcceptDrops(bool on)
virtual void mousePressEvent(QMouseEvent *event)
void setBackgroundRole(QPalette::ColorRole role)