18 #include "SearchLineEdit.h"
19 #include <QToolButton>
24 SearchLineEdit::SearchLineEdit(QWidget *parent) :
28 this->mSearchButton =
new QToolButton(
this);
29 this->mSearchButton->setFixedSize(18, 18);
30 this->mSearchButton->setCursor(Qt::ArrowCursor);
31 this->mSearchButton->setStyleSheet(this->buttonStyleSheetForCurrentState());
34 QObject::connect(
this, SIGNAL(textChanged(QString)),
35 SLOT(updateSearchButton(QString)));
38 this->setPlaceholderText(tr(
"Search"));
39 this->setStyleSheet(this->styleSheetForCurrentState());
41 int frameWidth = this->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
42 QSize minSizeHint = this->minimumSizeHint();
44 qMax(minSizeHint.width(),
45 this->mSearchButton->sizeHint().width() + frameWidth * 2 + 2),
46 qMax(minSizeHint.height(),
47 this->mSearchButton->sizeHint().height() + frameWidth * 2 + 2));
50 void SearchLineEdit::resizeEvent(QResizeEvent *event)
53 QSize size = this->mSearchButton->sizeHint();
54 int frameWidth = this->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
55 this->mSearchButton->move(
56 this->rect().right() - frameWidth - size.width() - 2,
57 (this->rect().bottom() + 2 - size.height()) / 2);
60 void SearchLineEdit::updateSearchButton(
const QString &text)
65 QObject::connect(this->mSearchButton, SIGNAL(clicked()), SLOT(clear()));
70 QObject::disconnect(this->mSearchButton, SIGNAL(clicked()),
74 this->setStyleSheet(this->styleSheetForCurrentState());
75 this->mSearchButton->setStyleSheet(this->buttonStyleSheetForCurrentState());
78 QString SearchLineEdit::styleSheetForCurrentState()
const
80 int frameWidth = this->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
83 style +=
"QLineEdit {";
84 if (this->text().isEmpty())
86 style +=
"font-family: 'MS Sans Serif';";
87 style +=
"font-style: italic;";
88 style +=
"font-size: 12px;";
91 style +=
"padding-left: 3px;";
92 style += QString(
"padding-right: %1px;").arg(
93 this->mSearchButton->sizeHint().width() + frameWidth + 1);
95 style +=
"border-width: 3px;";
96 style +=
"border-image: url(:/images/esf-border.png) 3 3 3 3 stretch;";
97 style +=
"background-color: rgba(255, 255, 255, 204);";
99 style +=
"QLineEdit:hover, QLineEdit:focus {";
100 style +=
"background-color: rgba(255, 255, 255, 255);";
105 QString SearchLineEdit::buttonStyleSheetForCurrentState()
const
108 style +=
"QToolButton {";
109 style +=
"border: none; margin: 0; padding: 0;";
110 style += QString(
"background-image: url(:/images/esf-%1.png);").
111 arg(this->text().isEmpty() ?
"search" :
"clear");
115 if (!this->text().isEmpty())
117 style +=
"QToolButton:hover { "
118 "background-image: url(:/images/esf-clear-hover.png); "
120 style +=
"QToolButton:pressed { "
121 "background-image: url(:/images/esf-clear-active.png); "
128 void SearchLineEdit::keyPressEvent(QKeyEvent *event)
130 if(event->key() == Qt::Key_Escape)
134 else if(event->key() == Qt::Key_Return)
136 if(event->modifiers() & Qt::ShiftModifier)
138 emit shiftReturnPressed();
142 emit returnPressed();
147 QLineEdit::keyPressEvent(event);