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

GroupingDesktop

  • sources
  • kde-4.14
  • workspace
  • kdeplasma-addons
  • containments
  • groupingdesktop
  • lib
gridhandle.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2010 by Giulio Camuffo <giuliocamuffo@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Library General Public License as
6  * published by the Free Software Foundation; either version 2, or
7  * (at your option) any later version.
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 General Public License for more details
13  *
14  * You should have received a copy of the GNU 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 "gridhandle.h"
21 
22 #include <QtGui/QPainter>
23 #include <QtGui/QGraphicsScene>
24 #include <QtGui/QGraphicsSceneMouseEvent>
25 
26 #include <KDebug>
27 #include <KIconLoader>
28 
29 #include <Plasma/Applet>
30 #include <Plasma/PaintUtils>
31 
32 #include "groupingcontainment.h"
33 #include "abstractgroup.h"
34 
35 GridHandle::GridHandle(GroupingContainment *containment, Plasma::Applet *applet)
36  : Handle(containment, applet),
37  m_moving(false),
38  SIZE(20)
39 {
40  init();
41 }
42 
43 GridHandle::GridHandle(GroupingContainment *containment, AbstractGroup *group)
44  : Handle(containment, group),
45  m_moving(false),
46  SIZE(20)
47 {
48  init();
49 }
50 
51 GridHandle::~GridHandle()
52 {
53 
54 }
55 
56 void GridHandle::init()
57 {
58  m_location = NoLocation;
59 
60  QGraphicsWidget *w = widget();
61  m_widgetPos = w->pos();
62  m_widgetSize = w->size();
63  w->installEventFilter(this);
64 
65  m_lastButton = Handle::NoButton;
66 
67  m_configureIcons = new Plasma::Svg(this);
68  m_configureIcons->setImagePath("widgets/configuration-icons");
69  m_configureIcons->setContainsMultipleImages(true);
70 }
71 
72 void GridHandle::detachWidget()
73 {
74  QGraphicsWidget *w = widget();
75  w->removeEventFilter(this);
76  w->setPos(m_widgetPos);
77 
78  Handle::detachWidget();
79 }
80 
81 bool GridHandle::eventFilter(QObject *obj, QEvent *event)
82 {
83  QGraphicsWidget *w = qobject_cast<QGraphicsWidget *>(obj);
84 
85  if (w != widget()) {
86  return true;
87  }
88 
89  if (event->type() == QEvent::GraphicsSceneHoverLeave) {
90  if (!fullRect().contains(static_cast<QGraphicsSceneHoverEvent *>(event)->pos())) {
91  emit disappearDone(this);
92  }
93  } else if (event->type() == QEvent::GraphicsSceneMove) {
94  QGraphicsSceneMoveEvent *e = static_cast<QGraphicsSceneMoveEvent *>(event);
95  m_widgetPos = e->newPos();
96  }
97 
98  return false;
99 }
100 
101 QRectF GridHandle::boundingRect() const
102 {
103  switch (m_location) {
104  case Left:
105  return QRectF(-SIZE, 0, SIZE, m_widgetSize.height());
106 
107  case Top:
108  return QRectF(0, -SIZE, m_widgetSize.width(), SIZE);
109 
110  case Right:
111  return QRectF(m_widgetSize.width(), 0, SIZE, m_widgetSize.height());
112 
113  case Bottom:
114  return QRectF(0, m_widgetSize.height(), m_widgetSize.width(), SIZE);
115 
116  default:
117  return QRectF();
118  }
119 }
120 
121 void GridHandle::setHoverPos(const QPointF &hoverPos)
122 {
123  if (isHorizontal()) {
124  const qreal width = m_widgetSize.width() / 2.;
125  QRectF left(0, 0, width, m_widgetSize.height());
126  QRectF right(width, 0, width, m_widgetSize.height());
127  if (left.contains(hoverPos)) {
128  m_location = Left;
129  } else if (right.contains(hoverPos)) {
130  m_location = Right;
131  }
132  } else {
133  const qreal height = m_widgetSize.height() / 2.;
134  QRectF top(0, 0, m_widgetSize.width(), height);
135  QRectF bottom(0, height, m_widgetSize.width(), height);
136  if (top.contains(hoverPos)) {
137  m_location = Top;
138  } else if (bottom.contains(hoverPos)) {
139  m_location = Bottom;
140  }
141  }
142 
143  widget()->removeEventFilter(this);
144  switch (m_location) {
145  case Left:
146  widget()->setPos(m_widgetPos + QPointF(SIZE, 0));
147  break;
148 
149  case Top:
150  widget()->setPos(m_widgetPos + QPointF(0, SIZE));
151  break;
152 
153  case Right:
154  widget()->setPos(m_widgetPos + QPointF(-SIZE, 0));
155  break;
156 
157  case Bottom:
158  widget()->setPos(m_widgetPos + QPointF(0, -SIZE));
159  break;
160 
161  default:
162  emit disappearDone(this);
163  return;
164  }
165  widget()->installEventFilter(this);
166 }
167 
168 bool GridHandle::isHorizontal() const
169 {
170  QRectF rect(widget()->parentItem()->boundingRect());
171  return rect.width() >= rect.height();
172 }
173 
174 void GridHandle::widgetResized()
175 {
176  m_widgetSize = widget()->size();
177 }
178 
179 void GridHandle::mousePressEvent(QGraphicsSceneMouseEvent *event)
180 {
181  if (event->button() == Qt::LeftButton) {
182  m_lastButton = mapToButton(event->pos());
183  if (m_lastButton == Handle::MoveButton) {
184  m_startPos = event->pos();
185  }
186  event->accept();
187  update();
188  } else {
189  event->ignore();
190  }
191 }
192 
193 void GridHandle::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
194 {
195  if (m_lastButton == Handle::MoveButton) {
196  if (!m_moving) {
197  m_moving = true;
198  containment()->setMovingWidget(widget());
199  } else {
200  QPointF p(event->pos() - m_startPos);
201  widget()->moveBy(p.x(), p.y());
202  }
203  event->accept();
204  } else {
205  event->ignore();
206  }
207 }
208 
209 void GridHandle::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
210 {
211  if (event->button() != Qt::LeftButton) {
212  m_lastButton = NoButton;
213  event->ignore();
214  return;
215  }
216 
217  Handle::ButtonType button = mapToButton(event->pos());
218  if (button != m_lastButton) {
219  m_lastButton = NoButton;
220  event->ignore();
221  update();
222  return;
223  }
224 
225  switch (button) {
226  case MoveButton:
227  if (m_moving) {
228  m_moving = false;
229  emit widgetMoved(widget());
230  m_widgetPos = widget()->pos();
231  }
232 
233  break;
234 
235  case RemoveButton:
236  if (applet()) {
237  applet()->destroy();
238  } else {
239  group()->destroy();
240  }
241 
242  break;
243 
244  case ConfigureButton:
245  if (applet()) {
246  applet()->showConfigurationInterface();
247  } else {
248  group()->showConfigurationInterface();
249  }
250 
251  break;
252 
253  case MaximizeButton:
254  if (applet()) {
255  applet()->runAssociatedApplication();
256  }
257 
258  break;
259 
260  default:
261  break;
262  }
263 
264  update();
265 }
266 
267 QRectF GridHandle::fullRect() const
268 {
269  return QRectF(boundingRect().topLeft(), widget()->boundingRect().bottomRight());
270 }
271 
272 void GridHandle::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
273 {
274  if (!fullRect().contains(event->pos())) {
275  emit disappearDone(this);
276  }
277 }
278 
279 Handle::ButtonType GridHandle::mapToButton(const QPointF &pos)
280 {
281  if (enoughRoom()) {
282  const QSize iconSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall);
283  QRectF iconRect(boundingRect().topLeft() + QPointF(2, 2), iconSize);
284 
285  if ((applet() && applet()->hasConfigurationInterface()) || (group() && group()->hasConfigurationInterface())) {
286  if (iconRect.contains(pos)) {
287  return Handle::ConfigureButton;
288  }
289 
290  if (isHorizontal()) {
291  iconRect.translate(0, iconSize.height());
292  } else {
293  iconRect.translate(iconSize.width(), 0);
294  }
295  }
296 
297  if (applet() && applet()->hasValidAssociatedApplication()) {
298  if (iconRect.contains(pos)) {
299  return Handle::MaximizeButton;
300  }
301  }
302 
303  if (isHorizontal()) {
304  iconRect.moveTo(boundingRect().bottomLeft() - QPointF(-2, iconSize.height() + 2));
305  } else {
306  iconRect.moveTo(boundingRect().topRight() - QPointF(iconSize.width() + 2, -2));
307  }
308 
309  if (iconRect.contains(pos)) {
310  return Handle::RemoveButton;
311  }
312  }
313 
314  return Handle::MoveButton;
315 }
316 
317 bool GridHandle::enoughRoom()
318 {
319  const QSize iconSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall);
320  int buttonsCount = 2;
321 
322  if ((applet() && applet()->hasConfigurationInterface()) || (group() && group()->hasConfigurationInterface())) {
323  ++buttonsCount;
324  }
325 
326  if (applet() && applet()->hasValidAssociatedApplication()) {
327  ++buttonsCount;
328  }
329 
330  return ((isHorizontal() && boundingRect().height() > buttonsCount * iconSize.height()) ||
331  (!isHorizontal() && boundingRect().width() > buttonsCount * iconSize.width()));
332 }
333 
334 void GridHandle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
335 {
336  Q_UNUSED(option)
337 
338  QRectF rect = boundingRect();
339 
340  painter->setPen(Qt::NoPen);
341  painter->setRenderHints(QPainter::Antialiasing|QPainter::SmoothPixmapTransform);
342 
343  QPainterPath p = Plasma::PaintUtils::roundedRectangle(rect.adjusted(1, 1, -2, -2), 4);
344  QColor c = Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor);
345  c.setAlphaF(0.3);
346 
347  painter->fillPath(p, c);
348 
349  if (!enoughRoom()) {
350  return;
351  }
352 
353  QPointF shiftC;
354  QPointF shiftD;
355  QPointF shiftMx;
356 
357  switch(m_lastButton)
358  {
359  case ConfigureButton:
360  shiftC = QPointF(2, 2);
361  break;
362  case RemoveButton:
363  shiftD = QPointF(2, 2);
364  break;
365  case MaximizeButton:
366  shiftMx = QPointF(2, 2);
367  break;
368  default:
369  break;
370  }
371 
372  const QSize iconSize(KIconLoader::SizeSmall, KIconLoader::SizeSmall);
373  QRectF iconRect(boundingRect().topLeft() + QPointF(2, 2), iconSize);
374 
375  if ((applet() && applet()->hasConfigurationInterface()) || (group() && group()->hasConfigurationInterface())) {
376  m_configureIcons->paint(painter, iconRect.translated(shiftC), "configure");
377  if (isHorizontal()) {
378  iconRect.translate(0, iconSize.height());
379  } else {
380  iconRect.translate(iconSize.width(), 0);
381  }
382  }
383 
384  if (applet() && applet()->hasValidAssociatedApplication()) {
385  m_configureIcons->paint(painter, iconRect.translated(shiftMx), "maximize");
386  }
387 
388  if (isHorizontal()) {
389  iconRect.moveTo(boundingRect().bottomLeft() - QPointF(-2, iconSize.height() + 2));
390  } else {
391  iconRect.moveTo(boundingRect().topRight() - QPointF(iconSize.width() + 2, -2));
392  }
393  m_configureIcons->paint(painter, iconRect.translated(shiftD), "close");
394 }
395 
396 #include "gridhandle.moc"
GridHandle::eventFilter
bool eventFilter(QObject *obj, QEvent *event)
Definition: gridhandle.cpp:81
GridHandle::GridHandle
GridHandle(GroupingContainment *containment, Plasma::Applet *applet)
Definition: gridhandle.cpp:35
QEvent
QWidget
Handle::disappearDone
void disappearDone(Handle *self)
QEvent::type
Type type() const
groupingcontainment.h
QSize::width
int width() const
QGraphicsObject::pos
pos
AbstractGroup::showConfigurationInterface
virtual void showConfigurationInterface()
Lets the user interact with the Group options.
Definition: abstractgroup.cpp:775
QGraphicsItem::moveBy
void moveBy(qreal dx, qreal dy)
GridHandle::mouseMoveEvent
void mouseMoveEvent(QGraphicsSceneMouseEvent *event)
Definition: gridhandle.cpp:193
Handle::detachWidget
virtual void detachWidget()
Definition: handle.cpp:105
Handle::ButtonType
ButtonType
Definition: handle.h:46
QGraphicsSceneMoveEvent::newPos
QPointF newPos() const
Handle
Definition: handle.h:41
GridHandle::init
void init()
Definition: gridhandle.cpp:56
Handle::RemoveButton
Definition: handle.h:51
GridHandle::setHoverPos
void setHoverPos(const QPointF &hoverPos)
Definition: gridhandle.cpp:121
abstractgroup.h
gridhandle.h
GroupingContainment::setMovingWidget
void setMovingWidget(QGraphicsWidget *widget)
Call this function when an Applet or a Group is being moved by the user.
Definition: groupingcontainment.cpp:969
QGraphicsItem::update
void update(const QRectF &rect)
Handle::containment
GroupingContainment * containment() const
Definition: handle.cpp:95
QPointF
QObject::event
virtual bool event(QEvent *e)
Handle::applet
Plasma::Applet * applet() const
Definition: handle.cpp:85
QGraphicsWidget::size
size
QGraphicsSceneHoverEvent::pos
QPointF pos() const
Handle::MaximizeButton
Definition: handle.h:53
QGraphicsItem::pos
QPointF pos() const
QRectF::translate
void translate(qreal dx, qreal dy)
GridHandle::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0)
Definition: gridhandle.cpp:334
QObject::installEventFilter
void installEventFilter(QObject *filterObj)
Handle::group
AbstractGroup * group() const
Definition: handle.cpp:90
QPainter::fillPath
void fillPath(const QPainterPath &path, const QBrush &brush)
QObject
QPainter::setPen
void setPen(const QColor &color)
QGraphicsSceneMouseEvent
QGraphicsItem::setPos
void setPos(const QPointF &pos)
QPainter
QGraphicsWidget
QGraphicsSceneMouseEvent::button
Qt::MouseButton button() const
GridHandle::~GridHandle
virtual ~GridHandle()
Definition: gridhandle.cpp:51
GridHandle::widgetResized
void widgetResized()
Definition: gridhandle.cpp:174
QColor
QGraphicsSceneHoverEvent
Handle::widget
QGraphicsWidget * widget() const
Definition: handle.cpp:80
QSize
Handle::ConfigureButton
Definition: handle.h:50
GridHandle::boundingRect
QRectF boundingRect() const
Definition: gridhandle.cpp:101
QPainter::setRenderHints
void setRenderHints(QFlags< QPainter::RenderHint > hints, bool on)
QPainterPath
QRectF::width
qreal width() const
GroupingContainment
The base Containment class.
Definition: groupingcontainment.h:38
QRectF
QRectF::translated
QRectF translated(qreal dx, qreal dy) const
QSize::height
int height() const
QGraphicsSceneMoveEvent
GridHandle::detachWidget
void detachWidget()
Definition: gridhandle.cpp:72
QColor::setAlphaF
void setAlphaF(qreal alpha)
QStyleOptionGraphicsItem
QRectF::adjusted
QRectF adjusted(qreal dx1, qreal dy1, qreal dx2, qreal dy2) const
QRectF::height
qreal height() const
QGraphicsItem::parentItem
QGraphicsItem * parentItem() const
GridHandle::mouseReleaseEvent
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
Definition: gridhandle.cpp:209
QRectF::contains
bool contains(const QPointF &point) const
Handle::NoButton
Definition: handle.h:47
AbstractGroup::destroy
void destroy()
Destroy this Groups and its children, deleting the configurations too.
Definition: abstractgroup.cpp:476
QGraphicsSceneMouseEvent::pos
QPointF pos() const
QSizeF::height
qreal height() const
AbstractGroup
The base Group class.
Definition: abstractgroup.h:43
QGraphicsItem::contains
virtual bool contains(const QPointF &point) const
GridHandle::hoverLeaveEvent
void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
Definition: gridhandle.cpp:272
Handle::widgetMoved
void widgetMoved(QGraphicsWidget *widget)
QSizeF::width
qreal width() const
QObject::removeEventFilter
void removeEventFilter(QObject *obj)
Handle::MoveButton
Definition: handle.h:48
GridHandle::mousePressEvent
void mousePressEvent(QGraphicsSceneMouseEvent *event)
Definition: gridhandle.cpp:179
QRectF::moveTo
void moveTo(qreal x, qreal y)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:42:57 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

GroupingDesktop

Skip menu "GroupingDesktop"
  • Main Page
  • Namespace List
  • 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