~librecad-dev/librecad/librecad

« back to all changes in this revision

Viewing changes to librecad/src/lib/gui/rs_painterqt.cpp

  • Committer: Scott Howard
  • Date: 2014-02-21 19:07:55 UTC
  • Revision ID: showard@debian.org-20140221190755-csjax9wb146hgdq4
first commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** This file is part of the LibreCAD project, a 2D CAD program
 
4
**
 
5
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
 
6
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
 
7
**
 
8
**
 
9
** This file may be distributed and/or modified under the terms of the
 
10
** GNU General Public License version 2 as published by the Free Software
 
11
** Foundation and appearing in the file gpl-2.0.txt included in the
 
12
** packaging of this file.
 
13
**
 
14
** This program is distributed in the hope that it will be useful,
 
15
** but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
** GNU General Public License for more details.
 
18
**
 
19
** You should have received a copy of the GNU General Public License
 
20
** along with this program; if not, write to the Free Software
 
21
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
22
**
 
23
** This copyright notice MUST APPEAR in all copies of the script!
 
24
**
 
25
**********************************************************************/
 
26
 
 
27
 
 
28
#include "rs_painterqt.h"
 
29
 
 
30
/**
 
31
 * Constructor.
 
32
 */
 
33
// RVT_PORT changed from RS_PainterQt::RS_PainterQt( const QPaintDevice* pd)
 
34
RS_PainterQt::RS_PainterQt( QPaintDevice* pd)
 
35
        : QPainter(pd), RS_Painter() {}
 
36
 
 
37
 
 
38
/**
 
39
 * Destructor
 
40
 */
 
41
RS_PainterQt::~RS_PainterQt() {}
 
42
 
 
43
 
 
44
 
 
45
void RS_PainterQt::moveTo(int x, int y) {
 
46
        //RVT_PORT changed from QPainter::moveTo(x,y);
 
47
        rememberX=x;
 
48
        rememberY=y;
 
49
}
 
50
 
 
51
 
 
52
void RS_PainterQt::lineTo(int x, int y) {
 
53
        // RVT_PORT changed from QPainter::lineTo(x, y);
 
54
        QPainterPath path;
 
55
        path.moveTo(rememberX,rememberY);
 
56
        path.lineTo(x,y);
 
57
        QPainter::drawPath(path);
 
58
        rememberX=x;
 
59
        rememberY=y;
 
60
}
 
61
 
 
62
/**
 
63
 * Draws a grid point at (x1, y1).
 
64
 */
 
65
void RS_PainterQt::drawGridPoint(const RS_Vector& p) {
 
66
    QPainter::drawPoint(toScreenX(p.x), toScreenY(p.y));
 
67
}
 
68
 
 
69
 
 
70
 
 
71
/**
 
72
 * Draws a point at (x1, y1).
 
73
 */
 
74
void RS_PainterQt::drawPoint(const RS_Vector& p) {
 
75
    QPainter::drawLine(toScreenX(p.x-1), toScreenY(p.y),
 
76
                       toScreenX(p.x+1), toScreenY(p.y));
 
77
    QPainter::drawLine(toScreenX(p.x), toScreenY(p.y-1),
 
78
                       toScreenX(p.x), toScreenY(p.y+1));
 
79
}
 
80
 
 
81
 
 
82
 
 
83
/**
 
84
 * Draws a line from (x1, y1) to (x2, y2).
 
85
 */
 
86
void RS_PainterQt::drawLine(const RS_Vector& p1, const RS_Vector& p2) {
 
87
#ifdef __APPLE1__
 
88
        int w2 = (int)getPen().getScreenWidth()/2;
 
89
    QPainter::drawLine(toScreenX(p1.x-w2), toScreenY(p1.y-w2),
 
90
                       toScreenX(p2.x-w2), toScreenY(p2.y-w2));
 
91
#else
 
92
    QPainter::drawLine(toScreenX(p1.x), toScreenY(p1.y),
 
93
                       toScreenX(p2.x), toScreenY(p2.y));
 
94
#endif
 
95
}
 
