5151#include " core_windowmanager.h"
5252#include " core_logview.h"
5353
54+ #define RECENT_PROJECTS_COUNT 20
55+
5456mbCoreUi::Strings::Strings () :
5557 settings_useNameWithSettings(QStringLiteral(" Ui.useNameWithSettings" )),
5658 settings_recentProjects(QStringLiteral(" Ui.recentProjects" )),
@@ -94,6 +96,8 @@ mbCoreUi::mbCoreUi(mbCore *core, QWidget *parent) :
9496 m_help = nullptr ;
9597
9698 m_menuRecent = new QMenu (this );
99+ m_actionFileRecentClear = new QAction (" Clear" , m_menuRecent);
100+ m_menuRecent->addAction (m_actionFileRecentClear);
97101 connect (m_menuRecent, &QMenu::triggered, this , &mbCoreUi::menuRecentTriggered);
98102}
99103
@@ -363,18 +367,27 @@ void mbCoreUi::menuSlotFileOpen()
363367{
364368 if (m_core->isRunning ())
365369 return ;
366- checkProjectModifiedAndSaveClose (QStringLiteral (" Open Project" ), QStringLiteral (" open another one" ));
370+ checkProjectModifiedAndSave (QStringLiteral (" Open Project" ), QStringLiteral (" open another one" ));
367371 QString file = m_dialogs->getOpenFileName (this ,
368372 QStringLiteral (" Open Project..." ),
369373 QString (),
370374 m_dialogs->getFilterString (mbCoreDialogs::Filter_ProjectAll));
371375 if (!file.isEmpty ())
376+ {
377+ closeProject ();
372378 openProject (file);
379+ }
373380}
374381
375382void mbCoreUi::menuSlotFileClose ()
376383{
377- checkProjectModifiedAndSaveClose (QStringLiteral (" Close Project" ), QStringLiteral (" close" ));
384+ if (m_core->isRunning ())
385+ return ;
386+ QMessageBox::StandardButton res = checkProjectModifiedAndSave (QStringLiteral (" Close Project" ),
387+ QStringLiteral (" close" ),
388+ QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
389+ if (res != QMessageBox::Cancel)
390+ closeProject ();
378391}
379392
380393void mbCoreUi::menuSlotFileSave ()
@@ -388,7 +401,8 @@ void mbCoreUi::menuSlotFileSave()
388401 }
389402 m_project->setWindowsData (m_windowManager->saveWindowsState ());
390403 m_project->resetVersion ();
391- m_core->builderCore ()->saveCore (m_project);
404+ if (m_core->builderCore ()->saveCore (m_project))
405+ addRecentFile (m_project->absoluteFilePath ());
392406 }
393407}
394408
@@ -967,6 +981,9 @@ void mbCoreUi::setProject(mbCoreProject *project)
967981 connect (m_project, &mbCoreProject::nameChanged, this , &mbCoreUi::setProjectName);
968982 setProjectName (m_project->name ());
969983 setWindowModified (m_project->isModified ());
984+ QString absPath = m_project->absoluteFilePath ();
985+ if (absPath.count ())
986+ addRecentFile (absPath);
970987 }
971988 else
972989 {
@@ -1054,7 +1071,7 @@ void mbCoreUi::statusChange(int status)
10541071 {
10551072 // QPalette palette = m_lbSystemStatus->palette();
10561073 QPalette palette = this ->palette ();
1057- palette.setColor (m_lbSystemStatus->backgroundRole (), Qt::gray );
1074+ palette.setColor (m_lbSystemStatus->backgroundRole (), Qt::lightGray );
10581075 palette.setColor (m_lbSystemStatus->foregroundRole (), Qt::black);
10591076 m_lbSystemStatus->setPalette (palette);
10601077 m_ui.actionRuntimeStartStop ->setText (" Start" );
@@ -1078,12 +1095,22 @@ void mbCoreUi::statusChange(int status)
10781095
10791096void mbCoreUi::menuRecentTriggered (QAction *a)
10801097{
1081- QString absPath = a->data ().toString ();
1082- checkProjectModifiedAndSaveClose (QStringLiteral (" Open Project" ), QStringLiteral (" open another one" ));
1083- openProject (absPath);
1098+ if (a == m_actionFileRecentClear)
1099+ {
1100+ recentClear ();
1101+ }
1102+ else
1103+ {
1104+ if (m_core->isRunning ())
1105+ return ;
1106+ QString absPath = a->data ().toString ();
1107+ checkProjectModifiedAndSave (QStringLiteral (" Open Project" ), QStringLiteral (" open another one" ));
1108+ closeProject ();
1109+ openProject (absPath);
1110+ }
10841111}
10851112
1086- QMessageBox::StandardButton mbCoreUi::checkProjectModifiedAndSaveClose (const QString &title, const QString &action, QMessageBox::StandardButtons buttons)
1113+ QMessageBox::StandardButton mbCoreUi::checkProjectModifiedAndSave (const QString &title, const QString &action, QMessageBox::StandardButtons buttons)
10871114{
10881115 if (m_project)
10891116 {
@@ -1097,7 +1124,6 @@ QMessageBox::StandardButton mbCoreUi::checkProjectModifiedAndSaveClose(const QSt
10971124 if (res == QMessageBox::Yes)
10981125 menuSlotFileSave ();
10991126 }
1100- closeProject ();
11011127 return res;
11021128 }
11031129 return QMessageBox::No;
@@ -1109,25 +1135,10 @@ void mbCoreUi::openProject(const QString &file)
11091135 if (p)
11101136 {
11111137 m_core->setProjectCore (p);
1112- QString absPath = p->absoluteFilePath ();
1113- QAction *a;
1114- auto it = m_recentProjectActions.find (absPath);
1115- if (it != m_recentProjectActions.end ())
1116- {
1117- a = it.value ();
1118- m_menuRecent->removeAction (a);
1119- }
1120- else
1121- {
1122- a = new QAction (absPath);
1123- a->setData (absPath);
1124- m_recentProjectActions.insert (absPath, a);
1125- }
1126- QList<QAction*> ls = m_menuRecent->actions ();
1127- if (ls.count ())
1128- m_menuRecent->insertAction (ls.first (), a);
1129- else
1130- m_menuRecent->addAction (a);
1138+ }
1139+ else
1140+ {
1141+ removeRecentFile (file);
11311142 }
11321143}
11331144
@@ -1136,11 +1147,59 @@ void mbCoreUi::closeProject()
11361147 m_core->setProjectCore (nullptr );
11371148}
11381149
1150+ void mbCoreUi::addRecentFile (const QString &absPath)
1151+ {
1152+ QAction *a;
1153+ auto it = m_recentProjectActions.find (absPath);
1154+ if (it != m_recentProjectActions.end ())
1155+ {
1156+ a = it.value ();
1157+ m_menuRecent->removeAction (a);
1158+ }
1159+ else
1160+ {
1161+ a = new QAction (absPath);
1162+ a->setData (absPath);
1163+ m_recentProjectActions.insert (absPath, a);
1164+ }
1165+ QList<QAction*> ls = m_menuRecent->actions ();
1166+ if (m_recentProjectActions.count () >= RECENT_PROJECTS_COUNT)
1167+ {
1168+ QAction *toRemove = ls.at (RECENT_PROJECTS_COUNT-1 );
1169+ removeRecentFile (toRemove->data ().toString ());
1170+ }
1171+ if (ls.count ())
1172+ m_menuRecent->insertAction (ls.first (), a);
1173+ else
1174+ m_menuRecent->addAction (a);
1175+ }
1176+
1177+ void mbCoreUi::removeRecentFile (const QString &absPath)
1178+ {
1179+ auto it = m_recentProjectActions.find (absPath);
1180+ if (it != m_recentProjectActions.end ())
1181+ {
1182+ QAction *a = it.value ();
1183+ m_menuRecent->removeAction (a);
1184+ delete a;
1185+ m_recentProjectActions.erase (it);
1186+ }
1187+ }
1188+
1189+ void mbCoreUi::recentClear ()
1190+ {
1191+ QList<QAction*> ls = m_menuRecent->actions ();
1192+ ls.removeAll (m_actionFileRecentClear);
1193+ Q_FOREACH (const QAction *a, ls)
1194+ removeRecentFile (a->data ().toString ());
1195+ }
1196+
11391197QVariantList mbCoreUi::cachedSettingsRecentProjects () const
11401198{
1141- QList<QAction*> acs = m_menuRecent->actions ();
1199+ QList<QAction*> ls = m_menuRecent->actions ();
1200+ ls.removeAll (m_actionFileRecentClear);
11421201 QVariantList vs;
1143- Q_FOREACH (const QAction* a, acs )
1202+ Q_FOREACH (const QAction* a, ls )
11441203 {
11451204 vs.append (a->data ());
11461205 }
@@ -1149,22 +1208,18 @@ QVariantList mbCoreUi::cachedSettingsRecentProjects() const
11491208
11501209void mbCoreUi::setCachedSettingsRecentProjects (const QVariantList &ls)
11511210{
1152- // TODO: ensure project files is unique in list
1153- Q_FOREACH (const QVariant& v, ls)
1211+ for (auto it = ls.rbegin (); it != ls.rend (); it++)
11541212 {
1155- QString absPath = v.toString ();
1156- QAction *a = new QAction (absPath);
1157- a->setData (absPath);
1158- m_recentProjectActions.insert (absPath, a);
1159- m_menuRecent->addAction (a);
1213+ QString absPath = (*it).toString ();
1214+ addRecentFile (absPath);
11601215 }
11611216}
11621217
11631218void mbCoreUi::closeEvent (QCloseEvent *e)
11641219{
1165- QMessageBox::StandardButton res = checkProjectModifiedAndSaveClose (QStringLiteral (" Quit" ),
1166- QStringLiteral (" exit" ),
1167- QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
1220+ QMessageBox::StandardButton res = checkProjectModifiedAndSave (QStringLiteral (" Quit" ),
1221+ QStringLiteral (" exit" ),
1222+ QMessageBox::Yes|QMessageBox::No|QMessageBox::Cancel);
11681223 switch (res)
11691224 {
11701225 case QMessageBox::Cancel:
0 commit comments