216 lines
9.3 KiB
C++
216 lines
9.3 KiB
C++
#include "ui/tkrMainWindow.h"
|
|
#include "BuildInfo.h"
|
|
|
|
#include "controller/tkrEditorController.h"
|
|
#include "ui/tkrDiagramScene.h"
|
|
#include "ui/tkrDiagramView.h"
|
|
#include "ui/tkrWorkspaceSidebar.h"
|
|
|
|
#include <QAction>
|
|
#include <QCloseEvent>
|
|
#include <QFileDialog>
|
|
#include <QFileInfo>
|
|
#include <QKeySequence>
|
|
#include <QLabel>
|
|
#include <QMenu>
|
|
#include <QMenuBar>
|
|
#include <QMessageBox>
|
|
#include <QPainter>
|
|
#include <QPlainTextEdit>
|
|
#include <QSplitter>
|
|
#include <QStatusBar>
|
|
#include <QVBoxLayout>
|
|
|
|
namespace TikzRefinery {
|
|
namespace {
|
|
constexpr int iCanvasInitialWidth = 650;
|
|
constexpr int iSourceInitialWidth = 430;
|
|
constexpr int iWindowInitialWidth = 1380;
|
|
constexpr int iWindowInitialHeight = 800;
|
|
constexpr int iSidebarPaneIndex = 0;
|
|
constexpr int iEditorPaneIndex = 1;
|
|
QString applicationIdentity()
|
|
{
|
|
return QStringLiteral("TikZRefinery - %1").arg(QString::fromLatin1(BuildInfo::pVersion));
|
|
}
|
|
} // namespace
|
|
|
|
tkrMainWindow::tkrMainWindow(QWidget *pParent, const QString &strInitialFilePath) : QMainWindow(pParent)
|
|
{
|
|
m_pScene = new tkrDiagramScene(this);
|
|
m_upController = std::make_unique<tkrEditorController>(m_document, *m_pScene);
|
|
QSplitter *pWorkspaceSplitter = new QSplitter(this);
|
|
pWorkspaceSplitter->setObjectName(QStringLiteral("workspaceSplitter"));
|
|
m_pSidebar = new tkrWorkspaceSidebar(pWorkspaceSplitter);
|
|
m_pSidebar->setProviderDescription(m_pScene->pluginDescription(), m_pScene->shapeProvider() != nullptr);
|
|
connect(m_pSidebar, &tkrWorkspaceSidebar::openDocumentRequested, this, &tkrMainWindow::openDocument);
|
|
QSplitter *pSplitter = new QSplitter(pWorkspaceSplitter);
|
|
QWidget *pCanvasPane = new QWidget(pSplitter);
|
|
QVBoxLayout *pCanvasLayout = new QVBoxLayout(pCanvasPane);
|
|
QLabel *pCanvasLabel = new QLabel(tr("Canvas"), pCanvasPane);
|
|
m_pView = new tkrDiagramView(m_pScene, pCanvasPane);
|
|
m_pView->setStyleSheet(QStringLiteral("QGraphicsView { border: 1px solid #64748b; }"));
|
|
m_pView->setRenderHint(QPainter::Antialiasing);
|
|
m_pView->setAccessibleName(pCanvasLabel->text());
|
|
pCanvasLabel->setBuddy(m_pView);
|
|
pCanvasLayout->addWidget(pCanvasLabel);
|
|
pCanvasLayout->addWidget(m_pView, 1);
|
|
|
|
QWidget *pSourcePane = new QWidget(pSplitter);
|
|
QVBoxLayout *pSourceLayout = new QVBoxLayout(pSourcePane);
|
|
QLabel *pSourceLabel = new QLabel(tr("TikZ source"), pSourcePane);
|
|
m_pSourceView = new QPlainTextEdit(pSourcePane);
|
|
m_pSourceView->setReadOnly(true);
|
|
m_pSourceView->setAccessibleName(pSourceLabel->text());
|
|
m_pSourceView->setPlaceholderText(tr("Open a TikZ source file to inspect it."));
|
|
pSourceLabel->setBuddy(m_pSourceView);
|
|
pSourceLayout->addWidget(pSourceLabel);
|
|
pSourceLayout->addWidget(m_pSourceView, 1);
|
|
|
|
pSplitter->addWidget(pCanvasPane);
|
|
pSplitter->addWidget(pSourcePane);
|
|
pSplitter->setChildrenCollapsible(false);
|
|
pSplitter->setSizes({iCanvasInitialWidth, iSourceInitialWidth});
|
|
pWorkspaceSplitter->addWidget(m_pSidebar);
|
|
pWorkspaceSplitter->addWidget(pSplitter);
|
|
pWorkspaceSplitter->setChildrenCollapsible(false);
|
|
pWorkspaceSplitter->setSizes({m_pSidebar->expandedWidth(), iCanvasInitialWidth + iSourceInitialWidth});
|
|
connect(m_pSidebar, &tkrWorkspaceSidebar::widthRequested, this,
|
|
[pWorkspaceSplitter](int iWidth) {
|
|
const QList<int> listSizes = pWorkspaceSplitter->sizes();
|
|
const int iTotalWidth = listSizes.at(iSidebarPaneIndex) + listSizes.at(iEditorPaneIndex);
|
|
pWorkspaceSplitter->setSizes({iWidth, iTotalWidth - iWidth});
|
|
});
|
|
setCentralWidget(pWorkspaceSplitter);
|
|
|
|
QMenu *pFileMenu = menuBar()->addMenu(tr("&File"));
|
|
QAction *pOpenAction = pFileMenu->addAction(tr("&Open..."));
|
|
pOpenAction->setShortcut(QKeySequence::Open);
|
|
connect(pOpenAction, &QAction::triggered, this, &tkrMainWindow::openDocument);
|
|
m_pSaveAction = pFileMenu->addAction(tr("&Save"));
|
|
m_pSaveAction->setShortcut(QKeySequence::Save);
|
|
m_pSaveAction->setEnabled(false);
|
|
connect(m_pSaveAction, &QAction::triggered, this, [this]() { saveDocument(); });
|
|
pFileMenu->addSeparator();
|
|
QAction *pCloseAction = pFileMenu->addAction(tr("&Exit"));
|
|
connect(pCloseAction, &QAction::triggered, this, [this]() { close(); });
|
|
|
|
QMenu *pEditMenu = menuBar()->addMenu(tr("&Edit"));
|
|
m_pUndoAction = pEditMenu->addAction(tr("&Undo"));
|
|
m_pRedoAction = pEditMenu->addAction(tr("&Redo"));
|
|
m_pUndoAction->setObjectName(QStringLiteral("undoAction"));
|
|
m_pRedoAction->setObjectName(QStringLiteral("redoAction"));
|
|
m_pUndoAction->setShortcuts(QKeySequence::keyBindings(QKeySequence::Undo));
|
|
m_pRedoAction->setShortcuts(QKeySequence::keyBindings(QKeySequence::Redo));
|
|
m_pUndoAction->setEnabled(false);
|
|
m_pRedoAction->setEnabled(false);
|
|
const auto handleHistoryResult = [this](const OperationResult &result) {
|
|
statusBar()->showMessage(result.bSucceeded ? result.strMessage
|
|
: tr("Edit rejected: %1 Source unchanged.").arg(result.strMessage));
|
|
};
|
|
connect(m_pUndoAction, &QAction::triggered, this, [this, handleHistoryResult]() {
|
|
handleHistoryResult(m_upController->undo());
|
|
});
|
|
connect(m_pRedoAction, &QAction::triggered, this, [this, handleHistoryResult]() {
|
|
handleHistoryResult(m_upController->redo());
|
|
});
|
|
|
|
connect(m_upController.get(), &tkrEditorController::documentChanged,
|
|
this, &tkrMainWindow::updateDocumentView);
|
|
connect(m_upController.get(), &tkrEditorController::statusChanged,
|
|
this, [this](const QString &strMessage) { statusBar()->showMessage(strMessage); });
|
|
QMenu *pHelpMenu = menuBar()->addMenu(tr("&Help"));
|
|
QAction *pAboutAction = pHelpMenu->addAction(tr("&About TikZRefinery"));
|
|
connect(pAboutAction, &QAction::triggered, this, [this]() {
|
|
QMessageBox::about(this, tr("About TikZRefinery"),
|
|
tr("%1 (%2)\n\nFull source checkpoint: %3\nSource state: %4\n\nShape provider:\n%5")
|
|
.arg(applicationIdentity(), QString::fromLatin1(BuildInfo::pShortCheckpoint),
|
|
QString::fromLatin1(BuildInfo::pCheckpoint), QString::fromLatin1(BuildInfo::pSourceState),
|
|
m_pScene->pluginDescription()));
|
|
});
|
|
setWindowTitle(applicationIdentity());
|
|
resize(iWindowInitialWidth, iWindowInitialHeight);
|
|
statusBar()->showMessage(tr("Open a TikZ document, then select and drag an element. %1")
|
|
.arg(m_pScene->pluginDescription().section(QLatin1Char('\n'), 0, 0)));
|
|
if (!strInitialFilePath.isEmpty()) {
|
|
const OperationResult resultOpen = m_upController->openDocument(strInitialFilePath);
|
|
if (!resultOpen.bSucceeded) {
|
|
QMessageBox::warning(this, tr("Could not open document"), resultOpen.strMessage);
|
|
}
|
|
}
|
|
}
|
|
|
|
tkrMainWindow::~tkrMainWindow() = default;
|
|
|
|
void tkrMainWindow::openDocument()
|
|
{
|
|
if (!confirmDiscardOrSave()) {
|
|
return;
|
|
}
|
|
const QString strFilePath = QFileDialog::getOpenFileName(
|
|
this, tr("Open TikZ document"), {}, tr("TeX source (*.tex *.tikz);;All files (*)"));
|
|
if (strFilePath.isEmpty()) {
|
|
return;
|
|
}
|
|
const OperationResult resultOpen = m_upController->openDocument(strFilePath);
|
|
if (!resultOpen.bSucceeded) {
|
|
QMessageBox::warning(this, tr("Could not open document"), resultOpen.strMessage);
|
|
}
|
|
}
|
|
|
|
bool tkrMainWindow::saveDocument()
|
|
{
|
|
const OperationResult resultSave = m_upController->saveDocument();
|
|
if (!resultSave.bSucceeded) {
|
|
QMessageBox::warning(this, tr("Could not save document"), resultSave.strMessage);
|
|
return false;
|
|
}
|
|
statusBar()->showMessage(resultSave.strMessage);
|
|
return true;
|
|
}
|
|
|
|
bool tkrMainWindow::confirmDiscardOrSave()
|
|
{
|
|
if (!m_document.hasUnsavedChanges()) {
|
|
return true;
|
|
}
|
|
const QMessageBox::StandardButton buttonChoice = QMessageBox::question(
|
|
this, tr("Unsaved changes"), tr("Save your changes before continuing?"),
|
|
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel,
|
|
QMessageBox::Cancel);
|
|
if (buttonChoice == QMessageBox::Save) {
|
|
return saveDocument();
|
|
}
|
|
return buttonChoice == QMessageBox::Discard;
|
|
}
|
|
|
|
void tkrMainWindow::updateDocumentView()
|
|
{
|
|
m_pView->setCanvasBounds(m_pScene->sceneRect(), m_pScene->hasFixedBounds());
|
|
// Restored geometry can keep identical bounds. Repaint foreground handles too.
|
|
m_pView->viewport()->update();
|
|
// Display is decoded for viewing only; saving always uses preserved source bytes.
|
|
m_pSourceView->setPlainText(QString::fromUtf8(m_document.sourceBytes()));
|
|
m_pSaveAction->setEnabled(m_document.hasUnsavedChanges());
|
|
m_pUndoAction->setEnabled(m_document.canUndo());
|
|
m_pRedoAction->setEnabled(m_document.canRedo());
|
|
m_pUndoAction->setText(m_document.canUndo() ? tr("&Undo %1").arg(m_document.undoDescription()) : tr("&Undo"));
|
|
m_pRedoAction->setText(m_document.canRedo() ? tr("&Redo %1").arg(m_document.redoDescription()) : tr("&Redo"));
|
|
setWindowModified(m_document.hasUnsavedChanges());
|
|
setWindowTitle(QFileInfo(m_document.filePath()).fileName() + QStringLiteral("[*] - ") + applicationIdentity());
|
|
}
|
|
|
|
void tkrMainWindow::closeEvent(QCloseEvent *pEvent)
|
|
{
|
|
m_pScene->cancelInteraction();
|
|
if (!confirmDiscardOrSave()) {
|
|
pEvent->ignore();
|
|
return;
|
|
}
|
|
pEvent->accept();
|
|
// main.cpp owns this window on the stack. Event-loop exit destroys the window,
|
|
// then its controller/document members and QObject-owned scene/widgets.
|
|
}
|
|
|
|
} // namespace TikzRefinery
|