Basic model implemented for TreeView

This implements a basic model for QTreeModel
Currently, it only shows 1 row with some random data.
In future commits, data from ContentManager::getBookInfos will be taken
This commit is contained in:
Nikhil Tanwar 2023-06-22 11:17:46 +05:30
parent 3c69c1a5c1
commit e5c8a31ec0
6 changed files with 221 additions and 0 deletions

View File

@ -34,8 +34,10 @@ DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
src/contentmanagermodel.cpp \
src/contenttypefilter.cpp \
src/findinpagebar.cpp \
src/node.cpp \
src/suggestionlistworker.cpp \
src/translation.cpp \
src/main.cpp \
@ -67,8 +69,10 @@ SOURCES += \
src/static_content.cpp
HEADERS += \
src/contentmanagermodel.h \
src/contenttypefilter.h \
src/findinpagebar.h \
src/node.h \
src/suggestionlistworker.h \
src/translation.h \
src/mainwindow.h \

View File

@ -11,6 +11,7 @@
#include <QDir>
#include <QStorageInfo>
#include <QMessageBox>
#include "contentmanagermodel.h"
ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader, QObject *parent)
: QObject(parent),
@ -21,6 +22,8 @@ ContentManager::ContentManager(Library* library, kiwix::Downloader* downloader,
// mp_view will be passed to the tab who will take ownership,
// so, we don't need to delete it.
mp_view = new ContentManagerView();
auto managerModel = new ContentManagerModel();
mp_view->setModel(managerModel);
mp_view->show();
setCurrentLanguage(QLocale().name().split("_").at(0));
connect(mp_library, &Library::booksChanged, this, [=]() {emit(this->booksChanged());});

103
src/contentmanagermodel.cpp Normal file
View File

@ -0,0 +1,103 @@
#include "contentmanagermodel.h"
#include "node.h"
#include<QList>
#include<QMap>
#include<QDebug>
#include <QStringList>
#include <QSize>
ContentManagerModel::ContentManagerModel(QObject *parent)
: QAbstractItemModel(parent)
{
rootNode = new Node({tr("Icon"), tr("Name"), tr("Date"), tr("Size"), tr("Content Type"), tr("Download")});
auto childNode = new Node({"someIcon", "test name", "test date", "test size", "test content", "download link"}, rootNode);
rootNode->appendChild(childNode);
}
ContentManagerModel::~ContentManagerModel()
{
delete rootNode;
}
int ContentManagerModel::columnCount(const QModelIndex &parent) const
{
if (parent.isValid())
return static_cast<Node*>(parent.internalPointer())->columnCount();
return rootNode->columnCount();
}
QVariant ContentManagerModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
return QVariant();
Node *item = static_cast<Node*>(index.internalPointer());
const auto displayRole = role == Qt::DisplayRole;
if (displayRole)
return item->data(index.column());
return QVariant();
}
Qt::ItemFlags ContentManagerModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags defaultFlags = QAbstractItemModel::flags(index);
if (index.isValid() && index.parent().isValid()) {
return defaultFlags & ~Qt::ItemIsDropEnabled & ~Qt::ItemIsDragEnabled & ~Qt::ItemIsSelectable & ~Qt::ItemIsEditable & ~Qt::ItemIsUserCheckable;
}
return defaultFlags;
}
QModelIndex ContentManagerModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
Node *parentItem;
if (!parent.isValid())
parentItem = rootNode;
else
parentItem = static_cast<Node*>(parent.internalPointer());
Node *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
return QModelIndex();
}
QModelIndex ContentManagerModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
Node *childItem = static_cast<Node*>(index.internalPointer());
Node *parentItem = childItem->parentItem();
if (parentItem == rootNode)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
int ContentManagerModel::rowCount(const QModelIndex &parent) const
{
return 5;
}
QVariant ContentManagerModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole || orientation != Qt::Horizontal)
return QVariant();
switch (section)
{
case 0: return QVariant();
case 1: return "Name";
case 2: return "Date";
case 3: return "Size";
case 4: return "Content Type";
default: return QVariant();
}
}

32
src/contentmanagermodel.h Normal file
View File

@ -0,0 +1,32 @@
#ifndef CONTENTMANAGERMODEL_H
#define CONTENTMANAGERMODEL_H
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>
class Node;
class ContentManagerModel : public QAbstractItemModel
{
Q_OBJECT
public:
explicit ContentManagerModel(QObject *parent = nullptr);
~ContentManagerModel();
QVariant data(const QModelIndex &index, int role) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
QVariant headerData(int section, Qt::Orientation orientation,
int role = Qt::DisplayRole) const override;
QModelIndex index(int row, int column,
const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
private:
Node *rootNode;
};
#endif // CONTENTMANAGERMODEL_H

52
src/node.cpp Normal file
View File

@ -0,0 +1,52 @@
#include "node.h"
Node::Node(const QList<QVariant> &data, Node *parent)
: m_itemData(data), m_parentItem(parent)
{}
Node::~Node()
{
qDeleteAll(m_childItems);
}
void Node::appendChild(Node *item)
{
m_childItems.append(item);
}
Node *Node::child(int row)
{
if (row < 0 || row >= m_childItems.size())
return nullptr;
return m_childItems.at(row);
}
int Node::childCount() const
{
return m_childItems.count();
}
int Node::columnCount() const
{
return m_itemData.count();
}
QVariant Node::data(int column) const
{
if (column < 0 || column >= m_itemData.size())
return QVariant();
return m_itemData.at(column);
}
Node *Node::parentItem()
{
return m_parentItem;
}
int Node::row() const
{
if (m_parentItem)
return m_parentItem->m_childItems.indexOf(const_cast<Node*>(this));
return 0;
}

27
src/node.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef NODE_H
#define NODE_H
#include <QVariant>
#include <QList>
class Node
{
public:
explicit Node(const QList<QVariant> &data, Node *parentNode = nullptr);
~Node();
void appendChild(Node *child);
Node *child(int row);
int childCount() const;
int columnCount() const;
QVariant data(int column) const;
int row() const;
Node *parentItem();
private:
QList<QVariant> m_itemData;
Node *m_parentItem;
QList<Node *> m_childItems;
};
#endif // NODE_H