diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2022-04-20 19:42:13 +0200 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2022-04-22 07:58:01 +0200 |
commit | d2ed1f39030d93f01d71250cb8061448c82f0404 (patch) | |
tree | e41dd8f6869662a47716a28f83d3eefb98bd0523 | |
parent | 350f2f5d488af018c7c1c6d97fc8ba5b40125e8f (diff) |
Turn QPdfBookmarkModel::Role into an enum class
- link, search and bookmark models have similar role names now
- being an enum class allows the role name to be shorter
- the Role enum name is the same as the name in roleNames(), lowercased
Change-Id: I2e710d7acb8479995d82ef2d324807b6cd4e10e5
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
-rw-r--r-- | examples/pdfwidgets/pdfviewer/mainwindow.cpp | 4 | ||||
-rw-r--r-- | src/pdf/qpdfbookmarkmodel.cpp | 27 | ||||
-rw-r--r-- | src/pdf/qpdfbookmarkmodel.h | 10 | ||||
-rw-r--r-- | tests/auto/pdf/qpdfbookmarkmodel/tst_qpdfbookmarkmodel.cpp | 72 |
4 files changed, 57 insertions, 56 deletions
diff --git a/examples/pdfwidgets/pdfviewer/mainwindow.cpp b/examples/pdfwidgets/pdfviewer/mainwindow.cpp index 3fbb77532..25bdaf25e 100644 --- a/examples/pdfwidgets/pdfviewer/mainwindow.cpp +++ b/examples/pdfwidgets/pdfviewer/mainwindow.cpp @@ -127,8 +127,8 @@ void MainWindow::bookmarkSelected(const QModelIndex &index) if (!index.isValid()) return; - const int page = index.data(QPdfBookmarkModel::PageNumberRole).toInt(); - const qreal zoomLevel = index.data(QPdfBookmarkModel::LevelRole).toReal(); + const int page = index.data(int(QPdfBookmarkModel::Role::Page)).toInt(); + const qreal zoomLevel = index.data(int(QPdfBookmarkModel::Role::Level)).toReal(); ui->pdfView->pageNavigation()->jump(page, {}, zoomLevel); } diff --git a/src/pdf/qpdfbookmarkmodel.cpp b/src/pdf/qpdfbookmarkmodel.cpp index 31c98a90b..c40fc0572 100644 --- a/src/pdf/qpdfbookmarkmodel.cpp +++ b/src/pdf/qpdfbookmarkmodel.cpp @@ -45,6 +45,7 @@ #include "third_party/pdfium/public/fpdf_doc.h" #include "third_party/pdfium/public/fpdfview.h" +#include <QMetaEnum> #include <QPointer> #include <QScopedPointer> #include <private/qabstractitemmodel_p.h> @@ -222,6 +223,10 @@ QPdfBookmarkModel::QPdfBookmarkModel(QObject *parent) : QAbstractItemModel(parent), d(new QPdfBookmarkModelPrivate) { d->q = this; + m_roleNames = QAbstractItemModel::roleNames(); + QMetaEnum rolesMetaEnum = metaObject()->enumerator(metaObject()->indexOfEnumerator("Role")); + for (int r = Qt::UserRole; r < int(Role::_Count); ++r) + m_roleNames.insert(r, QByteArray(rolesMetaEnum.valueToKey(r)).toLower()); } QPdfBookmarkModel::~QPdfBookmarkModel() = default; @@ -272,13 +277,7 @@ int QPdfBookmarkModel::columnCount(const QModelIndex &parent) const QHash<int, QByteArray> QPdfBookmarkModel::roleNames() const { - QHash<int, QByteArray> names = QAbstractItemModel::roleNames(); - - names[TitleRole] = "title"; - names[LevelRole] = "level"; - names[PageNumberRole] = "pageNumber"; - - return names; + return m_roleNames; } QVariant QPdfBookmarkModel::data(const QModelIndex &index, int role) const @@ -287,17 +286,17 @@ QVariant QPdfBookmarkModel::data(const QModelIndex &index, int role) const return QVariant(); const BookmarkNode *node = static_cast<BookmarkNode*>(index.internalPointer()); - switch (role) { - case Qt::DisplayRole: - case TitleRole: + switch (Role(role)) { + case Role::Title: return node->title(); - case LevelRole: + case Role::Level: return node->level(); - case PageNumberRole: + case Role::Page: return node->pageNumber(); - default: - return QVariant(); + case Role::_Count: + break; } + return QVariant(); } QModelIndex QPdfBookmarkModel::index(int row, int column, const QModelIndex &parent) const diff --git a/src/pdf/qpdfbookmarkmodel.h b/src/pdf/qpdfbookmarkmodel.h index 7db2f03da..d6166508e 100644 --- a/src/pdf/qpdfbookmarkmodel.h +++ b/src/pdf/qpdfbookmarkmodel.h @@ -63,11 +63,12 @@ public: }; Q_ENUM(StructureMode) - enum Role + enum class Role : int { - TitleRole = Qt::UserRole, - LevelRole, - PageNumberRole + Title = Qt::DisplayRole, + Level = Qt::UserRole, + Page, + _Count }; Q_ENUM(Role) @@ -93,6 +94,7 @@ Q_SIGNALS: void structureModeChanged(QPdfBookmarkModel::StructureMode structureMode); private: + QHash<int, QByteArray> m_roleNames; std::unique_ptr<QPdfBookmarkModelPrivate> d; Q_PRIVATE_SLOT(d, void _q_documentStatusChanged()) diff --git a/tests/auto/pdf/qpdfbookmarkmodel/tst_qpdfbookmarkmodel.cpp b/tests/auto/pdf/qpdfbookmarkmodel/tst_qpdfbookmarkmodel.cpp index 72d2383f1..43af1be40 100644 --- a/tests/auto/pdf/qpdfbookmarkmodel/tst_qpdfbookmarkmodel.cpp +++ b/tests/auto/pdf/qpdfbookmarkmodel/tst_qpdfbookmarkmodel.cpp @@ -157,43 +157,43 @@ void tst_QPdfBookmarkModel::testTreeStructure() QCOMPARE(model.rowCount(), 3); const QModelIndex index1 = model.index(0, 0); - QCOMPARE(index1.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 1")); - QCOMPARE(index1.data(QPdfBookmarkModel::LevelRole).toInt(), 0); + QCOMPARE(index1.data(int(QPdfBookmarkModel::Role::Title)).toString(), QLatin1String("Section 1")); + QCOMPARE(index1.data(int(QPdfBookmarkModel::Role::Level)).toInt(), 0); QCOMPARE(model.rowCount(index1), 2); const QModelIndex index1_1 = model.index(0, 0, index1); - QCOMPARE(index1_1.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 1.1")); - QCOMPARE(index1_1.data(QPdfBookmarkModel::LevelRole).toInt(), 1); + QCOMPARE(index1_1.data(int(QPdfBookmarkModel::Role::Title)).toString(), QLatin1String("Section 1.1")); + QCOMPARE(index1_1.data(int(QPdfBookmarkModel::Role::Level)).toInt(), 1); QCOMPARE(model.rowCount(index1_1), 0); const QModelIndex index1_2 = model.index(1, 0, index1); - QCOMPARE(index1_2.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 1.2")); - QCOMPARE(index1_2.data(QPdfBookmarkModel::LevelRole).toInt(), 1); + QCOMPARE(index1_2.data(int(QPdfBookmarkModel::Role::Title)).toString(), QLatin1String("Section 1.2")); + QCOMPARE(index1_2.data(int(QPdfBookmarkModel::Role::Level)).toInt(), 1); QCOMPARE(model.rowCount(index1_2), 0); const QModelIndex index2 = model.index(1, 0); - QCOMPARE(index2.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 2")); - QCOMPARE(index2.data(QPdfBookmarkModel::LevelRole).toInt(), 0); + QCOMPARE(index2.data(int(QPdfBookmarkModel::Role::Title)).toString(), QLatin1String("Section 2")); + QCOMPARE(index2.data(int(QPdfBookmarkModel::Role::Level)).toInt(), 0); QCOMPARE(model.rowCount(index2), 2); const QModelIndex index2_1 = model.index(0, 0, index2); - QCOMPARE(index2_1.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 2.1")); - QCOMPARE(index2_1.data(QPdfBookmarkModel::LevelRole).toInt(), 1); + QCOMPARE(index2_1.data(int(QPdfBookmarkModel::Role::Title)).toString(), QLatin1String("Section 2.1")); + QCOMPARE(index2_1.data(int(QPdfBookmarkModel::Role::Level)).toInt(), 1); QCOMPARE(model.rowCount(index2_1), 1); const QModelIndex index2_1_1 = model.index(0, 0, index2_1); - QCOMPARE(index2_1_1.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 2.1.1")); - QCOMPARE(index2_1_1.data(QPdfBookmarkModel::LevelRole).toInt(), 2); + QCOMPARE(index2_1_1.data(int(QPdfBookmarkModel::Role::Title)).toString(), QLatin1String("Section 2.1.1")); + QCOMPARE(index2_1_1.data(int(QPdfBookmarkModel::Role::Level)).toInt(), 2); QCOMPARE(model.rowCount(index2_1_1), 0); const QModelIndex index2_2 = model.index(1, 0, index2); - QCOMPARE(index2_2.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 2.2")); - QCOMPARE(index2_2.data(QPdfBookmarkModel::LevelRole).toInt(), 1); + QCOMPARE(index2_2.data(int(QPdfBookmarkModel::Role::Title)).toString(), QLatin1String("Section 2.2")); + QCOMPARE(index2_2.data(int(QPdfBookmarkModel::Role::Level)).toInt(), 1); QCOMPARE(model.rowCount(index2_2), 0); const QModelIndex index3 = model.index(2, 0); - QCOMPARE(index3.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 3")); - QCOMPARE(index3.data(QPdfBookmarkModel::LevelRole).toInt(), 0); + QCOMPARE(index3.data(int(QPdfBookmarkModel::Role::Title)).toString(), QLatin1String("Section 3")); + QCOMPARE(index3.data(int(QPdfBookmarkModel::Role::Level)).toInt(), 0); QCOMPARE(model.rowCount(index3), 0); const QModelIndex index4 = model.index(3, 0); @@ -219,43 +219,43 @@ void tst_QPdfBookmarkModel::testListStructure() QCOMPARE(model.rowCount(), 8); const QModelIndex index1 = model.index(0, 0); - QCOMPARE(index1.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 1")); - QCOMPARE(index1.data(QPdfBookmarkModel::LevelRole).toInt(), 0); + QCOMPARE(index1.data(int(QPdfBookmarkModel::Role::Title)).toString(), QLatin1String("Section 1")); + QCOMPARE(index1.data(int(QPdfBookmarkModel::Role::Level)).toInt(), 0); QCOMPARE(model.rowCount(index1), 0); const QModelIndex index1_1 = model.index(1, 0); - QCOMPARE(index1_1.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 1.1")); - QCOMPARE(index1_1.data(QPdfBookmarkModel::LevelRole).toInt(), 1); + QCOMPARE(index1_1.data(int(QPdfBookmarkModel::Role::Title)).toString(), QLatin1String("Section 1.1")); + QCOMPARE(index1_1.data(int(QPdfBookmarkModel::Role::Level)).toInt(), 1); QCOMPARE(model.rowCount(index1_1), 0); const QModelIndex index1_2 = model.index(2, 0); - QCOMPARE(index1_2.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 1.2")); - QCOMPARE(index1_2.data(QPdfBookmarkModel::LevelRole).toInt(), 1); + QCOMPARE(index1_2.data(int(QPdfBookmarkModel::Role::Title)).toString(), QLatin1String("Section 1.2")); + QCOMPARE(index1_2.data(int(QPdfBookmarkModel::Role::Level)).toInt(), 1); QCOMPARE(model.rowCount(index1_2), 0); const QModelIndex index2 = model.index(3, 0); - QCOMPARE(index2.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 2")); - QCOMPARE(index2.data(QPdfBookmarkModel::LevelRole).toInt(), 0); + QCOMPARE(index2.data(int(QPdfBookmarkModel::Role::Title)).toString(), QLatin1String("Section 2")); + QCOMPARE(index2.data(int(QPdfBookmarkModel::Role::Level)).toInt(), 0); QCOMPARE(model.rowCount(index2), 0); const QModelIndex index2_1 = model.index(4, 0); - QCOMPARE(index2_1.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 2.1")); - QCOMPARE(index2_1.data(QPdfBookmarkModel::LevelRole).toInt(), 1); + QCOMPARE(index2_1.data(int(QPdfBookmarkModel::Role::Title)).toString(), QLatin1String("Section 2.1")); + QCOMPARE(index2_1.data(int(QPdfBookmarkModel::Role::Level)).toInt(), 1); QCOMPARE(model.rowCount(index2_1), 0); const QModelIndex index2_1_1 = model.index(5, 0); - QCOMPARE(index2_1_1.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 2.1.1")); - QCOMPARE(index2_1_1.data(QPdfBookmarkModel::LevelRole).toInt(), 2); + QCOMPARE(index2_1_1.data(int(QPdfBookmarkModel::Role::Title)).toString(), QLatin1String("Section 2.1.1")); + QCOMPARE(index2_1_1.data(int(QPdfBookmarkModel::Role::Level)).toInt(), 2); QCOMPARE(model.rowCount(index2_1_1), 0); const QModelIndex index2_2 = model.index(6, 0); - QCOMPARE(index2_2.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 2.2")); - QCOMPARE(index2_2.data(QPdfBookmarkModel::LevelRole).toInt(), 1); + QCOMPARE(index2_2.data(int(QPdfBookmarkModel::Role::Title)).toString(), QLatin1String("Section 2.2")); + QCOMPARE(index2_2.data(int(QPdfBookmarkModel::Role::Level)).toInt(), 1); QCOMPARE(model.rowCount(index2_2), 0); const QModelIndex index3 = model.index(7, 0); - QCOMPARE(index3.data(QPdfBookmarkModel::TitleRole).toString(), QLatin1String("Section 3")); - QCOMPARE(index3.data(QPdfBookmarkModel::LevelRole).toInt(), 0); + QCOMPARE(index3.data(int(QPdfBookmarkModel::Role::Title)).toString(), QLatin1String("Section 3")); + QCOMPARE(index3.data(int(QPdfBookmarkModel::Role::Level)).toInt(), 0); QCOMPARE(model.rowCount(index3), 0); const QModelIndex index4 = model.index(8, 0); @@ -273,16 +273,16 @@ void tst_QPdfBookmarkModel::testPageNumberRole() QCOMPARE(model.rowCount(), 3); const QModelIndex index1 = model.index(0, 0); - QCOMPARE(index1.data(QPdfBookmarkModel::PageNumberRole).toInt(), 0); + QCOMPARE(index1.data(int(QPdfBookmarkModel::Role::Page)).toInt(), 0); const QModelIndex index2 = model.index(1, 0); - QCOMPARE(index2.data(QPdfBookmarkModel::PageNumberRole).toInt(), 1); + QCOMPARE(index2.data(int(QPdfBookmarkModel::Role::Page)).toInt(), 1); const QModelIndex index2_1 = model.index(0, 0, index2); - QCOMPARE(index2_1.data(QPdfBookmarkModel::PageNumberRole).toInt(), 1); + QCOMPARE(index2_1.data(int(QPdfBookmarkModel::Role::Page)).toInt(), 1); const QModelIndex index3 = model.index(2, 0); - QCOMPARE(index3.data(QPdfBookmarkModel::PageNumberRole).toInt(), 2); + QCOMPARE(index3.data(int(QPdfBookmarkModel::Role::Page)).toInt(), 2); } QTEST_MAIN(tst_QPdfBookmarkModel) |