Show notebooks in a tree widget

This commit is contained in:
King_DuckZ 2021-03-27 02:58:24 +01:00
parent 6b9556e711
commit 9319e27c5a
5 changed files with 154 additions and 7 deletions

View file

@ -16,15 +16,19 @@
*/
#include "main_window.hpp"
#include "notebook_tree_model.hpp"
#include "config.h"
#include <QLabel>
#include <QTreeView>
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;

View file

@ -19,11 +19,18 @@
#include <QMainWindow>
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

View file

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

90
notebook_tree_model.cpp Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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<int>(m_tree->children.size());
else
return static_cast<int>(static_cast<Node*>(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<Node*>(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<Node*>(index.internalPointer());
return QString::fromUtf8(item->info->visible_name.data(), static_cast<int>(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<Node*>(index.internalPointer());
if (curr_node->parent == m_tree) {
return {};
}
else {
const Node* const parent_node = curr_node->parent;
const int parent_index = static_cast<int>(parent_node - parent_node->parent->children.data());
return createIndex(parent_index, 0, const_cast<Node*>(parent_node));
}
}
} //namespace duck

42
notebook_tree_model.hpp Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QAbstractItemModel>
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