Add 'Open Recent Project' menu

This commit is contained in:
GriffinR 2023-12-28 00:55:45 -05:00
parent 6a8d3a8197
commit 6fedab9a22
5 changed files with 72 additions and 19 deletions

View file

@ -3021,7 +3021,13 @@
<property name="title">
<string>File</string>
</property>
<widget class="QMenu" name="menuOpen_Recent_Project">
<property name="title">
<string>Open Recent Project</string>
</property>
</widget>
<addaction name="action_Open_Project"/>
<addaction name="menuOpen_Recent_Project"/>
<addaction name="action_Reload_Project"/>
<addaction name="action_Save"/>
<addaction name="action_Save_Project"/>

View file

@ -51,7 +51,7 @@ public:
reset();
}
virtual void reset() override {
this->recentProject = "";
this->recentProjects.clear();
this->reopenOnLaunch = true;
this->mapSortOrder = MapSortOrder::Group;
this->prettyCursors = true;
@ -73,7 +73,8 @@ public:
this->projectSettingsTab = 0;
this->warpBehaviorWarningDisabled = false;
}
void setRecentProject(QString project);
void addRecentProject(QString project);
void setRecentProjects(QStringList projects);
void setReopenOnLaunch(bool enabled);
void setMapSortOrder(MapSortOrder order);
void setPrettyCursors(bool enabled);
@ -101,6 +102,7 @@ public:
void setProjectSettingsTab(int tab);
void setWarpBehaviorWarningDisabled(bool disabled);
QString getRecentProject();
QStringList getRecentProjects();
bool getReopenOnLaunch();
MapSortOrder getMapSortOrder();
bool getPrettyCursors();
@ -134,7 +136,7 @@ protected:
virtual void onNewConfigFileCreated() override {};
virtual void setUnreadKeys() override {};
private:
QString recentProject;
QStringList recentProjects;
bool reopenOnLaunch;
QString stringFromByteArray(QByteArray);
QByteArray bytesFromString(QString);

View file

@ -354,6 +354,7 @@ private:
bool setInitialMap();
void setRecentMap(QString map_name);
QStandardItem* createMapItem(QString mapName, int groupNum, int inGroupNum);
void refreshRecentProjectsMenu();
void drawMapListIcons(QAbstractItemModel *model);
void updateMapList();

View file

