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

Plasma

dashboardview.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright 2007 Aaron Seigo <aseigo@kde.org>
00003  *   Copyright 2007 Matt Broadstone <mbroadst@gmail.com>
00004  *   Copyright 2007 André Duffeck <duffeck@kde.org>
00005  *
00006  *   This program is free software; you can redistribute it and/or modify
00007  *   it under the terms of the GNU Library General Public License as
00008  *   published by the Free Software Foundation; either version 2, or
00009  *   (at your option) any later version.
00010  *
00011  *   This program is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *   GNU General Public License for more details
00015  *
00016  *   You should have received a copy of the GNU Library General Public
00017  *   License along with this program; if not, write to the
00018  *   Free Software Foundation, Inc.,
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00020  */
00021 
00022 #include "dashboardview.h"
00023 
00024 #include <QAction>
00025 #include <QKeyEvent>
00026 #include <QTimer>
00027 
00028 #include <KWindowSystem>
00029 
00030 #include <Plasma/Applet>
00031 #include <Plasma/Corona>
00032 #include <Plasma/Containment>
00033 #include <Plasma/Svg>
00034 #include "plasmaapp.h"
00035 
00036 #include <kephal/screens.h>
00037 
00038 #include "appletbrowser.h"
00039 
00040 static const int SUPPRESS_SHOW_TIMEOUT = 500; // Number of millis to prevent reshow of dashboard
00041 
00042 DashboardView::DashboardView(Plasma::Containment *containment, QWidget *parent)
00043     : Plasma::View(containment, parent),
00044       m_appletBrowser(0),
00045       m_suppressShow(false),
00046       m_zoomIn(false),
00047       m_zoomOut(false)
00048 {
00049     //setContextMenuPolicy(Qt::NoContextMenu);
00050     setWindowFlags(Qt::FramelessWindowHint);
00051     if (!PlasmaApp::hasComposite()) {
00052         setAutoFillBackground(false);
00053         setAttribute(Qt::WA_NoSystemBackground);
00054     }
00055 
00056     setGeometry(Kephal::ScreenUtils::screenGeometry(containment->screen()));
00057 
00058     setWallpaperEnabled(!PlasmaApp::hasComposite());
00059 
00060     connect(scene(), SIGNAL(releaseVisualFocus()), SLOT(hideView()));
00061 
00062     m_hideAction = new QAction(i18n("Hide Dashboard"), this);
00063     m_hideAction->setIcon(KIcon("preferences-desktop-display"));
00064     m_hideAction->setEnabled(false);
00065     containment->addToolBoxAction(m_hideAction);
00066     connect(m_hideAction, SIGNAL(triggered()), this, SLOT(hideView()));
00067 
00068     installEventFilter(this);
00069 }
00070 
00071 DashboardView::~DashboardView()
00072 {
00073     delete m_appletBrowser;
00074 }
00075 
00076 void DashboardView::drawBackground(QPainter * painter, const QRectF & rect)
00077 {
00078     if (PlasmaApp::hasComposite()) {
00079         setWallpaperEnabled(false);
00080         painter->setCompositionMode(QPainter::CompositionMode_Source);
00081         painter->fillRect(rect, QColor(0, 0, 0, 180));
00082     } else {
00083         setWallpaperEnabled(true);
00084         Plasma::View::drawBackground(painter, rect);
00085     }
00086 }
00087 
00088 void DashboardView::paintEvent(QPaintEvent *event)
00089 {
00090     Plasma::View::paintEvent(event);
00091 
00092     // now draw a little label saying "this is your friendly neighbourhood dashboard"
00093     const QRect r = rect();
00094     const QString text = i18n("Widget Dashboard");
00095     QFont f = font();
00096     f.bold();
00097     const QFontMetrics fm(f);
00098     const int margin = 6;
00099     const int textWidth = fm.width(text);
00100     const QPoint centered(r.width() / 2 - textWidth / 2 - margin, r.y());
00101     const QRect boundingBox(centered, QSize(margin * 2 + textWidth, fm.height() + margin * 2));
00102 
00103     if (!viewport() || !event->rect().intersects(boundingBox)) {
00104         return;
00105     }
00106 
00107     QPainterPath box;
00108     box.moveTo(boundingBox.topLeft());
00109     box.lineTo(boundingBox.bottomLeft() + QPoint(0, -margin * 2));
00110     box.quadTo(boundingBox.bottomLeft(), boundingBox.bottomLeft() + QPoint(margin * 2, 0));
00111     box.lineTo(boundingBox.bottomRight() + QPoint(-margin * 2, 0));
00112     box.quadTo(boundingBox.bottomRight(), boundingBox.bottomRight() + QPoint(0, -margin * 2));
00113     box.lineTo(boundingBox.topRight());
00114     box.closeSubpath();
00115 
00116     QPainter painter(viewport());
00117     painter.setRenderHint(QPainter::Antialiasing);
00118     painter.setFont(f);
00119     //kDebug() << "******************** painting from" << centered << boundingBox << rect() << event->rect();
00120     QColor highlight = palette().highlight().color();
00121     highlight.setAlphaF(0.7);
00122     painter.setPen(highlight.darker());
00123     painter.setBrush(highlight);
00124     painter.drawPath(box);
00125     painter.setPen(palette().highlightedText().color());
00126     painter.drawText(boundingBox, Qt::AlignCenter | Qt::AlignVCenter, text);
00127 }
00128 
00129 void DashboardView::showAppletBrowser()
00130 {
00131     if (!containment()) {
00132         return;
00133     }
00134 
00135     if (!m_appletBrowser) {
00136         m_appletBrowser = new Plasma::AppletBrowser(this, Qt::FramelessWindowHint );
00137         m_appletBrowser->setContainment(containment());
00138         //TODO: make this proportional to the screen
00139         m_appletBrowser->setInitialSize(QSize(400, 400));
00140         m_appletBrowser->setApplication();
00141         m_appletBrowser->setWindowTitle(i18n("Add Widgets"));
00142         QPalette p = m_appletBrowser->palette();
00143         p.setBrush(QPalette::Background, QBrush(QColor(0, 0, 0, 180)));
00144         m_appletBrowser->setPalette(p);
00145         m_appletBrowser->setBackgroundRole(QPalette::Background);
00146         m_appletBrowser->setAutoFillBackground(true);
00147         KWindowSystem::setState(m_appletBrowser->winId(), NET::KeepAbove|NET::SkipTaskbar);
00148         m_appletBrowser->move(0, 0);
00149         m_appletBrowser->installEventFilter(this);
00150     }
00151 
00152     m_appletBrowser->setHidden(m_appletBrowser->isVisible());
00153 }
00154 
00155 void DashboardView::appletBrowserDestroyed()
00156 {
00157     m_appletBrowser = 0;
00158 }
00159 
00160 bool DashboardView::eventFilter(QObject *watched, QEvent *event)
00161 {
00162     if (watched != m_appletBrowser) {
00163         return false;
00164     }
00165 
00166     if (event->type() == QEvent::MouseButtonPress) {
00167         QMouseEvent *me = static_cast<QMouseEvent *>(event);
00168         m_appletBrowserDragStart = me->globalPos();
00169     } else if (event->type() == QEvent::MouseMove && m_appletBrowserDragStart != QPoint()) {
00170         const QMouseEvent *me = static_cast<QMouseEvent *>(event);
00171         const QPoint newPos = me->globalPos();
00172         const QPoint curPos = m_appletBrowser->pos();
00173         int x = curPos.x();
00174         int y = curPos.y();
00175 
00176         if (curPos.y() == 0 || curPos.y() + m_appletBrowser->height() >= height()) {
00177            x = curPos.x() + (newPos.x() - m_appletBrowserDragStart.x());
00178            if (x < 0) {
00179                x = 0;
00180            } else if (x + m_appletBrowser->width() > width()) {
00181                x = width() - m_appletBrowser->width();
00182            }
00183         }
00184 
00185         if (x == 0 || x + m_appletBrowser->width() >= width()) {
00186             y = curPos.y() + (newPos.y() - m_appletBrowserDragStart.y());
00187 
00188             if (y < 0) {
00189                 y = 0;
00190             } else if (y + m_appletBrowser->height() > height()) {
00191                 y = height() - m_appletBrowser->height();
00192             }
00193         }
00194 
00195         m_appletBrowser->move(x, y);
00196         m_appletBrowserDragStart = newPos;
00197     } else if (event->type() == QEvent::MouseButtonRelease) {
00198         m_appletBrowserDragStart = QPoint();
00199     }
00200 
00201     return false;
00202 }
00203 
00204 bool DashboardView::event(QEvent *event)
00205 {
00206     if (event->type() == QEvent::Paint) {
00207         QPainter p(this);
00208         p.setCompositionMode(QPainter::CompositionMode_Source);
00209         p.fillRect(rect(), Qt::transparent);
00210     }
00211 
00212     return Plasma::View::event(event);
00213 }
00214 
00215 void DashboardView::toggleVisibility()
00216 {
00217     if (isHidden() && containment()) {
00218         if (m_suppressShow) {
00219             kDebug() << "DashboardView::toggleVisibility but show was suppressed";
00220             return;
00221         }
00222 
00223         setWindowState(Qt::WindowFullScreen);
00224         KWindowSystem::setOnAllDesktops(winId(), true);
00225         KWindowSystem::setState(winId(), NET::KeepAbove|NET::SkipTaskbar);
00226 
00227         QAction *action = containment()->action("zoom out");
00228         m_zoomOut = action ? action->isEnabled() : false;
00229         action = containment()->action("zoom in");
00230         m_zoomIn = action ? action->isEnabled() : false;
00231 
00232         m_hideAction->setEnabled(true);
00233         containment()->enableAction("zoom out", false);
00234         containment()->enableAction("zoom in", false);
00235 
00236         show();
00237         raise();
00238 
00239         m_suppressShow = true;
00240         QTimer::singleShot(SUPPRESS_SHOW_TIMEOUT, this, SLOT(suppressShowTimeout()));
00241         containment()->openToolBox();
00242     } else {
00243         hideView();
00244     }
00245 }
00246 
00247 void DashboardView::setContainment(Plasma::Containment *newContainment)
00248 {
00249     if (!newContainment || newContainment == containment()) {
00250         return;
00251     }
00252 
00253     Plasma::Containment *oldContainment = containment();
00254     if (oldContainment) {
00255         oldContainment->removeToolBoxAction(m_hideAction);
00256     }
00257     newContainment->addToolBoxAction(m_hideAction);
00258 
00259     if (isVisible()) {
00260         if (oldContainment) {
00261             disconnect(oldContainment, SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showAppletBrowser()));
00262             oldContainment->closeToolBox();
00263             oldContainment->enableAction("zoom out", m_zoomOut);
00264             oldContainment->enableAction("zoom in", m_zoomIn);
00265         }
00266 
00267         connect(newContainment, SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showAppletBrowser()));
00268         QAction *action = newContainment->action("zoom out");
00269         m_zoomOut = action ? action->isEnabled() : false;
00270         action = newContainment->action("zoom in");
00271         m_zoomIn = action ? action->isEnabled() : false;
00272         newContainment->enableAction("zoom out", false);
00273         newContainment->enableAction("zoom in", false);
00274         newContainment->openToolBox();
00275     }
00276 
00277     if (m_appletBrowser) {
00278         m_appletBrowser->setContainment(newContainment);
00279     }
00280 
00281     View::setContainment(0); // we don't actually to mess with the screen settings
00282     View::setContainment(newContainment);
00283 }
00284 
00285 void DashboardView::hideView()
00286 {
00287     if (m_appletBrowser) {
00288         m_appletBrowser->hide();
00289     }
00290 
00291 #ifndef Q_WS_WIN
00292     disconnect(KWindowSystem::self(), SIGNAL(activeWindowChanged(WId)), this, SLOT(activeWindowChanged(WId)));
00293 #endif
00294 
00295     if (containment()) {
00296         disconnect(containment(), SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showAppletBrowser()));
00297 
00298         containment()->closeToolBox();
00299         containment()->enableAction("zoom out", m_zoomOut);
00300         containment()->enableAction("zoom in", m_zoomIn);
00301     }
00302 
00303     m_hideAction->setEnabled(false);
00304     hide();
00305 }
00306 
00307 void DashboardView::suppressShowTimeout()
00308 {
00309     kDebug() << "DashboardView::suppressShowTimeout";
00310     m_suppressShow = false;
00311 }
00312 
00313 void DashboardView::keyPressEvent(QKeyEvent *event)
00314 {
00315     if (event->key() == Qt::Key_Escape) {
00316         hideView();
00317         event->accept();
00318         return;
00319     }
00320 
00321     Plasma::View::keyPressEvent(event);
00322 }
00323 
00324 void DashboardView::activeWindowChanged(WId id)
00325 {
00326     if (id != winId() && (!m_appletBrowser || id != m_appletBrowser->winId()) && find(id) != 0) {
00327         hideView();
00328     }
00329 }
00330 
00331 void DashboardView::showEvent(QShowEvent *event)
00332 {
00333     KWindowSystem::setState(winId(), NET::SkipPager);
00334 #ifndef Q_WS_WIN
00335     connect(KWindowSystem::self(), SIGNAL(activeWindowChanged(WId)), this, SLOT(activeWindowChanged(WId)));
00336 #endif
00337     if (containment()) {
00338         connect(containment(), SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showAppletBrowser()));
00339     }
00340     Plasma::View::showEvent(event);
00341 }
00342 
00343 #include "dashboardview.moc"
00344 

Plasma

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

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference 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