aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/mdbviz/mainwindow.cpp
blob: 5f5746b6325bcf60e9015c62289cfc2e8e0ee124 (plain)
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
/*
 * Copyright 2017 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <QtWidgets>

#include "MainWindow.h"

MainWindow::MainWindow() {
    this->createActions();
    this->createStatusBar();
    this->createDockWindows();

    this->setWindowTitle("MDB Viz");

    this->readSettings();
    this->setUnifiedTitleAndToolBarOnMac(true);
}

void MainWindow::openFile() {
    QString fileName = QFileDialog::getOpenFileName(this);
    if (!fileName.isEmpty()) {
        this->loadFile(fileName);
    }
}

void MainWindow::setupOpListWidget() {
    fOpListWidget->clear();

    QTreeWidgetItem* item = nullptr;
    SkTDArray<QTreeWidgetItem*> parents;

    for (int i = 0; i < fModel.numOps(); i++) {
        item = new QTreeWidgetItem();

        item->setText(0, QString::number(i));
        item->setData(0, Qt::UserRole, i);
        item->setText(1, fModel.getOpName(i));

        if (fModel.isHierarchyPop(i)) {
            parents.pop();
        }

        if (parents.isEmpty()) {
            fOpListWidget->addTopLevelItem(item);
        } else {
            parents.top()->addChild(item);
        }

        if (fModel.isHierarchyPush(i)) {
            *parents.push() = item;
        }
    }

    fOpListWidget->setCurrentItem(item);
    fOpListWidget->expandToDepth(100);
}

void MainWindow::presentCurrentRenderState() {
    fImage = QImage((uchar*)fModel.getPixels(), fModel.width(), fModel.height(),
                    QImage::Format_RGBA8888);
    fImageLabel->setPixmap(QPixmap::fromImage(fImage));
}

void MainWindow::loadFile(const QString &fileName) {
    QFile file(fileName);
    if (!file.open(QFile::ReadOnly | QFile::Text)) {
        QMessageBox::warning(this, tr("MDB Viz"),
                             tr("Cannot read file %1:\n%2.")
                             .arg(QDir::toNativeSeparators(fileName), file.errorString()));
        return;
    }

    QTextStream in(&file);
#ifndef QT_NO_CURSOR
    QApplication::setOverrideCursor(Qt::WaitCursor);
#endif

    std::string str = file.fileName().toLocal8Bit().constData();

    Model::ErrorCode err = fModel.load(str.c_str());
    if (Model::ErrorCode::kOK != err) {
        this->statusBar()->showMessage(Model::ErrorString(err));
        return;
    }

    this->setupOpListWidget();
    this->presentCurrentRenderState();

#ifndef QT_NO_CURSOR
    QApplication::restoreOverrideCursor();
#endif
}


void MainWindow::about() {
   QMessageBox::about(this, "About MDB Viz", "Visualize MDB");
}

void MainWindow::createActions() {

    // File menu
    QMenu* fileMenu = this->menuBar()->addMenu(tr("&File"));
    QToolBar* fileToolBar = this->addToolBar(tr("File"));

    const QIcon openIcon = QIcon::fromTheme("document-open", QIcon(":/images/open.png"));
    QAction* openAct = new QAction(openIcon, tr("&Open..."), this);
    openAct->setShortcuts(QKeySequence::Open);
    openAct->setStatusTip(tr("Open an existing file"));
    connect(openAct, &QAction::triggered, this, &MainWindow::openFile);
    fileMenu->addAction(openAct);
    fileToolBar->addAction(openAct);

    fileMenu->addSeparator();

    const QIcon exitIcon = QIcon::fromTheme("application-exit");
    QAction *exitAct = fileMenu->addAction(exitIcon, tr("E&xit"), this, &QWidget::close);
    exitAct->setShortcuts(QKeySequence::Quit);
    exitAct->setStatusTip(tr("Exit the application"));

    // View menu
    fViewMenu = this->menuBar()->addMenu(tr("&View"));

    // Help menu
    this->menuBar()->addSeparator();

    QMenu* helpMenu = this->menuBar()->addMenu(tr("&Help"));

    QAction *aboutAct = helpMenu->addAction(tr("&About"), this, &MainWindow::about);
    aboutAct->setStatusTip(tr("Show the application's About box"));
}

void MainWindow::onCurrentItemChanged(QTreeWidgetItem* cur, QTreeWidgetItem* /* prev */) {
    int currentRow = cur->data(0, Qt::UserRole).toInt();
    fModel.setCurOp(currentRow);
    this->presentCurrentRenderState();
}

void MainWindow::createStatusBar() {
    this->statusBar()->showMessage(tr("Ready"));
}

void MainWindow::createDockWindows() {

    // Op List Window
    {
        QDockWidget* opListDock = new QDockWidget("Ops", this);
        opListDock->setAllowedAreas(Qt::LeftDockWidgetArea);

        fOpListWidget = new QTreeWidget(opListDock);

        QTreeWidgetItem* headerItem = new QTreeWidgetItem;
        headerItem->setText(0, "Index");
        headerItem->setText(1, "Op Name");
        fOpListWidget->setHeaderItem(headerItem);

        fOpListWidget->header()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
        fOpListWidget->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);

        opListDock->setWidget(fOpListWidget);
        this->addDockWidget(Qt::LeftDockWidgetArea, opListDock);

        fViewMenu->addAction(opListDock->toggleViewAction());

        connect(fOpListWidget, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
                this, SLOT(onCurrentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)));
    }

    // Main canvas Window
    {
        QDockWidget* mainCanvasDock = new QDockWidget("Main Canvas", this);
        mainCanvasDock->setAllowedAreas(Qt::RightDockWidgetArea);

        fImageLabel = new QLabel(mainCanvasDock);

        fImage = QImage(1024, 1024, QImage::Format_RGBA8888);
        fImage.fill(0);
        fImageLabel->setPixmap(QPixmap::fromImage(fImage));

        mainCanvasDock->setWidget(fImageLabel);
        this->addDockWidget(Qt::RightDockWidgetArea, mainCanvasDock);

        fViewMenu->addAction(mainCanvasDock->toggleViewAction());
    }
}

void MainWindow::readSettings() {
    QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
    const QByteArray geometry = settings.value("geometry", QByteArray()).toByteArray();
    if (geometry.isEmpty()) {
        const QRect availableGeometry = QApplication::desktop()->availableGeometry(this);
        resize(availableGeometry.width() / 3, availableGeometry.height() / 2);
        move((availableGeometry.width() - width()) / 2,
             (availableGeometry.height() - height()) / 2);
    } else {
        this->restoreGeometry(geometry);
    }
}

void MainWindow::writeSettings() {
    QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
    settings.setValue("geometry", this->saveGeometry());
}