feat(editor): recent files
This commit is contained in:
parent
93984ce1c0
commit
e2054a51a8
2 changed files with 98 additions and 1 deletions
|
@ -12,6 +12,8 @@
|
||||||
#include "undohistory.h"
|
#include "undohistory.h"
|
||||||
#include "version.h"
|
#include "version.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <QToolButton>
|
||||||
|
#include <QSettings>
|
||||||
#include <qclipboard.h>
|
#include <qclipboard.h>
|
||||||
#include <qevent.h>
|
#include <qevent.h>
|
||||||
#include <qglobal.h>
|
#include <qglobal.h>
|
||||||
|
@ -27,6 +29,7 @@
|
||||||
#include <qtextcursor.h>
|
#include <qtextcursor.h>
|
||||||
#include <qtextedit.h>
|
#include <qtextedit.h>
|
||||||
#include <miniaudio.h>
|
#include <miniaudio.h>
|
||||||
|
#include <qtoolbutton.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#ifdef _NDEBUG
|
#ifdef _NDEBUG
|
||||||
|
@ -67,6 +70,8 @@ MainWindow::MainWindow(QWidget *parent)
|
||||||
: QMainWindow(parent)
|
: QMainWindow(parent)
|
||||||
, ui(new Ui::MainWindow)
|
, ui(new Ui::MainWindow)
|
||||||
{
|
{
|
||||||
|
loadState();
|
||||||
|
|
||||||
gDataModel->Init();
|
gDataModel->Init();
|
||||||
|
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
@ -102,6 +107,28 @@ MainWindow::MainWindow(QWidget *parent)
|
||||||
|
|
||||||
connectActionHandlers();
|
connectActionHandlers();
|
||||||
|
|
||||||
|
// Add open recents menu
|
||||||
|
refreshRecentsMenu();
|
||||||
|
for (QObject* child : ui->fileTools->children()) {
|
||||||
|
if (auto toolButton = dynamic_cast<QToolButton*>(child)) {
|
||||||
|
if (toolButton->defaultAction() != ui->actionOpen) continue;
|
||||||
|
|
||||||
|
// https://stackoverflow.com/a/12283957/16255372
|
||||||
|
// https://stackoverflow.com/a/5365184/16255372
|
||||||
|
|
||||||
|
toolButton->setMenu(recentsMenu);
|
||||||
|
toolButton->setPopupMode(QToolButton::MenuButtonPopup);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add open recents dropdown to file menu
|
||||||
|
auto actions = ui->menuFile->actions();
|
||||||
|
for (int i = 0; i < actions.size(); i++) {
|
||||||
|
if (actions[i] != ui->actionOpen) continue;
|
||||||
|
|
||||||
|
ui->menuFile->insertMenu((i+1) < actions.size() ? actions[i+1] : nullptr, recentsMenu);
|
||||||
|
}
|
||||||
|
|
||||||
// ui->explorerView->Init(ui);
|
// ui->explorerView->Init(ui);
|
||||||
placeDocument = new PlaceDocument(this);
|
placeDocument = new PlaceDocument(this);
|
||||||
placeDocument->setAttribute(Qt::WA_DeleteOnClose, true);
|
placeDocument->setAttribute(Qt::WA_DeleteOnClose, true);
|
||||||
|
@ -125,6 +152,7 @@ MainWindow::MainWindow(QWidget *parent)
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::closeEvent(QCloseEvent* evt) {
|
void MainWindow::closeEvent(QCloseEvent* evt) {
|
||||||
|
saveState();
|
||||||
#ifdef NDEBUG
|
#ifdef NDEBUG
|
||||||
// Ask if the user wants to save their changes
|
// Ask if the user wants to save their changes
|
||||||
// https://stackoverflow.com/a/33890731
|
// https://stackoverflow.com/a/33890731
|
||||||
|
@ -147,6 +175,64 @@ void MainWindow::closeEvent(QCloseEvent* evt) {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindow::refreshRecentsMenu() {
|
||||||
|
if (!recentsMenu) recentsMenu = new QMenu();
|
||||||
|
|
||||||
|
recentsMenu->setTitle("Recent files...");
|
||||||
|
|
||||||
|
recentsMenu->clear(); // Actions not shown in any other menu are automatically deleted
|
||||||
|
for (QString item : recentFiles) {
|
||||||
|
QAction* itemAction = new QAction();
|
||||||
|
itemAction->setText(item.split('/').last());
|
||||||
|
recentsMenu->addAction(itemAction);
|
||||||
|
|
||||||
|
connect(itemAction, &QAction::triggered, [item, this]{
|
||||||
|
if (!QFile::exists(item)) {
|
||||||
|
QMessageBox::warning(this, "File not found", "The file '" + item + "' could not longer be found at that location.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<DataModel> newModel = DataModel::LoadFromFile(item.toStdString());
|
||||||
|
editModeDataModel = newModel;
|
||||||
|
gDataModel = newModel;
|
||||||
|
newModel->Init();
|
||||||
|
ui->explorerView->updateRoot(newModel);
|
||||||
|
|
||||||
|
// Reset running state
|
||||||
|
placeDocument->setRunState(RUN_STOPPED);
|
||||||
|
undoManager.Reset();
|
||||||
|
updateToolbars();
|
||||||
|
pushRecentFile(item);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (recentsMenu->isEmpty()) {
|
||||||
|
QAction* emptyAction = new QAction("No recents");
|
||||||
|
emptyAction->setEnabled(false);
|
||||||
|
recentsMenu->addAction(emptyAction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::loadState() {
|
||||||
|
QSettings settings("openblocks");
|
||||||
|
recentFiles = settings.value("recentFiles").toStringList();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::saveState() {
|
||||||
|
QSettings settings("openblocks");
|
||||||
|
settings.setValue("recentFiles", recentFiles);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainWindow::pushRecentFile(QString file) {
|
||||||
|
// https://www.walletfox.com/course/qtopenrecentfiles.php
|
||||||
|
recentFiles.removeAll(file);
|
||||||
|
recentFiles.prepend(file);
|
||||||
|
while (recentFiles.size() > 10) recentFiles.removeLast();
|
||||||
|
|
||||||
|
refreshRecentsMenu();
|
||||||
|
saveState();
|
||||||
|
}
|
||||||
|
|
||||||
void MainWindow::setUpCommandBar() {
|
void MainWindow::setUpCommandBar() {
|
||||||
CommandEdit* commandEdit;
|
CommandEdit* commandEdit;
|
||||||
QToolBar* commandBar = ui->commandBar;
|
QToolBar* commandBar = ui->commandBar;
|
||||||
|
@ -280,6 +366,7 @@ void MainWindow::connectActionHandlers() {
|
||||||
}
|
}
|
||||||
|
|
||||||
editModeDataModel->SaveToFile(path);
|
editModeDataModel->SaveToFile(path);
|
||||||
|
if (path) pushRecentFile(QString::fromStdString(path.value()));
|
||||||
});
|
});
|
||||||
|
|
||||||
connect(ui->actionSaveAs, &QAction::triggered, this, [&]() {
|
connect(ui->actionSaveAs, &QAction::triggered, this, [&]() {
|
||||||
|
@ -287,6 +374,7 @@ void MainWindow::connectActionHandlers() {
|
||||||
if (!path || path == "") return;
|
if (!path || path == "") return;
|
||||||
|
|
||||||
editModeDataModel->SaveToFile(path);
|
editModeDataModel->SaveToFile(path);
|
||||||
|
if (path) pushRecentFile(QString::fromStdString(path.value()));
|
||||||
});
|
});
|
||||||
|
|
||||||
connect(ui->actionOpen, &QAction::triggered, this, [&]() {
|
connect(ui->actionOpen, &QAction::triggered, this, [&]() {
|
||||||
|
@ -307,6 +395,7 @@ void MainWindow::connectActionHandlers() {
|
||||||
placeDocument->setRunState(RUN_STOPPED);
|
placeDocument->setRunState(RUN_STOPPED);
|
||||||
undoManager.Reset();
|
undoManager.Reset();
|
||||||
updateToolbars();
|
updateToolbars();
|
||||||
|
if (path) pushRecentFile(QString::fromStdString(path.value()));
|
||||||
});
|
});
|
||||||
|
|
||||||
connect(ui->actionDelete, &QAction::triggered, this, [&]() {
|
connect(ui->actionDelete, &QAction::triggered, this, [&]() {
|
||||||
|
|
|
@ -64,10 +64,18 @@ public:
|
||||||
void openFile(std::string path);
|
void openFile(std::string path);
|
||||||
|
|
||||||
Ui::MainWindow *ui;
|
Ui::MainWindow *ui;
|
||||||
|
|
||||||
|
QMenu* recentsMenu = nullptr;
|
||||||
|
QStringList recentFiles;
|
||||||
|
void refreshRecentsMenu();
|
||||||
|
void pushRecentFile(QString);
|
||||||
|
|
||||||
|
void loadState();
|
||||||
|
void saveState();
|
||||||
|
|
||||||
friend PlaceDocument;
|
friend PlaceDocument;
|
||||||
private:
|
private:
|
||||||
PlaceDocument* placeDocument;
|
PlaceDocument* placeDocument = nullptr;
|
||||||
|
|
||||||
void setUpCommandBar();
|
void setUpCommandBar();
|
||||||
void connectActionHandlers();
|
void connectActionHandlers();
|
||||||
|
|
Loading…
Add table
Reference in a new issue