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
This commit is contained in:
Nikhil Tanwar 2023-06-22 12:37:20 +05:30
parent bddda5cb2b
commit 1f33d86340
6 changed files with 102 additions and 1 deletions

View File

@ -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 \

View File

@ -15,6 +15,7 @@
#include <zim/error.h>
#include <zim/item.h>
#include <QHeaderView>
#include "contentmanagerdelegate.h"
ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, QObject *parent)
: QObject(parent),

View File

@ -0,0 +1,73 @@
#include <QtGui>
#include "contentmanagerdelegate.h"
#include <QApplication>
#include <QDialog>
#include <QStyleOptionViewItemV4>
#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);
}

View File

@ -0,0 +1,21 @@
#ifndef CONTENTMANAGERDELEGATE_H
#define CONTENTMANAGERDELEGATE_H
#include <QStyledItemDelegate>
#include <QPushButton>
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<QPushButton> baseButton;
};
#endif // CONTENTMANAGERDELEGATE_H

View File

@ -31,7 +31,8 @@ QVariant ContentManagerModel::data(const QModelIndex& index, int role) const
Node *item = static_cast<Node*>(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();

View File

@ -1,9 +1,12 @@
#include "contentmanagerview.h"
#include <QFile>
#include "kiwixapp.h"
#include "contentmanagerdelegate.h"
ContentManagerView::ContentManagerView(QWidget *parent)
: QTreeView(parent)
{
setSortingEnabled(true);
auto managerDelegate = new ContentManagerDelegate();
setItemDelegate(managerDelegate);
}