Debian Codegraph @

DatabaseExplorer/ErdTable.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
5 // file name : ErdTable.cpp
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25
26 #include "ErdTable.h"
27 #include "wx/wxsf/CommonFcn.h"
28 #include "wx/wxsf/ShapeCanvas.h"
29
30 #include <wx/recguard.h>
31
32 #include "res/gui/key-p.xpm"
33 #include "res/gui/key-f.xpm"
34
35 using namespace wxSFCommonFcn;
36
37 #define constOffset MAX_ID
38
39 XS_IMPLEMENT_CLONABLE_CLASS(ErdTable,wxSFRoundRectShape);
40
41 ErdTable::ErdTable():wxSFRoundRectShape()
42 {
43 Initialize();
44 }
45
46 ErdTable::ErdTable(Table* tab):wxSFRoundRectShape()
47 {
48 SetUserData(tab);
49 Initialize();
50 }
51
52 ErdTable::ErdTable(const ErdTable& obj):wxSFRoundRectShape(obj)
53 {
54
55 m_pLabel = (wxSFTextShape*) obj.m_pLabel->Clone();
56 if (m_pLabel)
57 {
58 m_pLabel->SetStyle(sfsPROPAGATE_DRAGGING | sfsPROPAGATE_SELECTION);
59 SF_ADD_COMPONENT( m_pLabel, wxT("title") );
60 }
61 }
62
63 ErdTable::~ErdTable()
64 {
65 }
66
67 void ErdTable::Initialize()
68 {
69 SetFill(wxBrush(wxColour(254, 253, 211)));
70
71 AcceptConnection(wxT("All"));
72 AcceptTrgNeighbour(wxT("All"));
73 AcceptSrcNeighbour(wxT("All"));
74
75 AddStyle( sfsLOCK_CHILDREN );
76 AddStyle( sfsSHOW_SHADOW );
77
78 SetBorder( wxPen( wxColour(70, 125, 170), 1, wxPENSTYLE_SOLID ) );
79 SetFill( wxBrush( wxColour(210, 225, 245) ) );
80
81 SetRadius(15);
82
83 m_pLabel = new wxSFTextShape();
84 m_pGrid = new wxSFFlexGridShape();
85
86 if (m_pLabel && m_pGrid)
87 {
88 m_pLabel->SetVAlign(wxSFShapeBase::valignTOP);
89 m_pLabel->SetHAlign(wxSFShapeBase::halignCENTER);
90 m_pLabel->SetVBorder(1);
91 m_pLabel->SetHBorder(5);
92
93 m_pLabel->GetFont().SetPointSize( 8 );
94 m_pLabel->GetFont().SetWeight(wxFONTWEIGHT_BOLD);
95
96 m_pLabel->SetText(wxT("DBETable name"));
97 m_pLabel->SetStyle( sfsHOVERING | sfsALWAYS_INSIDE | sfsPROCESS_DEL | sfsEMIT_EVENTS |sfsPROPAGATE_DRAGGING | sfsPROPAGATE_SELECTION );
98
99 SF_ADD_COMPONENT( m_pLabel, wxT("title") );
100
101 // set grid
102 m_pGrid->SetRelativePosition( 0, 17 );
103 m_pGrid->SetStyle( sfsALWAYS_INSIDE | sfsPROCESS_DEL |sfsPROPAGATE_DRAGGING | sfsPROPAGATE_SELECTION );
104 m_pGrid->SetDimensions( 1, 2 );
105
106 m_pGrid->SetFill( *wxTRANSPARENT_BRUSH );
107 m_pGrid->SetBorder( *wxTRANSPARENT_PEN);
108
109 m_pGrid->SetHAlign( wxSFShapeBase::halignLEFT );
110 m_pGrid->SetVBorder( 13 );
111 m_pGrid->SetHBorder( 5 );
112 m_pGrid->AcceptChild( wxT("wxSFTextShape") );
113 m_pGrid->AcceptChild( wxT("wxSFBitmapShape") );
114 m_pGrid->AcceptChild( wxT("wxSFShapeBase") );
115 m_pGrid->Activate( false );
116
117 SF_ADD_COMPONENT( m_pGrid, wxT("main_grid") );
118 }
119 }
120
121 void ErdTable::DrawHighlighted(wxDC& dc)
122 {
123 wxSFRoundRectShape::DrawHighlighted(dc);
124 DrawDetails(dc);
125 }
126
127 void ErdTable::DrawDetails(wxDC& dc)
128 {
129 dc.SetPen( *wxWHITE_PEN );
130 dc.SetBrush( *wxWHITE_BRUSH );
131
132 dc.DrawRectangle( Conv2Point(GetAbsolutePosition() + wxRealPoint(1, m_nRadius)),
133 Conv2Size(m_nRectSize - wxRealPoint(2, 2*m_nRadius - 2)) );
134 }
135
136 void ErdTable::DrawHover(wxDC& dc)
137 {
138 wxSFRoundRectShape::DrawHover(dc);
139 DrawDetails(dc);
140 }
141
142 void ErdTable::DrawNormal(wxDC& dc)
143 {
144 wxSFRoundRectShape::DrawNormal(dc);
145 DrawDetails(dc);
146 }
147
148 void ErdTable::UpdateColumns()
149 {
150 ClearGrid();
151 ClearConnections();
152
153 SetRectSize(GetRectSize().x, 0);
154
155 ShapeList list;
156 if (GetShapeManager())
157 {
158 GetShapeManager()->GetShapes(CLASSINFO(ErdTable), list);
159 }
160
161 int i = 0;
162 Constraint::constraintType type;
163
164 Table* tab = (Table*) wxDynamicCast(GetUserData(),Table);
165 if (tab)
166 {
167 m_pLabel->SetText(tab->GetName());
168 SerializableList::compatibility_iterator node = tab->GetFirstChildNode();
169 while( node )
170 {
171 Column* pCol = wxDynamicCast(node->GetData(), Column);
172 if( pCol )
173 {
174 type = Constraint::noKey;
175
176 SerializableList::compatibility_iterator cstrNode = tab->GetFirstChildNode();
177 while( cstrNode )
178 {
179 Constraint* constr = wxDynamicCast(cstrNode->GetData(), Constraint);
180 if (constr)
181 {
182 if (constr->GetLocalColumn() == pCol->GetName())
183 {
184 type = constr->GetType();
185 }
186 }
187 cstrNode = cstrNode->GetNext();
188 }
189
190 AddColumnShape(pCol->GetName(), i, type);
191 i += 2;
192
193 }
194 node = node->GetNext();
195 }
196
197 node = tab->GetFirstChildNode();
198 while( node )
199 {
200 if( node->GetData()->IsKindOf( CLASSINFO(Constraint)) )
201 {
202 ErdTable* pSecondTab = NULL;
203 Constraint* pConstr = wxDynamicCast(node->GetData(), Constraint);
204
205 ShapeList::compatibility_iterator nodeTab = list.GetFirst();
206 while(nodeTab)
207 {
208 ErdTable* pTab = wxDynamicCast(nodeTab->GetData(), ErdTable);
209 if (pTab)
210 {
211 if (pTab->GetTable()->GetName() == pConstr->GetRefTable()) pSecondTab = pTab;
212 }
213 nodeTab = nodeTab->GetNext();
214 }
215 if (pSecondTab)
216 {
217 GetShapeManager()->CreateConnection(GetId(), pSecondTab->GetId(), new ErdForeignKey(pConstr), sfDONT_SAVE_STATE);
218 }
219 }
220 node = node->GetNext();
221 }
222 }
223
224 m_pGrid->Update();
225 Update();
226 }
227
228 void ErdTable::ClearGrid()
229 {
230 m_pGrid->RemoveChildren();
231 // re-initialize grid control
232 m_pGrid->ClearGrid();
233 m_pGrid->SetDimensions( 1, 2 );
234 m_pGrid->SetCellSpace( 2 );
235 Refresh();
236 }
237
238 void ErdTable::AddColumnShape(const wxString& colName, int id, Constraint::constraintType type)
239 {
240 if( type != Constraint::noKey )
241 {
242 // key bitmap
243 wxSFBitmapShape* pBitmap = new wxSFBitmapShape();
244 if( pBitmap )
245 {
246 pBitmap->SetId(id + 10000);
247 if( m_pGrid->AppendToGrid(pBitmap) )
248 {
249 if( type == Constraint::primaryKey )
250 {
251 pBitmap->CreateFromXPM( key_p_xpm );
252 }
253 else
254 pBitmap->CreateFromXPM( key_f_xpm );
255
256 SetCommonProps(pBitmap);
257 }
258 else
259 delete pBitmap;
260 }
261 }
262 else
263 {
264 // spacer
265 wxSFShapeBase* pSpacer = new wxSFShapeBase();
266 if( pSpacer )
267 {
268 pSpacer->SetId(id+10000);
269 if( m_pGrid->AppendToGrid(pSpacer) )
270 {
271 SetCommonProps(pSpacer);
272 }
273 else
274 delete pSpacer;
275 }
276 }
277
278 // label
279 wxSFTextShape* pCol = new wxSFTextShape();
280 if (pCol)
281 {
282 pCol->SetId(id +10000+ 1);
283 if ( m_pGrid->AppendToGrid(pCol) )
284 {
285 SetCommonProps(pCol);
286 pCol->GetFont().SetPointSize(8);
287 pCol->SetText( colName );
288 }
289 else
290 delete pCol;
291 }
292 }
293
294 void ErdTable::SetCommonProps(wxSFShapeBase* shape)
295 {
296 shape->EnableSerialization(false);
297 shape->SetStyle(sfsALWAYS_INSIDE | sfsPROCESS_DEL | sfsPROPAGATE_DRAGGING | sfsPROPAGATE_SELECTION);
298 shape->SetHAlign(wxSFShapeBase::halignLEFT);
299 shape->SetVAlign(wxSFShapeBase::valignMIDDLE);
300 shape->SetVBorder(0);
301 shape->SetHBorder(0);
302 shape->Activate(false);
303 }
304
305 void ErdTable::AddColumn(const wxString& colName, IDbType* type)
306 {
307 Table* tab = (Table*) wxDynamicCast(GetUserData(),Table);
308 tab->AddColumn(new Column(colName,_("New table"),type));
309 }
310
311 void ErdTable::ClearConnections()
312 {
313 ShapeList lstShapes;
314 GetShapeManager()->GetAssignedConnections(this,CLASSINFO(ErdForeignKey),lineSTARTING ,lstShapes);
315
316 // remove all child shapes
317 ShapeList::compatibility_iterator node = lstShapes.GetFirst();
318 while( node )
319 {
320 ErdForeignKey* key = wxDynamicCast(node->GetData(),ErdForeignKey);
321 if (key)
322 {
323 GetShapeManager()->RemoveShape(key);
324 }
325 node = node->GetNext();
326 }
327 }