96
 
 
97
 
 
98
 
 
99
 
 
100
 
 
101
 
 
102
/**
 
103
 * Draws a rectangle with corners p1, p2.
 
104
 */
 
105
/*void RS_PainterQt::drawRect(const RS_Vector& p1, const RS_Vector& p2) {
 
106
        / *QPainter::drawRect(toScreenX(p1.x), toScreenY(p1.y),
 
107
                                           abs(toScreenX(p2.x) - toScreenX(p1.x)),
 
108
                                           abs(toScreenY(p2.y) - toScreenY(p1.y)));* /
 
109
        QPainter::drawLine(toScreenX(p1.x), toScreenY(p1.y),
 
110
                       toScreenX(p2.x), toScreenY(p1.y));
 
111
    QPainter::drawLine(toScreenX(p2.x), toScreenY(p1.y),
 
112
                       toScreenX(p2.x), toScreenY(p2.y));
 
113
    QPainter::drawLine(toScreenX(p2.x), toScreenY(p2.y),
 
114
                       toScreenX(p1.x), toScreenY(p2.y));
 
115
    QPainter::drawLine(toScreenX(p1.x), toScreenY(p2.y),
 
116
                       toScreenX(p1.x), toScreenY(p1.y));
 
117
}*/
 
118
 
 
119
 
 
120
/**
 
121
 * Draws an arc which starts / ends exactly at the given coordinates.
 
122
 *
 
123
 * @param cx center in x
 
124
 * @param cy center in y
 
125
 * @param radius Radius
 
126
 * @param a1 Angle 1 in rad
 
127
 * @param a2 Angle 2 in rad
 
128
 * @param x1 startpoint x
 
129
 * @param y1 startpoint y
 
130
 * @param x2 endpoint x
 
131
 * @param y2 endpoint y
 
132
 * @param reversed true: clockwise, false: counterclockwise
 
133
 */
 
134
void RS_PainterQt::drawArc(const RS_Vector& cp, double radius,
 
135
                           double a1, double a2,
 
136
                           const RS_Vector& p1, const RS_Vector& p2,
 
137
                           bool reversed) {
 
138
    /*
 
139
    QPainter::drawArc(cx-radius, cy-radius,
 
140
                      2*radius, 2*radius,
 
141
                      a1*16, (a2-a1)*16);
 
142
    */
 
143
 
 
144
    if(radius<=0.5) {
 
145
        drawGridPoint(cp);
 
146
    } else {
 
147
#ifdef __APPLE1__
 
148
                drawArcMac(cp, radius, a1, a2, reversed);
 
149
#else
 
150
        int   cix;            // Next point on circle
 
151
        int   ciy;            //
 
152
        double aStep;         // Angle Step (rad)
 
153
        double a;             // Current Angle (rad)
 
154
        double linStep;       // linear step (pixels)
 
155
 
 
156
        if (drawingMode==RS2::ModePreview) {
 
157
            linStep = 20.0;
 
158
        } else {
 
159
            linStep = 6.0;
 
160
        }
 
161
 
 
162
        if (fabs(linStep/radius)<=1.0) {
 
163
            aStep=asin(linStep/radius);
 
164
        } else {
 
165
            aStep=1.0;
 
166
        }
 
167
 
 
168
        if (aStep<0.05) {
 
169
            aStep = 0.05;
 
170
        }
 
171
 
 
172
        if(!reversed) {
 
173
            // Arc Counterclockwise:
 
174
            if(a1>a2-1.0e-10) {
 
175
                a2+=2*M_PI;
 
176
            }
 
177
            //moveTo(toScreenX(p1.x), toScreenY(p1.y));
 
178
            QPolygon pa;
 
179
            int i=0;
 
180
            pa.resize(i+1);
 
181
            pa.setPoint(i++, toScreenX(p1.x), toScreenY(p1.y));
 
182
            for(a=a1+aStep; a<=a2; a+=aStep) {
 
183
                cix = toScreenX(cp.x+cos(a)*radius);
 
184
                ciy = toScreenY(cp.y-sin(a)*radius);
 
185
                //lineTo(cix, ciy);
 
186
                pa.resize(i+1);
 
187
                pa.setPoint(i++, cix, ciy);
 
188
            }
 
189
            //lineTo(toScreenX(p2.x), toScreenY(p2.y));
 
190
            pa.resize(i+1);
 
191
            pa.setPoint(i++, toScreenX(p2.x), toScreenY(p2.y));
 
192
            drawPolyline(pa);
 
193
        } else {
 
194
            // Arc Clockwise:
 
195
            if(a1<a2+1.0e-10) {
 
196
                a2-=2*M_PI;
 
197
            }
 
198
            QPolygon pa;
 
199
            int i=0;
 
200
            pa.resize(i+1);
 
201
            pa.setPoint(i++, toScreenX(p1.x), toScreenY(p1.y));
 
202
            //moveTo(toScreenX(p1.x), toScreenY(p1.y));
 
203
            for(a=a1-aStep; a>=a2; a-=aStep) {
 
204
                cix = toScreenX(cp.x+cos(a)*radius);
 
205
                ciy = toScreenY(cp.y-sin(a)*radius);
 
206
                //lineTo(cix, ciy);
 
207
                pa.resize(i+1);
 
208
                pa.setPoint(i++, cix, ciy);
 
209
            }
 
210
            //lineTo(toScreenX(p2.x), toScreenY(p2.y));
 
211
            pa.resize(i+1);
 
212
            pa.setPoint(i++, toScreenX(p2.x), toScreenY(p2.y));
 
213
            drawPolyline(pa);
 
214
        }
 
215
#endif
 
216
    }
 
217
}
 
