NCL Composer  0.1.5
 All Classes Functions Variables Pages
SearchLineEdit.cpp
1 /*
2  * Copyright 2011-2012 TeleMidia/PUC-Rio.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see
16  * <http://www.gnu.org/licenses/>.
17  */
18 #include "SearchLineEdit.h"
19 #include <QToolButton>
20 #include <QStyle>
21 
22 #include <QDebug>
23 
24 SearchLineEdit::SearchLineEdit(QWidget *parent) :
25  QLineEdit(parent)
26 {
27  // Create the search button and set its icon, cursor, and stylesheet
28  this->mSearchButton = new QToolButton(this);
29  this->mSearchButton->setFixedSize(18, 18);
30  this->mSearchButton->setCursor(Qt::ArrowCursor);
31  this->mSearchButton->setStyleSheet(this->buttonStyleSheetForCurrentState());
32 
33  // Update the search button when the text changes
34  QObject::connect(this, SIGNAL(textChanged(QString)),
35  SLOT(updateSearchButton(QString)));
36 
37  // Some stylesheet and size corrections for the text box
38  this->setPlaceholderText(tr("Search"));
39  this->setStyleSheet(this->styleSheetForCurrentState());
40 
41  int frameWidth = this->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
42  QSize minSizeHint = this->minimumSizeHint();
43  this->setMinimumSize(
44  qMax(minSizeHint.width(),
45  this->mSearchButton->sizeHint().width() + frameWidth * 2 + 2),
46  qMax(minSizeHint.height(),
47  this->mSearchButton->sizeHint().height() + frameWidth * 2 + 2));
48 }
49 
50 void SearchLineEdit::resizeEvent(QResizeEvent *event)
51 {
52  Q_UNUSED(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);
58 }
59 
60 void SearchLineEdit::updateSearchButton(const QString &text)
61 {
62  if (!text.isEmpty())
63  {
64  // We have some text in the box - set the button to clear the text
65  QObject::connect(this->mSearchButton, SIGNAL(clicked()), SLOT(clear()));
66  }
67  else
68  {
69  // The text box is empty - make the icon do nothing when clicked
70  QObject::disconnect(this->mSearchButton, SIGNAL(clicked()),
71  this, SLOT(clear()));
72  }
73 
74  this->setStyleSheet(this->styleSheetForCurrentState());
75  this->mSearchButton->setStyleSheet(this->buttonStyleSheetForCurrentState());
76 }
77 
78 QString SearchLineEdit::styleSheetForCurrentState() const
79 {
80  int frameWidth = this->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
81 
82  QString style;
83  style += "QLineEdit {";
84  if (this->text().isEmpty())
85  {
86  style += "font-family: 'MS Sans Serif';";
87  style += "font-style: italic;";
88  style += "font-size: 12px;";
89  }
90 
91  style += "padding-left: 3px;";
92  style += QString("padding-right: %1px;").arg(
93  this->mSearchButton->sizeHint().width() + frameWidth + 1);
94 
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);";
98  style += "}";
99  style += "QLineEdit:hover, QLineEdit:focus {";
100  style += "background-color: rgba(255, 255, 255, 255);";
101  style += "}";
102  return style;
103 }
104 
105 QString SearchLineEdit::buttonStyleSheetForCurrentState() const
106 {
107  QString style;
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");
112 
113  style += "}";
114 
115  if (!this->text().isEmpty())
116  {
117  style += "QToolButton:hover { "
118  "background-image: url(:/images/esf-clear-hover.png); "
119  "}";
120  style += "QToolButton:pressed { "
121  "background-image: url(:/images/esf-clear-active.png); "
122  "}";
123  }
124 
125  return style;
126 }
127 
128 void SearchLineEdit::keyPressEvent(QKeyEvent *event)
129 {
130  if(event->key() == Qt::Key_Escape)
131  {
132  emit escPressed();
133  }
134  else if(event->key() == Qt::Key_Return)
135  {
136  if(event->modifiers() & Qt::ShiftModifier)
137  {
138  emit shiftReturnPressed();
139  }
140  else
141  {
142  emit returnPressed();
143  }
144  }
145  else
146  {
147  QLineEdit::keyPressEvent(event);
148  }
149 }