NCL Composer  0.1.5
 All Classes Functions Variables Pages
LineEditWithButton.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 "LineEditWithButton.h"
19 #include <QToolButton>
20 #include <QStyle>
21 
22 LineEditWithButton::LineEditWithButton(QWidget *parent,
23  const QString &iconPath) :
24  QLineEdit(parent), iconPath(iconPath)
25 {
26  // Create the search button and set its icon, cursor, and stylesheet
27  this->mButton = new QToolButton(this);
28  // this->mButton->setIcon(QIcon(iconPath));
29 // this->mButton->setFixedSize(18, 18);
30  this->mButton->setText("...");
31  this->mButton->setCursor(Qt::ArrowCursor);
32  this->mButton->setStyleSheet(this->buttonStyleSheetForCurrentState());
33 
34  // Some stylesheet and size corrections for the text box
35  this->setStyleSheet(this->styleSheetForCurrentState());
36 
37 // int frameWidth = this->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
38 // QSize minSizeHint = this->minimumSizeHint();
39 // this->setMinimumSize(
40 // qMax(minSizeHint.width(),
41 // this->mButton->sizeHint().width() + frameWidth * 2 + 2),
42 // qMax(minSizeHint.height(),
43 // this->mButton->sizeHint().height() + frameWidth * 2 + 2));
44 
45  QObject::connect(this->mButton, SIGNAL(clicked()), SIGNAL(buttonPressed()));
46 }
47 
48 void LineEditWithButton::resizeEvent(QResizeEvent *event)
49 {
50  Q_UNUSED(event);
51  QSize size = this->mButton->sizeHint();
52  int frameWidth = this->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
53  this->mButton->move(
54  this->rect().right() - frameWidth - size.width() - 2,
55  (this->rect().bottom() + 2 - size.height()) / 2);
56 }
57 
58 QString LineEditWithButton::styleSheetForCurrentState() const
59 {
60  int frameWidth = this->style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
61 
62  QString style;
63  style += "QLineEdit {";
64  /* if (this->text().isEmpty())
65  { */
66 // style += "font-family: 'MS Sans Serif';";
67 // style += "font-style: italic;";
68  style += "font-size: 11px;";
69  // }
70 
71 // style += "padding-left: 3px;";
72  style += QString("padding-right: %1px;").arg(
73  this->mButton->sizeHint().width() + frameWidth + 1);
74  /*
75  style += "border-width: 3px;";
76  style += "border-image: url( " + iconPath + ") 3 3 3 3 stretch;";
77  style += "background-color: rgba(255, 255, 255, 204);";
78  style += "}";
79  style += "QLineEdit:hover, QLineEdit:focus {";
80  style += "background-color: rgba(255, 255, 255, 255);"; */
81  style += "}";
82  return style;
83 }
84 
85 QString LineEditWithButton::buttonStyleSheetForCurrentState() const
86 {
87  QString style;
88  /*style += "QToolButton {";
89  style += "border: none; margin: 0; padding: 0;";
90  style += QString("background-image: " + iconPath + ";");
91  arg(this->text().isEmpty() ? "search" : "clear");
92 
93  style += "}";
94 
95  if (!this->text().isEmpty())
96  {
97  style += "QToolButton:hover { "
98  "background-image: url(:/images/esf-clear-hover.png); "
99  "}";
100  style += "QToolButton:pressed { "
101  "background-image: url(:/images/esf-clear-active.png); "
102  "}";
103  }*/
104 
105  return style;
106 }