|
|
@ -8,21 +8,28 @@ |
|
|
|
****************************************************************************/ |
|
|
|
****************************************************************************/ |
|
|
|
|
|
|
|
|
|
|
|
#include "lineedit.h" |
|
|
|
#include "lineedit.h" |
|
|
|
#include <QToolButton> |
|
|
|
#include <algorithm> |
|
|
|
#include <QStyle> |
|
|
|
#include <QStyle> |
|
|
|
#include <QtDebug> |
|
|
|
#include <QToolButton> |
|
|
|
|
|
|
|
#include <QResizeEvent> |
|
|
|
|
|
|
|
|
|
|
|
LineEdit::LineEdit(QWidget *parent) |
|
|
|
LineEdit::LineEdit(QWidget *parent) |
|
|
|
: QLineEdit(parent) |
|
|
|
: QLineEdit(parent) |
|
|
|
{ |
|
|
|
{ |
|
|
|
searchButton = new QToolButton(this); |
|
|
|
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); |
|
|
|
|
|
|
|
|
|
|
|
QPixmap pixmap1(":/lineeditimages/search.png"); |
|
|
|
QPixmap pixmap1(":/lineeditimages/search.png"); |
|
|
|
|
|
|
|
searchButton = new QToolButton(this); |
|
|
|
searchButton->setIcon(QIcon(pixmap1)); |
|
|
|
searchButton->setIcon(QIcon(pixmap1)); |
|
|
|
searchButton->setIconSize(pixmap1.size()); |
|
|
|
searchButton->setIconSize(pixmap1.size()); |
|
|
|
searchButton->setCursor(Qt::ArrowCursor); |
|
|
|
searchButton->setCursor(Qt::ArrowCursor); |
|
|
|
searchButton->setStyleSheet("QToolButton { border: none; padding: 2px; }"); |
|
|
|
searchButton->setStyleSheet("QToolButton { border: none; padding: 2px; }"); |
|
|
|
clearButton = new QToolButton(this); |
|
|
|
|
|
|
|
|
|
|
|
int clearButtonSizeHintWidth = 0; |
|
|
|
|
|
|
|
int clearButtonSizeHintHeight = 0; |
|
|
|
|
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) |
|
|
|
QPixmap pixmap2(":/lineeditimages/clear_left.png"); |
|
|
|
QPixmap pixmap2(":/lineeditimages/clear_left.png"); |
|
|
|
|
|
|
|
clearButton = new QToolButton(this); |
|
|
|
clearButton->setIcon(QIcon(pixmap2)); |
|
|
|
clearButton->setIcon(QIcon(pixmap2)); |
|
|
|
clearButton->setIconSize(pixmap2.size()); |
|
|
|
clearButton->setIconSize(pixmap2.size()); |
|
|
|
clearButton->setCursor(Qt::ArrowCursor); |
|
|
|
clearButton->setCursor(Qt::ArrowCursor); |
|
|
@ -31,25 +38,35 @@ LineEdit::LineEdit(QWidget *parent) |
|
|
|
clearButton->hide(); |
|
|
|
clearButton->hide(); |
|
|
|
connect(clearButton, SIGNAL(clicked()), this, SLOT(clear())); |
|
|
|
connect(clearButton, SIGNAL(clicked()), this, SLOT(clear())); |
|
|
|
connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(updateCloseButton(const QString&))); |
|
|
|
connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(updateCloseButton(const QString&))); |
|
|
|
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); |
|
|
|
|
|
|
|
setStyleSheet(QString("QLineEdit { padding-right: %1px; padding-left: %2px; }").arg(clearButton->sizeHint().width() + frameWidth + 1).arg(clearButton->sizeHint().width() + frameWidth + 1)); |
|
|
|
clearButtonSizeHintWidth = clearButton->sizeHint().width(); |
|
|
|
QSize msz = minimumSizeHint(); |
|
|
|
clearButtonSizeHintHeight = clearButton->sizeHint().height(); |
|
|
|
setMinimumSize(qMax(msz.width(), clearButton->sizeHint().width() + searchButton->sizeHint().width() + frameWidth * 2 + 2), |
|
|
|
setStyleSheet(QString("QLineEdit { padding-left: %1px; padding-right: %2px; }").arg(searchButton->sizeHint().width()).arg(clearButtonSizeHintWidth)); |
|
|
|
qMax(msz.height(), clearButton->sizeHint().height() + frameWidth * 2 + 2)); |
|
|
|
#else |
|
|
|
|
|
|
|
setClearButtonEnabled(true); |
|
|
|
|
|
|
|
setStyleSheet(QString("QLineEdit { padding-left: %1px; }").arg(searchButton->sizeHint().width())); // padding between text and widget borders
|
|
|
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QSize msz = sizeHint(); |
|
|
|
|
|
|
|
setMinimumSize(qMax(msz.width(), searchButton->sizeHint().width() + clearButtonSizeHintWidth), |
|
|
|
|
|
|
|
std::max({ msz.height(), searchButton->sizeHint().height(), clearButtonSizeHintHeight }) + frameWidth * 2); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void LineEdit::resizeEvent(QResizeEvent *) |
|
|
|
void LineEdit::resizeEvent(QResizeEvent *e) |
|
|
|
{ |
|
|
|
{ |
|
|
|
QSize sz = searchButton->sizeHint(); |
|
|
|
|
|
|
|
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); |
|
|
|
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); |
|
|
|
searchButton->move(rect().left() + frameWidth, (rect().bottom() + 2 - sz.height())/2); |
|
|
|
|
|
|
|
sz = clearButton->sizeHint(); |
|
|
|
QSize sz = searchButton->sizeHint(); |
|
|
|
clearButton->move(rect().right() - frameWidth - sz.width(), |
|
|
|
searchButton->move(frameWidth, (e->size().height() - sz.height()) / 2); |
|
|
|
(rect().bottom() + 2 - sz.height())/2); |
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) |
|
|
|
|
|
|
|
QSize cz = clearButton->sizeHint(); |
|
|
|
|
|
|
|
clearButton->move((e->size().width() - frameWidth - cz.width()), (e->size().height() - sz.height()) / 2); |
|
|
|
|
|
|
|
#endif |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void LineEdit::updateCloseButton(const QString& text) |
|
|
|
void LineEdit::updateCloseButton(const QString& text) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) |
|
|
|
clearButton->setVisible(!text.isEmpty()); |
|
|
|
clearButton->setVisible(!text.isEmpty()); |
|
|
|
|
|
|
|
#endif |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|