ClearableLineEdit.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package MemoryX::gui-plugins::SceneEditor
19  * @date 2015
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 /* Qt-headers */
25 #include "ClearableLineEdit.h"
26 
27 #include <QStyle>
28 
30  QLineEdit(parent), clearButton(new QToolButton(this))
31 {
32  QIcon icon = QIcon::fromTheme("edit-clear", QIcon(":/icon/dialog-close.ico"));
33  clearButton->setIcon(icon);
34  clearButton->setCursor(Qt::ArrowCursor);
35  clearButton->setStyleSheet("QToolButton { border: none; padding: 0px; }");
36  clearButton->hide();
37 
38  int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
39  setStyleSheet(QString("QLineEdit { padding-right: %1px; } ")
40  .arg(clearButton->sizeHint().width() + frameWidth + 1));
41  QSize minSize = minimumSizeHint();
42  setMinimumSize(qMax(minSize.width(), clearButton->sizeHint().height() + frameWidth * 2 + 2),
43  qMax(minSize.height(), clearButton->sizeHint().height() + frameWidth * 2 + 2));
44 
45  connect(clearButton, SIGNAL(clicked()), this, SLOT(clear()));
46  connect(
47  this, SIGNAL(textChanged(const QString&)), this, SLOT(updateClearButton(const QString&)));
48 }
49 
50 void
52 {
53  QSize size = clearButton->sizeHint();
54  int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
55  clearButton->move(rect().right() - frameWidth - size.width(),
56  (rect().bottom() + 1 - size.height()) / 2);
57 }
58 
59 void
60 gui::ClearableLineEdit::updateClearButton(const QString& text)
61 {
62  clearButton->setVisible(!text.isEmpty());
63 }
64 
66 {
67 }
gui::ClearableLineEdit::~ClearableLineEdit
~ClearableLineEdit() override
Destructor.
Definition: ClearableLineEdit.cpp:65
ClearableLineEdit.h
gui::ClearableLineEdit::resizeEvent
void resizeEvent(QResizeEvent *) override
This event handler is implemented to receive widget resize events which are passed in the event param...
Definition: ClearableLineEdit.cpp:51
gui::ClearableLineEdit::ClearableLineEdit
ClearableLineEdit(QWidget *parent=0)
Constructor.
Definition: ClearableLineEdit.cpp:29