Rework colorLineEdit
parent
b8dfcc6cc2
commit
0f8fb0c5b7
|
@ -1,81 +1,45 @@
|
|||
#include "colorlineedit.h"
|
||||
|
||||
#include <QStyle>
|
||||
#include <QToolButton>
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
#ifndef COLORLINEEDIT_H
|
||||
#define COLORLINEEDIT_H
|
||||
#include <QAction>
|
||||
#include <QColorDialog>
|
||||
#include <QLineEdit>
|
||||
#include <QPushButton>
|
||||
#include <QPainter>
|
||||
|
||||
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) ;
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue