Skip to content

Commit b50efbe

Browse files
authored
Create a list of recent files (#1885)
* Create a list of recent files * Improve and format code * TextManager should not know anything about the SettingsManager * Lupdate
1 parent 39e1c3a commit b50efbe

File tree

11 files changed

+79
-10
lines changed

11 files changed

+79
-10
lines changed

qrgui/mainWindow/mainWindow.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ MainWindow::MainWindow(const QString &fileToOpen)
112112
, mRootIndex(QModelIndex())
113113
, mPreferencesDialog(new gui::PreferencesDialog(this))
114114
, mRecentProjectsLimit(SettingsManager::value("recentProjectsLimit").toInt())
115+
, mRecentFilesLimit(SettingsManager::value("recentFilesLimit").toInt())
115116
, mSceneCustomizer(new SceneCustomizer())
116117
, mInitialFileToOpen(fileToOpen)
117118
{
@@ -343,6 +344,7 @@ void MainWindow::connectActions()
343344
connect(mUi->tabs, &QTabWidget::currentChanged, this, &MainWindow::sceneSelectionChanged);
344345
connect(&*mTextManager, &text::TextManager::textChanged, this, &MainWindow::setTextChanged);
345346
connect(&*mTextManager, &text::TextManager::textChanged, mUi->actionUndo, &QAction::setEnabled);
347+
connect(&*mTextManager, &text::TextManager::needRefreshRecentFileList, this, &MainWindow::refreshRecentFilesList);
346348

347349
connect(&*mProjectManager, &ProjectManager::afterOpen, mUi->paletteTree, &PaletteTree::refreshUserPalettes);
348350
connect(&*mProjectManager, &ProjectManager::closed, mUi->paletteTree, &PaletteTree::refreshUserPalettes);
@@ -598,6 +600,31 @@ void MainWindow::refreshRecentProjectsList(const QString &fileName)
598600
SettingsManager::setValue("recentProjects", previousString);
599601
}
600602

603+
void MainWindow::refreshRecentFilesList(const QString &fileName) {
604+
auto previousString = SettingsManager::value("recentFiles").toString();
605+
auto previousList = previousString.split(";", QString::SkipEmptyParts);
606+
previousList.removeOne(fileName);
607+
608+
if (!previousList.isEmpty() && (previousList.size() == mRecentFilesLimit)) {
609+
previousList.removeLast();
610+
}
611+
612+
previousString.clear();
613+
if (mRecentFilesLimit > 0) {
614+
previousList.push_front(fileName);
615+
QStringListIterator iterator(previousList);
616+
while (iterator.hasNext()) {
617+
const auto recentFileName = iterator.next();
618+
const QFileInfo fileInfo(recentFileName);
619+
if (fileInfo.exists() && fileInfo.isFile()) {
620+
previousString = previousString + recentFileName + ";";
621+
}
622+
}
623+
}
624+
625+
SettingsManager::setValue("recentFiles", previousString);
626+
}
627+
601628
void MainWindow::openRecentProjectsMenu()
602629
{
603630
mRecentProjectsMenu->clear();
@@ -616,6 +643,26 @@ void MainWindow::openRecentProjectsMenu()
616643
}
617644
}
618645

