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

Plasma

saverview.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  *   Copyright 2008 Chani Armitage <chanika@gmail.com>
00006  *
00007  *   This program is free software; you can redistribute it and/or modify
00008  *   it under the terms of the GNU Library General Public License as
00009  *   published by the Free Software Foundation; either version 2, or
00010  *   (at your option) any later version.
00011  *
00012  *   This program is distributed in the hope that it will be useful,
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *   GNU General Public License for more details
00016  *
00017  *   You should have received a copy of the GNU Library General Public
00018  *   License along with this program; if not, write to the
00019  *   Free Software Foundation, Inc.,
00020  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00021  */
00022 
00023 #include "saverview.h"
00024 
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 
00035 #include "appletbrowser.h"
00036 #include "plasmaapp.h"
00037 
00038 static const int SUPPRESS_SHOW_TIMEOUT = 500; // Number of millis to prevent reshow of dashboard
00039 
00040 SaverView::SaverView(Plasma::Containment *containment, QWidget *parent)
00041     : Plasma::View(containment, parent),
00042       m_appletBrowser(0),
00043       m_suppressShow(false),
00044       m_setupMode(false)
00045 {
00046     setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint |
00047             Qt::X11BypassWindowManagerHint);
00048     if (!PlasmaApp::hasComposite()) {
00049         setAutoFillBackground(false);
00050         setAttribute(Qt::WA_NoSystemBackground);
00051     }
00052 
00053     //app is doing this for us - if needed
00054     //QDesktopWidget *desktop = QApplication::desktop();
00055     //setGeometry(desktop->screenGeometry(containment->screen()));
00056 
00057     setWallpaperEnabled(!PlasmaApp::hasComposite());
00058 
00059     installEventFilter(this);
00060 }
00061 
00062 SaverView::~SaverView()
00063 {
00064     delete m_appletBrowser;
00065 }
00066 
00067 void SaverView::enableSetupMode()
00068 {
00069     if (!m_setupMode) {
00070         m_setupMode = true;
00071         update();
00072     }
00073 }
00074 
00075 void SaverView::disableSetupMode()
00076 {
00077     if (m_setupMode) {
00078         m_setupMode = false;
00079         update();
00080     }
00081 }
00082 
00083 void SaverView::drawBackground(QPainter * painter, const QRectF & rect)
00084 {
00085     if (PlasmaApp::hasComposite()) {
00086         painter->setCompositionMode(QPainter::CompositionMode_Source);
00087         painter->fillRect(rect, QColor(0, 0, 0, 0));
00088         //FIXME kwin's shadow effect is getting drawn behind me. do not want.
00089     } else {
00090         Plasma::View::drawBackground(painter, rect);
00091     }
00092 }
00093 
00094 void SaverView::showAppletBrowser()
00095 {
00096     if (!m_appletBrowser) {
00097         m_appletBrowser = new Plasma::AppletBrowser(this, Qt::FramelessWindowHint );
00098         m_appletBrowser->setContainment(containment());
00099         //TODO: make this proportional to the screen
00100         m_appletBrowser->setInitialSize(QSize(400, 400));
00101         m_appletBrowser->setApplication();
00102         m_appletBrowser->setWindowTitle(i18n("Add Widgets"));
00103         QPalette p = m_appletBrowser->palette();
00104         p.setBrush(QPalette::Background, QBrush(QColor(0, 0, 0, 180)));
00105         m_appletBrowser->setPalette(p);
00106         m_appletBrowser->setBackgroundRole(QPalette::Background);
00107         m_appletBrowser->setAutoFillBackground(true);
00108         m_appletBrowser->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint |
00109                 Qt::X11BypassWindowManagerHint);
00110         //KWindowSystem::setState(m_appletBrowser->winId(), NET::KeepAbove|NET::SkipTaskbar);
00111         m_appletBrowser->move(0, 0);
00112         m_appletBrowser->installEventFilter(this);
00113     }
00114 
00115     //TODO give the filter kbd focus
00116     m_appletBrowser->setHidden(m_appletBrowser->isVisible());
00117 }
00118 
00119 void SaverView::hideAppletBrowser()
00120 {
00121     if (m_appletBrowser) {
00122         m_appletBrowser->hide();
00123     }
00124 }
00125 
00126 void SaverView::appletBrowserDestroyed()
00127 {
00128     m_appletBrowser = 0;
00129 }
00130 
00131 void SaverView::paintEvent(QPaintEvent *event)
00132 {
00133     Plasma::View::paintEvent(event);
00134     if (!m_setupMode) {
00135         return;
00136     }
00137 
00138     // now draw a little label reminding the user their screen's not quite locked
00139     const QRect r = rect();
00140     const QString text = i18n("Setup Mode - Screen is NOT locked");
00141     QFont f = font();
00142     f.bold();
00143     const QFontMetrics fm(f);
00144     const int margin = 6;
00145     const int textWidth = fm.width(text);
00146     const QPoint centered(r.width() / 2 - textWidth / 2 - margin, r.y());
00147     const QRect boundingBox(centered, QSize(margin * 2 + textWidth, fm.height() + margin * 2));
00148 
00149     if (!viewport() || !event->rect().intersects(boundingBox)) {
00150         return;
00151     }
00152 
00153     QPainterPath box;
00154     box.moveTo(boundingBox.topLeft());
00155     box.lineTo(boundingBox.bottomLeft() + QPoint(0, -margin * 2));
00156     box.quadTo(boundingBox.bottomLeft(), boundingBox.bottomLeft() + QPoint(margin * 2, 0));
00157     box.lineTo(boundingBox.bottomRight() + QPoint(-margin * 2, 0));
00158     box.quadTo(boundingBox.bottomRight(), boundingBox.bottomRight() + QPoint(0, -margin * 2));
00159     box.lineTo(boundingBox.topRight());
00160     box.closeSubpath();
00161 
00162     QPainter painter(viewport());
00163     painter.setRenderHint(QPainter::Antialiasing);
00164     painter.setFont(f);
00165     //kDebug() << "******************** painting from" << centered << boundingBox << rect() << event->rect();
00166     QColor highlight = palette().highlight().color();
00167     highlight.setAlphaF(0.7);
00168     painter.setPen(highlight.darker());
00169     painter.setBrush(highlight);
00170     painter.drawPath(box);
00171     painter.setPen(palette().highlightedText().color());
00172     painter.drawText(boundingBox, Qt::AlignCenter | Qt::AlignVCenter, text);
00173 }
00174 
00175 bool SaverView::eventFilter(QObject *watched, QEvent *event)
00176 {
00177     if (watched != m_appletBrowser) {
00178         /*if (event->type() == QEvent::MouseButtonPress) {
00179             QMouseEvent *me = static_cast<QMouseEvent *>(event);
00180             if (me->button() == Qt::LeftButton) {
00181                 hideView();
00182             }
00183         }*/
00184         return false;
00185     }
00186 
00187     if (event->type() == QEvent::MouseButtonPress) {
00188         QMouseEvent *me = static_cast<QMouseEvent *>(event);
00189         m_appletBrowserDragStart = me->globalPos();
00190     } else if (event->type() == QEvent::MouseMove && m_appletBrowserDragStart != QPoint()) {
00191         QMouseEvent *me = static_cast<QMouseEvent *>(event);
00192         QPoint newPos = me->globalPos();
00193         QPoint curPos = m_appletBrowser->pos();
00194         int x = curPos.x();
00195         int y = curPos.y();
00196 
00197         if (curPos.y() == 0 || curPos.y() + m_appletBrowser->height() >= height()) {
00198            x = curPos.x() + (newPos.x() - m_appletBrowserDragStart.x());
00199            if (x < 0) {
00200                x = 0;
00201            } else if (x + m_appletBrowser->width() > width()) {
00202                x = width() - m_appletBrowser->width();
00203            }
00204         }
00205 
00206         if (x == 0 || x + m_appletBrowser->width() >= width()) {
00207             y = m_appletBrowser->y() + (newPos.y() - m_appletBrowserDragStart.y());
00208 
00209             if (y < 0) {
00210                 y = 0;
00211             } else if (y + m_appletBrowser->height() > height()) {
00212                 y = height() - m_appletBrowser->height();
00213             }
00214         }
00215         m_appletBrowser->move(x, y);
00216         m_appletBrowserDragStart = newPos;
00217     } else if (event->type() == QEvent::MouseButtonRelease) {
00218         m_appletBrowserDragStart = QPoint();
00219     }
00220 
00221     return false;
00222 }
00223 
00224 void SaverView::showView()
00225 {
00226     if (isHidden()) {
00227         if (m_suppressShow) {
00228             kDebug() << "show was suppressed";
00229             return;
00230         }
00231 
00232         setWindowState(Qt::WindowFullScreen);
00233         //KWindowSystem::setOnAllDesktops(winId(), true);
00234         //KWindowSystem::setState(winId(), NET::KeepAbove|NET::SkipTaskbar);
00235 
00236         show();
00237         raise();
00238 
00239         m_suppressShow = true;
00240         QTimer::singleShot(SUPPRESS_SHOW_TIMEOUT, this, SLOT(suppressShowTimeout()));
00241     }
00242 }
00243 
00244 void SaverView::setContainment(Plasma::Containment *newContainment)
00245 {
00246     if (newContainment == containment()) {
00247         return;
00248     }
00249 
00250     if (isVisible()) {
00251         disconnect(containment(), SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showAppletBrowser()));
00252         connect(newContainment, SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showAppletBrowser()));
00253     }
00254 
00255     if (m_appletBrowser) {
00256         m_appletBrowser->setContainment(newContainment);
00257     }
00258 
00259     View::setContainment(newContainment);
00260 }
00261 
00262 void SaverView::hideView()
00263 {
00264     if (isHidden()) {
00265         return;
00266     }
00267     if (m_appletBrowser) {
00268         m_appletBrowser->hide();
00269     }
00270 
00271     disconnect(containment(), SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showAppletBrowser()));
00272 
00273     containment()->closeToolBox();
00274     hide();
00275     //let the lockprocess know
00276     emit hidden();
00277 }
00278 
00279 void SaverView::suppressShowTimeout()
00280 {
00281     kDebug() << "SaverView::suppressShowTimeout";
00282     m_suppressShow = false;
00283 }
00284 
00285 void SaverView::keyPressEvent(QKeyEvent *event)
00286 {
00287     /*if (event->key() == Qt::Key_Escape) {
00288         hideView();
00289         event->accept();
00290         return;
00291     }*/
00292 
00293     //kDebug() << event->key() << event->spontaneous();
00294     Plasma::View::keyPressEvent(event);
00295 }
00296 
00297 //eeeeew. why did dashboard ever have this? wtf!
00298 void SaverView::showEvent(QShowEvent *event)
00299 {
00300     //KWindowSystem::setState(winId(), NET::SkipPager);
00301     connect(containment(), SIGNAL(showAddWidgetsInterface(QPointF)), this, SLOT(showAppletBrowser()));
00302     Plasma::View::showEvent(event);
00303 }
00304 
00305 #include "saverview.moc"
00306 

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