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 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
|
/*
* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
*
* 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 2 of the
* License.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "wb_model_diagram_form.h"
#include "workbench/wb_context.h"
#include "wb_component.h"
#include "wb_component_basic.h"
#include "canvas_floater.h"
#include "mdc_back_layer.h"
#include "mforms/form.h"
#include "model/wb_context_model.h"
#include "model/wb_layer_tree.h"
#include "grt/common.h"
#include "grt/clipboard.h"
#include "wbcanvas/model_figure_impl.h"
#include "wbcanvas/model_layer_impl.h"
#include "wbcanvas/model_diagram_impl.h"
#include "wbcanvas/model_connection_impl.h"
#include "workbench/wb_context_ui.h"
#include "wb_physical_model_diagram_features.h"
#include "base/string_utilities.h"
#include "base/log.h"
#include <boost/lambda/bind.hpp>
#include "mforms/menubar.h"
#define DEFAULT_TOOL "basic/select"
DEFAULT_LOG_DOMAIN("ModelDiagram");
using namespace wb;
using namespace bec;
using namespace base;
static const double zoom_steps[]= {
2.0,
1.5,
1.2,
1.0,
0.95,
0.9,
0.85,
0.8,
0.7,
0.6,
0.5,
0.4,
0.3,
0.2,
0.1
};
ModelDiagramForm::ModelDiagramForm(WBComponent *owner, const model_DiagramRef &view)
: _catalog_tree(NULL), _view(NULL), _owner(owner), _model_diagram(view), _mini_view(NULL), _menu(NULL),
_toolbar(NULL), _tools_toolbar(NULL), _options_toolbar(NULL)
{
_drag_panning= false;
_space_panning= false;
_highlight_fks = false;
_inline_edit_context= 0;
_update_count = 0;
_layer_tree = 0;
// scoped_connect(_model_diagram->signal_refreshDisplay(), boost::bind(&ModelDiagramForm::diagram_changed, this, _1));
scoped_connect(_model_diagram->signal_list_changed(), boost::bind(&ModelDiagramForm::diagram_changed, this, _1, _2, _3));
_current_mouse_x= -1;
_current_mouse_y= -1;
_main_layer= 0;
_floater_layer= 0;
_badge_layer= 0;
_tool= DEFAULT_TOOL;
_paste_offset= 0;
_shortcuts= owner->get_wb()->get_ui()->get_command_ui()->get_shortcuts_for_context(WB_CONTEXT_MODEL);
scoped_connect(owner->get_wb()->get_clipboard()->signal_changed(), boost::bind(&ModelDiagramForm::clipboard_changed, this));
_features= new PhysicalModelDiagramFeatures(this);
_options_toolbar = new mforms::ToolBar(mforms::OptionsToolBar);
NotificationCenter::get()->add_observer(this, "GNColorsChanged");
NotificationCenter::get()->add_observer(this, "GNMainFormChanged");
}
//--------------------------------------------------------------------------------------------------
ModelDiagramForm::~ModelDiagramForm()
{
NotificationCenter::get()->remove_observer(this);
_idle_node_mark.disconnect();
delete _layer_tree;
delete _options_toolbar;
delete _toolbar;
delete _tools_toolbar;
delete _menu;
delete _mini_view;
delete _features;
}
//--------------------------------------------------------------------------------------------------
void ModelDiagramForm::handle_notification(const std::string &name, void *sender, base::NotificationInfo &info)
{
if (name == "GNColorsChanged")
{
// Single colors or the entire color scheme changed.
update_toolbar_icons();
}
// else if(name == "GNMainFormChanged" && _catalog_tree != NULL)
// {
// _catalog_tree->refill();
// }
}
//--------------------------------------------------------------------------------------------------
std::string ModelDiagramForm::get_form_context_name() const
{
return WB_CONTEXT_MODEL;
}
//--------------------------------------------------------------------------------------------------
mforms::ToolBar *ModelDiagramForm::get_toolbar()
{
if (!_toolbar)
{
_toolbar = get_wb()->get_ui()->get_command_ui()->create_toolbar("data/model_diagram_toolbar.xml");
update_toolbar_icons();
}
return _toolbar;
}
//--------------------------------------------------------------------------------------------------
// Implemented in wb_sql_editor_form_ui.cpp.
extern std::string find_icon_name(std::string icon_name, bool use_win8);
void ModelDiagramForm::update_toolbar_icons()
{
if (_toolbar == NULL)
return; // Can happen if the diagram hasn't shown yet.
bool use_win8;
switch (base::Color::get_active_scheme())
{
case base::ColorSchemeStandardWin8:
case base::ColorSchemeStandardWin8Alternate:
use_win8 = true;
break;
default:
use_win8 = false;
}
mforms::ToolBarItem *item = _toolbar->find_item("wb.toggleSidebar");
if (item != NULL)
{
item->set_icon(find_icon_name(item->get_icon(), use_win8));
item->set_alt_icon(find_icon_name(item->get_alt_icon(), use_win8));
}
item = _toolbar->find_item("wb.toggleSecondarySidebar");
if (item != NULL)
{
item->set_icon(find_icon_name(item->get_icon(), use_win8));
item->set_alt_icon(find_icon_name(item->get_alt_icon(), use_win8));
}
}
//--------------------------------------------------------------------------------------------------
mforms::TreeNodeView *ModelDiagramForm::get_layer_tree()
{
if (!_layer_tree)
{
_layer_tree = new LayerTree(get_wb()->get_ui(), this, _model_diagram);
_layer_tree->refresh();
}
return _layer_tree;
}
//--------------------------------------------------------------------------------------------------
mforms::ToolBar *ModelDiagramForm::get_tools_toolbar()
{
if (!_tools_toolbar)
{
_tools_toolbar = new mforms::ToolBar(mforms::ToolPickerToolBar);
app_ToolbarRef toolbar[3];
toolbar[0]= app_ToolbarRef::cast_from(get_wb()->get_grt()->unserialize(make_path(get_wb()->get_datadir(),
"data/tools_toolbar.xml")));
toolbar[1]= get_wb()->get_component_named("basic")->get_tools_toolbar();
toolbar[2]= get_wb()->get_component_named("physical")->get_tools_toolbar();
for (int t= 0; t < 3; t++)
{
for (size_t c = toolbar[t]->items().count(), i = 0; i < c; i++)
{
app_ToolbarItemRef titem(toolbar[t]->items()[i]);
mforms::ToolBarItem *item;
if (titem->itemType() == "separator")
item = mforms::manage(new mforms::ToolBarItem(mforms::SeparatorItem));
else
{
item = mforms::manage(new mforms::ToolBarItem(mforms::ToggleItem));
item->set_icon(IconManager::get_instance()->get_icon_path(*titem->icon()));
item->set_name((*titem->command()).substr(strlen("tool:")));
scoped_connect(item->signal_activated(),boost::bind(&ModelDiagramForm::set_tool, this, item->get_name()));
}
std::string shortcut;
for (std::vector<WBShortcut>::const_iterator iter= _shortcuts.begin(); iter != _shortcuts.end(); ++iter)
{
if (iter->command == *titem->command())
{
shortcut= iter->shortcut;
break;
}
}
if (shortcut.empty())
item->set_tooltip(titem->tooltip());
else
item->set_tooltip(strfmt("%s (Quick key - press %s)", titem->tooltip().c_str(), shortcut.c_str()));
_tools_toolbar->add_item(item);
if (item->get_name() == DEFAULT_TOOL)
item->set_checked(true);
}
toolbar[t]->reset_references();
}
}
return _tools_toolbar;
}
mforms::ToolBar *ModelDiagramForm::get_options_toolbar()
{
update_options_toolbar();
return _options_toolbar;
}
mforms::MenuBar *ModelDiagramForm::get_menubar()
{
if (!_menu)
{
_menu = _owner->get_wb()->get_ui()->get_command_ui()->create_menubar_for_context(WB_CONTEXT_MODEL);
scoped_connect(_menu->signal_will_show(),boost::bind(&ModelDiagramForm::revalidate_menu, this));
mforms::MenuItem *item = _menu->find_item("wb.edit.editSelectedFigure");
if (item)
item->set_validator(boost::bind(&ModelDiagramForm::has_selection, this));
item = _menu->find_item("wb.edit.editSelectedFigureInNewWindow");
if (item)
item->set_validator(boost::bind(&ModelDiagramForm::has_selection, this));
}
revalidate_menu();
return _menu;
}
void ModelDiagramForm::revalidate_menu()
{
static const char *figure_notations[] = {
"workbench/default",
"workbench/simple",
"workbench/pkonly",
"classic",
"idef1x",
NULL
};
static const char *relationship_notations[] = {
"crowsfoot",
"classic",
"fromcolumn",
"uml",
"idef1x",
NULL
};
if (_menu)
{
bool has_selection_ = has_selection();
_menu->set_item_enabled("wb.edit.goToNextSelected", has_selection_);
_menu->set_item_enabled("wb.edit.goToPreviousSelected", has_selection_);
_menu->set_item_enabled("wb.edit.selectSimilar", has_selection_);
_menu->set_item_enabled("wb.edit.selectConnected", get_selection().count() == 1);
_menu->set_item_checked("wb.edit.toggleGridAlign", _owner->get_grt_manager()->get_app_option_int("AlignToGrid", 0) != 0);
_menu->set_item_checked("wb.edit.toggleGrid", get_diagram_options().get_int("ShowGrid", 1) != 0);
_menu->set_item_checked("wb.edit.togglePageGrid", get_diagram_options().get_int("ShowPageGrid", 1) != 0);
_menu->set_item_checked("wb.edit.toggleFKHighlight", get_diagram_options().get_int("ShowFKHighlight", 0) != 0);
std::string notation = workbench_physical_ModelRef::cast_from(get_model_diagram()->owner())->figureNotation();
for (int i= 0; figure_notations[i]; i++)
_menu->set_item_checked(strfmt("wb.view.setFigureNotation:%s", figure_notations[i]), notation == figure_notations[i]);
notation = workbench_physical_ModelRef::cast_from(get_model_diagram()->owner())->connectionNotation();
for (int i= 0; relationship_notations[i]; i++)
_menu->set_item_checked(strfmt("wb.view.setRelationshipNotation:%s", relationship_notations[i]), notation == relationship_notations[i]);
model_ModelRef model(get_model_diagram()->owner());
for (int i= 1; i < 10; i++)
{
_menu->set_item_checked(strfmt("wb.view.setMarker:%i", i), false);
_menu->set_item_enabled(strfmt("wb.view.goToMarker:%i", i), false);
}
for (size_t c= model->markers().count(), i= 0; i < c; i++)
{
_menu->set_item_checked(strfmt("wb.view.setMarker:%s", model->markers().get(i)->name().c_str()), true);
_menu->set_item_enabled(strfmt("wb.view.goToMarker:%s", model->markers().get(i)->name().c_str()), true);
}
_menu->find_item("plugins")->validate();
}
}
//--------------------------------------------------------------------------------------------------
void ModelDiagramForm::update_options_toolbar()
{
app_ToolbarRef toolbar = get_wb()->get_component_named("basic")->get_tool_options(get_tool());
_options_toolbar->remove_all();
if (!toolbar.is_valid())
toolbar = get_wb()->get_component_named("physical")->get_tool_options(get_tool());
if (!toolbar.is_valid())
return;
for (size_t c= toolbar->items().count(), i= 0; i < c; i++)
{
app_ToolbarItemRef titem(toolbar->items()[i]);
mforms::ToolBarItem *item;
std::string type = titem->itemType();
if (type == "label")
{
item = mforms::manage(new mforms::ToolBarItem(mforms::LabelItem));
item->set_text(*titem->command());
}
else if (type == "dropdown")
{
std::string selected;
std::vector<std::string> items(get_dropdown_items(titem->name(), titem->command(), selected));
if (!items.empty() && !items.front().empty() && items.front()[0] == '#')
item = mforms::manage(new mforms::ToolBarItem(mforms::ColorSelectorItem));
else
item = mforms::manage(new mforms::ToolBarItem(mforms::SelectorItem));
item->set_selector_items(items);
item->set_text(selected);
scoped_connect(item->signal_activated(),boost::bind(&ModelDiagramForm::select_dropdown_item, this, titem->command(), item));
}
else if (type == "separator")
item = mforms::manage(new mforms::ToolBarItem(mforms::SeparatorItem));
else if (type == "action")
{
item = mforms::manage(new mforms::ToolBarItem(mforms::ActionItem));
}
else if (type == "check")
{
item = mforms::manage(new mforms::ToolBarItem(mforms::ToggleItem));
// icon text is used as label for check items
item->set_text(IconManager::get_instance()->get_icon_path(*titem->icon()));
}
else if (type == "toggle")
{
item = mforms::manage(new mforms::ToolBarItem(mforms::ToggleItem));
}
else
continue;
if (!titem->icon().empty())
item->set_icon(IconManager::get_instance()->get_icon_path(*titem->icon()));
if (!titem->altIcon().empty())
item->set_alt_icon(IconManager::get_instance()->get_icon_path(*titem->altIcon()));
item->set_name(titem->name());
item->set_tooltip(titem->tooltip());
_options_toolbar->add_item(item);
}
// don't reset refs here, only do it when closing, since the toolbar object will be used later
//toolbar->reset_references();
}
std::vector<std::string> ModelDiagramForm::get_dropdown_items(const std::string &name, const std::string &option, std::string &selected)
{
std::vector<std::string> items;
WBComponent *compo;
compo= get_wb()->get_component_named(base::split(name, "/")[0]);
if (compo)
{
std::string::size_type p = option.find(':');
if (p != std::string::npos)
{
std::string option_name = option.substr(p+1);
items= compo->get_command_dropdown_items(option_name);
selected= compo->get_command_option_value(option_name);
}
}
return items;
}
void ModelDiagramForm::select_dropdown_item(const std::string &option, mforms::ToolBarItem *item)
{
WBComponent *compo;
compo= get_wb()->get_component_named(base::split(item->get_name(), "/")[0]);
if (compo)
{
std::string::size_type p = option.find(':');
if (p != std::string::npos)
{
std::string option_name = option.substr(p+1);
compo->set_command_option_value(option_name, item->get_text());
}
}
}
void ModelDiagramForm::toggle_checkbox_item(const std::string &name, const std::string &option, bool state)
{
WBComponent *compo;
compo= get_wb()->get_component_named(base::split(name, "/")[0]);
if (compo)
{
std::string::size_type p = option.find(':');
if (p != std::string::npos)
{
std::string option_name = option.substr(p+1);
compo->set_command_option_value(option, state?"1":"0");
}
}
}
void ModelDiagramForm::activate_catalog_tree_item(const grt::ValueRef &value)
{
if (value.is_valid() && db_DatabaseObjectRef::can_wrap(value))
{
db_DatabaseObjectRef object(db_DatabaseObjectRef::cast_from(value));
_owner->get_grt_manager()->open_object_editor(object);
}
}
void ModelDiagramForm::selection_changed()
{
get_wb()->request_refresh(RefreshSelection, "", 0);
if (get_wb()->get_grt_manager()->in_main_thread())
revalidate_menu();
else
get_wb()->get_grt_manager()->run_once_when_idle(boost::bind(&ModelDiagramForm::revalidate_menu, this));
}
//--------------------------------------------------------------------------------------------------
void ModelDiagramForm::diagram_changed(grt::internal::OwnedList* olist, bool added, const grt::ValueRef& val)
{
_idle_node_mark.disconnect();
if (added)
_idle_node_mark = get_wb()->get_grt_manager()->run_once_when_idle(boost::bind(&ModelDiagramForm::mark_catalog_node, this, val, true));
}
void ModelDiagramForm::mark_catalog_node(grt::ValueRef val, bool mark)
{
if (model_ObjectRef::can_wrap(val))
{
model_ObjectRef f(model_ObjectRef::cast_from(val));
if (f.is_valid())
_catalog_tree->mark_node(_owner->get_object_for_figure(f), mark);
}
}
void ModelDiagramForm::attach_canvas_view(mdc::CanvasView *cview)
{
_view= cview;
cview->set_tag(_model_diagram.id());
cview->set_grid_snapping(_owner->get_grt_manager()->get_app_option_int("AlignToGrid", 0) != 0);
cview->get_background_layer()->set_grid_visible(_model_diagram->options().get_int("ShowGrid", 1)!=0);
cview->get_background_layer()->set_paper_visible(_model_diagram->options().get_int("ShowPageGrid", 1)!=0);
scoped_connect(cview->get_selection()->signal_begin_dragging(),boost::bind(&ModelDiagramForm::begin_selection_drag, this));
scoped_connect(cview->get_selection()->signal_end_dragging(),boost::bind(&ModelDiagramForm::end_selection_drag, this));
scoped_connect(_model_diagram->get_data()->signal_selection_changed(),boost::bind(&ModelDiagramForm::selection_changed, this));
_main_layer= _view->get_current_layer();
_badge_layer= _view->new_layer("badges");
_floater_layer= _view->new_layer("floater");
// force updates
selection_changed();
}
void ModelDiagramForm::close()
{
set_closed(true);
_mini_view->set_active_view(NULL, model_DiagramRef());
delete _mini_view;
_mini_view = 0;
_model_diagram->get_data()->unrealize();
}
CatalogTreeView* ModelDiagramForm::get_catalog_tree()
{
if (_catalog_tree == NULL)
{
_catalog_tree = new CatalogTreeView(this);
_catalog_tree->set_activate_callback(boost::bind(&ModelDiagramForm::activate_catalog_tree_item, this, _1));
}
return _catalog_tree;
}
void ModelDiagramForm::notify_catalog_tree(const CatalogNodeNotificationType ¬ify_type, grt::ValueRef value)
{
_idle_node_mark.disconnect(); //if there is pending mark_node, disable it
if (_catalog_tree)
{
switch(notify_type)
{
case wb::NodeUnmark:
_catalog_tree->mark_node(value, false);
break;
case wb::NodeAddUpdate:
_catalog_tree->add_update_node_caption(value);
break;
case wb::NodeDelete:
_catalog_tree->remove_node(value);
}
}
}
void ModelDiagramForm::refill_catalog_tree()
{
if (!_catalog_tree)
{
_catalog_tree = new CatalogTreeView(this);
_catalog_tree->set_activate_callback(boost::bind(&ModelDiagramForm::activate_catalog_tree_item, this, _1));
}
_catalog_tree->refill(true);
}
void ModelDiagramForm::set_closed(bool flag)
{
if (_model_diagram.is_valid())
_model_diagram->closed(flag!=0);
}
bool ModelDiagramForm::is_closed()
{
return *_model_diagram->closed() != 0;
}
void ModelDiagramForm::set_button_callback(const boost::function<bool (ModelDiagramForm*, mdc::MouseButton, bool, Point, mdc::EventState)> &cb)
{
_handle_button= cb;
}
void ModelDiagramForm::set_motion_callback(const boost::function<bool (ModelDiagramForm*, Point, mdc::EventState)> &cb)
{
_handle_motion= cb;
}
void ModelDiagramForm::set_reset_tool_callback(const boost::function<void (ModelDiagramForm*)> &cb)
{
_reset_tool= cb;
}
//--------------------------------------------------------------------------------------------------
/**
* Enables or disables zooming via shortcut and mouse click (similar to Adobe Photoshop).
*
* @param enable If true the tool is enabled otherwise the previous tool is restored.
* @param zoomin If true mouse clicks will zoom into the diagram otherwise zoom out.
*/
void ModelDiagramForm::enable_zoom_click(bool enable, bool zoomin)
{
if (!enable)
{
_reset_tool(this);
_tool= _old_tool;
_cursor= _old_cursor;
_reset_tool= _old_reset_tool;
_handle_button= _old_handle_button;
_handle_motion= _old_handle_motion;
set_tool(_tool);
return;
}
_old_tool= _tool;
if (zoomin)
_tool= WB_TOOL_ZOOM_IN;
else
_tool= WB_TOOL_ZOOM_OUT;
_old_reset_tool= _reset_tool;
_old_handle_button= _handle_button;
_old_handle_motion= _handle_motion;
_old_cursor= _cursor;
WBComponent *compo= _owner->get_wb()->get_component_named("basic");
compo->setup_canvas_tool(this, _tool);
set_tool(_tool);
}
//--------------------------------------------------------------------------------------------------
void ModelDiagramForm::enable_panning(bool flag)
{
if (flag)
{
_old_tool= _tool;
_old_reset_tool= _reset_tool;
_old_handle_button= _handle_button;
_old_handle_motion= _handle_motion;
_old_cursor= _cursor;
_tool= WB_TOOL_HAND;
WBComponent *compo= _owner->get_wb()->get_component_named("basic");
compo->setup_canvas_tool(this, _tool);
set_tool(_tool);
}
else
{
_reset_tool(this);
_tool= _old_tool;
_cursor= _old_cursor;
_reset_tool= _old_reset_tool;
_handle_button= _old_handle_button;
_handle_motion= _old_handle_motion;
set_tool(_tool);
}
}
void ModelDiagramForm::begin_selection_drag()
{
mdc::Selection::ContentType selection(_view->get_selection()->get_contents());
mdc::AreaGroup *root= _view->get_current_layer()->get_root_area_group();
// save current position of everything before dragging around objects
// so that we can store that for undo
{
_old_positions.clear();
grt::ListRef<model_Object> sel(_model_diagram->selection());
for (size_t c= sel.count(), i= 0; i < c; i++)
{
if (sel[i]->is_instance(model_Figure::static_class_name()))
{
model_FigureRef figure(model_FigureRef::cast_from(sel[i]));
_old_positions[figure.valueptr()].pos= Point(figure->left(), figure->top());
_old_positions[figure.valueptr()].layer_id= figure->layer().id();
}
else if (sel[i]->is_instance(model_Layer::static_class_name()))
{
model_LayerRef layer(model_LayerRef::cast_from(sel[i]));
_old_positions[layer.valueptr()].pos= Point(layer->left(), layer->top());
}
}
}
// remove objects from respective layers so they can be freely dragged
for (mdc::Selection::ContentType::const_iterator iter= selection.begin();
iter != selection.end(); ++iter)
{
// if the layer of the figure is also selected, then leave the figure alone
Point rpos= (*iter)->get_root_position();
if (root != (*iter)->get_parent() && !(*iter)->get_parent()->get_selected())
{
root->add(*iter);
(*iter)->move_to(rpos);
}
}
}
void ModelDiagramForm::end_selection_drag()
{
std::string name;
bool moved= false;
int count= 0;
grt::UndoManager *um= _owner->get_grt()->get_undo_manager();
um->begin_undo_group();
// save original position of objects into undo record
grt::ListRef<model_Object> sel(_model_diagram->selection());
for (size_t c= sel.count(), i= 0; i < c; i++)
{
if (sel[i]->is_instance(model_Figure::static_class_name()))
{
model_FigureRef figure(model_FigureRef::cast_from(sel[i]));
{
Point p= _old_positions[figure.valueptr()].pos;
std::string layer_id= _old_positions[figure.valueptr()].layer_id;
if (*figure->left() != p.x || *figure->top() != p.y || layer_id != figure->layer().id())
{
moved= true;
um->add_undo(new grt::UndoObjectChangeAction(figure, "left", grt::DoubleRef(p.x)));
um->add_undo(new grt::UndoObjectChangeAction(figure, "top", grt::DoubleRef(p.y)));
}
}
name= figure->name();
count++;
}
else if (sel[i]->is_instance(model_Layer::static_class_name()))
{
model_LayerRef layer(model_LayerRef::cast_from(sel[i]));
{
Point p= _old_positions[layer.valueptr()].pos;
if (*layer->left() != p.x || *layer->top() != p.y)
{
moved= true;
um->add_undo(new grt::UndoObjectChangeAction(layer, "left", grt::DoubleRef(p.x)));
um->add_undo(new grt::UndoObjectChangeAction(layer, "top", grt::DoubleRef(p.y)));
}
}
name= layer->name();
count++;
}
}
// change the layer of moved stuff
{
grt::AutoUndo undo(_owner->get_grt());
if (relocate_figures())
{
moved= true;
undo.end("Update Object Layers");
}
else
undo.cancel();
}
if (!moved)
{
um->cancel_undo_group();
return;
}
if (!name.empty() && count == 1)
um->end_undo_group(strfmt(_("Move %s"), name.c_str()));
else
um->end_undo_group(_("Move Objects"));
}
bool ModelDiagramForm::current_mouse_position(int &x, int &y)
{
int w, h;
_view->get_view_size(w, h);
x= _current_mouse_x;
y= _current_mouse_y;
if (x < 0 || y < 0 || x >= w || y >= h)
return false;
return true;
}
bool ModelDiagramForm::current_mouse_position(Point &pos)
{
int x, y;
bool inside= current_mouse_position(x, y);
pos= _view->window_to_canvas(x, y);
return inside;
}
mdc::CanvasItem *ModelDiagramForm::get_leaf_item_at(const Point &pos)
{
return _view->get_leaf_item_at(pos);
}
//--------------------------------------------------------------------------------------------------
void ModelDiagramForm::handle_mouse_button(mdc::MouseButton button, bool press, int x, int y, mdc::EventState state)
{
if (_features)
_features->tooltip_cancel();
stop_editing();
Point pos(_view->window_to_canvas(x, y));
if (button == mdc::ButtonRight && !press)
{
model_ObjectRef object= get_object_at(pos);
bec::MenuItem item;
bec::MenuItemList items;
// If the user clicks in a selected object or in the view just show popup.
// If user clicks in an unselected object, select it and show popup.
if (object.is_valid() && _model_diagram->selection().get_index(object) == grt::BaseListRef::npos)
_view->get_selection()->set(_view->get_item_at(pos));
{
std::list<std::string> groups;
groups.push_back("Catalog/*");
groups.push_back("Model/*");
get_wb()->get_model_context()->get_object_list_popup_items(this, std::vector<bec::NodeId>(),
grt::ListRef<GrtObject>::cast_from(_model_diagram->selection()),
get_edit_target_name(), groups, items);
}
//else
//_owner->get_model_popup_items(items);
if (!items.empty())
{
int x, y;
_view->canvas_to_window(pos, x, y);
_context_menu.clear();
_context_menu.add_items_from_list(items);
_context_menu.set_handler(boost::bind(&CommandUI::activate_command, get_wb()->get_ui()->get_command_ui(), _1));
_context_menu.popup_at(NULL, x, y);
}
return;
}
// temporarily select Hand tool when middle-button-dragging
if (button == mdc::ButtonMiddle)
{
if (press && !_drag_panning && !_space_panning)
{
_drag_panning= true;
enable_panning(true);
}
else if (!press && _drag_panning)
{
_drag_panning= false;
enable_panning(false);
}
}
if (press && button == mdc::ButtonLeft)
{
// Handle zoom clicks.
if (_tool == WB_TOOL_ZOOM_IN)
{
zoom_in();
return;
}
else
if (_tool == WB_TOOL_ZOOM_OUT)
{
zoom_out();
return;
}
}
if (button != mdc::ButtonLeft && button != mdc::ButtonMiddle)
return;
if (_handle_button && _handle_button(this, button, press, pos, state))
return;
_view->handle_mouse_button(button, press, x, y, state);
}
//--------------------------------------------------------------------------------------------------
void ModelDiagramForm::handle_mouse_double_click(mdc::MouseButton button, int x, int y, mdc::EventState state)
{
stop_editing();
if (button != mdc::ButtonLeft && button != mdc::ButtonMiddle)
return;
_view->handle_mouse_double_click(button, x, y, state);
}
//--------------------------------------------------------------------------------------------------
void ModelDiagramForm::handle_mouse_move(int x, int y, mdc::EventState state)
{
Point pos(_view->window_to_canvas(x, y));
//_features->mouse_moved(x, y, state);
_current_mouse_x= x;
_current_mouse_y= y;
if (_handle_motion && _handle_motion(this, pos, state))
return;
_view->handle_mouse_move(x, y, state);
}
void ModelDiagramForm::handle_mouse_leave(int x, int y, mdc::EventState state)
{
_view->handle_mouse_leave(x, y, state);
}
bool ModelDiagramForm::handle_key(const mdc::KeyInfo &key, bool press, mdc::EventState state)
{
if (press)
{
// cancel tooltip on keypress
if (_features)
_features->tooltip_cancel();
for (std::vector<WBShortcut>::const_iterator iter= _shortcuts.begin();
iter != _shortcuts.end(); ++iter)
{
if (iter->modifiers == state && iter->key == key)
{
if (iter->command.find("tool:") == 0)
set_tool(iter->command.substr(strlen("tool:")));
else if (iter->command == "zoomin")
zoom_in();
else if (iter->command == "zoomout")
zoom_out();
else if (iter->command == "zoomdefault")
set_zoom(1);
else
get_wb()->get_ui()->get_command_ui()->activate_command(iter->command);
return true;
}
}
if (key.keycode == mdc::KSpace)
{
if (state == 0)
{
if (!_drag_panning && !_space_panning)
{
_space_panning= true;
enable_panning(true);
}
}
else
if (_tool != WB_TOOL_ZOOM_IN && _tool != WB_TOOL_ZOOM_OUT)
{
if (state == mdc::SControlMask)
{
enable_zoom_click(true, true);
return true;
}
else
if (state == mdc::SAltMask)
{
enable_zoom_click(true, false);
return true;
}
}
else
return true; // Still return true to tell the caller we consumed the input
// (even though only the first appearance, but if we don't it is as if
// we hadn't consumed it at all).
}
}
else
{
// Key release.
if (_space_panning)
{
_space_panning= false;
enable_panning(false);
}
if (_tool == WB_TOOL_ZOOM_IN || _tool == WB_TOOL_ZOOM_OUT)
enable_zoom_click(false, false);
}
return _view->handle_key(key, press, state);
}
//--------------------------------------------------------------------------------------------------
void ModelDiagramForm::set_tool(std::string tool)
{
if (_tool != DEFAULT_TOOL)
reset_tool(false);
_tool= tool;
WBComponent *compo= _owner->get_wb()->get_component_named(base::split(tool, "/")[0]);
if (compo)
compo->setup_canvas_tool(this, tool);
else
throw std::runtime_error("Invalid tool "+tool);
std::vector<mforms::ToolBarItem*> items = _tools_toolbar->get_items();
for (std::vector<mforms::ToolBarItem*>::iterator item = items.begin(); item != items.end(); ++item)
{
if ((*item)->get_type() == mforms::ToggleItem)
{
if ((*item)->get_name() == _tool)
(*item)->set_checked(true);
else
(*item)->set_checked(false);
}
}
if (_owner->get_wb()->tool_changed)
_owner->get_wb()->tool_changed(_view);
}
void ModelDiagramForm::reset_tool(bool notify)
{
// _tool_args.clear();
if (_tools_toolbar)
{
mforms::ToolBarItem *item = _tools_toolbar->find_item(_tool);
if (!_tool.empty() && item)
item->set_checked(false);
item = _tools_toolbar->find_item(DEFAULT_TOOL);
if (item)
item->set_checked(true);
}
_tool= DEFAULT_TOOL;
if (_reset_tool)
_reset_tool(this);
_cursor= "";
boost::function<bool ()> f = boost::lambda::constant(false);
_handle_button = (boost::bind(f));
_handle_motion = (boost::bind(f));
_reset_tool = (boost::bind(f));
if (notify && _owner->get_wb()->tool_changed)
_owner->get_wb()->tool_changed(_view);
}
std::string ModelDiagramForm::get_tool_argument(const std::string &option)
{
return _tool_args[option];
}
/**
****************************************************************************
* @brief Sets options for the selected tool
*
* Change the value of an option for the currently selected tool.
*
* @param option name of the option
* @param value value of the option
*****************************************************************************/
void ModelDiagramForm::set_tool_argument(const std::string &option, const std::string &value)
{
_tool_args[option]= value;
_tool_argument_changed(option);
}
void ModelDiagramForm::set_zoom(double zoom)
{
_model_diagram->zoom(zoom);
}
double ModelDiagramForm::get_zoom()
{
return _model_diagram->zoom();
}
bec::Clipboard *ModelDiagramForm::get_clipboard()
{
return _owner->get_grt_manager()->get_clipboard();
}
bool ModelDiagramForm::can_undo()
{
return _owner->get_grt()->get_undo_manager()->can_undo();
}
bool ModelDiagramForm::can_redo()
{
return _owner->get_grt()->get_undo_manager()->can_redo();
}
bool ModelDiagramForm::can_copy()
{
return get_copiable_selection().count() > 0;
}
bool ModelDiagramForm::can_select_all()
{
return true;
}
static void check_if_can_paste(WBComponent *compo, const grt::ObjectRef &object, bool *result)
{
if (compo->can_paste_object(object))
*result= true;
}
bool ModelDiagramForm::can_paste()
{
std::list<grt::ObjectRef> data(get_clipboard()->get_data());
WBContext *wb= _owner->get_wb();
for (std::list<grt::ObjectRef>::iterator iter= data.begin();
iter != data.end(); ++iter)
{
if (!(*iter).is_valid())
{
log_warning("copy buffer has null value");
return false;
}
bool result= false;
wb->foreach_component(boost::bind(check_if_can_paste, _1, *iter, &result));
if (!result)
return false;
}
return !get_clipboard()->empty();
}
bool ModelDiagramForm::can_delete()
{
return has_selection();
}
std::string ModelDiagramForm::get_edit_target_name()
{
grt::ListRef<model_Object> sel(get_copiable_selection());
if (sel.count()==0)
return "";
if (sel.count() == 1)
{
std::string name;
name= sel[0]->name().c_str();
if (name.empty() && sel[0]->has_member("caption"))
name= sel[0]->get_string_member("caption");
return strfmt("'%s'", name.c_str());
}
else
return strfmt(_("%i Selected Figures"), (int) sel.count());
}
void ModelDiagramForm::undo()
{
_owner->get_grt()->get_undo_manager()->undo();
}
void ModelDiagramForm::redo()
{
_owner->get_grt()->get_undo_manager()->redo();
}
void ModelDiagramForm::cut()
{
grt::UndoManager *um= _owner->get_grt()->get_undo_manager();
std::string edit_target_name= get_edit_target_name();
um->begin_undo_group();
copy();
int count = (int)get_copiable_selection().count();
remove_selection();
um->end_undo_group();
um->set_action_description(strfmt(_("Cut %s"), edit_target_name.c_str()));
_owner->get_wb()->show_status_text(strfmt(_("%i figure(s) cut."), count));
}
void ModelDiagramForm::copy()
{
// object must be duplicated on copy because if the object is edited after copy
// whatever is pasted should still be in the same state as it was on copy
grt::ListRef<model_Object> selection(get_copiable_selection());
bec::Clipboard *clip= get_clipboard();
int count= 0;
grt::CopyContext copy_context(_owner->get_grt());
clip->clear();
for (size_t c= selection.count(), i= 0; i < c; i++)
{
WBComponent *compo= _owner->get_wb()->get_component_handling(selection.get(i));
if (compo)
{
compo->copy_object_to_clipboard(selection.get(i), copy_context);
count++;
}
}
clip->set_content_description(get_edit_target_name());
copy_context.finish();
clip->changed();
_owner->get_wb()->show_status_text(strfmt(_("%i object(s) copied."), count));
}
void ModelDiagramForm::clipboard_changed()
{
_paste_offset= 0;
}
static void get_component_that_can_paste(WBComponent *compo,
const grt::ObjectRef &object,
WBComponent **result)
{
if (compo->can_paste_object(object))
*result= compo;
}
void ModelDiagramForm::paste()
{
// Prevent too many updates.
UpdateLock lock(this);
WBContext *wb= _owner->get_wb();
int count= 0;
int duplicated = 0;
_paste_offset+= 20;
_owner->get_wb()->show_status_text(_("Pasting figures..."));
grt::CopyContext context(wb->get_grt());
grt::AutoUndo undo(wb->get_grt());
_model_diagram->unselectAll();
std::list<grt::ObjectRef> data(get_clipboard()->get_data());
// find what objects are in layers so that if they are also selected individually, they don't
// get copied again
std::set<std::string> skip_objects;
for (std::list<grt::ObjectRef>::iterator iter= data.begin();
iter != data.end(); ++iter)
{
if (model_LayerRef::can_wrap(*iter))
{
grt::ListRef<model_Figure> figures(model_LayerRef::cast_from(*iter)->figures());
GRTLIST_FOREACH(model_Figure, figures, fig)
{
skip_objects.insert((*fig)->id());
}
}
}
for (std::list<grt::ObjectRef>::iterator iter= data.begin();
iter != data.end(); ++iter)
{
// paste the object
WBComponent *compo= 0;
wb->foreach_component(boost::bind(get_component_that_can_paste, _1, *iter, &compo));
if (compo)
{
if (skip_objects.find((*iter)->id()) == skip_objects.end())
{
compo->paste_object(this, *iter, context);
count++;
}
}
else
log_warning("Don't know how to paste %s\n", iter->class_name().c_str());
}
context.finish();
undo.end(strfmt(_("Paste %s"), get_clipboard()->get_content_description().c_str()));
if (duplicated == 0)
_owner->get_wb()->show_status_text(strfmt(_("%i figure(s) pasted."), count));
else
_owner->get_wb()->show_status_text(strfmt(_("%i figure(s) pasted, %i duplicated."), count, duplicated));
}
void ModelDiagramForm::select_all()
{
for (size_t c= get_model_diagram()->figures().count(), i= 0; i < c; i++)
get_model_diagram()->selectObject(get_model_diagram()->figures().get(i));
for (size_t c= get_model_diagram()->layers().count(), i= 0; i < c; i++)
get_model_diagram()->selectObject(get_model_diagram()->layers().get(i));
}
void ModelDiagramForm::remove_selection()
{
grt::UndoManager *um= _owner->get_grt()->get_undo_manager();
grt::ListRef<model_Object> selection= get_selection();
std::vector<model_ObjectRef> objects;
std::string edit_target_name= get_edit_target_name();
um->begin_undo_group();
for (size_t c= selection.count(), i= 0; i < c; i++)
{
if (selection.get(i).is_instance(model_Object::static_class_name()))
objects.push_back(model_ObjectRef::cast_from(selection.get(i)));
}
for (size_t c= objects.size(), i= 0; i < c; i++)
_owner->get_wb()->get_model_context()->remove_figure(objects[i]);
um->end_undo_group();
um->set_action_description(strfmt(_("Remove %s"), edit_target_name.c_str()));
_owner->get_wb()->show_status_text(strfmt(_("%i figure(s) removed. The corresponding DB objects were kept."), (int)objects.size()));
}
//XXX unused? but it's virtual, need to check if this is used anywhere...
void ModelDiagramForm::delete_selection()
{
grt::UndoManager *um= _owner->get_grt()->get_undo_manager();
grt::ListRef<model_Object> selection= get_selection();
std::vector<model_ObjectRef> objects;
std::string edit_target_name= get_edit_target_name();
um->begin_undo_group();
for (size_t c= selection.count(), i= 0; i < c; i++)
{
if (selection.get(i).is_instance(model_Object::static_class_name()))
objects.push_back(model_ObjectRef::cast_from(selection.get(i)));
}
for (size_t c= objects.size(), i= 0; i < c; i++)
_owner->get_wb()->get_model_context()->delete_object(objects[i]);
um->end_undo_group();
um->set_action_description(strfmt(_("Delete %s"), edit_target_name.c_str()));
_owner->get_wb()->show_status_text(strfmt(_("%i object(s) deleted."), (int)objects.size()));
}
std::string ModelDiagramForm::get_diagram_info_text()
{
if (_model_diagram.is_valid())
return strfmt("%i x %i mm", (int)*_model_diagram->width(), (int)*_model_diagram->height());
return "";
}
std::vector<std::string> ModelDiagramForm::get_accepted_drop_types()
{
std::vector<std::string> vec;
vec.push_back(WB_DBOBJECT_DRAG_TYPE);
return vec;
}
grt::ListRef<model_Object> ModelDiagramForm::get_selection()
{
return _model_diagram->selection();
}
grt::ListRef<model_Object> ModelDiagramForm::get_copiable_selection()
{
grt::ListRef<model_Object> sel(_model_diagram->selection());
grt::ListRef<model_Object> copiable(_owner->get_grt());
for (size_t c= sel.count(), i= 0; i < c; i++)
{
if (!sel.get(i).is_instance(model_Connection::static_class_name()))
copiable.insert(sel[i]);
}
return copiable;
}
bool ModelDiagramForm::has_selection()
{
return _model_diagram->selection().count() > 0;
}
bool ModelDiagramForm::is_visible(const model_ObjectRef &object, bool partially)
{
mdc::CanvasItem *item= 0;
if (object.is_instance(model_Figure::static_class_name()))
{
item= model_FigureRef::cast_from(object)->get_data()->get_canvas_item();
}
else if (object.is_instance(model_Connection::static_class_name()))
{
item= model_ConnectionRef::cast_from(object)->get_data()->get_canvas_item();
}
else if (object.is_instance(model_Layer::static_class_name()))
{
item= model_LayerRef::cast_from(object)->get_data()->get_area_group();
}
else
{
g_warning("unhandled");
return false;
}
if (!item) return false;
Rect bounds= item->get_root_bounds();
Rect viewport= _view->get_viewport();
if (partially)
return mdc::bounds_intersect(viewport, bounds);
else
return mdc::bounds_contain_bounds(viewport, bounds);
}
void ModelDiagramForm::focus_and_make_visible(const model_ObjectRef &object, bool select)
{
mdc::CanvasItem *item= 0;
if (object.is_instance(model_Figure::static_class_name()))
{
item= model_FigureRef::cast_from(object)->get_data()->get_canvas_item();
}
else if (object.is_instance(model_Connection::static_class_name()))
{
item= model_ConnectionRef::cast_from(object)->get_data()->get_canvas_item();
}
else if (object.is_instance(model_Layer::static_class_name()))
{
item= model_LayerRef::cast_from(object)->get_data()->get_area_group();
}
if (item)
{
mdc::CanvasView *view= item->get_view();
Rect bounds= item->get_root_bounds();
Rect viewport= view->get_viewport();
if (!mdc::bounds_contain_bounds(viewport, bounds))
{
Point offset= viewport.pos;
// get the offset that will move the viewport the least to bring the item inside it
if (bounds.left() < viewport.left())
offset.x= bounds.left() - 20;
else if (bounds.right() > viewport.right())
offset.x= bounds.right() - viewport.width();
if (bounds.top() < viewport.top())
offset.y= bounds.top() - 20;
else if (bounds.bottom() > viewport.bottom())
offset.y= bounds.bottom() - viewport.height();
view->set_offset(offset);
}
view->focus_item(item);
if (select)
view->get_selection()->set(item);
}
}
static model_ObjectRef search_object_list(const grt::ListRef<model_Object> &objects,
size_t starting_index,
const std::string &text)
{
for (size_t count= objects.count(), i= starting_index; i < count; i++)
{
model_ObjectRef object(objects[i]);
if (strstr(object->name().c_str(), text.c_str()))
return object;
}
return model_ObjectRef();
}
bool ModelDiagramForm::search_and_focus_object(const std::string &text)
{
if (text.empty()) return false;
grt::ListRef<model_Object> selection(get_selection());
model_ObjectRef selected, found_object;
if (selection.count() > 0)
selected= selection[0];
if (!selected.is_valid() || selected.is_instance(model_Figure::static_class_name()))
{
size_t index= 0;
if (selected.is_valid())
{
index = _model_diagram->figures().get_index(selected);
if (index == grt::BaseListRef::npos)
index= 0;
else
index++;
}
found_object= search_object_list(_model_diagram->figures(), index, text);
}
if (!found_object.is_valid() && selected.is_instance(model_Connection::static_class_name()))
{
size_t index= 0;
if (selected.is_valid())
{
index = _model_diagram->connections().get_index(selected);
if (index == grt::BaseListRef::npos)
index= 0;
else
index++;
}
found_object= search_object_list(_model_diagram->connections(), index, text);
}
if (!found_object.is_valid() && selected.is_instance(model_Layer::static_class_name()))
{
size_t index= 0;
if (selected.is_valid())
{
index = _model_diagram->layers().get_index(selected);
if (index == grt::BaseListRef::npos)
index= 0;
else
index++;
}
found_object= search_object_list(_model_diagram->layers(), index, text);
}
if (found_object.is_valid())
{
_owner->get_grt_manager()->replace_status_text(strfmt(_("Found %s '%s'"),
found_object.get_metaclass()->get_attribute("caption").c_str(),
found_object->name().c_str()));
focus_and_make_visible(found_object, true);
return true;
}
else
{
if (_model_diagram->selection().count() > 0)
_owner->get_grt_manager()->replace_status_text(_("No more matches"));
else
_owner->get_grt_manager()->replace_status_text(_("No match found"));
}
_model_diagram->selection().remove_all();
return false;
}
void ModelDiagramForm::set_cursor(const std::string &cursor)
{
_cursor= cursor;
}
model_LayerRef ModelDiagramForm::get_layer_at(const Point &pos, Point &offset)
{
model_LayerRef layer;
mdc::AreaGroup *ag= 0;
mdc::CanvasItem *item= _view->get_item_at(pos);
if (!item)
{
offset= pos;
return _model_diagram->rootLayer();
}
item= item->get_toplevel();
while (item && (ag= dynamic_cast<mdc::AreaGroup*>(item))==0)
item= item->get_parent();
if (ag)
{
for (size_t c= _model_diagram->layers().count(), i= 0; i < c; i++)
{
layer= _model_diagram->layers().get(i);
if (ag == layer->get_data()->get_area_group())
{
offset= ag->convert_point_from(pos, 0);
return layer;
}
}
}
offset= pos;
return _model_diagram->rootLayer();
}
model_LayerRef ModelDiagramForm::get_layer_bounding(const Rect &rect, Point &offset)
{
grt::ListRef<model_Layer> layers(_model_diagram->layers());
for (grt::ListRef<model_Layer>::const_reverse_iterator layer= layers.rbegin();
layer != layers.rend(); ++layer)
{
if ((*layer)->get_data()->get_area_group() &&
mdc::bounds_contain_bounds((*layer)->get_data()->get_area_group()->get_root_bounds(), rect))
{
return *layer;
}
}
return model_LayerRef();
}
model_ObjectRef ModelDiagramForm::get_object_at(const Point &pos)
{
mdc::CanvasItem *item= _view->get_item_at(pos);
if (!item)
return model_ObjectRef();
std::string id= item->get_tag();
if (id.empty())
return model_ObjectRef();
model_ObjectRef object;
object= grt::find_object_in_list(_model_diagram->figures(), id);
if (object.is_valid())
return object;
object= grt::find_object_in_list(_model_diagram->layers(), id);
if (object.is_valid())
return object;
object= grt::find_object_in_list(_model_diagram->connections(), id);
if (object.is_valid())
return object;
return model_ObjectRef();
}
bool ModelDiagramForm::relocate_figures()
{
bool relocated= false;
grt::ListRef<model_Figure> figures(_model_diagram->figures());
// relocate all figures in the diagram to the layers they're inside
for (size_t c= figures.count(), i= 0; i < c; i++)
{
model_FigureRef figure(figures[i]);
relocated|= _model_diagram->get_data()->update_layer_of_figure(figure);
}
return relocated;
}
//--------------------------------------------------------------------------------------------------
bool ModelDiagramForm::accepts_drop(int x, int y, const std::string &type, const std::list<GrtObjectRef> &objects)
{
return _owner->accepts_drop(this, x, y, type, objects);
}
//--------------------------------------------------------------------------------------------------
bool ModelDiagramForm::accepts_drop(int x, int y, const std::string &type, const std::string &text)
{
return _owner->accepts_drop(this, x, y, type, text);
}
//--------------------------------------------------------------------------------------------------
bool ModelDiagramForm::perform_drop(int x, int y, const std::string &type, const std::list<GrtObjectRef> &objects)
{
bool retval = _owner->perform_drop(this, x, y, type, objects);
if (_catalog_tree && retval) // if it was accepted then we can mark all objects
{ // we will do the long way so we will not need to reload the whole tree and remember expanded rows
std::list<GrtObjectRef>::const_iterator it;
for(it = objects.begin(); it != objects.end(); ++it)
_catalog_tree->mark_node(*it);
}
return retval;
}
//--------------------------------------------------------------------------------------------------
bool ModelDiagramForm::perform_drop(int x, int y, const std::string &type, const std::string &text)
{
return _owner->perform_drop(this, x, y, type, text);
}
mdc::Layer *ModelDiagramForm::get_floater_layer()
{
return _floater_layer;
}
void ModelDiagramForm::add_floater(Floater *floater)
{
Point pos;
pos.x= _view->get_viewport().right() - 200;
pos.y= _view->get_viewport().top() + 20;
floater->move_to(pos);
_floater_layer->add_item(floater);
}
WBContext *ModelDiagramForm::get_wb()
{
return _owner->get_wb();
}
/* unused
void ModelDiagramForm::hover_in_figure(mdc::CanvasItem *item, Point pos)
{
}
void ModelDiagramForm::hover_out_figure(mdc::CanvasItem *item)
{
}
*/
// inline editing
void ModelDiagramForm::begin_editing(const Rect &rect, const std::string &text, float text_size, bool multiline)
{
if (_inline_edit_context)
{
int x, y;
int width, height;
_inline_edit_context->set_font_size(text_size);
_inline_edit_context->set_multiline(multiline);
_view->canvas_to_window(rect, x, y, width, height);
_inline_edit_context->begin_editing(x, y, width, height, text);
}
}
void ModelDiagramForm::stop_editing()
{
if (_inline_edit_context)
_inline_edit_context->end_editing();
}
static void forward_edit_finished(const std::string &text, EditFinishReason reason,
ModelDiagramForm *form)
{
(*form->signal_editing_done())(text, reason);
}
void ModelDiagramForm::set_inline_editor_context(InlineEditContext *context)
{
_inline_edit_context= context;
scoped_connect(_inline_edit_context->signal_edit_finished(),
boost::bind(forward_edit_finished, _1, _2, this));
}
void ModelDiagramForm::zoom_in()
{
model_DiagramRef view(get_model_diagram());
double zoom= *view->zoom();
for (size_t i= 0; i < sizeof(zoom_steps)/sizeof(*zoom_steps); i++)
{
if (zoom >= zoom_steps[i])
{
if (i > 0)
view->zoom(zoom_steps[i-1]);
return;
}
}
view->zoom(zoom_steps[0]);
}
void ModelDiagramForm::zoom_out()
{
model_DiagramRef view(get_model_diagram());
double zoom= *view->zoom();
for (size_t i= 0; i < sizeof(zoom_steps)/sizeof(*zoom_steps); i++)
{
if (zoom >= zoom_steps[i])
{
if (i+1 < sizeof(zoom_steps)/sizeof(*zoom_steps))
view->zoom(zoom_steps[i+1]);
return;
}
}
view->zoom(zoom_steps[sizeof(zoom_steps)/sizeof(*zoom_steps)-1]);
}
void ModelDiagramForm::setup_mini_view(mdc::CanvasView *view)
{
if (!_mini_view)
{
_mini_view= new MiniView(view->get_current_layer());
view->initialize();
view->get_background_layer()->set_visible(false);
view->set_page_layout(1, 1);
view->set_page_size(view->get_viewable_size());
view->get_current_layer()->add_item(_mini_view);
int w, h;
view->get_view_size(w, h);
_mini_view->set_active_view(get_view(), get_model_diagram());
update_mini_view_size(w, h);
}
}
//--------------------------------------------------------------------------------------------------
void ModelDiagramForm::update_mini_view_size(int w, int h)
{
if (_mini_view)
{
mdc::CanvasView *view= _mini_view->get_layer()->get_view();
view->update_view_size(w, h);
view->set_page_size(view->get_viewable_size());
_mini_view->update_size();
}
}
//--------------------------------------------------------------------------------------------------
std::string ModelDiagramForm::get_title()
{
return std::string(_model_diagram->name());
}
//--------------------------------------------------------------------------------------------------
void ModelDiagramForm::set_highlight_fks(bool flag)
{
_highlight_fks = flag;
_features->highlight_all_connections(flag);
}
//--------------------------------------------------------------------------------------------------
ModelDiagramForm::UpdateLock::~UpdateLock()
{
if (_form->_update_count > 0)
_form->_update_count--;
if (_form->_update_count == 0)
_form->_layer_tree->refresh();
}
|