• Skip to content
  • Skip to link menu
KDE 4.2 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 <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             // we didn't get a sane value assigned to us, so lets
00054             // grab the next available id
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()) { //we're not zoomed out
00075             q->setSceneRect(containment->sceneBoundingRect());
00076         } else {
00077             //kDebug() << "trying to show the containment nicely";
00078             q->ensureVisible(containment->sceneBoundingRect());
00079             //q->centerOn(containment);
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         //setCacheMode(QGraphicsView::CacheBackground);
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     // FIXME FIX a focus crash but i wasn't able to reproduce in a simple test case for Qt guys
00142     //       NB: this is also done in Corona
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         // -1 == All desktops
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; //so that we don't end up on the old containment's screen
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     //add keyboard-shortcut actions
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         // assign the old containment the old screen/desktop
00237         oldContainment->setScreen(otherScreen, otherDesktop);
00238     }
00239 
00240     /*
00241     if (oldContainment) {
00242         kDebug() << "old" << (QObject*)oldContainment << screen << oldContainment->screen()
00243                  << "new" << (QObject*)containment << otherScreen << containment->screen();
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         // ensure that the old containments configuration is up to date
00276         old->save(oldConfig);
00277 
00278         // Copy configuration to new containment
00279         oldConfig.copyTo(&newConfig);
00280 
00281         if (old == d->containment) {
00282             // set our containment to the new one, if the the old containment was us
00283             setContainment(c);
00284         }
00285 
00286         // load the configuration of the old containment into the new one
00287         c->restore(newConfig);
00288         foreach (Applet *applet, c->applets()) {
00289             applet->init();
00290             // We have to flush the applet constraints manually
00291             applet->flushPendingConstraintsEvents();
00292         }
00293 
00294         // destroy the old one
00295         old->destroy(false);
00296 
00297         // and now save the config
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 } // namespace Plasma
00350 
00351 #include "view.moc"
00352 

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
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
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