646+
void MainWindow::openRecentFilesMenu()
647+
{
648+
mRecentFilesMenu->clear();
649+
const auto stringList = SettingsManager::value("recentFiles").toString();
650+
auto recentFiles = stringList.split(";", QString::SkipEmptyParts);
651+
mRecentFilesLimit = SettingsManager::value("recentFilesLimit", mRecentFilesLimit).toInt();
652+
while (recentFiles.size() > mRecentFilesLimit) {
653+
recentFiles.pop_front();
654+
}
655+
656+
for (auto &&filePath : recentFiles) {
657+
const QFileInfo fileInfo(filePath);
658+
if (fileInfo.exists() && fileInfo.isFile()) {
659+
mRecentFilesMenu->addAction(filePath);
660+
QObject::connect(mRecentFilesMenu->actions().last(), &QAction::triggered
661+
, &*mProjectManager, [this, filePath](){ mProjectManager->openExisting(filePath);});
662+
}
663+
}
664+
}
665+
619666
void MainWindow::tryToSave()
620667
{
621668
if(!mProjectManager->saveText() && !mProjectManager->saveOrSuggestToSaveAs()) {
@@ -2149,6 +2196,10 @@ void MainWindow::initRecentProjectsMenu()
21492196
mRecentProjectsMenu = new QMenu(tr("Recent projects"), mUi->menu_File);
21502197
mUi->menu_File->insertMenu(mUi->menu_File->actions().at(1), mRecentProjectsMenu);
21512198
connect(mRecentProjectsMenu, &QMenu::aboutToShow, this, &MainWindow::openRecentProjectsMenu);
2199+
2200+
mRecentFilesMenu = new QMenu(tr("Recent files"), mUi->menu_File);
2201+
mUi->menu_File->insertMenu(mUi->menu_File->actions().at(2), mRecentFilesMenu);
2202+
connect(mRecentFilesMenu, &QMenu::aboutToShow, this, &MainWindow::openRecentFilesMenu);
21522203
}
21532204

21542205
void MainWindow::saveDiagramAsAPictureToFile(const QString &fileName)

qrgui/mainWindow/mainWindow.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ public slots:
195195
void closeStartTab();
196196
void closeAllTabs();
197197
void refreshRecentProjectsList(const QString &fileName);
198+
void refreshRecentFilesList(const QString &fileName);
198199
void createDiagram(const QString &idString);
199200
/// Creates project with specified root diagram
200201
bool createProject(const QString &diagramIdString);
@@ -230,6 +231,7 @@ private slots:
230231
void hideBottomDocks();
231232

232233
void openRecentProjectsMenu();
234+
void openRecentFilesMenu();
233235

234236
void tryToSave();
235237
void saveDiagramAsAPicture();
@@ -399,7 +401,9 @@ private slots:
399401
qReal::gui::PreferencesDialog *mPreferencesDialog; //Has ownership
400402

401403
int mRecentProjectsLimit {};
404+
int mRecentFilesLimit {};
402405
QMenu *mRecentProjectsMenu {}; // Has ownership
406+
QMenu *mRecentFilesMenu {}; // Has ownership
403407

404408
QScopedPointer<FindManager> mFindHelper;
405409
StartWidget *mStartWidget {}; // Has ownership

qrgui/systemFacade/components/nullTextManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class QRGUI_SYSTEM_FACADE_EXPORT NullTextManager : public TextManagerInterface
4646

4747
void showInTextEditor(const QFileInfo &fileInfo, const QString &genName
4848
, const text::LanguageInfo &language) override;
49+
4950
void showInTextEditor(const QFileInfo &fileInfo, const text::LanguageInfo &language) override;
5051

5152
bool saveText(bool saveAs) override;

qrgui/textEditor/textManager.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ void TextManager::showInTextEditor(const QFileInfo &fileInfo, const text::Langua
254254
return;
255255
}
256256

257+
emit needRefreshRecentFileList(filePath);
257258
area->show();
258259

259260
// Need to bind diagram and code file only if code is just generated
@@ -289,7 +290,6 @@ bool TextManager::saveText(bool saveAs)
289290

290291
if (!fileInfo.fileName().isEmpty()) {
291292
mMainWindow.setTabText(area, fileInfo.fileName());
292-
293293
utils::OutFile out(fileInfo.absoluteFilePath());
294294

295295
out() << area->text();
@@ -304,6 +304,8 @@ bool TextManager::saveText(bool saveAs)
304304
if (saveAs && !diagram.isNull()) {
305305
emit mSystemEvents.codePathChanged(diagram, path(area), fileInfo);
306306
}
307+
308+
emit needRefreshRecentFileList(fileInfo.absoluteFilePath());
307309
}
308310

309311
return true;

qrgui/textEditor/textManager.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <QtCore/QObject>
1818
#include <QtCore/QMap>
1919
#include <QtCore/QMultiHash>
20-
2120
#include "qrgui/textEditor/textEditorDeclSpec.h"
2221
#include "qrgui/textEditor/textManagerInterface.h"
2322
#include "qrgui/textEditor/codeBlockManager.h"

qrgui/textEditor/textManagerInterface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class QRGUI_TEXT_EDITOR_EXPORT TextManagerInterface : public QObject
5656

5757
signals:
5858
void textChanged(text::QScintillaTextEdit *editor, bool changed);
59+
void needRefreshRecentFileList(const QString &fileName);
5960
};
6061

