~ubuntu-branches/ubuntu/raring/calligra/raring-proposed

1 by Jonathan Riddell
Import upstream version 2.3.85
1
/* This file is part of the KDE project
2
   Copyright 2007 Montel Laurent <montel@kde.org>
1.1.6 by Philip Muškovac
Import upstream version 2.4.90
3
   Copyright 2011 Silvio Heinrich <plassy@web.de>
1 by Jonathan Riddell
Import upstream version 2.3.85
4
5
   This library is free software; you can redistribute it and/or
6
   modify it under the terms of the GNU Library General Public
7
   License as published by the Free Software Foundation; either
8
   version 2 of the License, or (at your option) any later version.
9
10
   This library is distributed in the hope that it will be useful,
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
   Library General Public License for more details.
14
15
   You should have received a copy of the GNU Library General Public License
16
   along with this library; see the file COPYING.LIB.  If not, write to
17
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
   Boston, MA 02110-1301, USA.
19
*/
20
21
#include "PictureTool.h"
22
#include "PictureShape.h"
23
#include "ChangeImageCommand.h"
1.1.7 by Rohan Garg
Import upstream version 2.4.91a
24
#include "ClipCommand.h"
1.1.6 by Philip Muškovac
Import upstream version 2.4.90
25
#include "CropWidget.h"
1 by Jonathan Riddell
Import upstream version 2.3.85
26
27
#include <KLocale>
28
#include <KIconLoader>
29
#include <KUrl>
30
#include <KFileDialog>
31
#include <KIO/Job>
32
33
#include <KoCanvasBase.h>
34
#include <KoImageCollection.h>
1.1.6 by Philip Muškovac
Import upstream version 2.4.90
35
#include <KoImageData.h>
1 by Jonathan Riddell
Import upstream version 2.3.85
36
#include <KoSelection.h>
37
#include <KoShapeManager.h>
1.1.7 by Rohan Garg
Import upstream version 2.4.91a
38
#include <KoShapeController.h>
39
#include <KoDocumentResourceManager.h>
1 by Jonathan Riddell
Import upstream version 2.3.85
40
#include <KoPointerEvent.h>
1.1.6 by Philip Muškovac
Import upstream version 2.4.90
41
#include <KoFilterEffect.h>
42
#include <KoFilterEffectRegistry.h>
43
#include <KoFilterEffectConfigWidgetBase.h>
44
#include <KoFilterEffectStack.h>
45
46
#include "ui_wdgPictureTool.h"
47
48
struct PictureToolUI: public QWidget, public Ui::PictureTool
49
{
50
    PictureToolUI()
51
    {
52
        setupUi(this);
53
    }
54
55
    void blockAllSignals(bool block)
56
    {
57
        leftDoubleSpinBox->blockSignals(block);
58
        rightDoubleSpinBox->blockSignals(block);
59
        topDoubleSpinBox->blockSignals(block);
60
        bottomDoubleSpinBox->blockSignals(block);
61
        cropWidget->blockSignals(block);
62
        cbAspect->blockSignals(block);
63
        cmbColorMode->blockSignals(block);
64
    }
65
};
66
67
// ---------------------------------------------------- //
68
69
PictureTool::PictureTool(KoCanvasBase* canvas)
70
    : KoToolBase(canvas),
71
      m_pictureshape(0),
72
      m_pictureToolUI(0)
