From 6b6bb3a15bf899d16716aa0f6d20f1122d67a0a5 2019-02-24 16:40:22 From: Alexis Jeandet Date: 2019-02-24 16:40:22 Subject: [PATCH] Several additions for Catalogue GUI Signed-off-by: Alexis Jeandet --- diff --git a/include/Catalogue/CatalogueController.h b/include/Catalogue/CatalogueController.h index b7d51fb..67b614f 100644 --- a/include/Catalogue/CatalogueController.h +++ b/include/Catalogue/CatalogueController.h @@ -60,6 +60,7 @@ public: std::vector events(); std::vector events(const QString& repository); + std::vector events(const Catalogue_ptr& catalogue); std::vector catalogues(); std::vector catalogues(const QString& repository); @@ -74,10 +75,15 @@ public: void save(const QString& repository); void add(const QString& repository); - void add(const QString& catalogue, const QString& repository); + Catalogue_ptr add(const QString& catalogue, const QString& repository); void add(Event_ptr event, Catalogue_ptr catalogue); void add(Event_ptr event, const QString& repository = default_repo); +signals: + void repositoryAdded(const QString& repository); + void catalogueAdded(const Catalogue_ptr& catalogue, const QString& repository); + void catalogueChanged(const Catalogue_ptr& catalogue); + // // Event // /// retrieveEvents with empty repository retrieve them from the default // repository std::list> retrieveEvents(const diff --git a/include/Common/StringUtils.h b/include/Common/StringUtils.h index 3dcdee2..856a138 100644 --- a/include/Common/StringUtils.h +++ b/include/Common/StringUtils.h @@ -3,33 +3,88 @@ #include "CoreGlobal.h" +#include +#include +#include +#include +#include #include -class QString; - /** * Utility class with methods for strings */ -struct SCIQLOP_CORE_EXPORT StringUtils { - /** - * Generates a unique name from a default name and a set of forbidden names. - * - * Generating the unique name is done by adding an index to the default name and stopping at the - * first index for which the generated name is not in the forbidden names. - * - * Examples (defaultName, forbiddenNames -> result): - * - "FGM", {"FGM"} -> "FGM1" - * - "FGM", {"ABC"} -> "FGM" - * - "FGM", {"FGM", "FGM1"} -> "FGM2" - * - "FGM", {"FGM", "FGM2"} -> "FGM1" - * - "", {"ABC"} -> "1" - * - * @param defaultName the default name - * @param forbiddenNames the set of forbidden names - * @return the unique name generated - */ - static QString uniqueName(const QString &defaultName, - const std::vector &forbiddenNames) noexcept; -}; +namespace StringUtils +{ + /** + * Generates a unique name from a default name and a set of forbidden names. + * + * Generating the unique name is done by adding an index to the default name + * and stopping at the first index for which the generated name is not in the + * forbidden names. + * + * Examples (defaultName, forbiddenNames -> result): + * - "FGM", {"FGM"} -> "FGM1" + * - "FGM", {"ABC"} -> "FGM" + * - "FGM", {"FGM", "FGM1"} -> "FGM2" + * - "FGM", {"FGM", "FGM2"} -> "FGM1" + * - "", {"ABC"} -> "1" + * + * @param defaultName the default name + * @param forbiddenNames the set of forbidden names + * @return the unique name generated + */ + static QString uniqueName(const QString& defaultName, + const std::vector& forbiddenNames) noexcept + { + // Gets the base of the unique name to generate, by removing trailing number + // (for example, base name of "FGM12" is "FGM") + auto baseName = defaultName; + baseName.remove(QRegExp{QStringLiteral("\\d*$")}); + + // Finds the unique name by adding an index to the base name and stops when + // the generated name isn't forbidden + QString newName{}; + auto forbidden = true; + for(auto i = 0; forbidden; ++i) + { + newName = (i == 0) ? baseName : baseName + QString::number(i); + forbidden = + newName.isEmpty() || + std::any_of(forbiddenNames.cbegin(), forbiddenNames.cend(), + [&newName](const auto& name) { + return name.compare(newName, Qt::CaseInsensitive) == 0; + }); + } + + return newName; + } + + template + QString join(const container& input, const char* sep) + { + QStringList list; + if constexpr(std::is_same_v) + { + std::transform( + std::cbegin(input), std::cend(input), std::back_inserter(list), + [](const auto& item) { return QString::fromStdString(item); }); + } + else if constexpr(std::is_same_v) + { + std::copy(std::cbegin(input), std::cend(input), std::back_inserter(list)); + } + return list.join(sep); + } + + template + QString join(const container& input, const char* sep, + std::function op) + { + QStringList list; + std::transform(std::cbegin(input), std::cend(input), + std::back_inserter(list), op); + return list.join(sep); + } +} // namespace StringUtils #endif // SCIQLOP_STRINGUTILS_H diff --git a/src/Catalogue/CatalogueController.cpp b/src/Catalogue/CatalogueController.cpp index e2070d9..74f6789 100644 --- a/src/Catalogue/CatalogueController.cpp +++ b/src/Catalogue/CatalogueController.cpp @@ -88,6 +88,15 @@ std::vector CatalogueController::events() return e_list; } +std::vector +CatalogueController::events(const CatalogueController::Catalogue_ptr& catalogue) +{ + std::vector e_list; + for(auto& [_, event] : catalogue->events()) + e_list.push_back(event); + return e_list; +} + std::vector> CatalogueController::events(const QString& repository) { @@ -212,17 +221,28 @@ void CatalogueController::save(const QString& repository) void CatalogueController::add(const QString& repository) { if(!contains(_currentRepos, repository)) - { _currentRepos[repository] = Repository_t{}; } + { + _currentRepos[repository] = Repository_t{}; + emit repositoryAdded(repository); + } } -void CatalogueController::add(const QString& catalogue, - const QString& repository) +CatalogueController::Catalogue_ptr +CatalogueController::add(const QString& catalogue, const QString& repository) { - if(!contains(_currentRepos, repository)) - { _currentRepos[repository] = Repository_t{}; } + if(!contains(_currentRepos, repository)) { add(repository); } auto new_catalogue = make_catalogue_ptr(); new_catalogue->name = catalogue.toStdString(); - _currentRepos[repository].add(std::move(new_catalogue)); + _currentRepos[repository].add(new_catalogue); + emit catalogueAdded(new_catalogue, repository); + return new_catalogue; +} + +void CatalogueController::add(CatalogueController::Event_ptr event, + CatalogueController::Catalogue_ptr catalogue) +{ + catalogue->add(event); + emit this->catalogueChanged(catalogue); } void CatalogueController::add(CatalogueController::Event_ptr event, diff --git a/src/Common/StringUtils.cpp b/src/Common/StringUtils.cpp index 784e313..c0e736e 100644 --- a/src/Common/StringUtils.cpp +++ b/src/Common/StringUtils.cpp @@ -1,30 +1,5 @@ #include "Common/StringUtils.h" -#include -#include -#include -QString StringUtils::uniqueName(const QString &defaultName, - const std::vector &forbiddenNames) noexcept -{ - // Gets the base of the unique name to generate, by removing trailing number (for example, base - // name of "FGM12" is "FGM") - auto baseName = defaultName; - baseName.remove(QRegExp{QStringLiteral("\\d*$")}); - // Finds the unique name by adding an index to the base name and stops when the generated name - // isn't forbidden - QString newName{}; - auto forbidden = true; - for (auto i = 0; forbidden; ++i) { - newName = (i == 0) ? baseName : baseName + QString::number(i); - forbidden = newName.isEmpty() - || std::any_of(forbiddenNames.cbegin(), forbiddenNames.cend(), - [&newName](const auto &name) { - return name.compare(newName, Qt::CaseInsensitive) == 0; - }); - } - - return newName; -}