Skip to content

Commit 5e0706f

Browse files
committed
Create a list of recent files
1 parent 92840bf commit 5e0706f

File tree

10 files changed

+84
-14
lines changed

10 files changed

+84
-14
lines changed

qrgui/mainWindow/mainWindow.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ MainWindow::MainWindow(const QString &fileToOpen)
111111
, mRootIndex(QModelIndex())
112112
, mPreferencesDialog(new gui::PreferencesDialog(this))
113113
, mRecentProjectsLimit(SettingsManager::value("recentProjectsLimit").toInt())
114+
, mRecentFilesLimit(SettingsManager::value("recentFilesLimit").toInt())
114115
, mSceneCustomizer(new SceneCustomizer())
115116
, mInitialFileToOpen(fileToOpen)
116117
{
@@ -614,6 +615,26 @@ void MainWindow::openRecentProjectsMenu()
614615
}
615616
}
616617

618+
void MainWindow::openRecentFilesMenu()
619+
{
620+
mRecentFilesMenu->clear();
621+
const QString stringList = SettingsManager::value("recentFiles").toString();
622+
QStringList recentFiles = stringList.split(";", QString::SkipEmptyParts);
623+
mRecentFilesLimit = SettingsManager::value("recentFilesLimit", mRecentFilesLimit).toInt();
624+
while (recentFiles.size() > mRecentFilesLimit) {
625+
recentFiles.pop_front();
626+
}
627+
628+
for (const QString &filePath : recentFiles) {
629+
const QFileInfo fileInfo(filePath);
630+
if (fileInfo.exists() && fileInfo.isFile()) {
631+
mRecentFilesMenu->addAction(filePath);
632+
QObject::connect(mRecentFilesMenu->actions().last(), &QAction::triggered
633+
, &*mProjectManager, [this, filePath](){ mProjectManager->openExisting(filePath);});
634+
}
635+
}
636+
}
637+
617638
void MainWindow::tryToSave()
618639
{
619640
if(!mProjectManager->saveText() && !mProjectManager->saveOrSuggestToSaveAs()) {
@@ -2117,6 +2138,10 @@ void MainWindow::initRecentProjectsMenu()
21172138
mRecentProjectsMenu = new QMenu(tr("Recent projects"), mUi->menu_File);
21182139
mUi->menu_File->insertMenu(mUi->menu_File->actions().at(1), mRecentProjectsMenu);
21192140
connect(mRecentProjectsMenu, &QMenu::aboutToShow, this, &MainWindow::openRecentProjectsMenu);
2141+
2142+
mRecentFilesMenu = new QMenu(tr("Recent files"), mUi->menu_File);
2143+
mUi->menu_File->insertMenu(mUi->menu_File->actions().at(2), mRecentFilesMenu);
2144+
connect(mRecentFilesMenu, &QMenu::aboutToShow, this, &MainWindow::openRecentFilesMenu);
21202145
}
21212146

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

qrgui/mainWindow/mainWindow.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ private slots:
230230
void hideBottomDocks();
231231

232232
void openRecentProjectsMenu();
233+
void openRecentFilesMenu();
233234

234235
void tryToSave();
235236
void saveDiagramAsAPicture();
@@ -395,7 +396,9 @@ private slots:
395396
qReal::gui::PreferencesDialog *mPreferencesDialog; //Has ownership
396397

397398
int mRecentProjectsLimit {};
399+
int mRecentFilesLimit {};
398400
QMenu *mRecentProjectsMenu {}; // Has ownership
401+
QMenu *mRecentFilesMenu {}; // Has ownership
399402

400403
QScopedPointer<FindManager> mFindHelper;
401404
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: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ using namespace qReal;
3030
using namespace text;
3131

3232
TextManager::TextManager(SystemEvents &systemEvents, gui::MainWindowInterpretersInterface &mainWindow)
33-
: mMainWindow(mainWindow)
33+
: mRecentFilesLimit(SettingsManager::value("recentFilesLimit").toInt())
34+
, mMainWindow(mainWindow)
3435
, mSystemEvents(systemEvents)
3536
{
3637
connect(&mSystemEvents, &SystemEvents::codeTabClosed, this, &TextManager::onTabClosed);
@@ -238,6 +239,31 @@ void TextManager::showInTextEditor(const QFileInfo &fileInfo
238239
}
239240
}
240241

242+
void TextManager::refreshRecentFilesList(const QString &fileName) {
243+
QString previousString = SettingsManager::value("recentFiles").toString();
244+
QStringList previousList = previousString.split(";", QString::SkipEmptyParts);
245+
previousList.removeOne(fileName);
246+
247+
if (!previousList.isEmpty() && (previousList.size() == mRecentFilesLimit)) {
248+
previousList.removeLast();
249+
}
250+
251+
previousString.clear();
252+
if (mRecentFilesLimit > 0) {
253+
previousList.push_front(fileName);
254+
QStringListIterator iterator(previousList);
255+
while (iterator.hasNext()) {
256+
const auto recentFileName = iterator.next();
257+
const QFileInfo fileInfo(recentFileName);
258+
if (fileInfo.exists() && fileInfo.isFile()) {
259+
previousString = previousString + recentFileName + ";";
260+
}
261+
}
262+
}
263+
264+
SettingsManager::setValue("recentFiles", previousString);
265+
}
266+
241267
void TextManager::showInTextEditor(const QFileInfo &fileInfo, const text::LanguageInfo &language)
242268
{
243269
Q_ASSERT(!fileInfo.completeBaseName().isEmpty());
@@ -254,6 +280,7 @@ void TextManager::showInTextEditor(const QFileInfo &fileInfo, const text::Langua
254280
return;
255281
}
256282

283+
refreshRecentFilesList(filePath);
257284
area->show();
258285

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

290317
if (!fileInfo.fileName().isEmpty()) {
291318
mMainWindow.setTabText(area, fileInfo.fileName());
292-
293319
utils::OutFile out(fileInfo.absoluteFilePath());
294320

295321
out() << area->text();
@@ -304,6 +330,8 @@ bool TextManager::saveText(bool saveAs)
304330
if (saveAs && !diagram.isNull()) {
305331
emit mSystemEvents.codePathChanged(diagram, path(area), fileInfo);
306332
}
333+
334+
refreshRecentFilesList(fileInfo.absoluteFilePath());
307335
}
308336

309337
return true;

qrgui/textEditor/textManager.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <QtCore/QObject>
1818
#include <QtCore/QMap>
1919
#include <QtCore/QMultiHash>
20-
20+
#include <qrkernel/settingsManager.h>
2121
#include "qrgui/textEditor/textEditorDeclSpec.h"
2222
#include "qrgui/textEditor/textManagerInterface.h"
2323
#include "qrgui/textEditor/codeBlockManager.h"
@@ -82,12 +82,14 @@ private slots:
8282
void onTabClosed(const QFileInfo &file);
8383

8484
private:
85+
86+
void refreshRecentFilesList(const QString &fileName);
8587
QMap<QString, text::QScintillaTextEdit*> mText; // No ownership
8688
QMap<text::QScintillaTextEdit*, QString> mPath; // No ownership
8789

8890
/// If default path - true.
8991
QMap<QString, bool> mPathType;
90-
92+
int mRecentFilesLimit {};
9193
/// Contains names of generator, which generate each file
9294
QMap<QString, QString> mGeneratorName;
9395

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="+620"/>
711+
<location filename="../../qrgui/mainWindow/mainWindow.cpp" line="+641"/>
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="-445"/>
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="+673"/>
732+
<location line="+693"/>
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<context>
55
<name>qReal::text::TextManager</name>
66
<message>
7-
<location filename="../../qrgui/textEditor/textManager.cpp" line="+86"/>
7+
<location filename="../../qrgui/textEditor/textManager.cpp" line="+87"/>
88
<source>Confirmation</source>
99
<translation type="unfinished"></translation>
1010
</message>
@@ -14,7 +14,7 @@
1414
<translation type="unfinished"></translation>
1515
</message>
1616
<message>
17-
<location line="+192"/>
17+
<location line="+218"/>
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="+869"/>
738+
<location filename="../../qrgui/mainWindow/mainWindow.cpp" line="+890"/>
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="-1047"/>
791+
<location line="-1067"/>
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="+444"/>
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>

qrtranslations/ru/qrgui_textEditor_ru.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<context>
4343
<name>qReal::text::TextManager</name>
4444
<message>
45-
<location filename="../../qrgui/textEditor/textManager.cpp" line="+86"/>
45+
<location filename="../../qrgui/textEditor/textManager.cpp" line="+87"/>
4646
<source>Confirmation</source>
4747
<translation>Подтвердите</translation>
4848
</message>
@@ -56,7 +56,7 @@
5656
<translation>Сохранить перед закрытием?</translation>
5757
</message>
5858
<message>
59-
<location line="+192"/>
59+
<location line="+218"/>
6060
<source>All files (*)</source>
6161
<translation>Все файлы (*)</translation>
6262
</message>

0 commit comments

Comments
 (0)