keepassxc/src/gui/LineEdit.cpp

78 lines
2.8 KiB
C++
Raw Normal View History

2012-05-27 13:43:34 -04:00
/*
* Copyright (C) 2007 Trolltech ASA <info@trolltech.com>
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2012 Florian Geyer <blueice@fobos.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 or (at your option)
* version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "LineEdit.h"
#include <QStyle>
#include <QToolButton>
2012-07-18 14:44:28 -04:00
#include "core/FilePath.h"
LineEdit::LineEdit(QWidget* parent)
: QLineEdit(parent)
, m_clearButton(new QToolButton(this))
{
2012-05-20 17:14:34 -04:00
m_clearButton->setObjectName("clearButton");
2012-05-27 13:43:34 -04:00
QIcon icon;
QString iconNameDirected = QString("edit-clear-locationbar-").append(
(layoutDirection() == Qt::LeftToRight) ? "rtl" : "ltr");
icon = QIcon::fromTheme(iconNameDirected);
if (icon.isNull()) {
icon = QIcon::fromTheme("edit-clear");
if (icon.isNull()) {
2012-07-18 14:44:28 -04:00
icon = filePath()->icon("actions", iconNameDirected, false);
2012-05-27 13:43:34 -04:00
}
}
2012-05-20 17:14:34 -04:00
m_clearButton->setIcon(icon);
m_clearButton->setCursor(Qt::ArrowCursor);
m_clearButton->setStyleSheet("QToolButton { border: none; padding: 0px; }");
m_clearButton->hide();
connect(m_clearButton, SIGNAL(clicked()), this, SLOT(clear()));
2012-07-14 13:34:38 -04:00
connect(this, SIGNAL(textChanged(QString)), this, SLOT(updateCloseButton(QString)));
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
setStyleSheet(QString("QLineEdit { padding-right: %1px; } ")
2012-05-20 17:14:34 -04:00
.arg(m_clearButton->sizeHint().width() + frameWidth + 1));
QSize msz = minimumSizeHint();
2012-05-20 17:14:34 -04:00
setMinimumSize(qMax(msz.width(), m_clearButton->sizeHint().height() + frameWidth * 2 + 2),
qMax(msz.height(), m_clearButton->sizeHint().height() + frameWidth * 2 + 2));
}
2012-05-20 17:14:34 -04:00
void LineEdit::resizeEvent(QResizeEvent* event)
{
2012-05-20 17:14:34 -04:00
QSize sz = m_clearButton->sizeHint();
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
2012-05-27 13:43:34 -04:00
int y = (rect().bottom() + 1 - sz.height()) / 2;
if (layoutDirection() == Qt::LeftToRight) {
m_clearButton->move(rect().right() - frameWidth - sz.width(), y);
}
else {
m_clearButton->move(rect().left() + frameWidth, y);
}
QLineEdit::resizeEvent(event);
}
void LineEdit::updateCloseButton(const QString& text)
{
2012-05-20 17:14:34 -04:00
m_clearButton->setVisible(!text.isEmpty());
}