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 #ifndef RS_GRAPHICVIEW_H
29 #define RS_GRAPHICVIEW_H
30 
31 #include "rs_entitycontainer.h"
32 #include "rs_snapper.h"
33 #include "lc_rect.h"
34 
35 #include <QDateTime>
36 #include <QMap>
37 #include <tuple>
38 #include <memory>
39 #include <QAction>
40 
41 
42 class QMouseEvent;
43 class QKeyEvent;
44 class RS_ActionInterface;
45 class RS_EventHandler;
46 class RS_CommandEvent;
47 class RS_Grid;
48 struct RS_LineTypePattern;
49 
50 
51 /**
52  * This class is a common GUI interface for the graphic viewer
53  * widget which has to be implemented by real GUI classes such
54  * as the Qt graphical view.
55  *
56  * Note that this is just an interface used as a slot to
57  * communicate with the LibreCAD from a GUI level.
58  */
59 class RS_GraphicView : public QWidget
60 {
61     Q_OBJECT
62 
63 public:
64 	RS_GraphicView(QWidget * parent = 0, Qt::WindowFlags f = 0);
65 	virtual ~RS_GraphicView();
66 
67     void cleanUp();
68 
69 	/**
70 	 * @return Pointer to the graphic entity if the entity container
71 	 * connected to this view is a graphic and valid.
72 	 * NULL otherwise.
73 	 */
74 	RS_Graphic* getGraphic() const;
75 
76 	/**
77 	 * \brief setDrawingMode Sets the drawing mode.
78 	 */
setDrawingMode(RS2::DrawingMode m)79 	void setDrawingMode(RS2::DrawingMode m) {
80 		drawingMode = m;
81 	}
82 
83 	/**
84 	 * @return Current drawing mode.
85 	 */
getDrawingMode()86     RS2::DrawingMode getDrawingMode() const {
87 		return drawingMode;
88 	}
89 
90 	/**
91 	 * Activates or deactivates the delete mode.
92 	 */
setDeleteMode(bool m)93 	void setDeleteMode(bool m) {
94 		deleteMode = m;
95 	}
96 
97 	/**
98 	 * @reval true Deleting instead of drawing.
99 		 *        false Normal drawing mode.
100 	 */
getDeleteMode()101     bool getDeleteMode() const{
102 		return deleteMode;
103 	}
104 
105 	/** This virtual method must be overwritten to return
106 	  the width of the widget the graphic is shown in */
107 	virtual int getWidth() const= 0;
108 	/** This virtual method must be overwritten to return
109 	  the height of the widget the graphic is shown in */
110 	virtual int getHeight() const= 0;
111 	/** This virtual method must be overwritten to redraw
112 	  the widget. */
113 	virtual void redraw(RS2::RedrawMethod method=RS2::RedrawAll) = 0;
114 	/** This virtual method must be overwritten and is then
115 	  called whenever the view changed */
116     virtual void adjustOffsetControls() = 0;
117 	/** This virtual method must be overwritten and is then
118 	  called whenever the view changed */
119     virtual void adjustZoomControls() = 0;
120 
121 	/**
122 	 * Sets the background color. Note that applying the background
123 	 * color for the widget is up to the implementing class.
124 	 */
125 	virtual void setBackground(const RS_Color& bg);
126 
127 	/**
128 		 * @return Current background color.
129 		 */
getBackground()130 	RS_Color getBackground() const{
131 		return background;
132 	}
133 
134 	/**
135 		 * @return Current foreground color.
136 		 */
getForeground()137 	RS_Color getForeground() const{
138 		return foreground;
139 	}
140 
141 	/**
142 		 * Sets the grid color.
143 		 */
setGridColor(const RS_Color & c)144 	void setGridColor(const RS_Color& c) {
145 		gridColor = c;
146 	}
147 
148 	/**
149 		 * Sets the meta grid color.
150 		 */
setMetaGridColor(const RS_Color & c)151 	void setMetaGridColor(const RS_Color& c) {
152 		metaGridColor = c;
153 	}
154 
155 	/**
156 		 * Sets the selection color.
157 		 */
setSelectedColor(const RS_Color & c)158 	void setSelectedColor(const RS_Color& c) {
159 		selectedColor = c;
160 	}
161 
162 	/**
163 		 * Sets the highlight color.
164 		 */
setHighlightedColor(const RS_Color & c)165 	void setHighlightedColor(const RS_Color& c) {
166 		highlightedColor = c;
167 	}
168 
169 	/**
170 		 * Sets the color for the first handle (start vertex)
171 		 */
setStartHandleColor(const RS_Color & c)172 	void setStartHandleColor(const RS_Color& c) {
173 		startHandleColor = c;
174 	}
175 
176 	/**
177 		 * Sets the color for handles, that are neither start nor end vertices
178 		 */
setHandleColor(const RS_Color & c)179 	void setHandleColor(const RS_Color& c) {
180 		handleColor = c;
181 	}
182 
183 	/**
184 		 * Sets the color for the last handle (end vertex)
185 		 */
setEndHandleColor(const RS_Color & c)186 	void setEndHandleColor(const RS_Color& c) {
187 		endHandleColor = c;
188 	}
189 
190 	/**
191 	 * This virtual method can be overwritten to set the mouse
192 	 * cursor to the given type.
193 	 */
194     virtual void setMouseCursor(RS2::CursorType /*c*/) = 0;
195 
196 	void setContainer(RS_EntityContainer* container);
197 	RS_EntityContainer* getContainer() const;
198 	void setFactor(double f);
199 	void setFactorX(double f);
200 	void setFactorY(double f);
201 	RS_Vector getFactor() const;
202 	/**
203 	 * @brief setOffset
204 	 * @param ox, offset X
205 	 * @param oy, offset Y
206 	 */
207 	virtual void setOffset(int ox, int oy);
208 	void setOffsetX(int ox);
209 	void setOffsetY(int oy);
210 	int getOffsetX() const;
211 	int getOffsetY() const;
212 	void centerOffsetX();
213 	void centerOffsetY();
214 	void centerX(double x);
215 	void centerY(double y);
216 	/**
217 	 * Sets a fixed border in pixel around the graphic. This border
218 	 * specifies how far the user can scroll outside the graphic
219 	 * area.
220 	 */
221 	void setBorders(int left, int top, int right, int bottom);
222 
223 	int getBorderLeft() const;
224 	int getBorderTop() const;
225 	int getBorderRight() const;
226 	int getBorderBottom() const;
227 
228 	void freezeZoom(bool freeze);
229 	bool isZoomFrozen() const;
230 
231 	void setDefaultAction(RS_ActionInterface* action);
232 	RS_ActionInterface*  getDefaultAction();
233     void setCurrentAction(RS_ActionInterface* action);
234 	RS_ActionInterface* getCurrentAction();
235 
236 	void killSelectActions();
237 	void killAllActions();
238 
239 	void back();
240 	void enter();
241 
242 	void commandEvent(RS_CommandEvent* e);
243 	void enableCoordinateInput();
244 	void disableCoordinateInput();
245 
246 	virtual void zoomIn(double f=1.5, const RS_Vector& center=RS_Vector(false));
247 	virtual void zoomInX(double f=1.5);
248 	virtual void zoomInY(double f=1.5);
249 	virtual void zoomOut(double f=1.5, const RS_Vector& center=RS_Vector(false));
250 	virtual void zoomOutX(double f=1.5);
251 	virtual void zoomOutY(double f=1.5);
252 	virtual void zoomAuto(bool axis=true, bool keepAspectRatio=true);
253 	virtual void zoomAutoY(bool axis=true);
254 	virtual void zoomPrevious();
255 	virtual void saveView();
256 	virtual void restoreView();
257 	virtual void zoomWindow(RS_Vector v1, RS_Vector v2,
258 							bool keepAspectRatio=true);
259 	//virtual void zoomPan(RS_Vector v1);
260 	virtual void zoomPan(int dx, int dy);
261 	virtual void zoomScroll(RS2::Direction direction);
262 	virtual void zoomPage();
263 
264 	virtual void drawWindow_DEPRECATED(RS_Vector v1, RS_Vector v2);
265 	virtual void drawLayer1(RS_Painter *painter);
266 	virtual void drawLayer2(RS_Painter *painter);
267 	virtual void drawLayer3(RS_Painter *painter);
268 	virtual void deleteEntity(RS_Entity* e);
269 	virtual void drawEntity(RS_Painter *painter, RS_Entity* e, double& patternOffset);
270 	virtual void drawEntity(RS_Painter *painter, RS_Entity* e);
271 	virtual void drawEntity(RS_Entity* e, double& patternOffset);
272 	virtual void drawEntity(RS_Entity* e);
273 	virtual void drawEntityPlain(RS_Painter *painter, RS_Entity* e);
274 	virtual void drawEntityPlain(RS_Painter *painter, RS_Entity* e, double& patternOffset);
275 	virtual void setPenForEntity(RS_Painter *painter, RS_Entity* e );
276     virtual RS_Vector getMousePosition() const = 0;
277 
278 	virtual const RS_LineTypePattern* getPattern(RS2::LineType t);
279 
280 	virtual void drawAbsoluteZero(RS_Painter *painter);
281 	virtual void drawRelativeZero(RS_Painter *painter);
282 	virtual void drawPaper(RS_Painter *painter);
283 	virtual void drawGrid(RS_Painter *painter);
284 	virtual void drawMetaGrid(RS_Painter *painter);
285 	virtual void drawOverlay(RS_Painter *painter);
286 
287 	RS_Grid* getGrid() const;
288     virtual void updateGridStatusWidget(const QString& /*text*/) = 0;
289 
290 	void setDefaultSnapMode(RS_SnapMode sm);
291 	RS_SnapMode getDefaultSnapMode() const;
292 	void setSnapRestriction(RS2::SnapRestriction sr);
293 	RS2::SnapRestriction getSnapRestriction() const;
294 
295 	bool isGridOn() const;
296 	bool isGridIsometric() const;
297 	void setCrosshairType(RS2::CrosshairType chType);
298 	RS2::CrosshairType getCrosshairType() const;
299 
300 	RS_Vector toGui(RS_Vector v) const;
301 	double toGuiX(double x) const;
302 	double toGuiY(double y) const;
303 	double toGuiDX(double d) const;
304 	double toGuiDY(double d) const;
305 
306 	RS_Vector toGraph(RS_Vector v) const;
307 	RS_Vector toGraph(int x, int y) const;
308 	double toGraphX(int x) const;
309 	double toGraphY(int y) const;
310 	double toGraphDX(int d) const;
311 	double toGraphDY(int d) const;
312 
313 	/**
314 		 * (Un-)Locks the position of the relative zero.
315 		 *
316 		 * @param lock true: lock, false: unlock
317 		 */
318 	void lockRelativeZero(bool lock);
319 
320 	/**
321 		 * @return true if the position of the relative zero point is
322 		 * locked.
323 		 */
324 	bool isRelativeZeroLocked() const;
325 
326 	/**
327 		 * @return Relative zero coordinate.
328 		 */
329 	RS_Vector const& getRelativeZero() const;
330 
331 	void setRelativeZero(const RS_Vector& pos);
332 	void moveRelativeZero(const RS_Vector& pos);
333 
334 	RS_EventHandler* getEventHandler() const;
335 
336 	/**
337 		 * Enables or disables print preview.
338 		 */
339 	void setPrintPreview(bool pv);
340 
341 	/**
342 		 * @retval true This is a print preview graphic view.
343 		 * @retval false Otherwise.
344 		 */
345 	bool isPrintPreview() const;
346 
347 	/**
348 		 * Enables or disables printing.
349 		 */
350 	void setPrinting(bool p);
351 
352 	/**
353 		 * @retval true This is a graphic view for printing.
354 		 * @retval false setSnapOtherwise.
355 		 */
356 	bool isPrinting() const;
357 
358 	/**
359 		 * @retval true Draft mode is on for this view (all lines with 1 pixel / no style scaling).
360 		 * @retval false Otherwise.
361 		 */
362 	bool isDraftMode() const;
363 
364 	void setDraftMode(bool dm);
365 	bool isCleanUp(void) const;
366 
367 	virtual RS_EntityContainer* getOverlayContainer(RS2::OverlayGraphics position);
368 
getViewRect()369     const LC_Rect& getViewRect() {
370         return view_rect;
371     }
372 
373     bool isPanning() const;
374     void setPanning(bool state);
375 
setLineWidthScaling(bool state)376     void setLineWidthScaling(bool state){
377         scaleLineWidth = state;
378     }
379 
getLineWidthScaling()380     bool getLineWidthScaling(){
381         return scaleLineWidth;
382     }
383 
384 
385 protected:
386 
387     RS_EntityContainer* container{nullptr}; // Holds a pointer to all the enties
388     RS_EventHandler* eventHandler;
389 
390 	/** background color (any color) */
391 	RS_Color background;
392 	/** foreground color (black or white) */
393 	RS_Color foreground;
394 	/** grid color */
395 	RS_Color gridColor;
396 	/** meta grid color */
397 	RS_Color metaGridColor;
398 	/** selected color */
399 	RS_Color selectedColor;
400 	/** highlighted color */
401 	RS_Color highlightedColor;
402 	/** Start handle color */
403 	RS_Color startHandleColor;
404 	/** Intermediate (not start/end vertex) handle color */
405 	RS_Color handleColor;
406 	/** End handle color */
407 	RS_Color endHandleColor;
408 	/** Grid */
409 	std::unique_ptr<RS_Grid> grid;
410 	/**
411 		 * Current default snap mode for this graphic view. Used for new
412 		 * actions.
413 		 */
414 	RS_SnapMode defaultSnapMode;
415 	/**
416 		 * Current default snap restriction for this graphic view. Used for new
417 		 * actions.
418 		 */
419 	RS2::SnapRestriction defaultSnapRes;
420 
421 	RS2::DrawingMode drawingMode;
422 
423 	/**
424 		 * Delete mode. If true, all drawing actions will delete in background color
425 		 * instead.
426 		 */
427 	bool deleteMode=false;
428 
429     LC_Rect view_rect;
430 
431 private:
432 
433 	bool zoomFrozen=false;
434 	bool draftMode=false;
435 
436 	RS_Vector factor=RS_Vector(1.,1.);
437 	int offsetX=0;
438 	int offsetY=0;
439 
440 	//circular buffer for saved views
441 	std::vector<std::tuple<int, int, RS_Vector> > savedViews;
442 	unsigned short savedViewIndex=0;
443 	unsigned short savedViewCount=0;
444 	QDateTime previousViewTime;
445 
446 	int borderLeft=0;
447 	int borderTop=0;
448 	int borderRight=0;
449 	int borderBottom=0;
450 
451 	RS_Vector relativeZero{false};
452 	bool relativeZeroLocked=false;
453 	//! Print preview flag
454 	bool printPreview=false;
455 	//! Active when printing only:
456 	bool printing=false;
457 
458 	// Map that will be used for overlaying additional items on top of the main CAD drawing
459 	QMap<int, RS_EntityContainer *> overlayEntities;
460 	/** if true, graphicView is under cleanup */
461 	bool m_bIsCleanUp=false;
462 
463     bool panning;
464 
465 	bool scaleLineWidth;
466 
467 signals:
468     void relative_zero_changed(const RS_Vector&);
469     void previous_zoom_state(bool);
470 };
471 
472 #endif
473