Files
        @ 48e24c542d4e
    
        
              Branch filter: 
        
    Location: tempo-plotter/src/mainwindow.cpp - annotation
        
            
            48e24c542d4e
            19.3 KiB
            text/x-c++hdr
        
        
    
    Change binary name
    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  | bdc33b5c53c6 8d241dce8310 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 52acb9a759b7 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 c1f2f6471ae5 c6761f5a99cb bdc33b5c53c6 b5af7d81e1f0 b5af7d81e1f0 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 6862f9405cdb bdc33b5c53c6 bdc33b5c53c6 a035648d9d3b bdc33b5c53c6 797666010f26 bdc33b5c53c6 c1f2f6471ae5 bdc33b5c53c6 6289addb553d bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 d19fdcb6a391 c6761f5a99cb 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 c6761f5a99cb c6761f5a99cb bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 a035648d9d3b 68f79a7c6762 bdc33b5c53c6 68f79a7c6762 68f79a7c6762 8d241dce8310 335ac381f2eb bdc33b5c53c6 bdc33b5c53c6 5a4e6dd9042e 68f79a7c6762 5a4e6dd9042e 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 bdc33b5c53c6 1f184be53ade 1f184be53ade ab6bc6d4e2f6 bdc33b5c53c6 bdc33b5c53c6 90cee575170f 90cee575170f bdc33b5c53c6 cd8c3e8601e0 cd8c3e8601e0 cd8c3e8601e0 e246af74222f cd8c3e8601e0 cd8c3e8601e0 1f184be53ade 1f184be53ade 1f184be53ade bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 90cee575170f 90cee575170f 90cee575170f bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 16cb36cb4df0 16cb36cb4df0 5a5543df77a6 5a5543df77a6 5a5543df77a6 5a5543df77a6 5a5543df77a6 bdc33b5c53c6 bdc33b5c53c6 a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b 16cb36cb4df0 5a5543df77a6 5a5543df77a6 5a5543df77a6 5a5543df77a6 5a5543df77a6 5a5543df77a6 5a5543df77a6 5a5543df77a6 5a5543df77a6 5a5543df77a6 5a5543df77a6 5a5543df77a6 a6437a54c018 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 c1f2f6471ae5 c1f2f6471ae5 c1f2f6471ae5 a6437a54c018 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 a6437a54c018 a6437a54c018 a6437a54c018 a6437a54c018 a6437a54c018 a6437a54c018 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 4126c800559d 1e14c645a35b 1e14c645a35b bdc33b5c53c6 f641cc1fcdd8 5a149e786568 f641cc1fcdd8 bfd45d598368 da58111e7ca9 bdc33b5c53c6 4126c800559d f7670384fcb0 f7670384fcb0 f7670384fcb0 4126c800559d 4126c800559d f102fddaeb9f f102fddaeb9f f102fddaeb9f a035648d9d3b bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 2f04c80c72e3 bdc33b5c53c6 bdc33b5c53c6 68f79a7c6762 e0c6c43cbcf5 e0c6c43cbcf5 e0c6c43cbcf5 e0c6c43cbcf5 e0c6c43cbcf5 e0c6c43cbcf5 e0c6c43cbcf5 e0c6c43cbcf5 e0c6c43cbcf5 e0c6c43cbcf5 e0c6c43cbcf5 e0c6c43cbcf5 e0c6c43cbcf5 e0c6c43cbcf5 e0c6c43cbcf5 e0c6c43cbcf5 e0c6c43cbcf5 e0c6c43cbcf5 e0c6c43cbcf5 e0c6c43cbcf5 e0c6c43cbcf5 e0c6c43cbcf5 bdc33b5c53c6 1bdd2654701b 1bdd2654701b 1bdd2654701b d92b6daade5d 1e14c645a35b 68f79a7c6762 68f79a7c6762 bdc33b5c53c6 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 2b698ce67e65 f7670384fcb0 f7670384fcb0 f7670384fcb0 da58111e7ca9 da58111e7ca9 2b698ce67e65 2b698ce67e65 5a149e786568 f102fddaeb9f bdc33b5c53c6 a9a5e414ab54 a9a5e414ab54 a9a5e414ab54 bdc33b5c53c6 bdc33b5c53c6 a9a5e414ab54 bdc33b5c53c6 5faf2cdfb6a5 5faf2cdfb6a5 bdc33b5c53c6 bd0977f58c60 bd0977f58c60 bd0977f58c60 bd0977f58c60 bd0977f58c60 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bd649e494eb1 bd649e494eb1 f25223973c55 d92b6daade5d d92b6daade5d 5faf2cdfb6a5 5faf2cdfb6a5 d92b6daade5d f25223973c55 6862f9405cdb 663a37aabb71 58fcfc14522e 6862f9405cdb 6862f9405cdb 58fcfc14522e 58fcfc14522e 58fcfc14522e 58fcfc14522e 58fcfc14522e e81770084e42 e81770084e42 e81770084e42 e81770084e42 e81770084e42 e81770084e42 e81770084e42 e246af74222f e81770084e42 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 a0f5016bf36d a0f5016bf36d bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 52acb9a759b7 52acb9a759b7 a0764458f35e 52acb9a759b7 52acb9a759b7 52acb9a759b7 16cb36cb4df0 52acb9a759b7 ac878c0193ff 52acb9a759b7 52acb9a759b7 52acb9a759b7 52acb9a759b7 52acb9a759b7 52acb9a759b7 a0764458f35e a0764458f35e 6862f9405cdb a0764458f35e a0764458f35e a0764458f35e a0764458f35e a0764458f35e a0764458f35e a0764458f35e a0764458f35e a0764458f35e a0764458f35e 16cb36cb4df0 a0764458f35e a0764458f35e a0764458f35e a0764458f35e a0764458f35e 16cb36cb4df0 a0764458f35e a0764458f35e a0764458f35e a0764458f35e a0764458f35e a0764458f35e a0764458f35e a0764458f35e a0764458f35e a0764458f35e a0764458f35e a0764458f35e a0764458f35e a0764458f35e a0764458f35e 52acb9a759b7 52acb9a759b7 52acb9a759b7 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 a9a5e414ab54 335ac381f2eb a9a5e414ab54 a9a5e414ab54 a9a5e414ab54 bdc33b5c53c6 bdc33b5c53c6 5faf2cdfb6a5 d92b6daade5d d92b6daade5d 5faf2cdfb6a5 d92b6daade5d d92b6daade5d bdc33b5c53c6 bdc33b5c53c6 68f79a7c6762 42da1825ce45 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 68f79a7c6762 42da1825ce45 1a05d0ad46b0 1a05d0ad46b0 5faf2cdfb6a5 bdc33b5c53c6 5faf2cdfb6a5 5faf2cdfb6a5 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b 557e95e17a97 557e95e17a97 a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b 68f79a7c6762 a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b a035648d9d3b bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 7f2c4af67ad6 7f2c4af67ad6 7f2c4af67ad6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 7a38fc903af3 7a38fc903af3 90cee575170f 7a38fc903af3 7a38fc903af3 bdc33b5c53c6 1e3fe1bbdd01 bdc33b5c53c6 bdc33b5c53c6 1e3fe1bbdd01 1e3fe1bbdd01 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 bdc33b5c53c6 f25223973c55 663a37aabb71 663a37aabb71 663a37aabb71 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 663a37aabb71 663a37aabb71 663a37aabb71 663a37aabb71 663a37aabb71 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 16cb36cb4df0 663a37aabb71 663a37aabb71 663a37aabb71 f25223973c55 f25223973c55 6289addb553d 6289addb553d 6289addb553d c6761f5a99cb c6761f5a99cb c6761f5a99cb c2fad2d38a60 c2fad2d38a60 c2fad2d38a60 6a0a653b5b34 6a0a653b5b34 6a0a653b5b34 1f184be53ade 1f184be53ade f25223973c55 f25223973c55 f25223973c55 663a37aabb71 f25223973c55 c6761f5a99cb f25223973c55 6289addb553d 6289addb553d c6761f5a99cb c6761f5a99cb c6761f5a99cb c6761f5a99cb c6761f5a99cb c6761f5a99cb c6761f5a99cb c2fad2d38a60 c2fad2d38a60 c2fad2d38a60 c2fad2d38a60 c2fad2d38a60 6a0a653b5b34 6a0a653b5b34 6a0a653b5b34 6a0a653b5b34 6a0a653b5b34 6a0a653b5b34 1f184be53ade 1f184be53ade 1f184be53ade 1f184be53ade f25223973c55 f25223973c55 a6437a54c018 a6437a54c018 a6437a54c018 a6437a54c018 a6437a54c018 a6437a54c018 a6437a54c018 a6437a54c018 a6437a54c018 663a37aabb71 a6437a54c018 a6437a54c018 a6437a54c018 a6437a54c018 a6437a54c018 a6437a54c018 a6437a54c018 a6437a54c018 a6437a54c018 a6437a54c018 a6437a54c018 663a37aabb71 a6437a54c018 a6437a54c018 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 6862f9405cdb b5af7d81e1f0 9b75748e06be b5af7d81e1f0 b5af7d81e1f0 6862f9405cdb b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 6862f9405cdb b5af7d81e1f0 6862f9405cdb b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 6862f9405cdb 6862f9405cdb b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0 b5af7d81e1f0  | /*
  Copyright © 2019 Hasan Yavuz Özderya
  This file is part of serialplot.
  serialplot 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 3 of the License, or
  (at your option) any later version.
  serialplot 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 serialplot.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QByteArray>
#include <QApplication>
#include <QFileDialog>
#include <QMessageBox>
#include <QFile>
#include <QTextStream>
#include <QMenu>
#include <QDesktopServices>
#include <QMap>
#include <QtDebug>
#include <QCommandLineParser>
#include <QFileInfo>
#include <qwt_plot.h>
#include <limits.h>
#include <cmath>
#include <iostream>
#include <cstdlib>
#include <plot.h>
#include <barplot.h>
#include "framebufferseries.h"
#include "utils.h"
#include "defines.h"
#include "version.h"
#include "setting_defines.h"
#if defined(Q_OS_WIN) && defined(QT_STATIC)
#include <QtPlugin>
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
#endif
// TODO: depends on tab insertion order, a better solution would be to use object names
const QMap<int, QString> panelSettingMap({
       // {0, "Port"},
       // {0, "DataFormat"},
        {0, "Plot"},
        {1, "Commands"},
        {2, "Record"},
        //{3, "TextView"},
        {3, "Log"}
    });
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    aboutDialog(this),
    portControl(&serialPort),
    secondaryPlot(NULL),
    snapshotMan(this, &stream),
    commandPanel(&serialPort),
    dataFormatPanel(&serialPort),
    recordPanel(&stream),
    textView(&stream),
    bpsLabel(&portControl, &dataFormatPanel, this)
{
    ui->setupUi(this);
    plotMan = new PlotManager(ui->plotArea, &plotMenu, &stream);
//    ui->tabWidget->insertTab(0, &portControl, "Port");
//    ui->tabWidget->insertTab(0, &dataFormatPanel, "Data Format");
    ui->tabWidget->insertTab(0, &plotControlPanel, "Plot");
    ui->tabWidget->insertTab(1, &commandPanel, "Commands");
    ui->tabWidget->insertTab(2, &recordPanel, "Record");
//    ui->tabWidget->insertTab(3, &textView, "Text View");
    ui->tabWidget->setCurrentIndex(0);
    auto tbPortControl = portControl.toolBar();
    addToolBar(tbPortControl);
    addToolBar(recordPanel.toolbar());
    ui->plotToolBar->addAction(snapshotMan.takeSnapshotAction());
    menuBar()->insertMenu(ui->menuHelp->menuAction(), snapshotMan.menu());
    menuBar()->insertMenu(ui->menuHelp->menuAction(), commandPanel.menu());
    connect(&commandPanel, &CommandPanel::focusRequested, [this]()
            {
                this->ui->tabWidget->setCurrentWidget(&commandPanel);
                this->ui->tabWidget->showTabs();
            });
    tbPortControl->setObjectName("tbPortControl");
    ui->plotToolBar->setObjectName("tbPlot");
    setupAboutDialog();
    // init view menu
    ui->menuBar->insertMenu(ui->menuSecondary->menuAction(), &plotMenu);
    plotMenu.addSeparator();
    QMenu* tbMenu = plotMenu.addMenu("Toolbars");
    tbMenu->addAction(ui->plotToolBar->toggleViewAction());
    tbMenu->addAction(portControl.toolBar()->toggleViewAction());
    // init secondary plot menu
    auto group = new QActionGroup(this);
    group->addAction(ui->actionVertical);
    group->addAction(ui->actionHorizontal);
    // init UI signals
    // Secondary plot menu signals
    connect(ui->actionBarPlot, &QAction::triggered,
            this, &MainWindow::showBarPlot);
    connect(ui->actionVertical, &QAction::triggered,
            [this](bool checked)
            {
                if (checked) ui->splitter->setOrientation(Qt::Vertical);
            });
    connect(ui->actionHorizontal, &QAction::triggered,
            [this](bool checked)
            {
                if (checked) ui->splitter->setOrientation(Qt::Horizontal);
            });
    // Help menu signals
    QObject::connect(ui->actionHelpAbout, &QAction::triggered,
              &aboutDialog, &QWidget::show);
    QObject::connect(ui->actionReportBug, &QAction::triggered,
                     [](){QDesktopServices::openUrl(QUrl(BUG_REPORT_URL));});
    // File menu signals
    QObject::connect(ui->actionExportCsv, &QAction::triggered,
                     this, &MainWindow::onExportCsv);
    QObject::connect(ui->actionSaveSettings, &QAction::triggered,
                     this, &MainWindow::onSaveSettings);
    QObject::connect(ui->actionLoadSettings, &QAction::triggered,
                     this, &MainWindow::onLoadSettings);
    ui->actionQuit->setShortcutContext(Qt::ApplicationShortcut);
    QObject::connect(ui->actionQuit, &QAction::triggered,
                     this, &MainWindow::close);
    // port control signals
    QObject::connect(&portControl, &PortControl::portToggled,
                     this, &MainWindow::onPortToggled);
    // plot control signals
    connect(&plotControlPanel, &PlotControlPanel::numOfSamplesChanged,
            this, &MainWindow::onNumOfSamplesChanged);
    connect(&plotControlPanel, &PlotControlPanel::numOfSamplesChanged,
            plotMan, &PlotManager::setNumOfSamples);
    connect(&plotControlPanel, &PlotControlPanel::yScaleChanged,
            plotMan, &PlotManager::setYAxis);
    connect(&plotControlPanel, &PlotControlPanel::xScaleChanged,
            &stream, &Stream::setXAxis);
    connect(&plotControlPanel, &PlotControlPanel::xScaleChanged,
            plotMan, &PlotManager::setXAxis);
    connect(&plotControlPanel, &PlotControlPanel::plotWidthChanged,
            plotMan, &PlotManager::setPlotWidth);
    // plot toolbar signals
    QObject::connect(ui->actionClear, SIGNAL(triggered(bool)),
                     this, SLOT(clearPlot()));
    QObject::connect(snapshotMan.takeSnapshotAction(), &QAction::triggered,
                     plotMan, &PlotManager::flashSnapshotOverlay);
    QObject::connect(ui->actionPause, &QAction::triggered,
                     &stream, &Stream::pause);
    QObject::connect(ui->actionPause, &QAction::triggered,
                     [this](bool enabled)
                     {
                         if (enabled && !recordPanel.recordPaused())
                         {
                             dataFormatPanel.pause(true);
                         }
                         else
                         {
                             dataFormatPanel.pause(false);
                         }
                     });
    QObject::connect(&recordPanel, &RecordPanel::recordPausedChanged,
                     [this](bool enabled)
                     {
                         if (ui->actionPause->isChecked() && enabled)
                         {
                             dataFormatPanel.pause(false);
                         }
                     });
    connect(&serialPort, &QIODevice::aboutToClose,
            &recordPanel, &RecordPanel::onPortClose);
    // init plot
    numOfSamples = plotControlPanel.numOfSamples();
    stream.setNumSamples(numOfSamples);
    plotControlPanel.setChannelInfoModel(stream.infoModel());
    // Show bar plot EMZ
    this->showBarPlot(true);
    // init scales
    stream.setXAxis(plotControlPanel.xAxisAsIndex(),
                    plotControlPanel.xMin(), plotControlPanel.xMax());
    plotMan->setYAxis(plotControlPanel.autoScale(),
                      plotControlPanel.yMin(), plotControlPanel.yMax());
    plotMan->setXAxis(plotControlPanel.xAxisAsIndex(),
                      plotControlPanel.xMin(), plotControlPanel.xMax());
    plotMan->setNumOfSamples(numOfSamples);
    plotMan->setPlotWidth(plotControlPanel.plotWidth());
    // init bps (bits per second) counter
    ui->statusBar->addPermanentWidget(&bpsLabel);
    // Init sps (sample per second) counter
    spsLabel.setText("0sps");
    spsLabel.setToolTip(tr("samples per second (per channel)"));
    ui->statusBar->addPermanentWidget(&spsLabel);
    connect(&sampleCounter, &SampleCounter::spsChanged,
            this, &MainWindow::onSpsChanged);
    bpsLabel.setMinimumWidth(70);
    bpsLabel.setAlignment(Qt::AlignRight);
    spsLabel.setMinimumWidth(70);
    spsLabel.setAlignment(Qt::AlignRight);
    // init demo
    QObject::connect(ui->actionDemoMode, &QAction::toggled,
                     this, &MainWindow::enableDemo);
    QObject::connect(ui->actionDemoMode, &QAction::toggled,
                     plotMan, &PlotManager::showDemoIndicator);
    // init stream connections
    connect(&dataFormatPanel, &DataFormatPanel::sourceChanged,
            this, &MainWindow::onSourceChanged);
    onSourceChanged(dataFormatPanel.activeSource());
    // load default settings
    QSettings settings(PROGRAM_NAME, PROGRAM_NAME);
    loadAllSettings(&settings);
    handleCommandLineOptions(*QApplication::instance());
    // ensure command panel has 1 command if none loaded
    if (!commandPanel.numOfCommands())
    {
        commandPanel.newCommandAction()->trigger();
    }
    // Important: This should be after newCommandAction is triggered
    // (above) we don't want user to be greeted with command panel on
    // the very first run.
    connect(commandPanel.newCommandAction(), &QAction::triggered, [this]()
            {
                this->ui->tabWidget->setCurrentWidget(&commandPanel);
                this->ui->tabWidget->showTabs();
            });
}
MainWindow::~MainWindow()
{
    if (serialPort.isOpen())
    {
        serialPort.close();
    }
    delete plotMan;
    delete ui;
    ui = NULL; // we check if ui is deleted in messageHandler
}
void MainWindow::closeEvent(QCloseEvent * event)
{
    // save snapshots
    if (!snapshotMan.isAllSaved())
    {
        auto clickedButton = QMessageBox::warning(
            this, "Closing Tempo Plotter",
            "There are un-saved snapshots. If you close you will loose the data.",
            QMessageBox::Discard, QMessageBox::Cancel);
        if (clickedButton == QMessageBox::Cancel)
        {
            event->ignore();
            return;
        }
    }
    // save settings
    QSettings settings(PROGRAM_NAME, PROGRAM_NAME);
    saveAllSettings(&settings);
    settings.sync();
    if (settings.status() != QSettings::NoError)
    {
        QString errorText;
        if (settings.status() == QSettings::AccessError)
        {
            QString file = settings.fileName();
            errorText = QString("Tempo Plotter cannot save settings due to access error. \
This happens if you have run serialplot as root (with sudo for ex.) previously. \
Try fixing the permissions of file: %1, or just delete it.").arg(file);
        }
        else
        {
            errorText = QString("Tempo Plotter cannot save settings due to unknown error: %1").\
                arg(settings.status());
        }
        auto button = QMessageBox::critical(
            NULL,
            "Failed to save settings!", errorText,
            QMessageBox::Cancel | QMessageBox::Ok);
        if (button == QMessageBox::Cancel)
        {
            event->ignore();
            return;
        }
    }
    QMainWindow::closeEvent(event);
}
void MainWindow::setupAboutDialog()
{
    Ui_AboutDialog uiAboutDialog;
    uiAboutDialog.setupUi(&aboutDialog);
    QObject::connect(uiAboutDialog.pbAboutQt, &QPushButton::clicked,
                     [](){ QApplication::aboutQt();});
    QString aboutText = uiAboutDialog.lbAbout->text();
    aboutText.replace("$VERSION_STRING$", VERSION_STRING);
    aboutText.replace("$VERSION_REVISION$", VERSION_REVISION);
    uiAboutDialog.lbAbout->setText(aboutText);
}
void MainWindow::onPortToggled(bool open)
{
    // make sure demo mode is disabled
    if (open && isDemoRunning()) enableDemo(false);
    ui->actionDemoMode->setEnabled(!open);
    if (!open)
    {
        spsLabel.setText("0sps");
    }
}
void MainWindow::onSourceChanged(Source* source)
{
    source->connectSink(&stream);
    source->connectSink(&sampleCounter);
}
void MainWindow::clearPlot()
{
    stream.clear();
    plotMan->replot();
}
void MainWindow::onNumOfSamplesChanged(int value)
{
    numOfSamples = value;
    stream.setNumSamples(value);
    plotMan->replot();
}
void MainWindow::onSpsChanged(float sps)
{
    int precision = sps < 1. ? 3 : 0;
    spsLabel.setText(QString::number(sps, 'f', precision) + "sps");
}
bool MainWindow::isDemoRunning()
{
    return ui->actionDemoMode->isChecked();
}
void MainWindow::enableDemo(bool enabled)
{
    if (enabled)
    {
        if (!serialPort.isOpen())
        {
            dataFormatPanel.enableDemo(true);
        }
        else
        {
            ui->actionDemoMode->setChecked(false);
        }
    }
    else
    {
        dataFormatPanel.enableDemo(false);
        ui->actionDemoMode->setChecked(false);
    }
}
void MainWindow::showSecondary(QWidget* wid)
{
    if (secondaryPlot != NULL)
    {
        secondaryPlot->deleteLater();
    }
    secondaryPlot = wid;
    ui->splitter->addWidget(wid);
    ui->splitter->setStretchFactor(0, 1);
    ui->splitter->setStretchFactor(1, 0);
}
void MainWindow::hideSecondary()
{
    if (secondaryPlot == NULL)
    {
        qFatal("Secondary plot doesn't exist!");
    }
    secondaryPlot->deleteLater();
    secondaryPlot = NULL;
}
void MainWindow::showBarPlot(bool show)
{
    if (show)
    {
        auto plot = new BarPlot(&stream, &plotMenu);
        plot->setYAxis(plotControlPanel.autoScale(),
                       plotControlPanel.yMin(),
                       plotControlPanel.yMax());
        connect(&plotControlPanel, &PlotControlPanel::yScaleChanged,
                plot, &BarPlot::setYAxis);
        showSecondary(plot);
    }
    else
    {
        hideSecondary();
    }
}
void MainWindow::onExportCsv()
{
    bool wasPaused = ui->actionPause->isChecked();
    ui->actionPause->setChecked(true); // pause plotting
    QString fileName = QFileDialog::getSaveFileName(this, tr("Export CSV File"));
    if (fileName.isNull())  // user canceled export
    {
        ui->actionPause->setChecked(wasPaused);
    }
    else
    {
        Snapshot* snapshot = snapshotMan.makeSnapshot();
        snapshot->save(fileName);
        delete snapshot;
    }
}
PlotViewSettings MainWindow::viewSettings() const
{
    return plotMenu.viewSettings();
}
void MainWindow::messageHandler(QtMsgType type,
                                const QString &logString,
                                const QString &msg)
{
    if (ui != NULL)
        ui->ptLog->appendPlainText(logString);
    if (type != QtDebugMsg && ui != NULL)
    {
        ui->statusBar->showMessage(msg, 5000);
    }
}
void MainWindow::saveAllSettings(QSettings* settings)
{
    saveMWSettings(settings);
//    portControl.saveSettings(settings);
//    dataFormatPanel.saveSettings(settings);
//    stream.saveSettings(settings);
//    plotControlPanel.saveSettings(settings);
//    plotMenu.saveSettings(settings);
//    commandPanel.saveSettings(settings);
//    recordPanel.saveSettings(settings);
//    textView.saveSettings(settings);
}
void MainWindow::loadAllSettings(QSettings* settings)
{
    loadMWSettings(settings);
//    portControl.loadSettings(settings);
//    dataFormatPanel.loadSettings(settings);
//    stream.loadSettings(settings);
//    plotControlPanel.loadSettings(settings);
//    plotMenu.loadSettings(settings);
//    commandPanel.loadSettings(settings);
//    recordPanel.loadSettings(settings);
//    textView.loadSettings(settings);
}
void MainWindow::saveMWSettings(QSettings* settings)
{
    // save window geometry
    settings->beginGroup(SettingGroup_MainWindow);
    settings->setValue(SG_MainWindow_Size, size());
    settings->setValue(SG_MainWindow_Pos, pos());
    // save active panel
    settings->setValue(SG_MainWindow_ActivePanel,
                       panelSettingMap.value(ui->tabWidget->currentIndex()));
    // save panel minimization
    settings->setValue(SG_MainWindow_HidePanels,
                       ui->tabWidget->hideAction.isChecked());
    // save window maximized state
    settings->setValue(SG_MainWindow_Maximized,
                       bool(windowState() & Qt::WindowMaximized));
    // save toolbar/dockwidgets state
    settings->setValue(SG_MainWindow_State, saveState());
    settings->endGroup();
}
void MainWindow::loadMWSettings(QSettings* settings)
{
    settings->beginGroup(SettingGroup_MainWindow);
    // load window geometry
    resize(settings->value(SG_MainWindow_Size, size()).toSize());
    move(settings->value(SG_MainWindow_Pos, pos()).toPoint());
    // set active panel
    QString tabSetting =
        settings->value(SG_MainWindow_ActivePanel, QString()).toString();
    ui->tabWidget->setCurrentIndex(
        panelSettingMap.key(tabSetting, ui->tabWidget->currentIndex()));
    // hide panels
    ui->tabWidget->hideAction.setChecked(
        settings->value(SG_MainWindow_HidePanels,
                        ui->tabWidget->hideAction.isChecked()).toBool());
    // maximize window
    if (settings->value(SG_MainWindow_Maximized).toBool())
    {
        showMaximized();
    }
    // load toolbar/dockwidgets state
    restoreState(settings->value(SG_MainWindow_State).toByteArray());
    settings->setValue(SG_MainWindow_State, saveState());
    settings->endGroup();
}
void MainWindow::onSaveSettings()
{
    QString fileName = QFileDialog::getSaveFileName(
        this, tr("Save Settings"), QString(), "INI (*.ini)");
    if (!fileName.isNull()) // user canceled
    {
        QSettings settings(fileName, QSettings::IniFormat);
        saveAllSettings(&settings);
    }
}
void MainWindow::onLoadSettings()
{
    QString fileName = QFileDialog::getOpenFileName(
        this, tr("Load Settings"), QString(), "INI (*.ini)");
    if (!fileName.isNull()) // user canceled
    {
        QSettings settings(fileName, QSettings::IniFormat);
        loadAllSettings(&settings);
    }
}
void MainWindow::handleCommandLineOptions(const QCoreApplication &app)
{
    QCommandLineParser parser;
    parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsCompactedShortOptions);
    parser.setApplicationDescription("Small and simple software for plotting data from serial port in realtime.");
    parser.addHelpOption();
    parser.addVersionOption();
    QCommandLineOption configOpt({"c", "config"}, "Load configuration from file.", "filename");
    QCommandLineOption portOpt({"p", "port"}, "Set port name.", "port name");
    QCommandLineOption baudrateOpt({"b" ,"baudrate"}, "Set port baud rate.", "baud rate");
    QCommandLineOption openPortOpt({"o", "open"}, "Open serial port.");
    parser.addOption(configOpt);
    parser.addOption(portOpt);
    parser.addOption(baudrateOpt);
    parser.addOption(openPortOpt);
    parser.process(app);
    if (parser.isSet(configOpt))
    {
        QString fileName = parser.value(configOpt);
        QFileInfo fileInfo(fileName);
        if (fileInfo.exists() && fileInfo.isFile())
        {
            QSettings settings(fileName, QSettings::IniFormat);
            loadAllSettings(&settings);
        }
        else
        {
            qCritical() << "Configuration file not exist. Closing application.";
            std::exit(1);
        }
    }
    if (parser.isSet(portOpt))
    {
        portControl.selectPort(parser.value(portOpt));
    }
    if (parser.isSet(openPortOpt))
    {
        portControl.openPort();
    }
}
 |