• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kde-runtime API Reference
  • KDE Home
  • Contact Us
 

PlasmaComponents

  • sources
  • kde-4.14
  • kde-runtime
  • plasma
  • declarativeimports
  • plasmacomponents
fullscreenwindow.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2012 Marco Martin <mart@kde.org>
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 "fullscreenwindow.h"
21 #include "../core/declarativeitemcontainer_p.h"
22 #include "plasmacomponentsplugin.h"
23 
24 #include <QApplication>
25 #include <QDeclarativeItem>
26 #include <QDeclarativeContext>
27 #include <QGraphicsObject>
28 #include <QGraphicsScene>
29 #include <QGraphicsView>
30 #include <QGraphicsWidget>
31 #include <QLayout>
32 #include <QTimer>
33 #include <QDesktopWidget>
34 #include <QFile>
35 
36 #include <KWindowSystem>
37 #include <KStandardDirs>
38 
39 #include <kdeclarative.h>
40 
41 #include <Plasma/Corona>
42 #include <Plasma/WindowEffects>
43 
44 
45 uint FullScreenWindow::s_numItems = 0;
46 
47 class Background : public QWidget
48 {
49 public:
50  Background(FullScreenWindow *dialog)
51  : QWidget( 0L ),
52  m_dialog(dialog)
53  {
54  setAttribute( Qt::WA_NoSystemBackground );
55  setAttribute( Qt::WA_TranslucentBackground );
56 
57  setWindowFlags(Qt::FramelessWindowHint | Qt::CustomizeWindowHint);
58  KWindowSystem::setOnAllDesktops(winId(), true);
59  unsigned long state = NET::Sticky | NET::StaysOnTop | NET::KeepAbove | NET::SkipTaskbar | NET::SkipPager | NET::MaxVert | NET::MaxHoriz;
60  KWindowSystem::setState(effectiveWinId(), state);
61  }
62 
63  ~Background()
64  {}
65 
66  void paintEvent( QPaintEvent *e )
67  {
68  QPainter painter( this );
69  painter.setCompositionMode(QPainter::CompositionMode_Source);
70  painter.fillRect(e->rect(), QColor(0, 0, 0, 80));
71  }
72 
73  void mousePressEvent(QMouseEvent *event)
74  {
75  event->accept();
76  m_dialog->view()->winId();
77  KWindowSystem::forceActiveWindow(m_dialog->view()->winId());
78  }
79 
80  void mouseReleaseEvent(QMouseEvent *event)
81  {
82  if (!m_dialog->view()->geometry().contains(event->globalPos())) {
83  emit m_dialog->clickedOutside();
84  m_dialog->close();
85  }
86  }
87 
88 private:
89  FullScreenWindow *m_dialog;
90 };
91 
92 FullScreenWindow::FullScreenWindow(QDeclarativeItem *parent)
93  : QDeclarativeItem(parent),
94  m_declarativeItemContainer(0)
95 {
96  m_view = new QGraphicsView();
97  m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
98  m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
99  m_view->installEventFilter(this);
100  m_view->setAutoFillBackground(false);
101  m_view->viewport()->setAutoFillBackground(false);
102  m_view->setAttribute(Qt::WA_TranslucentBackground);
103  m_view->setAttribute(Qt::WA_NoSystemBackground);
104  m_view->viewport()->setAttribute(Qt::WA_NoSystemBackground);
105  m_view->setCacheMode(QGraphicsView::CacheNone);
106  m_view->setWindowFlags(Qt::FramelessWindowHint | Qt::CustomizeWindowHint);
107  m_view->setFrameShape(QFrame::NoFrame);
108  KWindowSystem::setOnAllDesktops(m_view->winId(), true);
109  unsigned long state = NET::Sticky | NET::StaysOnTop | NET::KeepAbove | NET::SkipTaskbar | NET::SkipPager;
110  KWindowSystem::setState(m_view->effectiveWinId(), state);
111 
112  m_background = new Background(this);
113 }
114 
115 FullScreenWindow::~FullScreenWindow()
116 {
117  delete m_view;
118 }
119 
120 void FullScreenWindow::init(const QString &componentName)
121 {
122  if (m_rootObject) {
123  return;
124  }
125 
126  //Try to figure out the path of the dialog component
127  const QString target = KDeclarative::componentsTarget();
128  QString filePath;
129  if (target != KDeclarative::defaultComponentsTarget()) {
130  const QString file = "platformimports/" % target % "/org/kde/plasma/components/" % componentName % ".qml";
131  filePath = KStandardDirs::locate("module", file);
132  }
133 
134  if (filePath.isEmpty()) {
135  const QString file = "imports/org/kde/plasma/components/" % componentName % ".qml";
136  filePath = KStandardDirs::locate("module", file);
137  }
138 
139  if (filePath.isEmpty()) {
140  kWarning() << "Component not found:" << componentName;
141  return;
142  }
143 
144  QDeclarativeEngine *engine = EngineBookKeeping::self()->engine();
145  if (!engine) {
146  kWarning() << "Warning, no QDeclarativeEngines available anymore, should never happen";
147  Q_ASSERT(0);
148  }
149  QDeclarativeComponent *component = new QDeclarativeComponent(engine, filePath, this);
150 
151  QDeclarativeContext *creationContext = component->creationContext();
152  m_rootObject = component->create(creationContext);
153  if (component->status() == QDeclarativeComponent::Error) {
154  kWarning()<<component->errors();
155  }
156 
157  if (m_rootObject) {
158  setMainItem(qobject_cast<QGraphicsObject *>(m_rootObject.data()));
159  connect(m_rootObject.data(), SIGNAL(statusChanged()), this, SLOT(statusHasChanged()));
160  connect(m_rootObject.data(), SIGNAL(accepted()), this, SIGNAL(accepted()));
161  connect(m_rootObject.data(), SIGNAL(rejected()), this, SIGNAL(rejected()));
162  connect(m_rootObject.data(), SIGNAL(clickedOutside()), this, SIGNAL(clickedOutside()));
163  }
164 }
165 
166 QGraphicsObject *FullScreenWindow::mainItem() const
167 {
168  return m_mainItem.data();
169 }
170 
171 void FullScreenWindow::setMainItem(QGraphicsObject *mainItem)
172 {
173  if (m_mainItem.data() != mainItem) {
174  if (m_mainItem) {
175  m_mainItem.data()->setParent(mainItem->parent());
176  m_mainItem.data()->removeEventFilter(this);
177  m_mainItem.data()->setY(0);
178  m_scene = 0;
179  }
180  m_mainItem = mainItem;
181  if (mainItem) {
182  mainItem->setParentItem(0);
183  mainItem->setParent(this);
184  m_scene = mainItem->scene();
185  m_view->resize(mainItem->boundingRect().size().toSize());
186  mainItem->installEventFilter(this);
187  }
188 
189  //if this is called in Compenent.onCompleted we have to wait a loop the item is added to a scene
190  QTimer::singleShot(0, this, SLOT(syncViewToMainItem()));
191  }
192 }
193 
194 void FullScreenWindow::syncViewToMainItem()
195 {
196  if (!m_mainItem) {
197  return;
198  }
199 
200  //not have a scene? go up in the hyerarchy until we find something with a scene
201  QGraphicsScene *scene = m_mainItem.data()->scene();
202  if (!scene) {
203  QObject *parent = m_mainItem.data();
204  while ((parent = parent->parent())) {
205  QGraphicsObject *qo = qobject_cast<QGraphicsObject *>(parent);
206  if (qo) {
207  scene = qo->scene();
208 
209  if (scene) {
210  scene->addItem(m_mainItem.data());
211  ++s_numItems;
212  Plasma::Corona *corona = qobject_cast<Plasma::Corona *>(scene);
213  QDeclarativeItem *di = qobject_cast<QDeclarativeItem *>(m_mainItem.data());
214 
215  if (corona && di) {
216  if (!m_declarativeItemContainer) {
217  m_declarativeItemContainer = new DeclarativeItemContainer();
218  scene->addItem(m_declarativeItemContainer);
219  corona->addOffscreenWidget(m_declarativeItemContainer);
220  }
221  m_declarativeItemContainer->setDeclarativeItem(di);
222  } else {
223  m_mainItem.data()->setY(-10000*s_numItems);
224  m_mainItem.data()->setY(10000*s_numItems);
225  }
226  break;
227  }
228  }
229  }
230  }
231 
232  if (!scene) {
233  return;
234  }
235 
236  m_view->setScene(scene);
237 
238 
239  QRectF itemGeometry(QPointF(m_mainItem.data()->x(), m_mainItem.data()->y()),
240  QSizeF(m_mainItem.data()->boundingRect().size()));
241  if (m_declarativeItemContainer) {
242  m_view->resize(itemGeometry.size().toSize());
243  m_view->setSceneRect(m_declarativeItemContainer->geometry());
244 
245  } else {
246  QRectF itemGeometry(QPointF(m_mainItem.data()->x(), m_mainItem.data()->y()),
247  QSizeF(m_mainItem.data()->boundingRect().size()));
248  m_view->resize(itemGeometry.size().toSize());
249  m_view->setSceneRect(itemGeometry);
250  }
251 
252  m_view->move(QApplication::desktop()->availableGeometry().center() - QPoint(m_view->width()/2, m_view->height()/2));
253 }
254 
255 void FullScreenWindow::syncMainItemToView()
256 {
257  if (!m_mainItem) {
258  return;
259  }
260 
261  m_mainItem.data()->setProperty("width", m_view->width());
262  m_mainItem.data()->setProperty("height", m_view->height());
263 
264  if (m_declarativeItemContainer) {
265  m_declarativeItemContainer->resize(m_view->size());
266  m_view->setSceneRect(m_declarativeItemContainer->geometry());
267  } else {
268  QRectF itemGeometry(QPointF(m_mainItem.data()->x(), m_mainItem.data()->y()),
269  QSizeF(m_mainItem.data()->boundingRect().size()));
270  m_view->setSceneRect(itemGeometry);
271  }
272 }
273 
274 bool FullScreenWindow::isVisible() const
275 {
276  return m_view->isVisible();
277 }
278 
279 void FullScreenWindow::setVisible(const bool visible)
280 {
281  if (m_view->isVisible() != visible) {
282  m_background->setVisible(visible);
283  Plasma::WindowEffects::slideWindow(m_view->winId(), Plasma::BottomEdge, 0);
284  m_view->setVisible(visible);
285  unsigned long state = NET::Sticky | NET::StaysOnTop | NET::KeepAbove | NET::SkipTaskbar | NET::SkipPager;
286  KWindowSystem::setState(m_view->effectiveWinId(), state);
287  KWindowSystem::setState(m_background->effectiveWinId(), state);
288  if (visible) {
289  m_view->raise();
290  KWindowSystem::forceActiveWindow(m_view->effectiveWinId());
291  }
292  }
293 }
294 
295 QGraphicsView *FullScreenWindow::view() const
296 {
297  return m_view;
298 }
299 
300 
301 QDeclarativeListProperty<QGraphicsObject> FullScreenWindow::title()
302 {
303  if (m_rootObject) {
304  return m_rootObject.data()->property("title").value<QDeclarativeListProperty<QGraphicsObject> >();
305  } else {
306  return QDeclarativeListProperty<QGraphicsObject>(this, m_dummyTitleElements);
307  }
308 }
309 
310 QDeclarativeListProperty<QGraphicsObject> FullScreenWindow::content()
311 {
312  if (m_rootObject) {
313  return m_rootObject.data()->property("content").value<QDeclarativeListProperty<QGraphicsObject> >();
314  } else {
315  return QDeclarativeListProperty<QGraphicsObject>(this, m_dummyContentElements);
316  }
317 }
318 
319 QDeclarativeListProperty<QGraphicsObject> FullScreenWindow::buttons()
320 {
321  if (m_rootObject) {
322  return m_rootObject.data()->property("buttons").value<QDeclarativeListProperty<QGraphicsObject> >();
323  } else {
324  return QDeclarativeListProperty<QGraphicsObject>(this, m_dummyButtonsElements);
325  }
326 }
327 
328 DialogStatus::Status FullScreenWindow::status() const
329 {
330  if (m_rootObject) {
331  return (DialogStatus::Status)m_rootObject.data()->property("status").toInt();
332  } else {
333  return DialogStatus::Closed;
334  }
335 }
336 
337 
338 void FullScreenWindow::statusHasChanged()
339 {
340  if (status() == DialogStatus::Closed) {
341  setVisible(false);
342  } else {
343  setVisible(true);
344  }
345  emit statusChanged();
346 }
347 
348 void FullScreenWindow::open()
349 {
350  if (m_rootObject) {
351  QMetaObject::invokeMethod(m_rootObject.data(), "open");
352  }
353 }
354 
355 void FullScreenWindow::accept()
356 {
357  if (m_rootObject) {
358  QMetaObject::invokeMethod(m_rootObject.data(), "accept");
359  }
360 }
361 
362 void FullScreenWindow::reject()
363 {
364  if (m_rootObject) {
365  QMetaObject::invokeMethod(m_rootObject.data(), "reject");
366  }
367 }
368 
369 void FullScreenWindow::close()
370 {
371  if (m_rootObject) {
372  QMetaObject::invokeMethod(m_rootObject.data(), "close");
373  }
374 }
375 
376 
377 
378 
379 bool FullScreenWindow::eventFilter(QObject *watched, QEvent *event)
380 {
381  if (watched == m_mainItem.data() &&
382  (event->type() == QEvent::GraphicsSceneResize)) {
383  syncViewToMainItem();
384  } else if (watched == m_view &&
385  (event->type() == QEvent::Resize)) {
386  syncMainItemToView();
387  }
388  return false;
389 }
390 
391 
392 
393 #include "fullscreenwindow.moc"
394 
FullScreenWindow::content
QDeclarativeListProperty< QGraphicsObject > content()
FullScreenWindow::accepted
void accepted()
QEvent
QWidget
QEvent::type
Type type() const
QGraphicsScene
QDeclarativeEngine
QDeclarativeComponent::status
status
QGraphicsItem::setParentItem
void setParentItem(QGraphicsItem *newParent)
QFrame::setFrameShape
void setFrameShape(Shape)
EngineBookKeeping::engine
QDeclarativeEngine * engine() const
Definition: plasmacomponentsplugin.cpp:59
FullScreenWindow::eventFilter
bool eventFilter(QObject *watched, QEvent *event)
Definition: fullscreenwindow.cpp:379
QRectF::size
QSizeF size() const
QGraphicsObject::visible
visible
QWidget::isVisible
bool isVisible() const
QWidget::setAttribute
void setAttribute(Qt::WidgetAttribute attribute, bool on)
QAbstractScrollArea::viewport
QWidget * viewport() const
QGraphicsView::setSceneRect
void setSceneRect(const QRectF &rect)
FullScreenWindow::status
DialogStatus::Status status() const
QWeakPointer::data
T * data() const
QPoint
QMouseEvent
DialogStatus::Closed
Definition: enums.h:37
QGraphicsItem::scene
QGraphicsScene * scene() const
QWidget::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *event)
FullScreenWindow::clickedOutside
void clickedOutside()
QPointF
QWidget::width
width
QWidget::resize
void resize(int w, int h)
QPaintEvent::rect
const QRect & rect() const
QWidget::paintEvent
virtual void paintEvent(QPaintEvent *event)
QMouseEvent::globalPos
const QPoint & globalPos() const
QObject::installEventFilter
void installEventFilter(QObject *filterObj)
QWidget::mousePressEvent
virtual void mousePressEvent(QMouseEvent *event)
FullScreenWindow::isVisible
bool isVisible() const
Definition: fullscreenwindow.cpp:274
QDeclarativeComponent::create
virtual QObject * create(QDeclarativeContext *context)
FullScreenWindow::title
QDeclarativeListProperty< QGraphicsObject > title()
QObject
FullScreenWindow::accept
Q_INVOKABLE void accept()
Definition: fullscreenwindow.cpp:355
QSizeF::toSize
QSize toSize() const
QAbstractScrollArea::setHorizontalScrollBarPolicy
void setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy)
QPainter
QWidget::raise
void raise()
QString::isEmpty
bool isEmpty() const
QDeclarativeContext
FullScreenWindow::rejected
void rejected()
QWidget::move
void move(int x, int y)
QGraphicsView::setScene
void setScene(QGraphicsScene *scene)
FullScreenWindow::Background
friend class Background
Definition: fullscreenwindow.h:100
QWidget::winId
WId winId() const
QString
QColor
FullScreenWindow::buttons
QDeclarativeListProperty< QGraphicsObject > buttons()
QGraphicsView::setCacheMode
void setCacheMode(QFlags< QGraphicsView::CacheModeFlag > mode)
FullScreenWindow::setMainItem
void setMainItem(QGraphicsObject *mainItem)
Definition: fullscreenwindow.cpp:171
QGraphicsObject
QDeclarativeComponent::errors
QList< QDeclarativeError > errors() const
FullScreenWindow::mainItem
QGraphicsObject * mainItem() const
Definition: fullscreenwindow.cpp:166
QDeclarativeComponent
QObject::setParent
void setParent(QObject *parent)
FullScreenWindow::FullScreenWindow
FullScreenWindow(QDeclarativeItem *parent=0)
Definition: fullscreenwindow.cpp:92
FullScreenWindow::setVisible
void setVisible(const bool visible)
Definition: fullscreenwindow.cpp:279
EngineBookKeeping::self
static EngineBookKeeping * self()
Definition: plasmacomponentsplugin.cpp:54
FullScreenWindow::view
QGraphicsView * view() const
Definition: fullscreenwindow.cpp:295
QMetaObject::invokeMethod
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
FullScreenWindow::reject
Q_INVOKABLE void reject()
Definition: fullscreenwindow.cpp:362
FullScreenWindow::open
Q_INVOKABLE void open()
Definition: fullscreenwindow.cpp:348
QWidget::setWindowFlags
void setWindowFlags(QFlags< Qt::WindowType > type)
plasmacomponentsplugin.h
QGraphicsObject::parent
parent
QSizeF
QApplication::desktop
QDesktopWidget * desktop()
QGraphicsItem::boundingRect
virtual QRectF boundingRect() const =0
FullScreenWindow::init
void init(const QString &componentName)
Definition: fullscreenwindow.cpp:120
QRectF
FullScreenWindow::~FullScreenWindow
~FullScreenWindow()
Definition: fullscreenwindow.cpp:115
fullscreenwindow.h
QDeclarativeItem
QWidget::setAutoFillBackground
void setAutoFillBackground(bool enabled)
FullScreenWindow::close
Q_INVOKABLE void close()
Definition: fullscreenwindow.cpp:369
QPaintEvent
QGraphicsScene::addItem
void addItem(QGraphicsItem *item)
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QWidget::effectiveWinId
WId effectiveWinId() const
QObject::parent
QObject * parent() const
QDeclarativeListProperty
QAbstractScrollArea::setVerticalScrollBarPolicy
void setVerticalScrollBarPolicy(Qt::ScrollBarPolicy)
QGraphicsView
FullScreenWindow
Definition: fullscreenwindow.h:38
QWidget::height
height
QDeclarativeComponent::creationContext
QDeclarativeContext * creationContext() const
DialogStatus::Status
Status
Definition: enums.h:33
FullScreenWindow::statusChanged
void statusChanged()
QTimer::singleShot
singleShot
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:08:40 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

PlasmaComponents

Skip menu "PlasmaComponents"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List

kde-runtime API Reference

Skip menu "kde-runtime API Reference"
  • KCMShell
  • KNotify
  • Plasma Runtime
  •     PlasmaCore
  •     DragAndDrop
  •     PlasmaComponents
  •     PlasmaExtraComponents
  •     QtExtraComponents

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