chalk

kis_grid_drawer.cpp
1/*
2 * This file is part of Chalk
3 *
4 * Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21#include "kis_grid_drawer.h"
22
23#ifdef HAVE_CONFIG_H
24#include <config.h>
25#endif
26
27#ifdef HAVE_GL
28#include <tqgl.h>
29#endif
30
31#include "kis_config.h"
32#include "kis_image.h"
33#include "kis_perspective_grid.h"
34#include "kis_perspective_grid_manager.h"
35
36TQt::PenStyle GridDrawer::gs2style(TQ_UINT32 s)
37{
38 switch(s)
39 {
40 case 1:
41 return TQt::DashLine;
42 case 2:
43 return TQt::DotLine;
44 case 3:
45 return TQt::DashDotLine;
46 case 4:
47 return TQt::DashDotDotLine;
48 default:
49 return TQt::SolidLine;
50 }
51}
52
53void GridDrawer::drawPerspectiveGrid(KisImageSP image, const TQRect& /*wr*/, const KisSubPerspectiveGrid* grid)
54{
55 Q_UNUSED(image);
56 KisConfig cfg;
57 TQPen mainPen = TQPen ( cfg.getGridMainColor(), 1, gs2style( cfg.getGridMainStyle() ) );
58 TQPen subdivisionPen = TQPen ( cfg.getGridSubdivisionColor(), 1, gs2style( cfg.getGridSubdivisionStyle() ) );
59 setPen(subdivisionPen );
60 // 1 -> top-left corner
61 // 2 -> top-right corner
62 // 3 -> bottom-right corner
63 // 4 -> bottom-left corner
64 // d12 line from top-left to top-right
65 // note that the notion of top-left is purely theorical
66 KisPerspectiveMath::LineEquation d12 = KisPerspectiveMath::computeLineEquation( grid->topLeft(), grid->topRight() ) ;
67 KisPoint v12 = KisPoint(*grid->topLeft() - *grid->topRight());
68 v12.setX( v12.x() / grid->subdivisions()); v12.setY( v12.y() / grid->subdivisions() );
69 KisPerspectiveMath::LineEquation d23 = KisPerspectiveMath::computeLineEquation( grid->topRight(), grid->bottomRight() );
70 KisPoint v23 = KisPoint(*grid->topRight() - *grid->bottomRight());
71 v23.setX( v23.x() / grid->subdivisions()); v23.setY( v23.y() / grid->subdivisions() );
72 KisPerspectiveMath::LineEquation d34 = KisPerspectiveMath::computeLineEquation( grid->bottomRight(), grid->bottomLeft() );
73 KisPerspectiveMath::LineEquation d41 = KisPerspectiveMath::computeLineEquation( grid->bottomLeft(), grid->topLeft() );
74
75 KisPoint horizVanishingPoint = KisPerspectiveMath::computeIntersection(d12,d34);
76 KisPoint vertVanishingPoint = KisPerspectiveMath::computeIntersection(d23,d41);
77
78 for(uint i = 1; i < static_cast<uint>(grid->subdivisions()); i ++)
79 {
80 KisPoint pol1 = *grid->topRight() + i * v12;
81 KisPerspectiveMath::LineEquation d1 = KisPerspectiveMath::computeLineEquation( &pol1, &vertVanishingPoint );
82 KisPoint pol1b = KisPerspectiveMath::computeIntersection(d1,d34);
83 drawLine( pol1.roundTQPoint(), pol1b.roundTQPoint() );
84
85 KisPoint pol2 = *grid->bottomRight() + i * v23;
86 KisPerspectiveMath::LineEquation d2 = KisPerspectiveMath::computeLineEquation( &pol2, &horizVanishingPoint );
87 KisPoint pol2b = KisPerspectiveMath::computeIntersection(d2,d41);
88 drawLine( pol2.roundTQPoint(), pol2b.roundTQPoint() );
89 }
90 setPen(mainPen);
91 drawLine( grid->topLeft(), grid->topRight() );
92 drawLine( grid->topRight(), grid->bottomRight() );
93 drawLine( grid->bottomRight(), grid->bottomLeft() );
94 drawLine( grid->bottomLeft(), grid->topLeft() );
95}
96
97void GridDrawer::drawGrid(KisImageSP image, const TQRect& wr)
98{
99 KisConfig cfg;
100
101 TQ_UINT32 offsetx = cfg.getGridOffsetX();
102 TQ_UINT32 offsety = cfg.getGridOffsetY();
103 TQ_UINT32 hspacing = cfg.getGridHSpacing();
104 TQ_UINT32 vspacing = cfg.getGridVSpacing();
105 TQ_UINT32 subdivision = cfg.getGridSubdivisions() - 1;
106 //double ihspsub = hspacing / (double)subdivision;
107 //double ivspsub = hspacing / (double)subdivision;
108
109 TQ_INT32 imageWidth = image->width();
110 TQ_INT32 imageHeight = image->height();
111
112 // Draw vertical line
113 TQPen mainPen = TQPen ( cfg.getGridMainColor(), 1, gs2style( cfg.getGridMainStyle() ) );
114 TQPen subdivisionPen = TQPen ( cfg.getGridSubdivisionColor(), 1, gs2style( cfg.getGridSubdivisionStyle() ) );
115 TQ_UINT32 i = 0;
116 for( TQ_INT32 x = offsetx; x <= wr.right(); x +=hspacing)
117 {
118 if( i == subdivision )
119 {
120 setPen(mainPen);
121 i = 0;
122 } else {
123 setPen(subdivisionPen);
124 i++;
125 }
126 if( x >= wr.x() )
127 {
128 // Always draw the full line otherwise the line stippling varies
129 // with the location of wr and we get glitchy patterns.
130 drawLine(x, 0, x, imageHeight);
131 }
132 }
133 // Draw horizontal line
134 i = 0;
135 for( TQ_INT32 y = offsety; y <= wr.bottom(); y +=vspacing)
136 {
137 if( i == subdivision )
138 {
139 setPen(mainPen);
140 i = 0;
141 } else {
142 setPen(subdivisionPen);
143 i++;
144 }
145 if( y >= wr.y() )
146 {
147 drawLine(0, y, imageWidth, y);
148 }
149 }
150}
151
152OpenGLGridDrawer::OpenGLGridDrawer()
153{
154#ifdef HAVE_GL
155 glPushAttrib(GL_ALL_ATTRIB_BITS);
156#endif
157}
158
159OpenGLGridDrawer::~OpenGLGridDrawer()
160{
161#ifdef HAVE_GL
162 glPopAttrib();
163#endif
164}
165
166void OpenGLGridDrawer::setPen(const TQPen& pen)
167{
168#ifdef HAVE_GL
169 TQt::PenStyle penStyle = pen.style();
170
171 if (penStyle == TQt::SolidLine) {
172 glDisable(GL_LINE_STIPPLE);
173 } else {
174 GLushort lineStipple;
175
176 switch (penStyle) {
177 case TQt::NoPen:
178 lineStipple = 0;
179 break;
180 default:
181 case TQt::SolidLine:
182 lineStipple = 0xffff;
183 break;
184 case TQt::DashLine:
185 lineStipple = 0x3fff;
186 break;
187 case TQt::DotLine:
188 lineStipple = 0x3333;
189 break;
190 case TQt::DashDotLine:
191 lineStipple = 0x33ff;
192 break;
193 case TQt::DashDotDotLine:
194 lineStipple = 0x333f;
195 break;
196 }
197
198 glEnable(GL_LINE_STIPPLE);
199 glLineStipple(1, lineStipple);
200 }
201
202 TQColor penColor = pen.color();
203
204 glColor3ub(penColor.red(), penColor.green(), penColor.blue());
205#else
206 Q_UNUSED(pen);
207#endif
208}
209
210void OpenGLGridDrawer::drawLine(TQ_INT32 x1, TQ_INT32 y1, TQ_INT32 x2, TQ_INT32 y2)
211{
212#ifdef HAVE_GL
213 glBegin(GL_LINES);
214 glVertex2i(x1, y1);
215 glVertex2i(x2, y2);
216 glEnd();
217#else
218 Q_UNUSED(x1);
219 Q_UNUSED(y1);
220 Q_UNUSED(x2);
221 Q_UNUSED(y2);
222#endif
223}
A double-based point class that can return it's coordinates approximated to integers.
Definition kis_point.h:28