summaryrefslogtreecommitdiffstats
path: root/kolourpaint/tools/kptoolpreviewdialog.cpp
blob: 6212d7af094c494735872d50a6d94b1e935aef4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431

/*
   Copyright (c) 2003,2004,2005 Clarence Dang <dang@kde.org>
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   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.

   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,
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#define DEBUG_KP_TOOL_PREVIEW_DIALOG 0

#include <kptoolpreviewdialog.h>

#include <tqapplication.h>
#include <layout.h>
#include <tqgroupbox.h>
#include <tqlabel.h>
#include <tqpushbutton.h>

#include <kdebug.h>
#include <klocale.h>

#include <kpcolor.h>
#include <kpdocument.h>
#include <kpmainwindow.h>
#include <kppixmapfx.h>
#include <kpresizesignallinglabel.h>
#include <kpselection.h>


kpToolPreviewDialog::kpToolPreviewDialog (Features features,
                                          bool reserveTopRow,
                                          const TQString &caption,
                                          const TQString &afterActionText,
                                          bool actOnSelection,
                                          kpMainWindow *parent,
                                          const char *name)
    : KDialogBase (parent, name, true/*modal*/,
                   caption,
                   KDialogBase::Ok | KDialogBase::Cancel),
      m_afterActionText (afterActionText),
      m_actOnSelection (actOnSelection),
      m_mainWindow (parent),
      m_dimensionsGroupBox (0),
      m_afterTransformDimensionsLabel (0),
      m_previewGroupBox (0),
      m_previewPixmapLabel (0),
      m_gridLayout (0)
{
    TQWidget *baseWidget = new TQWidget (this);
    setMainWidget (baseWidget);


    if (document ())
    {
        m_oldWidth = document ()->width (actOnSelection);
        m_oldHeight = document ()->height (actOnSelection);
    }
    else
    {
        m_oldWidth = m_oldHeight = 1;
    }


    if (features & Dimensions)
        createDimensionsGroupBox ();

    if (features & Preview)
        createPreviewGroupBox ();


    m_gridLayout = new TQGridLayout (baseWidget, 4, 2,
                                    0/*margin*/, spacingHint ());
    m_gridNumRows = reserveTopRow ? 1 : 0;
    if (m_dimensionsGroupBox || m_previewGroupBox)
    {
        if (m_dimensionsGroupBox && m_previewGroupBox)
        {
            m_gridLayout->addWidget (m_dimensionsGroupBox, m_gridNumRows, 0);
            m_gridLayout->addWidget (m_previewGroupBox, m_gridNumRows, 1);

            m_gridLayout->setColStretch (1, 1);
        }
        else if (m_dimensionsGroupBox)
        {
            m_gridLayout->addMultiCellWidget (m_dimensionsGroupBox,
                                              m_gridNumRows, m_gridNumRows, 0, 1);
        }
        else if (m_previewGroupBox)
        {
            m_gridLayout->addMultiCellWidget (m_previewGroupBox,
                                              m_gridNumRows, m_gridNumRows, 0, 1);
        }

        m_gridLayout->setRowStretch (m_gridNumRows, 1);
        m_gridNumRows++;;
    }
}

kpToolPreviewDialog::~kpToolPreviewDialog ()
{
}


// private
void kpToolPreviewDialog::createDimensionsGroupBox ()
{
    m_dimensionsGroupBox = new TQGroupBox (i18n ("Dimensions"), mainWidget ());

    TQLabel *originalLabel = new TQLabel (i18n ("Original:"), m_dimensionsGroupBox);
    TQString originalDimensions;
    if (document ())
    {
         originalDimensions = i18n ("%1 x %2")
                                  .arg (m_oldWidth)
                                  .arg (m_oldHeight);

         // Stop the Dimensions Group Box from resizing so often
         const TQString minimumLengthString ("100000 x 100000");
         const int padLength = minimumLengthString.length ();
         for (int i = originalDimensions.length (); i < padLength; i++)
             originalDimensions += " ";
    }
    TQLabel *originalDimensionsLabel = new TQLabel (originalDimensions, m_dimensionsGroupBox);

    TQLabel *afterTransformLabel = new TQLabel (m_afterActionText, m_dimensionsGroupBox);
    m_afterTransformDimensionsLabel = new TQLabel (m_dimensionsGroupBox);


    TQGridLayout *dimensionsLayout = new TQGridLayout (m_dimensionsGroupBox,
                                                     2, 2,
                                                     marginHint () * 2, spacingHint ());

    dimensionsLayout->addWidget (originalLabel, 0, 0, TQt::AlignBottom);
    dimensionsLayout->addWidget (originalDimensionsLabel, 0, 1, TQt::AlignBottom);
    dimensionsLayout->addWidget (afterTransformLabel, 1, 0, TQt::AlignTop);
    dimensionsLayout->addWidget (m_afterTransformDimensionsLabel, 1, 1, TQt::AlignTop);
}