218
 
 
219
 
 
220
 
 
221
/**
 
222
 * Draws an arc.
 
223
 *
 
224
 * @param cx center in x
 
225
 * @param cy center in y
 
226
 * @param radius Radius
 
227
 * @param a1 Angle 1 in rad
 
228
 * @param a2 Angle 2 in rad
 
229
 * @param reversed true: clockwise, false: counterclockwise
 
230
 */
 
231
void RS_PainterQt::drawArc(const RS_Vector& cp, double radius,
 
232
                           double a1, double a2,
 
233
                           bool reversed) {
 
234
    if(radius<=0.5) {
 
235
        drawGridPoint(cp);
 
236
    } else {
 
237
#ifdef __APPL1E__
 
238
                drawArcMac(cp, radius, a1, a2, reversed);
 
239
#else
 
240
        QPolygon pa;
 
241
        createArc(pa, cp, radius, a1, a2, reversed);
 
242
        drawPolyline(pa);
 
243
#endif
 
244
    }
 
245
}
 
246
 
 
247
 
 
248
/**
 
249
 * Draws an arc on apple.
 
250
 *
 
251
 * @param cx center in x
 
252
 * @param cy center in y
 
253
 * @param radius Radius
 
254
 * @param a1 Angle 1 in rad
 
255
 * @param a2 Angle 2 in rad
 
256
 * @param reversed true: clockwise, false: counterclockwise
 
257
 */
 
