add more spacing
[personal-kdebase.git] / workspace / plasma / applets / systemtray / protocols / fdo / fdographicswidget.cpp
blobd429647f3aa411ca8253cdd11422425baf6d022d
1 /***************************************************************************
2 * fdographicswidget.cpp *
3 * *
4 * Copyright (C) 2008 Jason Stubbs <jasonbstubbs@gmail.com> *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
20 ***************************************************************************/
22 #include "fdographicswidget.h"
23 #include "x11embeddelegate.h"
24 #include "x11embedcontainer.h"
26 #include <QtCore/QPointer>
27 #include <QtCore/QTimer>
29 #include <QtGui/QApplication>
30 #include <QtGui/QGraphicsView>
32 #include <plasma/theme.h>
35 namespace SystemTray
38 class FdoGraphicsWidget::Private
40 public:
41 Private()
42 : clientEmbedded(false)
46 ~Private()
48 delete widget;
51 WId winId;
52 bool clientEmbedded;
53 QPointer<X11EmbedDelegate> widget;
56 FdoGraphicsWidget::FdoGraphicsWidget(WId winId, QGraphicsWidget *parent)
57 : QGraphicsWidget(parent),
58 d(new Private())
60 d->winId = winId;
62 setMinimumSize(22, 22);
63 setMaximumSize(22, 22);
64 resize(22, 22);
66 setCacheMode(QGraphicsItem::NoCache);
68 connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()),
69 this, SLOT(updateWidgetBackground()));
70 QTimer::singleShot(0, this, SLOT(setupXEmbedDelegate()));
74 FdoGraphicsWidget::~FdoGraphicsWidget()
76 delete d;
80 void FdoGraphicsWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *parentWidget)
82 QGraphicsWidget::paint(painter, option, parentWidget);
84 QGraphicsView *parentView = 0;
85 foreach (QGraphicsView *view, scene()->views()) {
86 if (view->isVisible() && view->sceneRect().intersects(sceneBoundingRect())) {
87 parentView = view;
91 if (!parentView) {
92 return;
95 if (!d->widget) {
96 QTimer::singleShot(0, this, SLOT(setupXEmbedDelegate()));
97 return;
98 } else if (!d->clientEmbedded) {
99 return;
102 if (d->widget->parentWidget() != parentView) {
103 //kDebug() << "embedding into" << parentView->metaObject()->className() << "(" << d->winId << ")";
104 d->widget->setParent(parentView);
107 QPoint pos = parentView->mapFromScene(scenePos());
108 pos += parentView->viewport()->pos();
109 if (d->widget->pos() != pos) {
110 d->widget->move(pos);
113 if (!d->widget->isVisible()) {
114 d->widget->show();
118 void FdoGraphicsWidget::hideEvent(QHideEvent *event)
120 if (d->widget) {
121 d->widget->hide();
125 void FdoGraphicsWidget::showEvent(QShowEvent *event)
127 if (d->widget) {
128 d->widget->show();
132 void FdoGraphicsWidget::setupXEmbedDelegate()
134 if (d->widget) {
135 return;
138 #if QT_VERSION < 0x040401
139 const Qt::ApplicationAttribute attr = (Qt::ApplicationAttribute)4;
140 #else
141 const Qt::ApplicationAttribute attr = Qt::AA_DontCreateNativeWidgetSiblings;
142 #endif
143 if (!QApplication::testAttribute(attr)) {
144 QApplication::setAttribute(attr);
147 d->widget = new X11EmbedDelegate();
148 d->widget->setMinimumSize(22, 22);
149 d->widget->setMaximumSize(22, 22);
150 d->widget->resize(22, 22);
152 connect(d->widget->container(), SIGNAL(clientIsEmbedded()),
153 this, SLOT(handleClientEmbedded()));
154 connect(d->widget->container(), SIGNAL(clientClosed()),
155 this, SLOT(handleClientClosed()));
156 connect(d->widget->container(), SIGNAL(error(QX11EmbedContainer::Error)),
157 this, SLOT(handleClientError(QX11EmbedContainer::Error)));
159 d->widget->container()->embedSystemTrayClient(d->winId);
162 void FdoGraphicsWidget::updateWidgetBackground()
164 if (!d->widget) {
165 return;
168 QPalette palette = d->widget->palette();
169 palette.setBrush(QPalette::Window, Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor));
170 d->widget->setPalette(palette);
171 d->widget->setBackgroundRole(QPalette::Window);
175 void FdoGraphicsWidget::handleClientEmbedded()
177 //kDebug() << "client embedded (" << d->winId << ")";
178 d->clientEmbedded = true;
179 update();
183 void FdoGraphicsWidget::handleClientClosed()
185 emit clientClosed();
186 //kDebug() << "client closed (" << d->winId << ")";
190 void FdoGraphicsWidget::handleClientError(QX11EmbedContainer::Error error)
192 Q_UNUSED(error);
194 //kDebug() << "client error (" << d->winId << ")";
195 emit clientClosed();
200 #include "fdographicswidget.moc"