// private
void kpToolPreviewDialog::createPreviewGroupBox ()
{
    m_previewGroupBox = new TQGroupBox (i18n ("Preview"), mainWidget ());

    m_previewPixmapLabel = new kpResizeSignallingLabel (m_previewGroupBox);
    m_previewPixmapLabel->setMinimumSize (150, 110);
    connect (m_previewPixmapLabel, TQT_SIGNAL (resized ()),
             this, TQT_SLOT (updatePreview ()));

    TQPushButton *updatePushButton = new TQPushButton (i18n ("&Update"),
                                                     m_previewGroupBox);
    connect (updatePushButton, TQT_SIGNAL (clicked ()),
             this, TQT_SLOT (slotUpdateWithWaitCursor ()));


    TQVBoxLayout *previewLayout = new TQVBoxLayout (m_previewGroupBox,
                                                  marginHint () * 2,
                                                  TQMAX (1, spacingHint () / 2));

    previewLayout->addWidget (m_previewPixmapLabel, 1/*stretch*/);
    previewLayout->addWidget (updatePushButton, 0/*stretch*/, TQt::AlignHCenter);
}


// protected
kpDocument *kpToolPreviewDialog::document () const
{
    return m_mainWindow ? m_mainWindow->document () : 0;
}


// protected
void kpToolPreviewDialog::addCustomWidgetToFront (TQWidget *w)
{
    m_gridLayout->addMultiCellWidget (w, 0, 0, 0, 1);
}

// protected
void kpToolPreviewDialog::addCustomWidget (TQWidget *w)
{
    m_gridLayout->addMultiCellWidget (w, m_gridNumRows, m_gridNumRows, 0, 1);
    m_gridNumRows++;
}


// private
void kpToolPreviewDialog::updateDimensions ()
{
    if (!m_dimensionsGroupBox)
        return;

    kpDocument *doc = document ();
    if (!doc)
        return;

    TQSize newDim = newDimensions ();
#if DEBUG_KP_TOOL_PREVIEW_DIALOG
    kdDebug () << "kpToolPreviewDialog::updateDimensions(): newDim=" << newDim << endl;
#endif

    TQString newDimString = i18n ("%1 x %2")
                               .arg (newDim.width ())
                               .arg (newDim.height ());
    m_afterTransformDimensionsLabel->setText (newDimString);
}


// public static
double kpToolPreviewDialog::aspectScale (int newWidth, int newHeight,
                                         int oldWidth, int oldHeight)
{
    double widthScale = double (newWidth) / double (oldWidth);
    double heightScale = double (newHeight) / double (oldHeight);

    // Keeps aspect ratio
    return TQMIN (widthScale, heightScale);
}

// public static
int kpToolPreviewDialog::scaleDimension (int dimension, double scale, int min, int max)
{
    return TQMAX (min,
                 TQMIN (max,
                       tqRound (dimension * scale)));
}


// private
void kpToolPreviewDialog::updateShrukenDocumentPixmap ()
{
#if DEBUG_KP_TOOL_PREVIEW_DIALOG
    kdDebug () << "kpToolPreviewDialog::updateShrukenDocumentPixmap()"
               << " shrunkenDocPixmap.size="
               << m_shrunkenDocumentPixmap.size ()
               << " previewPixmapLabelSizeWhenUpdatedPixmap="
               << m_previewPixmapLabelSizeWhenUpdatedPixmap
               << " previewPixmapLabel.size="
               << m_previewPixmapLabel->size ()
               << endl;
#endif

    if (!m_previewGroupBox)
        return;


    kpDocument *doc = document ();
    if (!doc || !doc->pixmap ())
    {
        kdError () << "kpToolPreviewDialog::updateShrunkenDocumentPixmap() doc="
                   << doc << endl;
        return;
    }

    if (m_shrunkenDocumentPixmap.isNull () ||
        m_previewPixmapLabel->size () != m_previewPixmapLabelSizeWhenUpdatedPixmap)
    {
    #if DEBUG_KP_TOOL_PREVIEW_DIALOG
        kdDebug () << "\tupdating shrunkenDocPixmap" << endl;
    #endif

        // TODO: Why the need to keep aspect ratio here?
        //       Isn't scaling the skewed result maintaining aspect enough?
        double keepsAspectScale = aspectScale (m_previewPixmapLabel->width (),
                                               m_previewPixmapLabel->height (),
                                               m_oldWidth,
                                               m_oldHeight);

        TQPixmap pixmap;

        if (m_actOnSelection)
        {
            kpSelection sel = *doc->selection ();
            if (!sel.pixmap ())
                sel.setPixmap (doc->getSelectedPixmap ());

            pixmap = sel.transparentPixmap ();
        }
        else
        {
            pixmap = *doc->pixmap ();
        }

        m_shrunkenDocumentPixmap = kpPixmapFX::scale (
            pixmap,
            scaleDimension (m_oldWidth,
                            keepsAspectScale,
                            1, m_previewPixmapLabel->width ()),
            scaleDimension (m_oldHeight,
                            keepsAspectScale,
                            1, m_previewPixmapLabel->height ()));
    #if 0
        m_shrunkenDocumentPixmap = kpPixmapFX::scale (
            m_actOnSelection ? doc->getSelectedPixmap () : *doc->pixmap (),
            m_previewPixmapLabel->width (),
            m_previewPixmapLabel->height ());
    #endif

        m_previewPixmapLabelSizeWhenUpdatedPixmap = m_previewPixmapLabel->size ();
    }
}


