2015-06-02 19:42:10 -07:00
|
|
|
#include "colorlineedit.h"
|
|
|
|
|
2017-03-17 02:49:29 -07:00
|
|
|
QPainter *ColorLineEdit::painter = new QPainter();
|
2015-06-02 19:42:10 -07:00
|
|
|
|
2017-03-17 02:49:29 -07:00
|
|
|
ColorLineEdit::ColorLineEdit(QWidget *parent) : QLineEdit(parent)
|
2015-06-02 19:42:10 -07:00
|
|
|
{
|
2017-03-17 02:49:29 -07:00
|
|
|
action = this->addAction(defaultIcon, QLineEdit::LeadingPosition);
|
|
|
|
connect(action, &QAction::triggered, this, &ColorLineEdit::selectColor);
|
2015-06-02 19:42:10 -07:00
|
|
|
|
2017-03-17 02:49:29 -07:00
|
|
|
connect(this, &QLineEdit::textChanged, this, &ColorLineEdit::updateColorButton);
|
2015-06-02 19:42:10 -07:00
|
|
|
}
|
|
|
|
|
2017-03-17 02:49:29 -07:00
|
|
|
|
2015-06-02 19:42:10 -07:00
|
|
|
void ColorLineEdit::selectColor()
|
|
|
|
{
|
|
|
|
const QColorDialog::ColorDialogOptions options = QFlag(QColorDialog::DontUseNativeDialog);
|
2017-03-17 02:49:29 -07:00
|
|
|
QColor current = QColor(this->text());
|
|
|
|
|
|
|
|
if (!current.isValid()) {
|
|
|
|
current = Qt::white;
|
|
|
|
}
|
|
|
|
const QColor color = QColorDialog::getColor(current, this, NULL, options);
|
2015-06-02 19:42:10 -07:00
|
|
|
|
|
|
|
if (color.isValid()) {
|
|
|
|
this->setText(color.name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 02:49:29 -07:00
|
|
|
inline const QIcon ColorLineEdit::getColorIcon(const QColor &color)
|
2015-06-02 19:42:10 -07:00
|
|
|
{
|
2017-03-17 02:49:29 -07:00
|
|
|
QPixmap pixmap(16, 16);
|
|
|
|
pixmap.fill(color);
|
|
|
|
painter->begin(&pixmap);
|
|
|
|
painter->drawRect(0, 0, pixmap.size().height() - 1, pixmap.size().width() - 1);
|
|
|
|
painter->end();
|
|
|
|
return QIcon(pixmap);
|
2015-06-02 19:42:10 -07:00
|
|
|
}
|
|
|
|
|
2017-03-17 02:49:29 -07:00
|
|
|
void ColorLineEdit::updateColorButton(const QString &text)
|
2015-06-02 19:42:10 -07:00
|
|
|
{
|
2017-03-17 02:49:29 -07:00
|
|
|
const QColor color = QColor(text);
|
|
|
|
const QIcon icon = color.isValid() ? getColorIcon(color) : defaultIcon;
|
2015-06-02 19:42:10 -07:00
|
|
|
|
2017-03-17 02:49:29 -07:00
|
|
|
action->setIcon(icon);
|
2015-06-02 19:42:10 -07:00
|
|
|
}
|