diff --git a/main_window.cpp b/main_window.cpp index 42bfa6e..7d7a3fe 100644 --- a/main_window.cpp +++ b/main_window.cpp @@ -16,15 +16,19 @@ */ #include "main_window.hpp" +#include "notebook_tree_model.hpp" #include "config.h" -#include +#include namespace duck { -MainWindow::MainWindow() { +MainWindow::MainWindow (Node* tree) : + m_tree(new QTreeView) +{ setWindowTitle(APP_NAME); - QLabel* const main_label = new QLabel("Main label thing"); - setCentralWidget(main_label); - main_label->setAlignment(Qt::AlignCenter); + setCentralWidget(m_tree); + + auto* model = new NotebookTreeModel(tree, m_tree); + m_tree->setModel(model); } MainWindow::~MainWindow() noexcept = default; diff --git a/main_window.hpp b/main_window.hpp index bb7f8f3..26fbb0e 100644 --- a/main_window.hpp +++ b/main_window.hpp @@ -19,11 +19,18 @@ #include +class QTreeView; + namespace duck { +class Node; + class MainWindow : public QMainWindow { Q_OBJECT public: - MainWindow(); + MainWindow (Node* tree); virtual ~MainWindow() noexcept; + +private: + QTreeView* m_tree; }; } //namespace duck diff --git a/meson.build b/meson.build index 35deab5..dbbe024 100644 --- a/meson.build +++ b/meson.build @@ -20,7 +20,10 @@ qt5 = import('qt5') qt5_dep = dependency('qt5', modules: ['Core', 'Gui', 'Widgets']) moc_files = qt5.preprocess( - moc_headers: 'main_window.hpp', + moc_headers: [ + 'main_window.hpp', + 'notebook_tree_model.hpp', + ], dependencies: qt5_dep ) @@ -33,6 +36,7 @@ executable(meson.project_name(), 'main_window.cpp', moc_files, 'notebook_tree.cpp', + 'notebook_tree_model.cpp', dependencies: [ simdjson_dep, qt5_dep, diff --git a/notebook_tree_model.cpp b/notebook_tree_model.cpp new file mode 100644 index 0000000..1cb341e --- /dev/null +++ b/notebook_tree_model.cpp @@ -0,0 +1,90 @@ +/* Copyright 2021, Michele Santullo + * This file is part of remarkable_tool. + * + * remarkable_tool is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Remarkable_tool is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with remarkable_tool. If not, see . + */ + +#include "notebook_tree_model.hpp" +#include "notebook_tree.hpp" + +namespace duck { +NotebookTreeModel::NotebookTreeModel (Node* root, QObject* parent) : + QAbstractItemModel(parent), + m_tree(root) +{ +} + +NotebookTreeModel::~NotebookTreeModel() noexcept = default; + +int NotebookTreeModel::rowCount (const QModelIndex& parent) const { + if (parent.column() > 0) + return 0; + + if (not parent.isValid()) + return static_cast(m_tree->children.size()); + else + return static_cast(static_cast(parent.internalPointer())->children.size()); +} + +int NotebookTreeModel::columnCount (const QModelIndex&) const { + return 1; +} + +Qt::ItemFlags NotebookTreeModel::flags (const QModelIndex& index) const { + if (!index.isValid()) + return Qt::NoItemFlags; + + return QAbstractItemModel::flags(index); +} + +QModelIndex NotebookTreeModel::index ( + int row, + int column, + const QModelIndex& parent +) const { + if (not hasIndex(row, column, parent)) + return {}; + + Node* const parent_item = (parent.isValid() ? static_cast(parent.internalPointer()) : m_tree); + Node* child_item = &parent_item->children[row]; + return createIndex(row, column, child_item); +} + +QVariant NotebookTreeModel::data (const QModelIndex& index, int role) const { + if (not index.isValid() or role != Qt::DisplayRole) + return {}; + + Node* const item = static_cast(index.internalPointer()); + return QString::fromUtf8(item->info->visible_name.data(), static_cast(item->info->visible_name.size())); +} + +QVariant NotebookTreeModel::headerData (int section, Qt::Orientation orientation, int role) const { + return {}; +} + +QModelIndex NotebookTreeModel::parent (const QModelIndex& index) const { + if (not index.isValid()) + return {}; + + Node* const curr_node = static_cast(index.internalPointer()); + if (curr_node->parent == m_tree) { + return {}; + } + else { + const Node* const parent_node = curr_node->parent; + const int parent_index = static_cast(parent_node - parent_node->parent->children.data()); + return createIndex(parent_index, 0, const_cast(parent_node)); + } +} +} //namespace duck diff --git a/notebook_tree_model.hpp b/notebook_tree_model.hpp new file mode 100644 index 0000000..851a6a3 --- /dev/null +++ b/notebook_tree_model.hpp @@ -0,0 +1,42 @@ +/* Copyright 2021, Michele Santullo + * This file is part of remarkable_tool. + * + * remarkable_tool is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Remarkable_tool is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with remarkable_tool. If not, see . + */ + +#pragma once + +#include + +namespace duck { +class Node; + +class NotebookTreeModel : public QAbstractItemModel { + Q_OBJECT +public: + NotebookTreeModel (Node* root, QObject* parent); + virtual ~NotebookTreeModel() noexcept; + + int rowCount (const QModelIndex& parent = QModelIndex()) const override; + int columnCount (const QModelIndex& parent = QModelIndex()) const override; + Qt::ItemFlags flags (const QModelIndex& index) const override; + QModelIndex index (int row, int column, const QModelIndex& parent = QModelIndex()) const override; + QVariant data (const QModelIndex& index, int role) const override; + QVariant headerData (int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; + QModelIndex parent (const QModelIndex& index) const override; + +private: + Node* m_tree; +}; +} //namespace duck