• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeprint
 

tdeprint

  • tdeprint
marginpreview.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (c) 2001 Michael Goffioul <tdeprint@swing.be>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License version 2 as published by the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  **/
19 
20 #include "marginpreview.h"
21 
22 #include <tdelocale.h>
23 #include <kdebug.h>
24 #include <tqpainter.h>
25 #include <kcursor.h>
26 
27 #define A4_WIDTH 595
28 #define A4_HEIGHT 842
29 #define A4_TOP 36
30 #define A4_BOTTOM 806
31 #define A4_LEFT 18
32 #define A4_RIGHT 577
33 
34 #define SCALE(d,z) ((int)(float(d)*z+0.5))
35 #define UNSCALE(d,z) ((int)(float(d)/z+0.5))
36 
37 static void draw3DPage(TQPainter *p, TQRect r)
38 {
39  // draw white page
40  p->fillRect(r,Qt::white);
41  // draw 3D border
42  p->setPen(Qt::black);
43  p->moveTo(r.left(),r.bottom());
44  p->lineTo(r.right(),r.bottom());
45  p->lineTo(r.right(),r.top());
46  p->setPen(Qt::darkGray);
47  p->lineTo(r.left(),r.top());
48  p->lineTo(r.left(),r.bottom());
49  p->setPen(Qt::gray);
50  p->moveTo(r.left()+1,r.bottom()-1);
51  p->lineTo(r.right()-1,r.bottom()-1);
52  p->lineTo(r.right()-1,r.top()+1);
53 }
54 
55 MarginPreview::MarginPreview(TQWidget *parent, const char *name)
56  : TQWidget(parent,name)
57 {
58  width_ = A4_WIDTH;
59  height_ = A4_HEIGHT;
60  top_ = A4_TOP;
61  bottom_ = A4_BOTTOM;
62  left_ = A4_LEFT;
63  right_ = A4_RIGHT;
64  nopreview_ = false;
65 
66  box_ = rect();
67  zoom_ = 1.0;
68  state_ = Fixed;
69  oldpos_ = -1;
70  symetric_ = false;
71 
72  setMouseTracking(true);
73 }
74 
75 MarginPreview::~MarginPreview()
76 {
77 }
78 
79 void MarginPreview::setPageSize(float w, float h)
80 {
81  setNoPreview(w <= 0 && h <= 0);
82  // do not change relative margins when changing page size !!
83  float old_b(height_-bottom_), old_r(width_-right_);
84  width_ = w;
85  height_ = h;
86  resizeEvent(NULL);
87  setMargins(top_,old_b,left_,old_r);
88  update();
89 }
90 
91 void MarginPreview::setMargins(float t, float b, float l, float r)
92 {
93  top_ = t;
94  left_ = l;
95  bottom_ = height_-b;
96  right_ = width_-r;
97  update();
98 }
99 
100 void MarginPreview::setSymetric(bool on)
101 {
102  symetric_ = on;
103 }
104 
105 void MarginPreview::resizeEvent(TQResizeEvent *)
106 {
107  if (width_/height_ > float(width())/height())
108  {
109  zoom_ = float(width()-3)/width_;
110  box_.setLeft(1);
111  box_.setRight(width()-3);
112  int m = (height()-3-SCALE(height_,zoom_))/2;
113  box_.setTop(m+1);
114  box_.setBottom(height()-m-3);
115  }
116  else
117  {
118  zoom_ = float(height()-3)/height_;
119  box_.setTop(1);
120  box_.setBottom(height()-3);
121  int m = (width()-3-SCALE(width_,zoom_))/2;
122  box_.setLeft(m+1);
123  box_.setRight(width()-m-3);
124  }
125 }
126 
127 void MarginPreview::paintEvent(TQPaintEvent *)
128 {
129  TQPainter p(this);
130 
131  TQRect pagebox(TQPoint(box_.left()-1,box_.top()-1),TQPoint(box_.right()+2,box_.bottom()+2));
132 
133  if (nopreview_)
134  {
135  p.drawText(pagebox,AlignCenter,i18n("No preview available"));
136  }
137  else
138  {
139  draw3DPage(&p,pagebox);
140 
141  // draw margins
142  p.setPen(DotLine);
143  int m = box_.left()+SCALE(left_,zoom_);
144  margbox_.setLeft(m+1);
145  p.drawLine(m,box_.top(),m,box_.bottom());
146  m = box_.left()+SCALE(right_,zoom_);
147  margbox_.setRight(m-1);
148  p.drawLine(m,box_.top(),m,box_.bottom());
149  m = box_.top()+SCALE(top_,zoom_);
150  margbox_.setTop(m+1);
151  p.drawLine(box_.left(),m,box_.right(),m);
152  m = box_.top()+SCALE(bottom_,zoom_);
153  margbox_.setBottom(m-1);
154  p.drawLine(box_.left(),m,box_.right(),m);
155 
156  // fill useful area
157  p.fillRect(margbox_,TQColor(220,220,220));
158  }
159 }
160 
161 void MarginPreview::setNoPreview(bool on)
162 {
163  nopreview_ = on;
164  update();
165 }
166 
167 // 0: nothing
168 // 1: top
169 // 2: bottom
170 // 3: left
171 // 4: right
172 int MarginPreview::locateMouse(const TQPoint& p)
173 {
174  int tol = 2;
175  if (p.x() <= margbox_.left()+tol && p.x() >= margbox_.left()-tol)
176  return LMoving;
177  else if (p.x() <= margbox_.right()+tol && p.x() >= margbox_.right()-tol)
178  return RMoving;
179  else if (p.y() <= margbox_.top()+tol && p.y() >= margbox_.top()-tol)
180  return TMoving;
181  else if (p.y() <= margbox_.bottom()+tol && p.y() >= margbox_.bottom()-tol)
182  return BMoving;
183  else
184  return 0;
185 }
186 
187 void MarginPreview::mouseMoveEvent(TQMouseEvent *e)
188 {
189  if (nopreview_ || state_ == Fixed)
190  return;
191  int pos = locateMouse(e->pos());
192  if (state_ == None && e->button() == Qt::NoButton)
193  {
194  switch (pos)
195  {
196  case 1:
197  case 2:
198  setCursor(KCursor::sizeVerCursor());
199  break;
200  case 3:
201  case 4:
202  setCursor(KCursor::sizeHorCursor());
203  break;
204  default:
205  setCursor(KCursor::arrowCursor());
206  break;
207  }
208  }
209  else if (state_ > None)
210  {
211  int newpos = -1;
212  switch (state_)
213  {
214  case TMoving:
215  newpos = TQMIN(TQMAX(e->pos().y(), box_.top()), (symetric_ ? (box_.top()+box_.bottom())/2 : margbox_.bottom()+1));
216  break;
217  case BMoving:
218  newpos = TQMIN(TQMAX(e->pos().y(), (symetric_? (box_.top()+box_.bottom()+1)/2 : margbox_.top()-1)), box_.bottom());
219  break;
220  case LMoving:
221  newpos = TQMIN(TQMAX(e->pos().x(), box_.left()), (symetric_ ? (box_.left()+box_.right())/2 : margbox_.right()+1));
222  break;
223  case RMoving:
224  newpos = TQMIN(TQMAX(e->pos().x(), (symetric_ ? (box_.left()+box_.right()+1)/2 : margbox_.left()-1)), box_.right());
225  break;
226  }
227  if (newpos != oldpos_)
228  {
229  TQPainter p(this);
230  p.setRasterOp(TQt::XorROP);
231  p.setPen(gray);
232  for (int i=0; i<2; i++, oldpos_ = newpos)
233  {
234  if (oldpos_ >= 0)
235  drawTempLine(&p);
236  }
237  }
238  }
239 }
240 
241 void MarginPreview::drawTempLine(TQPainter *p)
242 {
243  if (state_ >= LMoving)
244  {
245  p->drawLine(oldpos_, box_.top(), oldpos_, box_.bottom());
246  if (symetric_)
247  {
248  int mirror = box_.left()+box_.right()-oldpos_;
249  p->drawLine(mirror, box_.top(), mirror, box_.bottom());
250  }
251  }
252  else
253  {
254  p->drawLine(box_.left(), oldpos_, box_.right(), oldpos_);
255  if (symetric_)
256  {
257  int mirror = box_.top()+box_.bottom()-oldpos_;
258  p->drawLine(box_.left(), mirror, box_.right(), mirror);
259  }
260  }
261 }
262 
263 void MarginPreview::mousePressEvent(TQMouseEvent *e)
264 {
265  if (e->button() != Qt::LeftButton || state_ != None)
266  return;
267  int mpos = locateMouse(e->pos());
268  if (mpos)
269  {
270  state_ = mpos;
271  }
272 }
273 
274 void MarginPreview::mouseReleaseEvent(TQMouseEvent *e)
275 {
276  if (state_ > None)
277  {
278  TQPainter p(this);
279  p.setRasterOp(TQt::XorROP);
280  p.setPen(gray);
281  if (oldpos_ >= 0)
282  {
283  drawTempLine(&p);
284  if (e)
285  {
286  float val = 0;
287  int st(state_);
288  if (symetric_ && (st == BMoving || st == RMoving))
289  st--;
290  switch (st)
291  {
292  case TMoving:
293  val = top_ = UNSCALE(oldpos_-box_.top(), zoom_);
294  if (symetric_)
295  bottom_ = height_-top_;
296  break;
297  case BMoving:
298  bottom_ = UNSCALE(oldpos_-box_.top(), zoom_);
299  val = UNSCALE(box_.bottom()-oldpos_, zoom_);
300  break;
301  case LMoving:
302  val = left_ = UNSCALE(oldpos_-box_.left(), zoom_);
303  if (symetric_)
304  right_ = width_-left_;
305  break;
306  case RMoving:
307  right_ = UNSCALE(oldpos_-box_.left(), zoom_);
308  val = UNSCALE(box_.right()-oldpos_, zoom_);
309  break;
310  }
311  update();
312  emit marginChanged(st, val);
313  }
314  }
315  state_ = 0;
316  oldpos_ = -1;
317  }
318 }
319 
320 void MarginPreview::enableRubberBand(bool on)
321 {
322  if (on && state_ == Fixed)
323  state_ = None;
324  else if (!on && state_ > Fixed)
325  {
326  mouseReleaseEvent(NULL);
327  state_ = Fixed;
328  }
329 }
330 
331 #include "marginpreview.moc"

tdeprint

Skip menu "tdeprint"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

tdeprint

Skip menu "tdeprint"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeprint by doxygen 1.8.8
This website is maintained by Timothy Pearson.