UI: Rules: Extract PolicyListBox

This commit is contained in:
Nodir Temirkhodjaev 2022-03-11 15:16:59 +03:00
parent f531ee54c3
commit f107029fc2
11 changed files with 203 additions and 147 deletions

View File

@ -20,6 +20,7 @@ SOURCES += \
conf/confmanager.cpp \
conf/firewallconf.cpp \
conf/inioptions.cpp \
conf/policy.cpp \
control/control.cpp \
control/controlmanager.cpp \
control/controlworker.cpp \
@ -55,6 +56,7 @@ SOURCES += \
form/opt/pages/optbasepage.cpp \
form/opt/pages/optionspage.cpp \
form/opt/pages/optmainpage.cpp \
form/opt/pages/rules/policylistbox.cpp \
form/opt/pages/rulespage.cpp \
form/opt/pages/schedulepage.cpp \
form/opt/pages/statisticspage.cpp \
@ -175,6 +177,8 @@ HEADERS += \
conf/confmanager.h \
conf/firewallconf.h \
conf/inioptions.h \
conf/policy.h \
conf/policylist.h \
control/control.h \
control/controlmanager.h \
control/controlworker.h \
@ -210,6 +214,7 @@ HEADERS += \
form/opt/pages/optbasepage.h \
form/opt/pages/optionspage.h \
form/opt/pages/optmainpage.h \
form/opt/pages/rules/policylistbox.h \
form/opt/pages/rulespage.h \
form/opt/pages/schedulepage.h \
form/opt/pages/statisticspage.h \

1
src/ui/conf/policy.cpp Normal file
View File

@ -0,0 +1 @@
#include "policy.h"

17
src/ui/conf/policy.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef POLICY_H
#define POLICY_H
#include <QObject>
class Policy
{
public:
bool isPreset = false;
bool enabled = true;
int policyId = 0;
QString name;
};
#endif // POLICY_H

14
src/ui/conf/policylist.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef POLICYLIST_H
#define POLICYLIST_H
enum PolicyListType {
PolicyListInvalid = -1,
PolicyListNone = 0,
PolicyListPresetLibrary,
PolicyListPresetApp,
PolicyListGlobalBeforeApp,
PolicyListGlobalAfterApp,
PolicyListCount
};
#endif // POLICYLIST_H

View File

