mainwindow.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright 2017 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include <QtWidgets>
  8. #include "MainWindow.h"
  9. MainWindow::MainWindow() {
  10. this->createActions();
  11. this->createStatusBar();
  12. this->createDockWindows();
  13. this->setWindowTitle("MDB Viz");
  14. this->readSettings();
  15. this->setUnifiedTitleAndToolBarOnMac(true);
  16. }
  17. void MainWindow::openFile() {
  18. QString fileName = QFileDialog::getOpenFileName(this);
  19. if (!fileName.isEmpty()) {
  20. this->loadFile(fileName);
  21. }
  22. }
  23. void MainWindow::setupOpListWidget() {
  24. fOpListWidget->clear();
  25. QTreeWidgetItem* item = nullptr;
  26. SkTDArray<QTreeWidgetItem*> parents;
  27. for (int i = 0; i < fModel.numOps(); i++) {
  28. item = new QTreeWidgetItem();
  29. item->setText(0, QString::number(i));
  30. item->setData(0, Qt::UserRole, i);
  31. item->setText(1, fModel.getOpName(i));
  32. if (fModel.isHierarchyPop(i)) {
  33. parents.pop();
  34. }
  35. if (parents.isEmpty()) {
  36. fOpListWidget->addTopLevelItem(item);
  37. } else {
  38. parents.top()->addChild(item);
  39. }
  40. if (fModel.isHierarchyPush(i)) {
  41. *parents.push() = item;
  42. }
  43. }
  44. fOpListWidget->setCurrentItem(item);
  45. fOpListWidget->expandToDepth(100);
  46. }
  47. void MainWindow::presentCurrentRenderState() {
  48. fImage = QImage((uchar*)fModel.getPixels(), fModel.width(), fModel.height(),
  49. QImage::Format_RGBA8888);
  50. fImageLabel->setPixmap(QPixmap::fromImage(fImage));
  51. }
  52. void MainWindow::loadFile(const QString &fileName) {
  53. QFile file(fileName);
  54. if (!file.open(QFile::ReadOnly | QFile::Text)) {
  55. QMessageBox::warning(this, tr("MDB Viz"),
  56. tr("Cannot read file %1:\n%2.")
  57. .arg(QDir::toNativeSeparators(fileName), file.errorString()));
  58. return;
  59. }
  60. QTextStream in(&file);
  61. #ifndef QT_NO_CURSOR
  62. QApplication::setOverrideCursor(Qt::WaitCursor);
  63. #endif
  64. std::string str = file.fileName().toLocal8Bit().constData();
  65. Model::ErrorCode err = fModel.load(str.c_str());
  66. if (Model::ErrorCode::kOK != err) {
  67. this->statusBar()->showMessage(Model::ErrorString(err));
  68. return;
  69. }
  70. this->setupOpListWidget();
  71. this->presentCurrentRenderState();
  72. #ifndef QT_NO_CURSOR
  73. QApplication::restoreOverrideCursor();
  74. #endif
  75. }
  76. void MainWindow::about() {
  77. QMessageBox::about(this, "About MDB Viz", "Visualize MDB");
  78. }
  79. void MainWindow::createActions() {
  80. // File menu
  81. QMenu* fileMenu = this->menuBar()->addMenu(tr("&File"));
  82. QToolBar* fileToolBar = this->addToolBar(tr("File"));
  83. const QIcon openIcon = QIcon::fromTheme("document-open", QIcon(":/images/open.png"));
  84. QAction* openAct = new QAction(openIcon, tr("&Open..."), this);
  85. openAct->setShortcuts(QKeySequence::Open);
  86. openAct->setStatusTip(tr("Open an existing file"));
  87. connect(openAct, &QAction::triggered, this, &MainWindow::openFile);
  88. fileMenu->addAction(openAct);
  89. fileToolBar->addAction(openAct);
  90. fileMenu->addSeparator();
  91. const QIcon exitIcon = QIcon::fromTheme("application-exit");
  92. QAction *exitAct = fileMenu->addAction(exitIcon, tr("E&xit"), this, &QWidget::close);
  93. exitAct->setShortcuts(QKeySequence::Quit);
  94. exitAct->setStatusTip(tr("Exit the application"));
  95. // View menu
  96. fViewMenu = this->menuBar()->addMenu(tr("&View"));
  97. // Help menu
  98. this->menuBar()->addSeparator();
  99. QMenu* helpMenu = this->menuBar()->addMenu(tr("&Help"));
  100. QAction *aboutAct = helpMenu->addAction(tr("&About"), this, &MainWindow::about);
  101. aboutAct->setStatusTip(tr("Show the application's About box"));
  102. }
  103. void MainWindow::onCurrentItemChanged(QTreeWidgetItem* cur, QTreeWidgetItem* /* prev */) {
  104. int currentRow = cur->data(0, Qt::UserRole).toInt();
  105. fModel.setCurOp(currentRow);
  106. this->presentCurrentRenderState();
  107. }
  108. void MainWindow::createStatusBar() {
  109. this->statusBar()->showMessage(tr("Ready"));
  110. }
  111. void MainWindow::createDockWindows() {
  112. // Op List Window
  113. {
  114. QDockWidget* opListDock = new QDockWidget("Ops", this);
  115. opListDock->setAllowedAreas(Qt::LeftDockWidgetArea);
  116. fOpListWidget = new QTreeWidget(opListDock);
  117. QTreeWidgetItem* headerItem = new QTreeWidgetItem;
  118. headerItem->setText(0, "Index");
  119. headerItem->setText(1, "Op Name");
  120. fOpListWidget->setHeaderItem(headerItem);
  121. fOpListWidget->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
  122. fOpListWidget->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
  123. opListDock->setWidget(fOpListWidget);
  124. this->addDockWidget(Qt::LeftDockWidgetArea, opListDock);
  125. fViewMenu->addAction(opListDock->toggleViewAction());
  126. connect(fOpListWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
  127. this, SLOT(onCurrentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
  128. }
  129. // Main canvas Window
  130. {
  131. QDockWidget* mainCanvasDock = new QDockWidget("Main Canvas", this);
  132. mainCanvasDock->setAllowedAreas(Qt::RightDockWidgetArea);
  133. fImageLabel = new QLabel(mainCanvasDock);
  134. fImage = QImage(1024, 1024, QImage::Format_RGBA8888);
  135. fImage.fill(0);
  136. fImageLabel->setPixmap(QPixmap::fromImage(fImage));
  137. mainCanvasDock->setWidget(fImageLabel);
  138. this->addDockWidget(Qt::RightDockWidgetArea, mainCanvasDock);
  139. fViewMenu->addAction(mainCanvasDock->toggleViewAction());
  140. }
  141. }
  142. void MainWindow::readSettings() {
  143. QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
  144. const QByteArray geometry = settings.value("geometry", QByteArray()).toByteArray();
  145. if (geometry.isEmpty()) {
  146. const QRect availableGeometry = QApplication::desktop()->availableGeometry(this);
  147. resize(availableGeometry.width() / 3, availableGeometry.height() / 2);
  148. move((availableGeometry.width() - width()) / 2,
  149. (availableGeometry.height() - height()) / 2);
  150. } else {
  151. this->restoreGeometry(geometry);
  152. }
  153. }
  154. void MainWindow::writeSettings() {
  155. QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
  156. settings.setValue("geometry", this->saveGeometry());
  157. }