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

Applets

applet.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2007 Robert Knight <robertknight@gmail.com>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library 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 GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 // Own
00021 #include "applet/applet.h"
00022 
00023 // Qt
00024 #include <QAction>
00025 #include <QApplication>
00026 #include <QDesktopWidget>
00027 #include <QGraphicsView>
00028 #include <QCheckBox>
00029 #include <QVBoxLayout>
00030 #include <QLabel>
00031 #include <QGraphicsLinearLayout>
00032 
00033 // KDE
00034 #include <KIcon>
00035 #include <KDebug>
00036 #include <KConfigDialog>
00037 #include <KNumInput>
00038 #include <KProcess>
00039 
00040 // Plasma
00041 #include <plasma/widgets/icon.h>
00042 #include <plasma/containment.h>
00043 #include <plasma/view.h>
00044 
00045 // Local
00046 #include "ui/launcher.h"
00047 
00048 class LauncherApplet::Private
00049 {
00050 public:
00051     Plasma::Icon *icon;
00052     Kickoff::Launcher *launcher;
00053 
00054     QCheckBox *switchOnHoverCheckBox;
00055     KIntNumInput *visibleCountEdit;
00056     QList<QAction*> actions;
00057     QAction* switcher;
00058 
00059     Private() : launcher(0), switcher(0) {}
00060     ~Private() { delete launcher; }
00061     void createLauncher(LauncherApplet *q);
00062 };
00063 
00064 void LauncherApplet::Private::createLauncher(LauncherApplet *q)
00065 {
00066     launcher = new Kickoff::Launcher(q);
00067     launcher->setWindowFlags(launcher->windowFlags()|Qt::WindowStaysOnTopHint|Qt::Popup);
00068     launcher->setAutoHide(true);
00069     launcher->resize(launcher->sizeHint());
00070     QObject::connect(launcher, SIGNAL(aboutToHide()), icon, SLOT(setUnpressed()));
00071     QObject::connect(launcher, SIGNAL(configNeedsSaving()), q, SIGNAL(configNeedsSaving()));
00072 }
00073 
00074 LauncherApplet::LauncherApplet(QObject *parent, const QVariantList &args)
00075     : Plasma::Applet(parent,args),
00076       d(new Private)
00077 {
00078     KGlobal::locale()->insertCatalog("plasma_applet_launcher");
00079 
00080     setHasConfigurationInterface(true);
00081     setBackgroundHints(NoBackground);
00082     d->icon = new Plasma::Icon(KIcon("start-here-kde"), QString(), this);
00083     d->icon->setFlag(ItemIsMovable, false);
00084     connect(d->icon, SIGNAL(pressed(bool)), this, SLOT(toggleMenu(bool)));
00085     connect(this, SIGNAL(activate()), this, SLOT(toggleMenu()));
00086 }
00087 
00088 LauncherApplet::~LauncherApplet()
00089 {
00090     delete d;
00091 }
00092 
00093 void LauncherApplet::init()
00094 {
00095     QGraphicsLinearLayout *layout = new QGraphicsLinearLayout(this);
00096     layout->setContentsMargins(0, 0, 0, 0);
00097     layout->setSpacing(0);
00098 
00099     layout->addItem(d->icon);
00100 
00101     if (KService::serviceByStorageId("kde4-kmenuedit.desktop")) {
00102         QAction* menueditor = new QAction(i18n("Menu Editor"), this);
00103         d->actions.append(menueditor);
00104         connect(menueditor, SIGNAL(triggered(bool)), this, SLOT(startMenuEditor()));
00105     }
00106 
00107     setAspectRatioMode(Plasma::ConstrainedSquare);
00108 
00109     Q_ASSERT( ! d->switcher );
00110     d->switcher = new QAction(i18n("Switch to Classic Menu Style"), this);
00111     d->switcher->setVisible(immutability() == Plasma::Mutable);
00112     d->actions.append(d->switcher);
00113     connect(d->switcher, SIGNAL(triggered(bool)), this, SLOT(switchMenuStyle()));
00114     resize(IconSize(KIconLoader::Desktop),IconSize(KIconLoader::Desktop));
00115     d->icon->resize(contentsRect().size());
00116 }
00117 
00118 void LauncherApplet::constraintsEvent(Plasma::Constraints constraints)
00119 {
00120     setBackgroundHints(NoBackground);
00121     if (constraints & Plasma::FormFactorConstraint) {
00122         if (formFactor() == Plasma::Planar ||
00123             formFactor() == Plasma::MediaCenter) {
00124             //FIXME set correct minimum size
00125             //setMinimumSize(d->icon->sizeFromIconSize(IconSize(KIconLoader::Desktop)));
00126         } else {
00127             //setMinimumSize(d->icon->sizeFromIconSize(IconSize(KIconLoader::Small)));
00128         }
00129     }
00130 
00131     if (constraints & Plasma::ImmutableConstraint && d->switcher) {
00132         d->switcher->setVisible(immutability() == Plasma::Mutable);
00133     }
00134 }
00135 
00136 void LauncherApplet::switchMenuStyle()
00137 {
00138     if (containment()) {
00139         containment()->addApplet("simplelauncher", QVariantList(), geometry());
00140         destroy();
00141     }
00142 }
00143 
00144 void LauncherApplet::startMenuEditor()
00145 {
00146     KProcess::execute("kmenuedit");
00147 }
00148 
00149 void LauncherApplet::createConfigurationInterface(KConfigDialog *parent)
00150 {
00151     QWidget *widget = new QWidget;
00152     QVBoxLayout *layout = new QVBoxLayout(widget);
00153     widget->setLayout(layout);
00154 
00155     QHBoxLayout *vl = new QHBoxLayout(widget);
00156     layout->addLayout(vl);
00157 
00158     d->switchOnHoverCheckBox = new QCheckBox(i18n("Switch tabs on hover"), widget);
00159     layout->addWidget(d->switchOnHoverCheckBox);
00160 
00161     parent->setButtons(KDialog::Ok | KDialog::Cancel | KDialog::Apply);
00162     connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
00163     connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
00164     parent->addPage(widget, parent->windowTitle(), icon());
00165 
00166     if (!d->launcher) {
00167         d->createLauncher(this);
00168     }
00169 
00170     d->switchOnHoverCheckBox->setChecked(d->launcher->switchTabsOnHover());
00171 }
00172 
00173 void LauncherApplet::configAccepted()
00174 {
00175     bool switchTabsOnHover = d->switchOnHoverCheckBox->isChecked();
00176 
00177     // TODO: should this be moved into Launcher as well? perhaps even the config itself?
00178     KConfigGroup cg = config();
00179     cg.writeEntry("SwitchTabsOnHover", switchTabsOnHover);
00180     emit configNeedsSaving();
00181 
00182     if (!d->launcher) {
00183         d->createLauncher(this);
00184     }
00185 
00186     d->launcher->setSwitchTabsOnHover(switchTabsOnHover);
00187 }
00188 
00189 
00190 void LauncherApplet::toggleMenu(bool pressed)
00191 {
00192     if (pressed) {
00193         toggleMenu();
00194     }
00195 }
00196 
00197 void LauncherApplet::toggleMenu()
00198 {
00199     if (!d->launcher) {
00200         d->createLauncher(this);
00201     }
00202 
00203     d->launcher->reset();
00204 
00205     if (!d->launcher->isVisible()) {
00206         // It's risky to calculate the popupPosition based on the size before
00207         // calling setLauncherOrigin, which can change the size
00208         // Probably just a problem on screens with strange aspect ratio or smaller than
00209         // kickoff's size.
00210         QPoint popupPosition = Applet::popupPosition(d->launcher->size());
00211         d->launcher->move( popupPosition );
00212         QPoint iconPosition = view()->mapToGlobal(
00213                 view()->mapFromScene( d->icon->scenePos() ) );
00214 
00215         Plasma::View *pv = dynamic_cast<Plasma::View *>(view());
00216         Plasma::Location loc = Plasma::Floating;
00217         if (pv) {
00218             loc = pv->containment()->location();
00219         }
00220 
00221         d->launcher->setLauncherOrigin( iconPosition, loc );
00222         emit releaseVisualFocus();
00223     }
00224 
00225     d->launcher->setVisible(!d->launcher->isVisible());
00226     d->icon->setPressed();
00227 }
00228 
00229 QList<QAction*> LauncherApplet::contextualActions()
00230 {
00231   return d->actions;
00232 }
00233 
00234 #include "applet.moc"

Applets

Skip menu "Applets"
  • 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
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
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