1
/****************************************************************************
3
** This file is part of the LibreCAD project, a 2D CAD program
5
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
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.
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.
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
23
** This copyright notice MUST APPEAR in all copies of the script!
25
**********************************************************************/
28
#include "rs_painterqt.h"
33
// RVT_PORT changed from RS_PainterQt::RS_PainterQt( const QPaintDevice* pd)
34
RS_PainterQt::RS_PainterQt( QPaintDevice* pd)
35
: QPainter(pd), RS_Painter() {}
41
RS_PainterQt::~RS_PainterQt() {}
45
void RS_PainterQt::moveTo(int x, int y) {
46
//RVT_PORT changed from QPainter::moveTo(x,y);
52
void RS_PainterQt::lineTo(int x, int y) {
53
// RVT_PORT changed from QPainter::lineTo(x, y);
55
path.moveTo(rememberX,rememberY);
57
QPainter::drawPath(path);
63
* Draws a grid point at (x1, y1).
65
void RS_PainterQt::drawGridPoint(const RS_Vector& p) {
66
QPainter::drawPoint(toScreenX(p.x), toScreenY(p.y));
72
* Draws a point at (x1, y1).
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));
84
* Draws a line from (x1, y1) to (x2, y2).
86
void RS_PainterQt::drawLine(const RS_Vector& p1, const RS_Vector& p2) {
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));
92
QPainter::drawLine(toScreenX(p1.x), toScreenY(p1.y),
93
toScreenX(p2.x), toScreenY(p2.y));
103
* Draws a rectangle with corners p1, p2.
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));
121
* Draws an arc which starts / ends exactly at the given coordinates.
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
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,
139
QPainter::drawArc(cx-radius, cy-radius,
148
drawArcMac(cp, radius, a1, a2, reversed);
150
int cix; // Next point on circle
152
double aStep; // Angle Step (rad)
153
double a; // Current Angle (rad)
154
double linStep; // linear step (pixels)
156
if (drawingMode==RS2::ModePreview) {
162
if (fabs(linStep/radius)<=1.0) {
163
aStep=asin(linStep/radius);
173
// Arc Counterclockwise:
177
//moveTo(toScreenX(p1.x), toScreenY(p1.y));
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);
187
pa.setPoint(i++, cix, ciy);
189
//lineTo(toScreenX(p2.x), toScreenY(p2.y));
191
pa.setPoint(i++, toScreenX(p2.x), toScreenY(p2.y));
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);
208
pa.setPoint(i++, cix, ciy);
210
//lineTo(toScreenX(p2.x), toScreenY(p2.y));
212
pa.setPoint(i++, toScreenX(p2.x), toScreenY(p2.y));
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
231
void RS_PainterQt::drawArc(const RS_Vector& cp, double radius,
232
double a1, double a2,
238
drawArcMac(cp, radius, a1, a2, reversed);
241
createArc(pa, cp, radius, a1, a2, reversed);
249
* Draws an arc on apple.
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
258
void RS_PainterQt::drawArcMac(const RS_Vector& cp, double radius,
259
double a1, double a2,
261
RS_DEBUG->print("RS_PainterQt::drawArcMac");
266
//createArc(pa, cp, radius, a1, a2, reversed);
268
double cix; // Next point on circle
270
double aStep; // Angle Step (rad)
271
double a; // Current Angle (rad)
275
if(2.0/radius<=1.0) {
276
aStep=asin(2.0/radius);
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;
295
// Arc Counterclockwise:
299
for(a=a1+aStep; a<=a2; a+=aStep) {
300
cix = cp.x+cos(a)*radius;
301
ciy = cp.y-sin(a)*radius;
303
drawLine(RS_Vector(ox, oy), RS_Vector(cix, ciy));
307
//pa.setPoint(i++, cix, ciy);
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));
322
//pa.setPoint(i++, cix, ciy);
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));
332
// toScreenX(cp.x+cos(a2)*radius),
333
// toScreenY(cp.y-sin(a2)*radius));
342
* @param cx center in x
343
* @param cy center in y
344
* @param radius Radius
346
void RS_PainterQt::drawCircle(const RS_Vector& cp, double radius) {
348
// RVT_PORT if (drawingMode==RS2::ModeXOR && 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));
365
cp + RS_Vector(radius, 0.0),
366
cp + RS_Vector(radius, 0.0),
375
* Draws a rotated ellipse arc.
377
void RS_PainterQt::drawEllipse(const RS_Vector& cp,
378
double radius1, double radius2,
380
double a1, double a2,
383
createEllipse(pa, cp, radius1, radius2, angle, a1, a2, reversed);
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) {
397
// Render smooth only at close zooms
398
if (factor.x < 1 || factor.y < 1) {
399
RS_PainterQt::setRenderHint(SmoothPixmapTransform , true);
402
RS_PainterQt::setRenderHint(SmoothPixmapTransform);
406
wm.translate(pos.x, pos.y);
407
wm.scale(factor.x, factor.y);
408
wm.rotate(RS_Math::rad2deg(-angle));
412
if (fabs(angle)<1.0e-4) {
413
drawImage(0+sx,-img.height()+sy, img, sx, sy, sw, sh);
416
drawImage(0,-img.height(), img);
424
void RS_PainterQt::drawTextH(int x1, int y1,
426
const QString& text) {
427
drawText(x1, y1, x2, y2,
428
Qt::AlignRight|Qt::AlignVCenter,
434
void RS_PainterQt::drawTextV(int x1, int y1,
436
const QString& text) {
438
QMatrix wm = worldMatrix();
443
drawText(x1, y1, x2, y2,
444
Qt::AlignRight|Qt::AlignVCenter,
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);
456
void RS_PainterQt::fillTriangle(const RS_Vector& p1,
458
const RS_Vector& p3) {
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());
470
void RS_PainterQt::erase() {
471
QPainter::eraseRect(0,0,getWidth(),getHeight());
475
int RS_PainterQt::getWidth() {
476
return device()->width();
479
/** get Density per millimeter on screen/print device
480
*@return density per millimeter in pixel/mm
482
double RS_PainterQt::getDpmm() {
483
int mm(device()->widthMM());
485
return double(device()->width())/mm;
489
int RS_PainterQt::getHeight() {
490
return device()->height();
493
RS_Pen RS_PainterQt::getPen() {
495
//RS_Pen p(pen().color(),
496
// RS2::qw(pen().width()),
497
// RS2::qw(pen().style()));
498
//return QPainter::pen();
502
void RS_PainterQt::setPen(const RS_Pen& pen) {
504
if (drawingMode==RS2::ModeBW) {
505
lpen.setColor(RS_Color(0,0,0));
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);
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));
519
lpen.setColor(color);
520
QPainter::setPen(color);
524
void RS_PainterQt::setPen(int r, int g, int b) {
525
if (drawingMode==RS2::ModeBW) {
526
setPen(QColor(0, 0, 0));
528
setPen(QColor(r, g, b));
532
void RS_PainterQt::disablePen() {
533
lpen = RS_Pen(RS2::FlagInvalid);
534
QPainter::setPen(Qt::NoPen);
537
void RS_PainterQt::setBrush(const RS_Color& color) {
538
if (drawingMode==RS2::ModeBW) {
539
QPainter::setBrush(QColor(0, 0, 0));
541
QPainter::setBrush(color);
546
void RS_PainterQt::drawPolygon(const QPolygon& a, Qt::FillRule rule) {
547
QPainter::drawPolygon(a,rule);
550
void RS_PainterQt::drawPath ( const QPainterPath & path ) {
551
QPainter::drawPath(path);
555
void RS_PainterQt::setClipRect(int x, int y, int w, int h) {
556
QPainter::setClipRect(x, y, w, h);
560
void RS_PainterQt::resetClipping() {
564
void RS_PainterQt::fillRect ( const QRectF & rectangle, const RS_Color & color ) {
566
double x1=rectangle.left();
567
double x2=rectangle.right();
568
double y1=rectangle.top();
569
double y2=rectangle.bottom();
571
QPainter::fillRect(toScreenX(x1),toScreenY(y1),toScreenX(x2)-toScreenX(x1),toScreenY(y2)-toScreenX(y1), color);
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);