-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathsessionlist.cpp
67 lines (61 loc) · 2.16 KB
/
sessionlist.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "sessionlist.h"
#include "configmanager.h"
#include "smallUsefulFunctions.h"
SessionList::SessionList(QMenu *menu, QObject *parent) : QObject(parent), m_menu(menu)
{
m_config = dynamic_cast<ConfigManager *>(ConfigManagerInterface::getInstance());
}
void SessionList::addFilenameToList(const QString &file)
{
if (file.endsWith("lastSession.txss")) return;
if (file.endsWith("lastSession.txss2")) return;
REQUIRE(m_config);
QStringList files = m_config->getOption("Files/Recent Session Files").toStringList();
int maxFiles = m_config->getOption("Files/Max Recent Sessions").toInt();
bool changed = addMostRecent(file, files, maxFiles);
if (changed) {
m_config->setOption("Files/Recent Session Files", files);
updateMostRecentMenu();
}
}
/*
* updates the most recent session entries in the menu
*/
void SessionList::updateMostRecentMenu()
{
REQUIRE(m_config);
QStringList files = m_config->getOption("Files/Recent Session Files").toStringList();
REQUIRE(m_menu);
QList<QAction *> actions = m_menu->actions();
int i = 0;
/* convention: session entries are actions with a data tag "session:<filename>" */
foreach (const QString &file, files) {
QAction *sessionAct;
while (i < actions.count() && !actions[i]->data().toString().startsWith("session")) i++;
if (i >= actions.count()) {
sessionAct = m_menu->addAction("");
} else {
sessionAct = actions[i];
}
QString temp=QDir::toNativeSeparators(file);
temp.replace("&","&&");
sessionAct->setText(temp);
sessionAct->setData("session:" + file);
sessionAct->setVisible(true);
connectUnique(sessionAct, SIGNAL(triggered()), this, SLOT(menuActionTriggered()));
i++;
}
// disable remaining unused actions
for (int ii=i+1; ii<actions.count(); ii++) {
if (actions[ii]->data().toString().startsWith("session:"))
actions[ii]->setVisible(false);
}
}
void SessionList::menuActionTriggered()
{
QAction *act = qobject_cast<QAction *>(sender());
REQUIRE(act);
QString filename = act->data().toString().remove(QRegularExpression("^session:"));
emit loadSessionRequest(filename);
addFilenameToList(filename);
}