summaryrefslogtreecommitdiffstats
path: root/kipi-plugins/printwizard/cropframe.cpp
blob: b92c39277859b103b21a12b67608d2555b096b08 (plain)
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
/***************************************************************************
                          cropframe.cpp  -  description
                             -------------------
    begin                : Mon Sep 30 2002
    copyright            : (C) 2002 by Todd Shoemaker
    email                : jtshoe11@yahoo.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

// C Ansi includes.
  
extern "C"
{
#include <math.h>
#include <stdio.h>
}

// TQt includes.

#include <tqpainter.h>
#include <tqimage.h>

// Local includes.

#include "cropframe.h"
#include "utils.h"

namespace KIPIPrintWizardPlugin
{

CropFrame::CropFrame(TQWidget *parent=0, const char *name=0)
         : TQWidget(parent, name)
{
  m_mouseDown = false;
}

// FIXME:  This method is doing way too much.  The cropFrame initialization
// should be a TPhoto method, and should not require the scaling of
// pixmaps to get the desired effect, which are too slow.

void CropFrame::init(TPhoto *photo, int width, int height, bool autoRotate, bool paint)
{
  m_photo = photo;
  TQImage scaledImg = m_photo->thumbnail().convertToImage();

  // has the cropRegion been set yet?
  bool resetCropRegion = (m_photo->cropRegion == TQRect(-1, -1, -1, -1));
  if (resetCropRegion) 
  {
    // first, let's see if we should rotate
    if (autoRotate) {
      if (m_photo->rotation == 0 && ((width > height &&
          m_photo->thumbnail().height() > m_photo->thumbnail().width()) ||
          (height > width &&
          m_photo->thumbnail().width() > m_photo->thumbnail().height())) )
      {
        // rotate
        m_photo->rotation = 90;
      }
    }
  }
  else
  {
    // does the crop region need updating (but the image shouldn't be rotated)?
    resetCropRegion = (m_photo->cropRegion == TQRect(-2, -2, -2, -2));
  }
  
  // rotate
  TQWMatrix matrix;
  matrix.rotate(m_photo->rotation);
  scaledImg = scaledImg.xForm(matrix);

  scaledImg = scaledImg.smoothScale(this->width(), this->height(), TQ_ScaleMin);

  m_pixmap = new TQPixmap();
  m_pixmap->convertFromImage(scaledImg);
  m_pixmapX = (this->width() / 2) - (m_pixmap->width() / 2);
  m_pixmapY = (this->height() / 2) - (m_pixmap->height() / 2);

  m_color = TQt::red;
  // size the rectangle based on the minimum image dimension
  int w = m_pixmap->width();
  int h = m_pixmap->height();;
  if (w < h)
  {
    h = NINT((double)w * ((double)height / (double)width));
    if (h > m_pixmap->height())
    {
      h = m_pixmap->height();
      w = NINT((double)h * ((double)width / (double)height));
    }
  }
  else
  {
    w = NINT((double)h * ((double)width / (double)height));
    if (w > m_pixmap->width())
    {
      w = m_pixmap->width();
      h = NINT((double)w * ((double)height / (double)width));
    }
  }

  if (resetCropRegion)
  {
    m_cropRegion.setRect((this->width() / 2) - (w / 2), (this->height() / 2) - (h / 2), w, h);

    m_photo->cropRegion = _screenToPhotoRect(m_cropRegion);
  }
  else
    m_cropRegion = _photoToScreenRect(m_photo->cropRegion);

  if (paint)  
    repaint(false);
}

TQRect CropFrame::_screenToPhotoRect(TQRect r)
{
  // r is given in screen coordinates, and we want to convert that
  // to photo coordinates
  double xRatio = 0.0;
  double yRatio = 0.0;

  // flip the photo dimensions if rotated
  int photoW;
  int photoH;

  if (m_photo->rotation == 0 || m_photo->rotation == 180)
  {
    photoW = m_photo->width();
    photoH = m_photo->height();
  }
  else
  {
    photoW = m_photo->height();
    photoH = m_photo->width();
  }
  if (m_pixmap->width() > 0)
    xRatio = (double) photoW / (double) m_pixmap->width();

  if (m_pixmap->height() > 0)
    yRatio = (double) photoH / (double) m_pixmap->height();

  
  int x1 = NINT((r.left() - m_pixmapX) * xRatio);
  int y1 = NINT((r.top()  - m_pixmapY) * yRatio);

  int w  = NINT(r.width() * xRatio);
  int h  = NINT(r.height() * yRatio);

  TQRect result;
  result.setRect(x1, y1, w, h);
  return result;
}

TQRect CropFrame::_photoToScreenRect(TQRect r)
{
  // r is given in photo coordinates, and we want to convert that
  // to screen coordinates
  double xRatio = 0.0;
  double yRatio = 0.0;

  // flip the photo dimensions if rotated
  int photoW;
  int photoH;
  
  if (m_photo->rotation == 0 || m_photo->rotation == 180)
  {
    photoW = m_photo->width();
    photoH = m_photo->height();
  }
  else
  {
    photoW = m_photo->height();
    photoH = m_photo->width();
  }
  
  if (m_photo->width() > 0)
    xRatio = (double) m_pixmap->width() / (double) photoW;

  if (m_photo->height() > 0)
    yRatio = (double)m_pixmap->height() / (double)photoH;


  int x1 = NINT(r.left() * xRatio + m_pixmapX);
  int y1 = NINT(r.top()  * yRatio + m_pixmapY);

  int w  = NINT(r.width() * xRatio);
  int h  = NINT(r.height() * yRatio);

  TQRect result;
  result.setRect(x1, y1, w, h);
  return result;
}

CropFrame::~CropFrame()
{
}
void CropFrame::paintEvent (TQPaintEvent *)
{

  TQPixmap bmp(this->width(), this->height());
  TQPainter p;
  p.begin(&bmp);

  p.eraseRect(0, 0, this->width(), this->height());

  // draw the background pixmap
  p.drawPixmap(m_pixmapX, m_pixmapY, *m_pixmap);

  // draw the rectangle
  p.setPen(TQPen(m_color, 2));
  p.drawRect(m_cropRegion);
  // draw the crosshairs
  int midX = m_cropRegion.left() + m_cropRegion.width() / 2;
  int midY = m_cropRegion.top()  + m_cropRegion.height() / 2;
  p.drawLine(midX - 10, midY, midX + 10, midY);
  p.drawLine(midX, midY - 10, midX, midY + 10);
  p.end();

  TQPainter newp(this);
  newp.drawPixmap(0, 0, bmp);
}

void CropFrame::mousePressEvent(TQMouseEvent *e)
{
  if (e->button() == Qt::LeftButton)
  {
    m_mouseDown = true;
    this->mouseMoveEvent(e);
  }

}

void CropFrame::mouseReleaseEvent(TQMouseEvent *e)
{
  if (e->button() == Qt::LeftButton)
    m_mouseDown = false;
}

void CropFrame::mouseMoveEvent(TQMouseEvent *e)
{
  if (m_mouseDown)
  {
    // don't let the rectangle float off the image.
    int newW = m_cropRegion.width();
    int newH = m_cropRegion.height();

    int newX = e->x() - (newW / 2);
    newX = MAX(m_pixmapX, newX);
    newX = MIN(m_pixmapX + m_pixmap->width() - newW, newX);

    int newY = e->y() - (newH / 2);
    newY = MAX(m_pixmapY, newY);
    newY = MIN(m_pixmapY + m_pixmap->height() - newH, newY);

    m_cropRegion.setRect(newX, newY, newW, newH);
    m_photo->cropRegion = _screenToPhotoRect(m_cropRegion);
    repaint(false);
  }
}

void CropFrame::keyPressEvent(TQKeyEvent *e)
{
  int newX = m_cropRegion.x();
  int newY = m_cropRegion.y();

  switch (e->key()) {
    case TQt::Key_Up : newY--;
                 break;
    case TQt::Key_Down : newY++;
                 break;
    case TQt::Key_Left : newX--;
                 break;
    case TQt::Key_Right : newX++;
                 break;
  }

  // keep inside the pixmap
  int w = m_cropRegion.width();
  int h = m_cropRegion.height();

  newX = MAX(m_pixmapX, newX);
  newX = MIN(m_pixmapX + m_pixmap->width() - w, newX);

  newY = MAX(m_pixmapY, newY);
  newY = MIN(m_pixmapY + m_pixmap->height() - h, newY);

  m_cropRegion.setRect(newX, newY, w, h);
  m_photo->cropRegion = _screenToPhotoRect(m_cropRegion);
  repaint(false);

}

void CropFrame::setColor(TQColor c)
{
  m_color = c;
  repaint(false);
}

TQColor CropFrame::color()
{
  return m_color;
}

}  // NameSpace KIPIPrintWizardPlugin

#include "cropframe.moc"