1 /*
2 # PostgreSQL Database Modeler (pgModeler)
3 #
4 # Copyright 2006-2020 - Raphael Araújo e Silva <raphael@pgmodeler.io>
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 version 3.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # The complete text of GPLv3 is at LICENSE file on source code root directory.
16 # Also, you can get the complete GNU General Public License at <http://www.gnu.org/licenses/>
17 */
18 
19 /**
20 \ingroup libobjrenderer
21 \class BaseObjectView
22 \brief Implements the basic routines to graphically represent the objects: table, relationship, textbox, view.
23 */
24 
25 #ifndef BASE_OBJECT_VIEW_H
26 #define BASE_OBJECT_VIEW_H
27 
28 #include <QtWidgets>
29 #include "basegraphicobject.h"
30 #include "baserelationship.h"
31 #include "xmlparser.h"
32 #include "roundedrectitem.h"
33 #include "textpolygonitem.h"
34 
35 class BaseObjectView: public QObject, public QGraphicsItemGroup {
36 	private:
37 		Q_OBJECT
38 
39 	protected:
40 		/*! \brief Indicates if the placeholder object must be used when moving objects.
41 		Place holder objects when enabled causes a significant performance gain mainly when
42 		moving tables linked to relationships because the relationships will be updated only
43 		when the table moviment ends and not during it */
44 		static bool use_placeholder;
45 
46 		/*! \brief Stores the global selection order of objects. This attributes
47 		 is incremented each time an object is selected. */
48 		static unsigned global_sel_order;
49 
50 		/*! \brief Stores the current status for object's compact view display.
51 		 * In compact view the alias of object's are shown instead of name. Also, data types and constraint types are hidden,
52 		 * as well the table's extended area is collapsed by default */
53 		static bool compact_view;
54 
55 		/*! \brief Stores the selection order of the current object. This attribute is used to
56 		 know when an item was selected before another in the scene because the implementation of
57 		 the method QGraphicsScene :: selectedItem() the selected objects are returned without
58 		 any sort, but for the database model objects is the ESSENTIAL to know the selection order
59 		 mainly when creating relationships between tables. */
60 		unsigned sel_order;
61 
62 		//! \brief This item display the current object position on the scene
63 		TextPolygonItem *pos_info_item;
64 
65 		//! \brief Stores the objects bounding rect
66 		QRectF bounding_rect;
67 
68 		//! \brief Graphical object that represents the object selection
69 		QGraphicsItem *obj_selection;
70 
71 		//! \brief Icon that represent the object protection
72 		QGraphicsItemGroup *protected_icon;
73 
74 		//! \brief Graphical object that represents the current object shadow
75 		QGraphicsItem *obj_shadow;
76 
77 		//! \brief Graphical object that represents the placeholder when the object is being moved
78 		RoundedRectItem *placeholder;
79 
80 		//! \brief Graphical object of the sql disabled info
81 		QGraphicsRectItem *sql_disabled_box;
82 
83 		//! \brief Graphical text for the sql disabled info
84 		QGraphicsSimpleTextItem *sql_disabled_txt;
85 
86 		//! \brief This items is used to display the sql disabled status of the object
87 		TextPolygonItem *sql_disabled_item;
88 
89 		//! \brief Stores the object font configuration
90 		static map<QString, QTextCharFormat> font_config;
91 
92 		//! \brief Stores the object colors configuration
93 		static map<QString, vector<QColor>> color_config;
94 
95 		//! \brief Configures the objects shadow polygon
configureObjectShadow(void)96 		void configureObjectShadow(void) {}
97 
98 		//! \brief Configures the object selection polygon
99 		void configureObjectSelection();
100 
101 		//! \brief Configures the polygons used to show the current object position
102 		void configurePositionInfo(QPointF pos);
103 
104 		//! \brief Configures the rectangle used to show the sql disabled status
105 		void configureSQLDisabledInfo();
106 
107 		//! \brief Configures the icon that denotes the object's protection
108 		void configureProtectedIcon();
109 
110 		void configurePlaceholder();
111 
112 		void mousePressEvent(QGraphicsSceneMouseEvent *event);
113 
114 		void setSelectionOrder(bool selected);
115 
116 		//! \brief Defines in which layer the object is visible
117 		void setLayer(unsigned layer);
118 
119 	public:
120 		static constexpr double VertSpacing=2.0,
121 		HorizSpacing=2.0,
122 		DefaultFontSize=10.0,
123 		ObjectBorderWidth=0.85;
124 
125 		static constexpr int ObjectAlphaChannel=128;
126 
127 		BaseObjectView(BaseObject *object=nullptr);
128 		virtual ~BaseObjectView();
129 
130 		//! \brief Returns the object selection order
131 		unsigned getSelectionOrder();
132 
133 		//! \brief Controls the changes during the object's selection and moving
134 		QVariant itemChange(GraphicsItemChange change, const QVariant &value);
135 
136 		//! \brief Returns the object that is representend by the graphical object
137 		BaseObject *getUnderlyingObject();
138 
139 		//! \brief Loads the font / color styels for the objects from a XML configuration file
140 		static void loadObjectsStyle();
141 
142 		//! \brief Returns the objects bounding rect in local coordination
143 		QRectF boundingRect(void) const;
144 
145 		//! \brief Returns the fill style in a form of gradient for the specified element id
146 		static QLinearGradient getFillStyle(const QString &id);
147 
148 		/*! \brief Returns fill style storing the colors on the specified parameters color1 and color2
149 		 for the specified element id */
150 		static void getFillStyle(const QString &id, QColor &color1, QColor &color2);
151 
152 		//! \brief Returns the border style for the specified element id
153 		static QPen getBorderStyle(const QString &id);
154 
155 		//! \brief Returns the font style for the specified element id
156 		static QTextCharFormat getFontStyle(const QString &id);
157 
158 		static void setPlaceholderEnabled(bool value);
159 
160 		static bool isPlaceholderEnabled();
161 
162 		static void setCompactViewEnabled(bool value);
163 
164 		static bool isCompactViewEnabled();
165 
166 		//! \brief Sets the  font style for the specified element id
167 		static void setFontStyle(const QString &id, QTextCharFormat font_fmt);
168 
169 		//! \brief Sets the color for the specified element id (used to set color for objects and font)
170 		static void setElementColor(const QString &id, QColor color, unsigned color_id);
171 
172 		//! \brief Returns the color for the specified element id (used to get color for objects and font)
173 		static QColor getElementColor(const QString &id, unsigned color_id);
174 
175 		//! \brief Defines the object that the view represents
176 		void setSourceObject(BaseObject *object);
177 
178 		//! \brief Pure virtual object (the derived classes must implement it)
179 		virtual void configureObject(void)=0;
180 
181 		/*! \brief Returns the center point of the whole object.
182 		Note: this is not the same as calling boundingRect()->center(). Instead, this
183 		method calculates the center point based upon the current object's position */
184 		virtual QPointF getCenter();
185 
186 		//! \brief Toggles the wireframe display
187 		virtual void togglePlaceholder(bool visible);
188 
189 		/*! \brief Returns the current font DPI factor of the screen. This factor is used to resize
190 		 * objects according to the screen's resolution/font dpi */
191 		static double getScreenDpiFactor();
192 
193 		//! \brief Returns the current factor between the default font size and the current defined one
194 		static double getFontFactor();
195 
196 		//! \brief Returns the layer in which the object is visible
197 		unsigned getLayer();
198 
199 	protected slots:
200 		//! \brief Make the basic object operations
201 		void __configureObject();
202 
203 		//! \brief Toggles the protection icon
204 		void toggleProtectionIcon(bool value);
205 
206 	signals:
207 		//! \brief Signal emmited when the object is (un)selected
208 		void s_objectSelected(BaseGraphicObject *object, bool selected);
209 
210 		//! \brief Signal emmited whenever the width or height of the table changes
211 		void s_objectDimensionChanged();
212 
213 	friend class ObjectsScene;
214 };
215 
216 #endif
217