From 1f33d86340266531ebbce25261a105e4c7945265 Mon Sep 17 00:00:00 2001 From: Nikhil Tanwar <2002nikhiltanwar@gmail.com> Date: Thu, 22 Jun 2023 12:37:20 +0530 Subject: [PATCH] Delegate for ContentManagerView Added a new delegate to style the view. Added the following things: 1. Button styling and click to open book 2. Increased size of rows --- kiwix-desktop.pro | 2 + src/contentmanager.cpp | 1 + src/contentmanagerdelegate.cpp | 73 ++++++++++++++++++++++++++++++++++ src/contentmanagerdelegate.h | 21 ++++++++++ src/contentmanagermodel.cpp | 3 +- src/contentmanagerview.cpp | 3 ++ 6 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/contentmanagerdelegate.cpp create mode 100644 src/contentmanagerdelegate.h diff --git a/kiwix-desktop.pro b/kiwix-desktop.pro index 00579d6..e672486 100644 --- a/kiwix-desktop.pro +++ b/kiwix-desktop.pro @@ -34,6 +34,7 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ + src/contentmanagerdelegate.cpp \ src/contentmanagermodel.cpp \ src/contenttypefilter.cpp \ src/findinpagebar.cpp \ @@ -69,6 +70,7 @@ SOURCES += \ src/static_content.cpp HEADERS += \ + src/contentmanagerdelegate.h \ src/contentmanagermodel.h \ src/contenttypefilter.h \ src/findinpagebar.h \ diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index 2f49c3b..21fbe59 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -15,6 +15,7 @@ #include #include #include +#include "contentmanagerdelegate.h" ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, QObject *parent) : QObject(parent), diff --git a/src/contentmanagerdelegate.cpp b/src/contentmanagerdelegate.cpp new file mode 100644 index 0000000..49d8f65 --- /dev/null +++ b/src/contentmanagerdelegate.cpp @@ -0,0 +1,73 @@ +#include +#include "contentmanagerdelegate.h" +#include +#include +#include +#include "kiwixapp.h" + +ContentManagerDelegate::ContentManagerDelegate(QObject *parent) + : QStyledItemDelegate(parent), baseButton(new QPushButton) +{ + baseButton->setStyleSheet("background-color: white;" + "border: 0;" + "font-weight: bold;" + "font-family: Selawik;" + "color: blue;" + "margin: 4px;"); +} + + +void ContentManagerDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + QStyleOptionButton button; + QRect r = option.rect; + int x,y,w,h; + x = r.left(); + y = r.top(); + w = r.width(); + h = r.height(); + button.rect = QRect(x,y,w,h); + button.text = "Open"; + button.state = QStyle::State_Enabled; + if (index.column() == 5 && index.data(Qt::UserRole+1) == QVariant()) { + baseButton->style()->drawControl( QStyle::CE_PushButton, &button, painter, baseButton.data()); + return; + } + QStyledItemDelegate::paint(painter, option, index); +} + +bool ContentManagerDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) +{ + if(event->type() == QEvent::MouseButtonRelease ) + { + QMouseEvent * e = (QMouseEvent *)event; + int clickX = e->x(); + int clickY = e->y(); + + QRect r = option.rect;//getting the rect of the cell + int x,y,w,h; + x = r.left();//the X coordinate + y = r.top();//the Y coordinate + w = r.width();//button width + h = r.height();//button height + + + if(index.column() == 5 && clickX > x && clickX < x + w ) + if( clickY > y && clickY < y + h ) + { + const auto modeel = index.model(); + const auto id = modeel->data(index).toString(); + KiwixApp::instance()->getContentManager()->openBook(id); + } + } + + return true; +} + +QSize ContentManagerDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const +{ + if (index.data(Qt::UserRole+1) != QVariant()) { + return QSize(300, 70); + } + return QSize(50, 70); +} diff --git a/src/contentmanagerdelegate.h b/src/contentmanagerdelegate.h new file mode 100644 index 0000000..0e9f474 --- /dev/null +++ b/src/contentmanagerdelegate.h @@ -0,0 +1,21 @@ +#ifndef CONTENTMANAGERDELEGATE_H +#define CONTENTMANAGERDELEGATE_H + +#include +#include + +class ContentManagerDelegate : public QStyledItemDelegate +{ + Q_OBJECT +public: + + ContentManagerDelegate(QObject *parent=0); + void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; + bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override; + QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override; + +private: + QScopedPointer baseButton; +}; + +#endif // CONTENTMANAGERDELEGATE_H diff --git a/src/contentmanagermodel.cpp b/src/contentmanagermodel.cpp index 9a1a84e..ddfa54f 100644 --- a/src/contentmanagermodel.cpp +++ b/src/contentmanagermodel.cpp @@ -31,7 +31,8 @@ QVariant ContentManagerModel::data(const QModelIndex& index, int role) const Node *item = static_cast(index.internalPointer()); const auto displayRole = role == Qt::DisplayRole; - if (displayRole) + const auto additionalInfoRole = role == Qt::UserRole+1 && item->isAdditonal(); + if (displayRole || additionalInfoRole) return item->data(index.column()); return QVariant(); diff --git a/src/contentmanagerview.cpp b/src/contentmanagerview.cpp index 72601c5..dacfd2b 100644 --- a/src/contentmanagerview.cpp +++ b/src/contentmanagerview.cpp @@ -1,9 +1,12 @@ #include "contentmanagerview.h" #include #include "kiwixapp.h" +#include "contentmanagerdelegate.h" ContentManagerView::ContentManagerView(QWidget *parent) : QTreeView(parent) { setSortingEnabled(true); + auto managerDelegate = new ContentManagerDelegate(); + setItemDelegate(managerDelegate); }