git-svn-id: svn://db.shs.com.ru/libs@117 a8b55f48-bf90-11e4-a774-851b48703e85
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <QDebug>
|
||||
#include <QBrush>
|
||||
#include <QColor>
|
||||
#include "qvariantedit.h"
|
||||
|
||||
using namespace CDUtils;
|
||||
|
||||
@@ -74,12 +75,29 @@ 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;
|
||||
if ((index.column() == 3 || index.column() == 4) && item->type_ == CDKItem::ItemCDType) f |= Qt::ItemIsEditable;
|
||||
if (item->type_ == CDKItem::ItemCDType && index.column() == 4) {
|
||||
CDType t = K.section(item->buildPath())[item->index_];
|
||||
if (t.type() == "b") {
|
||||
f |= Qt::ItemIsUserCheckable;
|
||||
// piCout << "ItemIsUserCheckable";
|
||||
}
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
bool CDKItemModel::setData(const QModelIndex &index, const QVariant &value, int role) {
|
||||
if (role == Qt::CheckStateRole && index.column() == 4) {
|
||||
CDKItem * item = getItem(index);
|
||||
if (item->type_ == CDKItem::ItemCDType) {
|
||||
CDType t = K.section(item->buildPath())[item->index_];
|
||||
if (t.type() == "b") {
|
||||
bool result = item->setData(index.column(), PI2QString(PIString::fromBool(value.toBool())));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (role != Qt::EditRole) return false;
|
||||
CDKItem * item = getItem(index);
|
||||
bool result = item->setData(index.column(), value);
|
||||
@@ -127,6 +145,8 @@ CDKItem * CDKItemModel::getItem(const QModelIndex &index) const {
|
||||
}
|
||||
|
||||
|
||||
// CDKItem
|
||||
|
||||
CDKItem::CDKItem(int index, CDKItem::CDKItemType type, CDKItem *parent) {
|
||||
index_ = index;
|
||||
parent_ = parent;
|
||||
@@ -143,9 +163,14 @@ QVariant CDKItem::data(int column, int role) const {
|
||||
if (role == Qt::BackgroundRole) {
|
||||
switch (type_) {
|
||||
case ItemCDType: return QBrush(QColor(255, 250, 230));
|
||||
case ItemCDSection: return QBrush(QColor(220, 220, 250));
|
||||
case ItemCDSection: return QBrush(QColor(230, 250, 230));
|
||||
}
|
||||
}
|
||||
if (role == Qt::CheckStateRole && type_ == ItemCDType && column == 4) {
|
||||
CDType & t = K.section(buildPath())[index_];
|
||||
if (t.type() == "b") return t.toBool() ? Qt::Checked : Qt::Unchecked;
|
||||
else QVariant();
|
||||
}
|
||||
if (role != Qt::DisplayRole && role != Qt::EditRole) return QVariant();
|
||||
PIDeque<int> path = buildPath();
|
||||
CDSection & rs = K.section(path);
|
||||
@@ -157,7 +182,7 @@ QVariant CDKItem::data(int column, int role) const {
|
||||
case 1: return PI2QString(rs[index_].name());
|
||||
case 2: return stringType(rs[index_].type());
|
||||
case 3: return PI2QString(rs[index_].formula());
|
||||
case 4: return PI2QString(rs[index_].value());
|
||||
case 4: return value(rs[index_], role);
|
||||
case 5: return PI2QString(rs[index_].comment());
|
||||
default:
|
||||
break;
|
||||
@@ -179,8 +204,23 @@ QVariant CDKItem::data(int column, int role) const {
|
||||
}
|
||||
|
||||
|
||||
QVariant CDKItem::value(CDType t, int role) const {
|
||||
if (t.type() == "f") return t.toDouble();
|
||||
if (t.type() == "n") return t.toInt();
|
||||
if (t.type() == "b") return t.toBool();
|
||||
if (t.type() == "e") {
|
||||
QVariantEdit::EnumType et;
|
||||
et.enum_list = PI2QStringList(t.enumValues());
|
||||
et.value = et.enum_list.at(t.toInt());//et.enum_list.indexOf("");
|
||||
if (role == Qt::EditRole)return QVariant::fromValue<QVariantEdit::EnumType>(et);
|
||||
else return et.value;
|
||||
}
|
||||
return PI2QString(t.value());
|
||||
}
|
||||
|
||||
|
||||
bool CDKItem::setData(int column, const QVariant &value) {
|
||||
if (column == 3 && type_ == ItemCDType) {
|
||||
if ((column == 3 || column == 4) && type_ == ItemCDType) {
|
||||
K.section(buildPath())[index_].setFormula(Q2PIString(value.toString()));
|
||||
return true;
|
||||
}
|
||||
@@ -221,3 +261,41 @@ QString CDKItem::stringType(const PIString & t) const {
|
||||
return QString("double");
|
||||
}
|
||||
|
||||
|
||||
// CDKDelegate
|
||||
|
||||
CDKDelegate::CDKDelegate(QObject *parent) : QStyledItemDelegate(parent) {
|
||||
}
|
||||
|
||||
|
||||
QWidget *CDKDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
|
||||
return new QVariantEdit(parent);
|
||||
}
|
||||
|
||||
|
||||
void CDKDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
|
||||
QVariantEdit *edit = static_cast<QVariantEdit*>(editor);
|
||||
edit->setValue(index.model()->data(index, Qt::EditRole));
|
||||
}
|
||||
|
||||
|
||||
void CDKDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
|
||||
QVariantEdit *edit = static_cast<QVariantEdit*>(editor);
|
||||
QVariant v = edit->value();
|
||||
if (v.canConvert<QVariantEdit::EnumType>()) {
|
||||
QVariantEdit::EnumType et = v.value<QVariantEdit::EnumType>();
|
||||
model->setData(index, et.enum_list.indexOf(et.value), Qt::EditRole);
|
||||
} else model->setData(index, v, Qt::EditRole);
|
||||
}
|
||||
|
||||
|
||||
void CDKDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const {
|
||||
editor->setGeometry(option.rect);
|
||||
}
|
||||
|
||||
|
||||
QSize CDKDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const {
|
||||
QSize s = QStyledItemDelegate::sizeHint(option, index);
|
||||
s.setWidth(s.width() + 20);
|
||||
return s;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user