diff --git a/kiwix-desktop.pro b/kiwix-desktop.pro index 34db554..00579d6 100644 --- a/kiwix-desktop.pro +++ b/kiwix-desktop.pro @@ -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 \ diff --git a/src/contentmanager.cpp b/src/contentmanager.cpp index ae22154..f24d029 100644 --- a/src/contentmanager.cpp +++ b/src/contentmanager.cpp @@ -11,6 +11,7 @@ #include #include #include +#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());}); diff --git a/src/contentmanagermodel.cpp b/src/contentmanagermodel.cpp new file mode 100644 index 0000000..ec5bfee --- /dev/null +++ b/src/contentmanagermodel.cpp @@ -0,0 +1,103 @@ +#include "contentmanagermodel.h" +#include "node.h" +#include +#include +#include +#include +#include + + +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(parent.internalPointer())->columnCount(); + return rootNode->columnCount(); +} + +QVariant ContentManagerModel::data(const QModelIndex& index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + Node *item = static_cast(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(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(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(); + } +} diff --git a/src/contentmanagermodel.h b/src/contentmanagermodel.h new file mode 100644 index 0000000..da45b63 --- /dev/null +++ b/src/contentmanagermodel.h @@ -0,0 +1,32 @@ +#ifndef CONTENTMANAGERMODEL_H +#define CONTENTMANAGERMODEL_H + +#include +#include +#include + +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 diff --git a/src/node.cpp b/src/node.cpp new file mode 100644 index 0000000..47b1cb5 --- /dev/null +++ b/src/node.cpp @@ -0,0 +1,52 @@ +#include "node.h" + +Node::Node(const QList &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(this)); + + return 0; +} diff --git a/src/node.h b/src/node.h new file mode 100644 index 0000000..91676e1 --- /dev/null +++ b/src/node.h @@ -0,0 +1,27 @@ +#ifndef NODE_H +#define NODE_H + +#include +#include + +class Node +{ +public: + explicit Node(const QList &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 m_itemData; + Node *m_parentItem; + QList m_childItems; +}; + + +#endif // NODE_H