1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
/*
This file is part of EqualX, based on LineEdit from Martin Rotter http://www.martin-rotter.8u.cz/2013/03/qlineedit-subclass-with-clear-button-in-english/
EqualX 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 3 of the License, or
(at your option) any later version.
EqualX 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 EqualX. If not, see <http://www.gnu.org/licenses/>.
Copyright 2012 - 2013 Martin Rotter
Copyright 2014 Mihai Niculescu
*/
/****************************************************************************
**
** Copyright (c) 2007 Trolltech ASA <info@trolltech.com>
**
** Use, modification and distribution is allowed without limitation,
** warranty, liability or support of any kind.
**
****************************************************************************/
#include <QStyle>
#include <QKeyEvent>
#include <QPushButton>
#include <QCompleter>
#include <QDebug>
#include "SearchLineEdit.h"
SearchLineEdit::SearchLineEdit(QWidget *parent)
: QLineEdit(parent)
{
mClearButton = new QToolButton(this);
int frame_width = frameWidth();
mClearButton->setIcon(QIcon::fromTheme("edit-clear",
QIcon(":/resources/icons/menu/clear_left.png")));
int SH = sizeHint().height() - 4 * frame_width;
mClearButton->setIconSize(QSize( SH,SH));
mClearButton->setCursor(Qt::ArrowCursor);
mClearButton->setStyleSheet("QToolButton { border: none; padding: 0px; }");
mClearButton->setToolTip(tr("Clear"));
mClearButton->hide();
mClearButtonEnabled = true;
// Create necessary connections.
connect(mClearButton, SIGNAL(clicked()), this, SLOT(clear()) );
connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(onTextChanged(const QString&)) );
// Add extra padding to the right of the line edit. It looks better.
setStyleSheet(QString("QLineEdit { padding-right: %1px; }").arg(mClearButton->sizeHint().width() + frame_width + 1));
QSize min_size_hint = minimumSizeHint();
setMinimumSize(qMax(min_size_hint.width(),
mClearButton->sizeHint().height() + frame_width),
qMax(min_size_hint.height(),
mClearButton->sizeHint().height() + frame_width));
setContextMenuPolicy(Qt::NoContextMenu);
mCompleter = new QCompleter(this);
}
SearchLineEdit::~SearchLineEdit() {
delete mClearButton;
}
void SearchLineEdit::onTextChanged(const QString &new_text) {
// If line edit is not read only (or not enabled) and clear button
// is enabled, then make sure it's displayed.
if (isReadOnly() == false && isEnabled() == true && mClearButtonEnabled == true) {
mClearButton->setVisible(new_text.isEmpty() == false);
}
}
void SearchLineEdit::setClearButtonEnabled(bool enable) {
mClearButtonEnabled = enable;
}
void SearchLineEdit::setEnabled(bool enable) {
QLineEdit::setEnabled(enable);
onTextChanged(text());
}
void SearchLineEdit::setReadOnly(bool read_only) {
QLineEdit::setReadOnly(read_only);
onTextChanged(text());
}
int SearchLineEdit::frameWidth() const {
return style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, this);
}
void SearchLineEdit::resizeEvent(QResizeEvent *event)
{
Q_UNUSED(event);
// Place clear button correctly, according to size of line edit.
QSize sz = mClearButton->sizeHint();
mClearButton->move(rect().right() - sz.width(),
rect().bottom() - sz.height() );
QLineEdit::resizeEvent(event);
}
|