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
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "mainwindow.h"
#include "editorwidget.h"
#include "previewwidget.h"
#include "../states/statecontroller.h"
#include <QCloseEvent>
#include <QFileSystemWatcher>
#include <QLayout>
#include <QMenuBar>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, m_editorWidget{new EditorWidget}
, m_previewWidget{new PreviewWidget}
, m_fileWatcher{new QFileSystemWatcher}
{
setWindowIcon(QIcon(":/resources/logo.png"));
setWindowTitle(tr("QML Previewer"));
resize(1400, 800);
initUI();
initMenuBar();
setupConnections();
StateController::instance()->fileLoaded(":/resources/default.qml");
m_editorWidget->updateEditor();
}
MainWindow::~MainWindow() {}
void MainWindow::initUI()
{
QWidget *centralWidget = new QWidget;
setCentralWidget(centralWidget);
QHBoxLayout *horizontalLayout = new QHBoxLayout;
centralWidget->setLayout(horizontalLayout);
horizontalLayout->addWidget(m_editorWidget, 1);
horizontalLayout->addWidget(m_previewWidget, 1);
}
void MainWindow::initMenuBar()
{
QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
m_openAction = fileMenu->addAction(tr("&Open"));
m_saveAction = fileMenu->addAction(tr("&Save"));
m_saveAction->setEnabled(false);
m_closeAction = fileMenu->addAction(tr("&Close"));
m_closeAction->setEnabled(false);
m_reloadAction = fileMenu->addAction(tr("&Reload"));
m_reloadAction->setEnabled(false);
fileMenu->addSeparator();
m_quitAction = fileMenu->addAction(tr("&Quit"));
#if QT_CONFIG(shortcut)
m_openAction->setShortcut(QKeySequence::Open);
m_saveAction->setShortcut(QKeySequence::Save);
m_closeAction->setShortcut(QKeySequence::Close);
m_reloadAction->setShortcut(Qt::CTRL | Qt::Key_R);
m_quitAction->setShortcut(QKeySequence::Quit);
#endif
}
void MainWindow::setupConnections()
{
connect(StateController::instance(), &StateController::stateChanged, this,
&MainWindow::onAppStateChanged);
connect(m_fileWatcher, &QFileSystemWatcher::fileChanged, this, &MainWindow::onFileChanged);
connect(menuBar(), &QMenuBar::triggered, this, &MainWindow::onMenuBarTriggered);
connect(m_previewWidget, &PreviewWidget::errorPositionSelected, m_editorWidget,
&EditorWidget::moveCursorTo);
}
void MainWindow::closeEvent(QCloseEvent *event)
{
switch (StateController::instance()->currentState()) {
case StateController::NewState:
case StateController::DirtyState: {
const QMessageBox::StandardButton answer =
QMessageBox::question(this, tr("About to Close"),
tr("You have unsaved changes. "
"Are you sure you want to close without saving?"),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
if (answer == QMessageBox::Yes)
event->accept();
else
event->ignore();
} break;
default:
event->accept();
break;
}
}
void MainWindow::onAppStateChanged(int oldState, int newState)
{
switch (newState) {
case StateController::InitState:
if (!m_previewWidget->sourcePath().isEmpty()) {
if (const QStringList files = m_fileWatcher->files(); !files.isEmpty())
m_fileWatcher->removePaths(files);
m_previewWidget->setSourcePath(QString{});
}
m_saveAction->setEnabled(false);
m_closeAction->setEnabled(false);
m_reloadAction->setEnabled(false);
break;
case StateController::OpenState:
if (const QStringList files = m_fileWatcher->files(); !files.isEmpty())
m_fileWatcher->removePaths(files);
m_fileWatcher->addPath(StateController::instance()->filePath());
m_previewWidget->setSourcePath(StateController::instance()->filePath());
m_saveAction->setEnabled(false);
m_closeAction->setEnabled(true);
m_reloadAction->setEnabled(false);
break;
case StateController::NewState:
m_saveAction->setEnabled(true);
m_closeAction->setEnabled(false);
m_reloadAction->setEnabled(false);
break;
case StateController::DirtyState:
m_saveAction->setEnabled(true);
m_closeAction->setEnabled(false);
m_reloadAction->setEnabled(true);
break;
default:
Q_UNREACHABLE();
break;
}
}
void MainWindow::onFileChanged(const QString &path)
{
StateController* stateController = StateController::instance();
m_fileWatcher->addPath(path);
if (stateController->currentState() == StateController::DirtyState) {
const QMessageBox::StandardButton answer =
QMessageBox::question(this, tr("Reload File"),
tr("The file <i>%1</i> has been changed on disk. "
"Do you want to reload it?").arg(path),
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if (answer == QMessageBox::No)
return;
}
m_editorWidget->updateEditor();
m_previewWidget->setSourcePath(path);
stateController->setDirty(false);
}
void MainWindow::onMenuBarTriggered(QAction *action)
{
if (action == m_openAction)
m_editorWidget->openFile();
else if (action == m_saveAction)
m_editorWidget->saveFile();
else if (action == m_closeAction)
m_editorWidget->closeFile();
else if (action == m_reloadAction)
m_editorWidget->reloadFile();
else if (action == m_quitAction)
QCoreApplication::quit();
}
|