• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeplasma-addons API Reference
  • KDE Home
  • Contact Us
 

GroupingDesktop

  • sources
  • kde-4.12
  • kdeplasma-addons
  • containments
  • groupingdesktop
  • lib
  • groupexplorer
explorerwindow.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Marco Martin <notmart@gmail.com>
3  * Copyright 2009 Aaron Seigo <aseigo@kde.org>
4  * Copyright 2010 Chani Armitage <chani@kde.org>
5  * Copyright 2010 Giulio Camuffo <giuliocamuffo@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Library General Public License as
9  * published by the Free Software Foundation; either version 2, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this program; if not, write to the
19  * Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22 
23 #include "explorerwindow.h"
24 
25 #include <QApplication>
26 #include <QBoxLayout>
27 #include <QDebug>
28 #include <QDesktopWidget>
29 #include <QPainter>
30 #include <QTimer>
31 #include <QGraphicsLayout>
32 
33 #include <kwindowsystem.h>
34 #include <netwm.h>
35 #include <KIconLoader>
36 
37 #include <Plasma/Containment>
38 #include <Plasma/Corona>
39 #include <Plasma/Theme>
40 #include <Plasma/FrameSvg>
41 #include <Plasma/Dialog>
42 #include <Plasma/WindowEffects>
43 
44 #include "groupexplorer.h"
45 
46 ExplorerWindow *ExplorerWindow::s_instance = 0;
47 
48 ExplorerWindow::ExplorerWindow(QWidget *parent)
49  : QWidget(parent),
50  m_location(Plasma::Floating),
51  m_layout(new QBoxLayout(QBoxLayout::TopToBottom, this)),
52  m_background(new Plasma::FrameSvg(this)),
53  m_corona(0),
54  m_view(0),
55  m_groupManager(0),
56  m_graphicsWidget(0)
57 {
58  Q_UNUSED(parent)
59 
60  m_background->setImagePath("dialogs/background");
61  m_background->setContainsMultipleImages(true);
62 
63  setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
64  KWindowSystem::setState(winId(), NET::SkipTaskbar | NET::SkipPager | NET::Sticky | NET::KeepAbove);
65  setAttribute(Qt::WA_DeleteOnClose);
66  setAttribute(Qt::WA_TranslucentBackground);
67  setFocus(Qt::ActiveWindowFocusReason);
68  setLocation(Plasma::BottomEdge);
69 
70  QPalette pal = palette();
71  pal.setBrush(backgroundRole(), Qt::transparent);
72  setPalette(pal);
73 
74  Plasma::WindowEffects::overrideShadow(winId(), true);
75 
76  m_layout->setContentsMargins(0, 0, 0, 0);
77 
78  connect(KWindowSystem::self(), SIGNAL(activeWindowChanged(WId)), this, SLOT(onActiveWindowChanged(WId)));
79  connect(m_background, SIGNAL(repaintNeeded()), SLOT(backgroundChanged()));
80  m_adjustViewTimer = new QTimer(this);
81  m_adjustViewTimer->setSingleShot(true);
82  connect(m_adjustViewTimer, SIGNAL(timeout()), this, SLOT(syncToGraphicsWidget()));
83 }
84 
85 ExplorerWindow::~ExplorerWindow()
86 {
87  if (m_groupManager) {
88  if (m_corona) {
89  m_corona->removeOffscreenWidget(m_groupManager);
90  }
91  }
92 
93  delete m_groupManager;
94  delete m_view;
95 
96  s_instance = 0;
97 }
98 
99 ExplorerWindow *ExplorerWindow::instance()
100 {
101  if (!s_instance) {
102  s_instance = new ExplorerWindow();
103  }
104 
105  return s_instance;
106 }
107 
108 void ExplorerWindow::adjustSize(int screen)
109 {
110  QSize screenSize = m_corona->screenGeometry(screen).size();
111 
112  setMaximumSize(screenSize);
113 }
114 
115 void ExplorerWindow::backgroundChanged()
116 {
117  Plasma::Location l = m_location;
118  m_location = Plasma::Floating;
119  setLocation(l);
120  update();
121 }
122 
123 void ExplorerWindow::setContainment(Plasma::Containment *containment)
124 {
125  if (containment == m_containment.data()) {
126  return;
127  }
128  m_containment = containment;
129 
130  if (m_containment) {
131  disconnect(m_containment.data(), 0, this, 0);
132  }
133 
134  if (!containment) {
135  return;
136  }
137  m_corona = m_containment.data()->corona();
138 
139  foreach (Plasma::Containment *containment, m_corona->containments()) {
140  connect(containment, SIGNAL(toolBoxToggled()), this, SLOT(close()));
141  }
142 
143  if (m_groupManager) {
144  m_groupManager->setContainment(containment);
145  }
146 }
147 
148 Plasma::Containment *ExplorerWindow::containment() const
149 {
150  return m_containment.data();
151 }
152 
153 void ExplorerWindow::setGraphicsWidget(QGraphicsWidget *widget)
154 {
155  if (m_graphicsWidget) {
156  m_graphicsWidget->removeEventFilter(this);
157  }
158 
159  m_graphicsWidget = widget;
160 
161  if (widget) {
162  if (!layout()) {
163  QVBoxLayout *lay = new QVBoxLayout(this);
164  lay->setMargin(0);
165  lay->setSpacing(0);
166  }
167 
168  if (!m_view) {
169  m_view = new QGraphicsView(this);
170  m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
171  m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
172  m_view->setFrameShape(QFrame::NoFrame);
173  m_view->viewport()->setAutoFillBackground(false);
174  layout()->addWidget(m_view);
175  }
176 
177  m_view->setScene(widget->scene());
178 
179  //try to have the proper size -before- showing the dialog
180  m_view->centerOn(widget);
181  if (widget->layout()) {
182  widget->layout()->activate();
183  }
184  static_cast<QGraphicsLayoutItem *>(widget)->updateGeometry();
185  widget->resize(widget->size().expandedTo(widget->effectiveSizeHint(Qt::MinimumSize)));
186 
187  syncToGraphicsWidget();
188 
189  //adjustSizeTimer->start(150);
190 
191  widget->installEventFilter(this);
192  } else {
193  delete m_view;
194  m_view = 0;
195  }
196 }
197 
198 void ExplorerWindow::syncToGraphicsWidget()
199 {
200  m_adjustViewTimer->stop();
201  if (m_view && m_graphicsWidget) {
202  QSize prevSize = size();
203 
204  //set the sizehints correctly:
205  int left, top, right, bottom;
206  getContentsMargins(&left, &top, &right, &bottom);
207 
208  QRect screenRect;
209  if (m_containment) {
210  screenRect = m_corona->screenGeometry(m_containment.data()->screen());
211  } else {
212  return;
213  }
214  QSize maxSize = KWindowSystem::workArea().intersect(screenRect).size();
215 
216  QSize windowSize;
217  if (m_location == Plasma::LeftEdge || m_location == Plasma::RightEdge) {
218  windowSize = QSize(qMin(int(m_graphicsWidget->size().width()) + left + right, maxSize.width()), maxSize.height());
219  m_graphicsWidget->resize(m_graphicsWidget->size().width(), windowSize.height());
220  } else {
221  windowSize = QSize(maxSize.width(), qMin(int(m_graphicsWidget->size().height()) + top + bottom, maxSize.height()));
222  m_graphicsWidget->resize(windowSize.width(), m_graphicsWidget->size().height());
223  }
224 
225  resize(windowSize);
226 
227  setMinimumSize(windowSize);
228 
229  updateGeometry();
230 
231  //reposition and resize the view.
232  //force a valid rect, otherwise it will take up the whole scene
233  QRectF sceneRect(m_graphicsWidget->sceneBoundingRect());
234 
235  sceneRect.setWidth(qMax(qreal(1), sceneRect.width()));
236  sceneRect.setHeight(qMax(qreal(1), sceneRect.height()));
237  m_view->setSceneRect(sceneRect);
238 
239  m_view->centerOn(m_graphicsWidget);
240 
241  }
242 }
243 
244 bool ExplorerWindow::eventFilter(QObject *watched, QEvent *event)
245 {
246  if (watched == m_graphicsWidget &&
247  (event->type() == QEvent::GraphicsSceneResize || event->type() == QEvent::GraphicsSceneMove)) {
248  m_adjustViewTimer->start(150);
249  }
250 
251  return QWidget::eventFilter(watched, event);
252 }
253 
254 void ExplorerWindow::setLocation(const Plasma::Location &loc)
255 {
256  if (m_location == loc) {
257  return;
258  }
259 
260  Plasma::WindowEffects::slideWindow(this, loc);
261 
262  m_location = loc;
263 
264  switch (loc) {
265  case Plasma::LeftEdge:
266  m_background->setEnabledBorders(Plasma::FrameSvg::RightBorder);
267  m_layout->setDirection(QBoxLayout::TopToBottom);
268  setContentsMargins(0, 0, m_background->marginSize(Plasma::RightMargin), 0);
269  break;
270 
271  case Plasma::RightEdge:
272  m_background->setEnabledBorders(Plasma::FrameSvg::LeftBorder);
273  m_layout->setDirection(QBoxLayout::TopToBottom);
274  setContentsMargins(m_background->marginSize(Plasma::LeftMargin), 0, 0, 0);
275  break;
276 
277  case Plasma::TopEdge:
278  m_background->setEnabledBorders(Plasma::FrameSvg::BottomBorder);
279  m_layout->setDirection(QBoxLayout::BottomToTop);
280  setContentsMargins(0, 0, 0, m_background->marginSize(Plasma::BottomMargin));
281  break;
282 
283  case Plasma::BottomEdge:
284  default:
285  m_background->setEnabledBorders(Plasma::FrameSvg::TopBorder);
286  m_layout->setDirection(QBoxLayout::TopToBottom);
287  setContentsMargins(0, m_background->marginSize(Plasma::TopMargin), 0, 0);
288  break;
289  }
290 
291  if (m_groupManager) {
292  m_groupManager->setLocation(location());
293  }
294 
295  resize(sizeHint());
296 }
297 
298 QPoint ExplorerWindow::positionForPanelGeometry(const QRect &panelGeom) const
299 {
300  int screen;
301  if (m_containment) {
302  screen = m_containment.data()->screen();
303  } else {
304  return QPoint();
305  }
306 
307  QRect screenGeom = m_corona->screenGeometry(screen);
308 
309  switch (m_location) {
310  case Plasma::LeftEdge:
311  return QPoint(panelGeom.right(), screenGeom.top());
312  break;
313  case Plasma::RightEdge:
314  return QPoint(panelGeom.left() - width(), screenGeom.top());
315  break;
316  case Plasma::TopEdge:
317  return QPoint(screenGeom.left(), panelGeom.bottom());
318  break;
319  case Plasma::BottomEdge:
320  default:
321  return QPoint(screenGeom.left(), panelGeom.top() - height());
322  break;
323  }
324 }
325 
326 Plasma::Location ExplorerWindow::location() const
327 {
328  return m_location;
329 }
330 
331 Qt::Orientation ExplorerWindow::orientation() const
332 {
333  if (m_location == Plasma::LeftEdge || m_location == Plasma::RightEdge) {
334  return Qt::Vertical;
335  }
336 
337  return Qt::Horizontal;
338 }
339 
340 
341 void ExplorerWindow::showGroupExplorer()
342 {
343  if (!m_groupManager) {
344  m_groupManager = new GroupExplorer(location());
345 
346  m_corona->addOffscreenWidget(m_groupManager);
347  m_groupManager->show();
348 
349  m_groupManager->setContainment(m_containment.data());
350  m_groupManager->setLocation(location());
351  if (orientation() == Qt::Horizontal) {
352  m_groupManager->resize(width(), m_groupManager->size().height());
353  } else {
354  m_groupManager->resize(m_groupManager->size().width(), height());
355  }
356 
357  m_groupManager->setIconSize(KIconLoader::SizeHuge);
358 
359  setGraphicsWidget(m_groupManager);
360 
361  connect(m_groupManager, SIGNAL(closeClicked()), this, SLOT(close()));
362  } else {
363  m_groupManager->setLocation(location());
364  m_groupManager->show();
365  setGraphicsWidget(m_groupManager);
366  }
367 
368 }
369 
370 bool ExplorerWindow::isControllerViewVisible() const
371 {
372  return m_view && m_view->isVisible();
373 }
374 
375 Plasma::FrameSvg *ExplorerWindow::background() const
376 {
377  return m_background;
378 }
379 
380 void ExplorerWindow::onActiveWindowChanged(WId id)
381 {
382  Q_UNUSED(id)
383 
384  //if the active window isn't the plasma desktop and the widgets explorer is visible,
385  //then close the panel controller
386  if (QApplication::activeWindow() == 0 || (QApplication::activeWindow()->winId() != KWindowSystem::activeWindow())) {
387  if (m_view && m_view->isVisible() && !isActiveWindow()) {
388  //close();
389  }
390  }
391 }
392 
393 void ExplorerWindow::paintEvent(QPaintEvent *event)
394 {
395  Q_UNUSED(event)
396 
397  QPainter painter(this);
398  painter.setCompositionMode(QPainter::CompositionMode_Source );
399 
400  m_background->paintFrame(&painter);
401 }
402 
403 void ExplorerWindow::keyPressEvent(QKeyEvent *event)
404 {
405  if (event->key() == Qt::Key_Escape) {
406  close();
407  }
408 }
409 
410 void ExplorerWindow::resizeEvent(QResizeEvent * event)
411 {
412  m_background->resizeFrame(size());
413 
414  Plasma::WindowEffects::enableBlurBehind(effectiveWinId(), true, m_background->mask());
415 
416  qDebug() << "ExplorerWindow::resizeEvent" << event->oldSize()<<event->size();
417 
418  QWidget::resizeEvent(event);
419 }
420 
421 #include "explorerwindow.moc"
ExplorerWindow::paintEvent
void paintEvent(QPaintEvent *event)
Definition: explorerwindow.cpp:393
ExplorerWindow
Definition: explorerwindow.h:40
QWidget
ExplorerWindow::background
Plasma::FrameSvg * background() const
Definition: explorerwindow.cpp:375
ExplorerWindow::location
Plasma::Location location() const
Definition: explorerwindow.cpp:326
GroupExplorer::setLocation
void setLocation(Plasma::Location location)
set orientation
Definition: groupexplorer.cpp:115
groupexplorer.h
GroupExplorer::setIconSize
void setIconSize(int size)
Sets the icon size.
Definition: groupexplorer.cpp:122
ExplorerWindow::setGraphicsWidget
void setGraphicsWidget(QGraphicsWidget *widget)
Definition: explorerwindow.cpp:153
GroupExplorer::setContainment
void setContainment(Plasma::Containment *containment)
Changes the current containment you've got to call this at least once so that it can access the coron...
Definition: groupexplorer.cpp:128
ExplorerWindow::showGroupExplorer
void showGroupExplorer()
Definition: explorerwindow.cpp:341
ExplorerWindow::setLocation
virtual void setLocation(const Plasma::Location &loc)
Definition: explorerwindow.cpp:254
ExplorerWindow::instance
static ExplorerWindow * instance()
Definition: explorerwindow.cpp:99
ExplorerWindow::resizeEvent
void resizeEvent(QResizeEvent *event)
Definition: explorerwindow.cpp:410
GroupExplorer
Definition: groupexplorer.h:33
ExplorerWindow::eventFilter
bool eventFilter(QObject *watched, QEvent *event)
Definition: explorerwindow.cpp:244
ExplorerWindow::keyPressEvent
void keyPressEvent(QKeyEvent *event)
Definition: explorerwindow.cpp:403
ExplorerWindow::orientation
Qt::Orientation orientation() const
Definition: explorerwindow.cpp:331
ExplorerWindow::containment
Plasma::Containment * containment() const
Definition: explorerwindow.cpp:148
ExplorerWindow::setContainment
virtual void setContainment(Plasma::Containment *containment)
Definition: explorerwindow.cpp:123
ExplorerWindow::positionForPanelGeometry
QPoint positionForPanelGeometry(const QRect &panelGeom) const
Definition: explorerwindow.cpp:298
explorerwindow.h
ExplorerWindow::isControllerViewVisible
bool isControllerViewVisible() const
Definition: explorerwindow.cpp:370
QGraphicsWidget
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Mon Oct 13 2014 22:55:03 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

kdeplasma-addons API Reference

Skip menu "kdeplasma-addons API Reference"
  •     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