Make use of Qt's MetaEnum for serialization of enums
parent
d59b85e4ba
commit
709c322046
47
geometry.cpp
47
geometry.cpp
|
@ -1,37 +1,28 @@
|
||||||
#include "geometry.h"
|
#include "geometry.h"
|
||||||
|
|
||||||
|
|
||||||
QMap<Geometry::Format, QString> Geometry::geometryIdNameMap;
|
const QMetaEnum Geometry::metaEnumFormat = QMetaEnum::fromType<Geometry::Format>();
|
||||||
QMap<QString, Geometry::Format> Geometry::geometryNameIdMap;
|
const QRegularExpression Geometry::corners = QRegularExpression("(-?\\d*),(-?\\d*):(-?\\d*),(-?\\d*)");
|
||||||
const Geometry::InitStatics initStatics;
|
const QRegularExpression Geometry::centerDimension = QRegularExpression("(-?\\d*),(-?\\d*):(-?\\d*)x(-?\\d*)");
|
||||||
|
const QRegularExpression Geometry::cornerDimension = QRegularExpression("(-?\\d*)[,:](-?\\d*)[+-](-?\\d*)[+-](-?\\d*)");
|
||||||
|
const QRegularExpression Geometry::cornerDimensionAlternate = QRegularExpression("(\\d*)x(\\d*)[+]?(-?\\d+)?[+]?(-?\\d+)?");
|
||||||
|
|
||||||
Geometry::InitStatics::InitStatics()
|
const char *Geometry::formatName(Geometry::Format id)
|
||||||
{
|
|
||||||
Geometry::geometryIdNameMap[Geometry::FormatUnknown] = "unknown";
|
|
||||||
Geometry::geometryIdNameMap[Geometry::FormatNone] = "none";
|
|
||||||
Geometry::geometryIdNameMap[Geometry::CenterDimensions] = "center-dimensions";
|
|
||||||
Geometry::geometryIdNameMap[Geometry::CornerDimensions] = "corner-dimensions";
|
|
||||||
Geometry::geometryIdNameMap[Geometry::Corners] = "corners";
|
|
||||||
Geometry::geometryIdNameMap[Geometry::FormatCustom] = "custom";
|
|
||||||
|
|
||||||
for (int i = Geometry::FormatNone; i < Geometry::FormatMax; i++)
|
|
||||||
Geometry::geometryNameIdMap[Geometry::geometryIdNameMap[static_cast<Format>(i)]] = static_cast<Format>(i);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
const QString &Geometry::formatName(Geometry::Format id)
|
|
||||||
{
|
{
|
||||||
if (id < Geometry::FormatNone || id >= Geometry::FormatMax)
|
if (id < Geometry::FormatNone || id >= Geometry::FormatMax)
|
||||||
id = Geometry::FormatUnknown;
|
id = Geometry::FormatUnknown;
|
||||||
return geometryIdNameMap[id];
|
return metaEnumFormat.key(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
Geometry::Format Geometry::formatId(const QString &name)
|
Geometry::Format Geometry::formatId(const QString &name)
|
||||||
{
|
{
|
||||||
if (geometryNameIdMap.find(name) == geometryNameIdMap.end())
|
return formatId(name.toLatin1().data());
|
||||||
return Geometry::FormatUnknown;
|
}
|
||||||
else
|
|
||||||
return geometryNameIdMap[name];
|
Geometry::Format Geometry::formatId(const char *name)
|
||||||
|
{
|
||||||
|
// keyToValue returns -1 for not found, which equals FormatUnknown
|
||||||
|
return static_cast<Format>(metaEnumFormat.keyToValue(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
Geometry::Format Geometry::set(const QString &str)
|
Geometry::Format Geometry::set(const QString &str)
|
||||||
|
@ -39,11 +30,9 @@ Geometry::Format Geometry::set(const QString &str)
|
||||||
qDebug()<<"Trying to detect format of "<<str;
|
qDebug()<<"Trying to detect format of "<<str;
|
||||||
QRegularExpressionMatch match;
|
QRegularExpressionMatch match;
|
||||||
if(str.isEmpty()){
|
if(str.isEmpty()){
|
||||||
qDebug()<<"format is FormatNone";
|
|
||||||
format = FormatNone;
|
format = FormatNone;
|
||||||
}
|
}
|
||||||
else if((match =corners.match(str)).hasMatch()) {
|
else if((match =corners.match(str)).hasMatch()) {
|
||||||
qDebug()<<"format is Corners";
|
|
||||||
corner[0][0] = match.captured(1).toInt();
|
corner[0][0] = match.captured(1).toInt();
|
||||||
corner[0][1] = match.captured(2).toInt();
|
corner[0][1] = match.captured(2).toInt();
|
||||||
corner[1][0] = match.captured(3).toInt();
|
corner[1][0] = match.captured(3).toInt();
|
||||||
|
@ -55,7 +44,6 @@ Geometry::Format Geometry::set(const QString &str)
|
||||||
format = Geometry::Corners;
|
format = Geometry::Corners;
|
||||||
}
|
}
|
||||||
else if((match =centerDimension.match(str)).hasMatch()){
|
else if((match =centerDimension.match(str)).hasMatch()){
|
||||||
qDebug()<<"format is CenterDimensions";
|
|
||||||
center[0] = match.captured(1).toInt();
|
center[0] = match.captured(1).toInt();
|
||||||
center[1] = match.captured(2).toInt();
|
center[1] = match.captured(2).toInt();
|
||||||
dimension[0] = match.captured(3).toInt();
|
dimension[0] = match.captured(3).toInt();
|
||||||
|
@ -70,7 +58,6 @@ Geometry::Format Geometry::set(const QString &str)
|
||||||
format = Geometry::CenterDimensions;
|
format = Geometry::CenterDimensions;
|
||||||
}
|
}
|
||||||
else if((match = cornerDimension.match(str)).hasMatch()){
|
else if((match = cornerDimension.match(str)).hasMatch()){
|
||||||
qDebug()<<"format is CornerDimensions";
|
|
||||||
corner[0][0] = match.captured(1).toInt();
|
corner[0][0] = match.captured(1).toInt();
|
||||||
corner[0][1] = match.captured(2).toInt();
|
corner[0][1] = match.captured(2).toInt();
|
||||||
dimension[0] = match.captured(3).toInt();
|
dimension[0] = match.captured(3).toInt();
|
||||||
|
@ -85,9 +72,7 @@ Geometry::Format Geometry::set(const QString &str)
|
||||||
format = Geometry::CornerDimensions;
|
format = Geometry::CornerDimensions;
|
||||||
}
|
}
|
||||||
else if((match = cornerDimensionAlternate.match(str)).hasMatch()){
|
else if((match = cornerDimensionAlternate.match(str)).hasMatch()){
|
||||||
qDebug() << "format is <width>x<height>[<+|-xoffset><+|-yoffset>]";
|
|
||||||
if(match.lastCapturedIndex() ==2){
|
if(match.lastCapturedIndex() ==2){
|
||||||
qDebug() << "format is CenterDimensions with center =0,0";
|
|
||||||
center[0] = 0;
|
center[0] = 0;
|
||||||
center[1] = 0;
|
center[1] = 0;
|
||||||
dimension[0] = match.captured(1).toInt();
|
dimension[0] = match.captured(1).toInt();
|
||||||
|
@ -102,7 +87,6 @@ Geometry::Format Geometry::set(const QString &str)
|
||||||
format = Geometry::CenterDimensions;
|
format = Geometry::CenterDimensions;
|
||||||
}
|
}
|
||||||
else if(match.lastCapturedIndex() ==4){
|
else if(match.lastCapturedIndex() ==4){
|
||||||
qDebug() << "format is CornerDimensions";
|
|
||||||
corner[0][0] = match.captured(3).toInt();
|
corner[0][0] = match.captured(3).toInt();
|
||||||
corner[0][1] = match.captured(4).toInt();
|
corner[0][1] = match.captured(4).toInt();
|
||||||
dimension[0] = match.captured(1).toInt();
|
dimension[0] = match.captured(1).toInt();
|
||||||
|
@ -122,7 +106,8 @@ Geometry::Format Geometry::set(const QString &str)
|
||||||
qDebug()<<"Warning: Could not parse format of string: "<<str;
|
qDebug()<<"Warning: Could not parse format of string: "<<str;
|
||||||
format = Geometry::FormatCustom;
|
format = Geometry::FormatCustom;
|
||||||
}
|
}
|
||||||
|
// Thanks to Qt's meta enum qDebug makes pretty printing of format
|
||||||
|
qDebug() << "Format is:" << format;
|
||||||
return format;
|
return format;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
33
geometry.h
33
geometry.h
|
@ -3,13 +3,17 @@
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QMap>
|
#include <QMap>
|
||||||
|
#include <QMetaEnum>
|
||||||
|
#include <QObject>
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
|
|
||||||
class Geometry
|
class Geometry : public QObject
|
||||||
{
|
{
|
||||||
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
enum Format {
|
enum Format
|
||||||
FormatKeep = -2, // Special value used when setting geometry: don't change current format.
|
{
|
||||||
|
FormatKeep = -2, // Special value used when setting geometry: don't change current format.
|
||||||
FormatUnknown = -1,
|
FormatUnknown = -1,
|
||||||
FormatNone = 0,
|
FormatNone = 0,
|
||||||
CenterDimensions = 1,
|
CenterDimensions = 1,
|
||||||
|
@ -18,14 +22,12 @@ public:
|
||||||
FormatCustom,
|
FormatCustom,
|
||||||
FormatMax
|
FormatMax
|
||||||
};
|
};
|
||||||
static QMap<Geometry::Format, QString> geometryIdNameMap;
|
Q_ENUM(Format)
|
||||||
static QMap<QString, Geometry::Format> geometryNameIdMap;
|
static const QMetaEnum metaEnumFormat;
|
||||||
|
|
||||||
static const QString &formatName(Geometry::Format id);
|
static const char *formatName(Geometry::Format id);
|
||||||
static Geometry::Format formatId(const QString &name);
|
static Geometry::Format formatId(const QString &name);
|
||||||
struct InitStatics { InitStatics(void); };
|
static Geometry::Format formatId(const char *name);
|
||||||
static const InitStatics initStatics;
|
|
||||||
friend struct InitStatics;
|
|
||||||
|
|
||||||
int center[2];
|
int center[2];
|
||||||
int dimension[2];
|
int dimension[2];
|
||||||
|
@ -34,7 +36,7 @@ public:
|
||||||
Geometry(void) { setMax(); }
|
Geometry(void) { setMax(); }
|
||||||
Geometry(const char *s) { set(s); }
|
Geometry(const char *s) { set(s); }
|
||||||
Geometry(const QString &s) { set(s); }
|
Geometry(const QString &s) { set(s); }
|
||||||
//Geometry(const Geometry &g);
|
// Geometry(const Geometry &g);
|
||||||
|
|
||||||
Geometry::Format set(const QString &str);
|
Geometry::Format set(const QString &str);
|
||||||
void setMax(void);
|
void setMax(void);
|
||||||
|
@ -42,15 +44,16 @@ public:
|
||||||
void setCornerDimensions(int cx, int cy, int dx, int dy);
|
void setCornerDimensions(int cx, int cy, int dx, int dy);
|
||||||
void setCorners(int c0x, int c0y, int c1x, int c1y);
|
void setCorners(int c0x, int c0y, int c1x, int c1y);
|
||||||
QString getString(Geometry::Format format = Geometry::FormatNone);
|
QString getString(Geometry::Format format = Geometry::FormatNone);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const QRegularExpression corners = QRegularExpression("(-?\\d*),(-?\\d*):(-?\\d*),(-?\\d*)");
|
static const QRegularExpression corners;
|
||||||
const QRegularExpression centerDimension = QRegularExpression("(-?\\d*),(-?\\d*):(-?\\d*)x(-?\\d*)");
|
static const QRegularExpression centerDimension;
|
||||||
const QRegularExpression cornerDimension = QRegularExpression("(-?\\d*)[,:](-?\\d*)[+-](-?\\d*)[+-](-?\\d*)");
|
static const QRegularExpression cornerDimension;
|
||||||
const QRegularExpression cornerDimensionAlternate = QRegularExpression("(\\d*)x(\\d*)[+]?(-?\\d+)?[+]?(-?\\d+)?");
|
static const QRegularExpression cornerDimensionAlternate;
|
||||||
bool adjustCorners(void);
|
bool adjustCorners(void);
|
||||||
void computeCorner0(void);
|
void computeCorner0(void);
|
||||||
void computeCorner1(void);
|
void computeCorner1(void);
|
||||||
void computeCenter(void); // Depends dimensions to be correct !
|
void computeCenter(void); // Depends dimensions to be correct !
|
||||||
void computeDimensions(void);
|
void computeDimensions(void);
|
||||||
|
|
||||||
Format format = FormatNone;
|
Format format = FormatNone;
|
||||||
|
|
Loading…
Reference in New Issue