31 #include <QMessageBox>
46 using namespace Controller;
47 using namespace Data_Structure;
48 using namespace Parser;
53 : QObject(parent), favlistSize(5), packages(packages), executor(exec),
57 model->setSourceModel(treemodel.get());
60 QObject::connect(&openScenarioView, SIGNAL(openScenario(
int,
int, QModelIndex)),
63 QObject::connect(&openScenarioView, SIGNAL(showAddPackageDialog()),
78 if (openScenarioItem ==
nullptr)
85 for (
unsigned int i = 0; i < packages->size(); i++)
89 package = packages->at(i);
94 if (package.get() ==
nullptr)
101 if (pos != std::string::npos)
114 if (scenario.get() ==
nullptr)
117 std::string messageStr;
119 messageStr <<
"Either the File was not found or it has no valid scx formatting";
120 message.setText(QString::fromStdString(messageStr));
125 for (
const auto& it : *scenario->getApplications())
127 executor->loadAndSetCachedProperties(it, cacheDir,
false,
false);
130 scenario->reloadAppInstances();
132 package->addScenario(scenario);
135 QSettings settings(QString(file.c_str()), QSettings::NativeFormat);
137 QStringList settingsPackages = settings.value(
"scenarios").toStringList();
138 settingsPackages.removeDuplicates();
140 QString toAppend = QString::fromStdString(scenario->getName());
141 toAppend.append(
"::Package::");
142 toAppend.append(QString::fromStdString(scenario->getPackage()->getName()));
144 settingsPackages.append(toAppend);
145 settings.setValue(
"scenarios", settingsPackages);
148 QMap<QString, int> favouriteCount;
150 settings.beginGroup(
"favourites");
151 QStringList keys = settings.childKeys();
152 foreach (QString key, keys)
154 favouriteCount[key] = settings.value(key).toInt();
158 settings.beginGroup(
"favourites");
159 QMap<QString, int>::const_iterator i = favouriteCount.constBegin();
161 while (i != favouriteCount.constEnd())
163 if (i.key().compare(QString::fromStdString(scenario->getName() +
"::Package::" + scenario->getPackage()->getName())) == 0)
165 settings.setValue(i.key(), i.value() + 1);
170 settings.setValue(i.key(), i.value());
177 settings.setValue(QString::fromStdString(scenario->getName() +
"::Package::" + scenario->getPackage()->getName()), 1);
187 openScenarioView.exec();
199 QStringList paths = getAllClosedScenarios();
200 QStringList favourites = getFavouriteScenarios(paths);
207 for (
int i = 0; i < favourites.size(); i++)
209 QStringList
split = favourites.at(i).split(
"::Package::");
214 for (
int i = 0; i < paths.size(); i++)
216 QStringList
split = paths.at(i).split(
"::Package::");
225 model->setSourceModel(treemodel.get());
229 QStringList OpenScenarioController::getFavouriteScenarios(QStringList allClosedScenarios)
232 QSettings settings(QString(file.c_str()), QSettings::NativeFormat);
234 QMap<QString, int> favouriteCount;
236 settings.beginGroup(
"favourites");
237 QStringList keys = settings.childKeys();
238 foreach (QString key, keys)
240 favouriteCount[key] = settings.value(key).toInt();
244 if (favouriteCount.size() == 0)
246 return QStringList();
249 QList<QPair<QString, int>> favouriteSort;
251 for (
const auto& key : favouriteCount.toStdMap())
253 favouriteSort.push_back(QPair<QString, int>(key.first, key.second));
256 std::sort(favouriteSort.begin(), favouriteSort.end(), [](
const QPair<QString, int>& p1,
const QPair<QString, int>& p2)
258 return p1.second > p2.second;
263 for (
int i = 0; i < favouriteSort.count(); ++i)
266 if (allClosedScenarios.contains(favouriteSort[i].first))
268 result.push_back(favouriteSort[i].first);
269 if ((
size_t)result.size() == favlistSize)
279 QStringList OpenScenarioController::getAllClosedScenarios()
282 QSettings settings(QString(file.c_str()), QSettings::NativeFormat);
283 auto packages = settings.value(
"packages").toStringList().toSet();
284 auto openedScenarios = settings.value(
"scenarios").toStringList().toSet();
287 for (
const auto& package : packages)
292 XMLScenarioParser scenarioParser;
293 std::vector<std::string> packageScenarios = scenarioParser.getScenariosFromFolder(
parser.getScenariosDir());
295 for (
const auto&
str : packageScenarios)
298 for (
const auto& opened : openedScenarios)
300 if (opened.compare(
"") == 0)
304 QString name = opened.split(
"::Package::").at(0);
305 QString p = opened.split(
"::Package::").at(1);
306 if (name.compare(QString::fromStdString(
str)) == 0 && p.compare(QString::fromStdString(package.toStdString())) == 0)
314 QString resultStr(QString::fromStdString(
str));
315 resultStr.append(
"::Package::");
316 resultStr.append(package);