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

This commit is contained in:
2019-09-02 14:08:38 +00:00
parent f862381b68
commit 3d06d2095e
929 changed files with 66799 additions and 0 deletions

View File

@@ -0,0 +1 @@
qad_project(utils "Gui;Widgets" "")

View File

@@ -0,0 +1,38 @@
#include "chunkstream.h"
void ChunkStream::setSource(const QByteArray & data) {
stream_.setVersion(QDataStream::Qt_4_8);
data_ = const_cast<QByteArray*>(&data);
_init();
}
void ChunkStream::setSource(QByteArray * data) {
stream_.setVersion(QDataStream::Qt_4_8);
data_ = (data ? data : &tmp_data);
_init();
}
void ChunkStream::setSource(QDataStream & str) {
stream_.setVersion(QDataStream::Qt_4_8);
str >> tmp_data;
data_ = &tmp_data;
_init();
}
ChunkStream::~ChunkStream() {
}
void ChunkStream::_init() {
last_id = -1;
last_data.clear();
buffer.close();
buffer.setBuffer(data_);
buffer.open(QIODevice::ReadWrite);
stream_.setDevice(&buffer);
}

View File

@@ -0,0 +1,60 @@
#ifndef CHUNKSTREAM_H
#define CHUNKSTREAM_H
#include <QDataStream>
#include <QBuffer>
#include <QByteArray>
class ChunkStream
{
public:
ChunkStream(const QByteArray & data) {setSource(data);}
ChunkStream(QDataStream & str) {setSource(str);}
ChunkStream(QByteArray * data = 0) {setSource(data);}
~ChunkStream();
template <typename T>
struct Chunk {
Chunk(int i, const T & d): id(i), data(d) {}
int id;
T data;
};
template <typename T> static Chunk<T> chunk(int id, const T & data) {return Chunk<T>(id, data);}
template <typename T> ChunkStream & add(int id, const T & data) {*this << Chunk<T>(id, data); return *this;}
void setSource(const QByteArray & data);
void setSource(QDataStream & str);
void setSource(QByteArray * data);
QDataStream & dataStream() {return stream_;}
QByteArray data() const {return tmp_data;}
bool atEnd() const {return stream_.atEnd();}
int read() {stream_ >> last_id >> last_data; return last_id;}
int getID() {return last_id;}
template <typename T>
T getData() const {T ret; QDataStream s(last_data); s.setVersion(QDataStream::Qt_4_8); s >> ret; return ret;}
template <typename T>
void get(T & v) const {v = getData<T>();}
private:
void _init();
int last_id;
QByteArray * data_, last_data, tmp_data;
QBuffer buffer;
QDataStream stream_;
};
template <typename T>
ChunkStream & operator <<(ChunkStream & s, const ChunkStream::Chunk<T> & c) {
QByteArray ba;
QDataStream bas(&ba, QIODevice::WriteOnly);
bas.setVersion(QDataStream::Qt_4_8);
bas << c.data;
s.dataStream() << c.id << ba;
return s;
}
#endif // CHUNKSTREAM_H

View File

@@ -0,0 +1 @@
qad_plugin(utils "Gui" "")

View File

@@ -0,0 +1,16 @@
//#include "qpiconfigplugin.h"
#include "qad_utils.h"
QADUtils::QADUtils(QObject * parent): QObject(parent) {
//m_widgets.append(new QPIConfigPlugin(this));
}
QList<QDesignerCustomWidgetInterface * > QADUtils::customWidgets() const {
return m_widgets;
}
#if QT_VERSION < 0x050000
Q_EXPORT_PLUGIN2(qad_utils_plugin, QADUtils)
#endif

View File

@@ -0,0 +1,24 @@
#ifndef QAD_UTILS_H
#define QAD_UTILS_H
#include <QtDesigner/QtDesigner>
#include <QtCore/qplugin.h>
class QADUtils: public QObject, public QDesignerCustomWidgetCollectionInterface
{
Q_OBJECT
//Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QDesignerCustomWidgetInterface")
#if QT_VERSION >= 0x050000
Q_PLUGIN_METADATA(IID "qad.utils")
#endif
Q_INTERFACES(QDesignerCustomWidgetCollectionInterface)
public:
explicit QADUtils(QObject * parent = 0);
virtual QList<QDesignerCustomWidgetInterface * > customWidgets() const;
private:
QList<QDesignerCustomWidgetInterface * > m_widgets;
};
#endif // QAD_UTILS_H

View File

@@ -0,0 +1,165 @@
#include <QColor>
#include "qad_types.h"
bool PropertyStorage::isPropertyExists(const QString & _name) const {
for (int i = 0; i < props.size(); ++i)
if (props[i].name == _name)
return true;
return false;
}
void PropertyStorage::addProperty(const PropertyStorage::Property & p) {
for (int i = 0; i < props.size(); ++i)
if (props[i].name == p.name) {
props[i] = p;
return;
}
props << p;
}
void PropertyStorage::removeProperty(const QString & _name) {
for (int i = 0; i < props.size(); ++i)
if (props[i].name == _name) {
props.removeAt(i);
return;
}
}
void PropertyStorage::removePropertiesByFlag(int flag) {
for (int i = 0; i < props.size(); ++i)
if ((props[i].flags & flag) == flag) {
props.removeAt(i);
--i;
}
}
void PropertyStorage::updateProperties(const QList<PropertyStorage::Property> & properties_, int flag_ignore) {
QVariantMap values;
foreach (const PropertyStorage::Property & p, props)
if (((p.flags & flag_ignore) != flag_ignore) || (flag_ignore == 0))
values[p.name] = p.value;
props = properties_;
for (int i = 0; i < props.size(); ++i) {
PropertyStorage::Property & p(props[i]);
if (values.contains(p.name)) {
QVariant pv = values[p.name];
if (pv.userType() == p.value.userType())
p.value = pv;
}
}
}
PropertyStorage::Property PropertyStorage::propertyByName(const QString & name) const {
foreach (const Property & p, props)
if (p.name == name)
return p;
return Property();
}
QVariant PropertyStorage::propertyValueByName(const QString & name) const {
foreach (const Property & p, props)
if (p.name == name)
return p.value;
return QVariant();
}
void PropertyStorage::setPropertyValue(const QString & name, const QVariant & value) {
for (int i = 0; i < props.size(); ++i)
if (props[i].name == name) {
props[i].value = value;
return;
}
}
void PropertyStorage::setPropertyComment(const QString & name, const QString & comment) {
for (int i = 0; i < props.size(); ++i)
if (props[i].name == name) {
props[i].comment = comment;
return;
}
}
void PropertyStorage::setPropertyFlags(const QString & name, int flags) {
for (int i = 0; i < props.size(); ++i)
if (props[i].name == name) {
props[i].flags = flags;
return;
}
}
PropertyStorage::Property PropertyStorage::parsePropertyLine(QString l) {
PropertyStorage::Property ret;
QString pn, pc, pt("s"), pv;
if (l.contains('#')) {
int i = l.indexOf('#');
pn = l.left(i).trimmed();
pc = l.right(l.length() - i - 1).trimmed();
} else {
if (l.contains('(')) {
int bs = l.indexOf('('), be = l.indexOf(')');
if (be > 0) {
pc = l.mid(bs + 1, be - bs - 1).trimmed();
l.remove(bs, be - bs + 1);
} else {
pc = l.right(l.length() - bs - 1).trimmed();
l = l.left(bs);
}
}
pn = l.trimmed();
}
if (!pc.isEmpty()) {
pt = pc.left(1);
pc = pc.remove(0, 1).trimmed();
}
if (pn.contains('=')) {
int i = pn.indexOf('=');
pv = pn.right(pn.length() - i - 1).trimmed();
pn.truncate(i);
pn = pn.trimmed();
}
ret.name = pn;
ret.comment = pc;
ret.value = QVariant(typeFromLetter(pt));
if (!pv.isEmpty()) {
//qDebug() << "set value !" << pv;
switch (ret.value.type()) {
case QVariant::Bool: pv = pv.toLower(); ret.value = (pv == "on" || pv == "true" || pv == "enable" || pv == "enabled" || pv.toInt() > 0 ? true : false); break;
case QVariant::Int: ret.value = pv.toInt(); break;
case QVariant::UInt: ret.value = pv.toUInt(); break;
case QVariant::LongLong: ret.value = pv.toLongLong(); break;
case QVariant::ULongLong: ret.value = pv.toULongLong(); break;
case QVariant::Double: ret.value = pv.toDouble(); break;
case QVariant::Color: ret.value = QColor(pv); break;
default: ret.value = pv; break;
};
}
return ret;
}
PropertyStorage::Property & PropertyStorage::operator[](const QString & name) {
for (int i = 0; i < props.size(); ++i)
if (props[i].name == name)
return props[i];
addProperty(name, "");
return props.back();
}
const PropertyStorage::Property PropertyStorage::operator[](const QString & name) const {
for (int i = 0; i < props.size(); ++i)
if (props[i].name == name)
return props[i];
return Property();
}

View File

@@ -0,0 +1,120 @@
#ifndef PROPERTYSTORAGE_H
#define PROPERTYSTORAGE_H
#include <QPointF>
#include <QRectF>
#include <QLineF>
#include <QVariant>
#include <QStringList>
#include <QDebug>
#include "chunkstream.h"
class PropertyStorage {
public:
PropertyStorage() {}
struct Property {
Property(const QString & n = QString(), const QString & c = QString(), const QVariant & v = QVariant(), int f = 0):
name(n), comment(c), value(v), flags(f) {}
bool toBool() const {return value.toBool();}
int toInt() const {return value.toInt();}
float toFloat() const {return value.toFloat();}
double toDouble() const {return value.toDouble();}
QString toString() const {return value.toString();}
QString name;
QString comment;
QVariant value;
int flags;
};
PropertyStorage(const QList<Property> & pl) {props = pl;}
typedef QList<Property>::const_iterator const_iterator;
typedef QList<Property>::iterator iterator;
iterator begin() {return props.begin();}
const_iterator begin() const {return props.begin();}
const_iterator constBegin() const {return props.constBegin();}
iterator end() {return props.end();}
const_iterator end() const {return props.end();}
const_iterator constEnd() const {return props.constEnd();}
int count() const {return props.count();}
int length() const {return props.length();}
int size() const {return props.size();}
bool isEmpty() const {return props.isEmpty();}
Property & first() {return props.first();}
const Property & first() const {return props.first();}
Property & front() {return props.front();}
const Property & front() const {return props.front();}
Property & last() {return props.last();}
const Property & last() const {return props.last();}
Property & back() {return props.back();}
const Property & back() const {return props.back();}
void removeFirst() {props.removeFirst();}
void removeLast() {props.removeLast();}
void removeAt(int i) {props.removeAt(i);}
Property value(int i) const {return props.value(i);}
Property value(int i, const Property & defaultValue) const {return props.value(i, defaultValue);}
void clear() {props.clear();}
PropertyStorage copy() const {return PropertyStorage(*this);}
int propertiesCount() const {return props.size();}
QList<Property> & properties() {return props;}
const QList<Property> & properties() const {return props;}
const PropertyStorage & propertyStorage() const {return *this;}
bool isPropertyExists(const QString & _name) const;
void clearProperties() {props.clear();}
void addProperty(const Property & p);
void addProperty(const QString & _name, const QVariant & _def_value, const QString & _comment = QString(), int _flags = 0) {addProperty(Property(_name, _comment, _def_value, _flags));}
void removeProperty(const QString & _name);
void removePropertiesByFlag(int flag);
void updateProperties(const QList<Property> & properties_, int flag_ignore = 0);
Property propertyByName(const QString & name) const;
QVariant propertyValueByName(const QString & name) const;
void setPropertyValue(const QString & name, const QVariant & value);
void setPropertyComment(const QString & name, const QString & comment);
void setPropertyFlags(const QString & name, int flags);
PropertyStorage & operator <<(const PropertyStorage::Property & p) {props << p; return *this;}
PropertyStorage & operator <<(const QList<Property> & p) {props << p; return *this;}
PropertyStorage & operator <<(const PropertyStorage & p) {props << p.props; return *this;}
Property & operator[](int i) {return props[i];}
const Property & operator[](int i) const {return props[i];}
Property & operator[](const QString & name);
const Property operator[](const QString & name) const;
static Property parsePropertyLine(QString l);
protected:
QList<Property> props;
};
inline QDebug operator <<(QDebug s, const PropertyStorage::Property & p) {s.nospace() << p.name << " (0x" << QString::number(p.flags, 16) << ") = " << p.value; return s.space();}
inline QDataStream & operator <<(QDataStream & s, const PropertyStorage & p) {s << p.properties(); return s;}
inline QDataStream & operator >>(QDataStream & s, PropertyStorage & p) {s >> p.properties(); return s;}
inline QDataStream & operator <<(QDataStream & s, const PropertyStorage::Property & p) {
ChunkStream cs;
cs << cs.chunk(1, p.name) << cs.chunk(2, p.comment) << cs.chunk(3, p.value) << cs.chunk(4, p.flags);
s << cs.data();
return s;
}
inline QDataStream & operator >>(QDataStream & s, PropertyStorage::Property & p) {
ChunkStream cs(s);
while (!cs.atEnd()) {
switch (cs.read()) {
case 1: cs.get(p.name); break;
case 2: cs.get(p.comment); break;
case 3: cs.get(p.value); break;
case 4: cs.get(p.flags); break;
}
}
return s;
}
#endif // PROPERTYSTORAGE_H

View File

@@ -0,0 +1,275 @@
#include "qad_types.h"
#include <QApplication>
#include <QFontMetrics>
#include <QMetaEnum>
#include <QWidget>
#if QT_VERSION >= 0x050000
# include <QWindow>
# include <QScreen>
#endif
bool __QADTypesRegistrator__::_inited = false;
__QADTypesRegistrator__::__QADTypesRegistrator__(int) {
instance()->_inited = true;
}
__QADTypesRegistrator__ *__QADTypesRegistrator__::instance() {
static __QADTypesRegistrator__ ret;
return &ret;
}
__QADTypesRegistrator__::__QADTypesRegistrator__() {
if (_inited) return;
_inited = true;
qRegisterMetaType<QAD::Enumerator>("QAD::Enumerator");
qRegisterMetaTypeStreamOperators<QAD::Enumerator>("QAD::Enumerator");
qRegisterMetaType<QAD::Enum>("QAD::Enum");
qRegisterMetaTypeStreamOperators<QAD::Enum>("QAD::Enum");
qRegisterMetaType<QAD::File>("QAD::File");
qRegisterMetaTypeStreamOperators<QAD::File>("QAD::File");
qRegisterMetaType<QAD::Dir>("QAD::Dir");
qRegisterMetaTypeStreamOperators<QAD::Dir>("QAD::Dir");
qRegisterMetaType<QAD::IODevice>("QAD::IODevice");
qRegisterMetaTypeStreamOperators<QAD::IODevice>("QAD::IODevice");
qRegisterMetaType<QAD::MathVector>("QAD::MathVector");
qRegisterMetaTypeStreamOperators<QAD::MathVector>("QAD::MathVector");
qRegisterMetaType<QAD::MathMatrix>("QAD::MathMatrix");
qRegisterMetaTypeStreamOperators<QAD::MathMatrix>("QAD::MathMatrix");
#if QT_VERSION >= 0x050200
QMetaType::registerConverter<QAD::Enum, int>(&QAD::Enum::selectedValue);
QMetaType::registerConverter<QAD::Enum, QString>(&QAD::Enum::selectedName);
QMetaType::registerConverter<QAD::File, QString>(&QAD::File::toString);
QMetaType::registerConverter<QAD::Dir, QString>(&QAD::Dir::toString);
QMetaType::registerConverter<QAD::IODevice, QString>(&QAD::IODevice::toString);
#endif
}
__QADTypesRegistrator__ __registrator__(1);
QAD::Enum::Enum(const QMetaEnum & meta, int selected) {
enum_name = meta.name();
for (int i = 0; i < meta.keyCount(); ++i) {
enum_list << QAD::Enumerator(meta.value(i), meta.key(i));
}
selectValue(selected);
}
int QAD::Enum::selectedValue() const {
foreach (const Enumerator & e, enum_list)
if (e.name == selected)
return e.value;
return 0;
}
bool QAD::Enum::selectValue(int v) {
foreach (const Enumerator & e, enum_list)
if (e.value == v) {
selected = e.name;
return true;
}
return false;
}
bool QAD::Enum::selectName(const QString & n) {
foreach (const Enumerator & e, enum_list)
if (e.name == n) {
selected = e.name;
return true;
}
return false;
}
int QAD::Enum::value(const QString & n) const {
foreach (const Enumerator & e, enum_list)
if (e.name == n)
return e.value;
return 0;
}
QString QAD::Enum::name(int v) const {
foreach (const Enumerator & e, enum_list)
if (e.value == v)
return e.name;
return QString();
}
QList<int> QAD::Enum::values() const {
QList<int> ret;
foreach (const Enumerator & e, enum_list)
ret << e.value;
return ret;
}
QStringList QAD::Enum::names() const {
QStringList ret;
foreach (const Enumerator & e, enum_list)
ret << e.name;
return ret;
}
QAD::Enum & QAD::Enum::operator <<(const QAD::Enumerator & v) {
enum_list << v;
return *this;
}
QAD::Enum & QAD::Enum::operator <<(const QString & v) {
enum_list << Enumerator(enum_list.size(), v);
return *this;
}
QAD::Enum & QAD::Enum::operator <<(const QStringList & v) {
foreach (const QString & s, v)
(*this) << s;
return *this;
}
QString QAD::IODevice::toString() const {
QString s;
if (__QADTypesRegistrator__::instance()->toString_funcs.contains(qMetaTypeId<QAD::IODevice>())) {
QVariant v;
v.setValue(*this);
(*(__QADTypesRegistrator__::instance()->toString_funcs[qMetaTypeId<QAD::IODevice>()]))(v, s);
return s;
} else {
// s += "IODevice(" + prefix + ", mode=";
// int rwc = 0;
// if (mode & QIODevice::ReadOnly) {s += "r"; ++rwc;}
// if (mode & QIODevice::WriteOnly) {s += "w"; ++rwc;}
// if (rwc == 1) s += "o";
// if (options != 0) {
// s += ", flags=";
// if (options & 1)
// s += "br";
// if (options & 2)
// if (options & 1)
// s+= "|";
// s += "bw";
// }
// PropertyStorage ps = props;
// foreach (const PropertyStorage::Property & p, ps) {
// QString vs = p.value.toString();
// if (p.value.type() == QVariant::StringList)
// vs = p.value.toStringList().join(";");
// s += ", " + p.name + "=\"" + vs + "\"";
// }
// s += ")";
return s;
}
}
QVariant::Type typeFromLetter(const QString & l) {
if (l.isEmpty()) return QVariant::String;
QString ft = l.left(1);
if (ft == "l") return QVariant::StringList;
if (ft == "b") return QVariant::Bool;
if (ft == "n") return QVariant::Int;
if (ft == "f") return QVariant::Double;
if (ft == "c") return QVariant::Color;
if (ft == "r") return QVariant::Rect;
if (ft == "a") return QVariant::RectF;
if (ft == "p") return QVariant::Point;
if (ft == "v") return QVariant::PointF;
if (ft == "e") return (QVariant::Type)qMetaTypeId<QAD::Enum>();
if (ft == "F") return (QVariant::Type)qMetaTypeId<QAD::File>();
if (ft == "D") return (QVariant::Type)qMetaTypeId<QAD::Dir>();
if (ft == "d") return (QVariant::Type)qMetaTypeId<QAD::IODevice>();
if (ft == "V") return (QVariant::Type)qMetaTypeId<QAD::MathVector>();
if (ft == "M") return (QVariant::Type)qMetaTypeId<QAD::MathMatrix>();
return QVariant::String;
}
QString uniqueName(QString n, const QStringList & names) {
if (!names.contains(n))
return n;
QString num;
while (!n.isEmpty()) {
if (n.right(1)[0].isDigit()) {
num.push_front(n.right(1));
n.chop(1);
} else break;
}
if (!n.endsWith('_')) n += '_';
int in = num.toInt() + 1;
QString nn = n + QString::number(in).rightJustified(3, '0');
while (names.contains(nn))
nn = n + QString::number(++in).rightJustified(3, '0');
return nn;
}
int fontHeight(const QWidget * w) {
#ifdef Q_OS_ANDROID
static int ret = QApplication::fontMetrics().size(0, "0").height();
return ret;
#else
# if QT_VERSION >= 0x050000
//qDebug() << "fontHeight" << w;
if (w) {
QWidget * pw = w->window();
if (pw) {
/*QWindow * wnd = pw->windowHandle();
//qDebug() << "wnd" << wnd;
if (wnd) {
QScreen * s = wnd->screen();
qDebug() << "s" << s;
if (s) {
qDebug() << "scales:";
qDebug() << QApplication::fontMetrics().size(0, "0").height() << QApplication::fontMetrics().xHeight();
qDebug() << s->logicalDotsPerInch() << s->logicalDotsPerInch()/96.*QApplication::font().pointSizeF();
}
}*/
return QFontMetrics(QApplication::font(), pw).size(0, "0").height();
}
}
# endif
#endif
return QApplication::fontMetrics().size(0, "0").height();
}
int lineThickness(const QWidget * w) {
return qMax<int>(qRound(fontHeight(w) / 15.), 1);
}
QSize preferredIconSize(float x, const QWidget * w) {
int s = qMax<int>(8, qRound(fontHeight(w) * x));
#ifdef Q_OS_MACOS
s /= 1.25;
#endif
return QSize(s, s);
}
double appScale(const QWidget * w) {
return qMax<double>(fontHeight(w) / 15., 1.);
}

148
test/qad/utils/qad_types.h Normal file
View File

@@ -0,0 +1,148 @@
#ifndef QAD_TYPES_H
#define QAD_TYPES_H
#include "propertystorage.h"
class QMetaEnum;
namespace QAD {
struct Enumerator {
Enumerator(int v = 0, const QString & n = QString()): value(v), name(n) {}
int value;
QString name;
};
struct Enum {
Enum(const QString & n = QString()): enum_name(n) {}
Enum(const QMetaEnum & meta, int selected = 0);
QString toString() const {return selected;} // obsolete, use selectedName()
int selectedValue() const;
QString selectedName() const {return selected;}
bool selectValue(int v);
bool selectName(const QString & n);
int value(const QString & n) const;
QString name(int v) const;
QList<int> values() const;
QStringList names() const;
int size() const {return enum_list.size();}
QString enum_name;
QString selected;
QList<Enumerator> enum_list;
Enum & operator <<(const Enumerator & v);
Enum & operator <<(const QString & v);
Enum & operator <<(const QStringList & v);
};
struct File {
File(const QString & p = QString(), const QString & f = QString(), bool abs = false): file(p), filter(f), is_abs(abs) {}
QString toString() const {return file;}
QString file;
QString filter;
bool is_abs;
};
struct Dir {
Dir(const QString & d = QString(), bool abs = false): dir(d), is_abs(abs) {}
QString toString() const {return dir;}
QString dir;
bool is_abs;
};
struct IODevice {
IODevice(const QString & device_prefix = QString(), const PropertyStorage & device_properties = PropertyStorage(),
int open_mode = QIODevice::ReadWrite, int device_options = 0)
: prefix(device_prefix), mode(open_mode), options(device_options), props(device_properties) {}
QString toString() const;
bool isValid() const {return !prefix.isEmpty();}
QString prefix;
int mode;
int options;
PropertyStorage props;
};
struct MathVector {
MathVector(const QVector<double> & vec = QVector<double>()) {v = vec;}
QVector<double> v;
};
struct MathMatrix {
MathMatrix(const QVector<QVector<double> > & mat = QVector<QVector<double> > ()) {m = mat;}
QVector<QVector<double> > m; // [Row][Column]
};
}
Q_DECLARE_METATYPE(QAD::Enumerator)
inline QDataStream & operator <<(QDataStream & s, const QAD::Enumerator & v) {s << v.value << v.name; return s;}
inline QDataStream & operator >>(QDataStream & s, QAD::Enumerator & v) {s >> v.value >> v.name; return s;}
inline QDebug operator <<(QDebug s, const QAD::Enumerator & v) {s.nospace() << v.name << "(" << v.value << ")"; return s.space();}
Q_DECLARE_METATYPE(QAD::Enum)
inline QDataStream & operator <<(QDataStream & s, const QAD::Enum & v) {s << v.enum_name << v.selected << v.enum_list; return s;}
inline QDataStream & operator >>(QDataStream & s, QAD::Enum & v) {s >> v.enum_name >> v.selected >> v.enum_list; return s;}
inline QDebug operator <<(QDebug s, const QAD::Enum & v) {s.nospace() << v.selected; return s.space();}
Q_DECLARE_METATYPE(QAD::File)
inline QDataStream & operator <<(QDataStream & s, const QAD::File & v) {s << v.file << v.filter << v.is_abs; return s;}
inline QDataStream & operator >>(QDataStream & s, QAD::File & v) {s >> v.file >> v.filter >> v.is_abs; return s;}
inline QDebug operator <<(QDebug s, const QAD::File & v) {s.nospace() << v.file; return s.space();}
Q_DECLARE_METATYPE(QAD::Dir)
inline QDataStream & operator <<(QDataStream & s, const QAD::Dir & v) {s << v.dir << v.is_abs; return s;}
inline QDataStream & operator >>(QDataStream & s, QAD::Dir & v) {s >> v.dir >> v.is_abs; return s;}
inline QDebug operator <<(QDebug s, const QAD::Dir & v) {s.nospace() << v.dir; return s.space();}
Q_DECLARE_METATYPE(QAD::IODevice)
inline QDataStream & operator <<(QDataStream & s, const QAD::IODevice & v) {s << v.prefix << v.mode << v.options << v.props; return s;}
inline QDataStream & operator >>(QDataStream & s, QAD::IODevice & v) {s >> v.prefix >> v.mode >> v.options >> v.props; return s;}
inline QDebug operator <<(QDebug s, const QAD::IODevice & v) {s.nospace() << v.toString(); return s.space();}
Q_DECLARE_METATYPE(QAD::MathVector)
inline QDataStream & operator <<(QDataStream & s, const QAD::MathVector & v) {s << v.v; return s;}
inline QDataStream & operator >>(QDataStream & s, QAD::MathVector & v) {s >> v.v; return s;}
inline QDebug operator <<(QDebug s, const QAD::MathVector & v) {s.nospace() << "Vector " << v.v; return s.space();}
Q_DECLARE_METATYPE(QAD::MathMatrix)
inline QDataStream & operator <<(QDataStream & s, const QAD::MathMatrix & v) {s << v.m; return s;}
inline QDataStream & operator >>(QDataStream & s, QAD::MathMatrix & v) {s >> v.m; return s;}
inline QDebug operator <<(QDebug s, const QAD::MathMatrix & v) {s.nospace() << "Matrix " << v.m; return s.space();}
class __QADTypesRegistrator__ {
public:
__QADTypesRegistrator__(int);
static __QADTypesRegistrator__ * instance();
QMap<int, void(*)(const QVariant &, QString &)> toString_funcs;
private:
__QADTypesRegistrator__();
static bool _inited;
};
inline qreal quantize(qreal x, qreal q = 10.f) {return qRound(x / q) * q;}
inline QPointF quantize(QPointF x, qreal q = 10.f) {return QPointF(quantize(x.x(), q), quantize(x.y(), q));}
inline qreal distPointToLine(const QPointF & lp0, const QPointF & lp1, const QPointF & p) {
QLineF a(lp0, lp1), b(lp0, p), c(lp1, p);
qreal f = qAbs(a.dx()*b.dy() - a.dy()*b.dx()) / a.length(), s = b.length() + c.length() - a.length();
return qMax(f, s);
}
inline QPointF nearestPointOnLine(const QPointF & lp0, const QPointF & lp1, const QPointF & p) {
QLineF a(lp0, lp1), b(lp0, p);
return a.pointAt(b.length() / a.length());
}
inline QRectF enlargedRect(const QRectF & r, qreal dx, qreal dy, qreal v) {
return QRectF(r.left() - v + dx, r.top() - v + dy, r.width() + v+v, r.height() + v+v);
}
QVariant::Type typeFromLetter(const QString & l);
QString uniqueName(QString n, const QStringList & names);
int fontHeight(const QWidget * w = 0);
int lineThickness(const QWidget * w = 0);
QSize preferredIconSize(float x = 1.f, const QWidget * w = 0);
double appScale(const QWidget * w = 0);
#endif // QAD_TYPES_H

