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 <QStyle>
26 
27 #include "ClearableLineEdit.h"
28 
30  QLineEdit(parent),
31  clearButton(new QToolButton(this))
32 {
33  QIcon icon = QIcon::fromTheme("edit-clear", QIcon(":/icon/dialog-close.ico"));
34  clearButton->setIcon(icon);
35  clearButton->setCursor(Qt::ArrowCursor);
36  clearButton->setStyleSheet("QToolButton { border: none; padding: 0px; }");
37  clearButton->hide();
38 
39  int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
40  setStyleSheet(QString("QLineEdit { padding-right: %1px; } ").arg(clearButton->sizeHint().width() + frameWidth + 1));
41  QSize minSize = minimumSizeHint();
42  setMinimumSize(qMax(minSize.width(), clearButton->sizeHint().height() + frameWidth * 2 + 2), qMax(minSize.height(), clearButton->sizeHint().height() + frameWidth * 2 + 2));
43 
44  connect(clearButton, SIGNAL(clicked()), this, SLOT(clear()));
45  connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(updateClearButton(const QString&)));
46 }
47 
49 {
50  QSize size = clearButton->sizeHint();
51  int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
52  clearButton->move(rect().right() - frameWidth - size.width(),
53  (rect().bottom() + 1 - size.height()) / 2);
54 }
55 
56 void gui::ClearableLineEdit::updateClearButton(const QString& text)
57 {
58  clearButton->setVisible(!text.isEmpty());
59 }
60 
61 
63 {
64 }
gui::ClearableLineEdit::~ClearableLineEdit
~ClearableLineEdit() override
Destructor.
Definition: ClearableLineEdit.cpp:62
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:48
gui::ClearableLineEdit::ClearableLineEdit
ClearableLineEdit(QWidget *parent=0)
Constructor.
Definition: ClearableLineEdit.cpp:29