• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • workspace API Reference
  • KDE Home
  • Contact Us
 

liblancelot

  • sources
  • kde-4.14
  • workspace
  • kdeplasma-addons
  • libs
  • lancelot
  • widgets
Widget.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007, 2008, 2009, 2010 Ivan Cukic <ivan.cukic(at)kde.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser/Library General Public License version 2,
6  * or (at your option) any later version, as published by the Free
7  * Software Foundation
8  *
9  * This program 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
12  * GNU Lesser/Library General Public License for more details
13  *
14  * You should have received a copy of the GNU Lesser/Library General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include "Widget.h"
21 #include "Global.h"
22 
23 #include <QApplication>
24 #include <QVariant>
25 #include <QtGui/QGraphicsSceneMouseEvent>
26 #include <QtGui/QPainter>
27 
28 #include <lancelot/lancelot.h>
29 
30 namespace Lancelot {
31 
32 class Widget::Private {
33 public:
34  Private()
35  : hover(false),
36  down(false),
37  paintBackwards(false)
38  {
39  };
40 
41  bool hover : 1;
42  bool down : 1;
43  bool paintBackwards : 1;
44 };
45 
46 Widget::Widget(QGraphicsItem * parent)
47  : QGraphicsWidget(parent),
48  d(new Private())
49 {
50  setGroup(NULL);
51  setFlag(QGraphicsItem::ItemClipsToShape);
52 }
53 
54 Widget::~Widget()
55 {
56  delete d;
57 }
58 
59 void Widget::hoverEnterEvent(QGraphicsSceneHoverEvent * event)
60 {
61  if (d->hover) {
62  return ;
63  }
64  if (!isEnabled()) return;
65  d->hover = true;
66  QGraphicsWidget::hoverEnterEvent(event);
67  emit mouseHoverEnter();
68  update();
69 }
70 
71 void Widget::hoverLeaveEvent(QGraphicsSceneHoverEvent * event)
72 {
73  if (!(d->hover)) {
74  return ;
75  }
76  if (!isEnabled()) return;
77  d->hover = false;
78  QGraphicsWidget::hoverLeaveEvent(event);
79  emit mouseHoverLeave();
80  update();
81 }
82 
83 void Widget::setHovered(bool value)
84 {
85  if (d->hover == value) {
86  return ;
87  }
88  d->hover = value;
89  update();
90 }
91 
92 void Widget::setDown(bool value)
93 {
94  d->down = value;
95  update();
96 }
97 
98 void Widget::mousePressEvent(QGraphicsSceneMouseEvent * event)
99 {
100  if (event->button() == Qt::LeftButton) {
101  d->down = true;
102  update();
103  event->accept();
104  emit pressed();
105  } else {
106  QGraphicsWidget::mousePressEvent(event);
107  }
108 }
109 
110 void Widget::mouseReleaseEvent(QGraphicsSceneMouseEvent * event)
111 {
112  if (d->down && (event->button() == Qt::LeftButton)) {
113  d->down = false;
114  emit released();
115  emit clicked();
116  } else {
117  QGraphicsWidget::mouseReleaseEvent(event);
118  }
119 }
120 
121 bool Widget::isHovered() const
122 {
123  return d->hover;
124 }
125 
126 QString Widget::groupName() const
127 {
128  if (group()) {
129  return group()->name();
130  }
131  return QString();
132 }
133 
134 void Widget::setGroupByName(const QString & groupName)
135 {
136  setGroup(Global::self()->group(groupName));
137 }
138 
139 void Widget::setGroup(Group * group)
140 {
141  Global::self()->setGroupForObject(this, group);
142 }
143 
144 Group * Widget::group() const
145 {
146  return Global::self()->groupForObject(
147  const_cast < Widget * > (this));
148 }
149 
150 void Widget::paint(QPainter * painter, const QStyleOptionGraphicsItem * option,
151  QWidget * widget) {
152  Q_UNUSED(option);
153  Q_UNUSED(widget);
154 
155  paintBackground(painter);
156 }
157 
158 void Widget::paintBackground(QPainter * painter)
159 {
160  if (!group()) return;
161 
162  QString element;
163  if (!isEnabled()) {
164  element = "disabled";
165  } else if (d->down) {
166  element = "down";
167  } else if (d->hover) {
168  element = "active";
169  }
170 
171  paintBackground(painter, element);
172 }
173 
174 void Widget::paintBackground(QPainter * painter, const QString & element)
175 {
176  if (!group()) return;
177  bool rtl = QApplication::isRightToLeft();
178 
179  // Svg Background Painting
180  if (Plasma::FrameSvg * svg = group()->backgroundSvg()) {
181  svg->setElementPrefix(element);
182  if (svg->size() != size().toSize()) {
183  svg->resizeFrame(size().toSize());
184  }
185 
186  if (rtl && d->paintBackwards) {
187  painter->translate(size().width(), 0);
188  painter->scale(-1, 1);
189  }
190  svg->paintFrame(painter);
191  if (rtl && d->paintBackwards) {
192  painter->translate(size().width(), 0);
193  painter->scale(-1, 1);
194  }
195 
196  return;
197  }
198 
199  // If we don't have SVG, should we paint a rectangle?
200  if (!group()->hasProperty("WholeColorBackground")) {
201  return;
202  }
203 
204  if (const Group::ColorScheme * scheme = group()->backgroundColor()) {
205  const QColor * color;
206  if (!isEnabled()) {
207  color = & (scheme->disabled);
208  } else if (d->hover) {
209  color = & (scheme->active);
210  } else {
211  color = & (scheme->normal);
212  }
213  painter->fillRect(QRectF(QPointF(0, 0), size()), QBrush(*color));
214  }
215 }
216 
217 QSizeF Widget::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const
218 {
219  QSizeF result;
220  if (!group()) return result;
221 
222  switch (which) {
223  case Qt::MinimumSize:
224  result = QSizeF();
225  break;
226  case Qt::MaximumSize:
227  result = MAX_WIDGET_SIZE;
228  break;
229  case Qt::PreferredSize:
230  if (Plasma::FrameSvg * svg = group()->backgroundSvg()) {
231  result = QSizeF(
232  svg->marginSize(Plasma::LeftMargin) +
233  svg->marginSize(Plasma::RightMargin),
234  svg->marginSize(Plasma::TopMargin) +
235  svg->marginSize(Plasma::BottomMargin)
236  );
237  } else {
238  result = QSizeF();
239  }
240  break;
241  default:
242  result = QSizeF();
243  break;
244  }
245  if (constraint.isValid()) {
246  result = result.boundedTo(constraint);
247  }
248  return result;
249 }
250 
251 bool Widget::isDown() const
252 {
253  return d->down;
254 }
255 
256 void Widget::hideEvent(QHideEvent * event)
257 {
258  d->down = false;
259  d->hover = false;
260  QGraphicsWidget::hideEvent(event);
261 }
262 
263 void Widget::setPaintBackwardsWhenRTL(bool value)
264 {
265  d->paintBackwards = value;
266 }
267 
268 
269 } // namespace Lancelot
270 
271 #include "Widget.moc"
272 
QHideEvent
Lancelot::Widget::group
Group * group() const
Returns this widget's group.
QGraphicsItem::mouseReleaseEvent
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
Lancelot::Widget::setGroupByName
virtual void setGroupByName(const QString &groupName)
Sets this widget's group by group name.
Definition: Widget.cpp:134
Lancelot::Widget::isHovered
bool isHovered() const
Returns whether the mouse cursor is hovering the widget.
Definition: Widget.cpp:121
Lancelot::Widget::mouseHoverEnter
void mouseHoverEnter()
This signal is emitted when the mouse cursor enters the widget.
Lancelot::Widget::mouseReleaseEvent
L_Override void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
Definition: Widget.cpp:110
QWidget
QApplication::isRightToLeft
bool isRightToLeft()
QGraphicsItem::hoverEnterEvent
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
QPainter::fillRect
void fillRect(const QRectF &rectangle, const QBrush &brush)
Lancelot::Widget::clicked
void clicked()
Emitted when the button is clicked.
Lancelot::Widget::mousePressEvent
L_Override void mousePressEvent(QGraphicsSceneMouseEvent *event)
Definition: Widget.cpp:98
QGraphicsItem::setFlag
void setFlag(GraphicsItemFlag flag, bool enabled)
Lancelot::Widget::~Widget
virtual ~Widget()
Destroys Lancelot::Widget.
Definition: Widget.cpp:54
Lancelot::Global::self
static Global * self()
Definition: Global.cpp:307
Lancelot::Global::groupForObject
Group * groupForObject(QObject *object)
Definition: Global.cpp:463
QPainter::scale
void scale(qreal sx, qreal sy)
Lancelot::Widget::setPaintBackwardsWhenRTL
void setPaintBackwardsWhenRTL(bool value)
Definition: Widget.cpp:263
Lancelot::Global::setGroupForObject
void setGroupForObject(QObject *object, Group *group)
Sets the group for the object.
Definition: Global.cpp:473
QSizeF::isValid
bool isValid() const
Lancelot::Widget::setGroup
virtual void setGroup(Group *group=NULL)
Sets this widget's group.
Definition: Widget.cpp:139
Lancelot::Widget::paint
L_Override void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
Definition: Widget.cpp:150
QBrush
QGraphicsItem
Lancelot::Widget::groupName
QString groupName() const
Definition: Widget.cpp:126
MAX_WIDGET_SIZE
#define MAX_WIDGET_SIZE
Definition: lancelot.h:45
QGraphicsItem::update
void update(const QRectF &rect)
QPointF
QGraphicsWidget::size
QSizeF size() const
Lancelot::Widget::setHovered
void setHovered(bool value)
Sets whether the widget is hovered.
Definition: Widget.cpp:83
Lancelot::Widget::released
void released()
This signal is emitted when the button is released.
QGraphicsSceneMouseEvent
QSizeF::toSize
QSize toSize() const
QPainter
Lancelot::Group
Represents a group of object.
Definition: Global.h:63
Lancelot::Widget::mouseHoverLeave
void mouseHoverLeave()
This signal is emitted when the mouse cursor leaves the widget.
QGraphicsItem::mousePressEvent
virtual void mousePressEvent(QGraphicsSceneMouseEvent *event)
QGraphicsWidget::hideEvent
virtual void hideEvent(QHideEvent *event)
Lancelot::Widget::paintBackground
void paintBackground(QPainter *painter)
Paints the widget background using the widget's SVG.
Definition: Widget.cpp:158
QGraphicsWidget
QGraphicsSceneMouseEvent::button
Qt::MouseButton button() const
Global.h
QString
QColor
QSizeF::boundedTo
QSizeF boundedTo(const QSizeF &otherSize) const
QGraphicsSceneHoverEvent
Lancelot::Group::name
QString name() const
Definition: Global.cpp:194
Lancelot::Widget::Widget
Widget(QGraphicsItem *parent=0)
Creates a new Lancelot::Widget.
Definition: Widget.cpp:46
Lancelot::Widget::down
bool down
Definition: Widget.h:41
Widget.h
QGraphicsItem::isEnabled
bool isEnabled() const
Lancelot::Widget::hideEvent
L_Override void hideEvent(QHideEvent *event)
Definition: Widget.cpp:256
QSizeF
QRectF
Lancelot::Widget::hoverEnterEvent
L_Override void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
Definition: Widget.cpp:59
Lancelot::Widget::isDown
bool isDown() const
Definition: Widget.cpp:251
Lancelot::Widget::hoverLeaveEvent
L_Override void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
Definition: Widget.cpp:71
Lancelot::Group::ColorScheme
This class contains the triplet od colors for three standard object states - normal, disabled and active.
Definition: Global.h:71
lancelot.h
Lancelot::Widget::sizeHint
L_Override QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint=QSizeF()) const
Definition: Widget.cpp:217
QPainter::translate
void translate(const QPointF &offset)
QStyleOptionGraphicsItem
Lancelot::Widget::pressed
void pressed()
This signal is emitted when the button is pressed down.
QGraphicsWidget::hoverLeaveEvent
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
Lancelot::Widget::setDown
void setDown(bool value)
Sets whether the widget is down (pressed).
Definition: Widget.cpp:92
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:43:01 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

liblancelot

Skip menu "liblancelot"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

workspace API Reference

Skip menu "workspace API Reference"
  • kdeplasma-addons
  •       GroupingDesktop
  •     liblancelot

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal