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

This commit is contained in:
2016-09-28 12:24:52 +00:00
parent b6e96da478
commit 9e1a99b598
16 changed files with 249 additions and 31 deletions

View File

@@ -128,6 +128,12 @@ void CDCore::k_update(PIIODevice * d) {
k_ = uk;
}
void CDCore::k_calculate() {
k_.calculate();
}
void CDCore::reinitConnection(const PIString &configuration) {
PIString c = configuration;
connection.stop();
@@ -248,7 +254,7 @@ void CDCore::dtReceiveFinished(bool ok) {
K_Send();
break;
case CD_KSend: {
piCoutObj << "K received";
piCoutObj << "K received";
PIByteArray k;
ba >> k;
k << uchar(0);

View File

@@ -42,6 +42,7 @@ public:
void k_read(PIIODevice * d);
void k_parse(PIIODevice * d);
void k_update(PIIODevice * d);
void k_calculate();
PIString pultConfig() {return PIString(pult_config);}
PIString appConfig() {return PIString(app_config);}
void reinitConnection(const PIString & configuration);

View File

@@ -118,6 +118,11 @@ void KInterface::update(PIIODevice * d) {
}
void KInterface::calculate() {
core->k_calculate();
}
PIString KInterface::appConfig() {
return core->appConfig();
}

View File

@@ -44,6 +44,7 @@ public:
void read(PIIODevice * d);
void parse(PIIODevice * d);
void update(PIIODevice * d);
void calculate();
PIString appConfig();
PIString pultConfig();

View File

@@ -1,6 +1,8 @@
#include "cdutils_types.h"
#include "piconfig.h"
#include "pifile.h"
#include "pievaluator.h"
#include "cdutils_core.h"
using namespace CDUtils;
@@ -10,8 +12,9 @@ CDType::CDType() {
index_ = -1;
value_d = 0.;
value_i = 0;
value_b = false;
value_b = calculated = false;
cd_type_ = cdNull;
parent = 0;
// debug_cnt = cdtype_debug_cnt;
// cdtype_debug_cnt++;
// piCout << "[CDType]" << "create Null" << debug_cnt;
@@ -31,6 +34,8 @@ CDType::CDType(int i, const PIString & n, const PIString & t, const PIString & v
value_i = v.toInt();
value_b = v.toBool();
cd_type_ = cd_t;
calculated = false;
parent = 0;
if (type_ == "e") {
enum_values << comment_.inBrackets('{', '}').split(",");
piForeach(PIString &s, enum_values) s.trim();
@@ -58,20 +63,85 @@ PIString CDType::value() const {
}
void CDType::setFormula(const PIString &f) {
void CDType::setFormula(const PIString & f) {
formula_ = f;
calculate();
calculated = false;
//PIEvaluator e;
//calculate(&e);
}
void CDType::calculate() {
value_s = formula_.trimmed();
void CDType::setValue(const PIString & value_) {
formula_ = value_;
value_d = formula_.toDouble();
value_i = formula_.toInt();
value_b = formula_.toBool();
}
bool CDType::calculate(PIEvaluator * e, PIVector<const CDType * > stack) {
if (stack.contains(this)) {
error_ = "Circular dependencies: ";
piForeachC (CDType * k, stack)
error_ << k->name() << " -> ";
error_ << name();
//piCout << error_;
return false;
}
stack << this;
if (calculated) return true;
calculated = true;
error_.clear();
if (!parent) return true;
//piCout << "calc" << name_ << (parent ? parent->alias : "root");
value_s = formula_.trimmed();
for (;;) {
int ki = value_s.find("K[");
if (ki < 0) break;
int ke = value_s.find("]", ki + 2);
if (ke < 0) break;
PIString kp = value_s.mid(ki + 2, ke - ki - 2);
//piCout << kp;
CDType & k((*parent)[kp]);
k.calculate(e, stack);
value_s.replace(ki, ke - ki + 1, PIString::fromNumber(k.value_d));
}
value_d = formula_.toDouble();
value_i = formula_.toInt();
value_b = formula_.toBool();
double ev = 0.;
if (!e->check(value_s) && value_d == 0. && value_i == 0 && !value_b) {
PIString f = formula_.trimmed().toLowerCase();
if (f != "off" && f != "false" && f != "no" && !value_b) {
error_ = e->error();
return false;
}
} else
if (e->isCorrect())
ev = e->evaluate().real();
//piCout << value_s << value_i << value_d << ev;
//if ((value_d == 0.) || (piAbsd(value_d) < piAbsd(ev))) value_d = ev;
//if ((value_i == 0) || (piAbsd(value_i) < piAbsd(ev))) value_i = int(ev);
if ((value_d == 0.) || (ev != 0.)) value_d = ev;
if ((value_i == 0) || (ev != 0.)) value_i = int(ev);
value_b = value_b || (ev > 0.);
if (value_i != 0) {
if (value_d == 0.) value_d = value_i;
value_b = value_i > 0;
}
if (value_d != 0.) {
if (value_i == 0) value_i = value_d;
value_b = value_d > 0.;
}
if (value_b) {
if (value_d == 0.) value_d = 1.;
if (value_i == 0) value_i = 1;
}
value_s = PIString::fromNumber(value_d);
return true;
}
//CDType::CDType(const CDType &cdt) {
// index_ = cdt.index_;
// name_ = cdt.name_;
@@ -111,6 +181,56 @@ void CDType::calculate() {
//}
CDType & CDSection::operator [](const PIString & name_) {
PIStringList np = name_.split(".");
if (np.isEmpty()) return null;
//piCout << np;
CDSection * cs = this, * ns = 0;
if (np.front().isEmpty()) {
if (np.size_s() < 2) return null;
cs = &(CDCore::instance()->k_);
np.pop_front();
}
for (int i = 0; i < np.size_s() - 1; ++i) {
if (np[i].isEmpty()) return null;
bool isd = np[i][0].isDigit() || (np[i][0] == '-');
int dv = 0;
if (isd) dv = np[i].toInt();
ns = 0;
PIMap<int, CDSection>::iterator it;
//piCout << np[i] << isd << dv;
for (it = cs->s.begin(); it != cs->s.end(); ++it) {
bool f = false;
if (isd) f = (dv == it.key());
else f = (np[i] == it.value().alias);
//piCout << "s..." << it.key() << it.value().alias << f;
if (f) {
ns = &(it.value());
break;
}
}
//piCout << ns;
if (!ns) return null;
cs = ns;
}
PIMap<int, CDType>::iterator it;
if (np.back().isEmpty()) return null;
bool isd = np.back()[0].isDigit() || (np.back()[0] == '-');
int dv = 0;
if (isd) dv = np.back().toInt();
//piCout << np.back() << isd << dv;
for (it = cs->k.begin(); it != cs->k.end(); ++it) {
bool f = false;
if (isd) f = (dv == it.key());
else f = (np.back() == it.value().name());
//piCout << "k..." << it.key() << it.value().name() << f;
if (f)
return cs->k[it.key()];
}
return null;
}
int CDSection::count(bool recursive) const {
int ret = k.size_s();
if (recursive) {
@@ -136,6 +256,13 @@ PIStringList CDSection::index_names() const {
}
void CDSection::calculate() {
CDCore::instance()->k_.prepareCalculate();
PIEvaluator e;
calculateRecursive(&e);
}
void CDSection::write(PIIODevice * d, const PIString & prefix) {
if (!d) return;
if (k.isEmpty() && s.isEmpty()) return;
@@ -266,3 +393,25 @@ bool CDSection::isSameStructure(CDSection & v) {
}
void CDSection::prepareCalculate() {
PIMap<int, CDType>::iterator i;
for (i = k.begin(); i != k.end(); ++i) {
i.value().parent = this;
i.value().calculated = false;
}
PIMap<int, CDSection>::iterator j;
for (j = s.begin(); j != s.end(); ++j)
j.value().prepareCalculate();
}
void CDSection::calculateRecursive(PIEvaluator * e) {
PIMap<int, CDType>::iterator i;
for (i = k.begin(); i != k.end(); ++i)
i.value().calculate(e);
PIMap<int, CDSection>::iterator j;
for (j = s.begin(); j != s.end(); ++j)
j.value().calculateRecursive(e);
}

View File

@@ -5,6 +5,7 @@
#include "pimap.h"
class PIIODevice;
class PIEvaluator;
class CD_Pult;
class CDKItem;
class CDKItemModel;
@@ -30,19 +31,22 @@ public:
bool toBool() const {return value_b;}
cdT cd_type() const {return cd_type_;}
void setFormula(const PIString & formula);
void calculate();
void setValue(const PIString & value_);
operator double() const {return value_d;}
const PIStringList & enumValues() const {return enum_values;}
const PIString & errorString() const {return error_;}
protected:
bool calculate(PIEvaluator * e, PIVector<const CDType * > stack = PIVector<const CDType * >());
cdT cd_type_;
int index_;
PIString name_, type_;
PIString value_s, formula_, comment_;
PIString value_s, formula_, comment_, error_;
PIStringList enum_values;
CDSection * parent;
double value_d;
int value_i;
bool value_b;
bool value_b, calculated;
};
@@ -60,6 +64,8 @@ public:
// CDType & operator [](int v) {if (!k.contains(v)) k[v].index_ = v; return k[v];}
CDType & operator [](int v) {return k[v];}
const CDType operator [](int v) const {return k[v];}
CDType & operator [](const PIString & name_);
const CDType operator [](const PIString & name_) const {return (*this)[name_];}
CDSection & section(int v) {return s[v];}
const CDSection section(int v) const {return s[v];}
@@ -68,6 +74,7 @@ public:
int sectionsCount() const;
PIVector<int> indexes() const {return k.keys();}
PIStringList index_names() const;
void calculate();
PIString name;
PIString alias;
@@ -81,9 +88,12 @@ protected:
void read(const void * ep);
void update(CDSection & v, bool keep_names);
bool isSameStructure(CDSection & v);
void prepareCalculate();
void calculateRecursive(PIEvaluator * e);
PIMap<int, CDType> k;
PIMap<int, CDSection> s;
CDType null;
};

View File

@@ -128,7 +128,7 @@ void CDKItemModel::buildItem(CDKItem *it, CDSection & r) {
void CDKItemModel::internalRebuild() {
qDebug() << "[CDKItemModel]" << "internalRebuild()";
//qDebug() << "[CDKItemModel]" << "internalRebuild()";
if (root) delete root;
root = new CDKItem(0, CDKItem::ItemCDSection, 0);
CDSection & r = K.root();
@@ -162,7 +162,11 @@ CDKItem::~CDKItem() {
QVariant CDKItem::data(int column, int role) const {
if (role == Qt::BackgroundRole) {
switch (type_) {
case ItemCDType: return QBrush(QColor(255, 250, 230));
case ItemCDType: {
CDType & t = K.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));
}
}
@@ -171,6 +175,10 @@ QVariant CDKItem::data(int column, int role) const {
if (t.type() == "b") return t.toBool() ? Qt::Checked : Qt::Unchecked;
else QVariant();
}
if (role == Qt::ToolTipRole && type_ == ItemCDType) {
CDType & t = K.section(buildPath())[index_];
return PI2QString(t.errorString());
}
if (role != Qt::DisplayRole && role != Qt::EditRole) return QVariant();
PIDeque<int> path = buildPath();
CDSection & rs = K.section(path);
@@ -221,7 +229,7 @@ QVariant CDKItem::value(CDType t, int role) const {
bool CDKItem::setData(int column, const QVariant &value) {
if ((column == 3 || column == 4) && type_ == ItemCDType) {
K.section(buildPath())[index_].setFormula(Q2PIString(value.toString()));
K.section(buildPath())[index_].setValue(Q2PIString(value.toString()));
return true;
}
return false;

View File

@@ -87,6 +87,11 @@ void CDKView::buildFromHeader(const QString &kdescription) {
}
void CDKView::calculateK() {
K.calculate();
}
void CDKView::k_sendFailed() {
bisyStatusChanged(false);
emit messageStatus("send failed");

View File

@@ -26,6 +26,7 @@ public slots:
void loadK();
void clearK();
void buildFromHeader(const QString & kdescription);
void calculateK();
private slots:
void k_sendFailed();

View File

@@ -1,12 +1,14 @@
#include "form.h"
#include "ui_form.h"
#include "QFileDialog"
#include "cdutils_k.h"
using namespace CDUtils;
Form::Form(QWidget *parent) : QWidget(parent), ui(new Ui::Form) {
ui->setupUi(this);
ui->treeView->setKFile("");
ui->treeView->refresh();
ui->treeView->startPing();
}
@@ -31,4 +33,14 @@ void Form::on_pushButton_4_clicked() {
void Form::on_pushButton_6_clicked() {
ui->treeView->buildFromHeader(QFileDialog::getOpenFileName(this, trUtf8("Select *.h file with K description"), "k_description.h", "C/C++ header files(*.h *.hpp);;All files(*)"));
/*piCout << K.root()["Radar_WaveFreqTune"];
piCout << K.root()["11.NVA_PulseGenFineTune"];
piCout << K.root()[".NVA.NVA_PulseGenFineTune"];
piCout << K.root()["NVA_A.1.NVA_Clk"];*/
}
void Form::on_buttonCalc_clicked() {
ui->treeView->calculateK();
ui->treeView->refresh();
}

View File

@@ -17,10 +17,9 @@ public:
private slots:
void on_pushButton_3_clicked();
void on_pushButton_4_clicked();
void on_pushButton_6_clicked();
void on_buttonCalc_clicked();
private:
Ui::Form *ui;

View File

@@ -14,7 +14,7 @@
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="3" column="0" colspan="2">
<item row="4" column="0" colspan="2">
<widget class="CDKView" name="treeView"/>
</item>
<item row="0" column="0">
@@ -59,6 +59,13 @@
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QPushButton" name="buttonCalc">
<property name="text">
<string>Calculate</string>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>

BIN
qad_widgets/icons/f1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

View File

@@ -35,5 +35,6 @@
<file>icons/qcodeedit.png</file>
<file>icons/qvariantedit.png</file>
<file>icons/code-word.png</file>
<file>icons/f1.png</file>
</qresource>
</RCC>

View File

@@ -22,12 +22,22 @@ QCodeEdit::QCodeEdit(QWidget * parent): QWidget(parent) {
es_line.format.setBackground(QColor(240, 245, 240));
es_line.format.setProperty(QTextFormat::FullWidthSelection, true);
es_cursor.format.setBackground(QColor(220, 255, 200));
lbl_help = new IconedLabel();
lbl_help->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
lbl_help->setFocusPolicy(Qt::NoFocus);
lbl_help->setFrameShadow(QFrame::Sunken);
lbl_help->setFrameShape(QFrame::StyledPanel);
lbl_help->setDirection(IconedLabel::RightToLeft);
widget_help = new QFrame();
widget_help->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
widget_help->setFocusPolicy(Qt::NoFocus);
widget_help->setFrameShadow(QFrame::Sunken);
widget_help->setFrameShape(QFrame::StyledPanel);
widget_help->setLayout(new QBoxLayout(QBoxLayout::TopToBottom));
widget_help->layout()->setContentsMargins(0, 0, 0, 0);
for (int i = 0; i < 2; ++i) {
lbl_help[i] = new IconedLabel();
lbl_help[i]->setFrameShadow(QFrame::Plain);
lbl_help[i]->setFrameShape(QFrame::NoFrame);
lbl_help[i]->setDirection(IconedLabel::RightToLeft);
widget_help->layout()->addWidget(lbl_help[i]);
}
lbl_help[1]->setIcon(QIcon(":/icons/f1.png"));
lbl_help[1]->setText(trUtf8("Press F1 for details"));
completer = new QTreeWidget();
completer->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
completer->setFocusPolicy(Qt::NoFocus);
@@ -107,7 +117,9 @@ QCodeEdit::~QCodeEdit() {
delete textCode;
delete textLines;
delete completer;
delete lbl_help;
//for (int i = 0; i < 2; ++i)
// delete lbl_help[i];
delete widget_help;
}
@@ -706,8 +718,8 @@ void QCodeEdit::raiseHelp(QTextCursor tc, int arg) {
}
//qDebug() << s.second << st;
ACClass acc = ac_classes.value(i.first);
lbl_help->setIcon(acc.icon);
lbl_help->setText(QString("<html><body>[%1] %2 %3</html></body>").arg(acc.name, s.first, selectArg(s.second, arg)));
lbl_help[0]->setIcon(acc.icon);
lbl_help[0]->setText(QString("<html><body>[%1] %2 %3</html></body>").arg(acc.name, s.first, selectArg(s.second, arg)));
ok = true;
break;
}
@@ -720,10 +732,10 @@ void QCodeEdit::raiseHelp(QTextCursor tc, int arg) {
es_cursor.cursor = tc;
applyExtraSelection();
tc.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, st.size());
lbl_help->setFont(font());
lbl_help->resize(lbl_help->sizeHint());
lbl_help->move(textCode->mapToGlobal(textCode->cursorRect(tc).topLeft() - QPoint(0, lbl_help->height() + 8)));
lbl_help->show();
lbl_help[0]->setFont(font());
widget_help->resize(widget_help->sizeHint());
widget_help->move(textCode->mapToGlobal(textCode->cursorRect(tc).topLeft() - QPoint(0, widget_help->height() + 8)));
widget_help->show();
cursor_scope = scope.first;
cursor_scope << scope.second;
//qDebug() << "tooltip" << st;
@@ -731,7 +743,7 @@ void QCodeEdit::raiseHelp(QTextCursor tc, int arg) {
void QCodeEdit::hideHelp() {
lbl_help->hide();
widget_help->hide();
es_cursor.cursor = QTextCursor();
cursor_scope.clear();
applyExtraSelection();

View File

@@ -75,7 +75,8 @@ private:
QPlainTextEdit * textCode, * textLines;
QTreeWidget * completer;
IconedLabel * lbl_help;
IconedLabel * lbl_help[2];
QFrame * widget_help;
QTextEdit::ExtraSelection es_line, es_cursor;
QList<QTextEdit::ExtraSelection> es_selected, es_custom;
QMap<int, ACClass> ac_classes;