Applets
contextmenufactory.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
00022 #include "ui/contextmenufactory.h"
00023
00024
00025 #include <QAbstractItemView>
00026 #include <QMap>
00027
00028
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
00040 #include <plasma/containment.h>
00041 #include <plasma/corona.h>
00042
00043
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
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
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
00131 QAction *addToDesktopAction = new QAction(this);
00132
00133
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
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
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
00177 QAction *viewSeparator = new QAction(this);
00178 viewSeparator->setSeparator(true);
00179 actions << viewSeparator;
00180 actions << viewActions(view);
00181
00182
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
00217 Plasma::Containment *panel = d->applet->containment();
00218 if (panel) {
00219 QVariantList args;
00220 args << url;
00221
00222
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"