6162
}

qrkernel/settingsDefaultValues

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ pythonPath=python
6767
generationTimeout=100
6868
nodesStateButtonExpands=true
6969
recentProjectsLimit=5
70+
recentFilesLimit=7
7071
dragArea = 12
7172
touchMode=false
7273
scriptInterpretation=false

qrtranslations/fr/qrgui_mainWindow_fr.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@
708708
<context>
709709
<name>qReal::MainWindow</name>
710710
<message>
711-
<location filename="../../qrgui/mainWindow/mainWindow.cpp" line="+622"/>
711+
<location filename="../../qrgui/mainWindow/mainWindow.cpp" line="+669"/>
712712
<source>Could not save file, try to save it to another place</source>
713713
<translation type="unfinished"></translation>
714714
</message>
@@ -717,7 +717,7 @@
717717
<translation type="vanished">À propo de QReal</translation>
718718
</message>
719719
<message>
720-
<location line="-425"/>
720+
<location line="-471"/>
721721
<source>Restore default settings</source>
722722
<translation type="unfinished"></translation>
723723
</message>
@@ -729,7 +729,7 @@ WARNING: The settings will be restored after application restart</source>
729729
<translation type="unfinished"></translation>
730730
</message>
731731
<message>
732-
<location line="+703"/>
732+
<location line="+749"/>
733733
<location line="+11"/>
734734
<source>Error</source>
735735
<translation>Erreur</translation>
@@ -861,6 +861,11 @@ WARNING: The settings will be restored after application restart</source>
861861
<source>Recent projects</source>
862862
<translation>Projets récents</translation>
863863
</message>
864+
<message>
865+
<location line="+4"/>
866+
<source>Recent files</source>
867+
<translation type="unfinished"></translation>
868+
</message>
864869
<message>
865870
<location line="+31"/>
866871
<source>Save File</source>

qrtranslations/fr/qrgui_textEditor_fr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<translation type="unfinished"></translation>
1515
</message>
1616
<message>
17-
<location line="+192"/>
17+
<location line="+193"/>
1818
<source>All files (*)</source>
1919
<translation>Tous les fichiers (*)</translation>
2020
</message>

qrtranslations/ru/qrgui_mainWindow_ru.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@
735735
<translation type="vanished">О QReal</translation>
736736
</message>
737737
<message>
738-
<location filename="../../qrgui/mainWindow/mainWindow.cpp" line="+901"/>
738+
<location filename="../../qrgui/mainWindow/mainWindow.cpp" line="+948"/>
739739
<location line="+11"/>
740740
<source>Error</source>
741741
<translation>Ошибка</translation>
@@ -788,7 +788,7 @@
788788
<translation>Создать диаграмму</translation>
789789
</message>
790790
<message>
791-
<location line="-1077"/>
791+
<location line="-1123"/>
792792
<source>Restore default settings</source>
793793
<translation>Восстановить настройки по-умолчанию</translation>
794794
</message>
@@ -802,7 +802,7 @@ WARNING: The settings will be restored after application restart</source>
802802
ВНИМАНИЕ: Настройки будут сброшены после перезапуска приложения</translation>
803803
</message>
804804
<message>
805-
<location line="+424"/>
805+
<location line="+470"/>
806806
<source>Could not save file, try to save it to another place</source>
807807
<translation>Не удалось сохранить файл, попробуйте сохранить его в другое место</translation>
808808
</message>
@@ -886,6 +886,11 @@ WARNING: The settings will be restored after application restart</source>
886886
<source>Recent projects</source>
887887
<translation>Недавние проекты</translation>
888888
</message>
889+
<message>
890+
<location line="+4"/>
891+
<source>Recent files</source>
892+
<translation>Недавние файлы</translation>
893+
</message>
889894
<message>
890895
<location line="+31"/>
891896
<source>Save File</source>

0 commit comments

Comments
 (0)