kpTransformSkewDialog.cpp 8.94 KB
Newer Older
1
/*
2
   Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
3
   All rights reserved.
Clarence Dang's avatar
   
Clarence Dang committed
4

5
6
7
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
Clarence Dang's avatar
   
Clarence Dang committed
8

9
10
11
12
13
   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
Clarence Dang's avatar
   
Clarence Dang committed
14

15
16
17
18
19
20
   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21
22
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
24
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26
*/

27

28
29
#define DEBUG_KP_TOOL_SKEW 0
#define DEBUG_KP_TOOL_SKEW_DIALOG 0
30

Clarence Dang's avatar
   
Clarence Dang committed
31

32
#include "dialogs/imagelib/transforms/kpTransformSkewDialog.h"
Clarence Dang's avatar
Clarence Dang committed
33

34
35
36
37
38
39
#include <QApplication>
#include <QGridLayout>
#include <QGroupBox>
#include <QLabel>
#include <QLayout>
#include <QPushButton>
Laurent Montel's avatar
Laurent Montel committed
40
#include <QTransform>
41
#include <QImage>
Martin Koller's avatar
Martin Koller committed
42
#include <QSpinBox>
43

44
#include "kpLogCategories.h"
45
#include <KLocalizedString>
46

47
48
49
50
51
#include "kpDefs.h"
#include "document/kpDocument.h"
#include "pixmapfx/kpPixmapFX.h"
#include "tools/kpTool.h"
#include "environments/dialogs/imagelib/transforms/kpTransformDialogEnvironment.h"
52
53


54
// private static
55
56
int kpTransformSkewDialog::s_lastWidth = -1,
    kpTransformSkewDialog::s_lastHeight = -1;
57

Clarence Dang's avatar
   
Clarence Dang committed
58
// private static
59
60
int kpTransformSkewDialog::s_lastHorizontalAngle = 0,
    kpTransformSkewDialog::s_lastVerticalAngle = 0;
Clarence Dang's avatar
   
Clarence Dang committed
61
62


63
kpTransformSkewDialog::kpTransformSkewDialog (bool actOnSelection,
64
        kpTransformDialogEnvironment *_env, QWidget *parent)
65
    : kpTransformPreviewDialog (kpTransformPreviewDialog::AllFeatures,
66
        false/*don't reserve top row*/,
67
        actOnSelection ? i18nc ("@title:window", "Skew Selection") : i18nc ("@title:window", "Skew Image"),
68
        i18n ("After skew:"),
69
        actOnSelection,
70
        _env, parent)
Clarence Dang's avatar
   
Clarence Dang committed
71
{
Clarence Dang's avatar
   
Clarence Dang committed
72
73
74
    // Too confusing - disable for now
    s_lastHorizontalAngle = s_lastVerticalAngle = 0;

75

Clarence Dang's avatar
   
Clarence Dang committed
76
    createAngleGroupBox ();
77
78


79
    if (s_lastWidth > 0 && s_lastHeight > 0) {
80
        resize (s_lastWidth, s_lastHeight);
81
    }
82
83
84


    slotUpdate ();
85
86
    

Martin Koller's avatar
Martin Koller committed
87
    m_horizontalSkewInput->setFocus ();
88
89
}

90
kpTransformSkewDialog::~kpTransformSkewDialog ()
91
{
92
93
    s_lastWidth = width ();
    s_lastHeight = height ();
94
95
96
}


Clarence Dang's avatar
   
