initial port for Cevo game in C++ (libs)
This commit is contained in:
commit
8436242032
375 files changed
+46282
No files matched your search
@@ -0,0 +1,34 @@
|
||||
#include "Cevo/ColorButton.h"
|
||||
|
||||
#include <QColorDialog>
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
ColorButton::ColorButton(QWidget *parent) : QPushButton(parent)
|
||||
{
|
||||
connect(this, &QPushButton::clicked, this, &ColorButton::pickColor);
|
||||
updateSwatch();
|
||||
}
|
||||
|
||||
void ColorButton::setColor(const QColor &color)
|
||||
{
|
||||
if (m_color == color)
|
||||
return;
|
||||
m_color = color;
|
||||
updateSwatch();
|
||||
emit colorChanged(m_color);
|
||||
}
|
||||
|
||||
void ColorButton::pickColor()
|
||||
{
|
||||
const QColor picked = QColorDialog::getColor(m_color, this);
|
||||
if (picked.isValid())
|
||||
setColor(picked);
|
||||
}
|
||||
|
||||
void ColorButton::updateSwatch()
|
||||
{
|
||||
setStyleSheet(QStringLiteral("background-color: %1;").arg(m_color.name()));
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
@@ -0,0 +1,46 @@
|
||||
#include "Cevo/DataFile.h"
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
DataFile::DataFile(QObject *parent) : QObject(parent)
|
||||
{
|
||||
m_fileName = fileTypeName() + fileExt();
|
||||
}
|
||||
|
||||
QString DataFile::fileTypeName() const
|
||||
{
|
||||
return tr("Data file");
|
||||
}
|
||||
|
||||
QString DataFile::fileFilter() const
|
||||
{
|
||||
return tr("All files") + QStringLiteral("|*.*");
|
||||
}
|
||||
|
||||
void DataFile::assign(const DataFile &source)
|
||||
{
|
||||
m_fileName = source.m_fileName;
|
||||
m_modified = source.m_modified;
|
||||
}
|
||||
|
||||
void DataFile::loadFromFile(const QString &newFileName)
|
||||
{
|
||||
setModified(false);
|
||||
setFileName(newFileName);
|
||||
}
|
||||
|
||||
void DataFile::saveToFile(const QString &newFileName)
|
||||
{
|
||||
setModified(false);
|
||||
setFileName(newFileName);
|
||||
}
|
||||
|
||||
void DataFile::setModified(bool value)
|
||||
{
|
||||
if (m_modified == value)
|
||||
return;
|
||||
m_modified = value;
|
||||
emit modifiedChanged();
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "Cevo/DebugLog.h"
|
||||
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QFileInfo>
|
||||
#include <QMutexLocker>
|
||||
#include <QTextStream>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
DebugLog::DebugLog(QObject *parent) : QObject(parent) {}
|
||||
|
||||
void DebugLog::setMaxCount(int value)
|
||||
{
|
||||
if (m_maxCount == value)
|
||||
return;
|
||||
m_maxCount = value;
|
||||
QMutexLocker locker(&m_lock);
|
||||
while (m_items.count() > m_maxCount)
|
||||
m_items.removeLast();
|
||||
}
|
||||
|
||||
void DebugLog::add(QString text, QString group)
|
||||
{
|
||||
DebugLogItem item;
|
||||
item.time = QDateTime::currentDateTime();
|
||||
item.group = group;
|
||||
item.text = text;
|
||||
|
||||
{
|
||||
QMutexLocker locker(&m_lock);
|
||||
m_items.prepend(item);
|
||||
while (m_items.count() > m_maxCount)
|
||||
m_items.removeLast();
|
||||
|
||||
if (writeToFileEnable) {
|
||||
// Matches the original literally: Group is folded into itself
|
||||
// ("Foo" -> "[Foo] "), which looks like it may be an upstream
|
||||
// copy-paste bug, but is kept as-is (debug-log-only impact).
|
||||
if (!group.isEmpty())
|
||||
group = group + '[' + group + QStringLiteral("] ");
|
||||
writeToFile(group + text);
|
||||
}
|
||||
}
|
||||
emit newItem(item);
|
||||
}
|
||||
|
||||
void DebugLog::writeToFile(QString text) const
|
||||
{
|
||||
if (fileName.isEmpty())
|
||||
throw std::invalid_argument("Filename not defined");
|
||||
const QFileInfo info(fileName);
|
||||
if (!info.path().isEmpty())
|
||||
QDir().mkpath(info.path());
|
||||
QFile file(fileName);
|
||||
if (!file.open(QIODevice::Append | QIODevice::Text))
|
||||
throw std::runtime_error(("Could not open " + fileName).toStdString());
|
||||
QTextStream stream(&file);
|
||||
stream << QDateTime::currentDateTime().toString(QStringLiteral("hh:mm:ss.zzz")) << ": " << text << '\n';
|
||||
}
|
||||
|
||||
QList<DebugLogItem> DebugLog::items() const
|
||||
{
|
||||
QMutexLocker locker(&m_lock);
|
||||
return m_items;
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
@@ -0,0 +1,32 @@
|
||||
#include "Cevo/Delay.h"
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
void Delay::start()
|
||||
{
|
||||
m_startTime = QDateTime::currentDateTime();
|
||||
m_enabled = true;
|
||||
}
|
||||
|
||||
void Delay::stop()
|
||||
{
|
||||
m_enabled = false;
|
||||
}
|
||||
|
||||
void Delay::setEnabled(bool value)
|
||||
{
|
||||
Q_UNUSED(value);
|
||||
m_enabled = true;
|
||||
}
|
||||
|
||||
bool Delay::isOverflowed() const
|
||||
{
|
||||
return m_enabled && m_startTime.msecsTo(QDateTime::currentDateTime()) > durationMs;
|
||||
}
|
||||
|
||||
bool Delay::isRunning() const
|
||||
{
|
||||
return m_enabled && m_startTime.msecsTo(QDateTime::currentDateTime()) <= durationMs;
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
@@ -0,0 +1,7 @@
|
||||
#include "Cevo/FormBase.h"
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
FormBase::FormBase(QWidget *parent) : QDialog(parent) {}
|
||||
|
||||
} // namespace Cevo
|
||||
@@ -0,0 +1,126 @@
|
||||
#include "Cevo/Geometric.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
int distance(QPoint p1, QPoint p2)
|
||||
{
|
||||
const QPoint d = p2 - p1;
|
||||
return static_cast<int>(std::sqrt(static_cast<double>(d.x() * d.x() + d.y() * d.y())));
|
||||
}
|
||||
|
||||
double dot(QPoint p1, QPoint p2)
|
||||
{
|
||||
return static_cast<double>(p1.x()) * p2.x() + static_cast<double>(p1.y()) * p2.y();
|
||||
}
|
||||
|
||||
int pointToLineDistance(QPoint p, QPoint v, QPoint w, QPoint &intersect)
|
||||
{
|
||||
// l2 is computed via the truncating integer distance() first (matches
|
||||
// the original's own precision loss - Distance(V, W) then squared).
|
||||
double l2 = distance(v, w);
|
||||
l2 = l2 * l2;
|
||||
if (l2 == 0) {
|
||||
intersect = v;
|
||||
return distance(p, v); // v == w case
|
||||
}
|
||||
const double t = dot(p - v, w - v) / l2;
|
||||
if (t < 0) {
|
||||
intersect = v; // beyond the 'v' end of the segment
|
||||
return distance(p, v);
|
||||
}
|
||||
if (t > 1) {
|
||||
intersect = w; // beyond the 'w' end of the segment
|
||||
return distance(p, w);
|
||||
}
|
||||
const QPoint tt(static_cast<int>(v.x() + t * (w.x() - v.x())), static_cast<int>(v.y() + t * (w.y() - v.y())));
|
||||
intersect = tt;
|
||||
return distance(p, tt);
|
||||
}
|
||||
|
||||
QPoint rotatePoint(QPoint center, QPoint p, double angle)
|
||||
{
|
||||
p -= center;
|
||||
return QPoint(center.x() + qRound(p.x() * std::cos(angle) - p.y() * std::sin(angle)),
|
||||
center.y() + qRound(p.x() * std::sin(angle) + p.y() * std::cos(angle)));
|
||||
}
|
||||
|
||||
QVector<QPoint> rotatePoints(QPoint center, const QVector<QPoint> &points, double angle)
|
||||
{
|
||||
QVector<QPoint> result;
|
||||
result.reserve(points.size());
|
||||
for (const QPoint &p : points)
|
||||
result.append(rotatePoint(center, p, angle));
|
||||
return result;
|
||||
}
|
||||
|
||||
bool lineIntersect(QPoint lineAP1, QPoint lineAP2, QPoint lineBP1, QPoint lineBP2, QPoint &intersection)
|
||||
{
|
||||
if (lineAP1 == lineAP2 || lineBP1 == lineBP2)
|
||||
return false;
|
||||
|
||||
const double detLineA = static_cast<double>(lineAP1.x()) * lineAP2.y() - static_cast<double>(lineAP1.y()) * lineAP2.x();
|
||||
const double detLineB = static_cast<double>(lineBP1.x()) * lineBP2.y() - static_cast<double>(lineBP1.y()) * lineBP2.x();
|
||||
|
||||
const QPoint diffLA = lineAP1 - lineAP2;
|
||||
const QPoint diffLB = lineBP1 - lineBP2;
|
||||
|
||||
const double d = static_cast<double>(diffLA.x()) * diffLB.y() - static_cast<double>(diffLA.y()) * diffLB.x();
|
||||
if (d == 0)
|
||||
return false; // parallel lines without intersection
|
||||
const double detDivInv = 1 / d;
|
||||
|
||||
intersection.setX(static_cast<int>((detLineA * diffLB.x() - diffLA.x() * detLineB) * detDivInv));
|
||||
intersection.setY(static_cast<int>((detLineA * diffLB.y() - diffLA.y() * detLineB) * detDivInv));
|
||||
return true;
|
||||
}
|
||||
|
||||
double arcTan2Point(QPoint point)
|
||||
{
|
||||
return std::atan2(static_cast<double>(point.y()), static_cast<double>(point.x()));
|
||||
}
|
||||
|
||||
double arcTanPoint(QPoint point)
|
||||
{
|
||||
if (point.y() == 0)
|
||||
return std::numeric_limits<double>::infinity();
|
||||
return std::atan(static_cast<double>(point.x()) / point.y());
|
||||
}
|
||||
|
||||
QRect rectEnlarge(QRect rect, int value)
|
||||
{
|
||||
return QRect(QPoint(rect.left() - value, rect.top() - value), QPoint(rect.right() + value, rect.bottom() + value));
|
||||
}
|
||||
|
||||
QRect shiftRect(QRect rect, QPoint delta)
|
||||
{
|
||||
return rect.translated(delta);
|
||||
}
|
||||
|
||||
double Vector::length() const
|
||||
{
|
||||
return std::sqrt(static_cast<double>(direction.x() * direction.x() + direction.y() * direction.y()));
|
||||
}
|
||||
|
||||
double Vector::angle() const
|
||||
{
|
||||
return arcTan2Point(direction);
|
||||
}
|
||||
|
||||
void Vector::setLength(double value)
|
||||
{
|
||||
const double a = angle();
|
||||
direction = QPoint(qRound(std::cos(a) * value), qRound(std::sin(a) * value));
|
||||
}
|
||||
|
||||
Vector Vector::create(QPoint p1, QPoint p2)
|
||||
{
|
||||
Vector v;
|
||||
v.position = p1;
|
||||
v.direction = p2 - p1;
|
||||
return v;
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
@@ -0,0 +1,177 @@
|
||||
#include "Cevo/Item.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
QString dataTypeStr(DataType dataType)
|
||||
{
|
||||
switch (dataType) {
|
||||
case DataType::None:
|
||||
return QStringLiteral("None");
|
||||
case DataType::String:
|
||||
return QStringLiteral("String");
|
||||
case DataType::Boolean:
|
||||
return QStringLiteral("Boolean");
|
||||
case DataType::Integer:
|
||||
return QStringLiteral("Integer");
|
||||
case DataType::Float:
|
||||
return QStringLiteral("Float");
|
||||
case DataType::Color:
|
||||
return QStringLiteral("Color");
|
||||
case DataType::Time:
|
||||
return QStringLiteral("Time");
|
||||
case DataType::Date:
|
||||
return QStringLiteral("Date");
|
||||
case DataType::DateTime:
|
||||
return QStringLiteral("DateTime");
|
||||
case DataType::Enumeration:
|
||||
return QStringLiteral("Enumeration");
|
||||
case DataType::Reference:
|
||||
return QStringLiteral("Reference");
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
ItemField &ItemFields::addField(int index, const QString &sysName, const QString &name, DataType dataType)
|
||||
{
|
||||
ItemField field;
|
||||
field.index = index;
|
||||
field.sysName = sysName;
|
||||
field.name = name;
|
||||
field.dataType = dataType;
|
||||
append(field);
|
||||
return last();
|
||||
}
|
||||
|
||||
const ItemField *ItemFields::searchByIndex(int index) const
|
||||
{
|
||||
for (const ItemField &field : *this)
|
||||
if (field.index == index)
|
||||
return &field;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ItemFields Item::getFields() const
|
||||
{
|
||||
ItemFields fields;
|
||||
fields.addField(1, QStringLiteral("Name"), QCoreApplication::translate("Item", "Name"), DataType::String);
|
||||
return fields;
|
||||
}
|
||||
|
||||
const ItemField *Item::getField(int index) const
|
||||
{
|
||||
return getFields().searchByIndex(index);
|
||||
}
|
||||
|
||||
QVariant Item::getValue(int index) const
|
||||
{
|
||||
throw std::out_of_range(QStringLiteral("Unsupported value index %1").arg(index).toStdString());
|
||||
}
|
||||
|
||||
void Item::setValue(int index, const QVariant &value)
|
||||
{
|
||||
Q_UNUSED(value);
|
||||
throw std::out_of_range(QStringLiteral("Unsupported value index %1").arg(index).toStdString());
|
||||
}
|
||||
|
||||
QString Item::getValueAsText(int index) const
|
||||
{
|
||||
const ItemField *field = getField(index);
|
||||
if (!field)
|
||||
throw std::invalid_argument("Unknown field index");
|
||||
switch (field->dataType) {
|
||||
case DataType::Integer:
|
||||
return QString::number(getValueInt(index));
|
||||
case DataType::String:
|
||||
return getValueString(index);
|
||||
case DataType::Color:
|
||||
return {};
|
||||
case DataType::Enumeration:
|
||||
return field->enumStates.value(getValueEnum(index));
|
||||
case DataType::Reference: {
|
||||
const Item *item = getValueReference(index);
|
||||
return item ? item->name : QString();
|
||||
}
|
||||
case DataType::Boolean:
|
||||
return getValueBool(index) ? QCoreApplication::translate("Item", "Yes")
|
||||
: QCoreApplication::translate("Item", "No");
|
||||
default:
|
||||
throw std::invalid_argument(("Unsupported field value data type " + dataTypeStr(field->dataType)).toStdString());
|
||||
}
|
||||
}
|
||||
|
||||
void Item::assign(const Item &source)
|
||||
{
|
||||
id = source.id;
|
||||
const ItemFields fields = getFields();
|
||||
for (const ItemField &field : fields) {
|
||||
switch (field.dataType) {
|
||||
case DataType::String:
|
||||
setValueString(field.index, source.getValueString(field.index));
|
||||
break;
|
||||
case DataType::Color:
|
||||
setValueColor(field.index, source.getValueColor(field.index));
|
||||
break;
|
||||
case DataType::Integer:
|
||||
setValueInt(field.index, source.getValueInt(field.index));
|
||||
break;
|
||||
case DataType::Boolean:
|
||||
setValueBool(field.index, source.getValueBool(field.index));
|
||||
break;
|
||||
case DataType::Enumeration:
|
||||
setValueEnum(field.index, source.getValueEnum(field.index));
|
||||
break;
|
||||
case DataType::Reference:
|
||||
setValueReference(field.index, source.getValueReference(field.index));
|
||||
break;
|
||||
default:
|
||||
throw std::invalid_argument(
|
||||
("Unsupported field value data type " + dataTypeStr(field.dataType)).toStdString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Item::compare(const Item &other) const
|
||||
{
|
||||
if (id != other.id)
|
||||
return false;
|
||||
const ItemFields fields = getFields();
|
||||
for (const ItemField &field : fields) {
|
||||
bool equal = true;
|
||||
switch (field.dataType) {
|
||||
case DataType::String:
|
||||
equal = getValueString(field.index) == other.getValueString(field.index);
|
||||
break;
|
||||
case DataType::Color:
|
||||
equal = getValueColor(field.index) == other.getValueColor(field.index);
|
||||
break;
|
||||
case DataType::Integer:
|
||||
equal = getValueInt(field.index) == other.getValueInt(field.index);
|
||||
break;
|
||||
case DataType::Boolean:
|
||||
equal = getValueBool(field.index) == other.getValueBool(field.index);
|
||||
break;
|
||||
case DataType::Enumeration:
|
||||
equal = getValueEnum(field.index) == other.getValueEnum(field.index);
|
||||
break;
|
||||
case DataType::Reference:
|
||||
equal = getValueReference(field.index) == other.getValueReference(field.index);
|
||||
break;
|
||||
default:
|
||||
throw std::invalid_argument(
|
||||
("Unsupported field value data type " + dataTypeStr(field.dataType)).toStdString());
|
||||
}
|
||||
if (!equal)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
QString Item::className() const
|
||||
{
|
||||
return QCoreApplication::translate("Item", "Item");
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
@@ -0,0 +1,42 @@
|
||||
#include "Cevo/MetaCanvas.h"
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
MetaCanvas::MetaCanvas()
|
||||
{
|
||||
m_painter.begin(&m_picture);
|
||||
}
|
||||
|
||||
void MetaCanvas::finishRecording()
|
||||
{
|
||||
if (m_painter.isActive())
|
||||
m_painter.end();
|
||||
}
|
||||
|
||||
void MetaCanvas::zoom(double factor)
|
||||
{
|
||||
m_transform.scale(factor, factor);
|
||||
}
|
||||
|
||||
void MetaCanvas::move(QPointF delta)
|
||||
{
|
||||
m_transform.translate(delta.x(), delta.y());
|
||||
}
|
||||
|
||||
void MetaCanvas::reset()
|
||||
{
|
||||
finishRecording();
|
||||
m_picture = QPicture();
|
||||
m_transform = QTransform();
|
||||
m_painter.begin(&m_picture);
|
||||
}
|
||||
|
||||
void MetaCanvas::drawTo(QPainter &target) const
|
||||
{
|
||||
target.save();
|
||||
target.setTransform(m_transform, true);
|
||||
target.drawPicture(0, 0, m_picture);
|
||||
target.restore();
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
@@ -0,0 +1,52 @@
|
||||
#include "Cevo/PersistentForm.h"
|
||||
|
||||
#include <QSettings>
|
||||
#include <QWidget>
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
PersistentForm::PersistentForm(QString settingsGroup) : m_settingsGroup(std::move(settingsGroup)) {}
|
||||
|
||||
void PersistentForm::load(QWidget *form, bool defaultMaximized, bool defaultFullScreen)
|
||||
{
|
||||
QSettings settings;
|
||||
settings.beginGroup(m_settingsGroup);
|
||||
const QByteArray geometry = settings.value(QStringLiteral("geometry")).toByteArray();
|
||||
if (!geometry.isEmpty())
|
||||
form->restoreGeometry(geometry);
|
||||
m_fullScreen = settings.value(QStringLiteral("fullScreen"), defaultFullScreen).toBool();
|
||||
const bool maximized = settings.value(QStringLiteral("maximized"), defaultMaximized).toBool();
|
||||
settings.endGroup();
|
||||
|
||||
m_restoredGeometry = form->normalGeometry();
|
||||
if (m_fullScreen)
|
||||
form->showFullScreen();
|
||||
else if (maximized)
|
||||
form->showMaximized();
|
||||
}
|
||||
|
||||
void PersistentForm::save(QWidget *form) const
|
||||
{
|
||||
QSettings settings;
|
||||
settings.beginGroup(m_settingsGroup);
|
||||
settings.setValue(QStringLiteral("geometry"), form->saveGeometry());
|
||||
settings.setValue(QStringLiteral("maximized"), form->isMaximized());
|
||||
settings.setValue(QStringLiteral("fullScreen"), m_fullScreen);
|
||||
settings.endGroup();
|
||||
}
|
||||
|
||||
void PersistentForm::setFullScreen(QWidget *form, bool state)
|
||||
{
|
||||
if (state == m_fullScreen)
|
||||
return;
|
||||
m_fullScreen = state;
|
||||
if (state) {
|
||||
m_restoredGeometry = form->normalGeometry();
|
||||
form->showFullScreen();
|
||||
} else {
|
||||
form->showNormal();
|
||||
form->setGeometry(m_restoredGeometry);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
@@ -0,0 +1,80 @@
|
||||
#include "Cevo/PrefixMultiplier.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
const PrefixMultiplierDef BasePrefixMultipliers = { {
|
||||
{ QStringLiteral("y"), QStringLiteral("yocto"), 1e-24 },
|
||||
{ QStringLiteral("z"), QStringLiteral("zepto"), 1e-21 },
|
||||
{ QStringLiteral("a"), QStringLiteral("atto"), 1e-18 },
|
||||
{ QStringLiteral("f"), QStringLiteral("femto"), 1e-15 },
|
||||
{ QStringLiteral("p"), QStringLiteral("piko"), 1e-12 },
|
||||
{ QStringLiteral("n"), QStringLiteral("nano"), 1e-9 },
|
||||
{ QStringLiteral("u"), QStringLiteral("mikro"), 1e-6 },
|
||||
{ QStringLiteral("m"), QStringLiteral("mili"), 1e-3 },
|
||||
{ QString(), QString(), 1e0 },
|
||||
{ QStringLiteral("k"), QStringLiteral("kilo"), 1e3 },
|
||||
{ QStringLiteral("M"), QStringLiteral("mega"), 1e6 },
|
||||
{ QStringLiteral("G"), QStringLiteral("giga"), 1e9 },
|
||||
{ QStringLiteral("T"), QStringLiteral("tera"), 1e12 },
|
||||
{ QStringLiteral("P"), QStringLiteral("peta"), 1e15 },
|
||||
{ QStringLiteral("E"), QStringLiteral("exa"), 1e18 },
|
||||
{ QStringLiteral("Z"), QStringLiteral("zetta"), 1e21 },
|
||||
{ QStringLiteral("Y"), QStringLiteral("yotta"), 1e24 },
|
||||
} };
|
||||
|
||||
const PrefixMultiplierDef TimePrefixMultipliers = { {
|
||||
{ QStringLiteral("ys"), QStringLiteral("yocto"), 1e-24 },
|
||||
{ QStringLiteral("zs"), QStringLiteral("zepto"), 1e-21 },
|
||||
{ QStringLiteral("as"), QStringLiteral("atto"), 1e-18 },
|
||||
{ QStringLiteral("fs"), QStringLiteral("femto"), 1e-15 },
|
||||
{ QStringLiteral("ps"), QStringLiteral("piko"), 1e-12 },
|
||||
{ QStringLiteral("ns"), QStringLiteral("nano"), 1e-9 },
|
||||
{ QStringLiteral("us"), QStringLiteral("mikro"), 1e-6 },
|
||||
{ QStringLiteral("ms"), QStringLiteral("mili"), 1e-3 },
|
||||
{ QStringLiteral("s"), QStringLiteral("sekunda"), 1 },
|
||||
{ QStringLiteral("min"), QStringLiteral("minuta"), 60 },
|
||||
{ QStringLiteral("hod"), QStringLiteral("hodina"), 3600 },
|
||||
{ QStringLiteral("den"), QStringLiteral("den"), 24 * 3600 },
|
||||
{ QStringLiteral("t\u00FDd"), QStringLiteral("t\u00FDden"), 7 * 24 * 3600 },
|
||||
{ QStringLiteral("m\u011Bs"), QStringLiteral("m\u011Bs\u00EDc"), 30 * 24 * 3600 },
|
||||
{ QStringLiteral("rok"), QStringLiteral("rok"), 365 * 24 * 3600 },
|
||||
{ QStringLiteral("stolet\u00ED"), QStringLiteral("stolet\u00ED"), 3153600000.0 },
|
||||
{ QStringLiteral("tis\u00EDcilet\u00ED"), QStringLiteral("tis\u00EDcilet\u00ED"), 3153600000.0 },
|
||||
} };
|
||||
|
||||
double truncateDigits(double value, int digits)
|
||||
{
|
||||
for (int ii = 2; ii >= -1; --ii) {
|
||||
if (value >= std::pow(10.0, ii)) {
|
||||
const int realDigits = digits < (ii + 1) ? ii + 1 : digits;
|
||||
const double scale = std::pow(10.0, ii - realDigits + 1);
|
||||
return std::round(value / scale) * scale;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
QString formatWithPrefix(double value, const PrefixMultiplierDef &prefixMultipliers, const QString &unitText,
|
||||
int digits)
|
||||
{
|
||||
const int size = static_cast<int>(prefixMultipliers.size());
|
||||
int i = 8;
|
||||
if (value == 0) {
|
||||
// stays at the base-unit entry
|
||||
} else if (value > 1) {
|
||||
// bounds check moved before the array access (the original checks
|
||||
// it after, which can read one element past the end of the array).
|
||||
while (i + 1 < size && i + 1 >= 0 && (value / prefixMultipliers[i + 1].value) > 1)
|
||||
++i;
|
||||
} else {
|
||||
while (i - 1 >= 0 && i - 1 < size && (value / prefixMultipliers[i - 1].value) < 1)
|
||||
--i;
|
||||
++i;
|
||||
}
|
||||
value = value / prefixMultipliers[i].value;
|
||||
return QString::number(truncateDigits(value, digits)) + ' ' + prefixMultipliers[i].shortText + unitText;
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "Cevo/StringTable.h"
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
void StringTable::setSize(QPoint value)
|
||||
{
|
||||
if (m_size == value)
|
||||
return;
|
||||
m_size = value;
|
||||
m_cells.resize(m_size.y());
|
||||
for (QVector<QString> &row : m_cells)
|
||||
row.resize(m_size.x());
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
@@ -0,0 +1,29 @@
|
||||
#include "Cevo/SyncCounter.h"
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
int SyncCounter::allocate()
|
||||
{
|
||||
QMutexLocker locker(&m_lock);
|
||||
return m_top++;
|
||||
}
|
||||
|
||||
bool SyncCounter::currentEqualTo(int value) const
|
||||
{
|
||||
QMutexLocker locker(&m_lock);
|
||||
return value == m_current;
|
||||
}
|
||||
|
||||
void SyncCounter::increment()
|
||||
{
|
||||
QMutexLocker locker(&m_lock);
|
||||
++m_current;
|
||||
}
|
||||
|
||||
void SyncCounter::assign(const SyncCounter &source)
|
||||
{
|
||||
m_current = source.m_current;
|
||||
m_top = source.m_top;
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
@@ -0,0 +1,428 @@
|
||||
#include "Cevo/Table.h"
|
||||
|
||||
#include <QDomDocument>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QTreeWidget>
|
||||
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
namespace {
|
||||
|
||||
QString quoteString(const QString &text, const QString "e)
|
||||
{
|
||||
return text.isEmpty() ? text : quote + text + quote;
|
||||
}
|
||||
|
||||
QString escapeHtml(const QString &text)
|
||||
{
|
||||
QString result = text;
|
||||
result.replace('&', QStringLiteral("&")); // must come first
|
||||
result.replace('<', QStringLiteral("<"));
|
||||
result.replace('>', QStringLiteral(">"));
|
||||
return result;
|
||||
}
|
||||
|
||||
// Parses one CSV line (comma-separated, "-quoted fields with "" as the
|
||||
// escaped-quote sequence) - matches Lazarus TStringList's
|
||||
// StrictDelimiter+DelimitedText convention the original relied on.
|
||||
QStringList parseCsvLine(const QString &line)
|
||||
{
|
||||
QStringList fields;
|
||||
QString field;
|
||||
bool inQuotes = false;
|
||||
int i = 0;
|
||||
while (i < line.size()) {
|
||||
const QChar c = line.at(i);
|
||||
if (inQuotes) {
|
||||
if (c == '"') {
|
||||
if (i + 1 < line.size() && line.at(i + 1) == '"') {
|
||||
field += '"';
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
inQuotes = false;
|
||||
++i;
|
||||
} else {
|
||||
field += c;
|
||||
++i;
|
||||
}
|
||||
} else if (c == '"') {
|
||||
inQuotes = true;
|
||||
++i;
|
||||
} else if (c == ',') {
|
||||
fields.append(field);
|
||||
field.clear();
|
||||
++i;
|
||||
} else {
|
||||
field += c;
|
||||
++i;
|
||||
}
|
||||
}
|
||||
fields.append(field);
|
||||
return fields;
|
||||
}
|
||||
|
||||
QStringList splitLines(const QString &text)
|
||||
{
|
||||
QStringList lines = text.split('\n');
|
||||
while (!lines.isEmpty() && lines.last().trimmed().isEmpty())
|
||||
lines.removeLast();
|
||||
return lines;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
QString tableFormatText(TableFormat format)
|
||||
{
|
||||
switch (format) {
|
||||
case TableFormat::Excel:
|
||||
return QStringLiteral("Excel paste");
|
||||
case TableFormat::Plain:
|
||||
return QStringLiteral("Plain text");
|
||||
case TableFormat::Csv:
|
||||
return QStringLiteral("CSV");
|
||||
case TableFormat::Html:
|
||||
return QStringLiteral("HTML");
|
||||
case TableFormat::ListView:
|
||||
return QStringLiteral("ListView");
|
||||
case TableFormat::MediaWiki:
|
||||
return QStringLiteral("MediaWiki");
|
||||
case TableFormat::Xml:
|
||||
return QStringLiteral("XML");
|
||||
case TableFormat::Json:
|
||||
return QStringLiteral("JSON");
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
QString tableFormatExt(TableFormat format)
|
||||
{
|
||||
switch (format) {
|
||||
case TableFormat::Excel:
|
||||
case TableFormat::Plain:
|
||||
case TableFormat::MediaWiki:
|
||||
return QStringLiteral(".txt");
|
||||
case TableFormat::Csv:
|
||||
return QStringLiteral(".csv");
|
||||
case TableFormat::Html:
|
||||
return QStringLiteral(".htm");
|
||||
case TableFormat::ListView:
|
||||
return QString();
|
||||
case TableFormat::Xml:
|
||||
return QStringLiteral(".xml");
|
||||
case TableFormat::Json:
|
||||
return QStringLiteral(".json");
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
void Table::clear()
|
||||
{
|
||||
columns.clear();
|
||||
rows.clear();
|
||||
}
|
||||
|
||||
Row &Table::addRow()
|
||||
{
|
||||
rows.push_back(std::make_unique<Row>());
|
||||
return *rows.back();
|
||||
}
|
||||
|
||||
QString Table::outputTabs() const
|
||||
{
|
||||
QString result = columns.join('\t') + '\n';
|
||||
for (const auto &row : rows)
|
||||
result += row->cells.join('\t') + '\n';
|
||||
return result;
|
||||
}
|
||||
|
||||
QString Table::outputPlain() const
|
||||
{
|
||||
QString result;
|
||||
for (const auto &row : rows)
|
||||
result += row->cells.join('\n') + QStringLiteral("\n\n===========================\n\n");
|
||||
return result;
|
||||
}
|
||||
|
||||
QString Table::outputCsv() const
|
||||
{
|
||||
QString result;
|
||||
for (int j = 0; j < columns.size(); ++j) {
|
||||
if (j > 0)
|
||||
result += ',';
|
||||
if (!columns.at(j).isEmpty())
|
||||
result += quoteString(columns.at(j), QStringLiteral("\""));
|
||||
}
|
||||
result += '\n';
|
||||
for (const auto &row : rows) {
|
||||
for (int j = 0; j < row->cells.size(); ++j) {
|
||||
if (j > 0)
|
||||
result += ',';
|
||||
if (!row->cells.at(j).isEmpty())
|
||||
result += quoteString(row->cells.at(j), QStringLiteral("\""));
|
||||
}
|
||||
result += '\n';
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
QString Table::outputXml() const
|
||||
{
|
||||
QDomDocument doc;
|
||||
doc.appendChild(
|
||||
doc.createProcessingInstruction(QStringLiteral("xml"), QStringLiteral("version=\"1.0\" encoding=\"UTF-8\"")));
|
||||
QDomElement tableElement = doc.createElement(QStringLiteral("table"));
|
||||
doc.appendChild(tableElement);
|
||||
if (!title.isEmpty()) {
|
||||
QDomElement titleElement = doc.createElement(QStringLiteral("title"));
|
||||
titleElement.appendChild(doc.createTextNode(title));
|
||||
tableElement.appendChild(titleElement);
|
||||
}
|
||||
QDomElement rowsElement = doc.createElement(QStringLiteral("rows"));
|
||||
tableElement.appendChild(rowsElement);
|
||||
for (const auto &row : rows) {
|
||||
QDomElement rowElement = doc.createElement(QStringLiteral("row"));
|
||||
rowsElement.appendChild(rowElement);
|
||||
for (int j = 0; j < row->cells.size(); ++j) {
|
||||
if (row->cells.at(j).isEmpty())
|
||||
continue;
|
||||
QDomElement cellElement = doc.createElement(QStringLiteral("cell"));
|
||||
cellElement.setAttribute(QStringLiteral("name"), columns.value(j));
|
||||
cellElement.appendChild(doc.createTextNode(row->cells.at(j)));
|
||||
rowElement.appendChild(cellElement);
|
||||
}
|
||||
}
|
||||
return doc.toString(2);
|
||||
}
|
||||
|
||||
QString Table::outputHtml() const
|
||||
{
|
||||
QString result = QStringLiteral("<html>\n <head>\n <title>") + escapeHtml(title) +
|
||||
QStringLiteral("</title>\n </head>\n <body>\n <table border=\"1\">\n <tr>\n");
|
||||
for (const QString &column : columns)
|
||||
result += QStringLiteral(" <th>") + escapeHtml(column).replace('\n', QStringLiteral("<br/>")) +
|
||||
QStringLiteral("</th>\n");
|
||||
result += QStringLiteral(" </tr>\n");
|
||||
for (const auto &row : rows) {
|
||||
result += QStringLiteral(" <tr>\n");
|
||||
for (const QString &cell : row->cells)
|
||||
result += QStringLiteral(" <td>") + escapeHtml(cell).replace('\n', QStringLiteral("<br/>")) +
|
||||
QStringLiteral("</td>\n");
|
||||
result += QStringLiteral(" </tr>\n");
|
||||
}
|
||||
result += QStringLiteral(" </table>\n </body>\n</html>");
|
||||
return result;
|
||||
}
|
||||
|
||||
QString Table::outputMediaWiki() const
|
||||
{
|
||||
QString result = QStringLiteral("{| class=\"wikitable sortable\"\n");
|
||||
for (int j = 0; j < columns.size(); ++j)
|
||||
result += (j == 0 ? QStringLiteral("! ") : QStringLiteral(" !! ")) + columns.at(j);
|
||||
result += QStringLiteral("\n|-\n");
|
||||
for (size_t i = 0; i < rows.size(); ++i) {
|
||||
const Row &row = *rows[i];
|
||||
for (int j = 0; j < row.cells.size(); ++j)
|
||||
result += (j == 0 ? QStringLiteral("| ") : QStringLiteral(" || ")) + row.cells.at(j);
|
||||
if (i + 1 < rows.size())
|
||||
result += QStringLiteral("\n|-\n");
|
||||
}
|
||||
result += QStringLiteral("\n|}");
|
||||
return result;
|
||||
}
|
||||
|
||||
QString Table::outputJson() const
|
||||
{
|
||||
QJsonArray array;
|
||||
for (const auto &row : rows) {
|
||||
QJsonObject object;
|
||||
for (int j = 0; j < row->cells.size() && j < columns.size(); ++j)
|
||||
if (!row->cells.at(j).isEmpty())
|
||||
object.insert(columns.at(j), row->cells.at(j));
|
||||
array.append(object);
|
||||
}
|
||||
return QString::fromUtf8(QJsonDocument(array).toJson(QJsonDocument::Indented));
|
||||
}
|
||||
|
||||
void Table::populateTreeWidget(QTreeWidget *tree) const
|
||||
{
|
||||
constexpr int minWidth = 120;
|
||||
tree->setColumnCount(columns.size());
|
||||
const int newWidth = std::max(minWidth, tree->width() / std::max(1, static_cast<int>(columns.size())));
|
||||
for (int i = 0; i < columns.size(); ++i) {
|
||||
tree->headerItem()->setText(i, columns.at(i));
|
||||
tree->setColumnWidth(i, newWidth);
|
||||
}
|
||||
tree->clear();
|
||||
for (const auto &row : rows) {
|
||||
auto *item = new QTreeWidgetItem(tree);
|
||||
for (int j = 0; j < row->cells.size(); ++j)
|
||||
item->setText(j, row->cells.at(j));
|
||||
}
|
||||
}
|
||||
|
||||
QString Table::output(TableFormat format) const
|
||||
{
|
||||
switch (format) {
|
||||
case TableFormat::Excel:
|
||||
return outputTabs();
|
||||
case TableFormat::Plain:
|
||||
return outputPlain();
|
||||
case TableFormat::Csv:
|
||||
return outputCsv();
|
||||
case TableFormat::Html:
|
||||
return outputHtml();
|
||||
case TableFormat::MediaWiki:
|
||||
return outputMediaWiki();
|
||||
case TableFormat::Xml:
|
||||
return outputXml();
|
||||
case TableFormat::Json:
|
||||
return outputJson();
|
||||
case TableFormat::ListView:
|
||||
break;
|
||||
}
|
||||
throw std::invalid_argument("Unsupported format");
|
||||
}
|
||||
|
||||
void Table::setInputCsv(const QString &text)
|
||||
{
|
||||
clear();
|
||||
const QStringList lines = splitLines(text);
|
||||
for (int i = 0; i < lines.size(); ++i) {
|
||||
if (i == 0)
|
||||
columns = parseCsvLine(lines.at(i).trimmed());
|
||||
else
|
||||
addRow().cells = parseCsvLine(lines.at(i).trimmed());
|
||||
}
|
||||
}
|
||||
|
||||
void Table::setInputMediaWiki(const QString &text)
|
||||
{
|
||||
clear();
|
||||
Row *row = nullptr;
|
||||
bool insideTable = false;
|
||||
for (const QString &rawLine : splitLines(text)) {
|
||||
const QString line = rawLine.trimmed();
|
||||
if (!insideTable) {
|
||||
if (line.startsWith(QStringLiteral("{|")))
|
||||
insideTable = true;
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith(QStringLiteral("|}"))) {
|
||||
insideTable = false;
|
||||
} else if (line.startsWith('!')) {
|
||||
QString remaining = line.mid(1).trimmed();
|
||||
for (;;) {
|
||||
const int index = remaining.indexOf(QStringLiteral("!!"));
|
||||
if (index < 0) {
|
||||
columns.append(remaining.trimmed());
|
||||
break;
|
||||
}
|
||||
columns.append(remaining.left(index).trimmed());
|
||||
remaining = remaining.mid(index + 2);
|
||||
}
|
||||
} else if (line.startsWith(QStringLiteral("|-"))) {
|
||||
row = &addRow();
|
||||
} else if (line.startsWith('|')) {
|
||||
if (row) {
|
||||
QString remaining = line.mid(1).trimmed();
|
||||
for (;;) {
|
||||
const int index = remaining.indexOf(QStringLiteral("||"));
|
||||
if (index < 0) {
|
||||
row->cells.append(remaining.trimmed());
|
||||
break;
|
||||
}
|
||||
row->cells.append(remaining.left(index).trimmed());
|
||||
remaining = remaining.mid(index + 2);
|
||||
}
|
||||
while (row->cells.size() < columns.size())
|
||||
row->cells.append(QString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Table::setInputJson(const QString &text)
|
||||
{
|
||||
clear();
|
||||
const QJsonDocument doc = QJsonDocument::fromJson(text.toUtf8());
|
||||
if (!doc.isArray())
|
||||
return;
|
||||
for (const QJsonValue &rowValue : doc.array()) {
|
||||
if (!rowValue.isObject())
|
||||
continue;
|
||||
const QJsonObject object = rowValue.toObject();
|
||||
Row &row = addRow();
|
||||
for (auto it = object.constBegin(); it != object.constEnd(); ++it) {
|
||||
int columnIndex = columns.indexOf(it.key());
|
||||
if (columnIndex < 0) {
|
||||
columns.append(it.key());
|
||||
columnIndex = columns.size() - 1;
|
||||
}
|
||||
while (row.cells.size() <= columnIndex)
|
||||
row.cells.append(QString());
|
||||
row.cells[columnIndex] = it.value().toVariant().toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Table::setInputXml(const QString &text)
|
||||
{
|
||||
clear();
|
||||
QDomDocument doc;
|
||||
if (!doc.setContent(text))
|
||||
return;
|
||||
const QDomElement tableElement = doc.documentElement();
|
||||
if (tableElement.tagName() != QStringLiteral("table"))
|
||||
return;
|
||||
QDomElement rowElement = tableElement.firstChildElement(QStringLiteral("rows")).firstChildElement(QStringLiteral("row"));
|
||||
while (!rowElement.isNull()) {
|
||||
Row &row = addRow();
|
||||
QDomElement cellElement = rowElement.firstChildElement(QStringLiteral("cell"));
|
||||
while (!cellElement.isNull()) {
|
||||
const QString cellName = cellElement.attribute(QStringLiteral("name"));
|
||||
int columnIndex = columns.indexOf(cellName);
|
||||
if (columnIndex < 0) {
|
||||
columns.append(cellName);
|
||||
columnIndex = columns.size() - 1;
|
||||
}
|
||||
while (row.cells.size() <= columnIndex)
|
||||
row.cells.append(QString());
|
||||
row.cells[columnIndex] = cellElement.text();
|
||||
cellElement = cellElement.nextSiblingElement(QStringLiteral("cell"));
|
||||
}
|
||||
rowElement = rowElement.nextSiblingElement(QStringLiteral("row"));
|
||||
}
|
||||
}
|
||||
|
||||
void Table::setInput(TableFormat format, const QString &text)
|
||||
{
|
||||
switch (format) {
|
||||
case TableFormat::Csv:
|
||||
setInputCsv(text);
|
||||
return;
|
||||
case TableFormat::MediaWiki:
|
||||
setInputMediaWiki(text);
|
||||
return;
|
||||
case TableFormat::Json:
|
||||
setInputJson(text);
|
||||
return;
|
||||
case TableFormat::Xml:
|
||||
setInputXml(text);
|
||||
return;
|
||||
default:
|
||||
throw std::invalid_argument("Unsupported format");
|
||||
}
|
||||
}
|
||||
|
||||
QList<TableFormat> Table::inputFormats()
|
||||
{
|
||||
return { TableFormat::Csv, TableFormat::Json, TableFormat::MediaWiki, TableFormat::Xml };
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
@@ -0,0 +1,47 @@
|
||||
#include "Cevo/TestCase.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QDebug>
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
QString testResultText(TestResult result)
|
||||
{
|
||||
switch (result) {
|
||||
case TestResult::None:
|
||||
return QCoreApplication::translate("TestCase", "None");
|
||||
case TestResult::Passed:
|
||||
return QCoreApplication::translate("TestCase", "Passed");
|
||||
case TestResult::Failed:
|
||||
return QCoreApplication::translate("TestCase", "Failed");
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
TestCase *TestCases::addNew(const QString &name, std::unique_ptr<TestCase> testCase)
|
||||
{
|
||||
testCase->name = name;
|
||||
TestCase *result = testCase.get();
|
||||
m_items.push_back(std::move(testCase));
|
||||
return result;
|
||||
}
|
||||
|
||||
void TestCases::run()
|
||||
{
|
||||
int passed = 0, failed = 0;
|
||||
for (const auto &testCase : m_items) {
|
||||
qDebug().noquote() << "== " << testCase->name << " ======= ";
|
||||
testCase->initialize();
|
||||
testCase->run();
|
||||
testCase->finalize();
|
||||
if (testCase->testResult == TestResult::Passed)
|
||||
++passed;
|
||||
else if (testCase->testResult == TestResult::Failed)
|
||||
++failed;
|
||||
}
|
||||
for (const auto &testCase : m_items)
|
||||
qDebug().noquote() << testCase->name << ": " << testResultText(testCase->testResult);
|
||||
qDebug().noquote() << "Total: " << count() << ", Passed: " << passed << ", Failed: " << failed;
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
@@ -0,0 +1,55 @@
|
||||
#include "Cevo/Threading.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QEventLoop>
|
||||
#include <QThread>
|
||||
|
||||
#include <exception>
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
void runInThread(const std::function<void()> &method)
|
||||
{
|
||||
std::exception_ptr failure;
|
||||
QThread *thread = QThread::create([&method, &failure]() {
|
||||
try {
|
||||
method();
|
||||
} catch (...) {
|
||||
failure = std::current_exception();
|
||||
}
|
||||
});
|
||||
|
||||
if (QThread::currentThread() == qApp->thread()) {
|
||||
QEventLoop loop;
|
||||
QObject::connect(thread, &QThread::finished, &loop, &QEventLoop::quit);
|
||||
thread->start();
|
||||
loop.exec();
|
||||
} else {
|
||||
thread->start();
|
||||
thread->wait();
|
||||
}
|
||||
delete thread;
|
||||
|
||||
if (failure)
|
||||
std::rethrow_exception(failure);
|
||||
}
|
||||
|
||||
void runInThreadAsync(const std::function<void()> &method, const std::function<void()> &callback)
|
||||
{
|
||||
QThread *thread = QThread::create(method);
|
||||
if (callback)
|
||||
QObject::connect(thread, &QThread::finished, thread, callback);
|
||||
QObject::connect(thread, &QThread::finished, thread, &QThread::deleteLater);
|
||||
thread->start();
|
||||
}
|
||||
|
||||
void synchronizeToMainThread(const std::function<void()> &method)
|
||||
{
|
||||
if (QThread::currentThread() == qApp->thread()) {
|
||||
method();
|
||||
return;
|
||||
}
|
||||
QMetaObject::invokeMethod(qApp, method, Qt::BlockingQueuedConnection);
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
@@ -0,0 +1,111 @@
|
||||
#include "Cevo/XML.h"
|
||||
|
||||
#include <QFile>
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Cevo {
|
||||
|
||||
namespace {
|
||||
void writeChild(QDomElement &node, const QString &name, const QString &text)
|
||||
{
|
||||
QDomElement child = node.ownerDocument().createElement(name);
|
||||
child.appendChild(node.ownerDocument().createTextNode(text));
|
||||
node.appendChild(child);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void writeInteger(QDomElement &node, const QString &name, int value)
|
||||
{
|
||||
writeChild(node, name, QString::number(value));
|
||||
}
|
||||
|
||||
void writeInt64(QDomElement &node, const QString &name, qint64 value)
|
||||
{
|
||||
writeChild(node, name, QString::number(value));
|
||||
}
|
||||
|
||||
void writeBoolean(QDomElement &node, const QString &name, bool value)
|
||||
{
|
||||
writeChild(node, name, value ? QStringLiteral("True") : QStringLiteral("False"));
|
||||
}
|
||||
|
||||
void writeString(QDomElement &node, const QString &name, const QString &value)
|
||||
{
|
||||
writeChild(node, name, value);
|
||||
}
|
||||
|
||||
void writeDateTime(QDomElement &node, const QString &name, const QDateTime &value)
|
||||
{
|
||||
writeChild(node, name, value.toString(Qt::ISODateWithMs));
|
||||
}
|
||||
|
||||
void writeDouble(QDomElement &node, const QString &name, double value)
|
||||
{
|
||||
writeChild(node, name, QString::number(value));
|
||||
}
|
||||
|
||||
int readInteger(const QDomElement &node, const QString &name, int defaultValue)
|
||||
{
|
||||
const QDomElement child = node.firstChildElement(name);
|
||||
return child.isNull() ? defaultValue : child.text().toInt();
|
||||
}
|
||||
|
||||
qint64 readInt64(const QDomElement &node, const QString &name, qint64 defaultValue)
|
||||
{
|
||||
const QDomElement child = node.firstChildElement(name);
|
||||
return child.isNull() ? defaultValue : child.text().toLongLong();
|
||||
}
|
||||
|
||||
bool readBoolean(const QDomElement &node, const QString &name, bool defaultValue)
|
||||
{
|
||||
const QDomElement child = node.firstChildElement(name);
|
||||
return child.isNull() ? defaultValue : (child.text().compare(QStringLiteral("True"), Qt::CaseInsensitive) == 0);
|
||||
}
|
||||
|
||||
QString readString(const QDomElement &node, const QString &name, const QString &defaultValue)
|
||||
{
|
||||
const QDomElement child = node.firstChildElement(name);
|
||||
return child.isNull() ? defaultValue : child.text();
|
||||
}
|
||||
|
||||
QDateTime readDateTime(const QDomElement &node, const QString &name, const QDateTime &defaultValue)
|
||||
{
|
||||
const QDomElement child = node.firstChildElement(name);
|
||||
if (child.isNull())
|
||||
return defaultValue;
|
||||
const QDateTime value = QDateTime::fromString(child.text(), Qt::ISODateWithMs);
|
||||
return value.isValid() ? value : defaultValue;
|
||||
}
|
||||
|
||||
double readDouble(const QDomElement &node, const QString &name, double defaultValue)
|
||||
{
|
||||
const QDomElement child = node.firstChildElement(name);
|
||||
return child.isNull() ? defaultValue : child.text().toDouble();
|
||||
}
|
||||
|
||||
QDomDocument readXmlFile(const QString &fileName)
|
||||
{
|
||||
QFile file(fileName);
|
||||
if (!file.open(QIODevice::ReadOnly))
|
||||
throw std::runtime_error(("Could not open " + fileName).toStdString());
|
||||
QDomDocument doc;
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
|
||||
const QDomDocument::ParseResult result = doc.setContent(&file);
|
||||
if (!result)
|
||||
throw std::runtime_error(QStringLiteral("XML parse error at %1:%2: %3")
|
||||
.arg(result.errorLine)
|
||||
.arg(result.errorColumn)
|
||||
.arg(result.errorMessage)
|
||||
.toStdString());
|
||||
#else
|
||||
QString errorMessage;
|
||||
int errorLine = 0, errorColumn = 0;
|
||||
if (!doc.setContent(&file, &errorMessage, &errorLine, &errorColumn))
|
||||
throw std::runtime_error(
|
||||
QStringLiteral("XML parse error at %1:%2: %3").arg(errorLine).arg(errorColumn).arg(errorMessage).toStdString());
|
||||
#endif
|
||||
return doc;
|
||||
}
|
||||
|
||||
} // namespace Cevo
|
||||
Reference in new issue
Block a user