/
basetableview.cpp
270 lines (219 loc) · 7.54 KB
/
basetableview.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
# PostgreSQL Database Modeler (pgModeler)
#
# Copyright 2006-2016 - Raphael Araújo e Silva <raphael@pgmodeler.com.br>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# The complete text of GPLv3 is at LICENSE file on source code root directory.
# Also, you can get the complete GNU General Public License at <http://www.gnu.org/licenses/>
*/
#include "basetableview.h"
bool BaseTableView::hide_ext_attribs=false;
bool BaseTableView::hide_tags=false;
BaseTableView::BaseTableView(BaseTable *base_tab) : BaseObjectView(base_tab)
{
if(!base_tab)
throw Exception(ERR_ASG_NOT_ALOC_OBJECT, __PRETTY_FUNCTION__, __FILE__, __LINE__);
body=new RoundedRectItem;
body->setRoundedCorners(RoundedRectItem::BOTTOMLEFT_CORNER | RoundedRectItem::BOTTOMRIGHT_CORNER);
title=new TableTitleView;
ext_attribs_body=new RoundedRectItem;
ext_attribs_body->setRoundedCorners(RoundedRectItem::BOTTOMLEFT_CORNER | RoundedRectItem::BOTTOMRIGHT_CORNER);
ext_attribs=new QGraphicsItemGroup;
ext_attribs->setZValue(1);
columns=new QGraphicsItemGroup;
columns->setZValue(1);
tag_name=new QGraphicsSimpleTextItem;
tag_name->setZValue(3);
tag_body=new QGraphicsPolygonItem;
tag_body->setZValue(2);
obj_shadow=new RoundedRectItem;
obj_shadow->setZValue(-1);
obj_selection=new RoundedRectItem;
obj_selection->setVisible(false);
obj_selection->setZValue(4);
this->addToGroup(obj_selection);
this->addToGroup(obj_shadow);
this->addToGroup(columns);
this->addToGroup(body);
this->addToGroup(title);
this->addToGroup(tag_name);
this->addToGroup(tag_body);
this->addToGroup(ext_attribs);
this->addToGroup(ext_attribs_body);
this->setAcceptHoverEvents(true);
sel_child_obj=nullptr;
connected_rels=0;
configurePlaceholder();
}
BaseTableView::~BaseTableView(void)
{
this->removeFromGroup(body);
this->removeFromGroup(title);
this->removeFromGroup(ext_attribs_body);
this->removeFromGroup(ext_attribs);
this->removeFromGroup(columns);
this->removeFromGroup(tag_name);
this->removeFromGroup(tag_body);
delete(ext_attribs_body);
delete(ext_attribs);
delete(body);
delete(title);
delete(columns);
delete(tag_name);
delete(tag_body);
}
void BaseTableView::setHideExtAttributes(bool value)
{
hide_ext_attribs=value;
}
void BaseTableView::setHideTags(bool value)
{
hide_tags=value;
}
bool BaseTableView::isExtAttributesHidden(void)
{
return(hide_ext_attribs);
}
bool BaseTableView::isTagsHidden(void)
{
return(hide_tags);
}
QVariant BaseTableView::itemChange(GraphicsItemChange change, const QVariant &value)
{
if(change==ItemSelectedHasChanged)
{
this->setToolTip(this->table_tooltip);
BaseObjectView::configureObjectSelection();
}
if(change==ItemPositionHasChanged)
emit s_objectMoved();
BaseObjectView::itemChange(change, value);
return(value);
}
void BaseTableView::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
//Emit a signal containing the select child object if the user right-click the focused item
if(!this->isSelected() && event->buttons()==Qt::RightButton && sel_child_obj)
{
if(this->scene())
this->scene()->clearSelection();
/* Deactivate the table in order not to hide the child object selection.
The table object is reativated when the context menu is hidden */
this->setEnabled(false);
emit s_childObjectSelected(sel_child_obj);
}
else
BaseObjectView::mousePressEvent(event);
}
void BaseTableView::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
{
if(!this->isSelected() && obj_selection->isVisible())
obj_selection->setVisible(false);
sel_child_obj=nullptr;
}
void BaseTableView::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
/* Case the table itself is not selected shows the child selector
at mouse position */
if(!this->isSelected())
{
QList<QGraphicsItem *> items;
double cols_height, item_idx, ext_height=0;
QRectF rect, rect1;
items.append(columns->childItems());
if(!hide_ext_attribs)
{
items.append(ext_attribs->childItems());
ext_height=ext_attribs->boundingRect().height();
}
//Calculates the default item height
cols_height=roundf((columns->boundingRect().height() + ext_height) / static_cast<double>(items.size()));
//Calculates the item index based upon the mouse position
rect=this->mapRectToItem(title, title->boundingRect());
item_idx=(event->pos().y() - rect.bottom()) / cols_height;
//If the index is invalid clears the selection
if(item_idx < 0 || item_idx >= items.size())
{
this->hoverLeaveEvent(event);
this->setToolTip(this->table_tooltip);
}
else if(!items.isEmpty())
{
//QPolygonF pol;
BaseObjectView *item=dynamic_cast<TableObjectView *>(items[item_idx]);
//Configures the selection with the item's dimension
if(obj_selection->boundingRect().height()!=item->boundingRect().height())
{
dynamic_cast<RoundedRectItem *>(obj_selection)->setBorderRadius(2);
dynamic_cast<RoundedRectItem *>(obj_selection)->setRect(QRectF(0,0,
title->boundingRect().width() - (2.5 * HORIZ_SPACING),
item->boundingRect().height()));
}
//Sets the selection position as same as item's position
rect1=this->mapRectToItem(item, item->boundingRect());
obj_selection->setVisible(true);
obj_selection->setPos(QPointF(title->pos().x() + HORIZ_SPACING,-rect1.top()));
//Stores the selected child object
sel_child_obj=dynamic_cast<TableObject *>(item->getSourceObject());
this->setToolTip(item->toolTip());
}
}
}
void BaseTableView::updateConnectedRelsCount(int inc)
{
connected_rels+=inc;
if(connected_rels < 0) connected_rels=0;
}
void BaseTableView::configureTag(void)
{
BaseTable *tab=dynamic_cast<BaseTable *>(this->getSourceObject());
Tag *tag=tab->getTag();
tag_body->setVisible(tag!=nullptr && !hide_tags);
tag_name->setVisible(tag!=nullptr && !hide_tags);
if(!hide_tags && tag)
{
QPolygonF pol;
QPointF p1, p2;
double bottom;
QFont fnt=BaseObjectView::getFontStyle(ParsersAttributes::TAG).font();
fnt.setPointSizeF(fnt.pointSizeF() * 0.80f);
tag_name->setFont(fnt);
tag_name->setText(tag->getName());
tag_name->setBrush(BaseObjectView::getFontStyle(ParsersAttributes::TAG).foreground());
p1=tag_name->boundingRect().topLeft(),
p2=tag_name->boundingRect().bottomRight();
bottom=this->boundingRect().bottom();
pol.append(QPointF(p1.x()-BaseObjectView::HORIZ_SPACING, p1.y() - BaseObjectView::VERT_SPACING));
pol.append(QPointF(p2.x(), p1.y() - BaseObjectView::VERT_SPACING));
pol.append(QPointF(p2.x() + BaseObjectView::HORIZ_SPACING + 5, p2.y()/2));
pol.append(QPointF(p2.x(), p2.y() + BaseObjectView::VERT_SPACING));
pol.append(QPointF(p1.x(), p2.y() + BaseObjectView::VERT_SPACING));
pol.append(QPointF(p1.x()-BaseObjectView::HORIZ_SPACING, p2.y() + BaseObjectView::VERT_SPACING));
tag_body->setPolygon(pol);
tag_body->setPen(BaseObjectView::getBorderStyle(ParsersAttributes::TAG));
tag_body->setBrush(BaseObjectView::getFillStyle(ParsersAttributes::TAG));
tag_name->setPos(-5, bottom - 1.5f);
tag_body->setPos(-5, bottom - 1.5f);
}
}
int BaseTableView::getConnectRelsCount(void)
{
return(connected_rels);
}
void BaseTableView::requestRelationshipsUpdate(void)
{
emit s_relUpdateRequest();
}
void BaseTableView::togglePlaceholder(bool value)
{
BaseObjectView::togglePlaceholder(connected_rels > 0 && value);
}