Wed, 19 Apr 2023 22:51:56 +0300
Made library_role_e an enum class
--- a/src/libraries.cpp Wed Apr 19 22:42:43 2023 +0300 +++ b/src/libraries.cpp Wed Apr 19 22:51:56 2023 +0300 @@ -118,7 +118,7 @@ * @param libraryIndex Index of the library * @param role New role */ -void LibrariesModel::setLibraryRole(index_t libraryIndex, const Library::Role role) +void LibrariesModel::setLibraryRole(index_t libraryIndex, const library_role_e role) { if (this->isValidIndex(libraryIndex)) { @@ -215,15 +215,15 @@ * @param role Role to get a string for * @returns string */ -QString Library::libraryRoleName(const Role role) +QString library_role_name(const library_role_e role) { switch (role) { - case Library::OfficialLibrary: + case library_role_e::official: return LibrariesModel::tr("Official library"); - case Library::UnofficialLibrary: + case library_role_e::unofficial: return LibrariesModel::tr("Unofficial library"); - case Library::WorkingLibrary: + case library_role_e::working: return LibrariesModel::tr("Working library"); } return "???"; @@ -260,7 +260,7 @@ switch (column) { case RoleColumn: - return Library::libraryRoleName(library.role); + return library_role_name(library.role); case PathColumn: return library.path.absolutePath(); } @@ -351,7 +351,7 @@ int role; QDataStream& result = stream >> path >> role; library.path.setPath(path); - library.role = static_cast<Library::Role>(role); + library.role = static_cast<library_role_e>(role); return result; }
--- a/src/libraries.h Wed Apr 19 22:42:43 2023 +0300 +++ b/src/libraries.h Wed Apr 19 22:51:56 2023 +0300 @@ -24,24 +24,31 @@ class QSettings; +enum class library_role_e +{ + official, + unofficial, + working, +}; + +constexpr static const library_role_e all_library_roles[] = +{ + library_role_e::official, + library_role_e::unofficial, + library_role_e::working +}; + struct Library { - enum Role - { - OfficialLibrary, - UnofficialLibrary, - WorkingLibrary, - } role; + library_role_e role; QDir path; - static QString libraryRoleName(const Role role); - static bool isValidRole(const Role role); - constexpr static const Role allRoles[] = {OfficialLibrary, UnofficialLibrary, WorkingLibrary}; }; +QString library_role_name(const library_role_e role); bool operator==(const Library& one, const Library& other); Q_DECLARE_METATYPE(Library) -Q_DECLARE_METATYPE(Library::Role) +Q_DECLARE_METATYPE(library_role_e) QDataStream &operator<<(QDataStream&, const Library&); QDataStream &operator>>(QDataStream&, Library&); using Libraries = QVector<Library>; @@ -58,7 +65,7 @@ void removeLibrary(const index_t libraryIndex); const Library& library(index_t libraryIndex) const; void setLibraryPath(index_t libraryIndex, const QDir& path); - void setLibraryRole(index_t libraryIndex, const Library::Role role); + void setLibraryRole(index_t libraryIndex, const library_role_e role); void restoreFromSettings(); void storeToSettings(); index_t count() const;
--- a/src/settingseditor/librarieseditor.cpp Wed Apr 19 22:42:43 2023 +0300 +++ b/src/settingseditor/librarieseditor.cpp Wed Apr 19 22:51:56 2023 +0300 @@ -63,7 +63,7 @@ tr("The directory %1 cannot be read.").arg(quoted(dir.path())) ); } - model->addLibrary({Library::OfficialLibrary, dir}); + model->addLibrary({library_role_e::official, dir}); this->ui.newLibraryPath->clear(); } } @@ -83,10 +83,10 @@ this, &LibrariesEditor::removeCurrentLibrary); QMenu* roleMenu = new QMenu{tr("Set role"), contextMenu}; - for (const Library::Role role : Library::allRoles) + for (const library_role_e role : all_library_roles) { - QAction* setRoleAction = new QAction{Library::libraryRoleName(role)}; - setRoleAction->setData(role); + QAction* setRoleAction = new QAction{library_role_name(role)}; + setRoleAction->setData(QVariant::fromValue(role)); roleMenu->addAction(setRoleAction); connect( setRoleAction, @@ -121,7 +121,7 @@ const index_t libraryIndex = currentLibraryIndex(); QObject* senderObject = sender(); QAction* senderAction = qobject_cast<QAction*>(senderObject); - const Library::Role role = senderAction->data().value<Library::Role>(); + const library_role_e role = senderAction->data().value<library_role_e>(); model->setLibraryRole(libraryIndex, role); } }