• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

Plasma

view.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright 2007 Aaron Seigo <aseigo@kde.org>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License as
00006  *   published by the Free Software Foundation; either version 2, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details
00013  *
00014  *   You should have received a copy of the GNU Library General Public
00015  *   License along with this program; if not, write to the
00016  *   Free Software Foundation, Inc.,
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  */
00019 
00020 #include "view.h"
00021 
00022 #include <QTimer>
00023 
00024 #include <kglobal.h>
00025 #include <kwindowsystem.h>
00026 #include <kactioncollection.h>
00027 
00028 #include "corona.h"
00029 #include "containment.h"
00030 #include "wallpaper.h"
00031 
00032 using namespace Plasma;
00033 
00034 namespace Plasma
00035 {
00036 
00037 class ViewPrivate
00038 {
00039 public:
00040     ViewPrivate(View *view, int uniqueId)
00041         : q(view),
00042           containment(0),
00043           viewId(0),
00044           lastScreen(-1),
00045           lastDesktop(-2),
00046           drawWallpaper(true),
00047           trackChanges(true),
00048           init(false)
00049     {
00050         if (uniqueId > s_maxViewId) {
00051             s_maxViewId = uniqueId;
00052             viewId = uniqueId;
00053         }
00054 
00055         if (viewId == 0) {
00056             // we didn't get a sane value assigned to us, so lets
00057             // grab the next available id
00058             viewId = ++s_maxViewId;
00059         }
00060     }
00061 
00062     ~ViewPrivate()
00063     {
00064     }
00065 
00066     void privateInit()
00067     {
00068         q->setContainment(containment);
00069         init = true;
00070     }
00071 
00072     void updateSceneRect()
00073     {
00074         if (!containment || !trackChanges) {
00075             return;
00076         }
00077 
00078         kDebug() << "!!!!!!!!!!!!!!!!! setting the scene rect to"
00079                  << containment->sceneBoundingRect()
00080                  << "associated screen is" << containment->screen();
00081 
00082         emit q->sceneRectAboutToChange();
00083         if (q->transform().isIdentity()) { //we're not zoomed out
00084             q->setSceneRect(containment->sceneBoundingRect());
00085         } else {
00086             //kDebug() << "trying to show the containment nicely";
00087             q->ensureVisible(containment->sceneBoundingRect());
00088             //q->centerOn(containment);
00089         }
00090         emit q->sceneRectChanged();
00091     }
00092 
00093     void containmentDestroyed()
00094     {
00095         containment = 0;
00096     }
00097 
00098     void containmentScreenChanged(int wasScreen, int newScreen, Plasma::Containment *containment)
00099     {
00100         Q_UNUSED(wasScreen)
00101         lastScreen = newScreen;
00102         lastDesktop = containment->desktop();
00103     }
00104 
00105     void initGraphicsView()
00106     {
00107         q->setFrameShape(QFrame::NoFrame);
00108         q->setAttribute(Qt::WA_TranslucentBackground);
00109         q->setAutoFillBackground(true);
00110         q->setDragMode(QGraphicsView::NoDrag);
00111         //setCacheMode(QGraphicsView::CacheBackground);
00112         q->setInteractive(true);
00113         q->setAcceptDrops(true);
00114         q->setAlignment(Qt::AlignLeft | Qt::AlignTop);
00115         q->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00116         q->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00117     }
00118 
00119     static int s_maxViewId;
00120 
00121     Plasma::View *q;
00122     Plasma::Containment *containment;
00123     int viewId;
00124     int lastScreen;
00125     int lastDesktop;
00126     bool drawWallpaper : 1;
00127     bool trackChanges : 1;
00128     bool init : 1;
00129 };
00130 
00131 int ViewPrivate::s_maxViewId(0);
00132 
00133 View::View(Containment *containment, QWidget *parent)
00134     : QGraphicsView(parent),
00135       d(new ViewPrivate(this, 0))
00136 {
00137     d->initGraphicsView();
00138 
00139     if (containment) {
00140         setScene(containment->scene());
00141         d->containment = containment;
00142         QTimer::singleShot(0, this, SLOT(privateInit()));
00143     }
00144 }
00145 
00146 View::View(Containment *containment, int viewId, QWidget *parent)
00147     : QGraphicsView(parent),
00148       d(new ViewPrivate(this, viewId))
00149 {
00150     d->initGraphicsView();
00151 
00152     if (containment) {
00153         setScene(containment->scene());
00154         d->containment = containment;
00155         QTimer::singleShot(0, this, SLOT(privateInit()));
00156     }
00157 }
00158 
00159 View::~View()
00160 {
00161     delete d;
00162     // FIXME FIX a focus crash but i wasn't able to reproduce in a simple test case for Qt guys
00163     //       NB: this is also done in Corona
00164     clearFocus();
00165 }
00166 
00167 void View::setScreen(int screen, int desktop)
00168 {
00169     if (screen > -1) {
00170         Corona *corona = qobject_cast<Corona*>(scene());
00171 
00172         if (!corona) {
00173             return;
00174         }
00175 
00176         // -1 == All desktops
00177         if (desktop < -1 || desktop > KWindowSystem::numberOfDesktops() - 1) {
00178             desktop = -1;
00179         }
00180 
00181         Containment *containment = corona->containmentForScreen(screen, desktop);
00182         if (containment) {
00183             d->containment = 0; //so that we don't end up on the old containment's screen
00184             d->lastScreen = screen;
00185             d->lastDesktop = desktop;
00186             setContainment(containment);
00187         }
00188     }
00189 }
00190 
00191 int View::screen() const
00192 {
00193     return d->lastScreen;
00194 }
00195 
00196 int View::desktop() const
00197 {
00198     if (d->containment) {
00199         return d->containment->desktop();
00200     }
00201 
00202     return d->lastDesktop;
00203 }
00204 
00205 int View::effectiveDesktop() const
00206 {
00207     int desk = desktop();
00208     return desk > -1 ? desk : KWindowSystem::currentDesktop();
00209 }
00210 
00211 void View::setContainment(Plasma::Containment *containment)
00212 {
00213     if (d->init && containment == d->containment) {
00214         return;
00215     }
00216 
00217     if (d->containment) {
00218         disconnect(d->containment, SIGNAL(destroyed(QObject*)), this, SLOT(containmentDestroyed()));
00219         disconnect(d->containment, SIGNAL(geometryChanged()), this, SLOT(updateSceneRect()));
00220         disconnect(d->containment, SIGNAL(screenChanged(int, int, Plasma::Containment *)), this, SLOT(containmentScreenChanged(int, int, Plasma::Containment *)));
00221         d->containment->removeAssociatedWidget(this);
00222     }
00223 
00224     if (!containment) {
00225         d->containment = 0;
00226         return;
00227     }
00228 
00229     Containment *oldContainment = d->containment;
00230 
00231     int screen = -1;
00232     int desktop = -1;
00233     if (oldContainment) {
00234         if (oldContainment != containment) {
00235             screen = d->containment->screen();
00236             desktop = d->containment->desktop();
00237         }
00238     } else {
00239         setScene(containment->scene());
00240     }
00241 
00242     d->containment = containment;
00243 
00244     //add keyboard-shortcut actions
00245     d->containment->addAssociatedWidget(this);
00246 
00247     int otherScreen = containment->screen();
00248     int otherDesktop = containment->desktop();
00249 
00250     if (screen > -1) {
00251         d->lastScreen = screen;
00252         d->lastDesktop = desktop;
00253         //kDebug() << "set screen from setContainment due to old containment";
00254         oldContainment->setScreen(-1, -1);
00255         containment->setScreen(-1, -1);
00256         containment->setScreen(screen, desktop);
00257     } else {
00258         d->lastScreen = otherScreen;
00259         d->lastDesktop = otherDesktop;
00260     }
00261 
00262     if (oldContainment && oldContainment != d->containment && otherScreen > -1) {
00263         // assign the old containment the old screen/desktop
00264         //kDebug() << "set screen from setContainment" << screen << otherScreen << desktop << otherDesktop;
00265         oldContainment->setScreen(otherScreen, otherDesktop);
00266     }
00267 
00268 
00269     /*
00270     if (oldContainment) {
00271         kDebug() << "old" << (QObject*)oldContainment << screen << oldContainment->screen()
00272                  << "new" << (QObject*)containment << otherScreen << containment->screen();
00273     }
00274     */
00275 
00276     d->updateSceneRect();
00277     connect(containment, SIGNAL(destroyed(QObject*)), this, SLOT(containmentDestroyed()));
00278     connect(containment, SIGNAL(geometryChanged()), this, SLOT(updateSceneRect()));
00279     connect(containment, SIGNAL(screenChanged(int, int, Plasma::Containment *)), this, SLOT(containmentScreenChanged(int, int, Plasma::Containment *)));
00280 }
00281 
00282 Containment *View::containment() const
00283 {
00284     return d->containment;
00285 }
00286 
00287 Containment *View::swapContainment(const QString &name, const QVariantList &args)
00288 {
00289     return swapContainment(d->containment, name, args);
00290 }
00291 
00292 Containment *View::swapContainment(Plasma::Containment *existing, const QString &name, const QVariantList &args)
00293 {
00294     if (!existing) {
00295         return 0;
00296     }
00297 
00298     Containment *old = existing;
00299     Plasma::Corona *corona = old->corona();
00300     Plasma::Containment *c = corona->addContainment(name, args);
00301     if (c) {
00302         KConfigGroup oldConfig = old->config();
00303         KConfigGroup newConfig = c->config();
00304 
00305         // ensure that the old containments configuration is up to date
00306         old->save(oldConfig);
00307 
00308         // Copy configuration to new containment
00309         oldConfig.copyTo(&newConfig);
00310 
00311         if (old == d->containment) {
00312             // set our containment to the new one, if the the old containment was us
00313             setContainment(c);
00314         }
00315 
00316         // load the configuration of the old containment into the new one
00317         c->restore(newConfig);
00318         foreach (Applet *applet, c->applets()) {
00319             applet->init();
00320             // We have to flush the applet constraints manually
00321             applet->flushPendingConstraintsEvents();
00322         }
00323 
00324         // destroy the old one
00325         old->destroy(false);
00326 
00327         // and now save the config
00328         c->save(newConfig);
00329         corona->requestConfigSync();
00330 
00331         return c;
00332     }
00333 
00334     return old;
00335 }
00336 
00337 KConfigGroup View::config() const
00338 {
00339     KConfigGroup views(KGlobal::config(), "PlasmaViews");
00340     return KConfigGroup(&views, QString::number(d->viewId));
00341 }
00342 
00343 void View::configNeedsSaving() const
00344 {
00345     Plasma::Corona *corona = qobject_cast<Plasma::Corona*>(scene());
00346     if (corona) {
00347         corona->requestConfigSync();
00348     } else {
00349         KGlobal::config()->sync();
00350     }
00351 }
00352 
00353 int View::id() const
00354 {
00355     return d->viewId;
00356 }
00357 
00358 void View::setWallpaperEnabled(bool draw)
00359 {
00360     d->drawWallpaper = draw;
00361 }
00362 
00363 bool View::isWallpaperEnabled() const
00364 {
00365     return d->drawWallpaper;
00366 }
00367 
00368 void View::setTrackContainmentChanges(bool trackChanges)
00369 {
00370     d->trackChanges = trackChanges;
00371 }
00372 
00373 bool View::trackContainmentChanges()
00374 {
00375     return d->trackChanges;
00376 }
00377 
00378 View * View::topLevelViewAt(const QPoint & pos)
00379 {
00380     QWidget *w = QApplication::topLevelAt(pos);
00381     if (w) {
00382         Plasma::View *v = qobject_cast<Plasma::View *>(w);
00383         return v;
00384     } else {
00385         return 0;
00386     }
00387 }
00388 
00389 } // namespace Plasma
00390 
00391 #include "view.moc"
00392 

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal