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

Applets

contextmenufactory.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2007 Robert Knight <robertknight@gmail.com>
00003     Copyright 2007 Kevin Ottens <ervin@kde.org>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library 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 GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
00019 */
00020 
00021 // Own
00022 #include "ui/contextmenufactory.h"
00023 
00024 // Qt
00025 #include <QAbstractItemView>
00026 #include <QMap>
00027 
00028 // KDE
00029 #include <KIcon>
00030 #include <KLocalizedString>
00031 #include <KMenu>
00032 #include <KActionCollection>
00033 #include <KFileItem>
00034 #include <KParts/BrowserExtension>
00035 #include <KBookmarkManager>
00036 #include <Solid/Device>
00037 #include <Solid/StorageAccess>
00038 
00039 // Plasma
00040 #include <plasma/containment.h>
00041 #include <plasma/corona.h>
00042 
00043 // Local
00044 #include "core/favoritesmodel.h"
00045 #include "core/models.h"
00046 
00047 using namespace Kickoff;
00048 
00049 class ContextMenuFactory::Private
00050 {
00051 public:
00052     Private()
00053       : applet(0)
00054     {
00055     }
00056 
00057     QAction *advancedActionsMenu(const QString& url) const
00058     {
00059        KUrl kUrl(url);
00060        KActionCollection actionCollection((QObject*)0);
00061        KFileItemList items;
00062        QString mimeType = KMimeType::findByUrl(kUrl,0,false,true)->name();
00063        items << KFileItem(url,mimeType,KFileItem::Unknown);
00064        KParts::BrowserExtension::PopupFlags browserFlags = KParts::BrowserExtension::DefaultPopupItems;
00065        if (items.first().isLocalFile()) {
00066            browserFlags |= KParts::BrowserExtension::ShowProperties;
00067        }
00068        KParts::BrowserExtension::ActionGroupMap actionGroupMap;
00069        return 0;
00070        // ### TODO: remove kdebase-apps dependency
00071 #if 0
00072        KonqPopupMenu *menu = new KonqPopupMenu(items, kUrl,actionCollection,
00073                                                0, 0, browserFlags,
00074                                                0, KBookmarkManager::userBookmarksManager(), actionGroupMap);
00075 
00076        if (!menu->isEmpty()) {
00077             QAction *action = menu->menuAction();
00078             action->setText(i18n("Advanced"));
00079             action->setIcon(KIcon("list-add"));
00080             return action;
00081        } else {
00082             delete menu;
00083             return 0;
00084        }
00085 #endif
00086     }
00087 
00088     QMap<QAbstractItemView*,QList<QAction*> > viewActions;
00089     Plasma::Applet *applet;
00090 };
00091 
00092 ContextMenuFactory::ContextMenuFactory(QObject *parent)
00093  : QObject(parent)
00094  , d(new Private)
00095 {
00096 }
00097 
00098 ContextMenuFactory::~ContextMenuFactory()
00099 {
00100   delete d;
00101 }
00102 
00103 void ContextMenuFactory::showContextMenu(QAbstractItemView *view,const QPoint& pos)
00104 {
00105     Q_ASSERT(view);
00106 
00107     const QModelIndex index = view->indexAt(pos);
00108     const QString url = index.data(UrlRole).value<QString>();
00109 
00110     if (url.isEmpty()) {
00111         return;
00112     }
00113 
00114     bool isFavorite = FavoritesModel::isFavorite(url);
00115 
00116     QList<QAction*> actions;
00117 
00118     // add to / remove from favorites
00119     QAction *favoriteAction = new QAction(this);
00120     if (isFavorite) {
00121         favoriteAction->setText(i18n("Remove From Favorites"));
00122         favoriteAction->setIcon(KIcon("list-remove"));
00123     } else {
00124         favoriteAction->setText(i18n("Add to Favorites"));
00125         favoriteAction->setIcon(KIcon("bookmark-new"));
00126     }
00127 
00128     actions << favoriteAction;
00129 
00130     // add to desktop
00131     QAction *addToDesktopAction = new QAction(this);
00132 
00133     // add to main panel
00134     QAction *addToPanelAction = new QAction(this);
00135 
00136     if (d->applet) {
00137         Plasma::Containment *containment = d->applet->containment();
00138 
00139         if (containment && containment->corona()) {
00140             Plasma::Containment *desktop = containment->corona()->containmentForScreen(containment->screen());
00141 
00142             if (desktop && desktop->immutability() == Plasma::Mutable) {
00143                 addToDesktopAction->setText(i18n("Add to Desktop"));
00144                 actions << addToDesktopAction;
00145             }
00146         }
00147 
00148         if (containment && containment->immutability() == Plasma::Mutable &&
00149             containment->containmentType() == Plasma::Containment::PanelContainment) {
00150             addToPanelAction->setText(i18n("Add to Panel"));
00151             actions << addToPanelAction;
00152         }
00153     }
00154 
00155     // advanced item actions
00156     QAction *advancedSeparator = new QAction(this);
00157     advancedSeparator->setSeparator(true);
00158     actions << advancedSeparator;
00159 
00160     QAction *advanced = d->advancedActionsMenu(url);
00161     if (advanced) {
00162         actions << advanced;
00163     }
00164 
00165     // device actions
00166     QString udi = index.data(DeviceUdiRole).toString();
00167     Solid::Device device(udi);
00168     Solid::StorageAccess *access = device.as<Solid::StorageAccess>();
00169     QAction *ejectAction = 0;
00170     if (device.isValid() && access) {
00171         ejectAction = new QAction(this);
00172         ejectAction->setText(i18n("Eject"));
00173         actions << ejectAction;
00174     }
00175 
00176     // add view specific actions
00177     QAction *viewSeparator = new QAction(this);
00178     viewSeparator->setSeparator(true);
00179     actions << viewSeparator;
00180     actions << viewActions(view);
00181 
00182     // display menu
00183     KMenu menu;
00184     menu.addTitle(index.data(Qt::DisplayRole).value<QString>());
00185     foreach (QAction* action, actions) {
00186         menu.addAction(action);
00187     }
00188 
00189     QAction *result = menu.exec(QCursor::pos());
00190 
00191     if (favoriteAction && result == favoriteAction) {
00192         if (isFavorite) {
00193             FavoritesModel::remove(url);
00194         } else {
00195             FavoritesModel::add(url);
00196         }
00197     } else if (ejectAction && result == ejectAction) {
00198         access->teardown();
00199     } else if (addToDesktopAction && result == addToDesktopAction) {
00200         if (d->applet) {
00201             Plasma::Containment *containment = d->applet->containment();
00202             if (containment) {
00203                 Plasma::Corona *corona = containment->corona();
00204                 if (corona) {
00205                     Plasma::Containment *desktop = corona->containmentForScreen(containment->screen());
00206                     if (desktop) {
00207                         QVariantList args;
00208                         args << url;
00209                         desktop->addApplet("icon", args);
00210                     }
00211                 }
00212             }
00213         }
00214     } else if (addToPanelAction && result == addToPanelAction) {
00215         if (d->applet) {
00216             // we assume that the panel is the same containment where the kickoff is located
00217             Plasma::Containment *panel = d->applet->containment();
00218             if (panel) {
00219                 QVariantList args;
00220                 args << url;
00221 
00222                 // move it to the middle of the panel
00223                 QRectF rect(panel->geometry().width()/2, 0, 150, panel->boundingRect().height());
00224                 panel->addApplet("icon", args, rect);
00225             }
00226         }
00227     }
00228 
00229     delete favoriteAction;
00230     delete addToDesktopAction;
00231     delete addToPanelAction;
00232     delete viewSeparator;
00233     delete ejectAction;
00234 }
00235 void ContextMenuFactory::setViewActions(QAbstractItemView *view,const QList<QAction*>& actions)
00236 {
00237     if (actions.isEmpty()) {
00238         d->viewActions.remove(view);
00239     } else {
00240         d->viewActions.insert(view,actions);
00241     }
00242 }
00243 QList<QAction*> ContextMenuFactory::viewActions(QAbstractItemView *view) const
00244 {
00245     return d->viewActions[view];
00246 }
00247 
00248 void ContextMenuFactory::setApplet(Plasma::Applet *applet)
00249 {
00250     d->applet = applet;
00251 }
00252 
00253 #include "contextmenufactory.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