~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to app/imageopscontextmanageritem.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
ImportĀ upstreamĀ versionĀ 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
 
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
2
2
/*
3
3
Gwenview: an image viewer
4
4
Copyright 2007 AurĆ©lien GĆ¢teau <agateau@kde.org>
34
34
 
35
35
// Local
36
36
#include "contextmanager.h"
37
 
#include "documentpanel.h"
 
37
#include "viewmainpage.h"
38
38
#include "gvcore.h"
39
39
#include "mainwindow.h"
40
40
#include "sidebar.h"
41
41
#include <lib/crop/croptool.h>
42
42
#include <lib/document/documentfactory.h>
 
43
#include <lib/documentview/rasterimageview.h>
43
44
#include <lib/eventwatcher.h>
44
45
#include <lib/redeyereduction/redeyereductiontool.h>
45
46
#include <lib/gwenviewconfig.h>
46
 
#include <lib/imageview.h>
47
47
#include <lib/resize/resizeimageoperation.h>
48
48
#include <lib/resize/resizeimagedialog.h>
49
49
#include <lib/transformimageoperation.h>
50
50
 
51
 
namespace Gwenview {
 
51
namespace Gwenview
 
52
{
52
53
 
53
54
#undef ENABLE_LOG
54
55
#undef LOG
60
61
#endif
61
62
 
62
63
struct ImageOpsContextManagerItem::Private {
63
 
        ImageOpsContextManagerItem* that;
64
 
        MainWindow* mMainWindow;
65
 
        SideBarGroup* mGroup;
66
 
 
67
 
        KAction* mRotateLeftAction;
68
 
        KAction* mRotateRightAction;
69
 
        KAction* mMirrorAction;
70
 
        KAction* mFlipAction;
71
 
        KAction* mResizeAction;
72
 
        KAction* mCropAction;
73
 
        KAction* mRedEyeReductionAction;
74
 
        QList<KAction*> mActionList;
75
 
 
76
 
        void setupActions() {
77
 
                KActionCollection* actionCollection = mMainWindow->actionCollection();
78
 
                KActionCategory* edit=new KActionCategory(i18nc("@title actions category - means actions changing image","Edit"), actionCollection);
79
 
                mRotateLeftAction = edit->addAction("rotate_left",that, SLOT(rotateLeft()));
80
 
                mRotateLeftAction->setText(i18n("Rotate Left"));
81
 
                mRotateLeftAction->setIcon(KIcon("object-rotate-left"));
82
 
                mRotateLeftAction->setShortcut(Qt::CTRL + Qt::Key_L);
83
 
 
84
 
                mRotateRightAction = edit->addAction("rotate_right",that, SLOT(rotateRight()));
85
 
                mRotateRightAction->setText(i18n("Rotate Right"));
86
 
                mRotateRightAction->setIcon(KIcon("object-rotate-right"));
87
 
                mRotateRightAction->setShortcut(Qt::CTRL + Qt::Key_R);
88
 
 
89
 
                mMirrorAction = edit->addAction("mirror",that, SLOT(mirror()));
90
 
                mMirrorAction->setText(i18n("Mirror"));
91
 
                mMirrorAction->setIcon(KIcon("object-flip-horizontal"));
92
 
 
93
 
                mFlipAction = edit->addAction("flip",that, SLOT(flip()));
94
 
                mFlipAction->setText(i18n("Flip"));
95
 
                mFlipAction->setIcon(KIcon("object-flip-vertical"));
96
 
 
97
 
                mResizeAction = edit->addAction("resize",that, SLOT(resizeImage()) );
98
 
                mResizeAction->setText(i18n("Resize"));
99
 
                mResizeAction->setIcon(KIcon("transform-scale"));
100
 
                mResizeAction->setShortcut(Qt::SHIFT + Qt::Key_R);
101
 
 
102
 
                mCropAction = edit->addAction("crop",that, SLOT(crop()));
103
 
                mCropAction->setText(i18n("Crop"));
104
 
                mCropAction->setIcon(KIcon("transform-crop-and-resize"));
105
 
                mCropAction->setShortcut(Qt::SHIFT + Qt::Key_C);
106
 
 
107
 
                mRedEyeReductionAction = edit->addAction("red_eye_reduction",that, SLOT(startRedEyeReduction()) );
108
 
                mRedEyeReductionAction->setText(i18n("Red Eye Reduction"));
109
 
                //mRedEyeReductionAction->setIcon(KIcon("transform-crop-and-resize"));
110
 
 
111
 
                mActionList
112
 
                        << mRotateLeftAction
113
 
                        << mRotateRightAction
114
 
                        << mMirrorAction
115
 
                        << mFlipAction
116
 
                        << mResizeAction
117
 
                        << mCropAction
118
 
                        << mRedEyeReductionAction
119
 
                        ;
120
 
        }
121
 
 
122
 
 
123
 
        bool ensureEditable() {
124
 
                KUrl url = that->contextManager()->currentUrl();
125
 
                return GvCore::ensureDocumentIsEditable(url);
126
 
        }
 
64
    ImageOpsContextManagerItem* q;
 
65
    MainWindow* mMainWindow;
 
66
    SideBarGroup* mGroup;
 
67
 
 
68
    KAction* mRotateLeftAction;
 
69
    KAction* mRotateRightAction;
 
70
    KAction* mMirrorAction;
 
71
    KAction* mFlipAction;
 
72
    KAction* mResizeAction;
 
73
    KAction* mCropAction;
 
74
    KAction* mRedEyeReductionAction;
 
75
    QList<KAction*> mActionList;
 
76
 
 
77
    void setupActions()
 
78
    {
 
79
        KActionCollection* actionCollection = mMainWindow->actionCollection();
 
80
        KActionCategory* edit = new KActionCategory(i18nc("@title actions category - means actions changing image", "Edit"), actionCollection);
 
81
        mRotateLeftAction = edit->addAction("rotate_left", q, SLOT(rotateLeft()));
 
82
        mRotateLeftAction->setText(i18n("Rotate Left"));
 
83
        mRotateLeftAction->setIcon(KIcon("object-rotate-left"));
 
84
        mRotateLeftAction->setShortcut(Qt::CTRL + Qt::Key_L);
 
85
 
 
86
        mRotateRightAction = edit->addAction("rotate_right", q, SLOT(rotateRight()));
 
87
        mRotateRightAction->setText(i18n("Rotate Right"));
 
88
        mRotateRightAction->setIcon(KIcon("object-rotate-right"));
 
89
        mRotateRightAction->setShortcut(Qt::CTRL + Qt::Key_R);
 
90
 
 
91
        mMirrorAction = edit->addAction("mirror", q, SLOT(mirror()));
 
92
        mMirrorAction->setText(i18n("Mirror"));
 
93
        mMirrorAction->setIcon(KIcon("object-flip-horizontal"));
 
94
 
 
95
        mFlipAction = edit->addAction("flip", q, SLOT(flip()));
 
96
        mFlipAction->setText(i18n("Flip"));
 
97
        mFlipAction->setIcon(KIcon("object-flip-vertical"));
 
98
 
 
99
        mResizeAction = edit->addAction("resize", q, SLOT(resizeImage()));
 
100
        mResizeAction->setText(i18n("Resize"));
 
101
        mResizeAction->setIcon(KIcon("transform-scale"));
 
102
        mResizeAction->setShortcut(Qt::SHIFT + Qt::Key_R);
 
103
 
 
104
        mCropAction = edit->addAction("crop", q, SLOT(crop()));
 
105
        mCropAction->setText(i18n("Crop"));
 
106
        mCropAction->setIcon(KIcon("transform-crop-and-resize"));
 
107
        mCropAction->setShortcut(Qt::SHIFT + Qt::Key_C);
 
108
 
 
109
        mRedEyeReductionAction = edit->addAction("red_eye_reduction", q, SLOT(startRedEyeReduction()));
 
110
        mRedEyeReductionAction->setText(i18n("Red Eye Reduction"));
 
111
        //mRedEyeReductionAction->setIcon(KIcon("transform-crop-and-resize"));
 
112
 
 
113
        mActionList
 
114
                << mRotateLeftAction
 
115
                << mRotateRightAction
 
116
                << mMirrorAction
 
117
                << mFlipAction
 
118
                << mResizeAction
 
119
                << mCropAction
 
120
                << mRedEyeReductionAction
 
121
                ;
 
122
    }
 
123
 
 
124
    bool ensureEditable()
 
125
    {
 
126
        KUrl url = q->contextManager()->currentUrl();
 
127
        return GvCore::ensureDocumentIsEditable(url);
 
128
    }
127
129
};
128
130
 
129
 
 
130
131
ImageOpsContextManagerItem::ImageOpsContextManagerItem(ContextManager* manager, MainWindow* mainWindow)
131
132
: AbstractContextManagerItem(manager)
132
 
, d(new Private) {
133
 
        d->that = this;
134
 
        d->mMainWindow = mainWindow;
135
 
        d->mGroup = new SideBarGroup(i18n("Image Operations"));
136
 
        setWidget(d->mGroup);
137
 
        EventWatcher::install(d->mGroup, QEvent::Show, this, SLOT(updateSideBarContent()));
138
 
        d->setupActions();
139
 
        updateActions();
140
 
        connect(contextManager(), SIGNAL(selectionChanged()),
141
 
                SLOT(updateActions()) );
142
 
        connect(mainWindow, SIGNAL(viewModeChanged()),
143
 
                SLOT(updateActions()) );
144
 
        connect(mainWindow->documentPanel(), SIGNAL(completed()),
145
 
                SLOT(updateActions()) );
146
 
}
147
 
 
148
 
 
149
 
ImageOpsContextManagerItem::~ImageOpsContextManagerItem() {
150
 
        delete d;
151
 
}
152
 
 
153
 
 
154
 
void ImageOpsContextManagerItem::updateSideBarContent() {
155
 
        if (!d->mGroup->isVisible()) {
156
 
                return;
157
 
        }
158
 
 
159
 
        d->mGroup->clear();
160
 
        Q_FOREACH(KAction* action, d->mActionList) {
161
 
                if (action->isEnabled()) {
162
 
                        d->mGroup->addAction(action);
163
 
                }
164
 
        }
165
 
}
166
 
 
167
 
 
168
 
void ImageOpsContextManagerItem::updateActions() {
169
 
        bool canModify = d->mMainWindow->currentDocumentIsRasterImage();
170
 
        bool documentPanelIsVisible = d->mMainWindow->documentPanel()->isVisible();
171
 
        if (!documentPanelIsVisible) {
172
 
                // Since we only support image operations on one image for now,
173
 
                // disable actions if several images are selected and the document
174
 
                // view is not visible.
175
 
                if (contextManager()->selectedFileItemList().count() != 1) {
176
 
                        canModify = false;
177
 
                }
178
 
        }
179
 
 
180
 
        d->mRotateLeftAction->setEnabled(canModify);
181
 
        d->mRotateRightAction->setEnabled(canModify);
182
 
        d->mMirrorAction->setEnabled(canModify);
183
 
        d->mFlipAction->setEnabled(canModify);
184
 
        d->mResizeAction->setEnabled(canModify);
185
 
        d->mCropAction->setEnabled(canModify && documentPanelIsVisible);
186
 
        d->mRedEyeReductionAction->setEnabled(canModify && documentPanelIsVisible);
187
 
 
188
 
        updateSideBarContent();
189
 
}
190
 
 
191
 
 
192
 
void ImageOpsContextManagerItem::rotateLeft() {
193
 
        TransformImageOperation* op = new TransformImageOperation(ROT_270);
194
 
        applyImageOperation(op);
195
 
}
196
 
 
197
 
 
198
 
void ImageOpsContextManagerItem::rotateRight() {
199
 
        TransformImageOperation* op = new TransformImageOperation(ROT_90);
200
 
        applyImageOperation(op);
201
 
}
202
 
 
203
 
 
204
 
void ImageOpsContextManagerItem::mirror() {
205
 
        TransformImageOperation* op = new TransformImageOperation(HFLIP);
206
 
        applyImageOperation(op);
207
 
}
208
 
 
209
 
 
210
 
void ImageOpsContextManagerItem::flip() {
211
 
        TransformImageOperation* op = new TransformImageOperation(VFLIP);
212
 
        applyImageOperation(op);
213
 
}
214
 
 
215
 
 
216
 
void ImageOpsContextManagerItem::resizeImage() {
217
 
        if (!d->ensureEditable()) {
218
 
                return;
219
 
        }
220
 
        Document::Ptr doc = DocumentFactory::instance()->load(contextManager()->currentUrl());
221
 
        doc->startLoadingFullImage();
 
133
, d(new Private)
 
134
{
 
135
    d->q = this;
 
136
    d->mMainWindow = mainWindow;
 
137
    d->mGroup = new SideBarGroup(i18n("Image Operations"));
 
138
    setWidget(d->mGroup);
 
139
    EventWatcher::install(d->mGroup, QEvent::Show, this, SLOT(updateSideBarContent()));
 
140
    d->setupActions();
 
141
    updateActions();
 
142
    connect(contextManager(), SIGNAL(selectionChanged()),
 
143
            SLOT(updateActions()));
 
144
    connect(mainWindow, SIGNAL(viewModeChanged()),
 
145
            SLOT(updateActions()));
 
146
    connect(mainWindow->documentPanel(), SIGNAL(completed()),
 
147
            SLOT(updateActions()));
 
148
}
 
149
 
 
150
ImageOpsContextManagerItem::~ImageOpsContextManagerItem()
 
151
{
 
152
    delete d;
 
153
}
 
154
 
 
155
void ImageOpsContextManagerItem::updateSideBarContent()
 
156
{
 
157
    if (!d->mGroup->isVisible()) {
 
158
        return;
 
159
    }
 
160
 
 
161
    d->mGroup->clear();
 
162
    Q_FOREACH(KAction * action, d->mActionList) {
 
163
        if (action->isEnabled()) {
 
164
            d->mGroup->addAction(action);
 
165
        }
 
166
    }
 
167
}
 
168
 
 
169
void ImageOpsContextManagerItem::updateActions()
 
170
{
 
171
    bool canModify = d->mMainWindow->currentDocumentIsRasterImage();
 
172
    bool documentPanelIsVisible = d->mMainWindow->documentPanel()->isVisible();
 
173
    if (!documentPanelIsVisible) {
 
174
        // Since we only support image operations on one image for now,
 
175
        // disable actions if several images are selected and the document
 
176
        // view is not visible.
 
177
        if (contextManager()->selectedFileItemList().count() != 1) {
 
178
            canModify = false;
 
179
        }
 
180
    }
 
181
 
 
182
    d->mRotateLeftAction->setEnabled(canModify);
 
183
    d->mRotateRightAction->setEnabled(canModify);
 
184
    d->mMirrorAction->setEnabled(canModify);
 
185
    d->mFlipAction->setEnabled(canModify);
 
186
    d->mResizeAction->setEnabled(canModify);
 
187
    d->mCropAction->setEnabled(canModify && documentPanelIsVisible);
 
188
    d->mRedEyeReductionAction->setEnabled(canModify && documentPanelIsVisible);
 
189
 
 
190
    updateSideBarContent();
 
191
}
 
192
 
 
193
void ImageOpsContextManagerItem::rotateLeft()
 
194
{
 
195
    TransformImageOperation* op = new TransformImageOperation(ROT_270);
 
196
    applyImageOperation(op);
 
197
}
 
198
 
 
199
void ImageOpsContextManagerItem::rotateRight()
 
200
{
 
201
    TransformImageOperation* op = new TransformImageOperation(ROT_90);
 
202
    applyImageOperation(op);
 
203
}
 
204
 
 
205
void ImageOpsContextManagerItem::mirror()
 
206
{
 
207
    TransformImageOperation* op = new TransformImageOperation(HFLIP);
 
208
    applyImageOperation(op);
 
209
}
 
210
 
 
211
void ImageOpsContextManagerItem::flip()
 
212
{
 
213
    TransformImageOperation* op = new TransformImageOperation(VFLIP);
 
214
    applyImageOperation(op);
 
215
}
 
216
 
 
217
void ImageOpsContextManagerItem::resizeImage()
 
218
{
 
219
    if (!d->ensureEditable()) {
 
220
        return;
 
221
    }
 
222
    Document::Ptr doc = DocumentFactory::instance()->load(contextManager()->currentUrl());
 
223
    doc->startLoadingFullImage();
222
224
    ResizeImageDialog dialog(d->mMainWindow);
223
225
    dialog.setOriginalSize(doc->size());
224
226
    if (!dialog.exec()) {
225
227
        return;
226
228
    }
227
 
        ResizeImageOperation* op = new ResizeImageOperation(dialog.size());
228
 
        applyImageOperation(op);
229
 
}
230
 
 
231
 
 
232
 
void ImageOpsContextManagerItem::crop() {
233
 
        if (!d->ensureEditable()) {
234
 
                return;
235
 
        }
236
 
        ImageView* imageView = d->mMainWindow->documentPanel()->imageView();
237
 
        if (!imageView) {
238
 
                kError() << "No ImageView available!";
239
 
                return;
240
 
        }
241
 
        CropTool* tool = new CropTool(imageView);
242
 
        connect(tool, SIGNAL(imageOperationRequested(AbstractImageOperation*)),
243
 
                SLOT(applyImageOperation(AbstractImageOperation*)) );
244
 
        connect(tool, SIGNAL(done()),
245
 
                SLOT(restoreDefaultImageViewTool()) );
246
 
 
247
 
        d->mMainWindow->setDistractionFreeMode(true);
248
 
        imageView->setCurrentTool(tool);
249
 
}
250
 
 
251
 
 
252
 
void ImageOpsContextManagerItem::startRedEyeReduction() {
253
 
        if (!d->ensureEditable()) {
254
 
                return;
255
 
        }
256
 
        ImageView* imageView = d->mMainWindow->documentPanel()->imageView();
257
 
        if (!imageView) {
258
 
                kError() << "No ImageView available!";
259
 
                return;
260
 
        }
261
 
        RedEyeReductionTool* tool = new RedEyeReductionTool(imageView);
262
 
        connect(tool, SIGNAL(imageOperationRequested(AbstractImageOperation*)),
263
 
                SLOT(applyImageOperation(AbstractImageOperation*)) );
264
 
        connect(tool, SIGNAL(done()),
265
 
                SLOT(restoreDefaultImageViewTool()) );
266
 
 
267
 
        d->mMainWindow->setDistractionFreeMode(true);
268
 
        imageView->setCurrentTool(tool);
269
 
}
270
 
 
271
 
 
272
 
void ImageOpsContextManagerItem::applyImageOperation(AbstractImageOperation* op) {
273
 
        // For now, we only support operations on one image
274
 
        KUrl url = contextManager()->currentUrl();
275
 
 
276
 
        Document::Ptr doc = DocumentFactory::instance()->load(url);
277
 
        op->applyToDocument(doc);
278
 
}
279
 
 
280
 
 
281
 
void ImageOpsContextManagerItem::restoreDefaultImageViewTool() {
282
 
        ImageView* imageView = d->mMainWindow->documentPanel()->imageView();
283
 
        if (!imageView) {
284
 
                kError() << "No ImageView available!";
285
 
                return;
286
 
        }
287
 
 
288
 
        AbstractImageViewTool* tool = imageView->currentTool();
289
 
        imageView->setCurrentTool(0);
290
 
        tool->deleteLater();
291
 
        d->mMainWindow->setDistractionFreeMode(false);
292
 
}
293
 
 
 
229
    ResizeImageOperation* op = new ResizeImageOperation(dialog.size());
 
230
    applyImageOperation(op);
 
231
}
 
232
 
 
233
void ImageOpsContextManagerItem::crop()
 
234
{
 
235
    if (!d->ensureEditable()) {
 
236
        return;
 
237
    }
 
238
    RasterImageView* imageView = d->mMainWindow->documentPanel()->imageView();
 
239
    if (!imageView) {
 
240
        kError() << "No ImageView available!";
 
241
        return;
 
242
    }
 
243
    CropTool* tool = new CropTool(imageView);
 
244
    connect(tool, SIGNAL(imageOperationRequested(AbstractImageOperation*)),
 
245
            SLOT(applyImageOperation(AbstractImageOperation*)));
 
246
    connect(tool, SIGNAL(done()),
 
247
            SLOT(restoreDefaultImageViewTool()));
 
248
 
 
249
    d->mMainWindow->setDistractionFreeMode(true);
 
250
    imageView->setCurrentTool(tool);
 
251
}
 
252
 
 
253
void ImageOpsContextManagerItem::startRedEyeReduction()
 
254
{
 
255
    if (!d->ensureEditable()) {
 
256
        return;
 
257
    }
 
258
    RasterImageView* view = d->mMainWindow->documentPanel()->imageView();
 
259
    if (!view) {
 
260
        kError() << "No RasterImageView available!";
 
261
        return;
 
262
    }
 
263
    RedEyeReductionTool* tool = new RedEyeReductionTool(view);
 
264
    connect(tool, SIGNAL(imageOperationRequested(AbstractImageOperation*)),
 
265
            SLOT(applyImageOperation(AbstractImageOperation*)));
 
266
    connect(tool, SIGNAL(done()),
 
267
            SLOT(restoreDefaultImageViewTool()));
 
268
 
 
269
    d->mMainWindow->setDistractionFreeMode(true);
 
270
    view->setCurrentTool(tool);
 
271
}
 
272
 
 
273
void ImageOpsContextManagerItem::applyImageOperation(AbstractImageOperation* op)
 
274
{
 
275
    // For now, we only support operations on one image
 
276
    KUrl url = contextManager()->currentUrl();
 
277
 
 
278
    Document::Ptr doc = DocumentFactory::instance()->load(url);
 
279
    op->applyToDocument(doc);
 
280
}
 
281
 
 
282
void ImageOpsContextManagerItem::restoreDefaultImageViewTool()
 
283
{
 
284
    RasterImageView* imageView = d->mMainWindow->documentPanel()->imageView();
 
285
    if (!imageView) {
 
286
        kError() << "No RasterImageView available!";
 
287
        return;
 
288
    }
 
289
 
 
290
    AbstractRasterImageViewTool* tool = imageView->currentTool();
 
291
    imageView->setCurrentTool(0);
 
292
    tool->deleteLater();
 
293
    d->mMainWindow->setDistractionFreeMode(false);
 
294
}
294
295
 
295
296
} // namespace