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 1962 1963 1964 1965 1966 1967 1968
|
/*
* This file is part of the PulseView project.
*
* Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#ifdef ENABLE_DECODE
#include <libsigrokdecode/libsigrokdecode.h>
#endif
#include <extdef.h>
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <iostream>
#include <iterator>
#include <unordered_set>
#include <QApplication>
#include <QDebug>
#include <QEvent>
#include <QFontMetrics>
#include <QMenu>
#include <QMouseEvent>
#include <QScrollBar>
#include <QVBoxLayout>
#include <libsigrokcxx/libsigrokcxx.hpp>
#include "analogsignal.hpp"
#include "header.hpp"
#include "logicsignal.hpp"
#include "ruler.hpp"
#include "signal.hpp"
#include "tracegroup.hpp"
#include "triggermarker.hpp"
#include "view.hpp"
#include "viewport.hpp"
#include "pv/data/logic.hpp"
#include "pv/data/logicsegment.hpp"
#include "pv/devices/device.hpp"
#include "pv/globalsettings.hpp"
#include "pv/session.hpp"
#include "pv/util.hpp"
#ifdef ENABLE_DECODE
#include "decodetrace.hpp"
#endif
using pv::data::SignalData;
using pv::data::Segment;
using pv::util::TimeUnit;
using pv::util::Timestamp;
using std::back_inserter;
using std::copy_if;
using std::count_if;
using std::inserter;
using std::max;
using std::make_pair;
using std::make_shared;
using std::min;
using std::numeric_limits;
using std::pair;
using std::set;
using std::set_difference;
using std::shared_ptr;
using std::unordered_map;
using std::unordered_set;
using std::vector;
namespace pv {
namespace views {
namespace trace {
const Timestamp View::MaxScale("1e9");
const Timestamp View::MinScale("1e-12");
const int View::MaxScrollValue = INT_MAX / 2;
/* Area at the top and bottom of the view that can't be scrolled out of sight */
const int View::ViewScrollMargin = 50;
const int View::ScaleUnits[3] = {1, 2, 5};
CustomScrollArea::CustomScrollArea(QWidget *parent) :
QAbstractScrollArea(parent)
{
}
bool CustomScrollArea::viewportEvent(QEvent *event)
{
switch (event->type()) {
case QEvent::Paint:
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
case QEvent::MouseButtonDblClick:
case QEvent::MouseMove:
case QEvent::Wheel:
case QEvent::TouchBegin:
case QEvent::TouchUpdate:
case QEvent::TouchEnd:
return false;
default:
return QAbstractScrollArea::viewportEvent(event);
}
}
View::View(Session &session, bool is_main_view, QMainWindow *parent) :
ViewBase(session, is_main_view, parent),
// Note: Place defaults in View::reset_view_state(), not here
splitter_(new QSplitter()),
header_was_shrunk_(false), // The splitter remains unchanged after a reset, so this goes here
sticky_scrolling_(false), // Default setting is set in MainWindow::setup_ui()
scroll_needs_defaults_(true)
{
QVBoxLayout *root_layout = new QVBoxLayout(this);
root_layout->setContentsMargins(0, 0, 0, 0);
root_layout->addWidget(splitter_);
viewport_ = new Viewport(*this);
scrollarea_ = new CustomScrollArea(splitter_);
scrollarea_->setViewport(viewport_);
scrollarea_->setFrameShape(QFrame::NoFrame);
ruler_ = new Ruler(*this);
header_ = new Header(*this);
header_->setMinimumWidth(10); // So that the arrow tips show at least
// We put the header into a simple layout so that we can add the top margin,
// allowing us to make it line up with the bottom of the ruler
QWidget *header_container = new QWidget();
header_container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QVBoxLayout *header_layout = new QVBoxLayout(header_container);
header_layout->setContentsMargins(0, ruler_->sizeHint().height(), 0, 0);
header_layout->addWidget(header_);
// To let the ruler and scrollarea be on the same split pane, we need a layout
QWidget *trace_container = new QWidget();
trace_container->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
QVBoxLayout *trace_layout = new QVBoxLayout(trace_container);
trace_layout->setSpacing(0); // We don't want space between the ruler and scrollarea
trace_layout->setContentsMargins(0, 0, 0, 0);
trace_layout->addWidget(ruler_);
trace_layout->addWidget(scrollarea_);
splitter_->addWidget(header_container);
splitter_->addWidget(trace_container);
splitter_->setHandleWidth(1); // Don't show a visible rubber band
splitter_->setCollapsible(0, false); // Prevent the header from collapsing
splitter_->setCollapsible(1, false); // Prevent the traces from collapsing
splitter_->setStretchFactor(0, 0); // Prevent the panes from being resized
splitter_->setStretchFactor(1, 1); // when the entire view is resized
splitter_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
viewport_->installEventFilter(this);
ruler_->installEventFilter(this);
header_->installEventFilter(this);
// Set up settings and event handlers
GlobalSettings settings;
colored_bg_ = settings.value(GlobalSettings::Key_View_ColoredBG).toBool();
snap_distance_ = settings.value(GlobalSettings::Key_View_SnapDistance).toInt();
GlobalSettings::add_change_handler(this);
connect(scrollarea_->horizontalScrollBar(), SIGNAL(valueChanged(int)),
this, SLOT(h_scroll_value_changed(int)));
connect(scrollarea_->verticalScrollBar(), SIGNAL(valueChanged(int)),
this, SLOT(v_scroll_value_changed()));
connect(header_, SIGNAL(selection_changed()),
ruler_, SLOT(clear_selection()));
connect(ruler_, SIGNAL(selection_changed()),
header_, SLOT(clear_selection()));
connect(header_, SIGNAL(selection_changed()),
this, SIGNAL(selection_changed()));
connect(ruler_, SIGNAL(selection_changed()),
this, SIGNAL(selection_changed()));
connect(splitter_, SIGNAL(splitterMoved(int, int)),
this, SLOT(on_splitter_moved()));
connect(&lazy_event_handler_, SIGNAL(timeout()),
this, SLOT(process_sticky_events()));
lazy_event_handler_.setSingleShot(true);
lazy_event_handler_.setInterval(1000 / ViewBase::MaxViewAutoUpdateRate);
// Set up local keyboard shortcuts
zoom_in_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Plus), this,
SLOT(on_zoom_in_shortcut_triggered()), nullptr, Qt::WidgetWithChildrenShortcut);
zoom_in_shortcut_->setAutoRepeat(false);
zoom_out_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Minus), this,
SLOT(on_zoom_out_shortcut_triggered()), nullptr, Qt::WidgetWithChildrenShortcut);
zoom_out_shortcut_->setAutoRepeat(false);
zoom_in_shortcut_2_ = new QShortcut(QKeySequence(Qt::Key_Up), this,
SLOT(on_zoom_in_shortcut_triggered()), nullptr, Qt::WidgetWithChildrenShortcut);
zoom_out_shortcut_2_ = new QShortcut(QKeySequence(Qt::Key_Down), this,
SLOT(on_zoom_out_shortcut_triggered()), nullptr, Qt::WidgetWithChildrenShortcut);
home_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Home), this,
SLOT(on_scroll_to_start_shortcut_triggered()), nullptr, Qt::WidgetWithChildrenShortcut);
home_shortcut_->setAutoRepeat(false);
end_shortcut_ = new QShortcut(QKeySequence(Qt::Key_End), this,
SLOT(on_scroll_to_end_shortcut_triggered()), nullptr, Qt::WidgetWithChildrenShortcut);
end_shortcut_->setAutoRepeat(false);
grab_ruler_left_shortcut_ = new QShortcut(QKeySequence(Qt::Key_1), this,
nullptr, nullptr, Qt::WidgetWithChildrenShortcut);
connect(grab_ruler_left_shortcut_, &QShortcut::activated,
this, [=]{on_grab_ruler(1);});
grab_ruler_left_shortcut_->setAutoRepeat(false);
grab_ruler_right_shortcut_ = new QShortcut(QKeySequence(Qt::Key_2), this,
nullptr, nullptr, Qt::WidgetWithChildrenShortcut);
connect(grab_ruler_right_shortcut_, &QShortcut::activated,
this, [=]{on_grab_ruler(2);});
grab_ruler_right_shortcut_->setAutoRepeat(false);
cancel_grab_shortcut_ = new QShortcut(QKeySequence(Qt::Key_Escape), this,
nullptr, nullptr, Qt::WidgetWithChildrenShortcut);
connect(cancel_grab_shortcut_, &QShortcut::activated,
this, [=]{grabbed_widget_ = nullptr;});
cancel_grab_shortcut_->setAutoRepeat(false);
// Trigger the initial event manually. The default device has signals
// which were created before this object came into being
signals_changed();
// Make sure the transparent widgets are on the top
ruler_->raise();
header_->raise();
reset_view_state();
}
View::~View()
{
GlobalSettings::remove_change_handler(this);
}
ViewType View::get_type() const
{
return ViewTypeTrace;
}
void View::reset_view_state()
{
ViewBase::reset_view_state();
segment_display_mode_ = Trace::ShowLastSegmentOnly;
segment_selectable_ = false;
scale_ = 1e-3;
offset_ = 0;
ruler_offset_ = 0;
zero_offset_ = 0;
custom_zero_offset_set_ = false;
updating_scroll_ = false;
settings_restored_ = false;
always_zoom_to_fit_ = false;
tick_period_ = 0;
tick_prefix_ = pv::util::SIPrefix::yocto;
tick_precision_ = 0;
time_unit_ = util::TimeUnit::Time;
show_cursors_ = false;
cursors_ = make_shared<CursorPair>(*this);
next_flag_text_ = 'A';
trigger_markers_.clear();
hover_widget_ = nullptr;
grabbed_widget_ = nullptr;
hover_point_ = QPoint(-1, -1);
scroll_needs_defaults_ = true;
saved_v_offset_ = 0;
scale_at_acq_start_ = 0;
offset_at_acq_start_ = 0;
suppress_zoom_to_fit_after_acq_ = false;
show_cursors_ = false;
cursor_state_changed(show_cursors_);
flags_.clear();
// Update the zoom state
calculate_tick_spacing();
// Make sure the standard bar's segment selector is in sync
set_segment_display_mode(segment_display_mode_);
}
Session& View::session()
{
return session_;
}
const Session& View::session() const
{
return session_;
}
vector< shared_ptr<Signal> > View::signals() const
{
return signals_;
}
shared_ptr<Signal> View::get_signal_by_signalbase(shared_ptr<data::SignalBase> base) const
{
shared_ptr<Signal> ret_val;
for (const shared_ptr<Signal>& s : signals_)
if (s->base() == base) {
ret_val = s;
break;
}
return ret_val;
}
void View::clear_signals()
{
ViewBase::clear_signals();
signals_.clear();
}
void View::add_signal(const shared_ptr<Signal> signal)
{
ViewBase::add_signalbase(signal->base());
signals_.push_back(signal);
signal->set_segment_display_mode(segment_display_mode_);
signal->set_current_segment(current_segment_);
connect(signal->base().get(), SIGNAL(name_changed(const QString&)),
this, SLOT(on_signal_name_changed()));
}
#ifdef ENABLE_DECODE
void View::clear_decode_signals()
{
ViewBase::clear_decode_signals();
decode_traces_.clear();
}
void View::add_decode_signal(shared_ptr<data::DecodeSignal> signal)
{
ViewBase::add_decode_signal(signal);
shared_ptr<DecodeTrace> d(
new DecodeTrace(session_, signal, decode_traces_.size()));
decode_traces_.push_back(d);
d->set_segment_display_mode(segment_display_mode_);
d->set_current_segment(current_segment_);
connect(signal.get(), SIGNAL(name_changed(const QString&)),
this, SLOT(on_signal_name_changed()));
}
void View::remove_decode_signal(shared_ptr<data::DecodeSignal> signal)
{
for (auto i = decode_traces_.begin(); i != decode_traces_.end(); i++)
if ((*i)->base() == signal) {
decode_traces_.erase(i);
signals_changed();
return;
}
ViewBase::remove_decode_signal(signal);
}
#endif
shared_ptr<Signal> View::get_signal_under_mouse_cursor() const
{
return signal_under_mouse_cursor_;
}
View* View::view()
{
return this;
}
const View* View::view() const
{
return this;
}
Viewport* View::viewport()
{
return viewport_;
}
const Viewport* View::viewport() const
{
return viewport_;
}
QAbstractScrollArea* View::scrollarea() const
{
return scrollarea_;
}
const Ruler* View::ruler() const
{
return ruler_;
}
void View::save_settings(QSettings &settings) const
{
settings.setValue("scale", scale_);
settings.setValue("v_offset",
scrollarea_->verticalScrollBar()->sliderPosition());
settings.setValue("splitter_state", splitter_->saveState());
settings.setValue("segment_display_mode", segment_display_mode_);
GlobalSettings::store_timestamp(settings, "offset", offset_);
if (custom_zero_offset_set_)
GlobalSettings::store_timestamp(settings, "zero_offset", -zero_offset_);
else
settings.remove("zero_offset");
for (const shared_ptr<Signal>& signal : signals_) {
settings.beginGroup(signal->base()->internal_name());
signal->save_settings(settings);
settings.endGroup();
}
}
void View::restore_settings(QSettings &settings)
{
// Note: It is assumed that this function is only called once,
// immediately after restoring a previous session.
if (settings.contains("scale"))
set_scale(settings.value("scale").toDouble());
if (settings.contains("offset")) {
// This also updates ruler_offset_
set_offset(GlobalSettings::restore_timestamp(settings, "offset"));
}
if (settings.contains("zero_offset"))
set_zero_position(GlobalSettings::restore_timestamp(settings, "zero_offset"));
if (settings.contains("splitter_state"))
splitter_->restoreState(settings.value("splitter_state").toByteArray());
if (settings.contains("segment_display_mode"))
set_segment_display_mode(
(Trace::SegmentDisplayMode)(settings.value("segment_display_mode").toInt()));
for (shared_ptr<Signal> signal : signals_) {
settings.beginGroup(signal->base()->internal_name());
signal->restore_settings(settings);
settings.endGroup();
}
if (settings.contains("v_offset")) {
saved_v_offset_ = settings.value("v_offset").toInt();
set_v_offset(saved_v_offset_);
scroll_needs_defaults_ = false;
// Note: see eventFilter() for additional information
}
settings_restored_ = true;
suppress_zoom_to_fit_after_acq_ = true;
// Update the ruler so that it uses the new scale
calculate_tick_spacing();
}
vector< shared_ptr<TimeItem> > View::time_items() const
{
const vector<shared_ptr<Flag>> f(flags());
vector<shared_ptr<TimeItem>> items(f.begin(), f.end());
if (cursors_) {
items.push_back(cursors_);
items.push_back(cursors_->first());
items.push_back(cursors_->second());
}
for (auto& trigger_marker : trigger_markers_)
items.push_back(trigger_marker);
return items;
}
double View::scale() const
{
return scale_;
}
void View::set_scale(double scale)
{
if (scale_ != scale) {
scale_ = scale;
scale_changed();
}
}
void View::set_offset(const pv::util::Timestamp& offset, bool force_update)
{
if ((offset_ != offset) || force_update) {
offset_ = offset;
ruler_offset_ = offset_ + zero_offset_;
offset_changed();
}
}
const Timestamp& View::offset() const
{
return offset_;
}
const Timestamp& View::ruler_offset() const
{
return ruler_offset_;
}
void View::set_zero_position(const pv::util::Timestamp& position)
{
zero_offset_ = -position;
custom_zero_offset_set_ = true;
// Force an immediate update of the offsets
set_offset(offset_, true);
ruler_->update();
}
void View::reset_zero_position()
{
zero_offset_ = 0;
// When enabled, the first trigger for this segment is used as the zero position
GlobalSettings settings;
bool trigger_is_zero_time = settings.value(GlobalSettings::Key_View_TriggerIsZeroTime).toBool();
if (trigger_is_zero_time) {
vector<util::Timestamp> triggers = session_.get_triggers(current_segment_);
if (triggers.size() > 0)
zero_offset_ = triggers.front();
}
custom_zero_offset_set_ = false;
// Force an immediate update of the offsets
set_offset(offset_, true);
ruler_->update();
}
pv::util::Timestamp View::zero_offset() const
{
return zero_offset_;
}
int View::owner_visual_v_offset() const
{
return -scrollarea_->verticalScrollBar()->sliderPosition();
}
void View::set_v_offset(int offset)
{
scrollarea_->verticalScrollBar()->setSliderPosition(offset);
header_->update();
viewport_->update();
}
void View::set_h_offset(int offset)
{
scrollarea_->horizontalScrollBar()->setSliderPosition(offset);
header_->update();
viewport_->update();
}
int View::get_h_scrollbar_maximum() const
{
return scrollarea_->horizontalScrollBar()->maximum();
}
unsigned int View::depth() const
{
return 0;
}
uint32_t View::current_segment() const
{
return current_segment_;
}
pv::util::SIPrefix View::tick_prefix() const
{
return tick_prefix_;
}
void View::set_tick_prefix(pv::util::SIPrefix tick_prefix)
{
if (tick_prefix_ != tick_prefix) {
tick_prefix_ = tick_prefix;
tick_prefix_changed();
}
}
unsigned int View::tick_precision() const
{
return tick_precision_;
}
void View::set_tick_precision(unsigned tick_precision)
{
if (tick_precision_ != tick_precision) {
tick_precision_ = tick_precision;
tick_precision_changed();
}
}
const pv::util::Timestamp& View::tick_period() const
{
return tick_period_;
}
unsigned int View::minor_tick_count() const
{
return minor_tick_count_;
}
void View::set_tick_period(const pv::util::Timestamp& tick_period)
{
if (tick_period_ != tick_period) {
tick_period_ = tick_period;
tick_period_changed();
}
}
TimeUnit View::time_unit() const
{
return time_unit_;
}
void View::set_time_unit(pv::util::TimeUnit time_unit)
{
if (time_unit_ != time_unit) {
time_unit_ = time_unit;
time_unit_changed();
}
}
void View::set_current_segment(uint32_t segment_id)
{
current_segment_ = segment_id;
for (const shared_ptr<Signal>& signal : signals_)
signal->set_current_segment(current_segment_);
#ifdef ENABLE_DECODE
for (shared_ptr<DecodeTrace>& dt : decode_traces_)
dt->set_current_segment(current_segment_);
#endif
vector<util::Timestamp> triggers = session_.get_triggers(current_segment_);
trigger_markers_.clear();
for (util::Timestamp timestamp : triggers)
trigger_markers_.push_back(make_shared<TriggerMarker>(*this, timestamp));
if (!custom_zero_offset_set_)
reset_zero_position();
viewport_->update();
segment_changed(segment_id);
}
bool View::segment_is_selectable() const
{
return segment_selectable_;
}
Trace::SegmentDisplayMode View::segment_display_mode() const
{
return segment_display_mode_;
}
void View::set_segment_display_mode(Trace::SegmentDisplayMode mode)
{
segment_display_mode_ = mode;
for (const shared_ptr<Signal>& signal : signals_)
signal->set_segment_display_mode(mode);
uint32_t last_segment = session_.get_segment_count() - 1;
switch (mode) {
case Trace::ShowLastSegmentOnly:
if (current_segment_ != last_segment)
set_current_segment(last_segment);
break;
case Trace::ShowLastCompleteSegmentOnly:
// Do nothing if we only have one segment so far
if (last_segment > 0) {
// If the last segment isn't complete, the previous one must be
uint32_t segment_id =
(session_.all_segments_complete(last_segment)) ?
last_segment : last_segment - 1;
if (current_segment_ != segment_id)
set_current_segment(segment_id);
}
break;
case Trace::ShowSingleSegmentOnly:
case Trace::ShowAllSegments:
case Trace::ShowAccumulatedIntensity:
default:
// Current segment remains as-is
break;
}
segment_selectable_ = true;
if ((mode == Trace::ShowAllSegments) || (mode == Trace::ShowAccumulatedIntensity))
segment_selectable_ = false;
viewport_->update();
segment_display_mode_changed((int)mode, segment_selectable_);
}
void View::zoom(double steps)
{
zoom(steps, viewport_->width() / 2);
}
void View::zoom(double steps, int offset)
{
set_zoom(scale_ * pow(3.0 / 2.0, -steps), offset);
}
void View::zoom_fit(bool gui_state)
{
// Act as one-shot when stopped, toggle along with the GUI otherwise
if (session_.get_capture_state() == Session::Stopped) {
always_zoom_to_fit_ = false;
always_zoom_to_fit_changed(false);
} else {
always_zoom_to_fit_ = gui_state;
always_zoom_to_fit_changed(gui_state);
}
const pair<Timestamp, Timestamp> extents = get_time_extents();
const Timestamp delta = extents.second - extents.first;
if (delta < Timestamp("1e-12"))
return;
assert(viewport_);
const int w = viewport_->width();
if (w <= 0)
return;
const Timestamp scale = max(min(delta / w, MaxScale), MinScale);
set_scale_offset(scale.convert_to<double>(), extents.first);
}
void View::set_scale_offset(double scale, const Timestamp& offset)
{
// Disable sticky scrolling / always zoom to fit when acquisition runs
// and user drags the viewport
if ((scale_ == scale) && (offset_ != offset) &&
(session_.get_capture_state() == Session::Running)) {
if (sticky_scrolling_) {
sticky_scrolling_ = false;
sticky_scrolling_changed(false);
}
if (always_zoom_to_fit_) {
always_zoom_to_fit_ = false;
always_zoom_to_fit_changed(false);
}
}
set_scale(scale);
set_offset(offset);
calculate_tick_spacing();
update_scroll();
ruler_->update();
viewport_->update();
}
vector< shared_ptr<SignalData> > View::get_visible_data() const
{
// Make a set of all the visible data objects
vector< shared_ptr<SignalData> > visible_data;
for (const shared_ptr<Signal>& sig : signals_)
if (sig->enabled())
visible_data.push_back(sig->data());
return visible_data;
}
pair<Timestamp, Timestamp> View::get_time_extents() const
{
boost::optional<Timestamp> left_time, right_time;
vector< shared_ptr<SignalData> > data;
if (signals_.size() == 0)
return make_pair(0, 0);
data.push_back(signals_.front()->data());
for (const shared_ptr<SignalData>& d : data) {
const vector< shared_ptr<Segment> > segments = d->segments();
for (const shared_ptr<Segment>& s : segments) {
double samplerate = s->samplerate();
samplerate = (samplerate <= 0.0) ? 1.0 : samplerate;
const Timestamp start_time = s->start_time();
left_time = left_time ?
min(*left_time, start_time) :
start_time;
right_time = right_time ?
max(*right_time, start_time + d->max_sample_count() / samplerate) :
start_time + d->max_sample_count() / samplerate;
}
}
if (!left_time || !right_time)
return make_pair(0, 0);
assert(*left_time < *right_time);
return make_pair(*left_time, *right_time);
}
bool View::colored_bg() const
{
return colored_bg_;
}
bool View::cursors_shown() const
{
return show_cursors_;
}
void View::show_cursors(bool show)
{
if (show_cursors_ != show) {
show_cursors_ = show;
cursor_state_changed(show);
ruler_->update();
viewport_->update();
}
}
void View::set_cursors(pv::util::Timestamp& first, pv::util::Timestamp& second)
{
assert(cursors_);
cursors_->first()->set_time(first);
cursors_->second()->set_time(second);
ruler_->update();
viewport_->update();
}
void View::center_cursors()
{
assert(cursors_);
const double time_width = scale_ * viewport_->width();
cursors_->first()->set_time(offset_ + time_width * 0.4);
cursors_->second()->set_time(offset_ + time_width * 0.6);
ruler_->update();
viewport_->update();
}
shared_ptr<CursorPair> View::cursors() const
{
return cursors_;
}
shared_ptr<Flag> View::add_flag(const Timestamp& time)
{
shared_ptr<Flag> flag =
make_shared<Flag>(*this, time, QString("%1").arg(next_flag_text_));
flags_.push_back(flag);
next_flag_text_ = (next_flag_text_ >= 'Z') ? 'A' :
(next_flag_text_ + 1);
time_item_appearance_changed(true, true);
return flag;
}
void View::remove_flag(shared_ptr<Flag> flag)
{
flags_.remove(flag);
time_item_appearance_changed(true, true);
}
vector< shared_ptr<Flag> > View::flags() const
{
vector< shared_ptr<Flag> > flags(flags_.begin(), flags_.end());
stable_sort(flags.begin(), flags.end(),
[](const shared_ptr<Flag> &a, const shared_ptr<Flag> &b) {
return a->time() < b->time();
});
return flags;
}
const QPoint& View::hover_point() const
{
return hover_point_;
}
const QWidget* View::hover_widget() const
{
return hover_widget_;
}
int64_t View::get_nearest_level_change(const QPoint &p)
{
// Is snapping disabled?
if (snap_distance_ == 0)
return -1;
struct entry_t {
entry_t(shared_ptr<Signal> s) :
signal(s), delta(numeric_limits<int64_t>::max()), sample(-1), is_dense(false) {}
shared_ptr<Signal> signal;
int64_t delta;
int64_t sample;
bool is_dense;
};
vector<entry_t> list;
// Create list of signals to consider
if (signal_under_mouse_cursor_)
list.emplace_back(signal_under_mouse_cursor_);
else
for (shared_ptr<Signal> s : signals_) {
if (!s->enabled())
continue;
list.emplace_back(s);
}
// Get data for listed signals
for (entry_t &e : list) {
// Calculate sample number from cursor position
const double samples_per_pixel = e.signal->base()->get_samplerate() * scale();
const int64_t x_offset = offset().convert_to<double>() / scale();
const int64_t sample_num = max(((x_offset + p.x()) * samples_per_pixel), 0.0);
vector<data::LogicSegment::EdgePair> edges =
e.signal->get_nearest_level_changes(sample_num);
if (edges.empty())
continue;
// Check first edge
const int64_t first_sample_delta = abs(sample_num - edges.front().first);
const int64_t first_delta = first_sample_delta / samples_per_pixel;
e.delta = first_delta;
e.sample = edges.front().first;
// Check second edge if available
if (edges.size() == 2) {
// Note: -1 because this is usually the right edge and sample points are left-aligned
const int64_t second_sample_delta = abs(sample_num - edges.back().first - 1);
const int64_t second_delta = second_sample_delta / samples_per_pixel;
// If both edges are too close, we mark this signal as being dense
if ((first_delta + second_delta) <= snap_distance_)
e.is_dense = true;
if (second_delta < first_delta) {
e.delta = second_delta;
e.sample = edges.back().first;
}
}
}
// Look for the best match: non-dense first, then dense
entry_t *match = nullptr;
for (entry_t &e : list) {
if (e.delta > snap_distance_ || e.is_dense)
continue;
if (match) {
if (e.delta < match->delta)
match = &e;
} else
match = &e;
}
if (!match) {
for (entry_t &e : list) {
if (!e.is_dense)
continue;
if (match) {
if (e.delta < match->delta)
match = &e;
} else
match = &e;
}
}
if (match) {
// Somewhat ugly hack to make TimeItem::drag_by() work
signal_under_mouse_cursor_ = match->signal;
return match->sample;
}
return -1;
}
void View::restack_all_trace_tree_items()
{
// Make a list of owners that is sorted from deepest first
const vector<shared_ptr<TraceTreeItem>> items(
list_by_type<TraceTreeItem>());
set< TraceTreeItemOwner* > owners;
for (const auto &r : items)
owners.insert(r->owner());
vector< TraceTreeItemOwner* > sorted_owners(owners.begin(), owners.end());
sort(sorted_owners.begin(), sorted_owners.end(),
[](const TraceTreeItemOwner* a, const TraceTreeItemOwner *b) {
return a->depth() > b->depth(); });
// Restack the items recursively
for (auto &o : sorted_owners)
o->restack_items();
// Animate the items to their destination
for (const auto &i : items)
i->animate_to_layout_v_offset();
}
int View::header_width() const
{
return header_->extended_size_hint().width();
}
void View::on_setting_changed(const QString &key, const QVariant &value)
{
GlobalSettings settings;
if (key == GlobalSettings::Key_View_ColoredBG) {
colored_bg_ = settings.value(GlobalSettings::Key_View_ColoredBG).toBool();
viewport_->update();
}
if ((key == GlobalSettings::Key_View_ShowSamplingPoints) ||
(key == GlobalSettings::Key_View_ShowAnalogMinorGrid))
viewport_->update();
if (key == GlobalSettings::Key_View_TriggerIsZeroTime)
on_settingViewTriggerIsZeroTime_changed(value);
if (key == GlobalSettings::Key_View_SnapDistance)
snap_distance_ = settings.value(GlobalSettings::Key_View_SnapDistance).toInt();
}
void View::trigger_event(int segment_id, util::Timestamp location)
{
// TODO This doesn't work if we're showing multiple segments at once
if ((uint32_t)segment_id != current_segment_)
return;
if (!custom_zero_offset_set_)
reset_zero_position();
trigger_markers_.push_back(make_shared<TriggerMarker>(*this, location));
}
void View::get_scroll_layout(double &length, Timestamp &offset) const
{
const pair<Timestamp, Timestamp> extents = get_time_extents();
length = ((extents.second - extents.first) / scale_).convert_to<double>();
offset = offset_ / scale_;
}
void View::set_zoom(double scale, int offset)
{
// Reset the "always zoom to fit" feature as the user changed the zoom
always_zoom_to_fit_ = false;
always_zoom_to_fit_changed(false);
const Timestamp cursor_offset = offset_ + scale_ * offset;
const Timestamp new_scale = max(min(Timestamp(scale), MaxScale), MinScale);
const Timestamp new_offset = cursor_offset - new_scale * offset;
set_scale_offset(new_scale.convert_to<double>(), new_offset);
}
void View::calculate_tick_spacing()
{
const double SpacingIncrement = 10.0f;
const double MinValueSpacing = 40.0f;
// Figure out the highest numeric value visible on a label
const QSize areaSize = viewport_->size();
const Timestamp max_time = max(fabs(offset_),
fabs(offset_ + scale_ * areaSize.width()));
double min_width = SpacingIncrement;
double label_width, tick_period_width;
QFontMetrics m(QApplication::font());
// Copies of the member variables with the same name, used in the calculation
// and written back afterwards, so that we don't emit signals all the time
// during the calculation.
pv::util::Timestamp tick_period = tick_period_;
pv::util::SIPrefix tick_prefix = tick_prefix_;
unsigned tick_precision = tick_precision_;
do {
const double min_period = scale_ * min_width;
const int order = (int)floorf(log10f(min_period));
const pv::util::Timestamp order_decimal =
pow(pv::util::Timestamp(10), order);
// Allow for a margin of error so that a scale unit of 1 can be used.
// Otherwise, for a SU of 1 the tick period will almost always be below
// the min_period by a small amount - and thus skipped in favor of 2.
// Note: margin assumes that SU[0] and SU[1] contain the smallest values
double tp_margin = (ScaleUnits[0] + ScaleUnits[1]) / 2.0;
double tp_with_margin;
unsigned int unit = 0;
do {
tp_with_margin = order_decimal.convert_to<double>() *
(ScaleUnits[unit++] + tp_margin);
} while (tp_with_margin < min_period && unit < countof(ScaleUnits));
minor_tick_count_ = (unit == 2) ? 4 : 5;
tick_period = order_decimal * ScaleUnits[unit - 1];
tick_prefix = static_cast<pv::util::SIPrefix>(
(order - pv::util::exponent(pv::util::SIPrefix::yocto)) / 3);
// Precision is the number of fractional digits required, not
// taking the prefix into account (and it must never be negative)
tick_precision = max(ceil(log10(1 / tick_period)).convert_to<int>(), 0);
tick_period_width = (tick_period / scale_).convert_to<double>();
const QString label_text = Ruler::format_time_with_distance(
tick_period, max_time, tick_prefix, time_unit_, tick_precision);
label_width = m.boundingRect(0, 0, INT_MAX, INT_MAX,
Qt::AlignLeft | Qt::AlignTop, label_text).width() +
MinValueSpacing;
min_width += SpacingIncrement;
} while (tick_period_width < label_width);
set_tick_period(tick_period);
set_tick_prefix(tick_prefix);
set_tick_precision(tick_precision);
}
void View::adjust_top_margin()
{
assert(viewport_);
const QSize areaSize = viewport_->size();
const pair<int, int> extents = v_extents();
const int top_margin = owner_visual_v_offset() + extents.first;
const int trace_bottom = owner_visual_v_offset() + extents.first + extents.second;
// Do we have empty space at the top while the last trace goes out of screen?
if ((top_margin > 0) && (trace_bottom > areaSize.height())) {
const int trace_height = extents.second - extents.first;
// Center everything vertically if there is enough space
if (areaSize.height() >= trace_height)
set_v_offset(extents.first -
((areaSize.height() - trace_height) / 2));
else
// Remove the top margin to make as many traces fit on screen as possible
set_v_offset(extents.first);
}
}
void View::update_scroll()
{
assert(viewport_);
QScrollBar *hscrollbar = scrollarea_->horizontalScrollBar();
QScrollBar *vscrollbar = scrollarea_->verticalScrollBar();
const QSize areaSize = viewport_->size();
// Set the horizontal scroll bar
double length = 0;
Timestamp offset;
get_scroll_layout(length, offset);
length = max(length - areaSize.width(), 0.0);
int major_tick_distance = (tick_period_ / scale_).convert_to<int>();
hscrollbar->setPageStep(areaSize.width() / 2);
hscrollbar->setSingleStep(major_tick_distance);
updating_scroll_ = true;
if (length < MaxScrollValue) {
hscrollbar->setRange(0, length);
hscrollbar->setSliderPosition(offset.convert_to<double>());
} else {
hscrollbar->setRange(0, MaxScrollValue);
hscrollbar->setSliderPosition(
(offset_ * MaxScrollValue / (scale_ * length)).convert_to<double>());
}
updating_scroll_ = false;
// Set the vertical scrollbar
vscrollbar->setPageStep(areaSize.height());
vscrollbar->setSingleStep(areaSize.height() / 8);
const pair<int, int> extents = v_extents();
// Don't change the scrollbar range if there are no traces
if (extents.first != extents.second) {
int top_margin = ViewScrollMargin;
int btm_margin = ViewScrollMargin;
vscrollbar->setRange(extents.first - areaSize.height() + top_margin,
extents.second - btm_margin);
}
if (scroll_needs_defaults_) {
set_scroll_default();
scroll_needs_defaults_ = false;
}
}
void View::reset_scroll()
{
scrollarea_->verticalScrollBar()->setRange(0, 0);
}
void View::set_scroll_default()
{
assert(viewport_);
const QSize areaSize = viewport_->size();
const pair<int, int> extents = v_extents();
const int trace_height = extents.second - extents.first;
// Do all traces fit in the view?
if (areaSize.height() >= trace_height)
// Center all traces vertically
set_v_offset(extents.first -
((areaSize.height() - trace_height) / 2));
else
// Put the first trace at the top, letting the bottom ones overflow
set_v_offset(extents.first);
}
void View::determine_if_header_was_shrunk()
{
const int header_pane_width =
splitter_->sizes().front(); // clazy:exclude=detaching-temporary
// Allow for a slight margin of error so that we also accept
// slight differences when e.g. a label name change increased
// the overall width
header_was_shrunk_ = (header_pane_width < (header_width() - 10));
}
void View::resize_header_to_fit()
{
// Setting the maximum width of the header widget doesn't work as
// expected because the splitter would allow the user to make the
// pane wider than that, creating empty space as a result.
// To make this work, we stricly enforce the maximum width by
// expanding the header unless the user shrunk it on purpose.
// As we're then setting the width of the header pane, we set the
// splitter to the maximum allowed position.
int splitter_area_width = 0;
for (int w : splitter_->sizes()) // clazy:exclude=range-loop
splitter_area_width += w;
// Make sure the header has enough horizontal space to show all labels fully
QList<int> pane_sizes;
pane_sizes.push_back(header_->extended_size_hint().width());
pane_sizes.push_back(splitter_area_width - header_->extended_size_hint().width());
splitter_->setSizes(pane_sizes);
}
void View::update_layout()
{
update_scroll();
}
TraceTreeItemOwner* View::find_prevalent_trace_group(
const shared_ptr<sigrok::ChannelGroup> &group,
const unordered_map<shared_ptr<data::SignalBase>, shared_ptr<Signal> >
&signal_map)
{
assert(group);
unordered_set<TraceTreeItemOwner*> owners;
vector<TraceTreeItemOwner*> owner_list;
// Make a set and a list of all the owners
for (const auto& channel : group->channels()) {
for (auto& entry : signal_map) {
if (entry.first->channel() == channel) {
TraceTreeItemOwner *const o = (entry.second)->owner();
owner_list.push_back(o);
owners.insert(o);
}
}
}
// Iterate through the list of owners, and find the most prevalent
size_t max_prevalence = 0;
TraceTreeItemOwner *prevalent_owner = nullptr;
for (TraceTreeItemOwner *owner : owners) {
const size_t prevalence = count_if(
owner_list.begin(), owner_list.end(),
[&](TraceTreeItemOwner *o) { return o == owner; });
if (prevalence > max_prevalence) {
max_prevalence = prevalence;
prevalent_owner = owner;
}
}
return prevalent_owner;
}
vector< shared_ptr<Trace> > View::extract_new_traces_for_channels(
const vector< shared_ptr<sigrok::Channel> > &channels,
const unordered_map<shared_ptr<data::SignalBase>, shared_ptr<Signal> >
&signal_map,
set< shared_ptr<Trace> > &add_list)
{
vector< shared_ptr<Trace> > filtered_traces;
for (const auto& channel : channels) {
for (auto& entry : signal_map) {
if (entry.first->channel() == channel) {
shared_ptr<Trace> trace = entry.second;
const auto list_iter = add_list.find(trace);
if (list_iter == add_list.end())
continue;
filtered_traces.push_back(trace);
add_list.erase(list_iter);
}
}
}
return filtered_traces;
}
void View::determine_time_unit()
{
// Check whether we know the sample rate and hence can use time as the unit
if (time_unit_ == util::TimeUnit::Samples) {
// Check all signals but...
for (const shared_ptr<Signal>& signal : signals_) {
const shared_ptr<SignalData> data = signal->data();
// ...only check first segment of each
const vector< shared_ptr<Segment> > segments = data->segments();
if (!segments.empty())
if (segments[0]->samplerate()) {
set_time_unit(util::TimeUnit::Time);
break;
}
}
}
}
bool View::eventFilter(QObject *object, QEvent *event)
{
const QEvent::Type type = event->type();
if (type == QEvent::MouseMove) {
if (object)
hover_widget_ = qobject_cast<QWidget*>(object);
const QMouseEvent *const mouse_event = (QMouseEvent*)event;
if (object == viewport_)
hover_point_ = mouse_event->pos();
else if (object == ruler_)
hover_point_ = mouse_event->pos();
else if (object == header_)
hover_point_ = QPoint(0, mouse_event->y());
else
hover_point_ = QPoint(-1, -1);
update_hover_point();
if (grabbed_widget_) {
int64_t nearest = get_nearest_level_change(hover_point_);
pv::util::Timestamp mouse_time = offset_ + hover_point_.x() * scale_;
if (nearest == -1) {
grabbed_widget_->set_time(mouse_time);
} else {
grabbed_widget_->set_time(nearest / get_signal_under_mouse_cursor()->base()->get_samplerate());
}
}
} else if (type == QEvent::MouseButtonPress) {
grabbed_widget_ = nullptr;
const QMouseEvent *const mouse_event = (QMouseEvent*)event;
if ((object == viewport_) && (mouse_event->button() & Qt::LeftButton)) {
// Send event to all trace tree items
const vector<shared_ptr<TraceTreeItem>> trace_tree_items(
list_by_type<TraceTreeItem>());
for (const shared_ptr<TraceTreeItem>& r : trace_tree_items)
r->mouse_left_press_event(mouse_event);
}
} else if (type == QEvent::Leave) {
hover_point_ = QPoint(-1, -1);
update_hover_point();
} else if (type == QEvent::Show) {
// This is somewhat of a hack, unfortunately. We cannot use
// set_v_offset() from within restore_settings() as the view
// at that point is neither visible nor properly sized.
// This is the least intrusive workaround I could come up
// with: set the vertical offset (or scroll defaults) when
// the view is shown, which happens after all widgets were
// resized to their final sizes.
update_layout();
if (settings_restored_)
determine_if_header_was_shrunk();
else
resize_header_to_fit();
if (scroll_needs_defaults_) {
set_scroll_default();
scroll_needs_defaults_ = false;
}
if (saved_v_offset_) {
set_v_offset(saved_v_offset_);
saved_v_offset_ = 0;
}
}
return QObject::eventFilter(object, event);
}
void View::contextMenuEvent(QContextMenuEvent *event)
{
QPoint pos = event->pos() - QPoint(0, ruler_->sizeHint().height());
const shared_ptr<ViewItem> r = viewport_->get_mouse_over_item(pos);
if (!r)
return;
QMenu *menu = r->create_view_context_menu(this, pos);
if (menu)
menu->popup(event->globalPos());
}
void View::resizeEvent(QResizeEvent* event)
{
// Only adjust the top margin if we shrunk vertically
if (event->size().height() < event->oldSize().height())
adjust_top_margin();
update_layout();
}
void View::update_hover_point()
{
// Determine signal that the mouse cursor is hovering over
signal_under_mouse_cursor_.reset();
if (hover_widget_ == this) {
for (const shared_ptr<Signal>& s : signals_) {
const pair<int, int> extents = s->v_extents();
const int top = s->get_visual_y() + extents.first;
const int btm = s->get_visual_y() + extents.second;
if ((hover_point_.y() >= top) && (hover_point_.y() <= btm)
&& s->base()->enabled())
signal_under_mouse_cursor_ = s;
}
}
// Update all trace tree items
const vector<shared_ptr<TraceTreeItem>> trace_tree_items(
list_by_type<TraceTreeItem>());
for (const shared_ptr<TraceTreeItem>& r : trace_tree_items)
r->hover_point_changed(hover_point_);
// Notify any other listeners
hover_point_changed(hover_widget_, hover_point_);
}
void View::row_item_appearance_changed(bool label, bool content)
{
if (label)
header_->update();
if (content)
viewport_->update();
}
void View::time_item_appearance_changed(bool label, bool content)
{
if (label) {
ruler_->update();
// Make sure the header pane width is updated, too
update_layout();
}
if (content)
viewport_->update();
}
void View::extents_changed(bool horz, bool vert)
{
sticky_events_ |=
(horz ? TraceTreeItemHExtentsChanged : 0) |
(vert ? TraceTreeItemVExtentsChanged : 0);
if (!lazy_event_handler_.isActive())
lazy_event_handler_.start();
}
void View::on_signal_name_changed()
{
if (!header_was_shrunk_)
resize_header_to_fit();
}
void View::on_splitter_moved()
{
// The header can only shrink when the splitter is moved manually
determine_if_header_was_shrunk();
if (!header_was_shrunk_)
resize_header_to_fit();
}
void View::on_zoom_in_shortcut_triggered()
{
zoom(1);
}
void View::on_zoom_out_shortcut_triggered()
{
zoom(-1);
}
void View::on_scroll_to_start_shortcut_triggered()
{
set_h_offset(0);
}
void View::on_scroll_to_end_shortcut_triggered()
{
set_h_offset(get_h_scrollbar_maximum());
}
void View::h_scroll_value_changed(int value)
{
if (updating_scroll_)
return;
// Disable sticky scrolling when user moves the horizontal scroll bar
// during a running acquisition
if (sticky_scrolling_ && (session_.get_capture_state() == Session::Running)) {
sticky_scrolling_ = false;
sticky_scrolling_changed(false);
}
const int range = scrollarea_->horizontalScrollBar()->maximum();
if (range < MaxScrollValue)
set_offset(scale_ * value);
else {
double length = 0;
Timestamp offset;
get_scroll_layout(length, offset);
set_offset(scale_ * length * value / MaxScrollValue);
}
ruler_->update();
viewport_->update();
}
void View::v_scroll_value_changed()
{
header_->update();
viewport_->update();
}
void View::on_grab_ruler(int ruler_id)
{
if (!cursors_shown()) {
center_cursors();
show_cursors();
}
// Release the grabbed widget if its trigger hotkey was pressed twice
if (ruler_id == 1)
grabbed_widget_ = (grabbed_widget_ == cursors_->first().get()) ?
nullptr : cursors_->first().get();
else
grabbed_widget_ = (grabbed_widget_ == cursors_->second().get()) ?
nullptr : cursors_->second().get();
if (grabbed_widget_)
grabbed_widget_->set_time(ruler_->get_absolute_time_from_x_pos(
mapFromGlobal(QCursor::pos()).x() - header_width()));
}
void View::signals_changed()
{
using sigrok::Channel;
vector< shared_ptr<Channel> > channels;
shared_ptr<sigrok::Device> sr_dev;
bool signals_added_or_removed = false;
// Do we need to set the vertical scrollbar to its default position later?
// We do if there are no traces, i.e. the scroll bar has no range set
bool reset_scrollbar =
(scrollarea_->verticalScrollBar()->minimum() ==
scrollarea_->verticalScrollBar()->maximum());
if (!session_.device()) {
reset_scroll();
signals_.clear();
} else {
sr_dev = session_.device()->device();
assert(sr_dev);
channels = sr_dev->channels();
}
vector< shared_ptr<TraceTreeItem> > new_top_level_items;
// Make a list of traces that are being added, and a list of traces
// that are being removed
const vector<shared_ptr<Trace>> prev_trace_list = list_by_type<Trace>();
const set<shared_ptr<Trace>> prev_traces(
prev_trace_list.begin(), prev_trace_list.end());
set< shared_ptr<Trace> > traces(signals_.begin(), signals_.end());
#ifdef ENABLE_DECODE
traces.insert(decode_traces_.begin(), decode_traces_.end());
#endif
set< shared_ptr<Trace> > add_traces;
set_difference(traces.begin(), traces.end(),
prev_traces.begin(), prev_traces.end(),
inserter(add_traces, add_traces.begin()));
set< shared_ptr<Trace> > remove_traces;
set_difference(prev_traces.begin(), prev_traces.end(),
traces.begin(), traces.end(),
inserter(remove_traces, remove_traces.begin()));
// Make a look-up table of sigrok Channels to pulseview Signals
unordered_map<shared_ptr<data::SignalBase>, shared_ptr<Signal> >
signal_map;
for (const shared_ptr<Signal>& sig : signals_)
signal_map[sig->base()] = sig;
// Populate channel groups
if (sr_dev)
for (auto& entry : sr_dev->channel_groups()) {
const shared_ptr<sigrok::ChannelGroup> &group = entry.second;
if (group->channels().size() <= 1)
continue;
// Find best trace group to add to
TraceTreeItemOwner *owner = find_prevalent_trace_group(
group, signal_map);
// If there is no trace group, create one
shared_ptr<TraceGroup> new_trace_group;
if (!owner) {
new_trace_group.reset(new TraceGroup());
owner = new_trace_group.get();
}
// Extract traces for the trace group, removing them from
// the add list
const vector< shared_ptr<Trace> > new_traces_in_group =
extract_new_traces_for_channels(group->channels(),
signal_map, add_traces);
// Add the traces to the group
const pair<int, int> prev_v_extents = owner->v_extents();
int offset = prev_v_extents.second - prev_v_extents.first;
for (const shared_ptr<Trace>& trace : new_traces_in_group) {
assert(trace);
owner->add_child_item(trace);
const pair<int, int> extents = trace->v_extents();
if (trace->enabled())
offset += -extents.first;
trace->force_to_v_offset(offset);
if (trace->enabled())
offset += extents.second;
}
if (new_trace_group) {
// Assign proper vertical offsets to each channel in the group
new_trace_group->restack_items();
// If this is a new group, enqueue it in the new top level
// items list
if (!new_traces_in_group.empty())
new_top_level_items.push_back(new_trace_group);
}
}
// Enqueue the remaining logic channels in a group
vector< shared_ptr<Channel> > logic_channels;
copy_if(channels.begin(), channels.end(), back_inserter(logic_channels),
[](const shared_ptr<Channel>& c) {
return c->type() == sigrok::ChannelType::LOGIC; });
const vector< shared_ptr<Trace> > non_grouped_logic_signals =
extract_new_traces_for_channels(logic_channels, signal_map, add_traces);
if (non_grouped_logic_signals.size() > 0) {
const shared_ptr<TraceGroup> non_grouped_trace_group(
make_shared<TraceGroup>());
for (const shared_ptr<Trace>& trace : non_grouped_logic_signals)
non_grouped_trace_group->add_child_item(trace);
non_grouped_trace_group->restack_items();
new_top_level_items.push_back(non_grouped_trace_group);
}
// Enqueue the remaining channels as free ungrouped traces
const vector< shared_ptr<Trace> > new_top_level_signals =
extract_new_traces_for_channels(channels, signal_map, add_traces);
new_top_level_items.insert(new_top_level_items.end(),
new_top_level_signals.begin(), new_top_level_signals.end());
// Enqueue any remaining traces i.e. decode traces
new_top_level_items.insert(new_top_level_items.end(),
add_traces.begin(), add_traces.end());
// Remove any removed traces
for (const shared_ptr<Trace>& trace : remove_traces) {
TraceTreeItemOwner *const owner = trace->owner();
assert(owner);
owner->remove_child_item(trace);
signals_added_or_removed = true;
}
// Remove any empty trace groups
for (shared_ptr<TraceGroup> group : list_by_type<TraceGroup>())
if (group->child_items().size() == 0) {
remove_child_item(group);
group.reset();
}
// Add and position the pending top levels items
int offset = v_extents().second;
for (auto item : new_top_level_items) {
add_child_item(item);
// Position the item after the last item or at the top if there is none
const pair<int, int> extents = item->v_extents();
if (item->enabled())
offset += -extents.first;
item->force_to_v_offset(offset);
if (item->enabled())
offset += extents.second;
signals_added_or_removed = true;
}
if (signals_added_or_removed && !header_was_shrunk_)
resize_header_to_fit();
update_layout();
header_->update();
viewport_->update();
if (reset_scrollbar)
set_scroll_default();
}
void View::capture_state_updated(int state)
{
GlobalSettings settings;
if (state == Session::Running) {
set_time_unit(util::TimeUnit::Samples);
trigger_markers_.clear();
if (!custom_zero_offset_set_)
set_zero_position(0);
scale_at_acq_start_ = scale_;
offset_at_acq_start_ = offset_;
// Activate "always zoom to fit" if the setting is enabled and we're
// the main view of this session (other trace views may be used for
// zooming and we don't want to mess them up)
bool state = settings.value(GlobalSettings::Key_View_ZoomToFitDuringAcq).toBool();
if (is_main_view_ && state) {
always_zoom_to_fit_ = true;
always_zoom_to_fit_changed(always_zoom_to_fit_);
}
// Enable sticky scrolling if the setting is enabled
sticky_scrolling_ = settings.value(GlobalSettings::Key_View_StickyScrolling).toBool();
// Reset all traces to segment 0
current_segment_ = 0;
set_current_segment(current_segment_);
}
if (state == Session::Stopped) {
// After acquisition has stopped we need to re-calculate the ticks once
// as it's otherwise done when the user pans or zooms, which is too late
calculate_tick_spacing();
// Reset "always zoom to fit", the acquisition has stopped
if (always_zoom_to_fit_) {
// Perform a final zoom-to-fit before disabling
zoom_fit(always_zoom_to_fit_);
always_zoom_to_fit_ = false;
always_zoom_to_fit_changed(always_zoom_to_fit_);
}
bool zoom_to_fit_after_acq =
settings.value(GlobalSettings::Key_View_ZoomToFitAfterAcq).toBool();
// Only perform zoom-to-fit if the user hasn't altered the viewport and
// we didn't restore settings in the meanwhile
if (zoom_to_fit_after_acq &&
!suppress_zoom_to_fit_after_acq_ &&
(scale_ == scale_at_acq_start_) &&
(offset_ == offset_at_acq_start_))
zoom_fit(false); // We're stopped, so the GUI state doesn't matter
suppress_zoom_to_fit_after_acq_ = false;
}
}
void View::on_new_segment(int new_segment_id)
{
on_segment_changed(new_segment_id);
}
void View::on_segment_completed(int segment_id)
{
on_segment_changed(segment_id);
}
void View::on_segment_changed(int segment)
{
switch (segment_display_mode_) {
case Trace::ShowLastSegmentOnly:
case Trace::ShowSingleSegmentOnly:
set_current_segment(segment);
break;
case Trace::ShowLastCompleteSegmentOnly:
// Only update if all segments are complete
if (session_.all_segments_complete(segment))
set_current_segment(segment);
break;
case Trace::ShowAllSegments:
case Trace::ShowAccumulatedIntensity:
default:
break;
}
}
void View::on_settingViewTriggerIsZeroTime_changed(const QVariant new_value)
{
(void)new_value;
if (!custom_zero_offset_set_)
reset_zero_position();
}
void View::perform_delayed_view_update()
{
if (always_zoom_to_fit_) {
zoom_fit(true);
} else if (sticky_scrolling_) {
// Make right side of the view sticky
double length = 0;
Timestamp offset;
get_scroll_layout(length, offset);
const QSize areaSize = viewport_->size();
length = max(length - areaSize.width(), 0.0);
set_offset(scale_ * length);
}
determine_time_unit();
update_scroll();
ruler_->update();
viewport_->update();
}
void View::process_sticky_events()
{
if (sticky_events_ & TraceTreeItemHExtentsChanged)
update_layout();
if (sticky_events_ & TraceTreeItemVExtentsChanged) {
restack_all_trace_tree_items();
update_scroll();
}
// Clear the sticky events
sticky_events_ = 0;
}
} // namespace trace
} // namespace views
} // namespace pv
|