From 52e934bef5f3d42589fefe10507613b37cf51072 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 25 May 2017 11:39:04 +0200 Subject: Displaying filter rules in a tree model view --- src/blocker/filtercollection.cpp | 11 +++- src/blocker/filtercollection.h | 2 + src/blocker/filternode.cpp | 71 +++++++++++++++++++++ src/blocker/filternode.h | 53 ++++++++++++++++ src/blocker/filtertree.cpp | 131 +++++++++++++++++++++++++++++++++++++++ src/blocker/filtertree.h | 50 +++++++++++++++ src/blocker/subscriptionform.ui | 26 +------- 7 files changed, 318 insertions(+), 26 deletions(-) create mode 100644 src/blocker/filternode.cpp create mode 100644 src/blocker/filternode.h create mode 100644 src/blocker/filtertree.cpp create mode 100644 src/blocker/filtertree.h (limited to 'src') diff --git a/src/blocker/filtercollection.cpp b/src/blocker/filtercollection.cpp index 2262008..5b0f96f 100644 --- a/src/blocker/filtercollection.cpp +++ b/src/blocker/filtercollection.cpp @@ -32,6 +32,10 @@ #include #include +#include +#include "filtertree.h" +#include "filternode.h" + FilterCollection::FilterCollection(const QString path, QWidget *parent) : QWidget(parent), ui(new Ui::SubscriptionForm) @@ -43,6 +47,8 @@ FilterCollection::FilterCollection(const QString path, QWidget *parent) : qDebug("Adding subscription [%s]", qUtf8Printable(m_url)); + m_filters = new FilterTree(this); + ui->treeView->setModel(m_filters); QFile filterFile(m_url); if(!filterFile.open(QIODevice::ReadOnly)) { @@ -104,7 +110,6 @@ void FilterCollection::load(const QJsonObject &json) for(QJsonValue v : json["rules"].toArray()) { r = createRule(v.toObject()); m_rules.append(r); - ui->blacklist_listWidget->addItem(r->filter()); } } @@ -123,6 +128,10 @@ FilterRule* FilterCollection::createRule(const QJsonObject &obj) rule = new FilterRule(obj["firstPartyUrl"].toString(), obj["requestUrl"].toString(), r, obj["shouldBlock"].toBool(), this); + QList nodeData; + nodeData << obj["firstPartyUrl"].toString() << obj["requestUrl"].toString(); + m_filters->addFilter(nodeData); + return rule; } diff --git a/src/blocker/filtercollection.h b/src/blocker/filtercollection.h index 08b42f3..689f0a0 100644 --- a/src/blocker/filtercollection.h +++ b/src/blocker/filtercollection.h @@ -24,6 +24,7 @@ #include #include #include "blocker/filterrule.h" +#include "filtertree.h" namespace Ui { class SubscriptionForm; @@ -58,6 +59,7 @@ private: QString m_url; QVector m_rules; + FilterTree *m_filters; }; diff --git a/src/blocker/filternode.cpp b/src/blocker/filternode.cpp new file mode 100644 index 0000000..e0612b5 --- /dev/null +++ b/src/blocker/filternode.cpp @@ -0,0 +1,71 @@ +/******************************************************************************* + ** + ** 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 . + ** + ******************************************************************************/ + +#include "filternode.h" + +FilterNode::FilterNode(const QList data, FilterNode *parentItem) +{ + m_itemData = data; + m_parentItem = parentItem; +} + +FilterNode::~FilterNode() +{ + qDeleteAll(m_children); +} + +FilterNode *FilterNode::parentItem() +{ + return m_parentItem; +} + +void FilterNode::appendChild(FilterNode *child) +{ + m_children.append(child); +} + +FilterNode *FilterNode::child(int row) +{ + return m_children.value(row); +} + +int FilterNode::childCount() const +{ + return m_children.count(); +} + +int FilterNode::columnCount() const +{ + return m_itemData.count(); +} + +QVariant FilterNode::data(int column) const +{ + return m_itemData.value(column); +} + +int FilterNode::row() const +{ + if(m_parentItem) { + return m_parentItem->m_children.indexOf(const_cast(this)); + } + + return 0; +} diff --git a/src/blocker/filternode.h b/src/blocker/filternode.h new file mode 100644 index 0000000..104f67d --- /dev/null +++ b/src/blocker/filternode.h @@ -0,0 +1,53 @@ +/******************************************************************************* + ** + ** 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 . + ** + ******************************************************************************/ + +#ifndef FILTERNODE_H +#define FILTERNODE_H + +#include +#include + +class FilterNode +{ +public: + explicit FilterNode(const QList data, FilterNode *parentItem = 0); + ~FilterNode(); + + FilterNode *parentItem(); + + // children + void appendChild(FilterNode *child); + FilterNode *child(int row); + int childCount() const; + + // data + int columnCount() const; + QVariant data(int column) const; + + int row() const; + +private: + FilterNode *m_parentItem; + QList m_children; + + QList m_itemData; +}; + +#endif // FILTERNODE_H 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 . + ** + ******************************************************************************/ + +#include "filtertree.h" +#include "filternode.h" + +FilterTree::FilterTree(QObject *parent) : + QAbstractItemModel(parent) +{ + // create some nodes here + QList 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(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(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(parent.internalPointer()); + } + + return parentItem->childCount(); +} + +int FilterTree::columnCount(const QModelIndex &parent) const +{ + if(parent.isValid()) { + return static_cast(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(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 &data) +{ + FilterNode *node = new FilterNode(data, rootItem); + rootItem->appendChild(node); + return node; +} diff --git a/src/blocker/filtertree.h b/src/blocker/filtertree.h new file mode 100644 index 0000000..eabca38 --- /dev/null +++ b/src/blocker/filtertree.h @@ -0,0 +1,50 @@ +/******************************************************************************* + ** + ** 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 . + ** + ******************************************************************************/ + +#ifndef FILTERTREE_H +#define FILTERTREE_H + +#include +#include +#include + +class FilterNode; +class FilterTree : public QAbstractItemModel +{ + Q_OBJECT + +public: + explicit FilterTree(QObject *parent = 0); + ~FilterTree(); + + 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; + QVariant data(const QModelIndex &index, int role) const override; + QVariant headerData(int section, Qt::Orientation orientation, int role) const override; + + FilterNode *addFilter(QList &data); + +private: + FilterNode *rootItem; +}; + +#endif // FILTERTREE_H diff --git a/src/blocker/subscriptionform.ui b/src/blocker/subscriptionform.ui index 703fb6d..0a20582 100644 --- a/src/blocker/subscriptionform.ui +++ b/src/blocker/subscriptionform.ui @@ -116,31 +116,7 @@ - - - 0 - - - - URL Blacklist - - - - - - - - - - URL Whitelist - - - - - - - - + -- cgit v1.2.1