From 0f8fb0c5b77b512becf23ce2667a5ad8c55ed09b Mon Sep 17 00:00:00 2001 From: Unknown Date: Fri, 17 Mar 2017 10:49:29 +0100 Subject: [PATCH] Rework colorLineEdit --- colorlineedit.cpp | 84 ++++++++++++++--------------------------------- colorlineedit.h | 18 ++++------ 2 files changed, 31 insertions(+), 71 deletions(-) diff --git a/colorlineedit.cpp b/colorlineedit.cpp index 959c0b2..9ae0b26 100644 --- a/colorlineedit.cpp +++ b/colorlineedit.cpp @@ -1,81 +1,45 @@ #include "colorlineedit.h" -#include -#include +QPainter *ColorLineEdit::painter = new QPainter(); -ColorLineEdit::ColorLineEdit(QWidget *parent) : - QLineEdit(parent) +ColorLineEdit::ColorLineEdit(QWidget *parent) : QLineEdit(parent) { - // Create the search button and set its icon, cursor, and stylesheet - this->mColorButton = new QToolButton(this); - this->mColorButton->setFixedSize(20,20); - this->mColorButton->setCursor(Qt::PointingHandCursor); - this->mColorButton->setStyleSheet(this->buttonStyleSheetForCurrentState()); + action = this->addAction(defaultIcon, QLineEdit::LeadingPosition); + connect(action, &QAction::triggered, this, &ColorLineEdit::selectColor); - // Update the search button when the text changes - QObject::connect(this, SIGNAL(textChanged(QString)), SLOT(updateColorButton(QString))); - QObject::connect(this->mColorButton,SIGNAL(clicked(bool)), SLOT(selectColor())); - // Some stylesheet and size corrections for the text box - //this->setPlaceholderText(tr("Search")); - //this->setStyleSheet(this->styleSheetForCurrentState()); - - int frameWidth = this->style()->pixelMetric(QStyle::PM_DefaultFrameWidth); - QSize minSizeHint = this->minimumSizeHint(); - this->setMinimumSize(qMax(minSizeHint.width(), this->mColorButton->sizeHint().width() + frameWidth * 2 + 2), - qMax(minSizeHint.height(), this->mColorButton->sizeHint().height() + frameWidth * 2+2)); + connect(this, &QLineEdit::textChanged, this, &ColorLineEdit::updateColorButton); } -void ColorLineEdit::resizeEvent(QResizeEvent *event) -{ - Q_UNUSED(event); - QSize size = this->mColorButton->sizeHint(); - int frameWidth = this->style()->pixelMetric(QStyle::PM_DefaultFrameWidth); - this->mColorButton->move(this->rect().right() - frameWidth - size.width() - 2, (this->rect().bottom() + 2 - size.height()) / 2); -} + void ColorLineEdit::selectColor() { const QColorDialog::ColorDialogOptions options = QFlag(QColorDialog::DontUseNativeDialog); - const QColor color = QColorDialog::getColor(this->text(), this, tr("select color"),options); + QColor current = QColor(this->text()); + + if (!current.isValid()) { + current = Qt::white; + } + const QColor color = QColorDialog::getColor(current, this, NULL, options); if (color.isValid()) { this->setText(color.name()); } } +inline const QIcon ColorLineEdit::getColorIcon(const QColor &color) +{ + 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); +} + void ColorLineEdit::updateColorButton(const QString &text) { const QColor color = QColor(text); - if (color.isValid()) - { - // We have some text in the box - set the button to clear the text - QPixmap pixmap(this->height(),this->height()); - pixmap.fill(color); - QIcon icon(pixmap); - this->mColorButton->setIcon(icon); - } - else - { - // The text box is empty - make the icon do nothing when clicked - QIcon icon(":/color"); - this->mColorButton->setIcon(icon); - } + const QIcon icon = color.isValid() ? getColorIcon(color) : defaultIcon; - //this->mColorButton->setStyleSheet(this->buttonStyleSheetForCurrentState()); -} - - -QString ColorLineEdit::buttonStyleSheetForCurrentState() const -{ - QString style; - style += "QToolButton {"; - style += "border: 1px solid black; margin: 0; padding: 0;"; - style += "}"; - - /*if (!this->text().isEmpty()) - { - style += "QToolButton:hover { background-image: url(:/images/esf-clear-hover.png); }"; - style += "QToolButton:pressed { background-image: url(:/images/esf-clear-active.png); }"; - }*/ - - return style; + action->setIcon(icon); } diff --git a/colorlineedit.h b/colorlineedit.h index 06c3aa9..0a58318 100644 --- a/colorlineedit.h +++ b/colorlineedit.h @@ -1,13 +1,11 @@ #ifndef COLORLINEEDIT_H #define COLORLINEEDIT_H +#include #include #include -#include +#include -class QToolButton; - -class ColorLineEdit : public QLineEdit -{ +class ColorLineEdit : public QLineEdit { Q_OBJECT public: @@ -16,17 +14,15 @@ public: public slots: void selectColor(); -protected: - void resizeEvent(QResizeEvent *event); - private slots: void updateColorButton(const QString &text); private: - //QString styleSheetForCurrentState() const; - QString buttonStyleSheetForCurrentState() const; + QAction *action; + const QIcon defaultIcon = QIcon(":/color"); + static QPainter *painter; - QToolButton *mColorButton; + static inline const QIcon getColorIcon(const QColor &color) ; };