aboutsummaryrefslogtreecommitdiff
path: root/src/blocker/filtertree.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/blocker/filtertree.cpp')
-rw-r--r--src/blocker/filtertree.cpp131
1 files changed, 131 insertions, 0 deletions
diff --git a/src/blocker/filtertree.cpp b/src/blocker/filtertree.cpp
new file mode 100644
index 0000000..e8b7ed3
--- /dev/null
+++ b/src/blocker/filtertree.cpp
@@ -0,0 +1,131 @@
+/*******************************************************************************
+ **
+ ** smolbote: yet another qute browser
+ ** Copyright (C) 2017 Xian Nox
+ **
+ ** This program 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.
+ **
+ ** This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
+ **
+ ******************************************************************************/
+
+#include "filtertree.h"
+#include "filternode.h"
+
+FilterTree::FilterTree(QObject *parent) :
+ QAbstractItemModel(parent)
+{
+ // create some nodes here
+ QList<QVariant> rootData;
+ rootData << "Domain" << "Request" << "Blocked" << "Allowed Types" << "Blocked Types";
+ rootItem = new FilterNode(rootData);
+}
+
+FilterTree::~FilterTree()
+{
+ delete rootItem;
+}
+
+QModelIndex FilterTree::index(int row, int column, const QModelIndex &parent) const
+{
+ if(!hasIndex(row, column, parent)) {
+ return QModelIndex();
+ }
+
+ FilterNode *parentItem;
+ if(!parent.isValid()) {
+ parentItem = rootItem;
+ } else {
+ parentItem = static_cast<FilterNode*>(parent.internalPointer());
+ }
+
+ FilterNode *childItem = parentItem->child(row);
+ if(childItem) {
+ return createIndex(row, column, childItem);
+ } else {
+ return QModelIndex();
+ }
+}
+
+QModelIndex FilterTree::parent(const QModelIndex &index) const
+{
+ if(!index.isValid()) {
+ return QModelIndex();
+ }
+
+ FilterNode *childItem = static_cast<FilterNode*>(index.internalPointer());
+ FilterNode *parentItem = childItem->parentItem();
+
+ if(parentItem == rootItem) {
+ return QModelIndex();
+ }
+
+ return createIndex(parentItem->row(), 0, parentItem);
+}
+
+int FilterTree::rowCount(const QModelIndex &parent) const
+{
+ FilterNode *parentItem;
+ if(parent.column() > 0) {
+ return 0;
+ }
+
+ if(!parent.isValid()) {
+ parentItem = rootItem;
+ } else {
+ parentItem = static_cast<FilterNode*>(parent.internalPointer());
+ }
+
+ return parentItem->childCount();
+}
+
+int FilterTree::columnCount(const QModelIndex &parent) const
+{
+ if(parent.isValid()) {
+ return static_cast<FilterNode*>(parent.internalPointer())->columnCount();
+ } else {
+ return rootItem->columnCount();
+ }
+}
+
+QVariant FilterTree::data(const QModelIndex &index, int role) const
+{
+ if(!index.isValid()) {
+ return QVariant();
+ }
+
+ if(role != Qt::DisplayRole) {
+ return QVariant();
+ }
+
+ return static_cast<FilterNode*>(index.internalPointer())->data(index.column());
+}
+
+QVariant FilterTree::headerData(int section, Qt::Orientation orientation, int role) const
+{
+ if(orientation != Qt::Horizontal) {
+ return QVariant();
+ }
+
+ if(role != Qt::DisplayRole) {
+ return QVariant();
+ }
+
+ return rootItem->data(section);
+}
+
+FilterNode *FilterTree::addFilter(QList<QVariant> &data)
+{
+ FilterNode *node = new FilterNode(data, rootItem);
+ rootItem->appendChild(node);
+ return node;
+}