1 /***************************************************************************
2 * fdographicswidget.cpp *
4 * Copyright (C) 2008 Jason Stubbs <jasonbstubbs@gmail.com> *
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. *
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. *
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>
38 class FdoGraphicsWidget::Private
42 : clientEmbedded(false)
53 QPointer
<X11EmbedDelegate
> widget
;
56 FdoGraphicsWidget::FdoGraphicsWidget(WId winId
, QGraphicsWidget
*parent
)
57 : QGraphicsWidget(parent
),
62 setMinimumSize(22, 22);
63 setMaximumSize(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()
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())) {
96 QTimer::singleShot(0, this, SLOT(setupXEmbedDelegate()));
98 } else if (!d
->clientEmbedded
) {
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()) {
118 void FdoGraphicsWidget::hideEvent(QHideEvent
*event
)
125 void FdoGraphicsWidget::showEvent(QShowEvent
*event
)
132 void FdoGraphicsWidget::setupXEmbedDelegate()
138 #if QT_VERSION < 0x040401
139 const Qt::ApplicationAttribute attr
= (Qt::ApplicationAttribute
)4;
141 const Qt::ApplicationAttribute attr
= Qt::AA_DontCreateNativeWidgetSiblings
;
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()
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;
183 void FdoGraphicsWidget::handleClientClosed()
186 //kDebug() << "client closed (" << d->winId << ")";
190 void FdoGraphicsWidget::handleClientError(QX11EmbedContainer::Error error
)
194 //kDebug() << "client error (" << d->winId << ")";
200 #include "fdographicswidget.moc"