@ -56,6 +56,7 @@ QToolButton *ControlUtil::createToolButton(
const QString &iconPath, const std::function<void()> &onClicked)
{
auto c = new QToolButton();
c->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
c->setIcon(IconCache::icon(iconPath));
c->connect(c, &QToolButton::clicked, onClicked);
@ -63,6 +64,16 @@ QToolButton *ControlUtil::createToolButton(
return c;
}
QToolButton *ControlUtil::createFlatToolButton(
const QString &iconPath, const std::function<void()> &onClicked)
{
auto c = createToolButton(iconPath, onClicked);
c->setToolButtonStyle(Qt::ToolButtonIconOnly);
c->setAutoRaise(true);
c->setFocusPolicy(Qt::NoFocus);
return c;
}
QPushButton *ControlUtil::createLinkButton(
const QString &iconPath, const QString &linkPath, const QString &toolTip)
{

View File

@ -28,6 +28,8 @@ public:
const QString &iconPath, const std::function<void()> &onClicked);
static QToolButton *createToolButton(
const QString &iconPath, const std::function<void()> &onClicked);
static QToolButton *createFlatToolButton(
const QString &iconPath, const std::function<void()> &onClicked);
static QPushButton *createLinkButton(const QString &iconPath,
const QString &linkPath = QString(), const QString &toolTip = QString());
static QPushButton *createFlatButton(

View File

@ -0,0 +1,75 @@
#include "policylistbox.h"
#include <QHBoxLayout>
#include <QHeaderView>
#include <QLabel>
#include <QToolButton>
#include <QVBoxLayout>
#include <form/controls/controlutil.h>
#include <form/controls/tableview.h>
#include <model/policylistmodel.h>
PolicyListBox::PolicyListBox(PolicyListType type, QWidget *parent) :
QWidget(parent), m_listModel(new PolicyListModel(type, this))
{
setupUi();
listModel()->initialize();
}
void PolicyListBox::onRetranslateUi()
{
m_btAddPolicy->setToolTip(tr("Add Policy"));
m_btRemovePolicy->setToolTip(tr("Remove Policy"));
m_btEditPolicy->setToolTip(tr("Edit Policy"));
}
void PolicyListBox::setupUi()
{
auto layout = new QVBoxLayout();
layout->setContentsMargins(0, 0, 0, 0);
// Header
auto headerLayout = setupHeader();
layout->addLayout(headerLayout);
// Table View
setupTableView();
layout->addWidget(m_tableView);
this->setLayout(layout);
}
QLayout *PolicyListBox::setupHeader()
{
m_label = ControlUtil::createLabel();
m_label->setFont(ControlUtil::fontDemiBold());
m_btAddPolicy = ControlUtil::createFlatToolButton(":/icons/add.png", [&] { emit addPolicy(); });
m_btRemovePolicy =
ControlUtil::createFlatToolButton(":/icons/delete.png", [&] { emit removePolicy(); });
m_btEditPolicy =
ControlUtil::createFlatToolButton(":/icons/pencil.png", [&] { emit editPolicy(); });
auto layout = new QHBoxLayout();
layout->setSpacing(2);
layout->addWidget(m_label, 1);
layout->addWidget(m_btAddPolicy);
layout->addWidget(m_btRemovePolicy);
layout->addWidget(m_btEditPolicy);
return layout;
}
void PolicyListBox::setupTableView()
{
m_tableView = new TableView();
m_tableView->setAlternatingRowColors(true);
m_tableView->setSelectionMode(QAbstractItemView::SingleSelection);
m_tableView->setSelectionBehavior(QAbstractItemView::SelectItems);
m_tableView->horizontalHeader()->setVisible(false);
m_tableView->setModel(listModel());
}

View File

@ -0,0 +1,49 @@
#ifndef POLICYLISTBOX_H
#define POLICYLISTBOX_H
#include <QWidget>
#include <conf/policylist.h>
QT_FORWARD_DECLARE_CLASS(QLabel)
QT_FORWARD_DECLARE_CLASS(QToolButton)
class PolicyListModel;
class TableView;
class PolicyListBox : public QWidget
{
Q_OBJECT
public:
explicit PolicyListBox(PolicyListType type, QWidget *parent = nullptr);
PolicyListModel *listModel() const { return m_listModel; }
QLabel *label() const { return m_label; }
TableView *tableView() const { return m_tableView; }
signals:
void addPolicy();
void removePolicy();
void editPolicy();
public slots:
void onRetranslateUi();
private:
void setupUi();
QLayout *setupHeader();
void setupTableView();
private:
PolicyListModel *m_listModel = nullptr;
QLabel *m_label = nullptr;
QToolButton *m_btAddPolicy = nullptr;
QToolButton *m_btRemovePolicy = nullptr;
QToolButton *m_btEditPolicy = nullptr;
TableView *m_tableView = nullptr;
};
#endif // POLICYLISTBOX_H

View File

@ -1,8 +1,8 @@
#include "rulespage.h"
#include <QGroupBox>
#include <QHBoxLayout>
#include <QHeaderView>
#include <QLabel>
#include <QSplitter>
#include <QVBoxLayout>
@ -10,19 +10,11 @@
#include <model/policylistmodel.h>
#include <user/iniuser.h>
RulesPage::RulesPage(OptionsController *ctrl, QWidget *parent) :
OptBasePage(ctrl, parent),
m_presetLibListModel(new PolicyListModel(PolicyListModel::PolicyListPresetLibrary, this)),
m_presetAppListModel(new PolicyListModel(PolicyListModel::PolicyListPresetApp, this)),
m_globalPreListModel(new PolicyListModel(PolicyListModel::PolicyListGlobalBeforeApp, this)),
m_globalPostListModel(new PolicyListModel(PolicyListModel::PolicyListGlobalAfterApp, this))
#include "rules/policylistbox.h"
RulesPage::RulesPage(OptionsController *ctrl, QWidget *parent) : OptBasePage(ctrl, parent)
{
setupUi();
presetAppListModel()->initialize();
presetLibListModel()->initialize();
globalPreListModel()->initialize();
globalPostListModel()->initialize();
}
void RulesPage::onSaveWindowState(IniUser *ini)
@ -41,10 +33,15 @@ void RulesPage::onRestoreWindowState(IniUser *ini)
void RulesPage::onRetranslateUi()
{
m_gbPresetLib->setTitle(tr("Library of preset rules:"));
m_gbPresetApp->setTitle(tr("Preset rules for applications:"));
m_gbGlobalPre->setTitle(tr("Global rules, applied before application rules:"));
m_gbGlobalPost->setTitle(tr("Global rules, applied after application rules:"));
m_presetLibBox->label()->setText(tr("Library of preset rules:"));
m_presetAppBox->label()->setText(tr("Preset rules for applications:"));
m_globalPreBox->label()->setText(tr("Global rules, applied before application rules:"));
m_globalPostBox->label()->setText(tr("Global rules, applied after application rules:"));
m_presetLibBox->onRetranslateUi();
m_presetAppBox->onRetranslateUi();
m_globalPreBox->onRetranslateUi();
m_globalPostBox->onRetranslateUi();
}
void RulesPage::setupUi()
@ -80,60 +77,18 @@ void RulesPage::setupPresetSplitter()
m_presetSplitter = new QSplitter();
m_presetSplitter->setHandleWidth(10);
m_presetSplitter->setOrientation(Qt::Vertical);
m_presetSplitter->addWidget(m_gbPresetLib);
m_presetSplitter->addWidget(m_gbPresetApp);
m_presetSplitter->addWidget(m_presetLibBox);
m_presetSplitter->addWidget(m_presetAppBox);
}
void RulesPage::setupPresetLibBox()
{
setupPresetLibView();
auto layout = new QVBoxLayout();
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(m_presetLibListView);
m_gbPresetLib = new QGroupBox(this);
m_gbPresetLib->setFlat(true);
m_gbPresetLib->setLayout(layout);
}
void RulesPage::setupPresetLibView()
{
m_presetLibListView = new TableView();
m_presetLibListView->setAlternatingRowColors(true);
m_presetLibListView->setSelectionMode(QAbstractItemView::SingleSelection);
m_presetLibListView->setSelectionBehavior(QAbstractItemView::SelectItems);
m_presetLibListView->horizontalHeader()->setVisible(false);
m_presetLibListView->setModel(presetLibListModel());
// m_presetLibListView->setMenu(m_btEdit->menu());
m_presetLibBox = new PolicyListBox(PolicyListPresetLibrary);
}
void RulesPage::setupPresetAppBox()
{
setupPresetAppView();
auto layout = new QVBoxLayout();
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(m_presetAppListView);
m_gbPresetApp = new QGroupBox(this);
m_gbPresetApp->setFlat(true);
m_gbPresetApp->setLayout(layout);
}
void RulesPage::setupPresetAppView()
{
m_presetAppListView = new TableView();
m_presetAppListView->setAlternatingRowColors(true);
m_presetAppListView->setSelectionMode(QAbstractItemView::SingleSelection);
m_presetAppListView->setSelectionBehavior(QAbstractItemView::SelectItems);
m_presetAppListView->horizontalHeader()->setVisible(false);
m_presetAppListView->setModel(presetAppListModel());
m_presetAppBox = new PolicyListBox(PolicyListPresetApp);
}
void RulesPage::setupGlobalSplitter()
@ -148,56 +103,16 @@ void RulesPage::setupGlobalSplitter()
m_globalSplitter = new QSplitter();
m_globalSplitter->setHandleWidth(10);
m_globalSplitter->setOrientation(Qt::Vertical);
m_globalSplitter->addWidget(m_gbGlobalPre);
m_globalSplitter->addWidget(m_gbGlobalPost);
m_globalSplitter->addWidget(m_globalPreBox);
m_globalSplitter->addWidget(m_globalPostBox);
}
void RulesPage::setupGlobalPreBox()
{
setupGlobalPreView();
auto layout = new QVBoxLayout();
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(m_globalPreListView);
m_gbGlobalPre = new QGroupBox(this);
m_gbGlobalPre->setFlat(true);
m_gbGlobalPre->setLayout(layout);
}
void RulesPage::setupGlobalPreView()
{
m_globalPreListView = new TableView();
m_globalPreListView->setAlternatingRowColors(true);
m_globalPreListView->setSelectionMode(QAbstractItemView::SingleSelection);
m_globalPreListView->setSelectionBehavior(QAbstractItemView::SelectItems);
m_globalPreListView->horizontalHeader()->setVisible(false);
m_globalPreListView->setModel(globalPreListModel());
m_globalPreBox = new PolicyListBox(PolicyListGlobalBeforeApp);
}
void RulesPage::setupGlobalPostBox()
{
setupGlobalPostView();
auto layout = new QVBoxLayout();
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(m_globalPostListView);
m_gbGlobalPost = new QGroupBox(this);
m_gbGlobalPost->setFlat(true);
m_gbGlobalPost->setLayout(layout);
}
void RulesPage::setupGlobalPostView()
{
m_globalPostListView = new TableView();
m_globalPostListView->setAlternatingRowColors(true);
m_globalPostListView->setSelectionMode(QAbstractItemView::SingleSelection);
m_globalPostListView->setSelectionBehavior(QAbstractItemView::SelectItems);
m_globalPostListView->horizontalHeader()->setVisible(false);
m_globalPostListView->setModel(globalPostListModel());
m_globalPostBox = new PolicyListBox(PolicyListGlobalAfterApp);
}

