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
|
/* ============================================================
*
* This file is a part of digiKam project
* http://www.digikam.org
*
* Date : 2005-01-04
* Description : a Digikam image editor plugin for superimpose a
* template to an image.
*
* Copyright (C) 2005-2008 by Gilles Caulier <caulier dot gilles at gmail dot com>
* Copyright (C) 2006-2008 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General
* Public License as published by the Free Software Foundation;
* either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* ============================================================ */
// C++ includes.
#include <cstdio>
// TQt includes.
#include <tqpainter.h>
// KDE includes.
#include <kstandarddirs.h>
#include <kcursor.h>
#include <tdeglobal.h>
// Local includes.
#include "superimpose.h"
#include "superimposewidget.h"
#include "superimposewidget.moc"
namespace DigikamSuperImposeImagesPlugin
{
SuperImposeWidget::SuperImposeWidget(int w, int h, TQWidget *parent)
: TQWidget(parent, 0, TQt::WDestructiveClose)
{
m_pixmap = new TQPixmap(w, h);
m_editMode = MOVE;
Digikam::ImageIface iface(0, 0);
m_w = iface.originalWidth();
m_h = iface.originalHeight();
setBackgroundMode(TQt::NoBackground);
setMinimumSize(w, h);
setMouseTracking(true);
resetEdit();
}
SuperImposeWidget::~SuperImposeWidget()
{
if(m_pixmap)
delete m_pixmap;
}
Digikam::DImg SuperImposeWidget::makeSuperImpose(void)
{
Digikam::ImageIface iface(0, 0);
SuperImpose superimpose(iface.getOriginalImg(), &m_template, m_currentSelection);
return superimpose.getTargetImage();
}
void SuperImposeWidget::resetEdit(void)
{
m_zoomFactor = 1.0;
m_currentSelection = TQRect(m_w/2 - m_rect.width()/2, m_h/2 - m_rect.height()/2,
m_rect.width(), m_rect.height());
makePixmap();
repaint(false);
}
void SuperImposeWidget::makePixmap(void)
{
Digikam::ImageIface iface(0, 0);
SuperImpose superimpose(iface.getOriginalImg(), &m_templateScaled, m_currentSelection);
Digikam::DImg image = superimpose.getTargetImage();
m_pixmap->fill(colorGroup().background());
TQPainter p(m_pixmap);
TQPixmap imagePix = image.convertToPixmap();
p.drawPixmap(m_rect.x(), m_rect.y(), imagePix, 0, 0, m_rect.width(), m_rect.height());
p.end();
}
void SuperImposeWidget::resizeEvent(TQResizeEvent * e)
{
blockSignals(true);
delete m_pixmap;
int w = e->size().width();
int h = e->size().height();
m_pixmap = new TQPixmap(w, h);
if (!m_template.isNull())
{
int templateWidth = m_template.width();
int templateHeight = m_template.height();
if (templateWidth < templateHeight)
{
int neww = (int) ((float)height() / (float)templateHeight * (float)templateWidth);
m_rect = TQRect(width()/2-neww/2, 0, neww, height());
}
else
{
int newh = (int) ((float)width() / (float)templateWidth * (float)templateHeight);
m_rect = TQRect(0, height()/2-newh/2, width(), newh);
}
m_templateScaled = m_template.smoothScale(m_rect.width(), m_rect.height());
makePixmap();
}
else
{
m_rect = TQRect();
m_pixmap->fill(colorGroup().background());
}
blockSignals(false);
}
void SuperImposeWidget::paintEvent( TQPaintEvent * )
{
bitBlt(this, 0, 0, m_pixmap);
}
void SuperImposeWidget::slotEditModeChanged(int mode)
{
m_editMode = mode;
}
void SuperImposeWidget::slotSetCurrentTemplate(const KURL& url)
{
m_template.load(url.path());
if (m_template.isNull())
{
m_rect = TQRect();
return;
}
int templateWidth = m_template.width();
int templateHeight = m_template.height();
if (templateWidth < templateHeight)
{
int neww = (int) ((float)height() / (float)templateHeight * (float)templateWidth);
m_rect = TQRect(width()/2-neww/2, 0, neww, height());
}
else
{
int newh = (int) ((float)width() / (float)templateWidth * (float)templateHeight);
m_rect = TQRect(0, height()/2-newh/2, width(), newh);
}
m_templateScaled = m_template.smoothScale(m_rect.width(), m_rect.height());
m_currentSelection = TQRect(m_w/2 - m_rect.width()/2, m_h/2 - m_rect.height()/2, m_rect.width(), m_rect.height());
zoomSelection(0);
}
void SuperImposeWidget::moveSelection(int dx, int dy)
{
TQRect selection = m_currentSelection;
float wf = (float)selection.width() / (float)m_rect.width();
float hf = (float)selection.height() / (float)m_rect.height();
selection.moveBy( -(int)(wf*(float)dx), -(int)(hf*(float)dy) );
if (selection.left() < 0)
selection.moveLeft(0);
if (selection.top() < 0)
selection.moveTop(0);
if (selection.bottom() > m_h)
selection.moveBottom(m_h);
if (selection.right() > m_w)
selection.moveRight(m_w);
m_currentSelection = selection;
}
bool SuperImposeWidget::zoomSelection(float deltaZoomFactor)
{
float newZoom = m_zoomFactor + deltaZoomFactor;
if (newZoom < 0.0)
return false;
TQRect selection = m_currentSelection;
int wf = (int)((float)m_rect.width() / newZoom);
int hf = (int)((float)m_rect.height() / newZoom);
int deltaX = (m_currentSelection.width() - wf) / 2;
int deltaY = (m_currentSelection.height() - hf) / 2;
selection.setLeft(m_currentSelection.left() + deltaX);
selection.setTop(m_currentSelection.top() + deltaY);
selection.setWidth(wf);
selection.setHeight(hf);
// check that selection is still inside original image
TQRect orgImageRect(0, 0, m_w, m_h);
if (!orgImageRect.contains(selection))
{
// try to adjust
if (selection.left() < 0)
selection.moveLeft(0);
if (selection.top() < 0)
selection.moveTop(0);
if (selection.bottom() > m_h)
selection.moveBottom(m_h);
if (selection.right() > m_w)
selection.moveRight(m_w);
// was it successful?
if (selection.contains(orgImageRect))
return false;
}
m_zoomFactor = newZoom;
m_currentSelection = selection;
makePixmap();
repaint(false);
return true;
}
void SuperImposeWidget::mousePressEvent ( TQMouseEvent * e )
{
if ( isEnabled() && e->button() == TQt::LeftButton &&
rect().contains( e->x(), e->y() ) )
{
switch (m_editMode)
{
case ZOOMIN:
if (zoomSelection(+0.05))
moveSelection(width()/2 - e->x(), height()/2 - e->y());
break;
case ZOOMOUT:
if (zoomSelection(-0.05))
moveSelection(width()/2 - e->x(), height()/2 - e->y());
break;
case MOVE:
m_xpos = e->x();
m_ypos = e->y();
}
}
}
void SuperImposeWidget::mouseReleaseEvent ( TQMouseEvent * )
{
setEditModeCursor();
}
void SuperImposeWidget::mouseMoveEvent ( TQMouseEvent * e )
{
if ( isEnabled() )
{
if ( e->state() == TQt::LeftButton )
{
switch (m_editMode)
{
case ZOOMIN:
case ZOOMOUT:
break;
case MOVE:
int newxpos = e->x();
int newypos = e->y();
if (newxpos < m_rect.left())
newxpos = m_rect.left();
if (newxpos > m_rect.right())
newxpos = m_rect.right();
if (newxpos < m_rect.top())
newxpos = m_rect.top();
if (newxpos > m_rect.bottom())
newxpos = m_rect.bottom();
moveSelection(newxpos - m_xpos, newypos - m_ypos);
makePixmap();
repaint(false);
m_xpos = newxpos;
m_ypos = newypos;
setCursor( KCursor::handCursor() );
break;
}
}
else if (rect().contains( e->x(), e->y() ))
{
setEditModeCursor();
}
}
}
void SuperImposeWidget::setEditModeCursor()
{
switch (m_editMode)
{
case ZOOMIN:
case ZOOMOUT:
setCursor ( KCursor::crossCursor() );
break;
case MOVE:
setCursor ( KCursor::sizeAllCursor() );
}
}
} // NameSpace DigikamSuperImposeImagesPlugin
|