@@ -60,6 +60,7 public: | |||||
60 |
|
60 | |||
61 | std::vector<Event_ptr> events(); |
|
61 | std::vector<Event_ptr> events(); | |
62 | std::vector<Event_ptr> events(const QString& repository); |
|
62 | std::vector<Event_ptr> events(const QString& repository); | |
|
63 | std::vector<Event_ptr> events(const Catalogue_ptr& catalogue); | |||
63 |
|
64 | |||
64 | std::vector<Catalogue_ptr> catalogues(); |
|
65 | std::vector<Catalogue_ptr> catalogues(); | |
65 | std::vector<Catalogue_ptr> catalogues(const QString& repository); |
|
66 | std::vector<Catalogue_ptr> catalogues(const QString& repository); | |
@@ -74,10 +75,15 public: | |||||
74 | void save(const QString& repository); |
|
75 | void save(const QString& repository); | |
75 |
|
76 | |||
76 | void add(const QString& repository); |
|
77 | void add(const QString& repository); | |
77 |
|
|
78 | Catalogue_ptr add(const QString& catalogue, const QString& repository); | |
78 | void add(Event_ptr event, Catalogue_ptr catalogue); |
|
79 | void add(Event_ptr event, Catalogue_ptr catalogue); | |
79 | void add(Event_ptr event, const QString& repository = default_repo); |
|
80 | void add(Event_ptr event, const QString& repository = default_repo); | |
80 |
|
81 | |||
|
82 | signals: | |||
|
83 | void repositoryAdded(const QString& repository); | |||
|
84 | void catalogueAdded(const Catalogue_ptr& catalogue, const QString& repository); | |||
|
85 | void catalogueChanged(const Catalogue_ptr& catalogue); | |||
|
86 | ||||
81 | // // Event |
|
87 | // // Event | |
82 | // /// retrieveEvents with empty repository retrieve them from the default |
|
88 | // /// retrieveEvents with empty repository retrieve them from the default | |
83 | // repository std::list<std::shared_ptr<DBEvent>> retrieveEvents(const |
|
89 | // repository std::list<std::shared_ptr<DBEvent>> retrieveEvents(const |
@@ -3,33 +3,88 | |||||
3 |
|
3 | |||
4 | #include "CoreGlobal.h" |
|
4 | #include "CoreGlobal.h" | |
5 |
|
5 | |||
|
6 | #include <QRegExp> | |||
|
7 | #include <QString> | |||
|
8 | #include <QStringList> | |||
|
9 | #include <functional> | |||
|
10 | #include <set> | |||
6 | #include <vector> |
|
11 | #include <vector> | |
7 |
|
12 | |||
8 | class QString; |
|
|||
9 |
|
||||
10 | /** |
|
13 | /** | |
11 | * Utility class with methods for strings |
|
14 | * Utility class with methods for strings | |
12 | */ |
|
15 | */ | |
13 | struct SCIQLOP_CORE_EXPORT StringUtils { |
|
16 | namespace StringUtils | |
14 | /** |
|
17 | { | |
15 | * Generates a unique name from a default name and a set of forbidden names. |
|
18 | /** | |
16 | * |
|
19 | * Generates a unique name from a default name and a set of forbidden names. | |
17 | * Generating the unique name is done by adding an index to the default name and stopping at the |
|
20 | * | |
18 | * first index for which the generated name is not in the forbidden names. |
|
21 | * Generating the unique name is done by adding an index to the default name | |
19 | * |
|
22 | * and stopping at the first index for which the generated name is not in the | |
20 | * Examples (defaultName, forbiddenNames -> result): |
|
23 | * forbidden names. | |
21 | * - "FGM", {"FGM"} -> "FGM1" |
|
24 | * | |
22 | * - "FGM", {"ABC"} -> "FGM" |
|
25 | * Examples (defaultName, forbiddenNames -> result): | |
23 |
|
|
26 | * - "FGM", {"FGM"} -> "FGM1" | |
24 |
|
|
27 | * - "FGM", {"ABC"} -> "FGM" | |
25 |
|
|
28 | * - "FGM", {"FGM", "FGM1"} -> "FGM2" | |
26 | * |
|
29 | * - "FGM", {"FGM", "FGM2"} -> "FGM1" | |
27 | * @param defaultName the default name |
|
30 | * - "", {"ABC"} -> "1" | |
28 | * @param forbiddenNames the set of forbidden names |
|
31 | * | |
29 | * @return the unique name generated |
|
32 | * @param defaultName the default name | |
30 | */ |
|
33 | * @param forbiddenNames the set of forbidden names | |
31 | static QString uniqueName(const QString &defaultName, |
|
34 | * @return the unique name generated | |
32 | const std::vector<QString> &forbiddenNames) noexcept; |
|
35 | */ | |
33 | }; |
|
36 | static QString uniqueName(const QString& defaultName, | |
|
37 | const std::vector<QString>& forbiddenNames) noexcept | |||
|
38 | { | |||
|
39 | // Gets the base of the unique name to generate, by removing trailing number | |||
|
40 | // (for example, base name of "FGM12" is "FGM") | |||
|
41 | auto baseName = defaultName; | |||
|
42 | baseName.remove(QRegExp{QStringLiteral("\\d*$")}); | |||
|
43 | ||||
|
44 | // Finds the unique name by adding an index to the base name and stops when | |||
|
45 | // the generated name isn't forbidden | |||
|
46 | QString newName{}; | |||
|
47 | auto forbidden = true; | |||
|
48 | for(auto i = 0; forbidden; ++i) | |||
|
49 | { | |||
|
50 | newName = (i == 0) ? baseName : baseName + QString::number(i); | |||
|
51 | forbidden = | |||
|
52 | newName.isEmpty() || | |||
|
53 | std::any_of(forbiddenNames.cbegin(), forbiddenNames.cend(), | |||
|
54 | [&newName](const auto& name) { | |||
|
55 | return name.compare(newName, Qt::CaseInsensitive) == 0; | |||
|
56 | }); | |||
|
57 | } | |||
|
58 | ||||
|
59 | return newName; | |||
|
60 | } | |||
|
61 | ||||
|
62 | template<typename container> | |||
|
63 | QString join(const container& input, const char* sep) | |||
|
64 | { | |||
|
65 | QStringList list; | |||
|
66 | if constexpr(std::is_same_v<typename container::value_type, std::string>) | |||
|
67 | { | |||
|
68 | std::transform( | |||
|
69 | std::cbegin(input), std::cend(input), std::back_inserter(list), | |||
|
70 | [](const auto& item) { return QString::fromStdString(item); }); | |||
|
71 | } | |||
|
72 | else if constexpr(std::is_same_v<typename container::value_type, QString>) | |||
|
73 | { | |||
|
74 | std::copy(std::cbegin(input), std::cend(input), std::back_inserter(list)); | |||
|
75 | } | |||
|
76 | return list.join(sep); | |||
|
77 | } | |||
|
78 | ||||
|
79 | template<typename container> | |||
|
80 | QString join(const container& input, const char* sep, | |||
|
81 | std::function<QString(typename container::value_type)> op) | |||
|
82 | { | |||
|
83 | QStringList list; | |||
|
84 | std::transform(std::cbegin(input), std::cend(input), | |||
|
85 | std::back_inserter(list), op); | |||
|
86 | return list.join(sep); | |||
|
87 | } | |||
|
88 | } // namespace StringUtils | |||
34 |
|
89 | |||
35 | #endif // SCIQLOP_STRINGUTILS_H |
|
90 | #endif // SCIQLOP_STRINGUTILS_H |
@@ -88,6 +88,15 std::vector<CatalogueController::Event_ptr> CatalogueController::events() | |||||
88 | return e_list; |
|
88 | return e_list; | |
89 | } |
|
89 | } | |
90 |
|
90 | |||
|
91 | std::vector<CatalogueController::Event_ptr> | |||
|
92 | CatalogueController::events(const CatalogueController::Catalogue_ptr& catalogue) | |||
|
93 | { | |||
|
94 | std::vector<CatalogueController::Event_ptr> e_list; | |||
|
95 | for(auto& [_, event] : catalogue->events()) | |||
|
96 | e_list.push_back(event); | |||
|
97 | return e_list; | |||
|
98 | } | |||
|
99 | ||||
91 | std::vector<std::shared_ptr<CatalogueController::Event_t>> |
|
100 | std::vector<std::shared_ptr<CatalogueController::Event_t>> | |
92 | CatalogueController::events(const QString& repository) |
|
101 | CatalogueController::events(const QString& repository) | |
93 | { |
|
102 | { | |
@@ -212,17 +221,28 void CatalogueController::save(const QString& repository) | |||||
212 | void CatalogueController::add(const QString& repository) |
|
221 | void CatalogueController::add(const QString& repository) | |
213 | { |
|
222 | { | |
214 | if(!contains(_currentRepos, repository)) |
|
223 | if(!contains(_currentRepos, repository)) | |
215 | { _currentRepos[repository] = Repository_t{}; } |
|
224 | { | |
|
225 | _currentRepos[repository] = Repository_t{}; | |||
|
226 | emit repositoryAdded(repository); | |||
|
227 | } | |||
216 | } |
|
228 | } | |
217 |
|
229 | |||
218 |
|
|
230 | CatalogueController::Catalogue_ptr | |
219 | const QString& repository) |
|
231 | CatalogueController::add(const QString& catalogue, const QString& repository) | |
220 | { |
|
232 | { | |
221 | if(!contains(_currentRepos, repository)) |
|
233 | if(!contains(_currentRepos, repository)) { add(repository); } | |
222 | { _currentRepos[repository] = Repository_t{}; } |
|
|||
223 | auto new_catalogue = make_catalogue_ptr(); |
|
234 | auto new_catalogue = make_catalogue_ptr(); | |
224 | new_catalogue->name = catalogue.toStdString(); |
|
235 | new_catalogue->name = catalogue.toStdString(); | |
225 |
_currentRepos[repository].add( |
|
236 | _currentRepos[repository].add(new_catalogue); | |
|
237 | emit catalogueAdded(new_catalogue, repository); | |||
|
238 | return new_catalogue; | |||
|
239 | } | |||
|
240 | ||||
|
241 | void CatalogueController::add(CatalogueController::Event_ptr event, | |||
|
242 | CatalogueController::Catalogue_ptr catalogue) | |||
|
243 | { | |||
|
244 | catalogue->add(event); | |||
|
245 | emit this->catalogueChanged(catalogue); | |||
226 | } |
|
246 | } | |
227 |
|
247 | |||
228 | void CatalogueController::add(CatalogueController::Event_ptr event, |
|
248 | void CatalogueController::add(CatalogueController::Event_ptr event, |
@@ -1,30 +1,5 | |||||
1 | #include "Common/StringUtils.h" |
|
1 | #include "Common/StringUtils.h" | |
2 |
|
2 | |||
3 | #include <QRegExp> |
|
|||
4 | #include <QString> |
|
|||
5 |
|
3 | |||
6 | #include <set> |
|
|||
7 |
|
4 | |||
8 | QString StringUtils::uniqueName(const QString &defaultName, |
|
|||
9 | const std::vector<QString> &forbiddenNames) noexcept |
|
|||
10 | { |
|
|||
11 | // Gets the base of the unique name to generate, by removing trailing number (for example, base |
|
|||
12 | // name of "FGM12" is "FGM") |
|
|||
13 | auto baseName = defaultName; |
|
|||
14 | baseName.remove(QRegExp{QStringLiteral("\\d*$")}); |
|
|||
15 |
|
5 | |||
16 | // Finds the unique name by adding an index to the base name and stops when the generated name |
|
|||
17 | // isn't forbidden |
|
|||
18 | QString newName{}; |
|
|||
19 | auto forbidden = true; |
|
|||
20 | for (auto i = 0; forbidden; ++i) { |
|
|||
21 | newName = (i == 0) ? baseName : baseName + QString::number(i); |
|
|||
22 | forbidden = newName.isEmpty() |
|
|||
23 | || std::any_of(forbiddenNames.cbegin(), forbiddenNames.cend(), |
|
|||
24 | [&newName](const auto &name) { |
|
|||
25 | return name.compare(newName, Qt::CaseInsensitive) == 0; |
|
|||
26 | }); |
|
|||
27 | } |
|
|||
28 |
|
||||
29 | return newName; |
|
|||
30 | } |
|
General Comments 0
You need to be logged in to leave comments.
Login now