1
/* ============================================================
3
* This file is a part of digiKam project
4
* http://www.digikam.org
7
* Description : a Digikam image editor plugin for superimpose a
8
* template to an image.
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>
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)
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.
24
* ============================================================ */
26
#include "superimposewidget.moc"
35
#include <QPaintEvent>
36
#include <QResizeEvent>
38
#include <QMouseEvent>
42
#include <kstandarddirs.h>
48
#include "superimpose.h"
50
namespace DigikamDecorateImagePlugin
53
SuperImposeWidget::SuperImposeWidget(int w, int h, QWidget* parent)
56
setAttribute(Qt::WA_DeleteOnClose);
57
m_bgColor = palette().color(QPalette::Background);
58
m_pixmap = new QPixmap(w, h);
61
ImageIface iface(0, 0);
62
m_w = iface.originalWidth();
63
m_h = iface.originalHeight();
66
setMouseTracking(true);
71
SuperImposeWidget::~SuperImposeWidget()
76
DImg SuperImposeWidget::makeSuperImpose()
78
ImageIface iface(0, 0);
79
SuperImpose superimpose(iface.getOriginalImg(), &m_template, m_currentSelection);
80
return superimpose.getTargetImage();
83
void SuperImposeWidget::resetEdit()
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());
92
void SuperImposeWidget::makePixmap()
94
ImageIface iface(0, 0);
95
SuperImpose superimpose(iface.getOriginalImg(), &m_templateScaled, m_currentSelection);
96
DImg image = superimpose.getTargetImage();
98
m_pixmap->fill(m_bgColor);
100
QPixmap imagePix = image.convertToPixmap();
101
p.drawPixmap(m_rect.x(), m_rect.y(), imagePix, 0, 0, m_rect.width(), m_rect.height());
105
void SuperImposeWidget::resizeEvent(QResizeEvent* e)
109
int w = e->size().width();
110
int h = e->size().height();
111
m_pixmap = new QPixmap(w, h);
113
if (!m_template.isNull())
115
int templateWidth = m_template.width();
116
int templateHeight = m_template.height();
118
if (templateWidth < templateHeight)
120
int neww = (int) ((float)height() / (float)templateHeight * (float)templateWidth);
121
m_rect = QRect(width()/2-neww/2, 0, neww, height());
125
int newh = (int) ((float)width() / (float)templateWidth * (float)templateHeight);
126
m_rect = QRect(0, height()/2-newh/2, width(), newh);
129
m_templateScaled = m_template.smoothScale(m_rect.width(), m_rect.height());
135
m_pixmap->fill(palette().color(QPalette::Background));
141
void SuperImposeWidget::setBackgroundColor(const QColor& bg)
148
void SuperImposeWidget::paintEvent(QPaintEvent*)
151
p.drawPixmap(0, 0, *m_pixmap);
155
void SuperImposeWidget::slotEditModeChanged(int mode)
160
void SuperImposeWidget::slotSetCurrentTemplate(const KUrl& url)
162
m_template.load(url.toLocalFile());
164
if (m_template.isNull())
170
int templateWidth = m_template.width();
171
int templateHeight = m_template.height();
173
if (templateWidth < templateHeight)
175
int neww = (int) ((float)height() / (float)templateHeight * (float)templateWidth);
176
m_rect = QRect(width()/2-neww/2, 0, neww, height());
180
int newh = (int) ((float)width() / (float)templateWidth * (float)templateHeight);
181
m_rect = QRect(0, height()/2-newh/2, width(), newh);
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());
190
void SuperImposeWidget::moveSelection(int dx, int dy)
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();
196
selection.translate( -(int)(wf*(float)dx), -(int)(hf*(float)dy) );
198
if (selection.left() < 0)
200
selection.moveLeft(0);
203
if (selection.top() < 0)
205
selection.moveTop(0);
208
if (selection.bottom() > m_h)
210
selection.moveBottom(m_h);
213
if (selection.right() > m_w)
215
selection.moveRight(m_w);
218
m_currentSelection = selection;
221
bool SuperImposeWidget::zoomSelection(float deltaZoomFactor)
223
float newZoom = m_zoomFactor + deltaZoomFactor;
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;
236
selection.setLeft(m_currentSelection.left() + deltaX);
237
selection.setTop(m_currentSelection.top() + deltaY);
238
selection.setWidth(wf);
239
selection.setHeight(hf);
241
// check that selection is still inside original image
242
QRect orgImageRect(0, 0, m_w, m_h);
244
if (!orgImageRect.contains(selection))
247
if (selection.left() < 0)
249
selection.moveLeft(0);
252
if (selection.top() < 0)
254
selection.moveTop(0);
257
if (selection.bottom() > m_h)
259
selection.moveBottom(m_h);
262
if (selection.right() > m_w)
264
selection.moveRight(m_w);
267
// was it successful?
268
if (selection.contains(orgImageRect))
275
m_zoomFactor = newZoom;
276
m_currentSelection = selection;
284
void SuperImposeWidget::mousePressEvent(QMouseEvent* e)
286
if ( isEnabled() && e->button() == Qt::LeftButton &&
287
rect().contains( e->x(), e->y() ) )
293
if (zoomSelection(+0.05F))
295
moveSelection(width()/2 - e->x(), height()/2 - e->y());
302
if (zoomSelection(-0.05F))
304
moveSelection(width()/2 - e->x(), height()/2 - e->y());
317
void SuperImposeWidget::mouseReleaseEvent(QMouseEvent*)
322
void SuperImposeWidget::mouseMoveEvent(QMouseEvent* e)
326
if ( e->buttons() == Qt::LeftButton )
335
int newxpos = e->x();
336
int newypos = e->y();
338
if (newxpos < m_rect.left())
340
newxpos = m_rect.left();
343
if (newxpos > m_rect.right())
345
newxpos = m_rect.right();
348
if (newxpos < m_rect.top())
350
newxpos = m_rect.top();
353
if (newxpos > m_rect.bottom())
355
newxpos = m_rect.bottom();
358
moveSelection(newxpos - m_xpos, newypos - m_ypos);
364
setCursor( Qt::PointingHandCursor );
368
else if (rect().contains( e->x(), e->y() ))
375
void SuperImposeWidget::setEditModeCursor()
381
setCursor ( Qt::CrossCursor );
385
setCursor ( Qt::SizeAllCursor );
390
} // namespace DigikamDecorateImagePlugin