not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / applets / kickoff / applet / applet.cpp
bloba830982f7543eac20283b7e2f96fb7b3362edc76
1 /*
2 Copyright 2007 Robert Knight <robertknight@gmail.com>
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 // Own
21 #include "applet/applet.h"
23 // Qt
24 #include <QtGui/QAction>
25 #include <QtGui/QApplication>
26 #include <QtGui/QGraphicsView>
27 #include <QtGui/QCheckBox>
28 #include <QtGui/QVBoxLayout>
29 #include <QtGui/QLabel>
30 #include <QtGui/QGraphicsLinearLayout>
32 // KDE
33 #include <KIcon>
34 #include <KDebug>
35 #include <KConfigDialog>
36 #include <KProcess>
38 // Plasma
39 #include <Plasma/IconWidget>
40 #include <Plasma/Containment>
41 #include <Plasma/View>
42 #include <Plasma/ToolTipManager>
44 // Local
45 #include "ui/launcher.h"
46 #include "core/recentapplications.h"
48 class LauncherApplet::Private
50 public:
51 Private(LauncherApplet *lApplet) : launcher(0), switcher(0), q(lApplet) { }
52 ~Private() {
53 delete launcher;
55 void createLauncher();
56 void initToolTip();
58 Kickoff::Launcher *launcher;
60 QCheckBox *switchOnHoverCheckBox;
61 QList<QAction*> actions;
62 QAction* switcher;
63 LauncherApplet *q;
66 void LauncherApplet::Private::createLauncher()
68 if (launcher) {
69 return;
72 launcher = new Kickoff::Launcher(q);
73 launcher->setAttribute(Qt::WA_NoSystemBackground);
74 launcher->setAutoHide(true);
75 QObject::connect(launcher, SIGNAL(aboutToHide()), q, SLOT(hidePopup()));
76 //launcher->resize(launcher->sizeHint());
77 //QObject::connect(launcher, SIGNAL(aboutToHide()), icon, SLOT(setUnpressed()));
78 //QObject::connect(launcher, SIGNAL(configNeedsSaving()), q, SIGNAL(configNeedsSaving()));
81 void LauncherApplet::Private::initToolTip()
83 Plasma::ToolTipContent data(i18n("Kickoff Application Launcher"),
84 i18n("Favorites, applications, computer places, "
85 "recently used items and desktop sessions"),
86 q->popupIcon().pixmap(IconSize(KIconLoader::Desktop)));
87 Plasma::ToolTipManager::self()->setContent(q, data);
90 LauncherApplet::LauncherApplet(QObject *parent, const QVariantList &args)
91 : Plasma::PopupApplet(parent, args),
92 d(new Private(this))
94 KGlobal::locale()->insertCatalog("plasma_applet_launcher");
95 setHasConfigurationInterface(true);
96 setPopupIcon("start-here-kde");
99 LauncherApplet::~LauncherApplet()
101 delete d;
104 void LauncherApplet::init()
106 if (KService::serviceByStorageId("kde4-kmenuedit.desktop")) {
107 QAction* menueditor = new QAction(i18n("Menu Editor"), this);
108 d->actions.append(menueditor);
109 connect(menueditor, SIGNAL(triggered(bool)), this, SLOT(startMenuEditor()));
112 Q_ASSERT(! d->switcher);
113 d->switcher = new QAction(i18n("Switch to Classic Menu Style"), this);
114 d->actions.append(d->switcher);
115 connect(d->switcher, SIGNAL(triggered(bool)), this, SLOT(switchMenuStyle()));
117 constraintsEvent(Plasma::ImmutableConstraint);
118 Plasma::ToolTipManager::self()->registerWidget(this);
121 void LauncherApplet::constraintsEvent(Plasma::Constraints constraints)
123 if ((constraints & Plasma::ImmutableConstraint) && d->switcher) {
124 d->switcher->setVisible(immutability() == Plasma::Mutable);
128 void LauncherApplet::switchMenuStyle()
130 if (containment()) {
131 containment()->addApplet("simplelauncher", QVariantList(), geometry());
132 destroy();
136 void LauncherApplet::startMenuEditor()
138 KProcess::execute("kmenuedit");
141 void LauncherApplet::createConfigurationInterface(KConfigDialog *parent)
143 QWidget *widget = new QWidget(parent);
144 QVBoxLayout *widgetLayout = new QVBoxLayout(widget);
145 widget->setLayout(widgetLayout);
147 d->switchOnHoverCheckBox = new QCheckBox(i18n("Switch tabs on hover"), widget);
148 widgetLayout->addWidget(d->switchOnHoverCheckBox);
150 widgetLayout->addStretch();
152 connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
153 connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
154 parent->addPage(widget, i18n("General"), icon());
156 d->createLauncher();
157 d->switchOnHoverCheckBox->setChecked(d->launcher->switchTabsOnHover());
160 void LauncherApplet::popupEvent(bool show)
162 if (show) {
163 Plasma::ToolTipManager::self()->clearContent(this);
164 d->launcher->setLauncherOrigin(popupPlacement(), location());
165 d->createLauncher();
169 void LauncherApplet::toolTipAboutToShow()
171 if (d->launcher->isVisible()) {
172 Plasma::ToolTipManager::self()->clearContent(this);
173 } else {
174 d->initToolTip();
178 void LauncherApplet::configAccepted()
180 bool switchTabsOnHover = d->switchOnHoverCheckBox->isChecked();
182 // TODO: should this be moved into Launcher as well? perhaps even the config itself?
183 KConfigGroup cg = globalConfig();
184 cg.writeEntry("SwitchTabsOnHover", switchTabsOnHover);
185 emit configNeedsSaving();
187 d->createLauncher();
188 d->launcher->setSwitchTabsOnHover(switchTabsOnHover);
191 QList<QAction*> LauncherApplet::contextualActions()
193 return d->actions;
196 QWidget *LauncherApplet::widget()
198 d->createLauncher();
199 return d->launcher;
202 #include "applet.moc"