Applets
applet.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "applet/applet.h"
00022
00023
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
00034 #include <KIcon>
00035 #include <KDebug>
00036 #include <KConfigDialog>
00037 #include <KNumInput>
00038 #include <KProcess>
00039
00040
00041 #include <plasma/widgets/icon.h>
00042 #include <plasma/containment.h>
00043 #include <plasma/view.h>
00044
00045
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
00125
00126 } else {
00127
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
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
00207
00208
00209
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"