View File

@@ -0,0 +1,17 @@
<RCC>
<qresource prefix="/">
<file>../icons/edit-clear.png</file>
<file>../icons/document-save.png</file>
<file>../icons/edit-clear-locationbar-rtl.png</file>
<file>../icons/edit-find.png</file>
<file>../icons/list-add.png</file>
<file>../icons/edit-delete.png</file>
<file>../icons/item-add.png</file>
<file>../icons/item.png</file>
<file>../icons/node-add.png</file>
<file>../icons/node.png</file>
<file>../icons/edit-copy.png</file>
<file>../icons/edit-paste.png</file>
<file>../icons/document-open_16.png</file>
</qresource>
</RCC>

View File

@@ -0,0 +1,825 @@
#include "qpiconfig.h"
#include <QFileInfo>
#include <QDir>
int QString2int(const QString & string) {
QString s = string.trimmed();
if (s.left(2) == "0x") return s.mid(2).toLongLong(0, 16);
if (s.left(1) == "0") return s.mid(1).toLongLong(0, 8);
return s.toLongLong();
}
QRect QString2QRect(const QString & string) {
int sl, st, sw, sh;
int s = 0, e;
e = string.indexOf(";");
sl = string.mid(s, e - s).toInt();
s = e + 1;
e = string.indexOf(";", s);
st = string.mid(s, e - s).toInt();
s = e + 1;
e = string.indexOf(";", s);
sw = string.mid(s, e - s).toInt();
s = e + 1;
e = string.length();
sh = string.mid(s, e - s).toInt();
return QRect(sl, st, sw, sh);
}
QRectF QString2QRectF(const QString & string) {
double sl, st, sw, sh;
int s = 0, e;
e = string.indexOf(";");
sl = string.mid(s, e - s).toDouble();
s = e + 1;
e = string.indexOf(";", s);
st = string.mid(s, e - s).toDouble();
s = e + 1;
e = string.indexOf(";", s);
sw = string.mid(s, e - s).toDouble();
s = e + 1;
e = string.length();
sh = string.mid(s, e - s).toDouble();
return QRectF(sl, st, sw, sh);
}
QPoint QString2QPoint(const QString & string) {
int sx, sy;
int s = 0, e;
e = string.indexOf(";");
sx = string.mid(s, e - s).toInt();
s = e + 1;
e = string.length();
sy = string.mid(s, e - s).toInt();
return QPoint(sx, sy);
}
QPointF QString2QPointF(const QString & string) {
double sx, sy;
int s = 0, e;
e = string.indexOf(";");
sx = string.mid(s, e - s).toDouble();
s = e + 1;
e = string.length();
sy = string.mid(s, e - s).toDouble();
return QPointF(sx, sy);
}
QString QRect2QString(const QRect & rect) {
return QString::number(rect.left()) + ";" +
QString::number(rect.top()) + ";" +
QString::number(rect.width()) + ";" +
QString::number(rect.height());
}
QString QRectF2QString(const QRectF & rect) {
return QString::number(rect.left()) + ";" +
QString::number(rect.top()) + ";" +
QString::number(rect.width()) + ";" +
QString::number(rect.height());
}
QPIConfig::Entry QPIConfig::Branch::_empty;
QPIConfig::Entry QPIConfig::Entry::_empty;
QPIConfig::Branch QPIConfig::Branch::allLeaves() {
Branch b;
b.delim = delim;
foreach (Entry * i, *this) {
if (i->isLeaf()) b << i;
else allLeaves(b, i);
}
return b;
}
QPIConfig::Entry & QPIConfig::Branch::getValue(const QString & vname, const QString & def, bool * exist) {
if (vname.isEmpty()) {
_empty.clear();
_empty.delim = delim;
if (exist != 0) *exist = false;
return _empty;
}
QStringList tree = vname.split(delim);
QString name = tree.front();
tree.pop_front();
Entry * ce = 0;
foreach (Entry * i, *this)
if (i->_name == name) {
ce = i;
break;
}
if (ce == 0) {
_empty._name = vname;
_empty._value = def;
_empty.delim = delim;
if (exist != 0) *exist = false;
return _empty;
}
foreach (QString i, tree) {
ce = ce->findChild(i);
if (ce == 0) {
_empty._name = vname;
_empty._value = def;
_empty.delim = delim;
if (exist != 0) *exist = false;
return _empty;
}
}
if (exist != 0) *exist = true;
return *ce;
}
QPIConfig::Branch QPIConfig::Branch::getValues(const QString & name) {
Branch b;
b.delim = delim;
foreach (Entry * i, *this) {
if (i->isLeaf()) {
if (i->_name.indexOf(name) >= 0)
b << i;
} else {
foreach (Entry * j, i->_children)
if (j->_name.indexOf(name) >= 0)
b << j;
}
}
return b;
}
QPIConfig::Branch QPIConfig::Branch::getLeaves() {
Branch b;
b.delim = delim;
foreach (Entry * i, *this)
if (i->isLeaf())
b << i;
return b;
}
QPIConfig::Branch QPIConfig::Branch::getBranches() {
Branch b;
b.delim = delim;
foreach (Entry * i, *this)
if (!i->isLeaf())
b << i;
return b;
}
QPIConfig::Branch & QPIConfig::Branch::filter(const QString & f) {
for (int i = 0; i < size(); ++i) {
if (at(i)->_name.indexOf(f) < 0) {
remove(i);
--i;
}
}
return *this;
}
bool QPIConfig::Branch::entryExists(const Entry * e, const QString & name) const {
if (e->_children.isEmpty()) {
if (e->_name == name) return true;
else return false;
}
foreach (Entry * i, e->_children)
if (entryExists(i, name)) return true;
return false;
}
QPIConfig::Entry & QPIConfig::Entry::getValue(const QString & vname, const QString & def, bool * exist) {
QStringList tree = vname.split(delim);
Entry * ce = this;
foreach (QString i, tree) {
ce = ce->findChild(i);
if (ce == 0) {
_empty._name = vname;
_empty._value = def;
_empty.delim = delim;
if (exist != 0) *exist = false;
return _empty;
}
}
if (exist != 0) *exist = true;
return *ce;
}
QPIConfig::Branch QPIConfig::Entry::getValues(const QString & vname) {
Branch b;
b.delim = delim;
foreach (Entry * i, _children)
if (i->_name.indexOf(vname) >= 0)
b << i;
return b;
};
bool QPIConfig::Entry::entryExists(const Entry * e, const QString & name) const {
if (e->_children.isEmpty()) {
if (e->_name == name) return true;
else return false;
}
foreach (Entry * i, e->_children)
if (entryExists(i, name)) return true;
return false;
}
QPIConfig::QPIConfig(const QString & path, QIODevice::OpenMode mode, QPIConfig::FileType type_): QFile(path) {
init();
type = type_;
open(mode);
//stream.setDevice(this);
parse();
}
QPIConfig::QPIConfig(const QString & path, QIODevice::OpenMode mode): QFile(path) {
init();
type = Config;
open(mode);
//stream.setDevice(this);
parse();
}
QPIConfig::QPIConfig(const QString & path, QPIConfig::FileType type_): QFile(path) {
init();
type = type_;
open(QIODevice::ReadWrite);
//stream.setDevice(this);
parse();
}
QPIConfig::QPIConfig(QString * str, QPIConfig::FileType type_) {
init();
type = type_;
buffer = str;
//stream.setDevice(this);
parse();
}
QPIConfig::QPIConfig(const QString & path, QStringList dirs) {
init();
type = Config;
internal = true;
dev = new QFile(path);
dev->open(QIODevice::ReadOnly);
incdirs = dirs;
incdirs << QFileInfo(path).absoluteDir().path();
QString cp = path;
while (!dev->isOpen()) {
if (dirs.isEmpty()) break;
cp = dirs.back();
if (cp.endsWith("/") || cp.endsWith("\\")) cp.chop(1);
cp += "/" + path;
dev->setFileName(cp);
dev->open(QIODevice::ReadOnly);
dirs.pop_back();
}
if (!dev->isOpen()) {
delete dev;
dev = 0;
return;
}
dev->close();
setFileName(cp);
open(QIODevice::ReadOnly);
parse();
}
QPIConfig::~QPIConfig() {
stream.setDevice(0);
root.deleteBranch();
foreach (QPIConfig * c, inc_devs)
delete c;
inc_devs.clear();
includes.clear();
}
void QPIConfig::init() {
internal = false;
buffer = 0;
dev = 0;
delim = ".";
root._name = "root";
root.delim = delim;
empty.delim = delim;
empty._parent = 0;
}
void QPIConfig::setFile(const QString & path, QIODevice::OpenMode mode) {
buffer = 0;
setFileName(path);
if (open(mode))
parse();
}
void QPIConfig::setString(QString * str) {
buffer = str;
parse();
}
QPIConfig::Entry & QPIConfig::getValue(const QString & vname, const QString & def, bool * exist) {
QStringList tree = vname.split(delim);
Entry * ce = &root;
foreach (QString i, tree) {
ce = ce->findChild(i);
if (ce == 0) {
if (exist != 0) *exist = false;
empty._name = vname;
empty._value = def;
empty.delim = delim;
return empty;
}
}
if (exist != 0) *exist = true;
return *ce;
}
QPIConfig::Branch QPIConfig::getValues(const QString & vname) {
Branch b;
b.delim = delim;
foreach (Entry * i, root._children)
if (i->_name.indexOf(vname) >= 0)
b << i;
return b;
}
QPIConfig::Entry & QPIConfig::addEntry(const QString & name, const QString & value, const QString & type, bool write, bool node) {
if (getValue(name)._parent != 0)
return empty;
QString sn = name, tn;
bool toRoot = false;
while (sn.indexOf(delim) == 0) sn.remove(0, delim.length());
QStringList tree = sn.split(delim);
QString ename = tree.back();
tn = tree.front();
tree.pop_back();
Entry * te, * ce, * entry = &root;
if (tree.isEmpty()) toRoot = true;
foreach (QString i, tree) {
te = entry->findChild(i);
if (te == 0) {
ce = new Entry();
ce->delim = delim;
ce->_tab = entry->_tab;
ce->_line = entry->_line;
ce->_name = i;
ce->_parent = entry;
//qDebug() << " [QPIC] add " + tn;
ce->_full_name = tn;
entry->_children << ce;
entry = ce;
} else entry = te;
tn += delim + i;
}
QPIConfig::Branch ch = entry->_children;
qSort(ch.begin(), ch.end(), QPIConfig::Entry::compare);
te = (entry->isLeaf() ? 0 : ch.back());
ce = new Entry();
ce->delim = delim;
ce->_name = ename;
if (!node) ce->_value = value;
ce->_type = type;
if (te == 0) {
//qDebug() << "[QPIC] te == 0";
ce->_tab = entry->_tab;
if (toRoot) ce->_line = other.size();
else {
ch = entry->_parent->_children;
qSort(ch.begin(), ch.end(), QPIConfig::Entry::compare);
ce->_line = ch.back()->_line + 1;
}
} else {
ce->_tab = te->_tab;
if (toRoot) ce->_line = other.size();
else ce->_line = te->_line + 1;
}
//qDebug() << "[QPIC] add " + sn + " at line " << ce->_line << ", parent " << entry->_name;
ce->_full_name = sn;
ce->_parent = entry;
entry->_children << ce;
//qDebug() << "[QPIC] children " << entry->childCount();
if (!node) {
other.insert(ce->_line, "");
Branch b = allLeaves();
//qDebug() << "[QPIC] allLeaves " << b.size();
bool found = false;
for (int i = 0; i < b.size(); ++i) {
if (found) {
b[i]->_line++;
continue;
}
if (b[i] == ce) {
found = true;
if (i > 0)
if (b[i - 1]->_line == b[i]->_line)
b[i - 1]->_line++;
}
//qDebug() << b[i]->_line;
}
}
//qDebug() << "[QPIC] add " + sn + " at line " << ce->_line << ", parent " + entry->_name;
if (write) writeAll();
return *ce;
}
void QPIConfig::setValue(const QString & name, const QString & value, const QString & type, bool write) {
Entry & e(getValue(name));
if (&e == &empty) {
addEntry(name, value, type, write);
return;
}
e._value = value;
e._type = type;
if (write) writeAll();
}
int QPIConfig::entryIndex(const QString & name) {
QStringList tree = name.split(delim);
Entry * ce = &root;
foreach (QString i, tree) {
ce = ce->findChild(i);
if (ce == 0)
return -1;
}
Branch b = allLeaves();
return allLeaves().indexOf(ce);
}
void QPIConfig::setValue(uint number, const QString & value, bool write) {
Entry & e(entryByIndex(number));
if (&e == &empty) return;
e._value = value;
if (write) writeAll();
}
void QPIConfig::setName(uint number, const QString & name, bool write) {
Entry & e(entryByIndex(number));
if (&e == &empty) return;
e._name = name;
if (write) writeAll();
}
void QPIConfig::setType(uint number, const QString & type, bool write) {
Entry & e(entryByIndex(number));
if (&e == &empty) return;
e._type = type;
if (write) writeAll();
}
void QPIConfig::setComment(uint number, const QString & comment, bool write) {
Entry & e(entryByIndex(number));
if (&e == &empty) return;
e._comment = comment;
if (write) writeAll();
}
void QPIConfig::removeEntry(const QString & name, bool write) {
Entry & e(getValue(name));
if (&e == &empty) return;
Branch b = allLeaves();
removeEntry(b, &e);
if (write) writeAll();
}
void QPIConfig::removeEntry(uint number, bool write) {
Entry & e(entryByIndex(number));
if (&e == &empty) return;
Branch b = allLeaves();
removeEntry(b, &e);
if (write) writeAll();
}
void QPIConfig::removeEntry(Branch & b, QPIConfig::Entry * e) {
bool leaf = true;
//qDebug() << " before " << b.size();
if (e->isLeaf()) other.removeAt(e->_line);
if (!e->isLeaf() && !e->_value.isEmpty()) {
e->_value.clear();
//leaf = false;
} else {
int cc = e->_children.size();
for (int i = 0; i < cc; ++i)
removeEntry(b, e->_children.back());
}
bool found = false;
for (int i = 0; i < b.size(); ++i) {
if (found) {
b[i]->_line--;
continue;
}
if (b[i] == e) found = true;
}
if (!leaf) return;
int ti = e->_parent->_children.indexOf(e);
if (ti >= 0) e->_parent->_children.remove(ti);
ti = b.indexOf(e);
if (ti >= 0) b.remove(ti);
//qDebug() << " after " << b.size();
delete e;
}
void QPIConfig::writeAll() {
if (buffer == 0) {
stream.setDevice(this);
resize(0);
} else {
stream.setString(buffer);
buffer->clear();
}
stream.seek(0);
//writeEntry(&root);
buildFullNames(&root);
Branch b = allLeaves();
QString prefix, tprefix;
bool isPrefix;
int j = 0;
for (int i = 0; i < other.size(); ++i) {
//cout << j << endl;
if (j >= 0 && j < b.size()) {
if (b[j]->_line == i) {
b[j]->buildLine();
stream << b[j]->_all << '\n';
++j;
} else {
stream << other[i];
tprefix = getPrefixFromLine(other[i], &isPrefix);
if (isPrefix) {
prefix = tprefix;
if (!prefix.isEmpty())
prefix += delim;
}
if (i < other.size() - 1) stream << '\n';
}
} else {
stream << other[i];
tprefix = getPrefixFromLine(other[i], &isPrefix);
if (isPrefix) {
prefix = tprefix;
if (!prefix.isEmpty())
prefix += delim;
}
if (i < other.size() - 1) stream << '\n';
}
}
if (buffer == 0)
flush();
readAll();
}
QString QPIConfig::getPrefixFromLine(QString line, bool * exists) {
line = line.trimmed();
if (line.left(1) == "#") {if (exists) *exists = false; return QString();}
int ci = line.indexOf("#");
if (ci >= 0) line = line.left(ci).trimmed();
if (line.indexOf("=") >= 0) {if (exists) *exists = false; return QString();}
if (line.indexOf("[") >= 0 && line.indexOf("]") >= 0) {
if (exists) *exists = true;
line.remove(0, 1);
return line.left(line.lastIndexOf("]")).trimmed();
}
if (exists) *exists = false;
return QString();
}
QString QPIConfig::writeAllToString() {
QString str;
QTextStream s(&str);
//writeEntry(&root);
buildFullNames(&root);
Branch b = allLeaves();
int j = 0;
for (int i = 0; i < other.size(); ++i) {
//cout << j << endl;
if (j >= 0 && j < b.size()) {
if (b[j]->_line == i) {
b[j]->buildLine();
s << b[j]->_all << '\n';
++j;
} else {
s << other[i];
if (i < other.size() - 1) s << '\n';
}
} else {
s << other[i];
if (i < other.size() - 1) s << '\n';
}
}
return str;
}
void QPIConfig::readAllFromString(const QString & str) {
parse(str);
}
void QPIConfig::clear() {
root.deleteBranch();
root.clear();
other.clear();
if (buffer == 0)
resize(0);
else
buffer->clear();
stream.seek(0);
}
void QPIConfig::readAll() {
if (buffer == 0)
flush();
parse();
}
bool QPIConfig::entryExists(const Entry * e, const QString & name) const {
if (e->_children.isEmpty()) {
if (e->_name == name) return true;
else return false;
}
foreach (Entry * i, e->_children)
if (entryExists(i, name)) return true;
return false;
}
void QPIConfig::updateIncludes() {
if (internal) return;
all_includes.clear();
foreach (QPIConfig * c, includes)
all_includes << c->allLeaves();
}
QString QPIConfig::parseLine(QString v) {
int i = -1, l = 0;
while (1) {
i = v.indexOf("${");
if (i < 0) break;
l = v.indexOf("}", i + 1);
QString w = v.mid(i + 2, l - i - 2), r;
l = w.length() + 3;
w = parseLine(w);
w = w.trimmed();
bool ex = false;
QPIConfig::Entry & me = getValue(w, "", &ex);
if (ex) {
r = me._value;
} else {
foreach (QPIConfig::Entry * e, all_includes) {
if (e->_full_name == w) {
r = e->_value;
break;
}
}
}
v.replace(i, l, r);
}
return v;
}
void QPIConfig::parse(QString content) {
root.deleteBranch();
root.clear();
QString src, str, tab, comm, all, name, type, prefix, tprefix;
QStringList tree;
Entry * entry, * te, * ce;
int ind, sind;
bool isNew, isPrefix;
foreach (QPIConfig * c, inc_devs)
delete c;
inc_devs.clear();
includes.clear();
if (content.isEmpty()) {
if (buffer == 0) {
if (!isOpen()) return;
stream.setDevice(this);
} else
stream.setString(buffer);
} else {
stream.setString(&content);
}
stream.seek(0);
other.clear();
lines = centry = 0;
while (!stream.atEnd()) {
other.push_back(QString());
src = str = parseLine(stream.readLine());
tprefix = getPrefixFromLine(src, &isPrefix);
if (isPrefix) {
prefix = tprefix;
if (!prefix.isEmpty())
prefix += delim;
}
//cout << str << endl;
tab = str.left(str.indexOf(str.trimmed().left(1)));
str = str.trimmed();
all = str;
ind = str.indexOf('=');
if ((ind > 0) && !(str[0] == '#')) {
sind = str.indexOf('#');
if (sind > 0) {
comm = str.right(str.length() - sind - 1).trimmed();
if (comm.length() > 0) type = comm[0];
else type = "s";
comm = comm.right(comm.length() - 1).trimmed();
str = str.left(sind);
} else {
type = "s";
comm = "";
}
//name = str.left(ind).trimmed();
tree = (prefix + str.left(ind).trimmed()).split(delim);
if (tree.front() == "include") {
name = str.right(str.length() - ind - 1).trimmed();
QPIConfig * iconf = new QPIConfig(name, incdirs);
if (!iconf->dev) {
delete iconf;
} else {
inc_devs << iconf;
includes << iconf << iconf->includes;
updateIncludes();
}
//piCout << "includes" << includes;
other.back() = src;
} else {
name = tree.back();
tree.pop_back();
entry = &root;
foreach (QString i, tree) {
te = entry->findChild(i);
if (te == 0) {
ce = new Entry();
ce->delim = delim;
ce->_tab = tab;
ce->_line = lines;
ce->_name = i;
ce->_parent = entry;
entry->_children << ce;
entry = ce;
} else entry = te;
}
isNew = false;
ce = entry->findChild(name);
if (ce == 0) {
ce = new Entry();
isNew = true;
}
ce->delim = delim;
ce->_tab = tab;
ce->_name = name;
ce->_value = str.right(str.length() - ind - 1).trimmed();
ce->_type = type;
ce->_comment = comm;
ce->_line = lines;
ce->_all = all;
if (isNew) {
ce->_parent = entry;
entry->_children << ce;
}
}
} else other.back() = src;
lines++;
}
setEntryDelim(&root, delim);
buildFullNames(&root);
//if (content.isEmpty()) stream.setDevice(this);
}

