Plasma
view.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "view.h"
00021
00022 #include <kglobal.h>
00023 #include <kwindowsystem.h>
00024 #include <kactioncollection.h>
00025
00026 #include "corona.h"
00027 #include "containment.h"
00028 #include "wallpaper.h"
00029
00030 using namespace Plasma;
00031
00032 namespace Plasma
00033 {
00034
00035 class ViewPrivate
00036 {
00037 public:
00038 ViewPrivate(View *view, int uniqueId)
00039 : q(view),
00040 containment(0),
00041 drawWallpaper(true),
00042 trackChanges(true),
00043 viewId(0),
00044 lastScreen(-1),
00045 lastDesktop(-2)
00046 {
00047 if (uniqueId > s_maxViewId) {
00048 s_maxViewId = uniqueId;
00049 viewId = uniqueId;
00050 }
00051
00052 if (viewId == 0) {
00053
00054
00055 viewId = ++s_maxViewId;
00056 }
00057 }
00058
00059 ~ViewPrivate()
00060 {
00061 }
00062
00063 void updateSceneRect()
00064 {
00065 if (!containment || !trackChanges) {
00066 return;
00067 }
00068
00069 kDebug() << "!!!!!!!!!!!!!!!!! setting the scene rect to"
00070 << containment->sceneBoundingRect()
00071 << "associated screen is" << containment->screen();
00072
00073 emit q->sceneRectAboutToChange();
00074 if (q->transform().isIdentity()) {
00075 q->setSceneRect(containment->sceneBoundingRect());
00076 } else {
00077
00078 q->ensureVisible(containment->sceneBoundingRect());
00079
00080 }
00081 emit q->sceneRectChanged();
00082 }
00083
00084 void containmentDestroyed()
00085 {
00086 containment = 0;
00087 }
00088
00089 void initGraphicsView()
00090 {
00091 q->setFrameShape(QFrame::NoFrame);
00092 q->setAutoFillBackground(true);
00093 q->setDragMode(QGraphicsView::NoDrag);
00094
00095 q->setInteractive(true);
00096 q->setAcceptDrops(true);
00097 q->setAlignment(Qt::AlignLeft | Qt::AlignTop);
00098 q->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00099 q->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00100 }
00101
00102 Plasma::View *q;
00103 Plasma::Containment *containment;
00104 bool drawWallpaper;
00105 bool trackChanges;
00106 int viewId;
00107 int lastScreen;
00108 int lastDesktop;
00109 static int s_maxViewId;
00110 };
00111
00112 int ViewPrivate::s_maxViewId(0);
00113
00114 View::View(Containment *containment, QWidget *parent)
00115 : QGraphicsView(parent),
00116 d(new ViewPrivate(this, 0))
00117 {
00118 d->initGraphicsView();
00119
00120 if (containment) {
00121 setScene(containment->scene());
00122 setContainment(containment);
00123 }
00124 }
00125
00126 View::View(Containment *containment, int viewId, QWidget *parent)
00127 : QGraphicsView(parent),
00128 d(new ViewPrivate(this, viewId))
00129 {
00130 d->initGraphicsView();
00131
00132 if (containment) {
00133 setScene(containment->scene());
00134 setContainment(containment);
00135 }
00136 }
00137
00138 View::~View()
00139 {
00140 delete d;
00141
00142
00143 clearFocus();
00144 }
00145
00146 void View::setScreen(int screen, int desktop)
00147 {
00148 if (screen > -1) {
00149 Corona *corona = qobject_cast<Corona*>(scene());
00150
00151 if (!corona) {
00152 return;
00153 }
00154
00155
00156 if (desktop < -1 || desktop > KWindowSystem::numberOfDesktops() - 1) {
00157 desktop = -1;
00158 }
00159
00160 Containment *containment = corona->containmentForScreen(screen, desktop);
00161 if (containment) {
00162 d->containment = 0;
00163 d->lastScreen = screen;
00164 d->lastDesktop = desktop;
00165 setContainment(containment);
00166 }
00167 }
00168 }
00169
00170 int View::screen() const
00171 {
00172 return d->lastScreen;
00173 }
00174
00175 int View::desktop() const
00176 {
00177 if (d->containment) {
00178 return d->containment->desktop();
00179 }
00180
00181 return d->lastDesktop;
00182 }
00183
00184 int View::effectiveDesktop() const
00185 {
00186 int desk = desktop();
00187 return desk > -1 ? desk : KWindowSystem::currentDesktop();
00188 }
00189
00190 void View::setContainment(Plasma::Containment *containment)
00191 {
00192 if (containment == d->containment) {
00193 return;
00194 }
00195
00196 if (d->containment) {
00197 disconnect(d->containment, SIGNAL(destroyed(QObject*)), this, SLOT(containmentDestroyed()));
00198 disconnect(d->containment, SIGNAL(geometryChanged()), this, SLOT(updateSceneRect()));
00199 d->containment->removeAssociatedWidget(this);
00200 }
00201
00202 if (!containment) {
00203 d->containment = 0;
00204 return;
00205 }
00206
00207 Containment *oldContainment = d->containment;
00208
00209 int screen = -1;
00210 int desktop = -1;
00211 if (oldContainment) {
00212 screen = d->containment->screen();
00213 desktop = d->containment->desktop();
00214 } else {
00215 setScene(containment->scene());
00216 }
00217
00218 d->containment = containment;
00219
00220
00221 d->containment->addAssociatedWidget(this);
00222
00223 int otherScreen = containment->screen();
00224 int otherDesktop = containment->desktop();
00225
00226 if (screen > -1) {
00227 d->lastScreen = screen;
00228 d->lastDesktop = desktop;
00229 containment->setScreen(screen, desktop);
00230 } else {
00231 d->lastScreen = otherScreen;
00232 d->lastDesktop = otherDesktop;
00233 }
00234
00235 if (oldContainment && otherScreen > -1) {
00236
00237 oldContainment->setScreen(otherScreen, otherDesktop);
00238 }
00239
00240
00241
00242
00243
00244
00245
00246
00247 d->updateSceneRect();
00248 connect(containment, SIGNAL(destroyed(QObject*)), this, SLOT(containmentDestroyed()));
00249 connect(containment, SIGNAL(geometryChanged()), this, SLOT(updateSceneRect()));
00250 }
00251
00252 Containment *View::containment() const
00253 {
00254 return d->containment;
00255 }
00256
00257 Containment *View::swapContainment(const QString &name, const QVariantList &args)
00258 {
00259 return swapContainment(d->containment, name, args);
00260 }
00261
00262 Containment *View::swapContainment(Plasma::Containment *existing, const QString &name, const QVariantList &args)
00263 {
00264 if (!existing) {
00265 return 0;
00266 }
00267
00268 Containment *old = existing;
00269 Plasma::Corona *corona = old->corona();
00270 Plasma::Containment *c = corona->addContainment(name, args);
00271 if (c) {
00272 KConfigGroup oldConfig = old->config();
00273 KConfigGroup newConfig = c->config();
00274
00275
00276 old->save(oldConfig);
00277
00278
00279 oldConfig.copyTo(&newConfig);
00280
00281 if (old == d->containment) {
00282
00283 setContainment(c);
00284 }
00285
00286
00287 c->restore(newConfig);
00288 foreach (Applet *applet, c->applets()) {
00289 applet->init();
00290
00291 applet->flushPendingConstraintsEvents();
00292 }
00293
00294
00295 old->destroy(false);
00296
00297
00298 c->save(newConfig);
00299 corona->requestConfigSync();
00300
00301 return c;
00302 }
00303
00304 return old;
00305 }
00306
00307 KConfigGroup View::config() const
00308 {
00309 KConfigGroup views(KGlobal::config(), "PlasmaViews");
00310 return KConfigGroup(&views, QString::number(d->viewId));
00311 }
00312
00313 int View::id() const
00314 {
00315 return d->viewId;
00316 }
00317
00318 void View::setWallpaperEnabled(bool draw)
00319 {
00320 d->drawWallpaper = draw;
00321 }
00322
00323 bool View::isWallpaperEnabled() const
00324 {
00325 return d->drawWallpaper;
00326 }
00327
00328 void View::setTrackContainmentChanges(bool trackChanges)
00329 {
00330 d->trackChanges = trackChanges;
00331 }
00332
00333 bool View::trackContainmentChanges()
00334 {
00335 return d->trackChanges;
00336 }
00337
00338 View * View::topLevelViewAt(const QPoint & pos)
00339 {
00340 QWidget *w = QApplication::topLevelAt(pos);
00341 if (w) {
00342 Plasma::View *v = qobject_cast<Plasma::View *>(w);
00343 return v;
00344 } else {
00345 return 0;
00346 }
00347 }
00348
00349 }
00350
00351 #include "view.moc"
00352