From e5e7e5e4068c2898cf33e202593c31efb87cafb3 Mon Sep 17 00:00:00 2001 From: Florian Geyer Date: Sun, 20 May 2012 20:14:15 +0200 Subject: [PATCH] Add line edit widget with integrated clear button. --- .../action/edit-clear-locationbar-rtl.png | Bin 0 -> 750 bytes src/CMakeLists.txt | 2 + src/gui/LineEdit.cpp | 50 ++++++++++++++++++ src/gui/LineEdit.h | 35 ++++++++++++ 4 files changed, 87 insertions(+) create mode 100644 share/icons/application/22x22/action/edit-clear-locationbar-rtl.png create mode 100644 src/gui/LineEdit.cpp create mode 100644 src/gui/LineEdit.h diff --git a/share/icons/application/22x22/action/edit-clear-locationbar-rtl.png b/share/icons/application/22x22/action/edit-clear-locationbar-rtl.png new file mode 100644 index 0000000000000000000000000000000000000000..80ff24af5698684b79ba8855d207067337d4347c GIT binary patch literal 750 zcmV5s$|ai^UL)Mln1*jERW}OioTB z91b%Y%nvdJ#kIADpviPE9SVi8v9W=ZlM~F&&LR*9!0-2CbaWJZdwV!MJY<7;y&3Q?uz*0iBo51-G6<>h6h(`m->@iFG+<}fxkhEytrY&MHrF2@G&cs!`QU6Eg0 zR#v7^-B2xR?`YB9-p)Gn8EfGu z(AwI{a5|l6X=y<)7{u=GE;5-6k09j-nwpw|$z(!XTN~*A%VLT|RaF%n4hJ}n1MLPh zHa5a)wZdYtU}k1U5MEqdfR98X0V*(zMkCZ}HN;|ZUU9O#yc~SSTCEm3oel-`d)O*XM;)DuqNM$zMDQCGnButfNRI!otD=TVH~0yGSI0uC7k3uC5C0$z&3D zb$3xxQgY>X6B8e(m?$k(({kvsw6uiv^>0kt#MxkXaOmv|v6)czlyL{@>+iE<3LX%O zKAB9Gt~1ub(QGGCFWk?bqQmP!SN98azwE@&yFr8p!g%6xqM7>8Obdwxc{8osck3f7 z%j)%;00tuih{i_nK2FDaWC(A=z3{*4rti-|v&`lf=a=hgtv@N1N)#YR$9nO%uT2Um gA-e*}zkSjF0Lui@@$wg%kN^Mx07*qoM6N<$f=@zU-~a#s literal 0 HcmV?d00001 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2644103cd..762f28c57 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -64,6 +64,7 @@ set(keepassx_SOURCES gui/FileDialog.cpp gui/IconModels.cpp gui/KeePass1OpenDialog.cpp + gui/LineEdit.cpp gui/MainWindow.cpp gui/entry/EditEntryWidget.cpp gui/entry/EntryAttachmentsModel.cpp @@ -102,6 +103,7 @@ set(keepassx_MOC gui/EditWidget.h gui/EditWidgetIcons.h gui/IconModels.h + gui/LineEdit.h gui/MainWindow.h gui/entry/EditEntryWidget.h gui/entry/EntryAttachmentsModel.h diff --git a/src/gui/LineEdit.cpp b/src/gui/LineEdit.cpp new file mode 100644 index 000000000..4879fdeb7 --- /dev/null +++ b/src/gui/LineEdit.cpp @@ -0,0 +1,50 @@ +/**************************************************************************** +** +** Copyright (c) 2007 Trolltech ASA +** +** Use, modification and distribution is allowed without limitation, +** warranty, liability or support of any kind. +** +****************************************************************************/ + +#include "LineEdit.h" + +#include +#include + +#include "core/DataPath.h" + +LineEdit::LineEdit(QWidget* parent) + : QLineEdit(parent) +{ + clearButton = new QToolButton(this); + clearButton->setObjectName("clearButton"); + QIcon icon = dataPath()->icon("action", "edit-clear-locationbar-rtl"); + clearButton->setIcon(icon); + clearButton->setCursor(Qt::ArrowCursor); + clearButton->setStyleSheet("QToolButton { border: none; padding: 0px; }"); + clearButton->hide(); + connect(clearButton, SIGNAL(clicked()), this, SLOT(clear())); + connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(updateCloseButton(const QString&))); + int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); + setStyleSheet(QString("QLineEdit { padding-right: %1px; } ") + .arg(clearButton->sizeHint().width() + frameWidth + 1)); + QSize msz = minimumSizeHint(); + setMinimumSize(qMax(msz.width(), clearButton->sizeHint().height() + frameWidth * 2 + 2), + qMax(msz.height(), clearButton->sizeHint().height() + frameWidth * 2 + 2)); +} + +void LineEdit::resizeEvent(QResizeEvent*) +{ + QSize sz = clearButton->sizeHint(); + int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); + clearButton->move(rect().right() - frameWidth - sz.width(), + (rect().bottom() + 1 - sz.height())/2); +} + +void LineEdit::updateCloseButton(const QString& text) +{ + clearButton->setVisible(!text.isEmpty()); +} + + diff --git a/src/gui/LineEdit.h b/src/gui/LineEdit.h new file mode 100644 index 000000000..f74a9474e --- /dev/null +++ b/src/gui/LineEdit.h @@ -0,0 +1,35 @@ +/**************************************************************************** +** +** Copyright (c) 2007 Trolltech ASA +** +** Use, modification and distribution is allowed without limitation, +** warranty, liability or support of any kind. +** +****************************************************************************/ + +#ifndef KEEPASSX_LINEEDIT_H +#define KEEPASSX_LINEEDIT_H + +#include + +class QToolButton; + +class LineEdit : public QLineEdit +{ + Q_OBJECT + +public: + LineEdit(QWidget* parent = 0); + +protected: + void resizeEvent(QResizeEvent*); + +private Q_SLOTS: + void updateCloseButton(const QString& text); + +private: + QToolButton* clearButton; +}; + +#endif // KEEPASSX_LINEEDIT_H +