Have a working model for showing the namespaces in a tree with checkboxes.
The namespaces are currently spit into user and system. Later we might add recognizing namespaces introduced by specific modules/extensions.
This commit is contained in:
parent
4e1120647c
commit
b5254ac723
9 changed files with 414 additions and 5 deletions
171
pglab/NamespaceItemModel.cpp
Normal file
171
pglab/NamespaceItemModel.cpp
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
#include "NamespaceItemModel.h"
|
||||
#include "PgNamespace.h"
|
||||
#include "PgNamespaceContainer.h"
|
||||
#include "ScopeGuard.h"
|
||||
|
||||
NamespaceItemModel::NamespaceItemModel(QObject *parent)
|
||||
: QAbstractItemModel(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void NamespaceItemModel::init(std::shared_ptr<const PgNamespaceContainer> ns)
|
||||
{
|
||||
beginResetModel();
|
||||
SCOPE_EXIT { endResetModel(); };
|
||||
|
||||
auto system = std::make_shared<GroupNode>("System");
|
||||
auto user = std::make_shared<GroupNode>("User");
|
||||
groups = { system, user };
|
||||
|
||||
for (const auto e : *ns)
|
||||
if (e.isSystemCatalog())
|
||||
system->leaves.push_back(std::make_shared<LeafNode>(system, e));
|
||||
else
|
||||
user->leaves.push_back(std::make_shared<LeafNode>(user, e));
|
||||
|
||||
system->sortLeaves();
|
||||
user->sortLeaves();
|
||||
for (auto e : user->leaves)
|
||||
e->checked = true;
|
||||
}
|
||||
|
||||
QModelIndex NamespaceItemModel::index(int row, int column,
|
||||
const QModelIndex &parent) const
|
||||
{
|
||||
QModelIndex result;
|
||||
if (hasIndex(row, column, parent)) {
|
||||
|
||||
// We only have two levels, the groups and the namespaces
|
||||
if (parent.isValid()) { // namespace level
|
||||
auto grp = static_cast<GroupNode*>(parent.internalPointer());
|
||||
result = createIndex(row, column, grp->leaves[row].get());
|
||||
}
|
||||
else { // toplevel (groups)
|
||||
const auto grp = groups[row];
|
||||
|
||||
result = createIndex(row, column, grp.get());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
QModelIndex NamespaceItemModel::parent(const QModelIndex &index) const
|
||||
{
|
||||
QModelIndex result;
|
||||
if (index.isValid()) {
|
||||
auto *n = static_cast<Node*>(index.internalPointer());
|
||||
LeafNode *ln = dynamic_cast<LeafNode*>(n);
|
||||
if (ln) { // leftnode
|
||||
auto grp = ln->parent.lock(); // Get the parent group
|
||||
auto fr = std::find(groups.begin(), groups.end(), grp); // find it in the list
|
||||
int row = fr - groups.begin(); // calculate index ie row
|
||||
result = createIndex(row, 0, grp.get()); // return index
|
||||
}
|
||||
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int NamespaceItemModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
int count = 0;
|
||||
if (parent.isValid() && parent.column() <= 0) {
|
||||
auto node = static_cast<Node*>(parent.internalPointer());
|
||||
count = node->getRowCount();
|
||||
}
|
||||
else {
|
||||
count = groups.size();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int NamespaceItemModel::columnCount(const QModelIndex &parent) const
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
QVariant NamespaceItemModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
QVariant v;
|
||||
auto *n = static_cast<Node*>(index.internalPointer());
|
||||
LeafNode *leaf = dynamic_cast<LeafNode*>(n);
|
||||
if (role == Qt::DisplayRole) {
|
||||
if (leaf) {
|
||||
v = leaf->ns.name;
|
||||
}
|
||||
else {
|
||||
GroupNode *grp = dynamic_cast<GroupNode*>(n);
|
||||
if (grp) {
|
||||
v = grp->name;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(role == Qt::CheckStateRole) {
|
||||
//return checkedItems.contains(index) ?
|
||||
// Qt::Checked : Qt::Unchecked;
|
||||
if (n)
|
||||
v = n->getCheckState();
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
bool NamespaceItemModel::setData(const QModelIndex &index,
|
||||
const QVariant &value, int role)
|
||||
{
|
||||
if(!index.isValid() || role != Qt::CheckStateRole)
|
||||
return false;
|
||||
|
||||
auto *n = static_cast<Node*>(index.internalPointer());
|
||||
LeafNode *leaf = dynamic_cast<LeafNode*>(n);
|
||||
if (leaf) {
|
||||
leaf->checked = value == Qt::Checked;
|
||||
emit dataChanged(index, index);
|
||||
emit dataChanged(index.parent(), index.parent());
|
||||
}
|
||||
else {
|
||||
GroupNode *gn = dynamic_cast<GroupNode*>(n);
|
||||
if (gn) {
|
||||
if (value == Qt::Checked)
|
||||
for (auto l : gn->leaves)
|
||||
l->checked = true;
|
||||
if (value == Qt::Unchecked)
|
||||
for (auto l : gn->leaves)
|
||||
l->checked = false;
|
||||
emit dataChanged(index, index);
|
||||
emit dataChanged(this->index(0, 0, index), this->index(gn->leaves.size(), 0, index));
|
||||
}
|
||||
|
||||
}
|
||||
// if(value == Qt::Checked)
|
||||
// checkedItems.insert(index);
|
||||
// else
|
||||
// checkedItems.remove(index);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Qt::ItemFlags NamespaceItemModel::flags(const QModelIndex &index) const
|
||||
{
|
||||
Qt::ItemFlags flags = QAbstractItemModel::flags(index);
|
||||
auto *n = static_cast<Node*>(index.internalPointer());
|
||||
// LeafNode *leaf = dynamic_cast<LeafNode*>(n);
|
||||
// if (leaf) {
|
||||
flags.setFlag(Qt::ItemIsUserCheckable);
|
||||
// }
|
||||
// else {
|
||||
// flags.setFlag(Qt::ItemIsAutoTristate);
|
||||
// }
|
||||
return flags;
|
||||
}
|
||||
|
||||
//std::set<Oid> NamespaceItemModel::getCheckedNamespaces() const
|
||||
//{
|
||||
// std::set<Oid> result;
|
||||
// for (auto g : groups)
|
||||
// for (auto l : g->leaves)
|
||||
// if (l->checked)
|
||||
// result.insert(l->ns.oid);
|
||||
|
||||
// return result;
|
||||
//}
|
||||
Loading…
Add table
Add a link
Reference in a new issue