~ubuntu-branches/ubuntu/saucy/digikam/saucy

« back to all changes in this revision

Viewing changes to core/imageplugins/decorate/superimpose/superimposewidget.cpp

  • Committer: Package Import Robot
  • Author(s): Felix Geyer, Rohan Garg, Philip Muškovac, Felix Geyer
  • Date: 2011-09-23 18:18:55 UTC
  • mfrom: (1.2.36 upstream)
  • Revision ID: package-import@ubuntu.com-20110923181855-ifs67wxkugshev9k
Tags: 2:2.1.1-0ubuntu1
[ Rohan Garg ]
* New upstream release (LP: #834190)
  - debian/control
    + Build with libqtwebkit-dev
 - debian/kipi-plugins-common
    + Install libkvkontakte required by kipi-plugins
 - debian/digikam
    + Install panoramagui

[ Philip Muškovac ]
* New upstream release
  - debian/control:
    + Add libcv-dev, libcvaux-dev, libhighgui-dev, libboost-graph1.46-dev,
      libksane-dev, libxml2-dev, libxslt-dev, libqt4-opengl-dev, libqjson-dev,
      libgpod-dev and libqca2-dev to build-deps
    + Add packages for kipi-plugins, libmediawiki, libkface, libkgeomap and
      libkvkontakte
  - debian/rules:
    + Don't build with gphoto2 since it doesn't build with it.
  - Add kubuntu_fix_test_linking.diff to fix linking of the dngconverter test
  - update install files
  - update kubuntu_01_mysqld_executable_name.diff for new cmake layout
    and rename to kubuntu_mysqld_executable_name.diff
* Fix typo in digikam-data description (LP: #804894)
* Fix Vcs links

[ Felix Geyer ]
* Move library data files to the new packages libkface-data, libkgeomap-data
  and libkvkontakte-data.
* Override version of the embedded library packages to 1.0~digikam<version>.
* Exclude the library packages from digikam-dbg to prevent file conflicts in
  the future.
* Call dh_install with --list-missing.

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        : 2005-01-04
 
7
 * Description : a Digikam image editor plugin for superimpose a
 
8
 *               template to an image.
 
9
 *
 
10
 * Copyright (C) 2005-2010 by Gilles Caulier <caulier dot gilles at gmail dot com>
 
11
 * Copyright (C) 2006-2010 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
 
12
 *
 
13
 * This program is free software; you can redistribute it
 
14
 * and/or modify it under the terms of the GNU General
 
15
 * Public License as published by the Free Software Foundation;
 
16
 * either version 2, or (at your option)
 
17
 * any later version.
 
18
 *
 
19
 * This program is distributed in the hope that it will be useful,
 
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
22
 * GNU General Public License for more details.
 
23
 *
 
24
 * ============================================================ */
 
25
 
 
26
#include "superimposewidget.moc"
 
27
 
 
28
// C++ includes
 
29
 
 
30
#include <cstdio>
 
31
 
 
32
// Qt includes
 
33
 
 
34
#include <QPainter>
 
35
#include <QPaintEvent>
 
36
#include <QResizeEvent>
 
37
#include <QPixmap>
 
38
#include <QMouseEvent>
 
39
 
 
40
// KDE includes
 
41
 
 
42
#include <kstandarddirs.h>
 
43
#include <kcursor.h>
 
44
#include <kglobal.h>
 
45
 
 
46
// Local includes
 
47
 
 
48
#include "superimpose.h"
 
49
 
 
50
namespace DigikamDecorateImagePlugin
 
51
{
 
52
 
 
53
SuperImposeWidget::SuperImposeWidget(int w, int h, QWidget* parent)
 
54
    : QWidget(parent)
 
55
{
 
56
    setAttribute(Qt::WA_DeleteOnClose);
 
57
    m_bgColor  = palette().color(QPalette::Background);
 
58
    m_pixmap   = new QPixmap(w, h);
 
59
    m_editMode = MOVE;
 
60
 
 
61
    ImageIface iface(0, 0);
 
62
    m_w = iface.originalWidth();
 
63
    m_h = iface.originalHeight();
 
64
 
 
65
    setMinimumSize(w, h);
 
66
    setMouseTracking(true);
 
67
 
 
68
    resetEdit();
 
69
}
 
70
 
 
71
SuperImposeWidget::~SuperImposeWidget()
 
72
{
 
73
    delete m_pixmap;
 
74
}
 
75
 
 
76
DImg SuperImposeWidget::makeSuperImpose()
 
77
{
 
78
    ImageIface iface(0, 0);
 
79
    SuperImpose superimpose(iface.getOriginalImg(), &m_template, m_currentSelection);
 
80
    return superimpose.getTargetImage();
 
81
}
 
82
 
 
83
void SuperImposeWidget::resetEdit()
 
84
{
 
85
    m_zoomFactor       = 1.0;
 
86
    m_currentSelection = QRect(m_w/2 - m_rect.width()/2, m_h/2 - m_rect.height()/2,
 
87
                               m_rect.width(), m_rect.height());
 
88
    makePixmap();
 
89
    repaint();
 
90
}
 
91
 
 
92
void SuperImposeWidget::makePixmap()
 
93
{
 
94
    ImageIface iface(0, 0);
 
95
    SuperImpose superimpose(iface.getOriginalImg(), &m_templateScaled, m_currentSelection);
 
96
    DImg image = superimpose.getTargetImage();
 
97
 
 
98
    m_pixmap->fill(m_bgColor);
 
99
    QPainter p(m_pixmap);
 
100
    QPixmap imagePix = image.convertToPixmap();
 
101
    p.drawPixmap(m_rect.x(), m_rect.y(), imagePix, 0, 0, m_rect.width(), m_rect.height());
 
102
    p.end();
 
103
}
 
104
 
 
105
void SuperImposeWidget::resizeEvent(QResizeEvent* e)
 
106
{
 
107
    blockSignals(true);
 
108
    delete m_pixmap;
 
109
    int w    = e->size().width();
 
110
    int h    = e->size().height();
 
111
    m_pixmap = new QPixmap(w, h);
 
112
 
 
113
    if (!m_template.isNull())
 
114
    {
 
115
        int templateWidth  = m_template.width();
 
116
        int templateHeight = m_template.height();
 
117
 
 
118
        if (templateWidth < templateHeight)
 
119
        {
 
120
            int neww = (int) ((float)height() / (float)templateHeight * (float)templateWidth);
 
121
            m_rect   = QRect(width()/2-neww/2, 0, neww, height());
 
122
        }
 
123
        else
 
124
        {
 
125
            int newh = (int) ((float)width() / (float)templateWidth * (float)templateHeight);
 
126
            m_rect   = QRect(0, height()/2-newh/2, width(), newh);
 
127
        }
 
128
 
 
129
        m_templateScaled = m_template.smoothScale(m_rect.width(), m_rect.height());
 
130
        makePixmap();
 
131
    }
 
132
    else
 
133
    {
 
134
        m_rect = QRect();
 
135
        m_pixmap->fill(palette().color(QPalette::Background));
 
136
    }
 
137
 
 
138
    blockSignals(false);
 
139
}
 
140
 
 
141
void SuperImposeWidget::setBackgroundColor(const QColor& bg)
 
142
{
 
143
    m_bgColor = bg;
 
144
    makePixmap();
 
145
    repaint();
 
146
}
 
147
 
 
148
void SuperImposeWidget::paintEvent(QPaintEvent*)
 
149
{
 
150
    QPainter p(this);
 
151
    p.drawPixmap(0, 0, *m_pixmap);
 
152
    p.end();
 
153
}
 
154
 
 
155
void SuperImposeWidget::slotEditModeChanged(int mode)
 
156
{
 
157
    m_editMode = mode;
 
158
}
 
159
 
 
160
void SuperImposeWidget::slotSetCurrentTemplate(const KUrl& url)
 
161
{
 
162
    m_template.load(url.toLocalFile());
 
163
 
 
164
    if (m_template.isNull())
 
165
    {
 
166
        m_rect = QRect();
 
167
        return;
 
168
    }
 
169
 
 
170
    int templateWidth  = m_template.width();
 
171
    int templateHeight = m_template.height();
 
172
 
 
173
    if (templateWidth < templateHeight)
 
174
    {
 
175
        int neww = (int) ((float)height() / (float)templateHeight * (float)templateWidth);
 
176
        m_rect   = QRect(width()/2-neww/2, 0, neww, height());
 
177
    }
 
178
    else
 
179
    {
 
180
        int newh = (int) ((float)width() / (float)templateWidth * (float)templateHeight);
 
181
        m_rect   = QRect(0, height()/2-newh/2, width(), newh);
 
182
    }
 
183
 
 
184
    m_templateScaled  = m_template.smoothScale(m_rect.width(), m_rect.height());
 
185
    m_currentSelection = QRect(m_w/2 - m_rect.width()/2, m_h/2 - m_rect.height()/2,
 
186
                               m_rect.width(), m_rect.height());
 
187
    zoomSelection(0);
 
188
}
 
189
 
 
190
void SuperImposeWidget::moveSelection(int dx, int dy)
 
191
{
 
192
    QRect selection = m_currentSelection;
 
193
    float wf = (float)selection.width() / (float)m_rect.width();
 
194
    float hf = (float)selection.height() / (float)m_rect.height();
 
195
 
 
196
    selection.translate( -(int)(wf*(float)dx), -(int)(hf*(float)dy) );
 
197
 
 
198
    if (selection.left() < 0)
 
199
    {
 
200
        selection.moveLeft(0);
 
201
    }
 
202
 
 
203
    if (selection.top() < 0)
 
204
    {
 
205
        selection.moveTop(0);
 
206
    }
 
207
 
 
208
    if (selection.bottom() > m_h)
 
209
    {
 
210
        selection.moveBottom(m_h);
 
211
    }
 
212
 
 
213
    if (selection.right() > m_w)
 
214
    {
 
215
        selection.moveRight(m_w);
 
216
    }
 
217
 
 
218
    m_currentSelection = selection;
 
219
}
 
220
 
 
221
bool SuperImposeWidget::zoomSelection(float deltaZoomFactor)
 
222
{
 
223
    float newZoom = m_zoomFactor + deltaZoomFactor;
 
224
 
 
225
    if (newZoom < 0.0)
 
226
    {
 
227
        return false;
 
228
    }
 
229
 
 
230
    QRect selection = m_currentSelection;
 
231
    int wf          = (int)((float)m_rect.width()  / newZoom);
 
232
    int hf          = (int)((float)m_rect.height() / newZoom);
 
233
    int deltaX      = (m_currentSelection.width()  - wf) / 2;
 
234
    int deltaY      = (m_currentSelection.height() - hf) / 2;
 
235
 
 
236
    selection.setLeft(m_currentSelection.left() + deltaX);
 
237
    selection.setTop(m_currentSelection.top() + deltaY);
 
238
    selection.setWidth(wf);
 
239
    selection.setHeight(hf);
 
240
 
 
241
    // check that selection is still inside original image
 
242
    QRect orgImageRect(0, 0, m_w, m_h);
 
243
 
 
244
    if (!orgImageRect.contains(selection))
 
245
    {
 
246
        // try to adjust
 
247
        if (selection.left() < 0)
 
248
        {
 
249
            selection.moveLeft(0);
 
250
        }
 
251
 
 
252
        if (selection.top() < 0)
 
253
        {
 
254
            selection.moveTop(0);
 
255
        }
 
256
 
 
257
        if (selection.bottom() > m_h)
 
258
        {
 
259
            selection.moveBottom(m_h);
 
260
        }
 
261
 
 
262
        if (selection.right() > m_w)
 
263
        {
 
264
            selection.moveRight(m_w);
 
265
        }
 
266
 
 
267
        // was it successful?
 
268
        if (selection.contains(orgImageRect))
 
269
        {
 
270
            return false;
 
271
        }
 
272
 
 
273
    }
 
274
 
 
275
    m_zoomFactor       = newZoom;
 
276
    m_currentSelection = selection;
 
277
 
 
278
    makePixmap();
 
279
    repaint();
 
280
 
 
281
    return true;
 
282
}
 
283
 
 
284
void SuperImposeWidget::mousePressEvent(QMouseEvent* e)
 
285
{
 
286
    if ( isEnabled() && e->button() == Qt::LeftButton &&
 
287
         rect().contains( e->x(), e->y() ) )
 
288
    {
 
289
        switch (m_editMode)
 
290
        {
 
291
            case ZOOMIN:
 
292
 
 
293
                if (zoomSelection(+0.05F))
 
294
                {
 
295
                    moveSelection(width()/2 - e->x(), height()/2 - e->y());
 
296
                }
 
297
 
 
298
                break;
 
299
 
 
300
            case ZOOMOUT:
 
301
 
 
302
                if (zoomSelection(-0.05F))
 
303
                {
 
304
                    moveSelection(width()/2 - e->x(), height()/2 - e->y());
 
305
                }
 
306
 
 
307
                break;
 
308
 
 
309
            case MOVE:
 
310
                m_xpos = e->x();
 
311
                m_ypos = e->y();
 
312
                break;
 
313
        }
 
314
    }
 
315
}
 
316
 
 
317
void SuperImposeWidget::mouseReleaseEvent(QMouseEvent*)
 
318
{
 
319
    setEditModeCursor();
 
320
}
 
321
 
 
322
void SuperImposeWidget::mouseMoveEvent(QMouseEvent* e)
 
323
{
 
324
    if ( isEnabled() )
 
325
    {
 
326
        if ( e->buttons() == Qt::LeftButton )
 
327
        {
 
328
            switch (m_editMode)
 
329
            {
 
330
                case ZOOMIN:
 
331
                case ZOOMOUT:
 
332
                    break;
 
333
 
 
334
                case MOVE:
 
335
                    int newxpos = e->x();
 
336
                    int newypos = e->y();
 
337
 
 
338
                    if (newxpos < m_rect.left())
 
339
                    {
 
340
                        newxpos = m_rect.left();
 
341
                    }
 
342
 
 
343
                    if (newxpos > m_rect.right())
 
344
                    {
 
345
                        newxpos = m_rect.right();
 
346
                    }
 
347
 
 
348
                    if (newxpos < m_rect.top())
 
349
                    {
 
350
                        newxpos = m_rect.top();
 
351
                    }
 
352
 
 
353
                    if (newxpos > m_rect.bottom())
 
354
                    {
 
355
                        newxpos = m_rect.bottom();
 
356
                    }
 
357
 
 
358
                    moveSelection(newxpos - m_xpos, newypos - m_ypos);
 
359
                    makePixmap();
 
360
                    repaint();
 
361
 
 
362
                    m_xpos = newxpos;
 
363
                    m_ypos = newypos;
 
364
                    setCursor( Qt::PointingHandCursor );
 
365
                    break;
 
366
            }
 
367
        }
 
368
        else if (rect().contains( e->x(), e->y() ))
 
369
        {
 
370
            setEditModeCursor();
 
371
        }
 
372
    }
 
373
}
 
374
 
 
375
void SuperImposeWidget::setEditModeCursor()
 
376
{
 
377
    switch (m_editMode)
 
378
    {
 
379
        case ZOOMIN:
 
380
        case ZOOMOUT:
 
381
            setCursor ( Qt::CrossCursor );
 
382
            break;
 
383
 
 
384
        case MOVE:
 
385
            setCursor ( Qt::SizeAllCursor );
 
386
            break;
 
387
    }
 
388
}
 
389
 
 
390
}  // namespace DigikamDecorateImagePlugin