Clarence Dang committed
97
// private
98
void kpTransformSkewDialog::createAngleGroupBox ()
99
{
100
    auto *angleGroupBox = new QGroupBox (i18n ("Angle"), mainWidget ());
Clarence Dang's avatar
   
Clarence Dang committed
101
102
103
    addCustomWidget (angleGroupBox);


104
    auto *horizontalSkewPixmapLabel = new QLabel (angleGroupBox);
105
    horizontalSkewPixmapLabel->setPixmap (QStringLiteral(":/icons/image_skew_horizontal"));
Clarence Dang's avatar
   
Clarence Dang committed
106

107
    auto *horizontalSkewLabel = new QLabel (i18n ("&Horizontal:"), angleGroupBox);
Martin Koller's avatar
Martin Koller committed
108
109
110
111
    m_horizontalSkewInput = new QSpinBox;
    m_horizontalSkewInput->setValue(s_lastHorizontalAngle);
    m_horizontalSkewInput->setMinimum(-89);
    m_horizontalSkewInput->setMaximum(+89);
Clarence Dang's avatar
   
Clarence Dang committed
112

113
    auto *horizontalSkewDegreesLabel = new QLabel (i18n ("degrees"), angleGroupBox);
Clarence Dang's avatar
   
Clarence Dang committed
114
115


116
    auto *verticalSkewPixmapLabel = new QLabel (angleGroupBox);
117
    verticalSkewPixmapLabel->setPixmap (QStringLiteral(":/icons/image_skew_vertical"));
Clarence Dang's avatar
   
Clarence Dang committed
118

119
    auto *verticalSkewLabel = new QLabel (i18n ("&Vertical:"), angleGroupBox);
Martin Koller's avatar
Martin Koller committed
120
121
122
123
    m_verticalSkewInput = new QSpinBox;
    m_verticalSkewInput->setValue(s_lastVerticalAngle);
    m_verticalSkewInput->setMinimum(-89);
    m_verticalSkewInput->setMaximum(+89);
Clarence Dang's avatar
   
Clarence Dang committed
124

125
    auto *verticalSkewDegreesLabel = new QLabel (i18n ("degrees"), angleGroupBox);
Clarence Dang's avatar
   
Clarence Dang committed
126
127
128
129
130
131


    horizontalSkewLabel->setBuddy (m_horizontalSkewInput);
    verticalSkewLabel->setBuddy (m_verticalSkewInput);


132
    auto *angleLayout = new QGridLayout (angleGroupBox);
Clarence Dang's avatar
   
Clarence Dang committed
133
134
135

    angleLayout->addWidget (horizontalSkewPixmapLabel, 0, 0);
    angleLayout->addWidget (horizontalSkewLabel, 0, 1);
136
    angleLayout->addWidget (m_horizontalSkewInput, 0, 2, Qt::AlignVCenter);
Clarence Dang's avatar
   
Clarence Dang committed
137
138
139
140
    angleLayout->addWidget (horizontalSkewDegreesLabel, 0, 3);

    angleLayout->addWidget (verticalSkewPixmapLabel, 1, 0);
    angleLayout->addWidget (verticalSkewLabel, 1, 1);
141
    angleLayout->addWidget (m_verticalSkewInput, 1, 2, Qt::AlignVCenter);
Clarence Dang's avatar
   
Clarence Dang committed
142
143
144
    angleLayout->addWidget (verticalSkewDegreesLabel, 1, 3);


145
146
147
148
149
150
151
    connect (m_horizontalSkewInput,
             static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
             this, &kpTransformSkewDialog::slotUpdate);

    connect (m_verticalSkewInput,
             static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
             this, &kpTransformSkewDialog::slotUpdate);
Clarence Dang's avatar
   
Clarence Dang committed
152
153
154
}


155
// private virtual [base kpTransformPreviewDialog]
156
QSize kpTransformSkewDialog::newDimensions () const
Clarence Dang's avatar
   
Clarence Dang committed
157
158
{
    kpDocument *doc = document ();
159
    Q_ASSERT (doc);
Clarence Dang's avatar
   
Clarence Dang committed
160

161
    auto skewMatrix = kpPixmapFX::skewMatrix (doc->image (),
Clarence Dang's avatar
   
Clarence Dang committed
162
163
                                                  horizontalAngleForPixmapFX (),
                                                  verticalAngleForPixmapFX ());
164
    auto skewRect = skewMatrix.mapRect (doc->rect (m_actOnSelection));
Clarence Dang's avatar
   
Clarence Dang committed
165

166
    return  {skewRect.width (), skewRect.height ()};
Clarence Dang's avatar
   
Clarence Dang committed
167
168
}

169
// private virtual [base kpTransformPreviewDialog]
170
QImage kpTransformSkewDialog::transformPixmap (const QImage &image,
Clarence Dang's avatar
   
Clarence Dang committed
171
172
                                           int targetWidth, int targetHeight) const
{
173
    return kpPixmapFX::skew (image,
Clarence Dang's avatar
   
Clarence Dang committed
174
175
                             horizontalAngleForPixmapFX (),
                             verticalAngleForPixmapFX (),
176
                             m_environ->backgroundColor (m_actOnSelection),
Clarence Dang's avatar
   
Clarence Dang committed
177
178
179
180
181
182
                             targetWidth,
                             targetHeight);
}


