tuchulcha  0.10.1
Graphical Workflow Configuration Editor
QFilterWidget.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2013 Jens-Malte Gottfried
2 
3  This file is part of Tuchulcha.
4 
5  Tuchulcha is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  Tuchulcha is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with Tuchulcha. If not, see <http://www.gnu.org/licenses/>.
17 */
28 #include "QFilterWidget.h"
29 
30 #include <QPainter>
31 #include <QStyle>
32 #include <QPropertyAnimation>
33 
34 QT_BEGIN_NAMESPACE
35 
36 QIconButton::QIconButton(QWidget *pp) : QToolButton(pp) {
37  setCursor(Qt::ArrowCursor);
38 }
39 
40 void QIconButton::paintEvent(QPaintEvent*) {
41  QPainter painter(this);
42  // Note isDown should really use the active state but in most styles
43  // this has no proper feedback
44  QIcon::Mode state = QIcon::Disabled;
45  if (isEnabled())
46  state = isDown() ? QIcon::Selected : QIcon::Normal;
47  QPixmap iconpixmap = icon().pixmap(QSize(ICONBUTTON_SIZE, ICONBUTTON_SIZE),
48  state, QIcon::Off);
49  QRect pixmapRect = QRect(0, 0, iconpixmap.width(), iconpixmap.height());
50  pixmapRect.moveCenter(rect().center());
51  painter.setOpacity(m_fader);
52  painter.drawPixmap(pixmapRect, iconpixmap);
53 }
54 
55 void QIconButton::animateShow(bool visible)
56 {
57  if (visible) {
58  QPropertyAnimation *animation = new QPropertyAnimation(this, "fader");
59  animation->setDuration(160);
60  animation->setEndValue(1.0);
61  animation->start(QAbstractAnimation::DeleteWhenStopped);
62  } else {
63  QPropertyAnimation *animation = new QPropertyAnimation(this, "fader");
64  animation->setDuration(160);
65  animation->setEndValue(0.0);
66  animation->start(QAbstractAnimation::DeleteWhenStopped);
67  }
68 }
69 
70 // ------------------- FilterWidget
72  QLineEdit(pp),
73  _resetButton(new QIconButton(this))
74 {
75  // Let the style determine minimum height for our widget
76  QSize wsize(ICONBUTTON_SIZE + 6, ICONBUTTON_SIZE + 2);
77 
78  // Note KDE does not reserve space for the highlight color
79  if (style()->inherits("OxygenStyle")) {
80  wsize = wsize.expandedTo(QSize(24, 0));
81  }
82 
83  // Make room for clear icon
84  QMargins margins = textMargins();
85  if (layoutDirection() == Qt::LeftToRight)
86  margins.setRight(wsize.width());
87  else
88  margins.setLeft(wsize.width());
89 
90  setTextMargins(margins);
91 
92  // KDE has custom icons for this. Notice that icon namings are counter intuitive
93  // If these icons are not avaiable we use the freedesktop standard name before
94  // falling back to a bundled resource
95  QIcon icon = QIcon::fromTheme(layoutDirection() == Qt::LeftToRight ?
96  QLatin1String("edit-clear-locationbar-rtl") :
97  QLatin1String("edit-clear-locationbar-ltr"),
98  QIcon::fromTheme("edit-clear",
99  QIcon(":/icons/edit-clear-filter.png")));
100 
101  _resetButton->setIcon(icon);
102  _resetButton->setToolTip(tr("clear text"));
103  connect(_resetButton, SIGNAL(clicked()), this, SLOT(clear()));
104  connect(this, SIGNAL(textChanged(QString)), this, SLOT(checkButton(QString)));
105 }
106 
107 void QFilterWidget::checkButton(const QString &txt)
108 {
109  if (_oldText.isEmpty() || txt.isEmpty())
110  _resetButton->animateShow(!text().isEmpty());
111  _oldText = txt;
112 }
113 
114 void QFilterWidget::resizeEvent(QResizeEvent *)
115 {
116  QRect contentRect = rect();
117  if (layoutDirection() == Qt::LeftToRight) {
118  const int iconoffset = textMargins().right() + 4;
119  _resetButton->setGeometry(contentRect.adjusted(width() - iconoffset, 0, 0, 0));
120  } else {
121  const int iconoffset = textMargins().left() + 4;
122  _resetButton->setGeometry(contentRect.adjusted(0, 0, -width() + iconoffset, 0));
123  }
124 }
125 
126 QT_END_NAMESPACE
QIconButton(QWidget *parent)
default constructor
void animateShow(bool visible)
fade in or out
void resizeEvent(QResizeEvent *event)
handle resize
QString _oldText
text cache (for checkButton(QString))
Definition: QFilterWidget.h:81
void paintEvent(QPaintEvent *event)
handle fade parameter
This is a simple helper class that represents clickable icons.
Definition: QFilterWidget.h:42
#define ICONBUTTON_SIZE
size of the reset button icon
Definition: QFilterWidget.h:39
QIconButton * _resetButton
reset button
Definition: QFilterWidget.h:80
void checkButton(const QString &text)
check for fade in/out of the icon button
Declaration of class QFilterWidget.
QFilterWidget(QWidget *parent=0)
default constructor
float m_fader
fading state
Definition: QFilterWidget.h:61