258
void RS_PainterQt::drawArcMac(const RS_Vector& cp, double radius,
 
259
                           double a1, double a2,
 
260
                           bool reversed) {
 
261
        RS_DEBUG->print("RS_PainterQt::drawArcMac");
 
262
    if(radius<=0.5) {
 
263
        drawGridPoint(cp);
 
264
    } else {
 
265
        //QPointArray pa;
 
266
        //createArc(pa, cp, radius, a1, a2, reversed);
 
267
 
 
268
              double cix;            // Next point on circle
 
269
              double ciy;            //
 
270
              double aStep;         // Angle Step (rad)
 
271
              double a;             // Current Angle (rad)
 
272
                          double ox;
 
273
                          double oy;
 
274
 
 
275
              if(2.0/radius<=1.0) {
 
276
                  aStep=asin(2.0/radius);
 
277
              } else {
 
278
                  aStep=1.0;
 
279
              }
 
280
 
 
281
              if (aStep<0.05) {
 
282
                  aStep = 0.05;
 
283
              }
 
284
 
 
285
              //QPointArray pa;
 
286
              //int i=0;
 
287
              //pa.resize(i+1);
 
288
              //pa.setPoint(i++, toScreenX(cp.x+cos(a1)*radius),
 
289
              //            toScreenY(cp.y-sin(a1)*radius));
 
290
              //moveTo(toScreenX(cp.x+cos(a1)*radius),
 
291
              //       toScreenY(cp.y-sin(a1)*radius));
 
292
              ox = cp.x+cos(a1)*radius;
 
293
              oy = cp.y-sin(a1)*radius;
 
294
              if(!reversed) {
 
295
                  // Arc Counterclockwise:
 
296
                  if(a1>a2-1.0e-10) {
 
297
                      a2+=2*M_PI;
 
298
                  }
 
299
                  for(a=a1+aStep; a<=a2; a+=aStep) {
 
300
                      cix = cp.x+cos(a)*radius;
 
301
                      ciy = cp.y-sin(a)*radius;
 
302
                      //lineTo(cix, ciy);
 
303
                                          drawLine(RS_Vector(ox, oy), RS_Vector(cix, ciy));
 
304
                                          ox = cix;
 
305
                                          oy = ciy;
 
306
                      //pa.resize(i+1);
 
307
                      //pa.setPoint(i++, cix, ciy);
 
308
                  }
 
309
              } else {
 
310
                  // Arc Clockwise:
 
311
                  if(a1<a2+1.0e-10) {
 
312
                      a2-=2*M_PI;
 
313
                  }
 
314
                  for(a=a1-aStep; a>=a2; a-=aStep) {
 
315
                      cix = cp.x+cos(a)*radius;
 
316
                      ciy = cp.y-sin(a)*radius;
 
317
                      drawLine(RS_Vector(ox, oy), RS_Vector(cix, ciy));
 
318
                                          ox = cix;
 
319
                                          oy = ciy;
 
320
                                          //lineTo(cix, ciy);
 
321
                      //pa.resize(i+1);
 
322
                      //pa.setPoint(i++, cix, ciy);
 
323
                  }
 
324
              }
 
325
              drawLine(RS_Vector(ox, oy),
 
326
                                        RS_Vector(cp.x+cos(a2)*radius,
 
327
                     cp.y-sin(a2)*radius));
 
328
              //lineTo(toScreenX(cp.x+cos(a2)*radius),
 
329
              //       toScreenY(cp.y-sin(a2)*radius));
 
330
              //pa.resize(i+1);
 
331
              //pa.setPoint(i++,
 
332
              //            toScreenX(cp.x+cos(a2)*radius),
 
333
              //            toScreenY(cp.y-sin(a2)*radius));
 
334
        //drawPolyline(pa);
 
335
    }
 
336
}
 
337
 
 
338
 
 
339
 
 
340
/**
 
341
 * Draws a circle.
 
342
 * @param cx center in x
 
343
 * @param cy center in y
 
344
 * @param radius Radius
 
345
 */
 
346
void RS_PainterQt::drawCircle(const RS_Vector& cp, double radius) {
 
347
 
 
348
// RVT_PORT    if (drawingMode==RS2::ModeXOR && radius<500) {
 
349
                if (radius<500) {
 
350
        // This is _very_ slow for large arcs:
 
351
        QPainter::drawEllipse(toScreenX(cp.x-radius),
 
352
                              toScreenY(cp.y-radius),
 
353
                              RS_Math::round(2.0*radius),
 
354
                              RS_Math::round(2.0*radius));
 
355
    } else {
 
356
#ifdef __APPLE1__
 
357
        drawArcMac(cp,
 
358
                radius,
 
359
                0.0, 2*M_PI,
 
360
                false);
 
361
#else
 
362
        drawArc(cp,
 
363
                radius,
 
364
                0.0, 2*M_PI,
 
365
                cp + RS_Vector(radius, 0.0),
 
366
                cp + RS_Vector(radius, 0.0),
 
367
                false);
 
368
#endif
 
369
        }
 
370
}
 
371
 
 
372
 
 
373
 
 
374
/**
 
375
 * Draws a rotated ellipse arc.
 
376
 */
 
