git-svn-id: svn://db.shs.com.ru/libs@380 a8b55f48-bf90-11e4-a774-851b48703e85

This commit is contained in:
2018-05-22 13:49:16 +00:00
parent d3e81d5efa
commit 7a8dd9efba
28 changed files with 1052 additions and 137 deletions

View File

@@ -1,22 +1,23 @@
#include "qcd_model.h"
#include "cdutils_interface.h"
#include "cdutils_core.h"
#include "cdutils_x.h"
#include "piqt.h"
#include <QDebug>
#include <QBrush>
#include <QColor>
#include <QMimeData>
#include "qvariantedit.h"
#include "qad_types.h"
using namespace CDUtils;
// CDKItem
CDItem::CDItem(CDUtils::Interface * i, int index, CDItem::CDItemType type, CDItem *parent) {
CDItem::CDItem(CDUtils::Interface * i, int _index, CDItem::CDItemType type, CDItem *parent) {
interface = i;
index_ = index;
index_ = _index;
parent_ = parent;
type_ = type;
}
@@ -31,20 +32,25 @@ QVariant CDItem::data(int column, int role) const {
if (role == Qt::BackgroundRole) {
switch (type_) {
case ItemCDType: {
CDType & t = interface->section(buildPath())[index_];
CDType & t(interface->section(buildPath())[index_]);
if (t.errorString().isEmpty()) return QBrush(QColor(255, 250, 230));
else return QBrush(QColor(255, 128, 128));
}
case ItemCDSection: return QBrush(QColor(230, 250, 230));
}
}
if (role == Qt::CheckStateRole && type_ == ItemCDType && column == 4) {
CDType & t = interface->section(buildPath())[index_];
if (t.type() == "b") return t.toBool() ? Qt::Checked : Qt::Unchecked;
else QVariant();
if (role == Qt::CheckStateRole && type_ == ItemCDType) {
CDType & t(interface->section(buildPath())[index_]);
if (column == cValue && t.cd_type() == CDType::cdK) {
if (t.type() == "b") return t.toBool() ? Qt::Checked : Qt::Unchecked;
else QVariant();
}
if (column == cName_Cmd && t.cd_type() == CDType::cdX) {
return t.isSelectedX() ? Qt::Checked : Qt::Unchecked;
}
}
if (role == Qt::ToolTipRole && type_ == ItemCDType) {
CDType & t = interface->section(buildPath())[index_];
CDType & t(interface->section(buildPath())[index_]);
return PI2QString(t.errorString());
}
if (role != Qt::DisplayRole && role != Qt::EditRole) return QVariant();
@@ -54,12 +60,14 @@ QVariant CDItem::data(int column, int role) const {
switch (type_) {
case ItemCDType:
switch (column) {
case 0: return QString::number(index_);
case 1: return PI2QString(rs[index_].name());
case 2: return stringType(rs[index_].type());
case 3: return PI2QString(rs[index_].formula());
case 4: return value(rs[index_], role);
case 5: return PI2QString(rs[index_].comment());
case cID: return QString::number(index_);
case cName_Cmd: return PI2QString(rs[index_].name());
case cType: return stringType(rs[index_].type());
case cXMode: return QVariant::fromValue(xModeEnum(rs[index_].xmode()));
case cXAvg: return rs[index_].avg();
case cExpression: return PI2QString(rs[index_].formula());
case cValue: return value(rs[index_], role);
case cComment: return PI2QString(rs[index_].comment());
default: break;
}
break;
@@ -67,9 +75,9 @@ QVariant CDItem::data(int column, int role) const {
s = rs.section(index_);
// piCout << rs.name << rs.alias << s.name << s.alias;
switch (column) {
case 0: return QString("[") + QString::number(index_) + QString("]");
case 1: return PI2QString(s.alias);
case 2: return PI2QString(s.name);
case cID: return QString("[") + QString::number(index_) + QString("]");
case cName_Cmd: return PI2QString(s.alias);
case cType: return PI2QString(s.name);
default: break;
}
break;
@@ -92,11 +100,28 @@ QVariant CDItem::value(CDType t, int role) const {
}
bool CDItem::setData(int column, const QVariant &value) {
if ((column == 3 || column == 4) && type_ == ItemCDType) {
interface->section(buildPath())[index_].setValue(Q2PIString(value.toString()));
interface->calculate();
return true;
bool CDItem::setData(int column, const QVariant & value) {
if (type_ == ItemCDType) {
CDType & t(interface->section(buildPath())[index_]);
if ((column == cExpression || column == cValue) && (t.cd_type() == CDType::cdK)) {
interface->section(buildPath())[index_].setValue(Q2PIString(value.toString()));
interface->calculate();
return true;
}
if (t.cd_type() == CDType::cdX) {
switch (column) {
case cName_Cmd:
X.setEnabled(t, value.toBool());
return true;
case cXMode:
t.setXMode((CDType::XMode)value.toInt());
return true;
case cXAvg:
t.setAvg(piMax(value.toInt(), 1));
return true;
default: break;
}
}
}
return false;
}
@@ -136,13 +161,51 @@ QString CDItem::stringType(const PIString & t) const {
}
QAD::Enum CDItem::xModeEnum(int v) const {
QAD::Enum ret;
ret << QAD::Enumerator(CDType::X_Current, "Current")
<< QAD::Enumerator(CDType::X_All_Avg, "All, Averaging");
ret.selectValue(v);
return ret;
}
// CDKDelegate
CDDelegate::CDDelegate(QObject *parent) : QStyledItemDelegate(parent) {
}
QWidget *CDDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
void CDDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const {
CDItem * item = ((CDItemModel*)index.model())->getItem(index);
if (item) {
if (item->itemType() == CDItem::ItemCDType && item->interface->cdType() == CDType::cdC) {
QStyleOptionButton bo;
bo.direction = option.direction;
bo.fontMetrics = option.fontMetrics;
bo.palette = option.palette;
bo.rect = option.rect;
bo.state = option.state;// & ~(QStyle::State_HasFocus | QStyle::State_MouseOver);
bo.text = item->data(1, Qt::DisplayRole).toString();
QWidget * v = (QWidget*)(painter->device());
if (v) {
QPoint cp = v->mapFromGlobal(QCursor::pos());
if (bo.rect.contains(cp, true)) {
//bo.state |= QStyle::State_MouseOver;
if (qApp->mouseButtons().testFlag(Qt::LeftButton))
bo.state |= QStyle::State_On;
}
}
qApp->style()->drawControl(QStyle::CE_PushButton, &bo, painter);
return;
}
}
QStyledItemDelegate::paint(painter, option, index);
}
QWidget * CDDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
return new QVariantEdit(parent);
}
@@ -192,7 +255,7 @@ CDItemModel::~CDItemModel() {
QVariant CDItemModel::data(const QModelIndex &index, int role) const {
if (!index.isValid()) return QVariant();
CDItem *item = getItem(index);
CDItem * item = getItem(index);
return item->data(index.column(), role);
}
@@ -200,12 +263,14 @@ QVariant CDItemModel::data(const QModelIndex &index, int role) const {
QVariant CDItemModel::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");
case cID: return trUtf8("Index");
case cName_Cmd: return interface->cdType() == CDType::cdC ? trUtf8("Command") : trUtf8("Name");
case cType: return trUtf8("Type");
case cXMode: return trUtf8("Mode");
case cXAvg: return trUtf8("Averaging");
case cExpression: return trUtf8("Expression");
case cValue: return trUtf8("Value");
case cComment: return trUtf8("Comment");
}
}
return QVariant();
@@ -213,7 +278,7 @@ QVariant CDItemModel::headerData(int section, Qt::Orientation orientation, int r
QModelIndex CDItemModel::index(int row, int column, const QModelIndex &parent) const {
if (parent.isValid() && parent.column() != 0) return QModelIndex();
if (parent.isValid() && parent.column() != cID) return QModelIndex();
CDItem * p = getItem(parent);
CDItem * c = p->childs.value(row, 0);
if (c) return createIndex(row, column, c);
@@ -226,18 +291,18 @@ QModelIndex CDItemModel::parent(const QModelIndex &index) const {
CDItem * c = getItem(index);
CDItem * p = c->parent_;
if (p == root) return QModelIndex();
return createIndex(p->parent_->childs.indexOf(p), 0, p);
return createIndex(p->parent_->childs.indexOf(p), cID, p);
}
int CDItemModel::rowCount(const QModelIndex &parent) const {
CDItem *p = getItem(parent);
CDItem * p = getItem(parent);
return p->childs.count();
}
int CDItemModel::columnCount(const QModelIndex &parent) const {
return 6;
return cLastColumn;
}
@@ -246,30 +311,42 @@ Qt::ItemFlags CDItemModel::flags(const QModelIndex & index) const {
Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
CDItem * item = getItem(index);
if (!item) return 0;
CDType & t(interface->section(item->buildPath())[item->index_]);
if ((index.column() == 3 || index.column() == 4) &&
(item->type_ == CDItem::ItemCDType) &&
(t.cd_type() == CDType::cdK))
f |= Qt::ItemIsEditable;
if (item->type_ == CDItem::ItemCDType && index.column() == 4) {
if (t.type() == "b") {
f |= Qt::ItemIsUserCheckable;
// piCout << "ItemIsUserCheckable";
if (item->type_ == CDItem::ItemCDType) {
CDType & t(interface->section(item->buildPath())[item->index_]);
if (t.cd_type() == CDType::cdK) {
if (index.column() == cExpression || index.column() == cValue)
f |= Qt::ItemIsEditable;
if (index.column() == cValue && t.type() == "b")
f |= Qt::ItemIsUserCheckable;
}
if (t.cd_type() == CDType::cdX) {
if (index.column() == cXMode || index.column() == cXAvg)
f |= Qt::ItemIsEditable;
if (index.column() == cName_Cmd)
f |= Qt::ItemIsUserCheckable | Qt::ItemIsDragEnabled;
}
}
return f;
}
bool CDItemModel::setData(const QModelIndex & index, const QVariant &value, int role) {
if (role == Qt::CheckStateRole && index.column() == 4) {
bool CDItemModel::setData(const QModelIndex & index, const QVariant & value, int role) {
if (role == Qt::CheckStateRole && (index.column() == cName_Cmd || index.column() == cValue)) {
CDItem * item = getItem(index);
if (item->type_ == CDItem::ItemCDType) {
CDType t = interface->section(item->buildPath())[item->index_];
if (t.type() == "b") {
bool result = item->setData(index.column(), PI2QString(PIString::fromBool(value.toBool())));
QModelIndex rin(CDItemModel::index(index.row(), 3, index.parent()));
emit dataChanged(rin, rin);
CDType & t(interface->section(item->buildPath())[item->index_]);
if (index.column() == cValue && (t.cd_type() == CDType::cdK)) {
if (t.type() == "b") {
bool result = item->setData(index.column(), PI2QString(PIString::fromBool(value.toBool())));
QModelIndex rin(CDItemModel::index(index.row(), cExpression, index.parent()));
emit dataChanged(rin, rin);
return result;
}
}
if (index.column() == cName_Cmd && (t.cd_type() == CDType::cdX)) {
bool result = item->setData(index.column(), value);
//QModelIndex rin(CDItemModel::index(index.row(), 1, index.parent()));
//emit dataChanged(rin, rin);
return result;
}
}
@@ -278,7 +355,7 @@ bool CDItemModel::setData(const QModelIndex & index, const QVariant &value, int
CDItem * item = getItem(index);
bool result = item->setData(index.column(), value);
if (result) {
QModelIndex rin(CDItemModel::index(index.row(), 3, index.parent()));
QModelIndex rin(CDItemModel::index(index.row(), cExpression, index.parent()));
emit dataChanged(rin, rin);
emit dataChanged(index, index);
}
@@ -286,6 +363,23 @@ bool CDItemModel::setData(const QModelIndex & index, const QVariant &value, int
}
QMimeData * CDItemModel::mimeData(const QModelIndexList & indexes) const {
if (indexes.size() == 1) {
QModelIndex index = indexes[0];
if (index.isValid() && interface->cdType() == CDType::cdX) {
CDItem * item = getItem(index);
if (item) {
CDType & t(interface->section(item->buildPath())[item->index_]);
QMimeData * mime = new QMimeData();
mime->setText(PI2QString(CDCore::pathToString(t.path())));
return mime;
}
}
}
return QAbstractItemModel::mimeData(indexes);
}
void CDItemModel::rebuildModel() {
beginResetModel();
internalRebuild();