View File

@ -3,8 +3,7 @@
#include "optbasepage.h"
class PolicyListModel;
class TableView;
class PolicyListBox;
class RulesPage : public OptBasePage
{
@ -13,11 +12,6 @@ class RulesPage : public OptBasePage
public:
explicit RulesPage(OptionsController *ctrl = nullptr, QWidget *parent = nullptr);
PolicyListModel *presetLibListModel() const { return m_presetLibListModel; }
PolicyListModel *presetAppListModel() const { return m_presetAppListModel; }
PolicyListModel *globalPreListModel() const { return m_globalPreListModel; }
PolicyListModel *globalPostListModel() const { return m_globalPostListModel; }
protected slots:
void onSaveWindowState(IniUser *ini) override;
void onRestoreWindowState(IniUser *ini) override;
@ -28,32 +22,19 @@ private:
void setupUi();
void setupPresetSplitter();
void setupPresetLibBox();
void setupPresetLibView();
void setupPresetAppBox();
void setupPresetAppView();
void setupGlobalSplitter();
void setupGlobalPreBox();
void setupGlobalPreView();
void setupGlobalPostBox();
void setupGlobalPostView();
private:
PolicyListModel *m_presetLibListModel = nullptr;
PolicyListModel *m_presetAppListModel = nullptr;
PolicyListModel *m_globalPreListModel = nullptr;
PolicyListModel *m_globalPostListModel = nullptr;
QGroupBox *m_gbPresetLib = nullptr;
QGroupBox *m_gbPresetApp = nullptr;
QGroupBox *m_gbGlobalPre = nullptr;
QGroupBox *m_gbGlobalPost = nullptr;
PolicyListBox *m_presetLibBox = nullptr;
PolicyListBox *m_presetAppBox = nullptr;
PolicyListBox *m_globalPreBox = nullptr;
PolicyListBox *m_globalPostBox = nullptr;
QSplitter *m_splitter = nullptr;
QSplitter *m_presetSplitter = nullptr;
QSplitter *m_globalSplitter = nullptr;
TableView *m_presetLibListView = nullptr;
TableView *m_presetAppListView = nullptr;
TableView *m_globalPreListView = nullptr;
TableView *m_globalPostListView = nullptr;
};
#endif // RULESPAGE_H

View File

@ -4,19 +4,15 @@
#include <QObject>
#include <QVector>
#include <conf/policy.h>
#include <conf/policylist.h>
#include <util/model/tablesqlmodel.h>
class ConfManager;
class SqliteDb;
struct PolicyRow : TableRow
struct PolicyRow : TableRow, public Policy
{
bool isPreset = false;
bool enabled = true;
int policyId = 0;
QString name;
};
class PolicyListModel : public TableSqlModel
@ -24,16 +20,6 @@ class PolicyListModel : public TableSqlModel
Q_OBJECT
public:
enum PolicyListType {
PolicyListInvalid = -1,
PolicyListNone = 0,
PolicyListPresetLibrary,
PolicyListPresetApp,
PolicyListGlobalBeforeApp,
PolicyListGlobalAfterApp,
PolicyListCount
};
explicit PolicyListModel(PolicyListType type, QObject *parent = nullptr);
PolicyListType type() const { return m_type; }