X-Git-Url: https://git.sur5r.net/?p=minitube;a=blobdiff_plain;f=src%2Fsearchlineedit.cpp;h=bbd2fe59e67bcec474aebb2e83722498380e2fda;hp=d31675275b7e3a1e4f8f1b84af4c73f1dc458c3a;hb=29f9a13b38a0547b70236d24300668385e1dbf6e;hpb=e5ab28f93dda6e878973f57276db5edab68d13bc diff --git a/src/searchlineedit.cpp b/src/searchlineedit.cpp index d316752..bbd2fe5 100644 --- a/src/searchlineedit.cpp +++ b/src/searchlineedit.cpp @@ -1,72 +1,17 @@ #include "searchlineedit.h" - -#include -#include -#include -#include -#include - #include "autocomplete.h" #include "iconutils.h" -ClearButton::ClearButton(QWidget *parent) : QAbstractButton(parent), hovered(false), mousePressed(false) { - setCursor(Qt::ArrowCursor); - setToolTip(tr("Clear")); - setVisible(false); - setFocusPolicy(Qt::NoFocus); -} - -void ClearButton::paintEvent(QPaintEvent *e) { - Q_UNUSED(e); - QPainter painter(this); - const int h = height(); - int iconSize = 16; - if (h > 30) iconSize = 22; - QIcon::Mode iconMode = QIcon::Normal; - if (mousePressed) iconMode = QIcon::Active; - QPixmap p = IconUtils::icon("edit-clear").pixmap(iconSize, iconSize, iconMode); - // QPixmap p = IconUtils::tintedIcon("edit-clear", Qt::black, QSize(iconSize, iconSize)).pixmap(iconSize, iconSize, iconMode); - int x = (width() - p.width()) / 2; - int y = (h - p.height()) / 2; - painter.drawPixmap(x, y, p); -} - -void ClearButton::textChanged(const QString &text) { - setVisible(!text.isEmpty()); -} - -void ClearButton::enterEvent(QEvent *e) { - Q_UNUSED(e); - hovered = true; -} - -void ClearButton::leaveEvent(QEvent *e) { - Q_UNUSED(e); - hovered = false; -} - -void ClearButton::mousePressEvent(QEvent *e) { - Q_UNUSED(e); - mousePressed = true; -} - -void ClearButton::mouseReleaseEvent(QEvent *e) { - Q_UNUSED(e); - mousePressed = false; -} - -/* - Search icon on the left hand side of the search widget - When a menu is set a down arrow appears - */ class SearchButton : public QAbstractButton { + public: SearchButton(QWidget *parent = 0); - void paintEvent(QPaintEvent *event); QMenu *m_menu; protected: - void mousePressEvent(QMouseEvent *event); + void paintEvent(QPaintEvent *e); + void mousePressEvent(QMouseEvent *e); + }; SearchButton::SearchButton(QWidget *parent) @@ -77,20 +22,20 @@ SearchButton::SearchButton(QWidget *parent) setFocusPolicy(Qt::NoFocus); } -void SearchButton::mousePressEvent(QMouseEvent *event) { - if (m_menu && event->button() == Qt::LeftButton) { +void SearchButton::mousePressEvent(QMouseEvent *e) { + if (m_menu && e->button() == Qt::LeftButton) { QWidget *p = parentWidget(); if (p) { QPoint r = p->mapToGlobal(QPoint(0, p->height())); m_menu->exec(QPoint(r.x() + height() / 2, r.y())); } - event->accept(); + e->accept(); } - QAbstractButton::mousePressEvent(event); + QAbstractButton::mousePressEvent(e); } -void SearchButton::paintEvent(QPaintEvent *event) { - Q_UNUSED(event); +void SearchButton::paintEvent(QPaintEvent *e) { + Q_UNUSED(e); QPainter painter(this); const int h = height(); int iconSize = 16; @@ -101,17 +46,10 @@ void SearchButton::paintEvent(QPaintEvent *event) { painter.drawPixmap(x, y, p); } -/* - SearchLineEdit is an enhanced QLineEdit - - A Search icon on the left with optional menu - - When there is no text and doesn't have focus an "inactive text" is displayed - - When there is text a clear button is displayed on the right hand side - */ -SearchLineEdit::SearchLineEdit(QWidget *parent) : ExLineEdit(parent), -searchButton(new SearchButton(this)) { - connect(lineEdit(), SIGNAL(textChanged(const QString &)), SIGNAL(textChanged(const QString &))); - connect(lineEdit(), SIGNAL(textEdited(const QString &)), SIGNAL(textEdited(const QString &))); - connect(lineEdit(), SIGNAL(returnPressed()), SLOT(returnPressed())); +SearchLineEdit::SearchLineEdit(QWidget *parent) : ExLineEdit(parent), searchButton(new SearchButton(this)) { + connect(m_lineEdit, SIGNAL(textChanged(const QString &)), SIGNAL(textChanged(const QString &))); + connect(m_lineEdit, SIGNAL(textEdited(const QString &)), SIGNAL(textEdited(const QString &))); + connect(m_lineEdit, SIGNAL(returnPressed()), SLOT(returnPressed())); setLeftWidget(searchButton); inactiveText = tr("Search"); @@ -124,27 +62,25 @@ searchButton(new SearchButton(this)) { connect(autoComplete, SIGNAL(suggestionAccepted(Suggestion*)), SIGNAL(suggestionAccepted(Suggestion*))); } -void SearchLineEdit::paintEvent(QPaintEvent *event) { - if (lineEdit()->text().isEmpty() && !hasFocus() && !inactiveText.isEmpty()) { - ExLineEdit::paintEvent(event); +void SearchLineEdit::paintEvent(QPaintEvent *e) { + ExLineEdit::paintEvent(e); + if (m_lineEdit->text().isEmpty() && !hasFocus() && !inactiveText.isEmpty()) { QStyleOptionFrameV2 panel; initStyleOption(&panel); QRect r = style()->subElementRect(QStyle::SE_LineEditContents, &panel, this); QFontMetrics fm = fontMetrics(); - int horizontalMargin = lineEdit()->x(); + int horizontalMargin = m_lineEdit->x(); QRect lineRect(horizontalMargin + r.x(), r.y() + (r.height() - fm.height() + 1) / 2, r.width() - 2 * horizontalMargin, fm.height()); QPainter painter(this); painter.setPen(palette().brush(QPalette::Disabled, QPalette::Text).color()); - painter.drawText(lineRect, Qt::AlignLeft|Qt::AlignVCenter, inactiveText); - } else { - ExLineEdit::paintEvent(event); + painter.drawText(lineRect, Qt::AlignLeft | Qt::AlignVCenter, inactiveText); } } -void SearchLineEdit::resizeEvent(QResizeEvent *event) { +void SearchLineEdit::resizeEvent(QResizeEvent *e) { updateGeometries(); - ExLineEdit::resizeEvent(event); + ExLineEdit::resizeEvent(e); } void SearchLineEdit::updateGeometries() { @@ -159,6 +95,14 @@ void SearchLineEdit::setInactiveText(const QString &text) { inactiveText = text; } +void SearchLineEdit::setText(const QString &text) { + m_lineEdit->setText(text); +} + +AutoComplete *SearchLineEdit::getAutoComplete() { + return autoComplete; +} + void SearchLineEdit::setMenu(QMenu *menu) { if (searchButton->m_menu) searchButton->m_menu->deleteLater(); @@ -176,9 +120,10 @@ QMenu *SearchLineEdit::menu() const { } void SearchLineEdit::returnPressed() { - if (!lineEdit()->text().isEmpty()) { + QString text = m_lineEdit->text().simplified(); + if (!text.isEmpty()) { autoComplete->preventSuggest(); - emit search(lineEdit()->text()); + emit search(text); } } @@ -190,7 +135,33 @@ void SearchLineEdit::preventSuggest() { autoComplete->preventSuggest(); } -void SearchLineEdit::focusInEvent(QFocusEvent *event) { - ExLineEdit::focusInEvent(event); +void SearchLineEdit::selectAll() { + m_lineEdit->selectAll(); +} + +void SearchLineEdit::setSuggester(Suggester *suggester) { + autoComplete->setSuggester(suggester); +} + +void SearchLineEdit::focusInEvent(QFocusEvent *e) { + ExLineEdit::focusInEvent(e); enableSuggest(); } + +void SearchLineEdit::emitTextChanged(const QString &text) { + autoComplete->enableSuggest(); + emit textEdited(text); +} + +QString SearchLineEdit::text() { + return m_lineEdit->text(); +} + +QLineEdit *SearchLineEdit::getLineEdit() { + return m_lineEdit; +} + +void SearchLineEdit::setEnabled(bool enabled) { + ExLineEdit::setEnabled(enabled); + emit enabledChanged(enabled); +}