20 #include "marginpreview.h"
22 #include <tdelocale.h>
24 #include <tqpainter.h>
34 #define SCALE(d,z) ((int)(float(d)*z+0.5))
35 #define UNSCALE(d,z) ((int)(float(d)/z+0.5))
37 static void draw3DPage(TQPainter *p, TQRect r)
40 p->fillRect(r,TQt::white);
42 p->setPen(TQt::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(TQt::darkGray);
47 p->lineTo(r.left(),r.top());
48 p->lineTo(r.left(),r.bottom());
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);
55 MarginPreview::MarginPreview(TQWidget *parent,
const char *name)
56 : TQWidget(parent,name)
72 setMouseTracking(
true);
75 MarginPreview::~MarginPreview()
79 void MarginPreview::setPageSize(
float w,
float h)
81 setNoPreview(w <= 0 && h <= 0);
83 float old_b(height_-bottom_), old_r(width_-right_);
87 setMargins(top_,old_b,left_,old_r);
91 void MarginPreview::setMargins(
float t,
float b,
float l,
float r)
100 void MarginPreview::setSymetric(
bool on)
105 void MarginPreview::resizeEvent(TQResizeEvent *)
107 if (width_/height_ >
float(width())/height())
109 zoom_ = float(width()-3)/width_;
111 box_.setRight(width()-3);
112 int m = (height()-3-SCALE(height_,zoom_))/2;
114 box_.setBottom(height()-m-3);
118 zoom_ = float(height()-3)/height_;
120 box_.setBottom(height()-3);
121 int m = (width()-3-SCALE(width_,zoom_))/2;
123 box_.setRight(width()-m-3);
127 void MarginPreview::paintEvent(TQPaintEvent *)
131 TQRect pagebox(TQPoint(box_.left()-1,box_.top()-1),TQPoint(box_.right()+2,box_.bottom()+2));
135 p.drawText(pagebox,AlignCenter,i18n(
"No preview available"));
139 draw3DPage(&p,pagebox);
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);
157 p.fillRect(margbox_,TQColor(220,220,220));
161 void MarginPreview::setNoPreview(
bool on)
172 int MarginPreview::locateMouse(
const TQPoint& p)
175 if (p.x() <= margbox_.left()+tol && p.x() >= margbox_.left()-tol)
177 else if (p.x() <= margbox_.right()+tol && p.x() >= margbox_.right()-tol)
179 else if (p.y() <= margbox_.top()+tol && p.y() >= margbox_.top()-tol)
181 else if (p.y() <= margbox_.bottom()+tol && p.y() >= margbox_.bottom()-tol)
187 void MarginPreview::mouseMoveEvent(TQMouseEvent *e)
189 if (nopreview_ || state_ == Fixed)
191 int pos = locateMouse(e->pos());
192 if (state_ == None && e->button() == TQt::NoButton)
198 setCursor(KCursor::sizeVerCursor());
202 setCursor(KCursor::sizeHorCursor());
205 setCursor(KCursor::arrowCursor());
209 else if (state_ > None)
215 newpos = TQMIN(TQMAX(e->pos().y(), box_.top()), (symetric_ ? (box_.top()+box_.bottom())/2 : margbox_.bottom()+1));
218 newpos = TQMIN(TQMAX(e->pos().y(), (symetric_? (box_.top()+box_.bottom()+1)/2 : margbox_.top()-1)), box_.bottom());
221 newpos = TQMIN(TQMAX(e->pos().x(), box_.left()), (symetric_ ? (box_.left()+box_.right())/2 : margbox_.right()+1));
224 newpos = TQMIN(TQMAX(e->pos().x(), (symetric_ ? (box_.left()+box_.right()+1)/2 : margbox_.left()-1)), box_.right());
227 if (newpos != oldpos_)
230 p.setRasterOp(TQt::XorROP);
232 for (
int i=0; i<2; i++, oldpos_ = newpos)
241 void MarginPreview::drawTempLine(TQPainter *p)
243 if (state_ >= LMoving)
245 p->drawLine(oldpos_, box_.top(), oldpos_, box_.bottom());
248 int mirror = box_.left()+box_.right()-oldpos_;
249 p->drawLine(mirror, box_.top(), mirror, box_.bottom());
254 p->drawLine(box_.left(), oldpos_, box_.right(), oldpos_);
257 int mirror = box_.top()+box_.bottom()-oldpos_;
258 p->drawLine(box_.left(), mirror, box_.right(), mirror);
263 void MarginPreview::mousePressEvent(TQMouseEvent *e)
265 if (e->button() != TQt::LeftButton || state_ != None)
267 int mpos = locateMouse(e->pos());
274 void MarginPreview::mouseReleaseEvent(TQMouseEvent *e)
279 p.setRasterOp(TQt::XorROP);
288 if (symetric_ && (st == BMoving || st == RMoving))
293 val = top_ = UNSCALE(oldpos_-box_.top(), zoom_);
295 bottom_ = height_-top_;
298 bottom_ = UNSCALE(oldpos_-box_.top(), zoom_);
299 val = UNSCALE(box_.bottom()-oldpos_, zoom_);
302 val = left_ = UNSCALE(oldpos_-box_.left(), zoom_);
304 right_ = width_-left_;
307 right_ = UNSCALE(oldpos_-box_.left(), zoom_);
308 val = UNSCALE(box_.right()-oldpos_, zoom_);
312 emit marginChanged(st, val);
320 void MarginPreview::enableRubberBand(
bool on)
322 if (on && state_ == Fixed)
324 else if (!on && state_ > Fixed)
326 mouseReleaseEvent(NULL);
331 #include "marginpreview.moc"