16 changed files with 382 additions and 3 deletions
@ -0,0 +1,137 @@ |
|||||
|
#include "coloredit_p.h" |
||||
|
#include <QtWidgets/QLineEdit> |
||||
|
#include <QtWidgets/QHBoxLayout> |
||||
|
#include <QtWidgets/QStylePainter> |
||||
|
#include <QtWidgets/QStyleOptionFrame> |
||||
|
#include <QtMvvmCore/Messages> |
||||
|
using namespace QtMvvm; |
||||
|
|
||||
|
ColorEdit::ColorEdit(QWidget *parent) : |
||||
|
QWidget{parent}, |
||||
|
_btn{new ColorButton{this}}, |
||||
|
_edit{new QLineEdit{this}}, |
||||
|
_validator{new QRegularExpressionValidator{this}} |
||||
|
{ |
||||
|
auto layout = new QHBoxLayout{this}; |
||||
|
layout->addWidget(_btn); |
||||
|
layout->addWidget(_edit); |
||||
|
layout->addStretch(1); |
||||
|
layout->setStretch(0, 0); |
||||
|
layout->setStretch(1, 0); |
||||
|
layout->setContentsMargins({0,0,0,0}); |
||||
|
setLayout(layout); |
||||
|
|
||||
|
_edit->setValidator(_validator); |
||||
|
connect(_edit, &QLineEdit::textEdited, |
||||
|
this, &ColorEdit::updateColor); |
||||
|
connect(_edit, &QLineEdit::editingFinished, |
||||
|
this, [this](){ |
||||
|
_edit->setText(_color.name(_alpha ? QColor::HexArgb : QColor::HexRgb)); |
||||
|
}); |
||||
|
|
||||
|
connect(_btn, &ColorButton::clicked, |
||||
|
this, &ColorEdit::showDialog); |
||||
|
|
||||
|
setAlpha(false); |
||||
|
setColor(palette().color(QPalette::Highlight)); |
||||
|
} |
||||
|
|
||||
|
QColor ColorEdit::color() const |
||||
|
{ |
||||
|
return _color; |
||||
|
} |
||||
|
|
||||
|
bool ColorEdit::useAlpha() const |
||||
|
{ |
||||
|
return _alpha; |
||||
|
} |
||||
|
|
||||
|
void ColorEdit::setColor(QColor color) |
||||
|
{ |
||||
|
if (_color == color) |
||||
|
return; |
||||
|
|
||||
|
_color = std::move(color); |
||||
|
_btn->setColor(_color); |
||||
|
if(!_skipEditUpdate) |
||||
|
_edit->setText(_color.name(_alpha ? QColor::HexArgb : QColor::HexRgb)); |
||||
|
emit colorChanged(_color); |
||||
|
} |
||||
|
|
||||
|
void ColorEdit::setAlpha(bool alpha) |
||||
|
{ |
||||
|
if (_alpha == alpha) |
||||
|
return; |
||||
|
|
||||
|
_alpha = alpha; |
||||
|
_validator->setRegularExpression(QRegularExpression{ |
||||
|
QStringLiteral(R"__(^#(?:[0-9a-f]{%1}){1,2}$)__").arg(_alpha ? 4 : 3), |
||||
|
QRegularExpression::CaseInsensitiveOption | QRegularExpression::DontCaptureOption |
||||
|
}); |
||||
|
_edit->setMaxLength(_alpha ? 9 : 7); |
||||
|
_edit->setText(_color.name(_alpha ? QColor::HexArgb : QColor::HexRgb)); |
||||
|
emit alphaChanged(_alpha); |
||||
|
} |
||||
|
|
||||
|
void ColorEdit::showDialog() |
||||
|
{ |
||||
|
QtMvvm::getColor(this, [this](const QColor &color) { |
||||
|
if(color.isValid()) |
||||
|
setColor(color); |
||||
|
}, tr("Select a color"), _color, _alpha); |
||||
|
} |
||||
|
|
||||
|
void ColorEdit::updateColor(const QString &text) |
||||
|
{ |
||||
|
if(_edit->hasAcceptableInput()) { |
||||
|
QColor color{text}; |
||||
|
if(color.isValid()) { |
||||
|
_skipEditUpdate = true; |
||||
|
setColor(color); |
||||
|
_skipEditUpdate = false; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
ColorButton::ColorButton(QWidget *parent) : |
||||
|
QAbstractButton{parent} |
||||
|
{ |
||||
|
setSizePolicy({QSizePolicy::Expanding, QSizePolicy::Preferred}); |
||||
|
} |
||||
|
|
||||
|
QColor ColorButton::color() const |
||||
|
{ |
||||
|
return _color; |
||||
|
} |
||||
|
|
||||
|
QSize ColorButton::sizeHint() const |
||||
|
{ |
||||
|
return {50, 25}; |
||||
|
} |
||||
|
|
||||
|
void ColorButton::setColor(QColor color) |
||||
|
{ |
||||
|
_color = color; |
||||
|
repaint(); |
||||
|
} |
||||
|
|
||||
|
void ColorButton::paintEvent(QPaintEvent *) |
||||
|
{ |
||||
|
QStylePainter painter{this}; |
||||
|
|
||||
|
painter.fillRect(painter.window(), _color); |
||||
|
|
||||
|
QStyleOptionFrame option; |
||||
|
option.initFrom(this); |
||||
|
option.frameShape = QFrame::Panel; |
||||
|
option.lineWidth = 2; |
||||
|
option.midLineWidth = 2; |
||||
|
if(isDown()) |
||||
|
option.state |= QStyle::State_Sunken; |
||||
|
else |
||||
|
option.state |= QStyle::State_Raised; |
||||
|
|
||||
|
painter.drawControl(QStyle::CE_ShapedFrame, option); |
||||
|
} |
@ -0,0 +1,74 @@ |
|||||
|
#ifndef COLOREDIT_P_H |
||||
|
#define COLOREDIT_P_H |
||||
|
|
||||
|
#include <QtGui/QColor> |
||||
|
#include <QtGui/QRegularExpressionValidator> |
||||
|
#include <QtWidgets/QWidget> |
||||
|
#include <QtWidgets/QAbstractButton> |
||||
|
#include <QtWidgets/QLineEdit> |
||||
|
|
||||
|
#include "qtmvvmwidgets_global.h" |
||||
|
|
||||
|
namespace QtMvvm { |
||||
|
|
||||
|
class ColorButton : public QAbstractButton |
||||
|
{ |
||||
|
Q_OBJECT |
||||
|
|
||||
|
Q_PROPERTY(QColor color READ color WRITE setColor) |
||||
|
|
||||
|
public: |
||||
|
explicit ColorButton(QWidget *parent = nullptr); |
||||
|
|
||||
|
QColor color() const; |
||||
|
|
||||
|
QSize sizeHint() const override; |
||||
|
|
||||
|
public Q_SLOTS: |
||||
|
void setColor(QColor color); |
||||
|
|
||||
|
protected: |
||||
|
void paintEvent(QPaintEvent *event) override; |
||||
|
|
||||
|
private: |
||||
|
QColor _color; |
||||
|
}; |
||||
|
|
||||
|
class ColorEdit : public QWidget |
||||
|
{ |
||||
|
Q_OBJECT |
||||
|
|
||||
|
Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged USER true) |
||||
|
Q_PROPERTY(bool alpha READ useAlpha WRITE setAlpha NOTIFY alphaChanged) |
||||
|
|
||||
|
public: |
||||
|
explicit ColorEdit(QWidget *parent = nullptr); |
||||
|
|
||||
|
QColor color() const; |
||||
|
bool useAlpha() const; |
||||
|
|
||||
|
public Q_SLOTS: |
||||
|
void setColor(QColor color); |
||||
|
void setAlpha(bool alpha); |
||||
|
|
||||
|
Q_SIGNALS: |
||||
|
void colorChanged(const QColor &color); |
||||
|
void alphaChanged(bool alpha); |
||||
|
|
||||
|
private Q_SLOTS: |
||||
|
void showDialog(); |
||||
|
void updateColor(const QString &text); |
||||
|
|
||||
|
private: |
||||
|
QColor _color; |
||||
|
bool _alpha = true; //in order to be set to false in the constructor
|
||||
|
|
||||
|
ColorButton *_btn; |
||||
|
QLineEdit *_edit; |
||||
|
QRegularExpressionValidator *_validator; |
||||
|
bool _skipEditUpdate = false; |
||||
|
}; |
||||
|
|
||||
|
} |
||||
|
|
||||
|
#endif // COLOREDIT_P_H
|
Loading…
Reference in new issue