~ubuntu-branches/ubuntu/trusty/digikam/trusty

« back to all changes in this revision

Viewing changes to core/utilities/importui/widgets/importcontextmenu.cpp

  • Committer: Package Import Robot
  • Author(s): Rohan Garg
  • Date: 2012-11-26 18:24:20 UTC
  • mfrom: (1.9.1) (3.1.23 experimental)
  • Revision ID: package-import@ubuntu.com-20121126182420-qoy6z0nx4ai0wzcl
Tags: 4:3.0.0~beta3-0ubuntu1
* New upstream release
  - Add build-deps :  libhupnp-dev, libqtgstreamer-dev, libmagickcore-dev
* Merge from debian, remaining changes:
  - Make sure libqt4-opengl-dev, libgl1-mesa-dev and libglu1-mesa-dev only
    install on i386,amd64 and powerpc
  - Depend on libtiff-dev instead of libtiff4-dev
  - Drop digikam breaks/replaces kipi-plugins-common since we're past the
    LTS release now
  - digikam to recommend mplayerthumbs | ffmpegthumbs. We currently only
    have latter in the archives, even though former is also supposed to
    be part of kdemultimedia. (LP: #890059)
  - kipi-plugins to recommend www-browser rather than konqueror directly
    since 2.8 no direct usage of konqueror is present in the flickr
    plugin anymore (LP: #1011211)
  - Keep kubuntu_mysqld_executable_name.diff
  - Don't install libkipi translations
  - Keep deps on libcv-dev, libcvaux-dev
  - Keep split packaging of libraries
  - Replace icons from KDE 3 time in debian/xpm.d/*.xpm with the new
    versions (LP: #658047)
* Update debian/not-installed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ============================================================
 
2
 *
 
3
 * This file is a part of digiKam project
 
4
 * http://www.digikam.org
 
5
 *
 
6
 * Date        : 2012-07-13
 
7
 * Description : Modified context menu helper for import tool
 
8
 *
 
9
 * Copyright (C) 2012 by Islam Wazery <wazery at ubuntu dot com>
 
10
 *
 
11
 * This program is free software; you can redistribute it
 
12
 * and/or modify it under the terms of the GNU General
 
13
 * Public License as published by the Free Software Foundation;
 
14
 * either version 2, or (at your option)
 
15
 * any later version.
 
16
 *
 
17
 * This program is distributed in the hope that it will be useful,
 
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
 * GNU General Public License for more details.
 
21
 *
 
22
 * ============================================================ */
 
23
 
 
24
#include "importcontextmenu.moc"
 
25
 
 
26
// Qt includes
 
27
 
 
28
#include <QAction>
 
29
 
 
30
// KDE includes
 
31
 
 
32
#include <kaction.h>
 
33
#include <kactionmenu.h>
 
34
#include <kactioncollection.h>
 
35
#include <kapplication.h>
 
36
#include <kservice.h>
 
37
#include <kmimetype.h>
 
38
#include <kmimetypetrader.h>
 
39
#include <kopenwithdialog.h>
 
40
#include <krun.h>
 
41
#include <kstandardaction.h>
 
42
 
 
43
// Local includes
 
44
 
 
45
#include "importui.h"
 
46
#include "picklabelwidget.h"
 
47
#include "colorlabelwidget.h"
 
48
#include "ratingwidget.h"
 
49
#include "tagmodificationhelper.h"
 
50
#include "tagspopupmenu.h"
 
51
#include "fileactionmngr.h"
 
52
 
 
53
namespace Digikam
 
54
{
 
55
 
 
56
class ImportContextMenuHelper::Private
 
57
{
 
58
public:
 
59
 
 
60
    explicit Private(ImportContextMenuHelper* const q) :
 
61
        importFilterModel(0),
 
62
        parent(0),
 
63
        ABCmenu(0),
 
64
        stdActionCollection(0),
 
65
        q(q)
 
66
    {}
 
67
 
 
68
    QList<qlonglong>             selectedIds;
 
69
    KUrl::List                   selectedItems;
 
70
 
 
71
    QMap<int, QAction*>          queueActions;
 
72
    QMap<QString, KService::Ptr> servicesMap;
 
73
 
 
74
    ImportFilterModel*           importFilterModel;
 
75
 
 
76
    QMenu*                       parent;
 
77
    QMenu*                       ABCmenu;
 
78
 
 
79
    KActionCollection*           stdActionCollection;
 
80
 
 
81
    ImportContextMenuHelper*     q;
 
82
 
 
83
public:
 
84
 
 
85
    QAction* copyFromMainCollection(const char* name)
 
86
    {
 
87
        QAction* mainAction = stdActionCollection->action(name);
 
88
 
 
89
        if (!mainAction)
 
90
        {
 
91
            return 0;
 
92
        }
 
93
 
 
94
        QAction* action = new QAction(mainAction->icon(), mainAction->text(), q);
 
95
        action->setToolTip(mainAction->toolTip());
 
96
        return action;
 
97
    }
 
98
};
 
99
 
 
100
ImportContextMenuHelper::ImportContextMenuHelper(QMenu* const parent, KActionCollection* const actionCollection)
 
101
    : QObject(parent), d(new Private(this))
 
102
{
 
103
    d->parent = parent;
 
104
 
 
105
    if (!actionCollection)
 
106
    {
 
107
        d->stdActionCollection = ImportUI::instance()->actionCollection();
 
108
    }
 
109
    else
 
110
    {
 
111
        d->stdActionCollection = actionCollection;
 
112
    }
 
113
}
 
114
 
 
115
ImportContextMenuHelper::~ImportContextMenuHelper()
 
116
{
 
117
    delete d;
 
118
}
 
119
 
 
120
void ImportContextMenuHelper::addAction(const char* name, bool addDisabled)
 
121
{
 
122
    QAction* action = d->stdActionCollection->action(name);
 
123
    addAction(action, addDisabled);
 
124
}
 
125
 
 
126
void ImportContextMenuHelper::addAction(QAction* action, bool addDisabled)
 
127
{
 
128
    if (!action)
 
129
    {
 
130
        return;
 
131
    }
 
132
 
 
133
    if (action->isEnabled() || addDisabled)
 
134
    {
 
135
        d->parent->addAction(action);
 
136
    }
 
137
}
 
138
 
 
139
void ImportContextMenuHelper::addSubMenu(KMenu* subMenu)
 
140
{
 
141
    d->parent->addMenu(subMenu);
 
142
}
 
143
 
 
144
void ImportContextMenuHelper::addSeparator()
 
145
{
 
146
    d->parent->addSeparator();
 
147
}
 
148
 
 
149
void ImportContextMenuHelper::addAction(QAction* action, QObject* recv, const char* slot,
 
150
                                        bool addDisabled)
 
151
{
 
152
    if (!action)
 
153
    {
 
154
        return;
 
155
    }
 
156
 
 
157
    connect(action, SIGNAL(triggered()),
 
158
            recv, slot);
 
159
 
 
160
    addAction(action, addDisabled);
 
161
}
 
162
 
 
163
void ImportContextMenuHelper::addServicesMenu(const KUrl::List& selectedItems)
 
164
{
 
165
    setSelectedItems(selectedItems);
 
166
 
 
167
    // This code is inspired by KonqMenuActions:
 
168
    // kdebase/apps/lib/konq/konq_menuactions.cpp
 
169
 
 
170
    QStringList    mimeTypes;
 
171
    KService::List offers;
 
172
 
 
173
    foreach(const KUrl& item, d->selectedItems)
 
174
    {
 
175
        const QString mimeType = KMimeType::findByUrl(item, 0, true, true)->name();
 
176
 
 
177
        if (!mimeTypes.contains(mimeType))
 
178
        {
 
179
            mimeTypes << mimeType;
 
180
        }
 
181
    }
 
182
 
 
183
    if (!mimeTypes.isEmpty())
 
184
    {
 
185
        // Query trader
 
186
        const QString firstMimeType      = mimeTypes.takeFirst();
 
187
        const QString constraintTemplate = "'%1' in ServiceTypes";
 
188
        QStringList   constraints;
 
189
 
 
190
        foreach(const QString& mimeType, mimeTypes)
 
191
        {
 
192
            constraints << constraintTemplate.arg(mimeType);
 
193
        }
 
194
 
 
195
        offers = KMimeTypeTrader::self()->query(firstMimeType, "Application", constraints.join(" and "));
 
196
 
 
197
        // remove duplicate service entries
 
198
        QSet<QString> seenApps;
 
199
 
 
200
        for (KService::List::iterator it = offers.begin(); it != offers.end();)
 
201
        {
 
202
            const QString appName((*it)->name());
 
203
 
 
204
            if (!seenApps.contains(appName))
 
205
            {
 
206
                seenApps.insert(appName);
 
207
                ++it;
 
208
            }
 
209
            else
 
210
            {
 
211
                it = offers.erase(it);
 
212
            }
 
213
        }
 
214
    }
 
215
 
 
216
    if (!offers.isEmpty())
 
217
    {
 
218
        KMenu* servicesMenu    = new KMenu(d->parent);
 
219
        qDeleteAll(servicesMenu->actions());
 
220
 
 
221
        QAction* serviceAction = servicesMenu->menuAction();
 
222
        serviceAction->setText(i18n("Open With"));
 
223
 
 
224
        foreach(KService::Ptr service, offers)
 
225
        {
 
226
            QString name         = service->name().replace('&', "&&");
 
227
            QAction* action      = servicesMenu->addAction(name);
 
228
            action->setIcon(KIcon(service->icon()));
 
229
            action->setData(service->name());
 
230
            d->servicesMap[name] = service;
 
231
        }
 
232
 
 
233
        servicesMenu->addSeparator();
 
234
        servicesMenu->addAction(i18n("Other..."));
 
235
 
 
236
        addAction(serviceAction);
 
237
 
 
238
        connect(servicesMenu, SIGNAL(triggered(QAction*)),
 
239
                this, SLOT(slotOpenWith(QAction*)));
 
240
    }
 
241
    else
 
242
    {
 
243
        QAction* serviceAction = new QAction(i18n("Open With..."), this);
 
244
        addAction(serviceAction);
 
245
 
 
246
        connect(serviceAction, SIGNAL(triggered()),
 
247
                this, SLOT(slotOpenWith()));
 
248
    }
 
249
}
 
250
 
 
251
void ImportContextMenuHelper::slotOpenWith()
 
252
{
 
253
    // call the slot with an "empty" action
 
254
    slotOpenWith(0);
 
255
}
 
256
 
 
257
void ImportContextMenuHelper::slotOpenWith(QAction* action)
 
258
{
 
259
    KService::Ptr service;
 
260
    KUrl::List list = d->selectedItems;
 
261
 
 
262
    QString name = action ? action->data().toString() : QString();
 
263
 
 
264
    if (name.isEmpty())
 
265
    {
 
266
        QPointer<KOpenWithDialog> dlg = new KOpenWithDialog(list);
 
267
 
 
268
        if (!dlg->exec() == KOpenWithDialog::Accepted)
 
269
        {
 
270
            delete dlg;
 
271
            return;
 
272
        }
 
273
 
 
274
        service = dlg->service();
 
275
 
 
276
        if (!service)
 
277
        {
 
278
            // User entered a custom command
 
279
            if (!dlg->text().isEmpty())
 
280
            {
 
281
                KRun::run(dlg->text(), list, d->parent);
 
282
            }
 
283
 
 
284
            delete dlg;
 
285
            return;
 
286
        }
 
287
 
 
288
        delete dlg;
 
289
    }
 
290
    else
 
291
    {
 
292
        service = d->servicesMap[name];
 
293
    }
 
294
 
 
295
    KRun::run(*service, list, d->parent);
 
296
}
 
297
 
 
298
void ImportContextMenuHelper::addRotateMenu(itemIds& /*ids*/)
 
299
{
 
300
//    setSelectedIds(ids);
 
301
 
 
302
//    KMenu* imageRotateMenu = new KMenu(i18n("Rotate"), d->parent);
 
303
//    imageRotateMenu->setIcon(KIcon("object-rotate-right"));
 
304
 
 
305
//    KAction* left = new KAction(this);
 
306
//    left->setObjectName("rotate_ccw");
 
307
//    left->setText(i18nc("rotate image left", "Left"));
 
308
//    connect(left, SIGNAL(triggered(bool)),
 
309
//            this, SLOT(slotRotate()));
 
310
//    imageRotateMenu->addAction(left);
 
311
 
 
312
//    KAction* right = new KAction(this);
 
313
//    right->setObjectName("rotate_cw");
 
314
//    right->setText(i18nc("rotate image right", "Right"));
 
315
//    connect(right, SIGNAL(triggered(bool)),
 
316
//            this, SLOT(slotRotate()));
 
317
//    imageRotateMenu->addAction(right);
 
318
 
 
319
//    d->parent->addMenu(imageRotateMenu);
 
320
}
 
321
 
 
322
void ImportContextMenuHelper::slotRotate()
 
323
{
 
324
//TODO: Implement rotate in import tool.
 
325
//    if (sender()->objectName() == "rotate_ccw")
 
326
//    {
 
327
//        FileActionMngr::instance()->transform(CamItemInfoList(d->selectedIds), KExiv2Iface::RotationMatrix::Rotate270);
 
328
//    }
 
329
//    else
 
330
//    {
 
331
//        FileActionMngr::instance()->transform(CamItemInfoList(d->selectedIds), KExiv2Iface::RotationMatrix::Rotate90);
 
332
//    }
 
333
}
 
334
 
 
335
void ImportContextMenuHelper::addAssignTagsMenu(itemIds& ids)
 
336
{
 
337
    setSelectedIds(ids);
 
338
 
 
339
    KMenu* assignTagsPopup = new TagsPopupMenu(ids, TagsPopupMenu::RECENTLYASSIGNED, d->parent);
 
340
    assignTagsPopup->menuAction()->setText(i18n("Assign Tag"));
 
341
    assignTagsPopup->menuAction()->setIcon(SmallIcon("tag"));
 
342
    d->parent->addMenu(assignTagsPopup);
 
343
 
 
344
    connect(assignTagsPopup, SIGNAL(signalTagActivated(int)),
 
345
            this, SIGNAL(signalAssignTag(int)));
 
346
 
 
347
    connect(assignTagsPopup, SIGNAL(signalPopupTagsView()),
 
348
            this, SIGNAL(signalPopupTagsView()));
 
349
}
 
350
 
 
351
void ImportContextMenuHelper::addRemoveTagsMenu(itemIds& ids)
 
352
{
 
353
    setSelectedIds(ids);
 
354
 
 
355
    KMenu* removeTagsPopup = new TagsPopupMenu(ids, TagsPopupMenu::REMOVE, d->parent);
 
356
    removeTagsPopup->menuAction()->setText(i18n("Remove Tag"));
 
357
    removeTagsPopup->menuAction()->setIcon(SmallIcon("tag"));
 
358
    d->parent->addMenu(removeTagsPopup);
 
359
 
 
360
    connect(removeTagsPopup, SIGNAL(signalTagActivated(int)),
 
361
            this, SIGNAL(signalRemoveTag(int)));
 
362
}
 
363
 
 
364
void ImportContextMenuHelper::addLabelsAction()
 
365
{
 
366
    KMenu* menuLabels           = new KMenu(i18n("Assign Labels"), d->parent);
 
367
    PickLabelMenuAction* pmenu  = new PickLabelMenuAction(d->parent);
 
368
    ColorLabelMenuAction* cmenu = new ColorLabelMenuAction(d->parent);
 
369
    RatingMenuAction* rmenu     = new RatingMenuAction(d->parent);
 
370
    menuLabels->addAction(pmenu);
 
371
    menuLabels->addAction(cmenu);
 
372
    menuLabels->addAction(rmenu);
 
373
    addSubMenu(menuLabels);
 
374
 
 
375
    connect(pmenu, SIGNAL(signalPickLabelChanged(int)),
 
376
            this, SIGNAL(signalAssignPickLabel(int)));
 
377
 
 
378
    connect(cmenu, SIGNAL(signalColorLabelChanged(int)),
 
379
            this, SIGNAL(signalAssignColorLabel(int)));
 
380
 
 
381
    connect(rmenu, SIGNAL(signalRatingChanged(int)),
 
382
            this, SIGNAL(signalAssignRating(int)));
 
383
}
 
384
 
 
385
void ImportContextMenuHelper::slotABCMenuTriggered(QAction* action)
 
386
{
 
387
    QString name = action->iconText();
 
388
    emit signalAddNewTagFromABCMenu(name);
 
389
}
 
390
 
 
391
void ImportContextMenuHelper::setImportFilterModel(ImportFilterModel* model)
 
392
{
 
393
    d->importFilterModel = model;
 
394
}
 
395
 
 
396
QAction* ImportContextMenuHelper::exec(const QPoint& pos, QAction* at)
 
397
{
 
398
    QAction* choice = d->parent->exec(pos, at);
 
399
 
 
400
    if (choice)
 
401
    {
 
402
        // check if a BQM action has been triggered
 
403
        for (QMap<int, QAction*>::const_iterator it = d->queueActions.constBegin();
 
404
             it != d->queueActions.constEnd(); ++it)
 
405
        {
 
406
            if (choice == it.value())
 
407
            {
 
408
                //emit signalAddToExistingQueue(it.key());
 
409
                return choice;
 
410
            }
 
411
        }
 
412
    }
 
413
 
 
414
    return choice;
 
415
}
 
416
 
 
417
void ImportContextMenuHelper::setSelectedIds(itemIds& ids)
 
418
{
 
419
    if (d->selectedIds.isEmpty())
 
420
    {
 
421
        d->selectedIds = ids;
 
422
    }
 
423
}
 
424
 
 
425
void ImportContextMenuHelper::setSelectedItems(const KUrl::List& urls)
 
426
{
 
427
    if (d->selectedItems.isEmpty())
 
428
    {
 
429
        d->selectedItems = urls;
 
430
    }
 
431
}
 
432
 
 
433
} // namespace Digikam