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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
|
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// gqbGraphsimple.cpp - A simple Implementation of the Graphic Interface for GQB
//
//////////////////////////////////////////////////////////////////////////
// App headers
#include "pgAdmin3.h"
// wxWindows headers
#include <wx/wx.h>
#include <wx/dcbuffer.h>
// App headers
#include "utils/sysSettings.h"
#include "gqb/gqbGraphSimple.h"
#include "gqb/gqbQueryObjs.h"
// GQB Images
#include "images/gqbColNotSel.pngc"
#include "images/gqbColSel.pngc"
gqbGraphSimple::gqbGraphSimple()
{
normalFont = settings->GetSystemFont();
TableTitleFont = settings->GetSystemFont();
TableTitleFont.SetWeight(wxFONTWEIGHT_BOLD);
BackgroundLayer1 = wxBrush(wxColour(112, 112, 112), wxSOLID);
BackgroundLayer2 = wxBrush (wxColour(208, 208, 208), wxSOLID);
BackgroundTitle = wxBrush (wxColour(245, 245, 245), wxSOLID);
minTableWidth = 80;
minTableHeight = 54;
rowHeight = 0; //By default but this it's replaced by font metrics value
rowLeftMargin = 14;
rowRightMargin = 5;
rowTopMargin = 1;
lineClickThreshold = 7;
selectedPen = wxPen(wxColour(0, 146, 195), 2, wxSOLID);
selectedBrush = wxBrush(wxColour(0, 146, 195), wxSOLID);
imgSelBoxEmpty = *gqbColNotSel_png_bmp;
imgSelBoxSelected = *gqbColSel_png_bmp;
}
// NOTES:(1) store values of width & height at queryTable.
// (2)Need to set a font for the device context before get font metrics with GetTextExtent
void gqbGraphSimple::drawTable(wxMemoryDC &bdc, wxPoint *origin, gqbQueryObject *queryTable)
{
#if wxCHECK_VERSION(2, 9, 0)
wxCoord w = 0, h = 0, height = 0, width = 0, margin = 5;
#else
long w = 0, h = 0, height = 0, width = 0, margin = 5;
#endif
// Get Value for row Height
if(!rowHeight)
{
bdc.SetFont(TableTitleFont);
bdc.GetTextExtent(wxT("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxtz"), &w, &h);
rowHeight = h;
}
// Get Title Metrics
bdc.SetFont(TableTitleFont);
height += rowHeight + rowTopMargin;
// Calculate font metrics for table title with/without alias
if(queryTable->getAlias().length() > 0)
bdc.GetTextExtent(queryTable->getName() + wxT(" (") + queryTable->getAlias() + wxT(")"), &w, &h);
else
bdc.GetTextExtent(queryTable->getName(), &w, &h);
width = rowLeftMargin + w + rowRightMargin;
// Get Columns Metrics
bdc.SetFont(normalFont);
// Don't use h value from font metrics to get consistency between columns vertical separation (height)
height += rowHeight * queryTable->parent->countCols() + rowTopMargin * queryTable->parent->countCols();
gqbIteratorBase *iterator = queryTable->parent->createColumnsIterator();
while(iterator->HasNext())
{
gqbColumn *tmp = (gqbColumn *)iterator->Next();
bdc.GetTextExtent(tmp->getName(), &w, &h);
if((rowLeftMargin + w + rowRightMargin) > width)
width = rowLeftMargin + w + rowRightMargin;
}
//Don't delete iterator because will be use below;
// Set table Size in ObjectModel (Temporary Values for object representation,
// and for this reason the view can modified model without using the controller
// because this values are used by controller when use object's size in internal operations)
if( (height + 2) < minTableHeight) // +2 from BackgroundLayers addition
{
queryTable->setHeight(minTableHeight);
height = minTableHeight;
}
else
queryTable->setHeight(height + 2);
if( (width + 2) < minTableWidth)
{
queryTable->setWidth(minTableWidth);
width = minTableWidth;
}
else
queryTable->setWidth(width + 2);
//Decorate Table
bdc.SetPen(*wxTRANSPARENT_PEN);
//draw second Layer
bdc.SetBrush(BackgroundLayer2);
bdc.DrawRectangle(wxRect(wxPoint(origin->x, origin->y), wxSize(width + 2, height + 2)));
//draw third Layer
bdc.SetBrush(BackgroundLayer1);
bdc.DrawRectangle(wxRect(wxPoint(origin->x, origin->y), wxSize(width + 1, height + 1)));
//draw real frame layer
bdc.SetBrush(*wxWHITE_BRUSH);
if(queryTable->getSelected())
{
bdc.SetPen(selectedPen);
}
else
{
bdc.SetPen(*wxBLACK_PEN);
}
bdc.DrawRectangle(wxRect(wxPoint(origin->x, origin->y), wxSize(width, height)));
//draw title layer
bdc.SetBrush(BackgroundTitle);
bdc.DrawRectangle(wxRect(wxPoint(origin->x, origin->y), wxSize(width, rowHeight + rowTopMargin)));
bdc.SetFont(TableTitleFont);
if(queryTable->getAlias().length() > 0)
bdc.DrawText(queryTable->getName() + wxT(" (") + queryTable->getAlias() + wxT(")"), origin->x + margin, origin->y + rowTopMargin);
else
bdc.DrawText(queryTable->getName(), origin->x + margin, origin->y + rowTopMargin);
bdc.SetFont(normalFont);
// GQB-TODO: in a future reuse a little more the iterator creating it inside the Query or Table Object
// and only delete it when delete the query object.
// Draw Columns
height = rowHeight + rowTopMargin;
iterator->ResetIterator();
while(iterator->HasNext())
{
gqbColumn *tmp = (gqbColumn *)iterator->Next();
if(queryTable->existsColumn(tmp))
{
bdc.SetTextForeground(* wxRED);
bdc.SetFont(normalFont);
bdc.DrawBitmap(imgSelBoxSelected, origin->x + 3, origin->y + height, true);
}
else
{
bdc.SetFont(normalFont);
bdc.DrawBitmap(imgSelBoxEmpty, origin->x + 3, origin->y + height, true);
}
bdc.DrawText(tmp->getName(), origin->x + rowLeftMargin, origin->y + height);
bdc.SetTextForeground( *wxBLACK);
height += rowHeight + rowTopMargin;
}
delete iterator; //now if delete because it's not needed anymore
}
// return a column when a user click on a checkbox [0->16] x point
gqbColumn *gqbGraphSimple::getColumnAtPosition(wxPoint *clickPoint, gqbQueryObject *queryTable, int sensibility)
{
int countCols = queryTable->parent->countCols(), colPos = -1;
if(countCols > 0) //exists any column
{
colPos = (clickPoint->y - queryTable->position.y) / (rowHeight + rowTopMargin);
}
int x = clickPoint->x - queryTable->position.x;
if( (x > 0 && x < sensibility) && colPos > 0)
// Because 0 is title
return queryTable->parent->getColumnAtIndex(colPos - 1);
else
return NULL;
}
void gqbGraphSimple::drawTempJoinLine(wxMemoryDC &bdc, wxPoint &origin, wxPoint &end)
{
wxPoint anchorsUsed = wxPoint(0, 0);
if(origin.x < end.x)
{
anchorsUsed.x = 1;
anchorsUsed.y = -1;
}
else
{
anchorsUsed.x = -1;
anchorsUsed.y = 1;
}
drawJoin(bdc, origin, end, anchorsUsed, true, _equally);
}
void gqbGraphSimple::drawJoin(wxMemoryDC &bdc, wxPoint &origin, wxPoint &dest, wxPoint &anchorUsed, bool selected = false, type_Join joinKind = _equally)
{
wxPoint origin2 = origin;
wxPoint dest2 = dest;
if(selected)
{
bdc.SetPen(selectedPen);
bdc.SetBrush(selectedBrush);
}
else
{
bdc.SetPen(*wxBLACK_PEN);
bdc.SetBrush(*wxBLACK_BRUSH);
}
// GQB-TODO: optimize this if possible, I know one other can be the same?
// getAnchorsUsed() [-1==left] [1==right] x->origin y->destination
if(anchorUsed.x == 1)
{
bdc.DrawRectangle(origin.x, origin.y - 4, 8, 8);
origin2.x += 20;
}
else
{
bdc.DrawRectangle(origin.x - 8, origin.y - 4, 8, 8);
origin2.x -= 20;
}
if(anchorUsed.y == 1)
{
bdc.DrawRectangle(dest.x, dest.y - 4, 8, 8);
dest2.x += 20;
}
else
{
bdc.DrawRectangle(dest.x - 8, dest.y - 4, 8, 8);
dest2.x -= 20;
}
bdc.DrawLine(origin, origin2);
bdc.DrawLine(dest, dest2);
bdc.DrawLine(origin2, dest2);
// Draw type of join
switch(joinKind)
{
case _equally:
bdc.DrawText(wxT("="), findLineMiddle(origin2, dest2));
break;
case _lesser:
bdc.DrawText(wxT("<"), findLineMiddle(origin2, dest2));
break;
case _greater:
bdc.DrawText(wxT(">"), findLineMiddle(origin2, dest2));
break;
case _equlesser:
bdc.DrawText(wxT("<="), findLineMiddle(origin2, dest2));
break;
case _equgreater:
bdc.DrawText(wxT(">="), findLineMiddle(origin2, dest2));
break;
};
}
// Return true if pt click over a threshold of the join, false if not
bool gqbGraphSimple::clickOnJoin(gqbQueryJoin *join, wxPoint &pt, wxPoint &origin, wxPoint &dest)
{
wxPoint origin2 = origin;
wxPoint dest2 = dest;
if(join->getAnchorsUsed().x == 1)
{
origin2.x += 20;
}
else
{
origin2.x -= 20;
}
if(join->getAnchorsUsed().y == 1)
{
dest2.x += 20;
}
else
{
dest2.x -= 20;
}
// Check origin anchor
bool value1 = insideLine(pt, origin, origin2, lineClickThreshold);
// Check dest anchor
bool value2 = insideLine(pt, dest, dest2, lineClickThreshold);
// Check line between both tables
bool value3 = insideLine(pt, origin2, dest2, lineClickThreshold);
if(value1 || value2 || value3)
return true;
else
return false;
}
bool gqbGraphSimple::insideLine(wxPoint &pt, wxPoint &p1, wxPoint &p2, int threshold = 7)
{
bool value = false;
if(distanceToLine(pt, p1, p2) < threshold)
{
value = true;
}
return value;
}
wxPoint gqbGraphSimple::findLineMiddle(wxPoint p1, wxPoint p2)
{
int middleX = -1, middleY = -1;
int dx = p2.x - p1.x;
if(dx > 0) // p1 at left
{
middleX = dx / 2 + p1.x;
} // p1 at right
else
{
middleX = p1.x + dx / 2;
}
int dy = p2.y - p1.y;
if(dy > 0) // p1 is above
{
middleY = dy / 2 + p1.y;
} // p1 is below
else
{
middleY = p1.y + dy / 2;
}
if(dy == 0)
middleY = p1.y;
if(dx == 0)
middleX = p1.x;
return wxPoint(middleX, middleY);
}
double gqbGraphSimple::distanceToLine(wxPoint pt, wxPoint p1, wxPoint p2)
{
p2.x -= p1.x;
p2.y -= p1.y;
pt.x -= p1.x;
pt.y -= p1.y;
double dprod = pt.x * p2.x + pt.y * p2.y;
double pLenSq;
if(dprod <= 0.0)
{
pLenSq = 0.0;
}
else
{
pt.x = p2.x - pt.x;
pt.y = p2.y - pt.y;
dprod = pt.x * p2.x + pt.y * p2.y;
if(dprod <= 0.0)
{
pLenSq = 0.0;
}
else
{
pLenSq = dprod * dprod / (p2.x * p2.x + p2.y * p2.y);
}
}
double lengthSq = pt.x * pt.x + pt.y * pt.y - pLenSq;
if(lengthSq < 0)
{
lengthSq = 0;
}
double length = sqrt(lengthSq);
return length;
}
// Set the anchors points [source, destination] for a join
void gqbGraphSimple::calcAnchorPoint(gqbQueryJoin *join)
{
int index, x, y;
wxPoint use; // [-1==left] [1==right] x->origin y->destination
int sx = join->getSourceQTable()->position.x;
int sy = join->getSourceQTable()->position.y;
int dx = join->getDestQTable()->position.x;
int dy = join->getDestQTable()->position.y;
// Source
index = join->getSourceQTable()->getColumnIndex(join->getSCol()) + 1;
if(sx < dx)
{
x = sx + join->getSourceQTable()->getWidth();
use.x = 1;
}
else
{
x = sx;
use.x = -1;
}
y = sy + index * (rowHeight + rowTopMargin) + ((rowHeight + rowTopMargin) / 2);
join->setSourceAnchor(wxPoint(x, y));
// Destination
index = join->getDestQTable()->getColumnIndex(join->getDCol()) + 1;
if(dx < sx)
{
x = dx + join->getDestQTable()->getWidth();
use.y = 1;
}
else
{
x = dx;
use.y = -1;
}
y = dy + index * (rowHeight + rowTopMargin) + ((rowHeight + rowTopMargin) / 2);
join->setDestAnchor(wxPoint(x, y));
join->setAnchorsUsed(use);
}
// Update position of Object in the query if move table & adjust all other items like joins (own & registered)
void gqbGraphSimple::UpdatePosObject(gqbQueryObject *queryTable, int x, int y, int cursorAdjustment)
{
x -= cursorAdjustment; // Move Pointer to a better Position;
y -= rowHeight / 2;
// Update position of table
// Do not allow table/view moved/repositioned less than (0, 0) cordinates
queryTable->position.x = x > 0 ? x : 0;
queryTable->position.y = y > 0 ? y : 0;
// Update position of anchor points of Joins that origin from this table
if(queryTable->getHaveJoins())
{
gqbIteratorBase *j = queryTable->createJoinsIterator();
while(j->HasNext())
{
gqbQueryJoin *tmp = (gqbQueryJoin *)j->Next();
calcAnchorPoint(tmp);
}
delete j;
}
// Update position of anchor points of Joins that come from others tables
if(queryTable->getHaveRegJoins())
{
gqbIteratorBase *r = queryTable->createRegJoinsIterator();
while(r->HasNext())
{
gqbQueryJoin *tmp = (gqbQueryJoin *)r->Next();
calcAnchorPoint(tmp);
}
delete r;
}
}
int gqbGraphSimple::getTitleRowHeight()
{
return rowHeight;
}
|