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

Plasma

controllerwindow.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright 2008 Marco Martin <notmart@gmail.com>
00003  *   Copyright 2009 Aaron Seigo <aseigo@kde.org>
00004  *
00005  *   This program is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU Library General Public License as
00007  *   published by the Free Software Foundation; either version 2, or
00008  *   (at your option) any later version.
00009  *
00010  *   This program is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details
00014  *
00015  *   You should have received a copy of the GNU Library General Public
00016  *   License along with this program; if not, write to the
00017  *   Free Software Foundation, Inc.,
00018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019  */
00020 
00021 #include "controllerwindow.h"
00022 
00023 #include <QApplication>
00024 #include <QBoxLayout>
00025 #include <QPainter>
00026 
00027 #include <kwindowsystem.h>
00028 #include <netwm.h>
00029 
00030 #include <Plasma/Containment>
00031 #include <Plasma/Corona>
00032 #include <Plasma/Theme>
00033 #include <Plasma/FrameSvg>
00034 #include <Plasma/View>
00035 
00036 #include "widgetsExplorer/widgetexplorer.h"
00037 
00038 #include <kephal/screens.h>
00039 
00040 ControllerWindow::ControllerWindow(QWidget* parent)
00041    : QWidget(parent),
00042      m_location(Plasma::Floating),
00043      m_layout(new QBoxLayout(QBoxLayout::TopToBottom, this)),
00044      m_background(new Plasma::FrameSvg(this)),
00045      m_containment(0),
00046      m_widgetExplorerView(0),
00047      m_widgetExplorer(0)
00048 {
00049     Q_UNUSED(parent)
00050     setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
00051     KWindowSystem::setState(winId(), NET::SkipTaskbar | NET::SkipPager | NET::Sticky | NET::KeepAbove);
00052     setAttribute(Qt::WA_DeleteOnClose);
00053     setAttribute(Qt::WA_TranslucentBackground);
00054     setFocus(Qt::ActiveWindowFocusReason);
00055 
00056     QPalette pal = palette();
00057     pal.setBrush(backgroundRole(), Qt::transparent);
00058     setPalette(pal);
00059 
00060     m_background->setImagePath("dialogs/background");
00061     m_background->setContainsMultipleImages(true);
00062 
00063     m_layout->setContentsMargins(0, 0, 0, 0);
00064 
00065     connect(KWindowSystem::self(), SIGNAL(activeWindowChanged(WId)), this, SLOT(onActiveWindowChanged(WId)));
00066     connect(m_background, SIGNAL(repaintNeeded()), SLOT(backgroundChanged()));
00067 }
00068 
00069 ControllerWindow::~ControllerWindow()
00070 {
00071     delete m_widgetExplorer;
00072     delete m_widgetExplorerView;
00073 }
00074 
00075 void ControllerWindow::backgroundChanged()
00076 {
00077     Plasma::Location l = m_location;
00078     m_location = Plasma::Floating;
00079     setLocation(l);
00080     update();
00081 }
00082 
00083 void ControllerWindow::setContainment(Plasma::Containment *containment)
00084 {
00085     if (!containment) {
00086         return;
00087     }
00088 
00089     if (m_containment) {
00090         disconnect(m_containment, 0, this, 0);
00091     }
00092 
00093     m_containment = containment;
00094 
00095     if (m_widgetExplorerView) {
00096         m_widgetExplorerView->setScreen(m_containment->screen(), m_containment->desktop());
00097     }
00098 
00099     if (m_widgetExplorer) {
00100         m_widgetExplorer->setContainment(m_containment);
00101     }
00102 }
00103 
00104 Plasma::Containment *ControllerWindow::containment() const
00105 {
00106     return m_containment;
00107 }
00108 
00109 QSize ControllerWindow::sizeHint() const
00110 {
00111     if (!m_containment) {
00112         return QWidget::sizeHint();
00113     }
00114 
00115     QRect screenGeom = Kephal::ScreenUtils::screenGeometry(m_containment->screen());
00116 
00117     switch (m_location) {
00118     case Plasma::LeftEdge:
00119     case Plasma::RightEdge:
00120         return QSize(QWidget::sizeHint().width(), screenGeom.height());
00121         break;
00122     case Plasma::TopEdge:
00123     case Plasma::BottomEdge:
00124     default:
00125         return QSize(screenGeom.width(), QWidget::sizeHint().height());
00126         break;
00127     }
00128 }
00129 
00130 void ControllerWindow::setLocation(const Plasma::Location &loc)
00131 {
00132     if (m_location == loc || !m_containment) {
00133         return;
00134     }
00135 
00136     m_location = loc;
00137     QRect screenGeom = Kephal::ScreenUtils::screenGeometry(m_containment->screen());
00138 
00139     switch (loc) {
00140     case Plasma::LeftEdge:
00141         m_background->setEnabledBorders(Plasma::FrameSvg::RightBorder);
00142         m_layout->setDirection(QBoxLayout::TopToBottom);
00143         m_layout->setContentsMargins(0, 0, m_background->marginSize(Plasma::RightMargin), 0);
00144         break;
00145 
00146     case Plasma::RightEdge:
00147         m_background->setEnabledBorders(Plasma::FrameSvg::LeftBorder);
00148         m_layout->setDirection(QBoxLayout::TopToBottom);
00149         m_layout->setContentsMargins(m_background->marginSize(Plasma::LeftMargin), 0, 0, 0);
00150         break;
00151 
00152     case Plasma::TopEdge:
00153         m_background->setEnabledBorders(Plasma::FrameSvg::BottomBorder);
00154         m_layout->setDirection(QBoxLayout::BottomToTop);
00155         m_layout->setContentsMargins(0, 0, 0, m_background->marginSize(Plasma::BottomMargin));
00156         break;
00157 
00158     case Plasma::BottomEdge:
00159     default:
00160         m_background->setEnabledBorders(Plasma::FrameSvg::TopBorder);
00161         m_layout->setDirection(QBoxLayout::TopToBottom);
00162         m_layout->setContentsMargins(0, m_background->marginSize(Plasma::TopMargin), 0, 0);
00163         break;
00164     }
00165 
00166     if (m_widgetExplorer) {
00167         switch (loc) {
00168         case Plasma::LeftEdge:
00169         case Plasma::RightEdge:
00170             m_widgetExplorer->setOrientation(Qt::Vertical);
00171             break;
00172         case Plasma::TopEdge:
00173         case Plasma::BottomEdge:
00174             m_widgetExplorer->setOrientation(Qt::Horizontal);
00175             break;
00176         default:
00177             break;
00178         }
00179     }
00180 }
00181 
00182 Plasma::Location ControllerWindow::location() const
00183 {
00184     return m_location;
00185 }
00186 
00187 Qt::Orientation ControllerWindow::orientation() const
00188 {
00189     if (m_location == Plasma::TopEdge || m_location == Plasma::BottomEdge) {
00190         return Qt::Horizontal;
00191     }
00192 
00193     return Qt::Vertical;
00194 }
00195 
00196 void ControllerWindow::showWidgetExplorer()
00197 {
00198     if (!m_containment) {
00199         return;
00200     }
00201 
00202     if (!m_widgetExplorerView) {
00203         m_widgetExplorerView = new Plasma::View(0, this);
00204         m_widgetExplorerView->setScene(m_containment->corona());
00205         m_widgetExplorerView->setScreen(m_containment->screen(), m_containment->desktop());
00206 
00207         m_widgetExplorerView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00208         m_widgetExplorerView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00209         m_widgetExplorerView->setStyleSheet("background: transparent; border: none;");
00210 
00211         
00212         m_widgetExplorerView->installEventFilter(this);
00213         m_layout->addWidget(m_widgetExplorerView);
00214     }
00215 
00216     if (!m_widgetExplorer) {
00217         m_widgetExplorer = new Plasma::WidgetExplorer();
00218         m_widgetExplorer->setContainment(m_containment);
00219         m_widgetExplorer->populateWidgetList();
00220         m_widgetExplorer->resize(m_widgetExplorerView->size());
00221 
00222         m_containment->corona()->addOffscreenWidget(m_widgetExplorer);
00223         m_widgetExplorerView->setSceneRect(m_widgetExplorer->geometry());
00224 
00225         m_widgetExplorer->installEventFilter(this);
00226         m_widgetExplorer->setIconSize(KIconLoader::SizeHuge);
00227     }
00228 
00229     m_widgetExplorer->setOrientation(orientation());
00230 
00231     if (orientation() == Qt::Horizontal) {
00232         resize(width(), m_widgetExplorer->size().height());
00233     } else {
00234         resize(m_widgetExplorer->size().width(), height());
00235     }
00236 
00237     m_widgetExplorer->show();
00238     // connect signals
00239 }
00240 
00241 bool ControllerWindow::isWidgetExplorerVisible() const
00242 {
00243     return m_widgetExplorerView && m_widgetExplorerView->isVisible();
00244 }
00245 
00246 Plasma::FrameSvg *ControllerWindow::background() const
00247 {
00248     return m_background;
00249 }
00250 
00251 void ControllerWindow::onActiveWindowChanged(WId id)
00252 {
00253     Q_UNUSED(id)
00254 
00255     //if the active window isn't the plasma desktop and the widgets explorer is visible,
00256     //then close the panel controller
00257     if (QApplication::activeWindow() == 0 || (QApplication::activeWindow()->winId() != KWindowSystem::activeWindow())) {
00258         if (m_widgetExplorerView && m_widgetExplorerView->isVisible() && !isActiveWindow()) {
00259             close();
00260         }
00261     }
00262 }
00263 
00264 void ControllerWindow::paintEvent(QPaintEvent *event)
00265 {
00266     Q_UNUSED(event)
00267 
00268     QPainter painter(this);
00269     painter.setCompositionMode(QPainter::CompositionMode_Source );
00270 
00271     m_background->paintFrame(&painter);
00272 }
00273 
00274 void ControllerWindow::keyPressEvent(QKeyEvent *event)
00275 {
00276     if (event->key() == Qt::Key_Escape) {
00277         close();
00278     }
00279 }
00280 
00281 void ControllerWindow::resizeEvent(QResizeEvent * event)
00282 {
00283     m_background->resizeFrame(size());
00284 
00285     qDebug() << "ControllerWindow::resizeEvent" << event->oldSize();
00286 
00287     // We want to stay aligned to the edge we are at
00288     if (event->oldSize().isValid()) {
00289         switch (m_location) {
00290             case Plasma::BottomEdge:
00291                 move(
00292                     x(),
00293                     y() - event->size().height()
00294                         + event->oldSize().height());
00295                 break;
00296         // TODO: Enable this after fixing the behaviour
00297         // of the vertical widget explorer
00298         //     case Plasma::RightEdge:
00299         //         move(
00300         //             x() - event->size().width()
00301         //                 + event->oldSize().width(),
00302         //             y());
00303         //         break;
00304             default:
00305                 // do nothing
00306                 break;
00307         }
00308     }
00309 }
00310 
00311 bool ControllerWindow::eventFilter(QObject *watched, QEvent *event)
00312 {
00313     //if widgetsExplorer moves or resizes, then the view has to adjust
00314     if ((watched == (QObject*)m_widgetExplorer) && (event->type() == QEvent::GraphicsSceneResize || event->type() == QEvent::GraphicsSceneMove)) {
00315         m_widgetExplorerView->resize(m_widgetExplorer->size().toSize());
00316         m_widgetExplorerView->setSceneRect(m_widgetExplorer->geometry());
00317         //kDebug() << "sizes are:" << m_widgetExplorer->size() << m_widgetExplorerView->size() << size();
00318     }
00319 
00320     //if the view resizes, then the widgetexplorer has to be resized
00321     if (watched == m_widgetExplorerView && event->type() == QEvent::Resize) {
00322         QResizeEvent *resizeEvent = static_cast<QResizeEvent *>(event);
00323         m_widgetExplorer->resize(resizeEvent->size());
00324         m_widgetExplorerView->setSceneRect(m_widgetExplorer->geometry());
00325 
00326         QSize borderSize = size() - m_layout->contentsRect().size();
00327 
00328         if (orientation() == Qt::Horizontal) {
00329             resize(width(), m_widgetExplorerView->height() + borderSize.height());
00330         } else {
00331             resize(m_widgetExplorerView->width() + borderSize.width(), height());
00332         }
00333     }
00334 
00335     return false;
00336 }
00337 
00338 #include "controllerwindow.moc"

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
  • System Settings
  •   SystemSettingsView
Generated for API Reference by doxygen 1.5.9-20090814
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