1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-10 00:43:59 +02:00

Merge pull request #4939 from Kuxe/map_editor_open_recent-v2

Add 'Open Recent' to mapeditor
This commit is contained in:
Ivan Savenko 2024-12-10 13:07:01 +02:00 committed by GitHub
commit 81d53497de
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 133 additions and 0 deletions

View File

@ -205,6 +205,7 @@
* Fixed duplicated list of spells in Mage Guild in copy-pasted towns * Fixed duplicated list of spells in Mage Guild in copy-pasted towns
* Removed separate versioning of map editor. Map editor now has same version as VCMI * Removed separate versioning of map editor. Map editor now has same version as VCMI
* Timed events interfaces now counts days from 1, instead of from 0 * Timed events interfaces now counts days from 1, instead of from 0
* Added Recent Files to File Menu and Toolbar
* Fixed crash on attempting to save map with random dwelling * Fixed crash on attempting to save map with random dwelling
### Modding ### Modding

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

View File

@ -16,6 +16,8 @@
#include <QFile> #include <QFile>
#include <QMessageBox> #include <QMessageBox>
#include <QFileInfo> #include <QFileInfo>
#include <QDialog>
#include <QListWidget>
#include "../lib/VCMIDirs.h" #include "../lib/VCMIDirs.h"
#include "../lib/VCMI_Lib.h" #include "../lib/VCMI_Lib.h"
@ -222,6 +224,8 @@ MainWindow::MainWindow(QWidget* parent) :
ui->toolFill->setIcon(QIcon{":/icons/tool-fill.png"}); ui->toolFill->setIcon(QIcon{":/icons/tool-fill.png"});
ui->toolSelect->setIcon(QIcon{":/icons/tool-select.png"}); ui->toolSelect->setIcon(QIcon{":/icons/tool-select.png"});
ui->actionOpen->setIcon(QIcon{":/icons/document-open.png"}); ui->actionOpen->setIcon(QIcon{":/icons/document-open.png"});
ui->actionOpenRecent->setIcon(QIcon{":/icons/document-open-recent.png"});
ui->menuOpenRecent->setIcon(QIcon{":/icons/document-open-recent.png"});
ui->actionSave->setIcon(QIcon{":/icons/document-save.png"}); ui->actionSave->setIcon(QIcon{":/icons/document-save.png"});
ui->actionNew->setIcon(QIcon{":/icons/document-new.png"}); ui->actionNew->setIcon(QIcon{":/icons/document-new.png"});
ui->actionLevel->setIcon(QIcon{":/icons/toggle-underground.png"}); ui->actionLevel->setIcon(QIcon{":/icons/toggle-underground.png"});
@ -265,6 +269,8 @@ MainWindow::MainWindow(QWidget* parent) :
scenePreview = new QGraphicsScene(this); scenePreview = new QGraphicsScene(this);
ui->objectPreview->setScene(scenePreview); ui->objectPreview->setScene(scenePreview);
connect(ui->actionOpenRecentMore, &QAction::triggered, this, &MainWindow::on_actionOpenRecent_triggered);
//loading objects //loading objects
loadObjectsTree(); loadObjectsTree();
@ -412,9 +418,21 @@ bool MainWindow::openMap(const QString & filenameSelect)
filename = filenameSelect; filename = filenameSelect;
initializeMap(controller.map()->version != EMapFormat::VCMI); initializeMap(controller.map()->version != EMapFormat::VCMI);
updateRecentMenu(filenameSelect);
return true; return true;
} }
void MainWindow::updateRecentMenu(const QString & filenameSelect) {
QSettings s(Ui::teamName, Ui::appName);
QStringList recentFiles = s.value(recentlyOpenedFilesSetting).toStringList();
recentFiles.removeAll(filenameSelect);
recentFiles.prepend(filenameSelect);
constexpr int maxRecentFiles = 10;
s.setValue(recentlyOpenedFilesSetting, QStringList(recentFiles.mid(0, maxRecentFiles)));
}
void MainWindow::on_actionOpen_triggered() void MainWindow::on_actionOpen_triggered()
{ {
if(!getAnswerAboutUnsavedChanges()) if(!getAnswerAboutUnsavedChanges())
@ -429,6 +447,91 @@ void MainWindow::on_actionOpen_triggered()
openMap(filenameSelect); openMap(filenameSelect);
} }
void MainWindow::on_actionOpenRecent_triggered()
{
QSettings s(Ui::teamName, Ui::appName);
QStringList recentFiles = s.value(recentlyOpenedFilesSetting).toStringList();
class RecentFileDialog : public QDialog
{
public:
RecentFileDialog(const QStringList& recentFiles, QWidget *parent)
: QDialog(parent), layout(new QVBoxLayout(this)), listWidget(new QListWidget(this))
{
setWindowTitle(tr("Recently Opened Files"));
setMinimumWidth(600);
connect(listWidget, &QListWidget::itemActivated, this, [this](QListWidgetItem *item)
{
accept();
});
for (const QString &file : recentFiles)
{
QListWidgetItem *item = new QListWidgetItem(file);
listWidget->addItem(item);
}
// Select most recent items by default.
// This enables a "CTRL+R => Enter"-workflow instead of "CTRL+R => 'mouse click on first item'"
if(listWidget->count() > 0)
{
listWidget->item(0)->setSelected(true);
}
layout->setSizeConstraint(QLayout::SetMaximumSize);
layout->addWidget(listWidget);
}
QString getSelectedFilePath() const
{
return listWidget->currentItem()->text();
}
private:
QVBoxLayout * layout;
QListWidget * listWidget;
};
RecentFileDialog d(recentFiles, this);
if(d.exec() == QDialog::Accepted && getAnswerAboutUnsavedChanges())
{
openMap(d.getSelectedFilePath());
}
}
void MainWindow::on_menuOpenRecent_aboutToShow()
{
// Clear all actions except "More...", lest the list will grow with each
// showing of the list
for (QAction* action : ui->menuOpenRecent->actions()) {
if (action != ui->actionOpenRecentMore) {
ui->menuOpenRecent->removeAction(action);
}
}
QSettings s(Ui::teamName, Ui::appName);
QStringList recentFiles = s.value(recentlyOpenedFilesSetting).toStringList();
// Dynamically populate menuOpenRecent with one action per file.
for (const QString & file : recentFiles) {
QAction *action = new QAction(file, this);
ui->menuOpenRecent->insertAction(ui->actionOpenRecentMore, action);
connect(action, &QAction::triggered, this, [this, file]() {
if(!getAnswerAboutUnsavedChanges())
return;
openMap(file);
});
}
// Finally add a separator between recent entries and "More..."
if(recentFiles.size() > 0) {
ui->menuOpenRecent->insertSeparator(ui->actionOpenRecentMore);
}
}
void MainWindow::saveMap() void MainWindow::saveMap()
{ {
if(!controller.map()) if(!controller.map())

View File

@ -28,6 +28,7 @@ class MainWindow : public QMainWindow
const QString mainWindowSizeSetting = "MainWindow/Size"; const QString mainWindowSizeSetting = "MainWindow/Size";
const QString mainWindowPositionSetting = "MainWindow/Position"; const QString mainWindowPositionSetting = "MainWindow/Position";
const QString lastDirectorySetting = "MainWindow/Directory"; const QString lastDirectorySetting = "MainWindow/Directory";
const QString recentlyOpenedFilesSetting = "MainWindow/RecentlyOpenedFiles";
#ifdef ENABLE_QT_TRANSLATIONS #ifdef ENABLE_QT_TRANSLATIONS
QTranslator translator; QTranslator translator;
@ -59,6 +60,10 @@ public:
private slots: private slots:
void on_actionOpen_triggered(); void on_actionOpen_triggered();
void on_actionOpenRecent_triggered();
void on_menuOpenRecent_aboutToShow();
void on_actionSave_as_triggered(); void on_actionSave_as_triggered();
void on_actionNew_triggered(); void on_actionNew_triggered();
@ -170,6 +175,8 @@ private:
void parseCommandLine(ExtractionOptions & extractionOptions); void parseCommandLine(ExtractionOptions & extractionOptions);
void updateRecentMenu(const QString & filenameSelect);
private: private:
Ui::MainWindow * ui; Ui::MainWindow * ui;
ObjectBrowserProxyModel * objectBrowser = nullptr; ObjectBrowserProxyModel * objectBrowser = nullptr;

View File

@ -58,8 +58,15 @@
<property name="title"> <property name="title">
<string>File</string> <string>File</string>
</property> </property>
<widget class="QMenu" name="menuOpenRecent">
<property name="title">
<string>Open Recent</string>
</property>
<addaction name="actionOpenRecentMore"/>
</widget>
<addaction name="actionNew"/> <addaction name="actionNew"/>
<addaction name="actionOpen"/> <addaction name="actionOpen"/>
<addaction name="menuOpenRecent"/>
<addaction name="actionSave"/> <addaction name="actionSave"/>
<addaction name="actionSave_as"/> <addaction name="actionSave_as"/>
<addaction name="actionExport"/> <addaction name="actionExport"/>
@ -133,6 +140,7 @@
</attribute> </attribute>
<addaction name="actionNew"/> <addaction name="actionNew"/>
<addaction name="actionOpen"/> <addaction name="actionOpen"/>
<addaction name="actionOpenRecent"/>
<addaction name="actionSave"/> <addaction name="actionSave"/>
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="actionUndo"/> <addaction name="actionUndo"/>
@ -1019,6 +1027,19 @@
<string notr="true">Ctrl+O</string> <string notr="true">Ctrl+O</string>
</property> </property>
</action> </action>
<action name="actionOpenRecent">
<property name="text">
<string>Open Recent</string>
</property>
</action>
<action name="actionOpenRecentMore">
<property name="text">
<string>More...</string>
</property>
<property name="shortcut">
<string notr="true">Ctrl+R</string>
</property>
</action>
<action name="actionSave"> <action name="actionSave">
<property name="text"> <property name="text">
<string>Save</string> <string>Save</string>

View File

@ -5,6 +5,7 @@
<file>icons/brush-4.png</file> <file>icons/brush-4.png</file>
<file>icons/document-new.png</file> <file>icons/document-new.png</file>
<file>icons/document-open.png</file> <file>icons/document-open.png</file>
<file>icons/document-open-recent.png</file>
<file>icons/document-save.png</file> <file>icons/document-save.png</file>
<file>icons/edit-clear.png</file> <file>icons/edit-clear.png</file>
<file>icons/edit-copy.png</file> <file>icons/edit-copy.png</file>