// private
void kpToolPreviewDialog::updatePreview ()
{
#if DEBUG_KP_TOOL_PREVIEW_DIALOG
    kdDebug () << "kpToolPreviewDialog::updatePreview()" << endl;
#endif

    if (!m_previewGroupBox)
        return;


    kpDocument *doc = document ();
    if (!doc)
        return;

    updateShrukenDocumentPixmap ();

    if (!m_shrunkenDocumentPixmap.isNull ())
    {
        TQSize newDim = newDimensions ();
        double keepsAspectScale = aspectScale (m_previewPixmapLabel->width (),
                                               m_previewPixmapLabel->height (),
                                               newDim.width (),
                                               newDim.height ());

        int targetWidth = scaleDimension (newDim.width (),
                                          keepsAspectScale,
                                          1,  // min
                                          m_previewPixmapLabel->width ());  // max
        int targetHeight = scaleDimension (newDim.height (),
                                           keepsAspectScale,
                                           1,  // min
                                           m_previewPixmapLabel->height ());  // max

        // TODO: Some effects work directly on TQImage; so could cache the
        //       TQImage so that transformPixmap() is faster
        TQPixmap transformedShrunkenDocumentPixmap =
            transformPixmap (m_shrunkenDocumentPixmap, targetWidth, targetHeight);

        TQPixmap previewPixmap (m_previewPixmapLabel->width (),
                               m_previewPixmapLabel->height ());
        kpPixmapFX::fill (&previewPixmap, kpColor::transparent);
        kpPixmapFX::setPixmapAt (&previewPixmap,
                                 (previewPixmap.width () - transformedShrunkenDocumentPixmap.width ()) / 2,
                                 (previewPixmap.height () - transformedShrunkenDocumentPixmap.height ()) / 2,
                                 transformedShrunkenDocumentPixmap);

#if DEBUG_KP_TOOL_PREVIEW_DIALOG
    kdDebug () << "kpToolPreviewDialog::updatePreview ():"
               << "   shrunkenDocumentPixmap: w="
               << m_shrunkenDocumentPixmap.width ()
               << " h="
               << m_shrunkenDocumentPixmap.height ()
               << "   previewPixmapLabel: w="
               << m_previewPixmapLabel->width ()
               << " h="
               << m_previewPixmapLabel->height ()
               << "   transformedShrunkenDocumentPixmap: w="
               << transformedShrunkenDocumentPixmap.width ()
               << " h="
               << transformedShrunkenDocumentPixmap.height ()
               << "   previewPixmap: w="
               << previewPixmap.width ()
               << " h="
               << previewPixmap.height ()
               << endl;
#endif

        m_previewPixmapLabel->setPixmap (previewPixmap);

        // immediate update esp. for expensive previews
        m_previewPixmapLabel->repaint (false/*no erase*/);

#if DEBUG_KP_TOOL_PREVIEW_DIALOG
    kdDebug () << "\tafter TQLabel::setPixmap() previewPixmapLabel: w="
               << m_previewPixmapLabel->width ()
               << " h="
               << m_previewPixmapLabel->height ()
               << endl;
#endif
    }
}


// protected slot virtual
void kpToolPreviewDialog::slotUpdate ()
{
#if DEBUG_KP_TOOL_PREVIEW_DIALOG
    kdDebug () << "kpToolPreviewDialog::slotUpdate()" << endl;
#endif
    updateDimensions ();
    updatePreview ();
}

// protected slot virtual
void kpToolPreviewDialog::slotUpdateWithWaitCursor ()
{
#if DEBUG_KP_TOOL_PREVIEW_DIALOG
    kdDebug () << "kpToolPreviewDialog::slotUpdateWithWaitCursor()"
               << endl;
#endif

    TQApplication::setOverrideCursor (TQt::waitCursor);

    slotUpdate ();

    TQApplication::restoreOverrideCursor ();
}


#include <kptoolpreviewdialog.moc>