7 #include <QColorDialog>
10 #include <QDragEnterEvent>
11 #include <QDragLeaveEvent>
12 #include <QDragMoveEvent>
17 #include <QMouseEvent>
20 #include <QToolButton>
25 #include "ui_colorselector.h"
37 CColorSelector::CColorSelector(QWidget *parent) : QFrame(parent), ui(new Ui::
CColorSelector)
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);
46 QCompleter *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);
58 if (color == m_lastColor) {
return; }
66 ui->le_Color->setText(color.
hex());
67 ui->lbl_ColorIcon->setPixmap(color.
toPixmap());
83 ui->le_Color->setReadOnly(readOnly);
84 ui->tb_ColorDialog->setVisible(!readOnly);
85 this->setAcceptDrops(!readOnly);
90 ui->le_Color->clear();
91 ui->lbl_ColorIcon->setPixmap(QPixmap());
96 if (!event) {
return; }
97 setBackgroundRole(QPalette::Highlight);
98 event->acceptProposedAction();
103 if (!event) {
return; }
104 event->acceptProposedAction();
111 if (!event) {
return; }
112 const QMimeData *mime =
event->mimeData();
113 if (!mime) {
return; }
115 if (mime->hasColor())
117 const QColor color = qvariant_cast<QColor>(event->mimeData()->colorData());
118 if (!color.isValid()) {
return; }
129 if (!rgb.
isValid()) {
return; }
134 const QColor qColor(valueVariant.
value<QColor>());
135 if (!qColor.isValid()) {
return; }
140 else if (mime->hasText())
142 const QString t = mime->text();
143 if (t.isEmpty()) {
return; }
144 if (!t.contains(
"{"))
156 if (!event || event->button() != Qt::LeftButton) { QFrame::mousePressEvent(event); }
160 QDrag *drag =
new QDrag(
this);
161 QMimeData *mimeData =
new QMimeData;
163 mimeData->setColorData(QVariant::fromValue(c.
toQColor()));
164 drag->setMimeData(mimeData);
167 Qt::DropAction dropAction = drag->exec(Qt::CopyAction);
168 Q_UNUSED(dropAction);
171 void CColorSelector::colorDialog()
174 if (!q.isValid()) { q = m_lastColor.
toQColor(); }
175 QColor newColor = QColorDialog::getColor(q,
this,
"Color picker");
176 if (!newColor.isValid()) {
return; }
180 void CColorSelector::onReturnPressed()
186 void CColorSelector::setColorByName(
const QString &colorName)
188 if (colorName.isEmpty()) {
return; }
190 if (c.isValid()) { this->
setColor(c); }
193 void CColorSelector::resetToLastValidColor()
195 if (!m_lastColor.
isValid()) {
return; }
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.
virtual void mousePressEvent(QMouseEvent *event)
For dragging the color.
~CColorSelector()
Destructor.
virtual void dropEvent(QDropEvent *event)
virtual void dragEnterEvent(QDragEnterEvent *event)
virtual void dragLeaveEvent(QDragLeaveEvent *event)
swift::misc::CRgbColor getColor() const
Selected color.
virtual void dragMoveEvent(QDragMoveEvent *event)
void setColor(const swift::misc::CRgbColor &color)
Color.
QString hex(bool withHash=false) const
Hex value.
QPixmap toPixmap() const
Icon as pixmap.
QColor toQColor() const
To QColor.
bool isValid() const
Valid?
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
T value() const
Return the value converted to the type T.
bool isValid() const
True if this variant is valid.
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.