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

« back to all changes in this revision

Viewing changes to imageplugins/superimpose/superimposewidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christian Mangold
  • Date: 2010-04-09 21:30:01 UTC
  • mfrom: (1.2.28 upstream)
  • Revision ID: james.westby@ubuntu.com-20100409213001-4bfyibrd359rn7o3
Tags: 2:1.2.0-0ubuntu1
* New upstream release (LP: #560576)
* Remove all patches, fixed upstream
  - Remove quilt build-depend

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-2008 by Gilles Caulier <caulier dot gilles at gmail dot com>
11
 
 * Copyright (C) 2006-2008 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
 
 
27
 
#include "superimposewidget.moc"
28
 
 
29
 
// C++ includes
30
 
 
31
 
#include <cstdio>
32
 
 
33
 
// Qt includes
34
 
 
35
 
#include <QPainter>
36
 
#include <QPaintEvent>
37
 
#include <QResizeEvent>
38
 
#include <QPixmap>
39
 
#include <QMouseEvent>
40
 
 
41
 
// KDE includes
42
 
 
43
 
#include <kstandarddirs.h>
44
 
#include <kcursor.h>
45
 
#include <kglobal.h>
46
 
 
47
 
// Local includes
48
 
 
49
 
#include "superimpose.h"
50
 
 
51
 
namespace DigikamSuperImposeImagesPlugin
52
 
{
53
 
 
54
 
SuperImposeWidget::SuperImposeWidget(int w, int h, QWidget *parent)
55
 
                 : QWidget(parent)
56
 
{
57
 
    setAttribute(Qt::WA_DeleteOnClose);
58
 
    m_pixmap   = new QPixmap(w, h);
59
 
    m_editMode = MOVE;
60
 
 
61
 
    Digikam::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
 
Digikam::DImg SuperImposeWidget::makeSuperImpose(void)
77
 
{
78
 
    Digikam::ImageIface iface(0, 0);
79
 
    SuperImpose superimpose(iface.getOriginalImg(), &m_template, m_currentSelection);
80
 
    return superimpose.getTargetImage();
81
 
}
82
 
 
83
 
void SuperImposeWidget::resetEdit(void)
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(void)
93
 
{
94
 
    Digikam::ImageIface iface(0, 0);
95
 
    SuperImpose superimpose(iface.getOriginalImg(), &m_templateScaled, m_currentSelection);
96
 
    Digikam::DImg image = superimpose.getTargetImage();
97
 
 
98
 
    m_pixmap->fill(palette().color(QPalette::Background));
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::paintEvent( QPaintEvent * )
142
 
{
143
 
    QPainter p(this);
144
 
    p.drawPixmap(0, 0, *m_pixmap);
145
 
    p.end();
146
 
}
147
 
 
148
 
void SuperImposeWidget::slotEditModeChanged(int mode)
149
 
{
150
 
    m_editMode = mode;
151
 
}
152
 
 
153
 
void SuperImposeWidget::slotSetCurrentTemplate(const KUrl& url)
154
 
{
155
 
    m_template.load(url.toLocalFile());
156
 
 
157
 
    if (m_template.isNull())
158
 
    {
159
 
        m_rect = QRect();
160
 
        return;
161
 
    }
162
 
 
163
 
    int templateWidth  = m_template.width();
164
 
    int templateHeight = m_template.height();
165
 
 
166
 
    if (templateWidth < templateHeight)
167
 
    {
168
 
        int neww = (int) ((float)height() / (float)templateHeight * (float)templateWidth);
169
 
        m_rect = QRect(width()/2-neww/2, 0, neww, height());
170
 
    }
171
 
    else
172
 
    {
173
 
        int newh = (int) ((float)width() / (float)templateWidth * (float)templateHeight);
174
 
        m_rect = QRect(0, height()/2-newh/2, width(), newh);
175
 
    }
176
 
 
177
 
    m_templateScaled = m_template.smoothScale(m_rect.width(), m_rect.height());
178
 
 
179
 
    m_currentSelection = QRect(m_w/2 - m_rect.width()/2, m_h/2 - m_rect.height()/2, m_rect.width(), m_rect.height());
180
 
    zoomSelection(0);
181
 
}
182
 
 
183
 
void SuperImposeWidget::moveSelection(int dx, int dy)
184
 
{
185
 
    QRect selection = m_currentSelection;
186
 
    float wf = (float)selection.width() / (float)m_rect.width();
187
 
    float hf = (float)selection.height() / (float)m_rect.height();
188
 
 
189
 
    selection.translate( -(int)(wf*(float)dx), -(int)(hf*(float)dy) );
190
 
 
191
 
    if (selection.left() < 0)
192
 
        selection.moveLeft(0);
193
 
    if (selection.top() < 0)
194
 
        selection.moveTop(0);
195
 
    if (selection.bottom() > m_h)
196
 
        selection.moveBottom(m_h);
197
 
    if (selection.right() > m_w)
198
 
        selection.moveRight(m_w);
199
 
 
200
 
    m_currentSelection = selection;
201
 
}
202
 
 
203
 
bool SuperImposeWidget::zoomSelection(float deltaZoomFactor)
204
 
{
205
 
    float newZoom = m_zoomFactor + deltaZoomFactor;
206
 
 
207
 
    if (newZoom < 0.0)
208
 
        return false;
209
 
 
210
 
    QRect selection = m_currentSelection;
211
 
    int wf = (int)((float)m_rect.width()  / newZoom);
212
 
    int hf = (int)((float)m_rect.height() / newZoom);
213
 
    int deltaX = (m_currentSelection.width()  - wf) / 2;
214
 
    int deltaY = (m_currentSelection.height() - hf) / 2;
215
 
 
216
 
    selection.setLeft(m_currentSelection.left() + deltaX);
217
 
    selection.setTop(m_currentSelection.top() + deltaY);
218
 
    selection.setWidth(wf);
219
 
    selection.setHeight(hf);
220
 
 
221
 
    // check that selection is still inside original image
222
 
    QRect orgImageRect(0, 0, m_w, m_h);
223
 
    if (!orgImageRect.contains(selection))
224
 
    {
225
 
        // try to adjust
226
 
        if (selection.left() < 0)
227
 
            selection.moveLeft(0);
228
 
        if (selection.top() < 0)
229
 
            selection.moveTop(0);
230
 
        if (selection.bottom() > m_h)
231
 
            selection.moveBottom(m_h);
232
 
        if (selection.right() > m_w)
233
 
            selection.moveRight(m_w);
234
 
 
235
 
        // was it successful?
236
 
        if (selection.contains(orgImageRect))
237
 
            return false;
238
 
 
239
 
    }
240
 
 
241
 
    m_zoomFactor = newZoom;
242
 
    m_currentSelection = selection;
243
 
 
244
 
    makePixmap();
245
 
    repaint();
246
 
 
247
 
    return true;
248
 
}
249
 
 
250
 
void SuperImposeWidget::mousePressEvent ( QMouseEvent * e )
251
 
{
252
 
    if ( isEnabled() && e->button() == Qt::LeftButton &&
253
 
         rect().contains( e->x(), e->y() ) )
254
 
    {
255
 
        switch (m_editMode)
256
 
        {
257
 
            case ZOOMIN:
258
 
                if (zoomSelection(+0.05F))
259
 
                    moveSelection(width()/2 - e->x(), height()/2 - e->y());
260
 
                break;
261
 
 
262
 
            case ZOOMOUT:
263
 
                if (zoomSelection(-0.05F))
264
 
                    moveSelection(width()/2 - e->x(), height()/2 - e->y());
265
 
                break;
266
 
 
267
 
            case MOVE:
268
 
                m_xpos = e->x();
269
 
                m_ypos = e->y();
270
 
        }
271
 
    }
272
 
}
273
 
 
274
 
void SuperImposeWidget::mouseReleaseEvent ( QMouseEvent * )
275
 
{
276
 
    setEditModeCursor();
277
 
}
278
 
 
279
 
void SuperImposeWidget::mouseMoveEvent ( QMouseEvent * e )
280
 
{
281
 
    if ( isEnabled() )
282
 
    {
283
 
        if ( e->buttons() == Qt::LeftButton )
284
 
        {
285
 
            switch (m_editMode)
286
 
            {
287
 
                case ZOOMIN:
288
 
                case ZOOMOUT:
289
 
                    break;
290
 
 
291
 
                case MOVE:
292
 
                    int newxpos = e->x();
293
 
                    int newypos = e->y();
294
 
 
295
 
                    if (newxpos < m_rect.left())
296
 
                        newxpos = m_rect.left();
297
 
                    if (newxpos > m_rect.right())
298
 
                        newxpos = m_rect.right();
299
 
                    if (newxpos < m_rect.top())
300
 
                        newxpos = m_rect.top();
301
 
                    if (newxpos > m_rect.bottom())
302
 
                        newxpos = m_rect.bottom();
303
 
 
304
 
                    moveSelection(newxpos - m_xpos, newypos - m_ypos);
305
 
                    makePixmap();
306
 
                    repaint();
307
 
 
308
 
                    m_xpos = newxpos;
309
 
                    m_ypos = newypos;
310
 
                    setCursor( Qt::PointingHandCursor );
311
 
                    break;
312
 
            }
313
 
        }
314
 
        else if (rect().contains( e->x(), e->y() ))
315
 
        {
316
 
            setEditModeCursor();
317
 
        }
318
 
    }
319
 
}
320
 
 
321
 
void SuperImposeWidget::setEditModeCursor()
322
 
{
323
 
    switch (m_editMode)
324
 
    {
325
 
        case ZOOMIN:
326
 
        case ZOOMOUT:
327
 
            setCursor ( Qt::CrossCursor );
328
 
            break;
329
 
 
330
 
        case MOVE:
331
 
            setCursor ( Qt::SizeAllCursor );
332
 
    }
333
 
}
334
 
 
335
 
}  // namespace DigikamSuperImposeImagesPlugin