280
test/qad/utils/qpiconfig.h Normal file
View File

@@ -0,0 +1,280 @@
#ifndef QPICONFIG_H
#define QPICONFIG_H
#include "QFile"
#include "QVector"
#include "QStringList"
#include "QTextStream"
#include <QColor>
#include <QPointF>
#include <QRectF>
#include <QDebug>
#include <cmath>
inline QByteArray QString2QByteArray(const QString & string) {return string.isEmpty() ? QByteArray() : qUncompress(QByteArray::fromBase64(string.toLatin1()));}
int QString2int(const QString & string);
inline QColor QString2QColor(const QString & string) {return (string.left(1) == "#" ? QColor(string.right(string.length() - 1).toInt(0, 16)) : QColor(QString2int(string)));}
QRect QString2QRect(const QString & string);
QRectF QString2QRectF(const QString & string);
QPoint QString2QPoint(const QString & string);
QPointF QString2QPointF(const QString & string);
inline QString QColor2QString(const QColor & color) {QString s = color.name(); return "0x" + QString::number(color.alpha(), 16).rightJustified(2, '0') + s.right(s.length() - 1);}
inline QString QPoint2QString(const QPoint & point) {return QString::number(point.x()) + ";" + QString::number(point.y());}
inline QString QPointF2QString(const QPointF & point) {return QString::number(point.x()) + ";" + QString::number(point.y());}
inline QString QByteArray2QString(const QByteArray & array) {return array.isEmpty() ? QString() : QString(qCompress(array, 9).toBase64());}
QString QRect2QString(const QRect & rect);
QString QRectF2QString(const QRectF & rect);
#define QPICONFIG_GET_VALUE \
Entry & getValue(const QString & vname, const char * def, bool * exist = 0) {return getValue(vname, QString(def), exist);} \
Entry & getValue(const QString & vname, const QStringList & def, bool * exist = 0) {return getValue(vname, def.join("%|%"), exist);} \
Entry & getValue(const QString & vname, const bool def, bool * exist = 0) {return getValue(vname, QString::number(def), exist);} \
Entry & getValue(const QString & vname, const short def, bool * exist = 0) {return getValue(vname, QString::number(def), exist);} \
Entry & getValue(const QString & vname, const int def, bool * exist = 0) {return getValue(vname, QString::number(def), exist);} \
Entry & getValue(const QString & vname, const long def, bool * exist = 0) {return getValue(vname, QString::number(def), exist);} \
Entry & getValue(const QString & vname, const uchar def, bool * exist = 0) {return getValue(vname, QString::number(def), exist);} \
Entry & getValue(const QString & vname, const ushort def, bool * exist = 0) {return getValue(vname, QString::number(def), exist);} \
Entry & getValue(const QString & vname, const uint def, bool * exist = 0) {return getValue(vname, QString::number(def), exist);} \
Entry & getValue(const QString & vname, const ulong def, bool * exist = 0) {return getValue(vname, QString::number(def), exist);} \
Entry & getValue(const QString & vname, const float def, bool * exist = 0) {return getValue(vname, QString::number(def), exist);} \
Entry & getValue(const QString & vname, const double def, bool * exist = 0) {return getValue(vname, QString::number(def), exist);} \
Entry & getValue(const QString & vname, const QColor & def, bool * exist = 0) {return getValue(vname, QColor2QString(def), exist);} \
Entry & getValue(const QString & vname, const Qt::GlobalColor def, bool * exist = 0) {return getValue(vname, QColor(def), exist);} \
Entry & getValue(const QString & vname, const QRect & def, bool * exist = 0) {return getValue(vname, QRect2QString(def), exist);} \
Entry & getValue(const QString & vname, const QRectF & def, bool * exist = 0) {return getValue(vname, QRectF2QString(def), exist);} \
Entry & getValue(const QString & vname, const QPoint & def, bool * exist = 0) {return getValue(vname, QPoint2QString(def), exist);} \
Entry & getValue(const QString & vname, const QPointF & def, bool * exist = 0) {return getValue(vname, QPointF2QString(def), exist);} \
Entry & getValue(const QString & vname, const QByteArray & def, bool * exist = 0) {return getValue(vname, QByteArray2QString(def), exist);}
class QPIConfig: public QFile
{
friend class Entry;
friend class Branch;
friend class QPIConfigWidget;
public:
enum FileType {Config, XML};
QPIConfig(const QString & path, QIODevice::OpenMode mode, QPIConfig::FileType type);
QPIConfig(const QString & path, QIODevice::OpenMode mode = QIODevice::ReadWrite);
QPIConfig(const QString & path, QPIConfig::FileType type);
QPIConfig(QString * str, QPIConfig::FileType type = QPIConfig::Config);
~QPIConfig();
void setFile(const QString & path, QIODevice::OpenMode mode = QIODevice::ReadWrite);
void setString(QString * str);
class Entry;
class Branch: public QVector<Entry * > {
friend class QPIConfig;
friend class Entry;
public:
Branch() {;}
Entry & getValue(const QString & vname, const QString & def = QString(), bool * exist = 0);
QPICONFIG_GET_VALUE
Branch allLeaves();
Branch getValues(const QString & name);
Branch getLeaves();
Branch getBranches();
Branch & filter(const QString & f);
bool isEntryExists(const QString & name) const {foreach (const Entry * i, *this) if (entryExists(i, name)) return true; return false;}
int indexOf(const Entry * e) {for (int i = 0; i < size(); ++i) if (at(i) == e) return i; return -1;}
//void clear() {foreach (Entry * i, *this) delete i; QVector<Entry * >::clear();}
private:
bool entryExists(const Entry * e, const QString & name) const;
void allLeaves(Branch & b, Entry * e) {foreach (Entry * i, e->_children) {if (i->isLeaf()) b << i; else allLeaves(b, i);}}
static Entry _empty;
QString delim;
};
class Entry {
friend class QPIConfig;
friend class Branch;
friend class QPIConfigWidget;
friend class ConfigValueWidget;
public:
Entry() {_parent = 0; _line = -1;}
Entry * parent() const {return _parent;}
int childCount() {return _children.size();}
Branch & children() {_children.delim = delim; return _children;}
Entry * child(const int index) const {return _children[index];}
const Entry * findChild(const QString & name) const {foreach (const Entry * i, _children) if (i->_name == name) return i; return 0;}
Entry * findChild(const QString & name) {foreach (Entry * i, _children) if (i->_name == name) return i; return 0;}
bool isLeaf() const {return _children.empty();}
const QString & name() const {return _name;}
const QString & value() const {return _value;}
const QString & type() const {return _type;}
const QString & comment() const {return _comment;}
Entry & setName(const QString & value) {_name = value; return *this;}
Entry & setType(const QString & value) {_type = value; return *this;}
Entry & setComment(const QString & value) {_comment = value; return *this;}
Entry & setValue(const QString & value) {_value = value; return *this;}
Entry & setValue(const QStringList & value) {setValue(value.join("%|%")); setType("l"); return *this;}
Entry & setValue(const char * value) {setValue(QString(value)); setType("s"); return *this;}
Entry & setValue(const bool value) {setValue(QString::number(value)); setType("b"); return *this;}
Entry & setValue(const short value) {setValue(QString::number(value)); setType("n"); return *this;}
Entry & setValue(const int value) {setValue(QString::number(value)); setType("n"); return *this;}
Entry & setValue(const long value) {setValue(QString::number(value)); setType("n"); return *this;}
Entry & setValue(const uchar value) {setValue(QString::number(value)); setType("n"); return *this;}
Entry & setValue(const ushort value) {setValue(QString::number(value)); setType("n"); return *this;}
Entry & setValue(const uint value) {setValue(QString::number(value)); setType("n"); return *this;}
Entry & setValue(const ulong value) {setValue(QString::number(value)); setType("n"); return *this;}
Entry & setValue(const float value) {setValue(QString::number(value)); setType("f"); return *this;}
Entry & setValue(const double value) {setValue(QString::number(value)); setType("f"); return *this;}
Entry & setValue(const QColor & value) {setValue(QColor2QString(value)); setType("c"); return *this;}
Entry & setValue(const Qt::GlobalColor & value) {setValue(QColor(value)); return *this;}
Entry & setValue(const QRect & value) {setValue(QRect2QString(value)); setType("r"); return *this;}
Entry & setValue(const QRectF & value) {setValue(QRectF2QString(value)); setType("a"); return *this;}
Entry & setValue(const QPoint & value) {setValue(QPoint2QString(value)); setType("p"); return *this;}
Entry & setValue(const QPointF & value) {setValue(QPointF2QString(value)); setType("v"); return *this;}
Entry & setValue(const QByteArray & value) {setValue(QByteArray2QString(value)); setType("s"); return *this;}
Entry & getValue(const QString & vname, const QString & def = QString(), bool * exist = 0);
QPICONFIG_GET_VALUE
Branch getValues(const QString & vname);
bool isEntryExists(const QString & name) const {return entryExists(this, name);}
const QString & stringValue() const {return _value;}
operator bool() {return (_value.toLower().trimmed() == "true" || _value.toLower().trimmed() == "yes" || _value.toLower().trimmed() == "on" || _value.toInt() > 0);}
operator char() {return (_value.isEmpty() ? 0 : _value[0].toLatin1());}
operator short() {return _value.toShort();}
operator int() {return QString2int(_value);}
operator long() {return QString2int(_value);}
operator uchar() {return QString2int(_value);}
operator ushort() {return QString2int(_value);}
operator uint() {return QString2int(_value);}
operator ulong() {return QString2int(_value);}
operator float() {return _value.toFloat();}
operator double() {return _value.toDouble();}
operator QString() {return _value;}
operator QStringList() {return _value.split("%|%");}
operator QColor() {return QString2QColor(_value);}
operator QRect() {return QString2QRect(_value);}
operator QRectF() {return QString2QRectF(_value);}
operator QPoint() {return QString2QPoint(_value);}
operator QPointF() {return QString2QPointF(_value);}
operator QByteArray() {return QString2QByteArray(_value);}
private:
static bool compare(const QPIConfig::Entry * f, const QPIConfig::Entry * s) {return f->_line < s->_line;}
bool entryExists(const Entry * e, const QString & name) const;
void buildLine() {_all = _tab + _full_name + " = " + _value + " #" + _type + " " + _comment;}
void clear() {_children.clear(); _name = _value = _type = _comment = _all = QString(); _line = 0; _parent = 0;}
void deleteBranch() {foreach (Entry * i, _children) {i->deleteBranch(); delete i;}}
static Entry _empty;
Entry * _parent;
Branch _children;
QString _tab;
QString _name;
QString _value;
QString _type;
QString _comment;
QString _all;
QString _full_name;
QString delim;
int _line;
};
Entry & getValue(const QString & vname, const QString & def = QString(), bool * exist = 0);
QPICONFIG_GET_VALUE
Branch getValues(const QString & vname);
void setValue(const QString & name, const QString & value, const QString & type = "s", bool write = true);
void setValue(const QString & name, const QStringList & value, bool write = true) {setValue(name, value.join("%|%"), "l", write);}
void setValue(const QString & name, const char * value, bool write = true) {setValue(name, QString(value), "s", write);}
void setValue(const QString & name, const bool value, bool write = true) {setValue(name, QString::number(value), "b", write);}
void setValue(const QString & name, const short value, bool write = true) {setValue(name, QString::number(value), "n", write);}
void setValue(const QString & name, const int value, bool write = true) {setValue(name, QString::number(value), "n", write);}
void setValue(const QString & name, const long value, bool write = true) {setValue(name, QString::number(value), "n", write);}
void setValue(const QString & name, const uchar value, bool write = true) {setValue(name, QString::number(value), "n", write);}
void setValue(const QString & name, const ushort value, bool write = true) {setValue(name, QString::number(value), "n", write);}
void setValue(const QString & name, const uint value, bool write = true) {setValue(name, QString::number(value), "n", write);}
void setValue(const QString & name, const ulong value, bool write = true) {setValue(name, QString::number(value), "n", write);}
void setValue(const QString & name, const float value, bool write = true) {setValue(name, QString::number(value), "f", write);}
void setValue(const QString & name, const double value, bool write = true) {setValue(name, QString::number(value), "f", write);}
void setValue(const QString & name, const QColor & value, bool write = true) {setValue(name, QColor2QString(value), "c", write);}
void setValue(const QString & name, const Qt::GlobalColor & value, bool write = true) {setValue(name, QColor(value), write);}
void setValue(const QString & name, const QRect & value, bool write = true) {setValue(name, QRect2QString(value), "r", write);}
void setValue(const QString & name, const QRectF & value, bool write = true) {setValue(name, QRectF2QString(value), "a", write);}
void setValue(const QString & name, const QPoint & value, bool write = true) {setValue(name, QPoint2QString(value), "p", write);}
void setValue(const QString & name, const QPointF & value, bool write = true) {setValue(name, QPointF2QString(value), "v", write);}
void setValue(const QString & name, const QByteArray & value, bool write = true) {setValue(name, QByteArray2QString(value), "s", write);}
Entry & rootEntry() {return root;}
int entriesCount() const {return childCount(&root);}
bool isEntryExists(const QString & name) const {return entryExists(&root, name);}
Branch allTree() {Branch b; foreach (Entry * i, root._children) b << i; return b;}
Branch allLeaves() {Branch b; allLeaves(b, &root); qSort(b.begin(), b.end(), Entry::compare); return b;}
int entryIndex(const QString & name);
QString getName(uint number) {return entryByIndex(number)._name;}
QString getValue(uint number) {return entryByIndex(number)._value;}
QChar getType(uint number) {return entryByIndex(number)._type[0];}
QString getComment(uint number) {return entryByIndex(number)._comment;}
QPIConfig::Entry & addEntry(const QString & name, const QString & value, const QString & type = "s", bool write = true, bool node = false);
void setName(uint number, const QString & name, bool write = true);
void setValue(uint number, const QString & value, bool write = true);
void setType(uint number, const QString & type, bool write = true);
void setComment(uint number, const QString & comment, bool write = true);
void removeEntry(const QString & name, bool write = true);
void removeEntry(uint number, bool write = true);
void clear();
void readAll();
void writeAll();
QString writeAllToString();
void readAllFromString(const QString & str);
const QString & delimiter() const {return delim;}
void setDelimiter(const QString & d) {delim = d; setEntryDelim(&root, d); readAll();}
private:
QPIConfig(const QString & path, QStringList dirs);
void init();
int childCount(const Entry * e) const {int c = 0; foreach (const Entry * i, e->_children) c += childCount(i); c += e->_children.size(); return c;}
bool entryExists(const Entry * e, const QString & name) const;
void buildFullNames(Entry * e) {foreach (Entry * i, e->_children) {if (e != &root) i->_full_name = e->_full_name + delim + i->_name; else i->_full_name = i->_name; buildFullNames(i);}}
void allLeaves(Branch & b, Entry * e) {foreach (Entry * i, e->_children) {if ((!i->_value.isEmpty() && !i->isLeaf()) || i->isLeaf()) b << i; allLeaves(b, i);}}
void setEntryDelim(Entry * e, const QString & d) {foreach (Entry * i, e->_children) setEntryDelim(i, d); e->delim = d;}
inline Entry & entryByIndex(const int index) {Branch b = allLeaves(); if (index < 0 || index >= b.size()) return empty; return *(b[index]);}
void removeEntry(Branch & b, Entry * e);
void deleteEntry(Entry * e) {foreach (Entry * i, e->_children) deleteEntry(i); delete e;}
QString getPrefixFromLine(QString line, bool * exists);
void updateIncludes();
QString parseLine(QString v);
void parse(QString content = QString());
int centry;
bool internal;
QVector<QPIConfig * > includes, inc_devs;
Branch all_includes;
QFile * dev;
QStringList incdirs;
QString delim, * buffer;
Entry root, empty;
uint lines;
QStringList other;
QTextStream stream;
FileType type;
};
#endif // QPICONFIG_H

