From 2f4a36f0ff8285e80de2c4b4670b522261963190 Mon Sep 17 00:00:00 2001 From: ShaopengLin Date: Tue, 4 Jun 2024 23:10:18 -0400 Subject: [PATCH 1/5] Refactored ContentManager click signal handler Handler is now a slot function instead of a lambda. --- src/contentmanagerview.cpp | 25 ++++++++++++++----------- src/contentmanagerview.h | 1 + 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/contentmanagerview.cpp b/src/contentmanagerview.cpp index 24ba1f9..75847d3 100644 --- a/src/contentmanagerview.cpp +++ b/src/contentmanagerview.cpp @@ -19,17 +19,7 @@ ContentManagerView::ContentManagerView(QWidget *parent) loader = new KiwixLoader(mp_ui->loading); mp_ui->stackedWidget->setCurrentIndex(0); - connect(mp_ui->m_view, &QTreeView::clicked, [=](QModelIndex index) { - if (index.column() == (mp_ui->m_view->model()->columnCount() - 1)) - return; - - auto zeroColIndex = index.siblingAtColumn(0); - if (mp_ui->m_view->isExpanded(zeroColIndex)) { - mp_ui->m_view->collapse(zeroColIndex); - } else { - mp_ui->m_view->expand(zeroColIndex); - } - }); + connect(mp_ui->m_view, &QTreeView::clicked, this, &ContentManagerView::onClicked); } ContentManagerView::~ContentManagerView() @@ -46,3 +36,16 @@ void ContentManagerView::showLoader(bool show) loader->stopAnimation(); } } + +void ContentManagerView::onClicked(QModelIndex index) +{ + if (index.column() == (mp_ui->m_view->model()->columnCount() - 1)) + return; + + auto zeroColIndex = index.siblingAtColumn(0); + if (mp_ui->m_view->isExpanded(zeroColIndex)) { + mp_ui->m_view->collapse(zeroColIndex); + } else { + mp_ui->m_view->expand(zeroColIndex); + } +} diff --git a/src/contentmanagerview.h b/src/contentmanagerview.h index 68017a5..790faf3 100644 --- a/src/contentmanagerview.h +++ b/src/contentmanagerview.h @@ -20,6 +20,7 @@ public: public slots: void showLoader(bool show); + void onClicked(QModelIndex index); private: Ui::contentmanagerview *mp_ui; From bc1107b0769d729d41d512ad33a608c00a4e8513 Mon Sep 17 00:00:00 2001 From: ShaopengLin Date: Fri, 7 Jun 2024 18:07:46 -0400 Subject: [PATCH 2/5] Introduce method to decide whether index contain description index.parent().isValid replaced with static function isDescriptionNode. --- src/contentmanagerdelegate.cpp | 4 ++-- src/contentmanagermodel.cpp | 2 +- src/contentmanagermodel.h | 5 +++++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/contentmanagerdelegate.cpp b/src/contentmanagerdelegate.cpp index bcdf7d5..2ba8666 100644 --- a/src/contentmanagerdelegate.cpp +++ b/src/contentmanagerdelegate.cpp @@ -194,7 +194,7 @@ void ContentManagerDelegate::paintBookState(QPainter *p, const QStyleOptionViewI void ContentManagerDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QRect r = option.rect; - if (index.parent().isValid()) { + if (isDescriptionIndex(index)) { // additional info QRect nRect = r; auto viewWidth = KiwixApp::instance()->getContentManager()->getView()->getView()->width(); @@ -293,7 +293,7 @@ QSize ContentManagerDelegate::sizeHint(const QStyleOptionViewItem &option, const { Q_UNUSED(option); - if (index.parent().isValid()) { + if (isDescriptionIndex(index)) { return QSize(300, 70); } return QSize(50, 70); diff --git a/src/contentmanagermodel.cpp b/src/contentmanagermodel.cpp index 559c9fb..dd9e3b9 100644 --- a/src/contentmanagermodel.cpp +++ b/src/contentmanagermodel.cpp @@ -86,7 +86,7 @@ QVariant ContentManagerModel::data(const QModelIndex& index, int role) const Qt::ItemFlags ContentManagerModel::flags(const QModelIndex &index) const { Qt::ItemFlags defaultFlags = QAbstractItemModel::flags(index); - if (index.isValid() && index.parent().isValid()) { + if (isDescriptionIndex(index)) { return defaultFlags & ~Qt::ItemIsDropEnabled & ~Qt::ItemIsDragEnabled & ~Qt::ItemIsSelectable & ~Qt::ItemIsEditable & ~Qt::ItemIsUserCheckable; } return defaultFlags; diff --git a/src/contentmanagermodel.h b/src/contentmanagermodel.h index fb09bed..bda0a42 100644 --- a/src/contentmanagermodel.h +++ b/src/contentmanagermodel.h @@ -97,4 +97,9 @@ private: // data QMap m_iconMap; }; +inline bool isDescriptionIndex(const QModelIndex& index) +{ + return index.parent().isValid(); +} + #endif // CONTENTMANAGERMODEL_H From e63561161beeb901a3ed72c6d7ed41919abfe9b6 Mon Sep 17 00:00:00 2001 From: ShaopengLin Date: Sun, 9 Jun 2024 04:27:02 -0400 Subject: [PATCH 3/5] Fixed hover misbehaviour over zim description Clicking and hovering the description dropdown of zim no longer whites out. Replaced manual drawing with default handling as it is no longer necessary. --- src/contentmanagerdelegate.cpp | 12 +++--------- src/contentmanagermodel.cpp | 9 ++++++--- src/contentmanagerview.cpp | 7 +++++++ src/contentmanagerview.h | 1 + src/descriptionnode.cpp | 2 +- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/contentmanagerdelegate.cpp b/src/contentmanagerdelegate.cpp index 2ba8666..e9abade 100644 --- a/src/contentmanagerdelegate.cpp +++ b/src/contentmanagerdelegate.cpp @@ -193,15 +193,9 @@ void ContentManagerDelegate::paintBookState(QPainter *p, const QStyleOptionViewI void ContentManagerDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { - QRect r = option.rect; - if (isDescriptionIndex(index)) { - // additional info - QRect nRect = r; - auto viewWidth = KiwixApp::instance()->getContentManager()->getView()->getView()->width(); - nRect.setWidth(viewWidth); - painter->drawText(nRect, Qt::AlignLeft | Qt::AlignVCenter, index.data(Qt::UserRole+1).toString()); - return; - } + if (isDescriptionIndex(index)) + return QStyledItemDelegate::paint(painter, option, index); + QStyleOptionViewItem eOpt = option; if (index.column() == 1) { auto bFont = painter->font(); diff --git a/src/contentmanagermodel.cpp b/src/contentmanagermodel.cpp index dd9e3b9..68c7a34 100644 --- a/src/contentmanagermodel.cpp +++ b/src/contentmanagermodel.cpp @@ -60,10 +60,13 @@ QVariant ContentManagerModel::data(const QModelIndex& index, int role) const return QVariant(); const auto col = index.column(); - const bool isThumbnailRequest = col == 0 && role == Qt::DecorationRole; - const bool otherDataRequest = col != 0 && (role == Qt::DisplayRole || role == Qt::UserRole+1 ); + const bool isThumbnailRequest = + !isDescriptionIndex(index) && col == 0 && role == Qt::DecorationRole; + const bool isDescriptionRequest = + isDescriptionIndex(index) && col == 0 && role == Qt::DisplayRole; + const bool otherDataRequest = col != 0 && role == Qt::DisplayRole; - if ( !isThumbnailRequest && !otherDataRequest ) + if ( !isThumbnailRequest && !otherDataRequest && !isDescriptionRequest ) return QVariant(); const auto item = static_cast(index.internalPointer()); diff --git a/src/contentmanagerview.cpp b/src/contentmanagerview.cpp index 75847d3..bce5a78 100644 --- a/src/contentmanagerview.cpp +++ b/src/contentmanagerview.cpp @@ -20,6 +20,7 @@ ContentManagerView::ContentManagerView(QWidget *parent) mp_ui->stackedWidget->setCurrentIndex(0); connect(mp_ui->m_view, &QTreeView::clicked, this, &ContentManagerView::onClicked); + connect(mp_ui->m_view, &QTreeView::expanded, this, &ContentManagerView::onExpanded); } ContentManagerView::~ContentManagerView() @@ -49,3 +50,9 @@ void ContentManagerView::onClicked(QModelIndex index) mp_ui->m_view->expand(zeroColIndex); } } + +void ContentManagerView::onExpanded(QModelIndex index) +{ + if (!mp_ui->m_view->isFirstColumnSpanned(0, index)) + mp_ui->m_view->setFirstColumnSpanned(0, index, true); +} diff --git a/src/contentmanagerview.h b/src/contentmanagerview.h index 790faf3..b5b5140 100644 --- a/src/contentmanagerview.h +++ b/src/contentmanagerview.h @@ -21,6 +21,7 @@ public: public slots: void showLoader(bool show); void onClicked(QModelIndex index); + void onExpanded(QModelIndex index); private: Ui::contentmanagerview *mp_ui; diff --git a/src/descriptionnode.cpp b/src/descriptionnode.cpp index b2bb544..fd5c549 100644 --- a/src/descriptionnode.cpp +++ b/src/descriptionnode.cpp @@ -36,7 +36,7 @@ int DescriptionNode::columnCount() const QVariant DescriptionNode::data(int column) { - if (column == 1) + if (column == 0) return m_desc; return QVariant(); } From ef7645ec67e1aff5cd3d23b34e428ff2294956ee Mon Sep 17 00:00:00 2001 From: ShaopengLin Date: Sun, 9 Jun 2024 04:33:51 -0400 Subject: [PATCH 4/5] Description row fits now content on expand The description row height fits the text context when expanded. --- src/contentmanagerdelegate.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/contentmanagerdelegate.cpp b/src/contentmanagerdelegate.cpp index e9abade..6fb9428 100644 --- a/src/contentmanagerdelegate.cpp +++ b/src/contentmanagerdelegate.cpp @@ -285,10 +285,22 @@ void ContentManagerDelegate::handleLastColumnClicked(const QModelIndex& index, Q QSize ContentManagerDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { - Q_UNUSED(option); + if (isDescriptionIndex(index)) + { + const auto treeView = KiwixApp::instance()->getContentManager()->getView()->getView(); - if (isDescriptionIndex(index)) { - return QSize(300, 70); + const int width = treeView->header()->length() - 2*treeView->indentation(); + // XXX: see QTreeView::padding in resources/css/_contentManager.css + const int verticalPadding = 4; + const int horizontalPadding = 4; + QRect descRect(0, 0, width - 2 * horizontalPadding, 0); + + /* Based on the rectangle and text, find the best fitting size. */ + QFontMetrics fm(option.font); + const QString text = index.data().toString(); + const auto format = Qt::AlignLeft | Qt::AlignVCenter | Qt::TextWordWrap; + const int textHeight = fm.boundingRect(descRect, format, text).height(); + return QSize(width, std::max(textHeight + verticalPadding, 70)); } return QSize(50, 70); } From fa2d42554dff048377c95cb4108c7ae5b201a297 Mon Sep 17 00:00:00 2001 From: ShaopengLin Date: Sun, 9 Jun 2024 04:43:51 -0400 Subject: [PATCH 5/5] Description row fits to content when screen size change Already expanded description row will now adjust with respect to any window size changes to fit the content. --- src/contentmanagerview.cpp | 19 +++++++++++++++++++ src/contentmanagerview.h | 4 ++++ src/mainwindow.cpp | 6 ++++++ src/mainwindow.h | 1 + 4 files changed, 30 insertions(+) diff --git a/src/contentmanagerview.cpp b/src/contentmanagerview.cpp index bce5a78..11bcbb7 100644 --- a/src/contentmanagerview.cpp +++ b/src/contentmanagerview.cpp @@ -21,6 +21,7 @@ ContentManagerView::ContentManagerView(QWidget *parent) connect(mp_ui->m_view, &QTreeView::clicked, this, &ContentManagerView::onClicked); connect(mp_ui->m_view, &QTreeView::expanded, this, &ContentManagerView::onExpanded); + connect(this, &ContentManagerView::sizeHintChanged, managerDelegate, &QStyledItemDelegate::sizeHintChanged); } ContentManagerView::~ContentManagerView() @@ -56,3 +57,21 @@ void ContentManagerView::onExpanded(QModelIndex index) if (!mp_ui->m_view->isFirstColumnSpanned(0, index)) mp_ui->m_view->setFirstColumnSpanned(0, index, true); } + +/** + * @brief Notify delegate to update size hint of the visible description rows. + */ +void ContentManagerView::updateSizeHint() +{ + auto view = this->getView(); + if (!view->isVisible()) + return; + + auto visibleIndex = view->indexAt(view->rect().topLeft()); + while (visibleIndex.isValid()) + { + if (isDescriptionIndex(visibleIndex)) + emit sizeHintChanged(visibleIndex); + visibleIndex = view->indexBelow(visibleIndex); + } +} diff --git a/src/contentmanagerview.h b/src/contentmanagerview.h index b5b5140..ec5174d 100644 --- a/src/contentmanagerview.h +++ b/src/contentmanagerview.h @@ -22,6 +22,10 @@ public slots: void showLoader(bool show); void onClicked(QModelIndex index); void onExpanded(QModelIndex index); + void updateSizeHint(); + +signals: + void sizeHintChanged(const QModelIndex& index); private: Ui::contentmanagerview *mp_ui; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index a751d3b..a34b872 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -125,6 +125,12 @@ void MainWindow::closeEvent(QCloseEvent *event) QMainWindow::closeEvent(event); } +void MainWindow::resizeEvent(QResizeEvent *event) +{ + QMainWindow::resizeEvent(event); + KiwixApp::instance()->getContentManager()->getView()->updateSizeHint(); +} + void MainWindow::readingListToggled(bool state) { if (state) { diff --git a/src/mainwindow.h b/src/mainwindow.h index c6b54b2..2dbb882 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -29,6 +29,7 @@ public: protected: bool eventFilter(QObject* object, QEvent* event) override; void closeEvent(QCloseEvent *event) override; + void resizeEvent(QResizeEvent *event) override; private slots: void toggleFullScreen();