From 6fe978f0311098224023cc3031285869d8e1ea53 Mon Sep 17 00:00:00 2001 From: Jakub Melka Date: Wed, 8 Nov 2023 19:24:04 +0100 Subject: [PATCH] Issue #103: Bookmark sidebar greyed out if a bookmark is not already present --- Pdf4QtLib/sources/pdfitemmodels.cpp | 17 +++++++++++++++-- Pdf4QtLib/sources/pdfitemmodels.h | 2 ++ Pdf4QtLib/sources/pdfoutline.cpp | 10 ++++++++++ Pdf4QtLib/sources/pdfoutline.h | 3 +++ Pdf4QtViewer/pdfsidebarwidget.cpp | 2 +- 5 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Pdf4QtLib/sources/pdfitemmodels.cpp b/Pdf4QtLib/sources/pdfitemmodels.cpp index 1a8ffaa..ffe408b 100644 --- a/Pdf4QtLib/sources/pdfitemmodels.cpp +++ b/Pdf4QtLib/sources/pdfitemmodels.cpp @@ -415,6 +415,7 @@ void PDFOutlineTreeItemModel::update() { outlineRoot = m_document->getCatalog()->getOutlineRootPtr(); } + if (outlineRoot) { if (m_editable) @@ -426,7 +427,15 @@ void PDFOutlineTreeItemModel::update() } else { - m_rootItem.reset(); + if (m_editable && m_document) + { + outlineRoot.reset(new pdf::PDFOutlineItem()); + m_rootItem.reset(new PDFOutlineTreeItem(nullptr, qMove(outlineRoot))); + } + else + { + m_rootItem.reset(); + } } endResetModel(); @@ -653,9 +662,12 @@ bool PDFOutlineTreeItemModel::insertRows(int row, int count, const QModelIndex& { QSharedPointer outlineItem(new PDFOutlineItem()); outlineItem->setTitle(tr("Item %1").arg(row + 1)); - PDFOutlineTreeItem* newTreeItem = new PDFOutlineTreeItem(item, qMove(outlineItem)); + PDFOutlineTreeItem* newTreeItem = new PDFOutlineTreeItem(item, outlineItem); item->insertCreatedChild(row, newTreeItem); + PDFOutlineItem* parentItem = item->getOutlineItem(); + parentItem->insertChild(row, outlineItem); + ++row; --count; } @@ -677,6 +689,7 @@ bool PDFOutlineTreeItemModel::removeRows(int row, int count, const QModelIndex& PDFOutlineTreeItem* item = parent.isValid() ? static_cast(parent.internalPointer()) : static_cast(m_rootItem.get()); while (count > 0) { + item->getOutlineItem()->removeChild(row); delete item->takeChild(row); --count; } diff --git a/Pdf4QtLib/sources/pdfitemmodels.h b/Pdf4QtLib/sources/pdfitemmodels.h index f26fa25..033a2b9 100644 --- a/Pdf4QtLib/sources/pdfitemmodels.h +++ b/Pdf4QtLib/sources/pdfitemmodels.h @@ -213,6 +213,8 @@ public: const PDFOutlineItem* getRootOutlineItem() const; + bool isEditable() const { return m_editable; } + private: QIcon m_icon; bool m_editable; diff --git a/Pdf4QtLib/sources/pdfoutline.cpp b/Pdf4QtLib/sources/pdfoutline.cpp index 1496ca6..06caba9 100644 --- a/Pdf4QtLib/sources/pdfoutline.cpp +++ b/Pdf4QtLib/sources/pdfoutline.cpp @@ -179,6 +179,16 @@ QSharedPointer PDFOutlineItem::clone() const return result; } +void PDFOutlineItem::insertChild(size_t index, QSharedPointer item) +{ + m_children.insert(std::next(m_children.begin(), index), item); +} + +void PDFOutlineItem::removeChild(size_t index) +{ + m_children.erase(std::next(m_children.begin(), index)); +} + bool PDFOutlineItem::isFontBold() const { return m_fontBold; diff --git a/Pdf4QtLib/sources/pdfoutline.h b/Pdf4QtLib/sources/pdfoutline.h index 37479f3..783ebe5 100644 --- a/Pdf4QtLib/sources/pdfoutline.h +++ b/Pdf4QtLib/sources/pdfoutline.h @@ -67,6 +67,9 @@ public: QSharedPointer clone() const; + void insertChild(size_t index, QSharedPointer item); + void removeChild(size_t index); + private: static void parseImpl(const PDFObjectStorage* storage, PDFOutlineItem* parent, diff --git a/Pdf4QtViewer/pdfsidebarwidget.cpp b/Pdf4QtViewer/pdfsidebarwidget.cpp index 2cc8063..a5e2d87 100644 --- a/Pdf4QtViewer/pdfsidebarwidget.cpp +++ b/Pdf4QtViewer/pdfsidebarwidget.cpp @@ -260,7 +260,7 @@ bool PDFSidebarWidget::isEmpty(Page page) const return true; case Bookmarks: - return m_outlineTreeModel->isEmpty(); + return m_outlineTreeModel->isEmpty() && (!m_document || !m_outlineTreeModel->isEditable()); case Thumbnails: return m_thumbnailsModel->isEmpty();