View File

@@ -0,0 +1,1047 @@
#include "qpievaluator.h"
QPIEvaluatorContent::QPIEvaluatorContent() {
addFunction("arcsin", 1);
addFunction("arccos", 1);
addFunction("arctg", 1);
addFunction("arcctg", 1);
addFunction("random", 2);
addFunction("sin", 1);
addFunction("cos", 1);
addFunction("ctg", 1);
addFunction("tg", 1);
addFunction("exp", 1);
addFunction("cth", 1);
addFunction("sh", 1);
addFunction("ch", 1);
addFunction("th", 1);
addFunction("sqrt", 1);
addFunction("sqr", 1);
addFunction("pow", 2);
addFunction("abs", 1);
addFunction("ln", 1);
addFunction("lg", 1);
addFunction("log", 2);
addFunction("im", 1);
addFunction("re", 1);
addFunction("arg", 1);
addFunction("len", 1);
addFunction("conj", 1);
addFunction("sign", 1);
addFunction("rad", 1);
addFunction("deg", 1);
clearCustomVariables();
//addVariable("n", 0.);
//addVariable("x1", 123);
}
bool QPIEvaluatorContent::setVariableValue(int index, complexd new_value) {
if (index < 0 || index >= variables.size()) return false;
variables[index].value = new_value;
return true;
}
bool QPIEvaluatorContent::setVariableName(int index, const QString & new_name) {
if (index < 0 || index >= variables.size()) return false;
variables[index].name = new_name;
return true;
}
void QPIEvaluatorContent::clearCustomVariables() {
var_index.clear();
func_index.clear();
variables.clear();
addVariable("i", complexd_i);
addVariable("pi", atan(1.) * 4.);
addVariable("e", exp(1.));
cv_count = variables.size();
}
void QPIEvaluatorContent::sortVariables() {
var_index.clear();
qSort(variables.begin(), variables.end());
for (int i = 0; i < variables.size(); i++)
var_index[variables[i].name] = i;
/*
qDebug() << "---";
for (int i = 0; i < variables.size(); i++) {
qDebug() << variables[i].name;
}
*/
}
QPIEvaluatorTypes::BaseFunctions QPIEvaluatorContent::getBaseFunction(const QString & name) {
if (name == "sin") return QPIEvaluatorTypes::bfSin;
if (name == "cos") return QPIEvaluatorTypes::bfCos;
if (name == "tg") return QPIEvaluatorTypes::bfTg;
if (name == "ctg") return QPIEvaluatorTypes::bfCtg;
if (name == "arcsin") return QPIEvaluatorTypes::bfArcsin;
if (name == "arccos") return QPIEvaluatorTypes::bfArccos;
if (name == "arctg") return QPIEvaluatorTypes::bfArctg;
if (name == "arcctg") return QPIEvaluatorTypes::bfArcctg;
if (name == "exp") return QPIEvaluatorTypes::bfExp;
if (name == "random") return QPIEvaluatorTypes::bfRandom;
if (name == "sh") return QPIEvaluatorTypes::bfSh;
if (name == "ch") return QPIEvaluatorTypes::bfCh;
if (name == "th") return QPIEvaluatorTypes::bfTh;
if (name == "cth") return QPIEvaluatorTypes::bfCth;
if (name == "sqrt") return QPIEvaluatorTypes::bfSqrt;
if (name == "sqr") return QPIEvaluatorTypes::bfSqr;
if (name == "pow") return QPIEvaluatorTypes::bfPow;
if (name == "abs") return QPIEvaluatorTypes::bfAbs;
if (name == "ln") return QPIEvaluatorTypes::bfLn;
if (name == "lg") return QPIEvaluatorTypes::bfLg;
if (name == "log") return QPIEvaluatorTypes::bfLog;
if (name == "im") return QPIEvaluatorTypes::bfIm;
if (name == "re") return QPIEvaluatorTypes::bfRe;
if (name == "arg") return QPIEvaluatorTypes::bfArg;
if (name == "len") return QPIEvaluatorTypes::bfLen;
if (name == "conj") return QPIEvaluatorTypes::bfConj;
if (name == "sign") return QPIEvaluatorTypes::bfSign;
if (name == "rad") return QPIEvaluatorTypes::bfRad;
if (name == "deg") return QPIEvaluatorTypes::bfDeg;
return QPIEvaluatorTypes::bfUnknown;
}
const QString & QPIEvaluator::prepare(const QString & string) {
currentString = string.trimmed();
if (currentString.isEmpty()) currentString = "0";
replaceOperators();
removeSpaces();
checkBrackets();
while (fillElements()) checkBrackets();
while (setSignes()) fillElements();
removeJunk();
findUnknownVariables();
return currentString;
}
void QPIEvaluator::removeSpaces() {
QString tmps = currentString;
for (int i = 0; i < tmps.length(); i++) {
if (tmps[i] == ' ' || tmps[i] == '\t') {
tmps.remove(i, 1);
i--;
}
}
currentString = tmps;
}
void QPIEvaluator::removeJunk() {
QChar cc;
bool junk = true;
int bcnt;
while (junk) {
if (currentString.left(1) != "(" || currentString.right(1) != ")") return;
bcnt = 1;
junk = false;
for (int i = 1; i < currentString.length(); i++) {
cc = currentString[i];
if (cc == '(') bcnt++;
if (cc == ')') bcnt--;
if (bcnt == 0) {
if (i == currentString.length() - 1) {
currentString = currentString.mid(1, currentString.length() - 2);
elements.pop_front();
elements.pop_back();
junk = true;
break;
} else break;
}
}
}
}
void QPIEvaluator::replaceOperators() {
currentString.replace("==", "=");
currentString.replace("!=", ":");
currentString.replace(">=", "}");
currentString.replace("<=", "{");
currentString.replace("&&", "&");
currentString.replace("||", "|");
}
void QPIEvaluator::makeOutput(QString & string) {
string.replace(":", "");
string.replace("}", "");
string.replace("{", "");
string.replace("&", "");
string.replace("|", "");
}
void QPIEvaluator::findUnknownVariables() {
QString cvar;
unknownVars.clear();
for (int i = 0; i < currentString.length(); i++) {
if (elements[i].var_num == -666) cvar += currentString[i];
else {
if (cvar.length() == 0) continue;
unknownVars << cvar;
cvar = "";
}
}
if (cvar.length() > 0) unknownVars << cvar;
unknownVars.removeDuplicates();
}
bool QPIEvaluator::isSign(const QChar & ch) {
return ch == '+' || ch == '-' ||
ch == '*' || ch == '/' ||
ch == '%' || ch == '^' ||
ch == '=' || ch == ':' ||
ch == '>' || ch == '<' ||
ch == '}' || ch == '{' ||
ch == '&' || ch == '|';
}
void QPIEvaluator::checkBrackets() {
QString tmps = currentString;
QChar fc, sc;
int bcnt = 0, bpos = 0, inserted = 0;
currentString = tmps;
for (int i = 0; i < tmps.length(); i++) {
if (tmps[i] == '(') {
if (bcnt == 0) bpos = i;
bcnt++;
}
if (tmps[i] == ')') {
if (bcnt == 0) {
currentString.insert(bpos + inserted, "(");
inserted++;
} else bcnt--;
}
}
if (bcnt > 0) currentString += QString(bcnt, ')');
tmps = currentString;
for (int i = 0; i < tmps.length() - 1; i++) {
fc = tmps[i].toLower();
sc = tmps[i + 1].toLower();
if ((fc == ')' && sc == '(') ||
(fc == ')' && sc >= '0' && sc <= '9') ||
(fc == ')' && sc >= 'a' && sc <= 'z') ) tmps.insert(++i, '*');
}
currentString = tmps;
}
bool QPIEvaluator::fillElements() {
int fstart, flen, cnum = 0, cpart = 0, cfunc;
QChar cc, nc, pc, fc = '!';
bool numFound = false;
QString curfind, tmps = currentString;
elements.resize(tmps.length());
for (int i = 0; i < elements.size(); i++) {
elements[i].type = QPIEvaluatorTypes::etVariable;
elements[i].var_num = -666;
}
currentVariables.clear();
//qDebug().nospace() << "search for functions ...";
for (int i = 0; i < content.functionsCount(); i++) {
curfind = content.function(i).identifier;
cfunc = i; //(int)content.function(i).type;
flen = curfind.length();
fstart = 0;
while (fstart >= 0) {
fstart = tmps.indexOf(curfind, fstart);
if (fstart < 0) break;
if (tmps[fstart + flen] != '(') {
//currentString.insert(fstart + flen, "(");
fstart++;
continue;
}
for (int j = fstart; j < fstart + flen; j++) {
elements[j].set(QPIEvaluatorTypes::etFunction, cnum, cfunc);
tmps.replace(j, 1, fc);
}
cnum++;
}
}
cnum = 0;
//qDebug().nospace() << "search for variables ...";
for (int i = 0; i < content.variablesCount(); i++) {
curfind = content.variable(i).name;
flen = curfind.length();
fstart = 0;
while (fstart >= 0) {
fstart = tmps.indexOf(curfind, fstart);
if (fstart < 0) break;
for (int j = fstart; j < fstart + flen; j++) {
elements[j].set(QPIEvaluatorTypes::etVariable, cnum, i);
tmps.replace(j, 1, fc);
}
cnum++;
}
}
curfind = "";
cnum = 1;
//qDebug().nospace() << "search for numbers ...";
for (int i = 0; i < tmps.length(); i++) {
cc = tmps[i];
/*if (cc == " " || cc == "(" || cc == ")") {
curfind = "";
cpart = 0;
numFound = false;
continue;
}*/
switch (cpart) {
case 0:
if ((cc >= '0' && cc <= '9')) {// || cc == '-' || cc == '+') {
curfind += cc;
cpart = 1;
continue;
}
if (cc == '.') {
curfind += cc;
cpart = 2;
continue;
}
if (cc == 'E') {
curfind += cc;
cpart = 3;
continue;
}
break;
case 1:
if (cc >= '0' && cc <= '9') {
curfind += cc;
continue;
}
if (cc == '.') {
curfind += cc;
cpart = 2;
continue;
}
if (cc == 'E') {
curfind += cc;
cpart = 3;
continue;
}
numFound = true;
break;
case 2:
if (cc >= '0' && cc <= '9') {
curfind += cc;
continue;
}
if (cc == 'E') {
curfind += cc;
cpart = 3;
continue;
}
numFound = true;
break;
case 3:
if ((cc >= '0' && cc <= '9') || cc == '-' || cc == '+') {
curfind += cc;
cpart = 4;
continue;
}
numFound = true;
break;
case 4:
if (cc >= '0' && cc <= '9') {
curfind += cc;
continue;
}
numFound = true;
break;
}
if (numFound) {
//qDebug().nospace() << "add " << cnum << ": " << curfind << " = " << curfind.toDouble();
currentVariables.push_back(QPIEvaluatorTypes::Variable("tmp" + QString::number(cnum), curfind.toDouble()));
for (int j = i - curfind.length(); j < i; j++) {
elements[j].set(QPIEvaluatorTypes::etNumber, cnum, -cnum);
tmps.replace(j, 1, fc);
}
curfind = "";
cnum++;
cpart = 0;
numFound = false;
}
}
if (cpart > 0) {
//qDebug().nospace() << "add " << cnum << ": " << curfind << " = " << curfind.toDouble();
currentVariables.push_back(QPIEvaluatorTypes::Variable("tmp" + QString::number(cnum), curfind.toDouble()));
for (int j = tmps.length() - curfind.length(); j < tmps.length(); j++) {
elements[j].set(QPIEvaluatorTypes::etNumber, cnum, -cnum);
tmps.replace(j, 1, fc);
}
}
cc = nc = fc;
//qDebug().nospace() << "search for signes ...";
for (int i = 0; i < tmps.length(); i++) {
cc = tmps[i];
if (i > 0) pc = tmps[i - 1];
else pc = fc;
if (i < tmps.length() - 1) nc = tmps[i + 1];
else nc = fc;
if (cc == '(' || cc == ')' || cc == ',') {
elements[i].set(QPIEvaluatorTypes::etOperator, -1);
continue;
}
if (cc == '-' || cc == '+') {
elements[i].set(QPIEvaluatorTypes::etOperator, -1);
if (i < tmps.length() - 1) if (elements[i + 1].type == QPIEvaluatorTypes::etVariable ||
elements[i + 1].type == QPIEvaluatorTypes::etFunction) continue;
if ((pc == '(' || isSign(pc) || i == 0) && i < tmps.length() - 1) {
if (elements[i + 1].type != QPIEvaluatorTypes::etOperator) {
cnum = elements[i + 1].num;
elements[i].set(QPIEvaluatorTypes::etNumber, cnum);
tmps.replace(i, 1, fc);
///cout << "found sign " << cc << " :" << cnum - 1 << endl;
if (cc == '-' && currentVariables.size() >= cnum)
currentVariables[cnum - 1].value = -currentVariables[cnum - 1].value;
//i++;
continue;
}
}
}
if (isSign(cc)) {
elements[i].set(QPIEvaluatorTypes::etOperator, -1);
continue;
}
}
/*
qDebug().nospace() << tmps;
cout << " ";
for (int i = 0; i < elements.size(); i++) {
switch (elements[i].type) {
case etFunction: cout << "f"; break;
case etNumber: cout << "n"; break;
case etOperator: cout << "o"; break;
case etVariable: cout << "v"; break;
}
}
cout << endl;
*/
return false;
//for (int i = 0; i < currentVariables.size(); i++) qDebug() << "var " << i << ": " << currentVariables[i].value.real();
}
bool QPIEvaluator::setSignes() {
int inserted = 0, ni, pi = 0, needInsert = 0;
QChar fc, sc, pc;
QString tmps = currentString;
for (int i = 0; i < tmps.length() - 1; i++) {
needInsert = 0;
ni = i + 1;
if (i > 0) pi = i - 1;
fc = tmps[i].toLower();
sc = tmps[ni].toLower();
pc = tmps[pi].toLower();
//if (elements[i].type == etOperator || elements[ni].type == etVariable) continue;
if (fc == ',' || sc == ',') continue;
if (elements[i].type == QPIEvaluatorTypes::etOperator && elements[ni].type == QPIEvaluatorTypes::etOperator) continue;
if (fc == ')' && (elements[ni].type == QPIEvaluatorTypes::etNumber || elements[ni].type == QPIEvaluatorTypes::etVariable || elements[ni].type == QPIEvaluatorTypes::etFunction)) needInsert = 1;
if (sc == '(' && (elements[i].type == QPIEvaluatorTypes::etNumber || elements[i].type == QPIEvaluatorTypes::etVariable)) needInsert = 1;
if (elements[i].type == QPIEvaluatorTypes::etNumber && elements[ni].type == QPIEvaluatorTypes::etNumber && elements[i].num != elements[ni].num) needInsert = 1;
if (elements[i].type == QPIEvaluatorTypes::etVariable && elements[ni].type == QPIEvaluatorTypes::etVariable && elements[i].num != elements[ni].num) needInsert = 1;
if ((elements[i].type == QPIEvaluatorTypes::etNumber && elements[ni].type == QPIEvaluatorTypes::etVariable) || (elements[i].type == QPIEvaluatorTypes::etVariable && elements[ni].type == QPIEvaluatorTypes::etNumber)) needInsert = 1;
if ((elements[i].type == QPIEvaluatorTypes::etNumber || elements[i].type == QPIEvaluatorTypes::etVariable) && elements[ni].type == QPIEvaluatorTypes::etFunction) needInsert = 1;
if (elements[i].type == QPIEvaluatorTypes::etFunction && elements[ni].type == QPIEvaluatorTypes::etFunction && elements[i].num != elements[ni].num) needInsert = 2;
if (elements[i].type == QPIEvaluatorTypes::etFunction && elements[ni].type != QPIEvaluatorTypes::etFunction && sc != '(') needInsert = 2;
if (elements[pi].type == QPIEvaluatorTypes::etOperator && (elements[ni].type == QPIEvaluatorTypes::etFunction || elements[ni].type == QPIEvaluatorTypes::etVariable) && fc == '-') needInsert = 3;
switch (needInsert) {
case 1:
currentString.insert(ni + inserted, "*");
elements.insert(ni + inserted, QPIEvaluatorTypes::Element(QPIEvaluatorTypes::etOperator, -1));
//inserted++;
//i++;
return true;
/*case 2:
currentString.insert(ni + inserted, ")");
currentString.insert(ni + inserted, "(");
elements.insert(ni + inserted, Element(etOperator, -1));
elements.insert(ni + inserted, Element(etOperator, -1));
inserted++;
i++;
return true;*/
case 3:
currentString.insert(ni + inserted, "1*");
elements.insert(ni + inserted, QPIEvaluatorTypes::Element(QPIEvaluatorTypes::etOperator, -1));
//inserted;
//i++;
return true;
}
}
/*if (elements[tmps.length() - 1].type == etFunction) {
currentString.insert(tmps.length() + inserted, ")");
currentString.insert(tmps.length() + inserted, "(");
elements.insert(tmps.length() + inserted, Element(etOperator, -1));
elements.insert(tmps.length() + inserted, Element(etOperator, -1));
return true;
}*/
return false;
}
void QPIEvaluator::convert() {
int j;
QPIEvaluatorTypes::Element ce, pe;
for (int i = 0; i < currentString.length(); i++) {
pe = elements[i];
if (pe.type != QPIEvaluatorTypes::etFunction) continue;
j = i + 1;
while (j < currentString.length()) {
ce = elements[j];
if (ce != pe) break;
j++;
}
currentString.replace(i, j - i, " ");
for (int k = i + 1; k < j; k++) elements.remove(i);
//i++;
}
for (int i = 0; i < currentString.length(); i++) {
pe = elements[i];
if (pe.type != QPIEvaluatorTypes::etNumber) continue;
j = i + 1;
while (j < currentString.length()) {
ce = elements[j];
if (ce != pe) break;
j++;
}
currentString.replace(i, j - i, " ");
for (int k = i + 1; k < j; k++) elements.remove(i);
//i++;
}
for (int i = 0; i < currentString.length(); i++) {
pe = elements[i];
if (pe.type != QPIEvaluatorTypes::etVariable) continue;
j = i + 1;
while (j < currentString.length()) {
ce = elements[j];
if (ce != pe) break;
j++;
}
currentString.replace(i, j - i, " ");
for (int k = i + 1; k < j; k++) elements.remove(i);
//i++;
}
/*qDebug().nospace() << currentString;
cout << " ";
for (int i = 0; i < elements.size(); i++) {
switch (elements[i].type) {
case etFunction: cout << "f"; break;
case etNumber: cout << "n"; break;
case etOperator: cout << "o"; break;
case etVariable: cout << "v"; break;
}
}
cout << endl;*/
}
const QString & QPIEvaluator::preprocess(const QString & string) {
static QString ret;
int lind;
ret = prepare(string);
convert();
instructions.clear();
//qDebug() << preproc->currentString;
variables = currentVariables;
lind = parse(currentString);
if (instructions.size() == 0) {
variables.push_back(QPIEvaluatorTypes::Variable());
instructions.push_back(QPIEvaluatorTypes::Instruction(QPIEvaluatorTypes::oNone, QVector<int>(1, lind), -variables.size()));
}
kvars = &(content.variables);
/*
cout << endl << "variables:" << endl;
for (int i = 0; i < variables.size(); i++)
cout << i << " value = " << variables[i].value << endl;
cout << endl << "instructions:" << endl;
for (int i = 0; i < instructions.size(); i++) {
cout << i << endl;
cout << " operation " << instructions[i].operation << endl;
cout << " operators: ";
for (int j = 0; j < instructions[i].operators.size(); j++)
cout << instructions[i].operators[j] << "; ";
cout << endl << " function " << instructions[i].function << endl;
cout << " out " << instructions[i].out << endl;
}
*/
makeOutput(ret);
return ret;
}
QPIEvaluatorTypes::Operation QPIEvaluator::operationInOrder(const int & index) {
switch (index) {
case 0: return QPIEvaluatorTypes::oPower;
case 1: return QPIEvaluatorTypes::oMultiply;
case 2: return QPIEvaluatorTypes::oDivide;
case 3: return QPIEvaluatorTypes::oResidue;
case 4: return QPIEvaluatorTypes::oAdd;
case 5: return QPIEvaluatorTypes::oSubtract;
case 6: return QPIEvaluatorTypes::oEqual;
case 7: return QPIEvaluatorTypes::oNotEqual;
case 8: return QPIEvaluatorTypes::oGreaterEqual;
case 9: return QPIEvaluatorTypes::oSmallerEqual;
case 10: return QPIEvaluatorTypes::oGreater;
case 11: return QPIEvaluatorTypes::oSmaller;
case 12: return QPIEvaluatorTypes::oAnd;
case 13: return QPIEvaluatorTypes::oOr;
default: return QPIEvaluatorTypes::oNone;
}
}
int QPIEvaluator::parse(const QString & string, int offset) {
int slen = string.length(), /*facnt,*/ farg, bcnt, k;
QChar cc;
QPIEvaluatorTypes::Element ce;
QPIEvaluatorTypes::Function cfunc;
QPIEvaluatorTypes::Operation coper;
QString sbrackets, carg;
QVector<int> args, atmp;
QVector<QPIEvaluatorTypes::Operation> opers;
///qDebug() << "to parse :" + string;
///cout << " "; for (int i = 0; i < slen; i++) cout << preproc->elements[i + offset].type; cout << endl;
for (int i = 0; i < slen; i++) {
ce = elements[i + offset];
cc = string[i];
switch (ce.type) {
case QPIEvaluatorTypes::etNumber:
args.push_back(ce.var_num);
continue;
case QPIEvaluatorTypes::etVariable:
args.push_back(ce.var_num);
continue;
case QPIEvaluatorTypes::etFunction:
i++;
cfunc = content.function(ce.var_num);
//facnt = cfunc.arguments;
atmp.clear();
bcnt = farg = 1;
///qDebug() << "function: " + cfunc.identifier;
//for (int k = 0; k < facnt; k++) {
carg = "";
k = i + 1;
//if (string.size() <= k || k < 0) return -666;
while (bcnt > 0) {
//if (k < facnt - 1) fcomma = string.indexOf(',', j);
cc = string[k];
switch (cc.toLatin1()) {
case '(': bcnt++; break;
case ')':
bcnt--;
if (bcnt == 0) {
///qDebug() << "arument: " << carg;
atmp.push_back(parse(carg, k + offset - carg.length()));
k++;
carg = "";
if (atmp.size() > 0) if (atmp.back() < 0 && farg > 0) farg = atmp.back();
continue;
}
break;
case ',':
if (bcnt == 1) {
///qDebug() << "arument: " << carg;
atmp.push_back(parse(carg, k + offset - carg.length()));
k++;
carg = "";
if (atmp.size() > 0) if (atmp.back() < 0 && farg > 0) farg = atmp.back();
continue;
}
break;
}
carg += cc;
k++;
}
i = k - 1;
if (farg > 0) {
variables.push_back(QPIEvaluatorTypes::Variable());
farg = -variables.size();
}
instructions.push_back(QPIEvaluatorTypes::Instruction(QPIEvaluatorTypes::oFunction, atmp, farg, ce.var_num));
args.push_back(farg);
//for (int i = 0; i < args.size(); i++) cout << preproc->currentVariables[-args[i]].value << endl;
//i = j + 1;
continue;
case QPIEvaluatorTypes::etOperator:
//qDebug() << "operator: " << cc;
if (cc == '(') {
sbrackets = inBrackets(string.right(slen - i));
args.push_back(parse(sbrackets, i + offset + 1));
i += sbrackets.length() + 1;
continue;
}
if (cc == '+') {opers.push_back(QPIEvaluatorTypes::oAdd); continue;}
if (cc == '-') {opers.push_back(QPIEvaluatorTypes::oSubtract); continue;}
if (cc == '*') {opers.push_back(QPIEvaluatorTypes::oMultiply); continue;}
if (cc == '/') {opers.push_back(QPIEvaluatorTypes::oDivide); continue;}
if (cc == '%') {opers.push_back(QPIEvaluatorTypes::oResidue); continue;}
if (cc == '^') {opers.push_back(QPIEvaluatorTypes::oPower); continue;}
if (cc == '=') {opers.push_back(QPIEvaluatorTypes::oEqual); continue;}
if (cc == ':') {opers.push_back(QPIEvaluatorTypes::oNotEqual); continue;}
if (cc == '}') {opers.push_back(QPIEvaluatorTypes::oGreaterEqual); continue;}
if (cc == '{') {opers.push_back(QPIEvaluatorTypes::oSmallerEqual); continue;}
if (cc == '>') {opers.push_back(QPIEvaluatorTypes::oGreater); continue;}
if (cc == '<') {opers.push_back(QPIEvaluatorTypes::oSmaller); continue;}
if (cc == '&') {opers.push_back(QPIEvaluatorTypes::oAnd); continue;}
if (cc == '|') {opers.push_back(QPIEvaluatorTypes::oOr); continue;}
}
}
/*
cout << "stack: " << endl << "args: ";
for (int i = 0; i < args.size(); i++) cout << args[i] << ", ";
cout << endl << "opers: ";
for (int i = 0; i < opers.size(); i++) cout << opers[i] << ", ";
*/
if (opers.size() == 0) {
if (args.size() > 0) return args.back();
else return -666;
}
for (int i = 0; i < QPIEvaluatorTypes::operationCount; i++) {
coper = operationInOrder(i);
for (int j = 0; j < opers.size(); j++) {
if (coper == QPIEvaluatorTypes::oDivide || coper == QPIEvaluatorTypes::oMultiply) {
if (opers[j] != QPIEvaluatorTypes::oDivide && opers[j] != QPIEvaluatorTypes::oMultiply) continue;
} else {
if (opers[j] != coper) continue;
}
atmp.clear();
if (j < args.size() && j >= 0) atmp.push_back(args[j]);
else atmp.push_back(-666);
if (j + 1 < args.size() && j >= -1) atmp.push_back(args[j + 1]);
else atmp.push_back(-666);
farg = 1;
if (atmp[0] < 0) farg = atmp[0];
else {
if (atmp[1] < 0) farg = atmp[1];
else {
variables.push_back(QPIEvaluatorTypes::Variable());
farg = -variables.size();
}
}
instructions.push_back(QPIEvaluatorTypes::Instruction(opers[j], atmp, farg));
if (j >= 0 && j < args.size()) {
args.remove(j);
if (j < args.size()) args[j] = farg;
}
opers.remove(j);
j--;
}
}
return instructions.back().out;
///cout << endl;
}
bool QPIEvaluator::check() {
QPIEvaluatorTypes::Instruction ci;
bool error;
if (unknownVars.size() > 0) {
lastError = "Unknown variables: \"" + unknownVars.join("\", \"") + "\"";
return false;
}
for (int i = 0; i < instructions.size(); i++) {
error = false;
ci = instructions[i];
switch (ci.operation) {
case QPIEvaluatorTypes::oNone: break;
case QPIEvaluatorTypes::oFunction:
for (int j = 0; j < ci.operators.size(); j++) {
if (ci.operators[j] == -666) { //(ci.operators[j] < -variables.size() || ci.operators[j] >= kvars->size()) {
error = true;
break;
}
}
if (ci.operators.size() != content.function(ci.function).arguments || error) {
lastError = "Invalid arguments count for function \"" + content.function(ci.function).identifier + "\"";
return false;
}
break;
default:
if (ci.operators[0] == -666 || ci.operators[1] == -666) error = true;
if (ci.operators.size() != 2 || error) {
lastError = "Invalid arguments count for operation \" " + operationChar(ci.operation) + " \"";
return false;
}
break;
}
if (ci.out < -variables.size()) {
lastError = "Invalid variable index \"" + QString::number(ci.out) + "\"";
return false;
}
for (int j = 0; j < ci.operators.size(); j++) {
if (ci.operators[j] < -variables.size() || ci.operators[j] >= kvars->size()) {
lastError = "Invalid variable index \"" + QString::number(ci.operators[j]) + "\"";
return false;
}
}
}
return true;
}
QString QPIEvaluator::inBrackets(const QString & string) {
int slen = string.length(), bcnt = 0;
QChar cc;
for (int i = 0; i < slen; i++) {
cc = string[i];
if (cc == '(') bcnt++;
if (cc == ')') {
bcnt--;
if (bcnt == 0) return string.mid(1, i - 1);
}
}
return QString();
}
QString QPIEvaluator::operationChar(const QPIEvaluatorTypes::Operation & operation) {
switch (operation) {
case QPIEvaluatorTypes::oAdd: return "+";
case QPIEvaluatorTypes::oSubtract: return "-";
case QPIEvaluatorTypes::oMultiply: return "*";
case QPIEvaluatorTypes::oDivide: return "/";
case QPIEvaluatorTypes::oPower: return "^";
case QPIEvaluatorTypes::oResidue: return "%";
case QPIEvaluatorTypes::oEqual: return "=";
case QPIEvaluatorTypes::oNotEqual: return ("");
case QPIEvaluatorTypes::oGreaterEqual: return ("");
case QPIEvaluatorTypes::oSmallerEqual: return ("");
case QPIEvaluatorTypes::oGreater: return ">";
case QPIEvaluatorTypes::oSmaller: return "<";
case QPIEvaluatorTypes::oAnd: return ("");
case QPIEvaluatorTypes::oOr: return ("");
default: return "???";
}
}
inline complexd QPIEvaluator::residue(const complexd & f, const complexd & s) {
complexd ret;
if (s.real() != 0.) ret = complexd(f.real() - ((int)(f.real() / s.real())) * s.real(), 0.);
if (s.imag() != 0.) ret = complexd(ret.real(), f.imag() - ((int)(f.imag() / s.imag())) * s.imag());
return ret;
}
inline void QPIEvaluator::execFunction(const QPIEvaluatorTypes::Instruction & ci) {
QPIEvaluatorTypes::Function cfunc = content.function(ci.function);
int oi = -ci.out - 1;
complexd tmp, stmp;
double ldtmp;
//qDebug() << "function " << (int)cfunc.type;
switch (cfunc.type) {
case QPIEvaluatorTypes::bfSin:
tmpvars[oi].value = sin(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfCos:
tmpvars[oi].value = cos(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfTg:
tmpvars[oi].value = tan(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfCtg:
tmp = tan(value(ci.operators[0]));
if (tmp == complexd_0) tmpvars[oi].value = 0.;
else tmpvars[oi].value = complexd_1 / tmp;
break;
case QPIEvaluatorTypes::bfArcsin:
tmpvars[oi].value = asinc_qpie(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfArccos:
tmpvars[oi].value = acosc_qpie(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfArctg:
tmpvars[oi].value = atanc_qpie(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfArcctg:
tmp = atanc_qpie(value(ci.operators[0]));
if (tmp == complexd_0) tmpvars[oi].value = 0.;
else tmpvars[oi].value = complexd_1 / tmp;
break;
case QPIEvaluatorTypes::bfSh:
tmpvars[oi].value = sinh(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfCh:
tmpvars[oi].value = cosh(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfTh:
tmpvars[oi].value = tanh(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfCth:
tmp = tanh(value(ci.operators[0]));
if (tmp == complexd_0) tmpvars[oi].value = 0.;
else tmpvars[oi].value = complexd_1 / tmp;
break;
case QPIEvaluatorTypes::bfAbs:
tmpvars[oi].value = abs(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfSqrt:
tmpvars[oi].value = sqrt(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfSqr:
tmpvars[oi].value = value(ci.operators[0]) * value(ci.operators[0]);
break;
case QPIEvaluatorTypes::bfExp:
tmpvars[oi].value = exp(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfPow:
tmpvars[oi].value = pow(value(ci.operators[0]), value(ci.operators[1]));
break;
case QPIEvaluatorTypes::bfLn:
tmpvars[oi].value = log(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfLg:
tmpvars[oi].value = log10(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfLog:
tmp = log(value(ci.operators[1]));
if (tmp == complexd_0) tmpvars[oi].value = 0.;
else tmpvars[oi].value = log(value(ci.operators[0])) / tmp;
break;
case QPIEvaluatorTypes::bfRe:
tmpvars[oi].value = value(ci.operators[0]).real();
break;
case QPIEvaluatorTypes::bfIm:
tmpvars[oi].value = value(ci.operators[0]).imag();
break;
case QPIEvaluatorTypes::bfArg:
tmpvars[oi].value = arg(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfLen:
tmpvars[oi].value = abs(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfConj:
tmpvars[oi].value = conj(value(ci.operators[0]));
break;
case QPIEvaluatorTypes::bfSign:
ldtmp = value(ci.operators[0]).real();
tmpvars[oi].value = ldtmp >= 0. ? complexd_1 : -complexd_1;
break;
case QPIEvaluatorTypes::bfRad:
tmpvars[oi].value = value(ci.operators[0]) * complexd(deg2rad_qpie, 0.);
break;
case QPIEvaluatorTypes::bfDeg:
tmpvars[oi].value = value(ci.operators[0]) * complexd(rad2deg_qpie, 0.);
break;
case QPIEvaluatorTypes::bfRandom:
tmp = static_cast<double>(qrand()) / RAND_MAX;
stmp = value(ci.operators[1]) - value(ci.operators[0]);
tmpvars[oi].value = value(ci.operators[0]) + tmp * stmp;
break;
default: break;
}
}
inline bool QPIEvaluator::execInstructions() {
QPIEvaluatorTypes::Instruction ci;
int oi;
complexd tmp;
tmpvars = variables;
//cout << "var count " << tmpvars.size() << endl;
for (int i = 0; i < instructions.size(); i++) {
ci = instructions[i];
oi = -ci.out - 1;
//cout << value(ci.operators[0]) << operationChar(ci.operation) << value(ci.operators[1]) << ", " << oi << endl;
switch (ci.operation) {
case QPIEvaluatorTypes::oAdd:
tmpvars[oi].value = value(ci.operators[0]) + value(ci.operators[1]);
break;
case QPIEvaluatorTypes::oSubtract:
tmpvars[oi].value = value(ci.operators[0]) - value(ci.operators[1]);
break;
case QPIEvaluatorTypes::oMultiply:
tmpvars[oi].value = value(ci.operators[0]) * value(ci.operators[1]);
break;
case QPIEvaluatorTypes::oDivide:
tmp = value(ci.operators[1]);
if (tmp == complexd(0., 0.)) tmpvars[oi].value = 0.;
else tmpvars[oi].value = value(ci.operators[0]) / tmp;
break;
case QPIEvaluatorTypes::oResidue:
tmpvars[oi].value = residue(value(ci.operators[0]), value(ci.operators[1]));
break;
case QPIEvaluatorTypes::oPower:
tmpvars[oi].value = pow(value(ci.operators[0]), value(ci.operators[1]));
break;
case QPIEvaluatorTypes::oEqual:
tmpvars[oi].value = value(ci.operators[0]) == value(ci.operators[1]);
break;
case QPIEvaluatorTypes::oNotEqual:
tmpvars[oi].value = value(ci.operators[0]) != value(ci.operators[1]);
break;
case QPIEvaluatorTypes::oGreaterEqual:
tmpvars[oi].value = value(ci.operators[0]).real() >= value(ci.operators[1]).real();
break;
case QPIEvaluatorTypes::oSmallerEqual:
tmpvars[oi].value = value(ci.operators[0]).real() <= value(ci.operators[1]).real();
break;
case QPIEvaluatorTypes::oGreater:
tmpvars[oi].value = value(ci.operators[0]).real() > value(ci.operators[1]).real();
break;
case QPIEvaluatorTypes::oSmaller:
tmpvars[oi].value = value(ci.operators[0]).real() < value(ci.operators[1]).real();
break;
case QPIEvaluatorTypes::oAnd:
tmpvars[oi].value = value(ci.operators[0]).real() > 0. && value(ci.operators[1]).real() > 0.;
break;
case QPIEvaluatorTypes::oOr:
tmpvars[oi].value = value(ci.operators[0]).real() > 0. || value(ci.operators[1]).real() > 0.;
break;
case QPIEvaluatorTypes::oFunction:
execFunction(ci);
break;
case QPIEvaluatorTypes::oNone:
tmpvars[oi].value = value(ci.operators[0]);
break;
}
}
if (!instructions.isEmpty())
out = value(instructions.back().out);
return true;
}
bool QPIEvaluator::check(const QString & string) {
currentString = preprocess(string);
correct = check();
if (!correct)
return false;
lastError = "Correct";
return true;
}
int QPIEvaluator::setVariable(const QString & name, complexd value) {
int i = content.findVariable(name);
if (i < 0) {
content.addVariable(name, value);
return content.findVariable(name);
} else {
content.setVariableValue(i, value);
return i;
}
return -1;
}
complexd QPIEvaluator::evaluate() {
if (!execInstructions()) out = 0.;
if (fabs(out.real()) < 1E-300) out = complexd(0., out.imag());
if (fabs(out.imag()) < 1E-300) out = complexd(out.real(), 0.);
return out;
}

View File

@@ -0,0 +1,178 @@
#ifndef QPIEVALUATOR_H
#define QPIEVALUATOR_H
#include <QStringList>
#include <QVector>
#include <QMap>
#include <QDebug>
#include <cmath>
#include <complex>
#ifndef PIP_MATH_COMPLEX
#define QPIEVALUATOR_COMPLEX
typedef std::complex<double> complexd;
const complexd complexd_i(0., 1.);
const complexd complexd_0(0.);
const complexd complexd_1(1.);
#endif
const double deg2rad_qpie = atan(1.) / 45.;
const double rad2deg_qpie = 45. / atan(1.);
inline complexd round_qpie(const complexd & c) {return complexd(round(c.real()), round(c.imag()));}
inline complexd floor_qpie(const complexd & c) {return complexd(floor(c.real()), floor(c.imag()));}
inline complexd ceil_qpie(const complexd & c) {return complexd(ceil(c.real()), ceil(c.imag()));}
inline complexd atanc_qpie(const complexd & c) {return -complexd(-0.5, 1.) * log((complexd_1 + complexd_i * c) / (complexd_1 - complexd_i * c));}
inline complexd asinc_qpie(const complexd & c) {return -complexd_i * log(complexd_i * c + sqrt(complexd_1 - c * c));}
inline complexd acosc_qpie(const complexd & c) {return -complexd_i * log(c + complexd_i * sqrt(complexd_1 - c * c));}
namespace QPIEvaluatorTypes {
static const int operationCount = 14;
enum eType {etNumber, etOperator, etVariable, etFunction};
enum Operation {oNone, oAdd, oSubtract, oMultiply, oDivide, oResidue, oPower,
oEqual, oNotEqual, oGreater, oSmaller, oGreaterEqual, oSmallerEqual,
oAnd, oOr, oFunction};
enum BaseFunctions {bfUnknown, bfSin, bfCos, bfTg, bfCtg,
bfArcsin, bfArccos, bfArctg, bfArcctg,
bfExp, bfRandom, bfSh, bfCh, bfTh, bfCth,
bfSqrt, bfSqr, bfPow, bfAbs,
bfLn, bfLg, bfLog, bfSign,
bfIm, bfRe, bfArg, bfLen, bfConj,
bfRad, bfDeg};
struct Instruction {
Instruction() {;}
Instruction(Operation oper, QVector<int> opers, int out_ind, int func = -1) {
operation = oper; operators = opers; out = out_ind; function = func;}
Operation operation;
QVector<int> operators;
int out;
int function;};
struct Element {
Element() {;}
Element(eType new_type, int new_num, int new_var_num = -1) {set(new_type, new_num, new_var_num);}
void set(eType new_type, int new_num, int new_var_num = -1) {type = new_type; num = new_num; var_num = new_var_num;}
eType type;
int num;
int var_num;};
struct Function {
Function() {arguments = 0; type = bfUnknown;}
Function(const QString & name, int args, BaseFunctions ftype) {identifier = name; arguments = args; type = ftype;}
QString identifier;
BaseFunctions type;
int arguments;};
struct Variable {
Variable() {value = 0.;}
Variable(const QString & var_name, complexd val) {name = var_name; value = val;}
QString name;
complexd value;};
inline bool operator <(const Variable & s1, const Variable & s2) {
if (s1.name.size() != s2.name.size())
return s1.name.size() > s2.name.size();
return s1.name > s2.name;
}
};
/*
≠ :
≥ }
≤ {
⋀ &
|
*/
class QPIEvaluatorContent
{
friend class QPIEvaluator;
public:
QPIEvaluatorContent();
~QPIEvaluatorContent() {;}
void addFunction(const QString & name, int args = 1) {functions.push_back(QPIEvaluatorTypes::Function(name, args, getBaseFunction(name)));}
void addVariable(const QString & name, const complexd & val = 0., bool sort = true) {variables.push_back(QPIEvaluatorTypes::Variable(name, val)); if (sort) sortVariables();}
int functionsCount() const {return functions.size();}
int variablesCount() const {return variables.size();}
int customVariablesCount() const {return variables.size() - cv_count;}
int findFunction(const QString & name) const {return func_index.value(name, -1);}
int findVariable(const QString & name) const {return var_index.value(name, -1);}
QPIEvaluatorTypes::Function function(int index) {if (index < 0 || index >= functions.size()) return QPIEvaluatorTypes::Function(); return functions[index];}
QPIEvaluatorTypes::Variable variable(int index) {if (index < 0 || index >= variables.size()) return QPIEvaluatorTypes::Variable(); return variables[index];}
QPIEvaluatorTypes::Function function(const QString & name) {return function(findFunction(name));}
QPIEvaluatorTypes::Variable variable(const QString & name) {return variable(findVariable(name));}
QPIEvaluatorTypes::Variable customVariable(int index) {if (index < cv_count || index >= variables.size() + cv_count) return QPIEvaluatorTypes::Variable(); return variables[index + cv_count];}
bool setVariableValue(int index, complexd new_value);
bool setVariableName(int index, const QString & new_name);
bool setVariableValue(const QString & var_name, const complexd & new_value) {return setVariableValue(findVariable(var_name), new_value);}
bool setVariableName(const QString & var_name, const QString & new_name) {return setVariableName(findVariable(var_name), new_name);}
void removeVariable(int index) {variables.remove(index);}
void removeVariable(const QString & var_name) {removeVariable(findVariable(var_name));}
void clearCustomVariables();
void sortVariables();
QPIEvaluatorTypes::BaseFunctions getBaseFunction(const QString & name);
private:
QVector<QPIEvaluatorTypes::Function> functions;
QVector<QPIEvaluatorTypes::Variable> variables;
QMap<QString, int> var_index, func_index;
int cv_count;
};
class QPIEvaluator
{
public:
QPIEvaluator() {correct = false ;}
~QPIEvaluator() {;}
bool check(const QString & string);
bool isCorrect() const {return correct;}
int setVariable(const QString & name, complexd value = 0.);
void setVariable(int index, complexd value = 0.) {if (index >= 0 && index < content.variablesCount()) content.setVariableValue(index, value);}
void setCustomVariableValue(int index, complexd value = 0.) {content.variables[index + content.cv_count].value = value;}
complexd evaluate();
void removeVariable(const QString & name) {content.removeVariable(name);}
void clearCustomVariables() {content.clearCustomVariables();}
int variableIndex(const QString & name) const {return content.findVariable(name);}
const QStringList & unknownVariables() const {return unknownVars;}
const QString & expression() const {return currentString;}
const QString & error() const {return lastError;}
const complexd & lastResult() const {return out;}
static QString inBrackets(const QString & string);
QPIEvaluatorContent content;
private:
const QString & prepare(const QString & string);
const QString & preprocess(const QString & string);
int parse(const QString & string, int offset = 0);
void convert();
void checkBrackets();
void removeSpaces();
void findUnknownVariables();
void removeJunk();
void replaceOperators();
void makeOutput(QString & string);
bool fillElements();
bool setSignes();
bool isSign(const QChar & ch);
QString inverse(const QString & string) {int len = string.length(); QString s; for (int i = 0; i < len; i++) s += string[len - i - 1]; return s;}
bool check();
bool execInstructions();
QString operationChar(const QPIEvaluatorTypes::Operation & operation);
QPIEvaluatorTypes::Operation operationInOrder(const int & index);
complexd value(const int & index) {if (index < 0) return tmpvars[-index - 1].value; else return kvars->at(index).value;}
inline complexd residue(const complexd & f, const complexd & s);
inline void execFunction(const QPIEvaluatorTypes::Instruction & ci);
QVector<QPIEvaluatorTypes::Element> elements;
QVector<QPIEvaluatorTypes::Variable> currentVariables, variables, tmpvars, * kvars;
QVector<QPIEvaluatorTypes::Instruction> instructions;
QStringList unknownVars;
QString currentString, lastError;
complexd out;
bool correct;
};
inline bool operator ==(QPIEvaluatorTypes::Element e1, QPIEvaluatorTypes::Element e2) {return (e1.type == e2.type && e1.num == e2.num);}
inline bool operator !=(QPIEvaluatorTypes::Element e1, QPIEvaluatorTypes::Element e2) {return (e1.type != e2.type || e1.num != e2.num);}
#endif // QPIEVALUATOR_H