// private
183
void kpTransformSkewDialog::updateLastAngles ()
Clarence Dang's avatar
   
Clarence Dang committed
184
185
186
187
188
{
    s_lastHorizontalAngle = horizontalAngle ();
    s_lastVerticalAngle = verticalAngle ();
}

189
// private slot virtual [base kpTransformPreviewDialog]
190
void kpTransformSkewDialog::slotUpdate ()
Clarence Dang's avatar
   
Clarence Dang committed
191
192
{
    updateLastAngles ();
193
    kpTransformPreviewDialog::slotUpdate ();
Clarence Dang's avatar
   
Clarence Dang committed
194
195
196
197
}


// public
198
int kpTransformSkewDialog::horizontalAngle () const
Clarence Dang's avatar
   
Clarence Dang committed
199
200
201
202
203
{
    return m_horizontalSkewInput->value ();
}

// public
204
int kpTransformSkewDialog::verticalAngle () const
Clarence Dang's avatar
   
Clarence Dang committed
205
206
207
208
209
210
{
    return m_verticalSkewInput->value ();
}


// public static
211
int kpTransformSkewDialog::horizontalAngleForPixmapFX (int hangle)
Clarence Dang's avatar
   
Clarence Dang committed
212
213
214
215
216
{
    return -hangle;
}

// public static
217
int kpTransformSkewDialog::verticalAngleForPixmapFX (int vangle)
Clarence Dang's avatar
   
Clarence Dang committed
218
219
{
    return -vangle;
220
221
}

Clarence Dang's avatar
   
Clarence Dang committed
222
223

// public
224
int kpTransformSkewDialog::horizontalAngleForPixmapFX () const
225
{
226
    return kpTransformSkewDialog::horizontalAngleForPixmapFX (horizontalAngle ());
227
228
}

Clarence Dang's avatar
   
Clarence Dang committed
229
// public
230
int kpTransformSkewDialog::verticalAngleForPixmapFX () const
Clarence Dang's avatar
   
Clarence Dang committed
231
{
232
    return kpTransformSkewDialog::verticalAngleForPixmapFX (verticalAngle ());
Clarence Dang's avatar
   
Clarence Dang committed
233
234
235
}


236
// public virtual [base kpTransformPreviewDialog]
237
bool kpTransformSkewDialog::isNoOp () const
238
239
240
{
    return (horizontalAngle () == 0) && (verticalAngle () == 0);
}
Clarence Dang's avatar
   
Clarence Dang committed
241

242

243
// private slot virtual [base QDialog]
244
void kpTransformSkewDialog::accept ()
245
{
246
247
    KLocalizedString message;
    QString caption, continueButtonText;
248
249
250

    if (document ()->selection ())
    {
251
        if (!document ()->textSelection ())
252
        {
253
            message =
254
                ki18n ("<qt><p>Skewing the selection to %1x%2"
255
256
257
258
259
                      " may take a substantial amount of memory."
                      " This can reduce system"
                      " responsiveness and cause other application resource"
                      " problems.</p>"

260
                      "<p>Are you sure you want to skew the selection?</p></qt>");
261

262
            caption = i18nc ("@title:window", "Skew Selection?");
263
            continueButtonText = i18n ("Sk&ew Selection");
264
265
266
267
        }
    }
    else
    {
268
        message =
269
            ki18n ("<qt><p>Skewing the image to %1x%2"
270
271
272
273
274
                  " may take a substantial amount of memory."
                  " This can reduce system"
                  " responsiveness and cause other application resource"
                  " problems.</p>"

275
                  "<p>Are you sure you want to skew the image?</p></qt>");
276

277
        caption = i18nc ("@title:window", "Skew Image?");
278
        continueButtonText = i18n ("Sk&ew Image");
279
280
281
282
283
284
285
286
287
    }


    const int newWidth = newDimensions ().width ();
    const int newHeight = newDimensions ().height ();

    if (kpTool::warnIfBigImageSize (m_oldWidth,
            m_oldHeight,
            newWidth, newHeight,
288
            message.subs (newWidth).subs (newHeight).toString (),
289
290
            caption,
            continueButtonText,
291
292
            this))
    {
Christoph Feck's avatar
Christoph Feck committed
293
        QDialog::accept ();
294
295
296
    }
}

297