Merge pull request #569 from GriffinRichards/recent-projects

Add 'Open Recent Project' menu
This commit is contained in:
GriffinR 2023-12-31 17:39:43 -05:00 committed by GitHub
commit 63102eccd6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 70 additions and 19 deletions

View file

@ -3021,7 +3021,13 @@
<property name="title"> <property name="title">
<string>File</string> <string>File</string>
</property> </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="action_Open_Project"/>
<addaction name="menuOpen_Recent_Project"/>
<addaction name="action_Reload_Project"/> <addaction name="action_Reload_Project"/>
<addaction name="action_Save"/> <addaction name="action_Save"/>
<addaction name="action_Save_Project"/> <addaction name="action_Save_Project"/>

View file

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

View file

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

View file

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

View file

@ -65,12 +65,8 @@ MainWindow::MainWindow(QWidget *parent) :
cleanupLargeLog(); cleanupLargeLog();
this->initWindow(); this->initWindow();
if (!this->openRecentProject()) { if (this->openRecentProject())
setWindowDisabled(true);
} else {
setWindowDisabled(false);
on_toolButton_Paint_clicked(); on_toolButton_Paint_clicked();
}
// there is a bug affecting macOS users, where the trackpad deilveres a bad touch-release gesture // 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 // 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->menuBar->setDisabled(false);
ui->menuFile->setDisabled(false); ui->menuFile->setDisabled(false);
ui->action_Open_Project->setDisabled(false); ui->action_Open_Project->setDisabled(false);
ui->menuOpen_Recent_Project->setDisabled(false);
refreshRecentProjectsMenu();
ui->action_Exit->setDisabled(false); ui->action_Exit->setDisabled(false);
ui->menuHelp->setDisabled(false); ui->menuHelp->setDisabled(false);
ui->actionAbout_Porymap->setDisabled(false); ui->actionAbout_Porymap->setDisabled(false);
@ -506,6 +504,7 @@ bool MainWindow::openRecentProject() {
bool MainWindow::openProject(QString dir) { bool MainWindow::openProject(QString dir) {
if (dir.isNull()) { if (dir.isNull()) {
projectOpenFailure = true; projectOpenFailure = true;
setWindowDisabled(true);
return false; return false;
} }
@ -553,18 +552,23 @@ bool MainWindow::openProject(QString dir) {
.arg(getLogPath()) .arg(getLogPath())
.arg(getMostRecentError()); .arg(getMostRecentError());
msgBox.critical(nullptr, "Error Opening Project", errorMsg); msgBox.critical(nullptr, "Error Opening Project", errorMsg);
setWindowDisabled(true);
return false; return false;
} }
showWindowTitle(); showWindowTitle();
this->statusBar()->showMessage(QString("Opened project %1").arg(nativeDir)); this->statusBar()->showMessage(QString("Opened project %1").arg(nativeDir));
porymapConfig.addRecentProject(dir);
refreshRecentProjectsMenu();
prefab.initPrefabUI( prefab.initPrefabUI(
editor->metatile_selector_item, editor->metatile_selector_item,
ui->scrollAreaWidgetContents_Prefabs, ui->scrollAreaWidgetContents_Prefabs,
ui->label_prefabHelp, ui->label_prefabHelp,
editor->map); editor->map);
Scripting::cb_ProjectOpened(dir); Scripting::cb_ProjectOpened(dir);
setWindowDisabled(false);
return true; return true;
} }
@ -599,6 +603,36 @@ bool MainWindow::setInitialMap() {
return false; 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) { void MainWindow::openSubWindow(QWidget * window) {
if (!window) return; if (!window) return;
@ -628,8 +662,7 @@ void MainWindow::on_action_Open_Project_triggered()
Scripting::cb_ProjectClosed(this->editor->project->root); Scripting::cb_ProjectClosed(this->editor->project->root);
this->ui->graphicsView_Map->clearOverlayMap(); this->ui->graphicsView_Map->clearOverlayMap();
} }
porymapConfig.setRecentProject(dir); openProject(dir);
setWindowDisabled(!openProject(dir));
} }
} }
@ -642,10 +675,8 @@ void MainWindow::on_action_Reload_Project_triggered() {
warning.setDefaultButton(QMessageBox::Cancel); warning.setDefaultButton(QMessageBox::Cancel);
warning.setIcon(QMessageBox::Warning); warning.setIcon(QMessageBox::Warning);
if (warning.exec() == QMessageBox::Ok) { if (warning.exec() == QMessageBox::Ok)
if (!openProject(editor->project->root)) openProject(editor->project->root);
setWindowDisabled(true);
}
} }
bool MainWindow::setMap(QString map_name, bool scrollTreeView) { bool MainWindow::setMap(QString map_name, bool scrollTreeView) {