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
330
331
|
/*
* This file is part of the KDE libraries
* Copyright (c) 2001 Michael Goffioul <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License version 2 as published by the Free Software Foundation.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
**/
#include "marginpreview.h"
#include <tdelocale.h>
#include <kdebug.h>
#include <tqpainter.h>
#include <kcursor.h>
#define A4_WIDTH 595
#define A4_HEIGHT 842
#define A4_TOP 36
#define A4_BOTTOM 806
#define A4_LEFT 18
#define A4_RIGHT 577
#define SCALE(d,z) ((int)(float(d)*z+0.5))
#define UNSCALE(d,z) ((int)(float(d)/z+0.5))
static void draw3DPage(TQPainter *p, TQRect r)
{
// draw white page
p->fillRect(r,TQt::white);
// draw 3D border
p->setPen(TQt::black);
p->moveTo(r.left(),r.bottom());
p->lineTo(r.right(),r.bottom());
p->lineTo(r.right(),r.top());
p->setPen(TQt::darkGray);
p->lineTo(r.left(),r.top());
p->lineTo(r.left(),r.bottom());
p->setPen(TQt::gray);
p->moveTo(r.left()+1,r.bottom()-1);
p->lineTo(r.right()-1,r.bottom()-1);
p->lineTo(r.right()-1,r.top()+1);
}
MarginPreview::MarginPreview(TQWidget *parent, const char *name)
: TQWidget(parent,name)
{
width_ = A4_WIDTH;
height_ = A4_HEIGHT;
top_ = A4_TOP;
bottom_ = A4_BOTTOM;
left_ = A4_LEFT;
right_ = A4_RIGHT;
nopreview_ = false;
box_ = rect();
zoom_ = 1.0;
state_ = Fixed;
oldpos_ = -1;
symetric_ = false;
setMouseTracking(true);
}
MarginPreview::~MarginPreview()
{
}
void MarginPreview::setPageSize(float w, float h)
{
setNoPreview(w <= 0 && h <= 0);
// do not change relative margins when changing page size !!
float old_b(height_-bottom_), old_r(width_-right_);
width_ = w;
height_ = h;
resizeEvent(NULL);
setMargins(top_,old_b,left_,old_r);
update();
}
void MarginPreview::setMargins(float t, float b, float l, float r)
{
top_ = t;
left_ = l;
bottom_ = height_-b;
right_ = width_-r;
update();
}
void MarginPreview::setSymetric(bool on)
{
symetric_ = on;
}
void MarginPreview::resizeEvent(TQResizeEvent *)
{
if (width_/height_ > float(width())/height())
{
zoom_ = float(width()-3)/width_;
box_.setLeft(1);
box_.setRight(width()-3);
int m = (height()-3-SCALE(height_,zoom_))/2;
box_.setTop(m+1);
box_.setBottom(height()-m-3);
}
else
{
zoom_ = float(height()-3)/height_;
box_.setTop(1);
box_.setBottom(height()-3);
int m = (width()-3-SCALE(width_,zoom_))/2;
box_.setLeft(m+1);
box_.setRight(width()-m-3);
}
}
void MarginPreview::paintEvent(TQPaintEvent *)
{
TQPainter p(this);
TQRect pagebox(TQPoint(box_.left()-1,box_.top()-1),TQPoint(box_.right()+2,box_.bottom()+2));
if (nopreview_)
{
p.drawText(pagebox,AlignCenter,i18n("No preview available"));
}
else
{
draw3DPage(&p,pagebox);
// draw margins
p.setPen(DotLine);
int m = box_.left()+SCALE(left_,zoom_);
margbox_.setLeft(m+1);
p.drawLine(m,box_.top(),m,box_.bottom());
m = box_.left()+SCALE(right_,zoom_);
margbox_.setRight(m-1);
p.drawLine(m,box_.top(),m,box_.bottom());
m = box_.top()+SCALE(top_,zoom_);
margbox_.setTop(m+1);
p.drawLine(box_.left(),m,box_.right(),m);
m = box_.top()+SCALE(bottom_,zoom_);
margbox_.setBottom(m-1);
p.drawLine(box_.left(),m,box_.right(),m);
// fill useful area
p.fillRect(margbox_,TQColor(220,220,220));
}
}
void MarginPreview::setNoPreview(bool on)
{
nopreview_ = on;
update();
}
// 0: nothing
// 1: top
// 2: bottom
// 3: left
// 4: right
int MarginPreview::locateMouse(const TQPoint& p)
{
int tol = 2;
if (p.x() <= margbox_.left()+tol && p.x() >= margbox_.left()-tol)
return LMoving;
else if (p.x() <= margbox_.right()+tol && p.x() >= margbox_.right()-tol)
return RMoving;
else if (p.y() <= margbox_.top()+tol && p.y() >= margbox_.top()-tol)
return TMoving;
else if (p.y() <= margbox_.bottom()+tol && p.y() >= margbox_.bottom()-tol)
return BMoving;
else
return 0;
}
void MarginPreview::mouseMoveEvent(TQMouseEvent *e)
{
if (nopreview_ || state_ == Fixed)
return;
int pos = locateMouse(e->pos());
if (state_ == None && e->button() == TQt::NoButton)
{
switch (pos)
{
case 1:
case 2:
setCursor(KCursor::sizeVerCursor());
break;
case 3:
case 4:
setCursor(KCursor::sizeHorCursor());
break;
default:
setCursor(KCursor::arrowCursor());
break;
}
}
else if (state_ > None)
{
int newpos = -1;
switch (state_)
{
case TMoving:
newpos = TQMIN(TQMAX(e->pos().y(), box_.top()), (symetric_ ? (box_.top()+box_.bottom())/2 : margbox_.bottom()+1));
break;
case BMoving:
newpos = TQMIN(TQMAX(e->pos().y(), (symetric_? (box_.top()+box_.bottom()+1)/2 : margbox_.top()-1)), box_.bottom());
break;
case LMoving:
newpos = TQMIN(TQMAX(e->pos().x(), box_.left()), (symetric_ ? (box_.left()+box_.right())/2 : margbox_.right()+1));
break;
case RMoving:
newpos = TQMIN(TQMAX(e->pos().x(), (symetric_ ? (box_.left()+box_.right()+1)/2 : margbox_.left()-1)), box_.right());
break;
}
if (newpos != oldpos_)
{
TQPainter p(this);
p.setRasterOp(TQt::XorROP);
p.setPen(gray);
for (int i=0; i<2; i++, oldpos_ = newpos)
{
if (oldpos_ >= 0)
drawTempLine(&p);
}
}
}
}
void MarginPreview::drawTempLine(TQPainter *p)
{
if (state_ >= LMoving)
{
p->drawLine(oldpos_, box_.top(), oldpos_, box_.bottom());
if (symetric_)
{
int mirror = box_.left()+box_.right()-oldpos_;
p->drawLine(mirror, box_.top(), mirror, box_.bottom());
}
}
else
{
p->drawLine(box_.left(), oldpos_, box_.right(), oldpos_);
if (symetric_)
{
int mirror = box_.top()+box_.bottom()-oldpos_;
p->drawLine(box_.left(), mirror, box_.right(), mirror);
}
}
}
void MarginPreview::mousePressEvent(TQMouseEvent *e)
{
if (e->button() != TQt::LeftButton || state_ != None)
return;
int mpos = locateMouse(e->pos());
if (mpos)
{
state_ = mpos;
}
}
void MarginPreview::mouseReleaseEvent(TQMouseEvent *e)
{
if (state_ > None)
{
TQPainter p(this);
p.setRasterOp(TQt::XorROP);
p.setPen(gray);
if (oldpos_ >= 0)
{
drawTempLine(&p);
if (e)
{
float val = 0;
int st(state_);
if (symetric_ && (st == BMoving || st == RMoving))
st--;
switch (st)
{
case TMoving:
val = top_ = UNSCALE(oldpos_-box_.top(), zoom_);
if (symetric_)
bottom_ = height_-top_;
break;
case BMoving:
bottom_ = UNSCALE(oldpos_-box_.top(), zoom_);
val = UNSCALE(box_.bottom()-oldpos_, zoom_);
break;
case LMoving:
val = left_ = UNSCALE(oldpos_-box_.left(), zoom_);
if (symetric_)
right_ = width_-left_;
break;
case RMoving:
right_ = UNSCALE(oldpos_-box_.left(), zoom_);
val = UNSCALE(box_.right()-oldpos_, zoom_);
break;
}
update();
emit marginChanged(st, val);
}
}
state_ = 0;
oldpos_ = -1;
}
}
void MarginPreview::enableRubberBand(bool on)
{
if (on && state_ == Fixed)
state_ = None;
else if (!on && state_ > Fixed)
{
mouseReleaseEvent(NULL);
state_ = Fixed;
}
}
#include "marginpreview.moc"
|