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
|
// Copyright 2015 - 2025, GIBIS-UNIFESP and the wiRedPanda contributors
// SPDX-License-Identifier: GPL-3.0-or-later
#include "mainwindow.h"
#include "mainwindow_ui.h"
#include "bewaveddolphin.h"
#include "codegenerator.h"
#include "common.h"
#include "dflipflop.h"
#include "elementfactory.h"
#include "elementlabel.h"
#include "globalproperties.h"
#include "graphicsview.h"
#include "ic.h"
#include "recentfiles.h"
#include "settings.h"
#include "simulation.h"
#include "simulationblocker.h"
#include "thememanager.h"
#include "workspace.h"
#include <QActionGroup>
#include <QCheckBox>
#include <QCloseEvent>
#include <QDebug>
#include <QDesktopServices>
#include <QFileDialog>
#include <QLocale>
#include <QLoggingCategory>
#include <QMessageBox>
#include <QPdfWriter>
#include <QPrinter>
#include <QResource>
#include <QSaveFile>
#include <QShortcut>
#include <QTemporaryFile>
#include <QTranslator>
#ifdef Q_OS_MAC
#include <QSvgRenderer>
void ensureSvgUsage() {
QSvgRenderer dummy; // for macdeployqt to add libqsvg.dylib
}
#endif
#ifdef Q_OS_WASM
#include <emscripten/emscripten.h>
#endif
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
#define SKIPEMPTYPARTS QString::SkipEmptyParts
#else
#define SKIPEMPTYPARTS Qt::SkipEmptyParts
#endif
MainWindow::MainWindow(const QString &fileName, QWidget *parent)
: QMainWindow(parent)
, m_ui(std::make_unique<MainWindow_Ui>())
{
qCDebug(zero) << "wiRedPanda Version = " APP_VERSION " OR " << GlobalProperties::version;
m_ui->setupUi(this);
qCDebug(zero) << "Settings fileName: " << Settings::fileName();
// Get language from settings, or auto-detect from system if not set
QString language = Settings::value("language").toString();
if (language.isEmpty()) {
// Auto-detect system language
QLocale systemLocale = QLocale::system();
QString systemLanguage = systemLocale.name(); // e.g., "en_US", "pt_BR"
QString baseLanguage = systemLanguage.split('_').first(); // e.g., "en", "pt"
qCDebug(zero) << "Auto-detected system locale:" << systemLanguage;
// Check if we have translation for this language
const auto availableLanguages = getAvailableLanguages();
// First try the full locale (e.g., "pt_BR")
if (availableLanguages.contains(systemLanguage)) {
language = systemLanguage;
}
// Then try the base language (e.g., "pt")
else if (availableLanguages.contains(baseLanguage)) {
language = baseLanguage;
}
// Fall back to English
else {
qCDebug(zero) << "No translation available for" << systemLanguage << "or" << baseLanguage << ", falling back to English";
language = "en";
}
qCDebug(zero) << "Selected language:" << language;
}
loadTranslation(language);
populateLanguageMenu();
connect(m_ui->tab, &QTabWidget::currentChanged, this, &MainWindow::tabChanged);
connect(m_ui->tab, &QTabWidget::tabCloseRequested, this, &MainWindow::closeTab);
qCDebug(zero) << "Restoring geometry and setting zoom controls.";
restoreGeometry(Settings::value("MainWindow/geometry").toByteArray());
restoreState(Settings::value("MainWindow/windowState").toByteArray());
m_ui->splitter->restoreGeometry(Settings::value("MainWindow/splitter/geometry").toByteArray());
m_ui->splitter->restoreState(Settings::value("MainWindow/splitter/state").toByteArray());
qCDebug(zero) << "Preparing theme and UI modes.";
auto *themeGroup = new QActionGroup(this);
const auto actions = m_ui->menuTheme->actions();
for (auto *action : actions) {
themeGroup->addAction(action);
}
themeGroup->setExclusive(true);
connect(&ThemeManager::instance(), &ThemeManager::themeChanged, this, &MainWindow::updateTheme);
updateTheme();
setFastMode(Settings::value("fastMode").toBool());
m_ui->actionLabelsUnderIcons->setChecked(Settings::value("labelsUnderIcons").toBool());
m_ui->mainToolBar->setToolButtonStyle(Settings::value("labelsUnderIcons").toBool() ? Qt::ToolButtonTextUnderIcon : Qt::ToolButtonIconOnly);
qCDebug(zero) << "Setting left side menus.";
populateLeftMenu();
m_ui->tabElements->setTabIcon(0, QIcon(":/input/buttonOff.svg"));
m_ui->tabElements->setTabIcon(1, QIcon(":/basic/xor.svg"));
m_ui->tabElements->setTabIcon(2, QIcon(":/basic/truthtable-rotated.svg"));
m_ui->tabElements->setTabIcon(3, QIcon(DFlipFlop::pixmapPath()));
m_ui->tabElements->setTabIcon(4, QIcon(":/basic/ic-panda.svg"));
m_ui->tabElements->setTabIcon(5, QIcon(":/misc/text.png"));
m_ui->tabElements->setTabEnabled(6, false);
qCDebug(zero) << "Loading recent file list.";
m_recentFiles = new RecentFiles(this);
connect(this, &MainWindow::addRecentFile, m_recentFiles, &RecentFiles::addRecentFile);
createRecentFileActions();
connect(m_recentFiles, &RecentFiles::recentFilesUpdated, this, &MainWindow::updateRecentFileActions);
qCDebug(zero) << "Checking playing simulation.";
m_ui->actionPlay->setChecked(true);
qCDebug(zero) << "Window title.";
setWindowTitle("wiRedPanda " APP_VERSION);
qCDebug(zero) << "Building a new tab.";
createNewTab();
qCDebug(zero) << "Opening file if not empty.";
if (!fileName.isEmpty()) {
loadPandaFile(fileName);
}
qCDebug(zero) << "Disabling Arduino export.";
m_ui->actionExportToArduino->setEnabled(false);
QPixmapCache::setCacheLimit(100000);
qCDebug(zero) << "Adding examples to menu";
QDir examplesDir("examples");
if (examplesDir.exists()) {
const auto entryList = examplesDir.entryList({"*.panda"}, QDir::Files);
for (const auto &entry : entryList) {
auto *action = new QAction(entry);
connect(action, &QAction::triggered, this, [this] {
if (auto *senderAction = qobject_cast<QAction *>(sender())) {
loadPandaFile("examples/" + senderAction->text());
}
});
m_ui->menuExamples->addAction(action);
}
}
if (m_ui->menuExamples->isEmpty()) {
m_ui->menuExamples->menuAction()->setVisible(false);
}
// Shortcuts
auto *searchShortcut = new QShortcut(QKeySequence("Ctrl+F"), this);
auto *prevMainPropShortcut = new QShortcut(QKeySequence("["), this);
auto *nextMainPropShortcut = new QShortcut(QKeySequence("]"), this);
auto *prevSecndPropShortcut = new QShortcut(QKeySequence("{"), this);
auto *nextSecndPropShortcut = new QShortcut(QKeySequence("}"), this);
auto *changePrevElmShortcut = new QShortcut(QKeySequence("<"), this);
auto *changeNextElmShortcut = new QShortcut(QKeySequence(">"), this);
connect(searchShortcut, &QShortcut::activated, m_ui->lineEditSearch, qOverload<>(&QWidget::setFocus));
connect(prevMainPropShortcut, &QShortcut::activated, m_currentTab->scene(), &Scene::prevMainPropShortcut);
connect(nextMainPropShortcut, &QShortcut::activated, m_currentTab->scene(), &Scene::nextMainPropShortcut);
connect(prevSecndPropShortcut, &QShortcut::activated, m_currentTab->scene(), &Scene::prevSecndPropShortcut);
connect(nextSecndPropShortcut, &QShortcut::activated, m_currentTab->scene(), &Scene::nextSecndPropShortcut);
connect(changePrevElmShortcut, &QShortcut::activated, m_currentTab->scene(), &Scene::prevElm);
connect(changeNextElmShortcut, &QShortcut::activated, m_currentTab->scene(), &Scene::nextElm);
qCDebug(zero) << "Setting connections";
connect(m_ui->actionAbout, &QAction::triggered, this, &MainWindow::on_actionAbout_triggered);
connect(m_ui->actionAboutQt, &QAction::triggered, this, &MainWindow::on_actionAboutQt_triggered);
connect(m_ui->actionAboutThisVersion, &QAction::triggered, this, &MainWindow::aboutThisVersion);
connect(m_ui->actionReportTranslationError, &QAction::triggered, this, &MainWindow::on_actionReportTranslationError_triggered);
connect(m_ui->actionChangeTrigger, &QAction::triggered, m_ui->elementEditor, &ElementEditor::changeTriggerAction);
connect(m_ui->actionDarkTheme, &QAction::triggered, this, &MainWindow::on_actionDarkTheme_triggered);
connect(m_ui->actionExit, &QAction::triggered, this, &MainWindow::on_actionExit_triggered);
connect(m_ui->actionExportToArduino, &QAction::triggered, this, &MainWindow::on_actionExportToArduino_triggered);
connect(m_ui->actionExportToImage, &QAction::triggered, this, &MainWindow::on_actionExportToImage_triggered);
connect(m_ui->actionExportToPdf, &QAction::triggered, this, &MainWindow::on_actionExportToPdf_triggered);
connect(m_ui->actionFastMode, &QAction::triggered, this, &MainWindow::on_actionFastMode_triggered);
connect(m_ui->actionFlipHorizontally, &QAction::triggered, this, &MainWindow::on_actionFlipHorizontally_triggered);
connect(m_ui->actionFlipVertically, &QAction::triggered, this, &MainWindow::on_actionFlipVertically_triggered);
connect(m_ui->actionFullscreen, &QAction::triggered, this, &MainWindow::on_actionFullscreen_triggered);
connect(m_ui->actionGates, &QAction::triggered, this, &MainWindow::on_actionGates_triggered);
connect(m_ui->actionLabelsUnderIcons, &QAction::triggered, this, &MainWindow::on_actionLabelsUnderIcons_triggered);
connect(m_ui->actionLightTheme, &QAction::triggered, this, &MainWindow::on_actionLightTheme_triggered);
connect(m_ui->actionMute, &QAction::triggered, this, &MainWindow::on_actionMute_triggered);
connect(m_ui->actionNew, &QAction::triggered, this, &MainWindow::on_actionNew_triggered);
connect(m_ui->actionOpen, &QAction::triggered, this, &MainWindow::on_actionOpen_triggered);
connect(m_ui->actionPlay, &QAction::toggled, this, &MainWindow::on_actionPlay_toggled);
connect(m_ui->actionReloadFile, &QAction::triggered, this, &MainWindow::on_actionReloadFile_triggered);
connect(m_ui->actionRename, &QAction::triggered, m_ui->elementEditor, &ElementEditor::renameAction);
connect(m_ui->actionResetZoom, &QAction::triggered, this, &MainWindow::on_actionResetZoom_triggered);
connect(m_ui->actionRestart, &QAction::triggered, this, &MainWindow::on_actionRestart_triggered);
connect(m_ui->actionRotateLeft, &QAction::triggered, this, &MainWindow::on_actionRotateLeft_triggered);
connect(m_ui->actionRotateRight, &QAction::triggered, this, &MainWindow::on_actionRotateRight_triggered);
connect(m_ui->actionSave, &QAction::triggered, this, &MainWindow::on_actionSave_triggered);
connect(m_ui->actionSaveAs, &QAction::triggered, this, &MainWindow::on_actionSaveAs_triggered);
connect(m_ui->actionSelectAll, &QAction::triggered, this, &MainWindow::on_actionSelectAll_triggered);
connect(m_ui->actionShortcutsAndTips, &QAction::triggered, this, &MainWindow::on_actionShortcuts_and_Tips_triggered);
connect(m_ui->actionWaveform, &QAction::triggered, this, &MainWindow::on_actionWaveform_triggered);
connect(m_ui->actionWires, &QAction::triggered, this, &MainWindow::on_actionWires_triggered);
connect(m_ui->actionZoomIn, &QAction::triggered, this, &MainWindow::on_actionZoomIn_triggered);
connect(m_ui->actionZoomOut, &QAction::triggered, this, &MainWindow::on_actionZoomOut_triggered);
connect(m_ui->lineEditSearch, &QLineEdit::returnPressed, this, &MainWindow::on_lineEditSearch_returnPressed);
connect(m_ui->lineEditSearch, &QLineEdit::textChanged, this, &MainWindow::on_lineEditSearch_textChanged);
connect(m_ui->pushButtonAddIC, &QPushButton::clicked, this, &MainWindow::on_pushButtonAddIC_clicked);
connect(m_ui->pushButtonRemoveIC, &QPushButton::clicked, this, &MainWindow::on_pushButtonRemoveIC_clicked);
connect(m_ui->pushButtonRemoveIC, &TrashButton::removeICFile, this, &MainWindow::removeICFile);
}
MainWindow::~MainWindow()
{
}
void MainWindow::loadAutosaveFiles()
{
QStringList autosaves(Settings::value("autosaveFile").toStringList());
qCDebug(zero) << "All autosave files: " << autosaves;
for (auto it = autosaves.begin(); it != autosaves.end();) {
QFile file(*it);
if (!file.exists()) {
qCDebug(zero) << "Removing from config the autosave file that does not exist.";
it = autosaves.erase(it);
continue;
}
try {
loadPandaFile(*it);
} catch (const std::exception &e) {
QMessageBox::critical(nullptr, tr("Error!"), e.what());
qCDebug(zero) << "Removing autosave file that is corrupted.";
it = autosaves.erase(it);
continue;
}
m_currentTab->setAutosaveFile();
++it;
}
Settings::setValue("autosaveFile", autosaves);
}
void MainWindow::createNewTab()
{
qCDebug(zero) << "Creating new workspace.";
auto *workspace = new WorkSpace(this);
connect(workspace, &WorkSpace::fileChanged, this, &MainWindow::setCurrentFile);
workspace->view()->setFastMode(m_ui->actionFastMode->isChecked());
workspace->scene()->updateTheme();
qCDebug(zero) << "Adding tab. #tabs: " << m_ui->tab->count() << ", current tab: " << m_tabIndex;
m_ui->tab->addTab(workspace, tr("New Project"));
qCDebug(zero) << "Selecting the newly created tab.";
m_ui->tab->setCurrentIndex(m_ui->tab->count() - 1);
qCDebug(zero) << "Finished #tabs: " << m_ui->tab->count() << ", current tab: " << m_tabIndex;
}
void MainWindow::removeUndoRedoMenu()
{
if (!m_currentTab) {
return;
}
m_ui->menuEdit->removeAction(m_ui->menuEdit->actions().constFirst());
m_ui->menuEdit->removeAction(m_ui->menuEdit->actions().constFirst());
}
void MainWindow::addUndoRedoMenu()
{
m_ui->menuEdit->insertAction(m_ui->menuEdit->actions().at(0), m_currentTab->scene()->undoAction());
m_ui->menuEdit->insertAction(m_ui->menuEdit->actions().at(1), m_currentTab->scene()->redoAction());
}
void MainWindow::setFastMode(const bool fastMode)
{
m_ui->actionFastMode->setChecked(fastMode);
if (m_currentTab) {
m_currentTab->view()->setFastMode(fastMode);
}
}
void MainWindow::on_actionExit_triggered()
{
close();
}
void MainWindow::save(const QString &fileName)
{
if (!m_currentTab) {
return;
}
m_currentTab->save(fileName);
updateICList();
m_ui->statusBar->showMessage(tr("File saved successfully."), 4000);
}
void MainWindow::show()
{
QMainWindow::show();
if (!Settings::contains("hideV4Warning")) {
aboutThisVersion();
}
qCDebug(zero) << "Checking for autosave file recovery.";
loadAutosaveFiles();
}
void MainWindow::aboutThisVersion()
{
qCDebug(zero) << "'hideV4Warning' message box.";
auto *checkBox = new QCheckBox(tr("Don't show this again."));
QMessageBox msgBox;
msgBox.setParent(this);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setIcon(QMessageBox::Icon::Information);
msgBox.setWindowTitle("wiRedPanda " APP_VERSION);
msgBox.setText(
tr("wiRedPanda version >= 4.0 is not 100% compatible with previous versions.\n"
"To open old version projects containing ICs (or boxes), skins, and/or "
"beWavedDolphin simulations, their files must be moved to the same directory "
"as the main project file.\n"
"wiRedPanda %1 will automatically list all other .panda files located "
"in the same directory of the current project as ICs in the editor tab.\n"
"You have to save new projects before accessing ICs and skins, or running "
"beWavedDolphin simulations.").arg(APP_VERSION));
msgBox.setWindowModality(Qt::WindowModal);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.setCheckBox(checkBox);
#if QT_VERSION < QT_VERSION_CHECK(6, 7, 0)
connect(checkBox, &QCheckBox::stateChanged, this, [](int state) {
#else
connect(checkBox, &QCheckBox::checkStateChanged, this, [](int state) {
#endif
if (static_cast<Qt::CheckState>(state) == Qt::CheckState::Checked) {
Settings::setValue("hideV4Warning", "true");
} else {
Settings::remove("hideV4Warning");
}
});
msgBox.exec();
}
int MainWindow::closeTabAnyway()
{
QMessageBox msgBox;
msgBox.setParent(this);
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox.setText(tr("File not saved. Close tab anyway?"));
msgBox.setWindowModality(Qt::WindowModal);
msgBox.setDefaultButton(QMessageBox::No);
return msgBox.exec();
}
int MainWindow::confirmSave(const bool multiple)
{
QMessageBox msgBox;
msgBox.setParent(this);
if (multiple) {
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll | QMessageBox::Cancel);
} else {
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
}
const QString fileName = m_currentFile.fileName().isEmpty() ? tr("New Project") : m_currentFile.fileName();
msgBox.setText(fileName + tr(" has been modified.\nDo you want to save your changes?"));
msgBox.setWindowModality(Qt::WindowModal);
msgBox.setDefaultButton(QMessageBox::Yes);
return msgBox.exec();
}
void MainWindow::on_actionNew_triggered()
{
createNewTab();
}
void MainWindow::on_actionWires_triggered(const bool checked)
{
if (m_currentTab) {
m_currentTab->scene()->showWires(checked);
}
}
void MainWindow::on_actionRotateRight_triggered()
{
if (m_currentTab) {
m_currentTab->scene()->rotateRight();
}
}
void MainWindow::on_actionRotateLeft_triggered()
{
if (m_currentTab) {
m_currentTab->scene()->rotateLeft();
}
}
void MainWindow::loadPandaFile(const QString &fileName)
{
createNewTab();
qCDebug(zero) << "Loading in editor.";
m_currentTab->load(fileName);
updateICList();
m_ui->statusBar->showMessage(tr("File loaded successfully."), 4000);
}
void MainWindow::on_actionOpen_triggered()
{
#ifdef Q_OS_WASM
auto fileContentReady = [this](const QString &fileName, const QByteArray &fileContent) {
if (!fileName.isEmpty()) {
// Write file content to a temporary file
QFile file(fileName);
file.open(QIODevice::WriteOnly);
file.write(fileContent);
file.close();
loadPandaFile(fileName);
}
};
QFileDialog::getOpenFileContent("Panda files (*.panda)", fileContentReady);
#else
const QString path = m_currentFile.exists() ? "" : "./examples";
const QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), path, tr("Panda files (*.panda)"));
if (fileName.isEmpty()) {
return;
}
loadPandaFile(fileName);
#endif
}
void MainWindow::on_actionSave_triggered()
{
if (!m_currentTab) {
return;
}
// TODO: if current file is autosave ask for filename
QString fileName = m_currentFile.absoluteFilePath();
if (fileName.isEmpty()) {
fileName = QFileDialog::getSaveFileName(this, tr("Save File as ..."), "", tr("Panda files (*.panda)"));
if (fileName.isEmpty()) {
return;
}
if (!fileName.endsWith(".panda")) {
fileName.append(".panda");
}
}
save(fileName);
}
void MainWindow::on_actionSaveAs_triggered()
{
if (!m_currentTab) {
return;
}
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File as ..."), m_currentFile.absoluteFilePath(), tr("Panda files (*.panda)"));
if (fileName.isEmpty()) {
return;
}
if (!fileName.endsWith(".panda")) {
fileName.append(".panda");
}
IC::copyFiles(QFileInfo(m_currentFile), QFileInfo(fileName));
loadPandaFile(fileName);
}
void MainWindow::on_actionAbout_triggered()
{
QMessageBox::about(
this,
"wiRedPanda",
tr("<p>wiRedPanda is a software developed by the students of the Federal University of São Paulo."
" This project was created in order to help students learn about logic circuits.</p>"
"<p>Software version: %1</p>"
"<p><strong>Creators:</strong></p>"
"<ul>"
"<li> Davi Morales </li>"
"<li> Lucas Lellis </li>"
"<li> Rodrigo Torres </li>"
"<li> Prof. Fábio Cappabianco, Ph.D. </li>"
"</ul>"
"<p> wiRedPanda is currently maintained by Prof. Fábio Cappabianco, Ph.D., João Pedro M. Oliveira, Matheus R. Esteves and Maycon A. Santana.</p>"
"<p> Please file a report at our GitHub page if bugs are found or if you wish for a new functionality to be implemented.</p>"
"<p><a href=\"http://gibis-unifesp.github.io/wiRedPanda/\">Visit our website!</a></p>")
.arg(QApplication::applicationVersion()));
}
void MainWindow::on_actionShortcuts_and_Tips_triggered()
{
QMessageBox::information(this,
tr("Shortcuts and Tips"),
tr("<h1>Canvas Shortcuts</h1>"
"<ul style=\"list-style:none;\">"
"<li> Ctrl+= : Zoom in </li>"
"<li> Ctrl+- : Zoom out </li>"
"<li> Ctrl+1 : Hide/Show wires </li>"
"<li> Ctrl+2 : Hide/Show gates </li>"
"<li> Ctrl+F : Search elements </li>"
"<li> Ctrl+W : Open beWaveDolphin </li>"
"<li> Ctrl+S : Save project </li>"
"<li> Ctrl+Q : Exit wiRedPanda </li>"
"<li> F5 : Start/Pause simulation </li>"
"<li> [ : Previous primary element property </li>"
"<li> ] : Next primary element property </li>"
"<li> { : Previous secondary element property </li>"
"<li> } : Next secondary element property </li>"
"<li> < : Morph to previous element </li>"
"<li> > : Morph to next element </li>"
"</ul>"
"<h1>General Tips</h1>"
"<p>Double click in a wire to create a node</p>"
));
}
void MainWindow::on_actionAboutQt_triggered()
{
QMessageBox::aboutQt(this);
}
void MainWindow::on_actionReportTranslationError_triggered()
{
QDesktopServices::openUrl(QUrl("https://hosted.weblate.org/projects/wiredpanda/wiredpanda"));
}
bool MainWindow::closeFiles()
{
while (m_ui->tab->count() != 0) {
if (!closeTab(m_ui->tab->count() - 1)) {
return false;
}
}
return true;
}
void MainWindow::closeEvent(QCloseEvent *event)
{
bool closeWindow = false;
if (!hasModifiedFiles()) {
auto reply =
QMessageBox::question(
this,
tr("Exit ") + QApplication::applicationName(),
tr("Are you sure?"),
QMessageBox::Cancel | QMessageBox::Yes,
QMessageBox::Yes);
if (reply == QMessageBox::Yes) {
closeWindow = true;
}
} else if (closeFiles()) {
closeWindow = true;
}
if (closeWindow) {
updateSettings();
event->accept();
} else {
event->ignore();
}
}
void MainWindow::updateSettings()
{
Settings::setValue("MainWindow/geometry", saveGeometry());
Settings::setValue("MainWindow/windowState", saveState());
Settings::setValue("MainWindow/splitter/geometry", m_ui->splitter->saveGeometry());
Settings::setValue("MainWindow/splitter/state", m_ui->splitter->saveState());
}
bool MainWindow::hasModifiedFiles()
{
const QStringList autosaves = Settings::value("autosaveFile").toStringList();
const auto workspaces = m_ui->tab->findChildren<WorkSpace *>();
for (auto *workspace : workspaces) {
auto *undoStack = workspace->scene()->undoStack();
if (!undoStack->isClean()) {
return true;
}
const QString fileName = workspace->fileInfo().fileName();
if (!fileName.isEmpty() && autosaves.contains(fileName)) {
return true;
}
}
return false;
}
QFileInfo MainWindow::currentFile() const
{
return m_currentTab ? m_currentTab->fileInfo() : QFileInfo();
}
QDir MainWindow::currentDir() const
{
return m_currentTab ? m_currentTab->fileInfo().absoluteDir() : QDir();
}
void MainWindow::setCurrentFile(const QFileInfo &fileInfo)
{
if (!m_currentTab) {
return;
}
m_currentFile = fileInfo;
QString text = fileInfo.exists() ? fileInfo.fileName() : tr("New Project");
if (!m_currentTab->scene()->undoStack()->isClean()) {
text += "*";
}
m_ui->tab->setTabText(m_tabIndex, text);
m_ui->tab->setTabToolTip(m_tabIndex, fileInfo.absoluteFilePath());
qCDebug(zero) << "Adding file to recent files.";
emit addRecentFile(fileInfo.absoluteFilePath());
}
void MainWindow::on_actionSelectAll_triggered()
{
m_currentTab->scene()->selectAll();
}
void MainWindow::updateICList()
{
m_ui->scrollAreaWidgetContents_IC->layout()->removeItem(m_ui->verticalSpacer_IC);
const auto items = m_ui->scrollAreaWidgetContents_IC->findChildren<ElementLabel *>();
for (auto *item : items) {
item->deleteLater();
}
const auto items2 = m_ui->scrollAreaWidgetContents_Search->findChildren<ElementLabel *>();
for (auto *item : items2) {
if (item->elementType() == ElementType::IC) {
item->deleteLater();
}
}
if (m_currentFile.exists()) {
qCDebug(zero) << "Show files.";
QDir directory(m_currentFile.absoluteDir());
QStringList files = directory.entryList({"*.panda", "*.PANDA"}, QDir::Files);
files.removeAll(m_currentFile.fileName());
for (int i = files.size() - 1; i >= 0; --i) {
if (files.at(i).at(0) == '.') {
files.removeAt(i);
}
}
qCDebug(zero) << "Files: " << files.join(", ");
for (const QString &file : std::as_const(files)) {
QPixmap pixmap(":/basic/ic-panda.svg");
auto *item = new ElementLabel(pixmap, ElementType::IC, file, this);
m_ui->scrollAreaWidgetContents_IC->layout()->addWidget(item);
auto *item2 = new ElementLabel(pixmap, ElementType::IC, file, this);
m_ui->scrollAreaWidgetContents_Search->layout()->addWidget(item2);
}
}
m_ui->scrollAreaWidgetContents_IC->layout()->addItem(m_ui->verticalSpacer_IC);
}
bool MainWindow::closeTab(const int tabIndex)
{
qCDebug(zero) << "Closing tab " << tabIndex + 1 << ", #tabs: " << m_ui->tab->count();
m_ui->tab->setCurrentIndex(tabIndex);
qCDebug(zero) << "Checking if needs to save file.";
if (!m_currentTab->scene()->undoStack()->isClean()) {
const int selectedButton = confirmSave(false);
if (selectedButton == QMessageBox::Cancel) {
return false;
}
if (selectedButton == QMessageBox::Yes) {
try {
save();
} catch (const std::exception &e) {
QMessageBox::critical(this, tr("Error"), e.what());
if (closeTabAnyway() == QMessageBox::No) {
return false;
}
}
}
}
qCDebug(zero) << "Deleting tab.";
m_currentTab->deleteLater();
m_ui->tab->removeTab(tabIndex);
qCDebug(zero) << "Closed tab " << tabIndex << ", #tabs: " << m_ui->tab->count() << ", current tab: " << m_tabIndex;
return true;
}
void MainWindow::disconnectTab()
{
if (!m_currentTab) {
return;
}
m_ui->elementEditor->setScene(nullptr);
qCDebug(zero) << "Stopping simulation.";
m_currentTab->simulation()->stop();
qCDebug(zero) << "Disconnecting zoom from UI.";
disconnect(m_currentTab->view(), &GraphicsView::zoomChanged, this, &MainWindow::zoomChanged);
qCDebug(zero) << "Removing undo and redo actions from UI menu.";
removeUndoRedoMenu();
disconnect(m_ui->actionClearSelection, &QAction::triggered, m_currentTab->scene(), &Scene::clearSelection);
disconnect(m_ui->actionCopy, &QAction::triggered, m_currentTab->scene(), &Scene::copyAction);
disconnect(m_ui->actionCut, &QAction::triggered, m_currentTab->scene(), &Scene::cutAction);
disconnect(m_ui->actionDelete, &QAction::triggered, m_currentTab->scene(), &Scene::deleteAction);
disconnect(m_ui->actionPaste, &QAction::triggered, m_currentTab->scene(), &Scene::pasteAction);
disconnect(m_ui->elementEditor, &ElementEditor::sendCommand, m_currentTab->scene(), &Scene::receiveCommand);
}
void MainWindow::connectTab()
{
qCDebug(zero) << "Connecting undo and redo functions to UI menu.";
addUndoRedoMenu();
qCDebug(zero) << "Setting Panda file info.";
m_currentFile = m_currentTab->fileInfo();
updateICList();
qCDebug(zero) << "Connecting current tab to element editor menu in UI.";
m_ui->elementEditor->setScene(m_currentTab->scene());
connect(m_currentTab->view(), &GraphicsView::zoomChanged, this, &MainWindow::zoomChanged);
connect(m_ui->actionClearSelection, &QAction::triggered, m_currentTab->scene(), &Scene::clearSelection);
connect(m_ui->actionCopy, &QAction::triggered, m_currentTab->scene(), &Scene::copyAction);
connect(m_ui->actionCut, &QAction::triggered, m_currentTab->scene(), &Scene::cutAction);
connect(m_ui->actionDelete, &QAction::triggered, m_currentTab->scene(), &Scene::deleteAction);
connect(m_ui->actionPaste, &QAction::triggered, m_currentTab->scene(), &Scene::pasteAction);
if (m_ui->actionPlay->isChecked()) {
qCDebug(zero) << "Restarting simulation.";
m_currentTab->simulation()->start();
m_currentTab->scene()->clearSelection();
}
m_currentTab->view()->setFastMode(m_ui->actionFastMode->isChecked());
m_ui->actionZoomIn->setEnabled(m_currentTab->view()->canZoomIn());
m_ui->actionZoomOut->setEnabled(m_currentTab->view()->canZoomOut());
ElementFactory::setLastId(m_currentTab->lastId());
}
WorkSpace *MainWindow::currentTab() const
{
return m_currentTab;
}
void MainWindow::tabChanged(const int newTabIndex)
{
disconnectTab(); // disconnect previously selected tab
m_tabIndex = newTabIndex;
m_ui->elementEditor->hide();
if (newTabIndex == -1) {
m_currentTab = nullptr;
return;
}
m_currentTab = qobject_cast<WorkSpace *>(m_ui->tab->currentWidget());
qCDebug(zero) << "Selecting tab: " << newTabIndex;
connectTab();
qCDebug(zero) << "New tab selected. Dolphin fileName: " << m_currentTab->dolphinFileName();
}
void MainWindow::on_lineEditSearch_textChanged(const QString &text)
{
m_ui->scrollAreaWidgetContents_Search->layout()->removeItem(m_ui->verticalSpacer_Search);
if (text.isEmpty()) {
m_ui->tabElements->tabBar()->show();
m_ui->tabElements->setCurrentIndex(m_lastTabIndex);
m_ui->tabElements->setTabEnabled(5, false);
m_lastTabIndex = -1;
} else {
if (m_lastTabIndex == -1) {
m_lastTabIndex = m_ui->tabElements->currentIndex();
}
m_ui->tabElements->tabBar()->hide();
m_ui->tabElements->setCurrentIndex(5);
m_ui->tabElements->setTabEnabled(5, true);
const auto allItems = m_ui->scrollArea_Search->findChildren<ElementLabel *>();
QRegularExpression regex1(QString("^label_.*%1.*").arg(text), QRegularExpression::CaseInsensitiveOption);
auto searchResults = m_ui->scrollArea_Search->findChildren<ElementLabel *>(regex1);
QRegularExpression regex2(QString(".*%1.*").arg(text), QRegularExpression::CaseInsensitiveOption);
for (auto *item : allItems) {
if (regex2.match(item->name()).hasMatch()) {
if (!searchResults.contains(item)) {
searchResults.append(item);
}
}
}
const auto ics = m_ui->scrollArea_Search->findChildren<ElementLabel *>("label_ic");
for (auto *ic : ics) {
if (regex2.match(ic->icFileName()).hasMatch()) {
searchResults.append(ic);
}
}
for (auto *item : allItems) {
item->setHidden(true);
}
for (auto *item : std::as_const(searchResults)) {
item->setVisible(true);
}
}
m_ui->scrollAreaWidgetContents_Search->layout()->addItem(m_ui->verticalSpacer_Search);
}
void MainWindow::on_lineEditSearch_returnPressed()
{
if (!m_currentTab) {
return;
}
auto allLabels = m_ui->scrollArea_Search->findChildren<ElementLabel *>();
for (auto *label : allLabels) {
if (label->isVisible()) {
m_currentTab->scene()->addItem(label->mimeData());
m_ui->lineEditSearch->clear();
return;
}
}
}
void MainWindow::on_actionReloadFile_triggered()
{
if (!m_currentFile.exists() || !m_currentTab) {
return;
}
const QString file = m_currentFile.absoluteFilePath();
closeTab(m_tabIndex);
loadPandaFile(file);
}
void MainWindow::on_actionGates_triggered(const bool checked)
{
if (!m_currentTab) {
return;
}
m_ui->actionWires->setEnabled(checked);
m_currentTab->scene()->showWires(checked ? m_ui->actionWires->isChecked() : checked);
m_currentTab->scene()->showGates(checked);
}
void MainWindow::exportToArduino(QString fileName)
{
if (!m_currentTab) {
return;
}
if (fileName.isEmpty()) {
throw PANDACEPTION("Missing file name.");
}
auto elements = m_currentTab->scene()->elements();
if (elements.isEmpty()) {
throw PANDACEPTION("The .panda file is empty.");
}
SimulationBlocker simulationBlocker(m_currentTab->simulation());
if (!fileName.endsWith(".ino")) {
fileName.append(".ino");
}
elements = Common::sortGraphicElements(elements);
CodeGenerator arduino(QDir::home().absoluteFilePath(fileName), elements);
arduino.generate();
m_ui->statusBar->showMessage(tr("Arduino code successfully generated."), 4000);
qCDebug(zero) << "Arduino code successfully generated.";
}
void MainWindow::exportToWaveFormFile(const QString &fileName)
{
if (fileName.isEmpty()) {
throw PANDACEPTION("Missing file name.");
}
auto *bewavedDolphin = new BewavedDolphin(m_currentTab->scene(), false, this);
bewavedDolphin->createWaveform(fileName);
bewavedDolphin->print();
}
void MainWindow::exportToWaveFormTerminal()
{
auto *bewavedDolphin = new BewavedDolphin(m_currentTab->scene(), false, this);
bewavedDolphin->createWaveform();
bewavedDolphin->print();
}
void MainWindow::on_actionExportToArduino_triggered()
{
if (!m_currentTab) {
return;
}
QString path;
if (m_currentFile.exists()) {
path = m_currentFile.absolutePath();
}
const QString fileName = QFileDialog::getSaveFileName(this, tr("Generate Arduino Code"), path, tr("Arduino file (*.ino)"));
if (!fileName.isEmpty()) {
exportToArduino(fileName);
}
}
void MainWindow::on_actionZoomIn_triggered() const
{
if (!m_currentTab) {
return;
}
m_currentTab->view()->zoomIn();
}
void MainWindow::on_actionZoomOut_triggered() const
{
if (!m_currentTab) {
return;
}
m_currentTab->view()->zoomOut();
}
void MainWindow::on_actionResetZoom_triggered() const
{
if (!m_currentTab) {
return;
}
m_currentTab->view()->resetZoom();
}
void MainWindow::zoomChanged()
{
if (!m_currentTab) {
return;
}
m_ui->actionZoomIn->setEnabled(m_currentTab->view()->canZoomIn());
m_ui->actionZoomOut->setEnabled(m_currentTab->view()->canZoomOut());
}
void MainWindow::updateRecentFileActions()
{
const auto files = m_recentFiles->recentFiles();
const int numRecentFiles = qMin(files.size(), GlobalProperties::maxRecentFiles);
if (numRecentFiles > 0) {
m_ui->menuRecentFiles->setEnabled(true);
}
auto actions = m_ui->menuRecentFiles->actions();
for (int i = 0; i < numRecentFiles; ++i) {
const QString text = "&" + QString::number(i + 1) + " " + QFileInfo(files.at(i)).fileName();
actions.at(i)->setText(text);
actions.at(i)->setData(files.at(i));
actions.at(i)->setVisible(true);
}
for (int i = numRecentFiles; i < GlobalProperties::maxRecentFiles; ++i) {
actions.at(i)->setVisible(false);
}
}
void MainWindow::openRecentFile()
{
if (auto *action = qobject_cast<QAction *>(sender())) {
loadPandaFile(action->data().toString());
}
}
void MainWindow::createRecentFileActions()
{
m_ui->menuRecentFiles->clear();
for (int i = 0; i < GlobalProperties::maxRecentFiles; ++i) {
auto *action = new QAction(this);
action->setVisible(false);
connect(action, &QAction::triggered, this, &MainWindow::openRecentFile);
m_ui->menuRecentFiles->addAction(action);
}
updateRecentFileActions();
}
void MainWindow::on_actionExportToPdf_triggered()
{
if (!m_currentTab) {
return;
}
m_currentTab->scene()->clearSelection();
QString path;
if (m_currentFile.exists()) {
path = m_currentFile.absolutePath();
}
QString pdfFile = QFileDialog::getSaveFileName(this, tr("Export to PDF"), path, tr("PDF files (*.pdf)"));
if (pdfFile.isEmpty()) {
return;
}
if (!pdfFile.endsWith(".pdf", Qt::CaseInsensitive)) {
pdfFile.append(".pdf");
}
QPrinter printer(QPrinter::HighResolution);
printer.setPageSize(QPageSize(QPageSize::A4));
printer.setPageOrientation(QPageLayout::Orientation::Landscape);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(pdfFile);
QPainter painter;
if (!painter.begin(&printer)) {
throw PANDACEPTION("Could not print this circuit to PDF.");
}
auto *scene = m_currentTab->scene();
scene->render(&painter, QRectF(), scene->itemsBoundingRect().adjusted(-64, -64, 64, 64));
painter.end();
m_ui->statusBar->showMessage(tr("Exported file successfully."), 4000);
QDesktopServices::openUrl(QUrl::fromLocalFile(pdfFile));
}
void MainWindow::on_actionExportToImage_triggered()
{
if (!m_currentTab) {
return;
}
m_currentTab->scene()->clearSelection();
QString path;
if (m_currentFile.exists()) {
path = m_currentFile.absolutePath();
}
QString pngFile = QFileDialog::getSaveFileName(this, tr("Export to Image"), path, tr("PNG files (*.png)"));
if (pngFile.isEmpty()) {
return;
}
if (!pngFile.endsWith(".png", Qt::CaseInsensitive)) {
pngFile.append(".png");
}
QRectF rect = m_currentTab->scene()->itemsBoundingRect().adjusted(-64, -64, 64, 64);
QPixmap pixmap(rect.size().toSize());
QPainter painter;
painter.begin(&pixmap);
painter.setRenderHint(QPainter::Antialiasing);
m_currentTab->scene()->render(&painter, QRectF(), rect);
painter.end();
pixmap.save(pngFile);
m_ui->statusBar->showMessage(tr("Exported file successfully."), 4000);
QDesktopServices::openUrl(QUrl::fromLocalFile(pngFile));
}
void MainWindow::retranslateUi()
{
ElementFactory::clearCache();
m_ui->retranslateUi();
m_ui->elementEditor->retranslateUi();
const auto items = m_ui->tabElements->findChildren<ElementLabel *>();
for (auto *item : items) {
item->updateName();
}
for (int index = 0; index < m_ui->tab->count(); ++index) {
auto *workspace = qobject_cast<WorkSpace *>(m_ui->tab->widget(index));
auto *undoStack = workspace->scene()->undoStack();
auto fileInfo = workspace->fileInfo();
QString text = fileInfo.exists() ? fileInfo.fileName() : tr("New Project");
if (!undoStack->isClean()) {
text += "*";
}
m_ui->tab->setTabText(index, text);
for (auto *elm : workspace->scene()->elements()) {
elm->retranslate();
}
}
}
void MainWindow::loadTranslation(const QString &language)
{
if (language.isEmpty()) {
return;
}
Settings::setValue("language", language);
qApp->removeTranslator(m_pandaTranslator);
qApp->removeTranslator(m_qtTranslator);
delete m_pandaTranslator;
delete m_qtTranslator;
m_pandaTranslator = nullptr;
m_qtTranslator = nullptr;
if (language == "en") {
retranslateUi();
return;
}
// ---------------------------------------------
// Dynamic translation loading based on available files
QString pandaFile = QString(":/translations/wpanda_%1.qm").arg(language);
QString qtFile = QString(":/translations/qt_%1.qm").arg(language);
// Check if wiRedPanda translation exists
QResource pandaResource(pandaFile);
if (pandaResource.isValid()) {
m_pandaTranslator = new QTranslator(this);
if (!m_pandaTranslator->load(pandaFile) || !qApp->installTranslator(m_pandaTranslator)) {
qCWarning(zero) << "Failed to load wiRedPanda translation for" << language << ", falling back to English";
delete m_pandaTranslator;
m_pandaTranslator = nullptr;
// Fallback to English
if (language != "en") {
loadTranslation("en");
return;
}
}
}
// Check if Qt translation exists
QResource qtResource(qtFile);
if (qtResource.isValid()) {
m_qtTranslator = new QTranslator(this);
if (!m_qtTranslator->load(qtFile) || !qApp->installTranslator(m_qtTranslator)) {
qCWarning(zero) << "Failed to load Qt translation for" << language << ", continuing without Qt translation";
delete m_qtTranslator;
m_qtTranslator = nullptr;
}
}
retranslateUi();
}
QStringList MainWindow::getAvailableLanguages() const
{
QStringList languages;
// Always include English as it's the default
languages << "en";
// Dynamically discover all available wpanda translation files
// Get all resources in the translations directory
QDir translationsDir(":/translations");
if (translationsDir.exists()) {
QStringList qmFiles = translationsDir.entryList({"wpanda_*.qm"}, QDir::Files);
for (const QString &file : qmFiles) {
// Extract language code from filename (e.g., "wpanda_fr.qm" -> "fr")
QString langCode = file;
langCode.remove("wpanda_");
langCode.remove(".qm");
// Verify the resource actually exists and is valid
QString resourcePath = QString(":/translations/%1").arg(file);
QResource resource(resourcePath);
if (resource.isValid() && !langCode.isEmpty()) {
languages << langCode;
}
}
} else {
// Fallback: Try all possible language patterns if directory listing fails
// This covers edge cases where QDir doesn't work with Qt resources
const QStringList languagePatterns = {
"ar", "bg", "bn", "cs", "da", "de", "el", "es", "et", "fa", "fi", "fr",
"he", "hi", "hr", "hu", "id", "it", "ja", "ko", "lt", "lv", "ms", "nb",
"nl", "pl", "pt", "pt_BR", "ro", "ru", "sk", "sv", "th", "tr", "uk",
"vi", "zh_Hans", "zh_Hant"
};
for (const QString &langCode : languagePatterns) {
QString resourcePath = QString(":/translations/wpanda_%1.qm").arg(langCode);
QResource resource(resourcePath);
if (resource.isValid()) {
languages << langCode;
}
}
}
// Remove duplicates and sort
languages.removeDuplicates();
languages.sort();
return languages;
}
void MainWindow::populateLanguageMenu()
{
// Clear existing actions from the UI menu
m_ui->menuLanguage->clear();
const auto availableLanguages = getAvailableLanguages();
auto *languageGroup = new QActionGroup(this);
languageGroup->setExclusive(true);
for (const QString &langCode : availableLanguages) {
auto *action = new QAction(getLanguageDisplayName(langCode), this);
action->setCheckable(true);
action->setData(langCode);
// Set the flag icon for the language
action->setIcon(QIcon(getLanguageFlagIcon(langCode)));
// Check if this is the current language
if (langCode == Settings::value("language").toString() ||
(langCode == "en" && Settings::value("language").toString().isEmpty())) {
action->setChecked(true);
}
languageGroup->addAction(action);
m_ui->menuLanguage->addAction(action);
connect(action, &QAction::triggered, this, [this, langCode]() {
loadTranslation(langCode);
});
}
}
QString MainWindow::getLanguageDisplayName(const QString &langCode) const
{
// Use Qt's built-in locale system to get native language names
QLocale locale(langCode);
// Special handling for some language codes that need specific locales
if (langCode == "pt_BR") {
locale = QLocale(QLocale::Portuguese, QLocale::Brazil);
}
// Get the native language name (e.g., "Deutsch" for German)
QString nativeName = locale.nativeLanguageName();
// If we have a country-specific variant, add the country name
if (langCode.contains('_') || langCode == "pt_BR") {
#if QT_VERSION < QT_VERSION_CHECK(6, 2, 0)
QString nativeCountryName = locale.nativeCountryName();
#else
QString nativeCountryName = locale.nativeTerritoryName();
#endif
if (!nativeCountryName.isEmpty() && nativeCountryName != nativeName) {
nativeName += QString(" (%1)").arg(nativeCountryName);
}
}
// Fallback to language code if Qt doesn't have the name
if (nativeName.isEmpty()) {
// Manual fallbacks for languages Qt might not fully support
static const QMap<QString, QString> fallbackNames = {
{"bn", "বাংলা"},
{"fa", "فارسی"},
{"he", "עברית"},
{"th", "ไทย"}
};
nativeName = fallbackNames.value(langCode, langCode);
}
// Ensure proper capitalization for the first letter
if (!nativeName.isEmpty() && nativeName.at(0).isLetter()) {
nativeName[0] = nativeName.at(0).toUpper();
}
return nativeName;
}
QString MainWindow::getLanguageFlagIcon(const QString &langCode) const
{
// Use Qt's locale system to determine the appropriate country flag
QLocale locale(langCode);
// Special handling for specific language variants
if (langCode == "pt") {
locale = QLocale(QLocale::Portuguese, QLocale::Portugal);
}
// Map Qt country codes to our flag resource names
static const QMap<QLocale::Country, QString> countryToFlag = {
{QLocale::SaudiArabia, ":/toolbar/arabic.svg"}, // Arabic
{QLocale::Bulgaria, ":/toolbar/bulgarian.svg"}, // Bulgarian
{QLocale::Bangladesh, ":/toolbar/bangladesh.svg"}, // Bengali
{QLocale::CzechRepublic, ":/toolbar/czech.svg"}, // Czech
{QLocale::Denmark, ":/toolbar/danish.svg"}, // Danish
{QLocale::Germany, ":/toolbar/german.svg"}, // German
{QLocale::Greece, ":/toolbar/greek.svg"}, // Greek
{QLocale::UnitedStates, ":/toolbar/usa.svg"}, // English
{QLocale::Spain, ":/toolbar/spanish.svg"}, // Spanish
{QLocale::Estonia, ":/toolbar/estonian.svg"}, // Estonian
{QLocale::Iran, ":/toolbar/iranian.svg"}, // Persian/Farsi
{QLocale::Finland, ":/toolbar/finnish.svg"}, // Finnish
{QLocale::France, ":/toolbar/french.svg"}, // French
{QLocale::Israel, ":/toolbar/hebrew.svg"}, // Hebrew
{QLocale::India, ":/toolbar/hindi.svg"}, // Hindi
{QLocale::Croatia, ":/toolbar/croatian.svg"}, // Croatian
{QLocale::Hungary, ":/toolbar/hungarian.svg"}, // Hungarian
{QLocale::Indonesia, ":/toolbar/indonesian.svg"}, // Indonesian
{QLocale::Italy, ":/toolbar/italian.svg"}, // Italian
{QLocale::Japan, ":/toolbar/japanese.svg"}, // Japanese
{QLocale::SouthKorea, ":/toolbar/korean.svg"}, // Korean
{QLocale::Lithuania, ":/toolbar/lithuanian.svg"}, // Lithuanian
{QLocale::Latvia, ":/toolbar/latvian.svg"}, // Latvian
{QLocale::Malaysia, ":/toolbar/malaysian.svg"}, // Malay
{QLocale::Norway, ":/toolbar/norwegian.svg"}, // Norwegian
{QLocale::Netherlands, ":/toolbar/dutch.svg"}, // Dutch
{QLocale::Poland, ":/toolbar/polish.svg"}, // Polish
{QLocale::Portugal, ":/toolbar/portuguese.svg"}, // Portuguese
{QLocale::Brazil, ":/toolbar/brasil.svg"}, // Portuguese (Brazil)
{QLocale::Romania, ":/toolbar/romanian.svg"}, // Romanian
{QLocale::Russia, ":/toolbar/russian.svg"}, // Russian
{QLocale::Slovakia, ":/toolbar/slovak.svg"}, // Slovak
{QLocale::Sweden, ":/toolbar/swedish.svg"}, // Swedish
{QLocale::Thailand, ":/toolbar/thai.svg"}, // Thai
{QLocale::Turkey, ":/toolbar/turkish.svg"}, // Turkish
{QLocale::Ukraine, ":/toolbar/ukrainian.svg"}, // Ukrainian
{QLocale::Vietnam, ":/toolbar/vietnamese.svg"}, // Vietnamese
{QLocale::China, ":/toolbar/chinese.svg"}, // Chinese Simplified
{QLocale::Taiwan, ":/toolbar/chinese_traditional.svg"} // Chinese Traditional
};
// Get the flag for the locale's country
#if QT_VERSION < QT_VERSION_CHECK(6, 2, 0)
QString flagIcon = countryToFlag.value(locale.country(), ":/toolbar/default.svg");
#else
QString flagIcon = countryToFlag.value(locale.territory(), ":/toolbar/default.svg");
#endif
// Fallback for languages where Qt might not detect the country correctly
if (flagIcon == ":/toolbar/default.svg") {
static const QMap<QString, QString> languageFallbacks = {
{"ar", ":/toolbar/arabic.svg"},
{"bg", ":/toolbar/bulgarian.svg"},
{"bn", ":/toolbar/bangladesh.svg"},
{"cs", ":/toolbar/czech.svg"},
{"da", ":/toolbar/danish.svg"},
{"de", ":/toolbar/german.svg"},
{"el", ":/toolbar/greek.svg"},
{"en", ":/toolbar/usa.svg"},
{"es", ":/toolbar/spanish.svg"},
{"et", ":/toolbar/estonian.svg"},
{"fa", ":/toolbar/iranian.svg"},
{"fi", ":/toolbar/finnish.svg"},
{"fr", ":/toolbar/french.svg"},
{"he", ":/toolbar/hebrew.svg"},
{"hi", ":/toolbar/hindi.svg"},
{"hr", ":/toolbar/croatian.svg"},
{"hu", ":/toolbar/hungarian.svg"},
{"id", ":/toolbar/indonesian.svg"},
{"it", ":/toolbar/italian.svg"},
{"ja", ":/toolbar/japanese.svg"},
{"ko", ":/toolbar/korean.svg"},
{"lt", ":/toolbar/lithuanian.svg"},
{"lv", ":/toolbar/latvian.svg"},
{"ms", ":/toolbar/malaysian.svg"},
{"nb", ":/toolbar/norwegian.svg"},
{"nl", ":/toolbar/dutch.svg"},
{"pl", ":/toolbar/polish.svg"},
{"pt", ":/toolbar/portuguese.svg"},
{"pt_BR", ":/toolbar/brasil.svg"},
{"ro", ":/toolbar/romanian.svg"},
{"ru", ":/toolbar/russian.svg"},
{"sk", ":/toolbar/slovak.svg"},
{"sv", ":/toolbar/swedish.svg"},
{"th", ":/toolbar/thai.svg"},
{"tr", ":/toolbar/turkish.svg"},
{"uk", ":/toolbar/ukrainian.svg"},
{"vi", ":/toolbar/vietnamese.svg"},
{"zh_Hans", ":/toolbar/chinese.svg"},
{"zh_Hant", ":/toolbar/chinese_traditional.svg"}
};
flagIcon = languageFallbacks.value(langCode, ":/toolbar/default.svg");
}
return flagIcon;
}
void MainWindow::on_actionPlay_toggled(const bool checked)
{
if (!m_currentTab) {
return;
}
auto *simulation = m_currentTab->simulation();
checked ? simulation->start() : simulation->stop();
}
void MainWindow::on_actionRestart_triggered()
{
if (!m_currentTab) {
return;
}
m_currentTab->simulation()->restart();
}
void MainWindow::populateMenu(QSpacerItem *spacer, const QStringList &names, QLayout *layout)
{
layout->removeItem(spacer);
for (const auto &name : names) {
auto type = ElementFactory::textToType(name);
auto pixmap(ElementFactory::pixmap(type));
layout->addWidget(new ElementLabel(pixmap, type, name, this));
m_ui->scrollAreaWidgetContents_Search->layout()->addWidget(new ElementLabel(pixmap, type, name, this));
}
layout->addItem(spacer);
}
void MainWindow::populateLeftMenu()
{
m_ui->tabElements->setCurrentIndex(0);
populateMenu(m_ui->verticalSpacer_InOut, {"InputVcc", "InputGnd", "InputButton", "InputSwitch", "InputRotary", "Clock", "Led", "Display7", "Display14", "Display16", "Buzzer", "AudioBox"}, m_ui->scrollAreaWidgetContents_InOut->layout());
populateMenu(m_ui->verticalSpacer_Gates, {"And", "Or", "Not", "Nand", "Nor", "Xor", "Xnor", "Node"}, m_ui->scrollAreaWidgetContents_Gates->layout());
populateMenu(m_ui->verticalSpacer_Combinational, {"TruthTable", "Mux", "Demux"}, m_ui->scrollAreaWidgetContents_Combinational->layout());
populateMenu(m_ui->verticalSpacer_Memory, {"DLatch", "SRLatch", "DFlipFlop", "JKFlipFlop", "TFlipFlop"}, m_ui->scrollAreaWidgetContents_Memory->layout());
populateMenu(m_ui->verticalSpacer_Misc, {"Text", "Line"}, m_ui->scrollAreaWidgetContents_Misc->layout());
}
void MainWindow::on_actionFastMode_triggered(const bool checked)
{
setFastMode(checked);
Settings::setValue("fastMode", checked);
}
void MainWindow::on_actionWaveform_triggered()
{
if (!m_currentTab) {
return;
}
qCDebug(zero) << "BD fileName: " << m_currentTab->dolphinFileName();
auto *bewavedDolphin = new BewavedDolphin(m_currentTab->scene(), true, this);
bewavedDolphin->createWaveform(m_currentTab->dolphinFileName());
bewavedDolphin->show();
}
void MainWindow::on_actionLightTheme_triggered()
{
ThemeManager::setTheme(Theme::Light);
}
void MainWindow::on_actionDarkTheme_triggered()
{
ThemeManager::setTheme(Theme::Dark);
}
void MainWindow::updateTheme()
{
switch (ThemeManager::theme()) {
case Theme::Dark: m_ui->actionDarkTheme->setChecked(true); break;
case Theme::Light: m_ui->actionLightTheme->setChecked(true); break;
default:
// Handle unexpected theme values - fallback to Light theme
m_ui->actionLightTheme->setChecked(true);
break;
}
m_ui->tabElements->setTabIcon(2, QIcon(DFlipFlop::pixmapPath()));
const auto labels = m_ui->memory->findChildren<ElementLabel *>();
for (auto *label : labels) {
label->updateTheme();
}
m_ui->elementEditor->updateTheme();
}
void MainWindow::on_actionFlipHorizontally_triggered()
{
if (!m_currentTab) {
return;
}
m_currentTab->scene()->flipHorizontally();
}
void MainWindow::on_actionFlipVertically_triggered()
{
if (!m_currentTab) {
return;
}
m_currentTab->scene()->flipVertically();
}
QString MainWindow::dolphinFileName()
{
if (!m_currentTab) {
return {};
}
return m_currentTab->dolphinFileName();
}
void MainWindow::setDolphinFileName(const QString &fileName)
{
if (!m_currentTab) {
return;
}
m_currentTab->setDolphinFileName(fileName);
}
void MainWindow::on_actionFullscreen_triggered()
{
isFullScreen() ? showNormal() : showFullScreen();
}
void MainWindow::on_actionMute_triggered(const bool checked)
{
if (!m_currentTab) {
return;
}
m_currentTab->scene()->mute(checked);
m_ui->actionMute->setText(checked ? tr("Unmute") : tr("Mute"));
}
void MainWindow::on_actionLabelsUnderIcons_triggered(const bool checked)
{
m_ui->mainToolBar->setToolButtonStyle(checked ? Qt::ToolButtonTextUnderIcon : Qt::ToolButtonIconOnly);
Settings::setValue("labelsUnderIcons", checked);
}
bool MainWindow::event(QEvent *event)
{
switch (event->type()) {
case QEvent::WindowActivate: {
if (m_ui->actionPlay->isChecked()) {
on_actionPlay_toggled(true);
}
break;
}
case QEvent::WindowDeactivate: {
if (!m_ui->actionBackground_Simulation->isChecked()) {
on_actionPlay_toggled(false);
}
break;
}
default: break;
};
return QMainWindow::event(event);
}
void MainWindow::on_pushButtonAddIC_clicked()
{
if (!m_currentTab) {
return;
}
if (!m_currentTab->fileInfo().isReadable()) {
throw PANDACEPTION("Save file first.");
}
QFileDialog fileDialog;
fileDialog.setObjectName(tr("Open File"));
fileDialog.setFileMode(QFileDialog::ExistingFile);
fileDialog.setNameFilter(tr("Panda (*.panda)"));
if (fileDialog.exec() == QDialog::Rejected) {
return;
}
const auto files = fileDialog.selectedFiles();
if (files.isEmpty()) {
return;
}
QMessageBox::information(this, tr("Info"), tr("Selected files (and their dependencies) will be copied to current file folder."));
for (const auto &file : files) {
QFileInfo destPath(GlobalProperties::currentDir + "/" + QFileInfo(file).fileName());
IC::copyFiles(QFileInfo(file), destPath);
}
updateICList();
}
void MainWindow::on_pushButtonRemoveIC_clicked()
{
QMessageBox::information(this, tr("Info"), tr("Drag here to remove."));
}
void MainWindow::removeICFile(const QString &icFileName)
{
if (!m_currentTab) {
return;
}
SimulationBlocker blocker(m_currentTab->simulation());
auto elements = m_currentTab->scene()->elements();
for (auto it = elements.rbegin(); it != elements.rend(); ++it) {
if ((*it)->elementType() == ElementType::IC && (*it)->label().append(".panda").toLower() == icFileName) {
m_currentTab->scene()->removeItem(*it);
delete *it;
}
}
QFile file(GlobalProperties::currentDir + "/" + icFileName);
if (!file.remove()) {
throw PANDACEPTION("Error removing file: %1", file.errorString());
}
updateICList();
on_actionSave_triggered();
}
|