1 by Jonathan Riddell
Import upstream version 2.3.85
73
{
74
}
75
76
void PictureTool::activate(ToolActivation toolActivation, const QSet<KoShape*> &shapes)
77
{
78
    Q_UNUSED(toolActivation);
79
80
    foreach (KoShape *shape, shapes) {
1.1.6 by Philip Muškovac
Import upstream version 2.4.90
81
        if ((m_pictureshape = dynamic_cast<PictureShape*>(shape)))
1 by Jonathan Riddell
Import upstream version 2.3.85
82
            break;
83
    }
1.1.6 by Philip Muškovac
Import upstream version 2.4.90
84
85
    if (!m_pictureshape) {
1 by Jonathan Riddell
Import upstream version 2.3.85
86
        emit done();
87
        return;
88
    }
1.1.6 by Philip Muškovac
Import upstream version 2.4.90
89
90
    if (m_pictureToolUI) {
91
        m_pictureToolUI->cropWidget->setPictureShape(m_pictureshape);
92
        updateControlElements();
93
    }
94
1 by Jonathan Riddell
Import upstream version 2.3.85
95
    useCursor(Qt::ArrowCursor);
96
}
97
98
void PictureTool::deactivate()
99
{
1.1.6 by Philip Muškovac
Import upstream version 2.4.90
100
    m_pictureshape = 0;
101
}
102
103
QWidget *PictureTool::createOptionWidget()
104
{
105
    m_pictureToolUI = new PictureToolUI();
106
    m_pictureToolUI->cmbColorMode->addItem(i18n("Standard")  , PictureShape::Standard);
107
    m_pictureToolUI->cmbColorMode->addItem(i18n("Greyscale") , PictureShape::Greyscale);
108
    m_pictureToolUI->cmbColorMode->addItem(i18n("Monochrome"), PictureShape::Mono);
109
    m_pictureToolUI->cmbColorMode->addItem(i18n("Watermark") , PictureShape::Watermark);
1.1.9 by Jonathan Riddell
Import upstream version 2.5.0
110
    m_pictureToolUI->bnImageFile->setIcon(SmallIcon("document-open"));
1.1.6 by Philip Muškovac
Import upstream version 2.4.90
111
112
    updateControlElements();
113
114
    connect(m_pictureToolUI->bnImageFile, SIGNAL(clicked(bool)), this, SLOT(changeUrlPressed()));
1.1.7 by Rohan Garg
Import upstream version 2.4.91a
115
    connect(m_pictureToolUI->cmbColorMode, SIGNAL(currentIndexChanged(int)), this, SLOT(colorModeChanged(int)));
1.1.6 by Philip Muškovac
Import upstream version 2.4.90
116
    connect(m_pictureToolUI->leftDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(cropEditFieldsChanged()));
117
    connect(m_pictureToolUI->rightDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(cropEditFieldsChanged()));
118
    connect(m_pictureToolUI->topDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(cropEditFieldsChanged()));
119
    connect(m_pictureToolUI->bottomDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(cropEditFieldsChanged()));
1.1.7 by Rohan Garg
Import upstream version 2.4.91a
120
    connect(m_pictureToolUI->cbAspect, SIGNAL(toggled(bool)), this, SLOT(aspectCheckBoxChanged(bool)));
1.1.6 by Philip Muškovac
Import upstream version 2.4.90
121
    connect(m_pictureToolUI->bnFill, SIGNAL(pressed()), this, SLOT(fillButtonPressed()));
1.1.7 by Rohan Garg
Import upstream version 2.4.91a
122
    connect(m_pictureToolUI->cbContour, SIGNAL(toggled(bool)), this, SLOT(contourCheckBoxChanged(bool)));
123
    connect(m_pictureToolUI->cropWidget, SIGNAL(sigCropRegionChanged(QRectF, bool)), this, SLOT(cropRegionChanged(QRectF, bool)));
1.1.6 by Philip Muškovac
Import upstream version 2.4.90
124
125
    return m_pictureToolUI;
126
}
127
128
void PictureTool::updateControlElements()
129
{
130
    if (m_pictureshape) {
131
        QSizeF imageSize = m_pictureshape->imageData()->imageSize();
132
        PictureShape::ColorMode mode      = m_pictureshape->colorMode();
133
        ClippingRect clippingRect(m_pictureshape->cropRect());
134
135
        clippingRect.right = 1.0 - clippingRect.right;
136
        clippingRect.bottom = 1.0 - clippingRect.bottom;
137
        clippingRect.scale(imageSize);
138
139
        m_pictureToolUI->blockAllSignals(true);
140
        m_pictureToolUI->cropWidget->setPictureShape(m_pictureshape);
141
        m_pictureToolUI->cropWidget->setKeepPictureProportion(m_pictureshape->isPictureInProportion());
142
        m_pictureToolUI->cbAspect->setChecked(m_pictureshape->isPictureInProportion());
143
        m_pictureToolUI->cmbColorMode->setCurrentIndex(m_pictureToolUI->cmbColorMode->findData(mode));
144
145
        m_pictureToolUI->leftDoubleSpinBox->setRange(0.0, imageSize.width());
146
        m_pictureToolUI->rightDoubleSpinBox->setRange(0.0, imageSize.width());
147
        m_pictureToolUI->topDoubleSpinBox->setRange(0.0, imageSize.height());
148
        m_pictureToolUI->bottomDoubleSpinBox->setRange(0.0, imageSize.height());
149
        m_pictureToolUI->leftDoubleSpinBox->setValue(clippingRect.left);
150
        m_pictureToolUI->rightDoubleSpinBox->setValue(clippingRect.right);
151
        m_pictureToolUI->topDoubleSpinBox->setValue(clippingRect.top);
152
        m_pictureToolUI->bottomDoubleSpinBox->setValue(clippingRect.bottom);
1.1.7 by Rohan Garg
Import upstream version 2.4.91a
153
        m_pictureToolUI->cbContour->setChecked(m_pictureshape->clipPath() != 0);
1.1.6 by Philip Muškovac
Import upstream version 2.4.90
154
        m_pictureToolUI->blockAllSignals(false);
155
    }
1 by Jonathan Riddell
Import upstream version 2.3.85
156
}
157
158
void PictureTool::changeUrlPressed()
159
{
160
    if (m_pictureshape == 0)
161
        return;
162
    KUrl url = KFileDialog::getOpenUrl();
163
    if (!url.isEmpty()) {
164
        // TODO move this to an action in the libs, with a nice dialog or something.
165
        KIO::StoredTransferJob *job = KIO::storedGet(url, KIO::NoReload, 0);
166
        connect(job, SIGNAL(result(KJob*)), this, SLOT(setImageData(KJob*)));
167
    }
168
}
169
1.1.6 by Philip Muškovac
Import upstream version 2.4.90
170
void PictureTool::cropEditFieldsChanged()
171
{
172
    ClippingRect clippingRect;
173
    clippingRect.left = m_pictureToolUI->leftDoubleSpinBox->value();
174
    clippingRect.right = m_pictureToolUI->rightDoubleSpinBox->value();
175
    clippingRect.top = m_pictureToolUI->topDoubleSpinBox->value();
176
    clippingRect.bottom = m_pictureToolUI->bottomDoubleSpinBox->value();
177
    clippingRect.uniform = false;
178
    clippingRect.inverted = true;
179
    clippingRect.normalize(m_pictureshape->imageData()->imageSize());
180
181
    m_pictureToolUI->cropWidget->setCropRect(clippingRect.toRect());
182
}
183
1.1.7 by Rohan Garg
Import upstream version 2.4.91a
184
void PictureTool::cropRegionChanged(const QRectF& rect, bool undoPrev)
1.1.6 by Philip Muškovac
Import upstream version 2.4.90
185
{
1.1.7 by Rohan Garg
Import upstream version 2.4.91a
186
    if (undoPrev) {
187
        canvas()->shapeController()->resourceManager()->undoStack()->undo();
188
    }
189
1.1.6 by Philip Muškovac
Import upstream version 2.4.90
190
    ChangeImageCommand *cmd = new ChangeImageCommand(m_pictureshape, rect);
191
    // connect before adding the command, so that "updateControlElements()" is executed
192
    // when the command is added to the undo stack.
193
    connect(cmd, SIGNAL(sigExecuted()), this, SLOT(updateControlElements()));
194
    canvas()->addCommand(cmd);
195
}
196
197
void PictureTool::colorModeChanged(int cmbIndex)
198
{
199
    PictureShape::ColorMode mode = (PictureShape::ColorMode)m_pictureToolUI->cmbColorMode->itemData(cmbIndex).toInt();
200
    ChangeImageCommand     *cmd  = new ChangeImageCommand(m_pictureshape, mode);
201
    canvas()->addCommand(cmd);
202
    // connect after adding the command to the undo stack to prevent a
203
    // call to "updateControlElements()" at this point
204
    connect(cmd, SIGNAL(sigExecuted()), this, SLOT(updateControlElements()));
205
}
206
207
void PictureTool::aspectCheckBoxChanged(bool checked)
208
{
209
    m_pictureToolUI->cropWidget->setKeepPictureProportion(checked);
210
}
211
1.1.7 by Rohan Garg
Import upstream version 2.4.91a
212
void PictureTool::contourCheckBoxChanged(bool checked)
213
{
214
    canvas()->addCommand(new ClipCommand(m_pictureshape, checked));
215
}
216
1.1.6 by Philip Muškovac
Import upstream version 2.4.90
217
void PictureTool::fillButtonPressed()
218
{
219
    m_pictureToolUI->cropWidget->maximizeCroppedArea();
220
}
221
1 by Jonathan Riddell
Import upstream version 2.3.85
222
void PictureTool::setImageData(KJob *job)
223
{
224
    if (m_pictureshape == 0)
225
        return; // ugh, the user deselected the image in between. We should move this code to main anyway redesign it
1.1.6 by Philip Muškovac
Import upstream version 2.4.90
226
1 by Jonathan Riddell
Import upstream version 2.3.85
227
    KIO::StoredTransferJob *transferJob = qobject_cast<KIO::StoredTransferJob*>(job);
228
    Q_ASSERT(transferJob);
229
    if (m_pictureshape->imageCollection()) {
1.1.6 by Philip Muškovac
Import upstream version 2.4.90
230
        KoImageData        *data = m_pictureshape->imageCollection()->createImageData(transferJob->data());
231
        ChangeImageCommand *cmd  = new ChangeImageCommand(m_pictureshape, data);
232
        // connect before adding the command, so that "updateControlElements()" is executed
233
        // when the command is added to the undo stack.
234
        connect(cmd, SIGNAL(sigExecuted()), this, SLOT(updateControlElements()));
1 by Jonathan Riddell
Import upstream version 2.3.85
235
        canvas()->addCommand(cmd);
236
    }
237
}
238
1.1.2 by Jonathan Riddell
Import upstream version 2.3.87
239
void PictureTool::mousePressEvent(KoPointerEvent *event)
240
{
241
    if (event->button() == Qt::RightButton) {
242
        event->ignore();
243
    }
244
}
245
246
void PictureTool::mouseDoubleClickEvent(KoPointerEvent *event)
1 by Jonathan Riddell
Import upstream version 2.3.85
247
{
1.1.6 by Philip Muškovac
Import upstream version 2.4.90
248
    if (canvas()->shapeManager()->shapeAt(event->point) != m_pictureshape) {
1 by Jonathan Riddell
Import upstream version 2.3.85
249
        event->ignore(); // allow the event to be used by another
250
        return;
251
    }
252
253
    changeUrlPressed();
254
}