377
void RS_PainterQt::drawEllipse(const RS_Vector& cp,
 
378
                               double radius1, double radius2,
 
379
                               double angle,
 
380
                               double a1, double a2,
 
381
                               bool reversed) {
 
382
    QPolygon pa;
 
383
    createEllipse(pa, cp, radius1, radius2, angle, a1, a2, reversed);
 
384
    drawPolyline(pa);
 
385
}
 
386
 
 
387
 
 
388
 
 
389
/**
 
390
 * Draws image.
 
391
 */
 
392
void RS_PainterQt::drawImg(QImage& img, const RS_Vector& pos,
 
393
                           double angle, const RS_Vector& factor,
 
394
                           int sx, int sy, int sw, int sh) {
 
395
    save();
 
396
 
 
397
    // Render smooth only at close zooms
 
398
    if (factor.x < 1 || factor.y < 1) {
 
399
       RS_PainterQt::setRenderHint(SmoothPixmapTransform , true);
 
400
    }
 
401
    else {
 
402
      RS_PainterQt::setRenderHint(SmoothPixmapTransform);
 
403
    }
 
404
 
 
405
    QMatrix wm;
 
406
    wm.translate(pos.x, pos.y);
 
407
    wm.scale(factor.x, factor.y);
 
408
    wm.rotate(RS_Math::rad2deg(-angle));
 
409
    setWorldMatrix(wm);
 
410
 
 
411
 
 
412
    if (fabs(angle)<1.0e-4) {
 
413
        drawImage(0+sx,-img.height()+sy, img, sx, sy, sw, sh);
 
414
    }
 
415
    else {
 
416
        drawImage(0,-img.height(), img);
 
417
    }
 
418
 
 
419
    restore();
 
420
}
 
421
 
 
422
 
 
423
 
 
424
void RS_PainterQt::drawTextH(int x1, int y1,
 
425
                             int x2, int y2,
 
426
                             const QString& text) {
 
427
    drawText(x1, y1, x2, y2,
 
428
             Qt::AlignRight|Qt::AlignVCenter,
 
429
             text);
 
430
}
 
431
 
 
432
 
 
433
 
 
434
void RS_PainterQt::drawTextV(int x1, int y1,
 
435
                             int x2, int y2,
 
436
                             const QString& text) {
 
437
    save();
 
438
    QMatrix wm = worldMatrix();
 
439
    wm.rotate(-90.0);
 
440
    setWorldMatrix(wm);
 
441
    //rotate(-90.0);
 
442
 
 
443
    drawText(x1, y1, x2, y2,
 
444
             Qt::AlignRight|Qt::AlignVCenter,
 
445
             text);
 
446
 
 
447
    restore();
 
448
}
 
449
 
 
450
void RS_PainterQt::fillRect(int x1, int y1, int w, int h,
 
451
                            const RS_Color& col) {
 
452
    QPainter::fillRect(x1, y1, w, h, col);
 
453
}
 
454
 
 
455
 
 
456
void RS_PainterQt::fillTriangle(const RS_Vector& p1,
 
457
                                const RS_Vector& p2,
 
458
                                const RS_Vector& p3) {
 
459
 
 
460
    QPolygon arr(3);
 
461
    arr.putPoints(0, 3,
 
462
                  toScreenX(p1.x),toScreenY(p1.y),
 
463
                  toScreenX(p2.x),toScreenY(p2.y),
 
464
                  toScreenX(p3.x),toScreenY(p3.y));
 
465
    setBrush(pen().color());
 
466
    drawPolygon(arr);
 
467
}
 
468
 
 
469
 
 
470
void RS_PainterQt::erase() {
 
471
    QPainter::eraseRect(0,0,getWidth(),getHeight());
 
472
}
 
473
 
 
474
 
 
475
int RS_PainterQt::getWidth() {
 
476
    return device()->width();
 
477
}
 
478
 
 
479
/** get Density per millimeter on screen/print device
 
480
  *@return density per millimeter in pixel/mm
 
481
  */
 
482
double RS_PainterQt::getDpmm() {
 
483
    int mm(device()->widthMM());
 
484
    if(mm==0) mm=400;
 
485
    return double(device()->width())/mm;
 
486
}
 
