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-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>
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
* ============================================================ */
27
#include "superimposewidget.moc"
36
#include <QPaintEvent>
37
#include <QResizeEvent>
39
#include <QMouseEvent>
43
#include <kstandarddirs.h>
49
#include "superimpose.h"
51
namespace DigikamSuperImposeImagesPlugin
54
SuperImposeWidget::SuperImposeWidget(int w, int h, QWidget *parent)
57
setAttribute(Qt::WA_DeleteOnClose);
58
m_pixmap = new QPixmap(w, h);
61
Digikam::ImageIface iface(0, 0);
62
m_w = iface.originalWidth();
63
m_h = iface.originalHeight();
66
setMouseTracking(true);
71
SuperImposeWidget::~SuperImposeWidget()
76
Digikam::DImg SuperImposeWidget::makeSuperImpose(void)
78
Digikam::ImageIface iface(0, 0);
79
SuperImpose superimpose(iface.getOriginalImg(), &m_template, m_currentSelection);
80
return superimpose.getTargetImage();
83
void SuperImposeWidget::resetEdit(void)
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(void)
94
Digikam::ImageIface iface(0, 0);
95
SuperImpose superimpose(iface.getOriginalImg(), &m_templateScaled, m_currentSelection);
96
Digikam::DImg image = superimpose.getTargetImage();
98
m_pixmap->fill(palette().color(QPalette::Background));
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::paintEvent( QPaintEvent * )
144
p.drawPixmap(0, 0, *m_pixmap);
148
void SuperImposeWidget::slotEditModeChanged(int mode)
153
void SuperImposeWidget::slotSetCurrentTemplate(const KUrl& url)
155
m_template.load(url.toLocalFile());
157
if (m_template.isNull())
163
int templateWidth = m_template.width();
164
int templateHeight = m_template.height();
166
if (templateWidth < templateHeight)
168
int neww = (int) ((float)height() / (float)templateHeight * (float)templateWidth);
169
m_rect = QRect(width()/2-neww/2, 0, neww, height());
173
int newh = (int) ((float)width() / (float)templateWidth * (float)templateHeight);
174
m_rect = QRect(0, height()/2-newh/2, width(), newh);
177
m_templateScaled = m_template.smoothScale(m_rect.width(), m_rect.height());
179
m_currentSelection = QRect(m_w/2 - m_rect.width()/2, m_h/2 - m_rect.height()/2, m_rect.width(), m_rect.height());
183
void SuperImposeWidget::moveSelection(int dx, int dy)
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();
189
selection.translate( -(int)(wf*(float)dx), -(int)(hf*(float)dy) );
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);
200
m_currentSelection = selection;
203
bool SuperImposeWidget::zoomSelection(float deltaZoomFactor)
205
float newZoom = m_zoomFactor + deltaZoomFactor;
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;
216
selection.setLeft(m_currentSelection.left() + deltaX);
217
selection.setTop(m_currentSelection.top() + deltaY);
218
selection.setWidth(wf);
219
selection.setHeight(hf);
221
// check that selection is still inside original image
222
QRect orgImageRect(0, 0, m_w, m_h);
223
if (!orgImageRect.contains(selection))
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);
235
// was it successful?
236
if (selection.contains(orgImageRect))
241
m_zoomFactor = newZoom;
242
m_currentSelection = selection;
250
void SuperImposeWidget::mousePressEvent ( QMouseEvent * e )
252
if ( isEnabled() && e->button() == Qt::LeftButton &&
253
rect().contains( e->x(), e->y() ) )
258
if (zoomSelection(+0.05F))
259
moveSelection(width()/2 - e->x(), height()/2 - e->y());
263
if (zoomSelection(-0.05F))
264
moveSelection(width()/2 - e->x(), height()/2 - e->y());
274
void SuperImposeWidget::mouseReleaseEvent ( QMouseEvent * )
279
void SuperImposeWidget::mouseMoveEvent ( QMouseEvent * e )
283
if ( e->buttons() == Qt::LeftButton )
292
int newxpos = e->x();
293
int newypos = e->y();
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();
304
moveSelection(newxpos - m_xpos, newypos - m_ypos);
310
setCursor( Qt::PointingHandCursor );
314
else if (rect().contains( e->x(), e->y() ))
321
void SuperImposeWidget::setEditModeCursor()
327
setCursor ( Qt::CrossCursor );
331
setCursor ( Qt::SizeAllCursor );
335
} // namespace DigikamSuperImposeImagesPlugin