193 lines
4.9 KiB
C++
193 lines
4.9 KiB
C++
#include "cd_kmodel.h"
|
|
#include "cdutils_k.h"
|
|
#include "piqt.h"
|
|
#include <QDebug>
|
|
|
|
using namespace CDUtils;
|
|
|
|
|
|
CDKItemModel::CDKItemModel(QObject *parent) : QAbstractItemModel(parent) {
|
|
root = 0;
|
|
internalRebuild();
|
|
}
|
|
|
|
|
|
CDKItemModel::~CDKItemModel() {
|
|
delete root;
|
|
}
|
|
|
|
|
|
QVariant CDKItemModel::data(const QModelIndex &index, int role) const {
|
|
if (!index.isValid()) return QVariant();
|
|
if (role != Qt::DisplayRole && role != Qt::EditRole) return QVariant();
|
|
CDKItem *item = getItem(index);
|
|
return item->data(index.column());
|
|
}
|
|
|
|
|
|
QVariant CDKItemModel::headerData(int section, Qt::Orientation orientation, int role) const {
|
|
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
|
|
switch (section) {
|
|
case 0: return trUtf8("Index");
|
|
case 1: return trUtf8("Name");
|
|
case 2: return trUtf8("Type");
|
|
case 3: return trUtf8("Expression");
|
|
case 4: return trUtf8("Value");
|
|
case 5: return trUtf8("Comment");
|
|
}
|
|
}
|
|
return QVariant();
|
|
}
|
|
|
|
|
|
QModelIndex CDKItemModel::index(int row, int column, const QModelIndex &parent) const {
|
|
if (parent.isValid() && parent.column() != 0) return QModelIndex();
|
|
CDKItem * p = getItem(parent);
|
|
CDKItem * c = p->childs.value(row, 0);
|
|
if (c) return createIndex(row, column, c);
|
|
else return QModelIndex();
|
|
}
|
|
|
|
|
|
QModelIndex CDKItemModel::parent(const QModelIndex &index) const {
|
|
if (!index.isValid()) return QModelIndex();
|
|
CDKItem * c = getItem(index);
|
|
CDKItem * p = c->parent_;
|
|
if (p == root) return QModelIndex();
|
|
return createIndex(p->parent_->childs.indexOf(p), 0, p);
|
|
}
|
|
|
|
|
|
int CDKItemModel::rowCount(const QModelIndex &parent) const {
|
|
CDKItem *p = getItem(parent);
|
|
return p->childs.count();
|
|
}
|
|
|
|
|
|
int CDKItemModel::columnCount(const QModelIndex &parent) const {
|
|
return 6;
|
|
}
|
|
|
|
|
|
Qt::ItemFlags CDKItemModel::flags(const QModelIndex &index) const {
|
|
if (!index.isValid()) return 0;
|
|
Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
|
|
CDKItem * item = getItem(index);
|
|
if (index.column() == 3 && item->type_ == CDKItem::ItemCDType) f |= Qt::ItemIsEditable;
|
|
return f;
|
|
}
|
|
|
|
|
|
bool CDKItemModel::setData(const QModelIndex &index, const QVariant &value, int role) {
|
|
if (role != Qt::EditRole) return false;
|
|
CDKItem * item = getItem(index);
|
|
bool result = item->setData(index.column(), value);
|
|
if (result) emit dataChanged(index, index);
|
|
return result;
|
|
}
|
|
|
|
|
|
void CDKItemModel::rebuildModel() {
|
|
beginResetModel();
|
|
internalRebuild();
|
|
endResetModel();
|
|
}
|
|
|
|
|
|
void CDKItemModel::buildItem(CDKItem *it, CDSection r) {
|
|
//piCout << "build item" << r.name << r.alias;
|
|
PIMap<int, CDType>::iterator i;
|
|
for (i = r.k.begin(); i != r.k.end(); ++i) {
|
|
it->childs << new CDKItem(i.key(), CDKItem::ItemCDType, it);
|
|
}
|
|
PIMap<int, CDSection>::iterator j;
|
|
for (j = r.s.begin(); j != r.s.end(); ++j) {
|
|
it->childs << new CDKItem(j.key(), CDKItem::ItemCDSection, it);
|
|
buildItem(it->childs.back(), j.value());
|
|
}
|
|
}
|
|
|
|
|
|
void CDKItemModel::internalRebuild() {
|
|
qDebug() << "[CDKItemModel]" << "internalRebuild()";
|
|
if (root) delete root;
|
|
root = new CDKItem(0, CDKItem::ItemCDSection, 0);
|
|
CDSection r = K.root();
|
|
buildItem(root, r);
|
|
}
|
|
|
|
|
|
CDKItem * CDKItemModel::getItem(const QModelIndex &index) const {
|
|
if (index.isValid()) {
|
|
CDKItem * item = static_cast<CDKItem*>(index.internalPointer());
|
|
if (item) return item;
|
|
}
|
|
return root;
|
|
}
|
|
|
|
|
|
CDKItem::CDKItem(int index, CDKItem::CDKItemType type, CDKItem *parent) {
|
|
index_ = index;
|
|
parent_ = parent;
|
|
type_ = type;
|
|
}
|
|
|
|
|
|
CDKItem::~CDKItem() {
|
|
qDeleteAll(childs);
|
|
}
|
|
|
|
|
|
QVariant CDKItem::data(int column) const {
|
|
CDSection rs = K.section(buildPath());
|
|
//piCout << rs.name << rs.alias << rs.count(false) << rs.sectionsCount() << K.root().name << K.root().alias <<K.root().count(false) << K.root().sectionsCount();
|
|
switch (type_) {
|
|
case ItemCDType:
|
|
switch (column) {
|
|
case 0: return QString::number(index_);
|
|
case 1: return PI2QString(rs[index_].name());
|
|
case 2: return PI2QString(rs[index_].type());
|
|
case 3: return PI2QString(rs[index_].formula());
|
|
case 4: return PI2QString(rs[index_].value());
|
|
case 5: return PI2QString(rs[index_].comment());
|
|
default:
|
|
break;
|
|
}
|
|
break;
|
|
case ItemCDSection:
|
|
rs = rs.section(index_);
|
|
switch (column) {
|
|
case 0: return QString("[") + QString::number(index_) + QString("]");
|
|
case 1: return PI2QString(rs.alias);
|
|
case 2: return PI2QString(rs.name);
|
|
default:
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
return QVariant();
|
|
}
|
|
|
|
|
|
bool CDKItem::setData(int column, const QVariant &value) {
|
|
if (column == 3 && type_ == ItemCDType) {
|
|
//K.section(buildPath())[index_].formula_ = Q2PIString(value.toString());
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
PIDeque<int> CDKItem::buildPath() const {
|
|
PIDeque<int> path;
|
|
CDKItem * p = parent_;
|
|
while (p) {
|
|
path.push_front(p->index_);
|
|
p = p->parent_;
|
|
}
|
|
path.take_front();
|
|
//piCout << path;
|
|
return path;
|
|
}
|
|
|