487
 
 
488
 
 
489
int RS_PainterQt::getHeight() {
 
490
    return device()->height();
 
491
}
 
492
 
 
493
RS_Pen RS_PainterQt::getPen() {
 
494
    return lpen;
 
495
    //RS_Pen p(pen().color(),
 
496
    //         RS2::qw(pen().width()),
 
497
    //         RS2::qw(pen().style()));
 
498
    //return QPainter::pen();
 
499
    //return p;
 
500
}
 
501
 
 
502
void RS_PainterQt::setPen(const RS_Pen& pen) {
 
503
    lpen = pen;
 
504
    if (drawingMode==RS2::ModeBW) {
 
505
        lpen.setColor(RS_Color(0,0,0));
 
506
    }
 
507
    QPen p(lpen.getColor(), RS_Math::round(lpen.getScreenWidth()),
 
508
           RS2::rsToQtLineType(lpen.getLineType()));
 
509
    p.setJoinStyle(Qt::RoundJoin);
 
510
    p.setCapStyle(Qt::RoundCap);
 
511
    QPainter::setPen(p);
 
512
}
 
513
 
 
514
void RS_PainterQt::setPen(const RS_Color& color) {
 
515
    if (drawingMode==RS2::ModeBW) {
 
516
        lpen.setColor(RS_Color(0,0,0));
 
517
        QPainter::setPen(RS_Color(0,0,0));
 
518
    } else {
 
519
        lpen.setColor(color);
 
520
        QPainter::setPen(color);
 
521
    }
 
522
}
 
523
 
 
524
void RS_PainterQt::setPen(int r, int g, int b) {
 
525
    if (drawingMode==RS2::ModeBW) {
 
526
        setPen(QColor(0, 0, 0));
 
527
    } else {
 
528
        setPen(QColor(r, g, b));
 
529
    }
 
530
}
 
531
 
 
532
void RS_PainterQt::disablePen() {
 
533
    lpen = RS_Pen(RS2::FlagInvalid);
 
534
    QPainter::setPen(Qt::NoPen);
 
535
}
 
536
 
 
537
void RS_PainterQt::setBrush(const RS_Color& color) {
 
538
    if (drawingMode==RS2::ModeBW) {
 
539
        QPainter::setBrush(QColor(0, 0, 0));
 
540
    } else {
 
541
        QPainter::setBrush(color);
 
542
    }
 
543
}
 
544
 
 
545
 
 
546
void RS_PainterQt::drawPolygon(const QPolygon& a, Qt::FillRule rule) {
 
547
    QPainter::drawPolygon(a,rule);
 
548
}
 
549
 
 
550
void RS_PainterQt::drawPath ( const QPainterPath & path ) {
 
551
    QPainter::drawPath(path);
 
552
}
 
553
 
 
554
 
 
555
void RS_PainterQt::setClipRect(int x, int y, int w, int h) {
 
556
    QPainter::setClipRect(x, y, w, h);
 
557
    setClipping(true);
 
558
}
 
559
 
 
560
void RS_PainterQt::resetClipping() {
 
561
    setClipping(false);
 
562
}
 
563
 
 
564
void RS_PainterQt::fillRect ( const QRectF & rectangle, const RS_Color & color ) {
 
565
 
 
566
        double x1=rectangle.left();
 
567
        double x2=rectangle.right();
 
568
        double y1=rectangle.top();
 
569
        double y2=rectangle.bottom();
 
570
 
 
571
        QPainter::fillRect(toScreenX(x1),toScreenY(y1),toScreenX(x2)-toScreenX(x1),toScreenY(y2)-toScreenX(y1), color);
 
572
}
 
573
void RS_PainterQt::fillRect ( const QRectF & rectangle, const QBrush & brush ) {
 
574
        double x1=rectangle.left();
 
575
        double x2=rectangle.right();
 
576
        double y1=rectangle.top();
 
577
        double y2=rectangle.bottom();
 
578
        QPainter::fillRect(toScreenX(x1),toScreenY(y1),toScreenX(x2),toScreenY(y2), brush);
 
579
}