@ -319,7 +319,10 @@ QString PorymapConfig::getConfigFilepath() {
void PorymapConfig::parseConfigKeyValue(QString key, QString value) {
if (key == "recent_project") {
this->recentProject = value;
QStringList paths = value.split(",", Qt::SkipEmptyParts);
for (auto path : paths)
this->recentProjects.append(path);
this->recentProjects.removeDuplicates();
} else if (key == "reopen_on_launch") {
this->reopenOnLaunch = getConfigBool(key, value);
} else if (key == "pretty_cursors") {
@ -404,7 +407,7 @@ void PorymapConfig::parseConfigKeyValue(QString key, QString value) {
QMap<QString, QString> PorymapConfig::getKeyValueMap() {
QMap<QString, QString> map;
map.insert("recent_project", this->recentProject);
map.insert("recent_project", this->recentProjects.join(","));
map.insert("reopen_on_launch", this->reopenOnLaunch ? "1" : "0");
map.insert("pretty_cursors", this->prettyCursors ? "1" : "0");
map.insert("map_sort_order", mapSortOrderMap.value(this->mapSortOrder));
@ -460,8 +463,14 @@ QByteArray PorymapConfig::bytesFromString(QString in) {
return ba;
}
void PorymapConfig::setRecentProject(QString project) {
this->recentProject = project;
void PorymapConfig::addRecentProject(QString project) {
this->recentProjects.removeOne(project);
this->recentProjects.prepend(project);
this->save();
}
void PorymapConfig::setRecentProjects(QStringList projects) {
this->recentProjects = projects;
this->save();
}
@ -599,7 +608,11 @@ void PorymapConfig::setProjectSettingsTab(int tab) {
}
QString PorymapConfig::getRecentProject() {
return this->recentProject;
return this->recentProjects.value(0);
}
QStringList PorymapConfig::getRecentProjects() {
return this->recentProjects;
}
bool PorymapConfig::getReopenOnLaunch() {

View file

@ -65,12 +65,8 @@ MainWindow::MainWindow(QWidget *parent) :
cleanupLargeLog();
this->initWindow();
if (!this->openRecentProject()) {
setWindowDisabled(true);
} else {
setWindowDisabled(false);
if (this->openRecentProject())
on_toolButton_Paint_clicked();
}
// there is a bug affecting macOS users, where the trackpad deilveres a bad touch-release gesture
// the warning is a bit annoying, so it is disabled here
@ -93,6 +89,8 @@ void MainWindow::setWindowDisabled(bool disabled) {
ui->menuBar->setDisabled(false);
ui->menuFile->setDisabled(false);
ui->action_Open_Project->setDisabled(false);
ui->menuOpen_Recent_Project->setDisabled(false);
refreshRecentProjectsMenu();
ui->action_Exit->setDisabled(false);
ui->menuHelp->setDisabled(false);
ui->actionAbout_Porymap->setDisabled(false);
@ -506,6 +504,7 @@ bool MainWindow::openRecentProject() {
bool MainWindow::openProject(QString dir) {
if (dir.isNull()) {
projectOpenFailure = true;
setWindowDisabled(true);
return false;
}
@ -553,18 +552,23 @@ bool MainWindow::openProject(QString dir) {
.arg(getLogPath())
.arg(getMostRecentError());
msgBox.critical(nullptr, "Error Opening Project", errorMsg);
setWindowDisabled(true);
return false;
}
showWindowTitle();
this->statusBar()->showMessage(QString("Opened project %1").arg(nativeDir));
porymapConfig.addRecentProject(dir);
refreshRecentProjectsMenu();
prefab.initPrefabUI(
editor->metatile_selector_item,
ui->scrollAreaWidgetContents_Prefabs,
ui->label_prefabHelp,
editor->map);
Scripting::cb_ProjectOpened(dir);
setWindowDisabled(false);
return true;
}
@ -599,6 +603,36 @@ bool MainWindow::setInitialMap() {
return false;
}
void MainWindow::refreshRecentProjectsMenu() {
ui->menuOpen_Recent_Project->clear();
QStringList recentProjects = porymapConfig.getRecentProjects();
if (isProjectOpen()) {
// Don't show the currently open project in this menu
recentProjects.removeOne(this->editor->project->root);
}
// Add project paths to menu. Arbitrary limit of 10 items.
const int numItems = qMin(10, recentProjects.length());
for (int i = 0; i < numItems; i++) {
const QString path = recentProjects.at(i);
ui->menuOpen_Recent_Project->addAction(path, [this, path](){
this->openProject(path);
});
}
// Add action to clear list of paths
if (!recentProjects.isEmpty()) ui->menuOpen_Recent_Project->addSeparator();
QAction *clearAction = ui->menuOpen_Recent_Project->addAction("Clear Items", [this](){
QStringList paths = QStringList();
if (isProjectOpen())
paths.append(this->editor->project->root);
porymapConfig.setRecentProjects(paths);
this->refreshRecentProjectsMenu();
});
clearAction->setEnabled(!recentProjects.isEmpty());
}
void MainWindow::openSubWindow(QWidget * window) {
if (!window) return;
@ -628,8 +662,7 @@ void MainWindow::on_action_Open_Project_triggered()
Scripting::cb_ProjectClosed(this->editor->project->root);
this->ui->graphicsView_Map->clearOverlayMap();
}
porymapConfig.setRecentProject(dir);
setWindowDisabled(!openProject(dir));
openProject(dir);
}
}
@ -642,10 +675,8 @@ void MainWindow::on_action_Reload_Project_triggered() {
warning.setDefaultButton(QMessageBox::Cancel);
warning.setIcon(QMessageBox::Warning);
if (warning.exec() == QMessageBox::Ok) {
if (!openProject(editor->project->root))
setWindowDisabled(true);
}
if (warning.exec() == QMessageBox::Ok)
openProject(editor->project->root);
}
bool MainWindow::setMap(QString map_name, bool scrollTreeView) {