Skip to content

Commit 8e3368b

Browse files
committed
Improve 'Recent Project' functionality
1 parent 7ace32b commit 8e3368b

File tree

14 files changed

+133
-63
lines changed

14 files changed

+133
-63
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required(VERSION 3.5) # 2.2 - case insensitive syntax
22

3-
project(ModbusTools VERSION 0.3.6 LANGUAGES CXX)
3+
project(ModbusTools VERSION 0.3.7 LANGUAGES CXX)
44

55
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")
66
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,4 @@
9191

9292
* Added menu `File->Recent` to quick open previous projects
9393
* Added menu `File->Close` to close current project
94+
* Fixed Client Scanner for Modbus TCP scanning

src/client/gui/scanner/client_scanner.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,22 @@ uint8_t mbClientScanner::getSettingUnitEnd(const Modbus::Settings &s, bool *ok)
3232
MBCLIENTSCANNER_GET_SETTING_MACRO(uint8_t, unitEnd, v = static_cast<uint8_t>(var.toUInt(&okInner)))
3333
}
3434

35-
QVariantList mbClientScanner::getSettingBaudRate(const Modbus::Settings &s, bool *ok)
35+
QVariantList mbClientScanner::getSettingBaudRate(const Modbus::Settings &s, bool */*ok*/)
3636
{
3737
return s.value(Modbus::Strings::instance().baudRate).toList();
3838
}
3939

40-
QVariantList mbClientScanner::getSettingDataBits(const Modbus::Settings &s, bool *ok)
40+
QVariantList mbClientScanner::getSettingDataBits(const Modbus::Settings &s, bool */*ok*/)
4141
{
4242
return s.value(Modbus::Strings::instance().dataBits).toList();
4343
}
4444

45-
QVariantList mbClientScanner::getSettingParity(const Modbus::Settings &s, bool *ok)
45+
QVariantList mbClientScanner::getSettingParity(const Modbus::Settings &s, bool */*ok*/)
4646
{
4747
return s.value(Modbus::Strings::instance().parity).toList();
4848
}
4949

50-
QVariantList mbClientScanner::getSettingStopBits(const Modbus::Settings &s, bool *ok)
50+
QVariantList mbClientScanner::getSettingStopBits(const Modbus::Settings &s, bool */*ok*/)
5151
{
5252
return s.value(Modbus::Strings::instance().stopBits).toList();
5353
}

src/client/gui/scanner/client_scannerthread.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void mbClientScannerThread::run()
108108

109109
quint32 deviceCount = 0;
110110
quint32 deviceFound = 0;
111-
for (int c = 0; m_ctrlRun && (c < m_combinationCount); c++)
111+
for (uint c = 0; m_ctrlRun && (c < m_combinationCount); c++)
112112
{
113113
// Get comibation number for each setting
114114
for (quint16 si = 0; si < m_names.count(); si++)

src/client/gui/sendmessage/client_sendmessageui.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ void mbClientSendMessageUi::setProject(mbCoreProject *p)
233233
{
234234
if (m_project)
235235
{
236-
project->disconnect(this);
236+
m_project->disconnect(this);
237237
ui->cmbDevice->clear();
238238
}
239239
m_project = project;

src/client/win_resource.rc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
IDI_ICON1 ICON DISCARDABLE "gui\\icons\\client.ico"
44

55
VS_VERSION_INFO VERSIONINFO
6-
FILEVERSION 0,3,6,0
7-
PRODUCTVERSION 0,3,6,0
6+
FILEVERSION 0,3,7,0
7+
PRODUCTVERSION 0,3,7,0
88
FILEFLAGSMASK 0x3fL
99
#ifdef _DEBUG
1010
FILEFLAGS VS_FF_DEBUG
@@ -21,11 +21,11 @@ VS_VERSION_INFO VERSIONINFO
2121
BEGIN
2222
VALUE "CompanyName", "\0"
2323
VALUE "FileDescription", "\0"
24-
VALUE "FileVersion", "0.3.6.0\0"
24+
VALUE "FileVersion", "0.3.7.0\0"
2525
VALUE "LegalCopyright", "\0"
2626
VALUE "OriginalFilename", "client.exe\0"
2727
VALUE "ProductName", "client\0"
28-
VALUE "ProductVersion", "0.3.6.0\0"
28+
VALUE "ProductVersion", "0.3.7.0\0"
2929
END
3030
END
3131
BLOCK "VarFileInfo"

src/core/core/core.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@ void mbCore::setProjectCore(mbCoreProject *project)
146146
if (m_project != project)
147147
{
148148
mbCoreProject *old = m_project;
149+
if (old)
150+
{
151+
QString absPath = old->absoluteFilePath();
152+
if (absPath.count())
153+
m_settings.lastProject = absPath;
154+
}
149155
m_project = project;
150156
Q_EMIT projectChanged(project);
151157
if (project)

src/core/gui/core_ui.cpp

Lines changed: 95 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
#include "core_windowmanager.h"
5252
#include "core_logview.h"
5353

54+
#define RECENT_PROJECTS_COUNT 20
55+
5456
mbCoreUi::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

375382
void 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

380393
void 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

10791096
void 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+
11391197
QVariantList 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

11501209
void 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

11631218
void 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:

src/core/gui/core_ui.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,15 @@ protected Q_SLOTS:
208208
void menuRecentTriggered(QAction *a);
209209

210210
protected:
211-
QMessageBox::StandardButton checkProjectModifiedAndSaveClose(const QString &title, const QString &action, QMessageBox::StandardButtons buttons = QMessageBox::StandardButtons(QMessageBox::Yes | QMessageBox::No));
211+
QMessageBox::StandardButton checkProjectModifiedAndSave(const QString &title,
212+
const QString &action,
213+
QMessageBox::StandardButtons buttons = QMessageBox::StandardButtons(QMessageBox::Yes | QMessageBox::No));
212214
void openProject(const QString &file);
213215
void closeProject();
216+
void addRecentFile(const QString &absPath);
217+
void removeRecentFile(const QString &absPath);
218+
void recentClear();
219+
214220
QVariantList cachedSettingsRecentProjects() const;
215221
void setCachedSettingsRecentProjects(const QVariantList &ls);
216222

@@ -303,6 +309,8 @@ protected Q_SLOTS:
303309
} m_ui;
304310

305311
QMenu *m_menuRecent;
312+
QAction *m_actionFileRecentClear;
313+
306314
QLabel *m_lbSystemName;
307315
QLabel *m_lbSystemStatus;
308316
QLabel *m_lbPortName;

src/core/sdk/mbcore_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
/*
1515
Patch part of mbtools version
1616
*/
17-
#define MBTOOLS_VERSION_PATCH 6
17+
#define MBTOOLS_VERSION_PATCH 7
1818

1919
#endif // MBCORE_CONFIG_H

0 commit comments

Comments
 (0)