Skip to content

Commit

Permalink
Add 'Open Recent Project' menu
Browse files Browse the repository at this point in the history
  • Loading branch information
GriffinRichards committed Dec 28, 2023
1 parent 6a8d3a8 commit 6fedab9
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 19 deletions.
6 changes: 6 additions & 0 deletions forms/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
Expand Down
8 changes: 5 additions & 3 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class PorymapConfig: public KeyValueConfigBase
reset();
}
virtual void reset() override {
this->recentProject = "";
this->recentProjects.clear();
this->reopenOnLaunch = true;
this->mapSortOrder = MapSortOrder::Group;
this->prettyCursors = true;
Expand All @@ -73,7 +73,8 @@ class PorymapConfig: public KeyValueConfigBase
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);
Expand Down Expand Up @@ -101,6 +102,7 @@ class PorymapConfig: public KeyValueConfigBase
void setProjectSettingsTab(int tab);
void setWarpBehaviorWarningDisabled(bool disabled);
QString getRecentProject();
QStringList getRecentProjects();
bool getReopenOnLaunch();
MapSortOrder getMapSortOrder();
bool getPrettyCursors();
Expand Down Expand Up @@ -134,7 +136,7 @@ class PorymapConfig: public KeyValueConfigBase
virtual void onNewConfigFileCreated() override {};
virtual void setUnreadKeys() override {};
private:
QString recentProject;
QStringList recentProjects;
bool reopenOnLaunch;
QString stringFromByteArray(QByteArray);
QByteArray bytesFromString(QString);
Expand Down
1 change: 1 addition & 0 deletions include/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ private slots:
bool setInitialMap();
void setRecentMap(QString map_name);
QStandardItem* createMapItem(QString mapName, int groupNum, int inGroupNum);
void refreshRecentProjectsMenu();

void drawMapListIcons(QAbstractItemModel *model);
void updateMapList();
Expand Down
23 changes: 18 additions & 5 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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() {
Expand Down
53 changes: 42 additions & 11 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -506,6 +504,7 @@ bool MainWindow::openRecentProject() {
bool MainWindow::openProject(QString dir) {
if (dir.isNull()) {
projectOpenFailure = true;
setWindowDisabled(true);
return false;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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) {
Expand Down

0 comments on commit 6fedab9

Please sign in to comment.