Skip to content

Commit 99af8f2

Browse files
committed
Initial commit
1 parent 6ce7b97 commit 99af8f2

File tree

11 files changed

+2595
-0
lines changed

11 files changed

+2595
-0
lines changed

argaction.cpp

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#include "argaction.h"
2+
3+
argAction::argAction()
4+
{
5+
6+
}
7+
8+
argAction::~argAction()
9+
{
10+
11+
}
12+
13+
bool argAction::setDoubleSpinBox(void *doubleSpinBox, QString argValue)
14+
{
15+
bool ok;
16+
double value = argValue.toDouble(&ok);
17+
18+
if (ok)
19+
static_cast<QDoubleSpinBox *>(doubleSpinBox)->setValue(value);
20+
21+
return ok;
22+
}
23+
24+
bool argAction::setSpinBox(void *spinBox, QString argValue)
25+
{
26+
bool ok;
27+
int value = argValue.toInt(&ok, 10);
28+
29+
if (ok)
30+
static_cast<QSpinBox *>(spinBox)->setValue(value);
31+
32+
return ok;
33+
}
34+
35+
bool argAction::setCheckBox(void *checkBox, QString argValue)
36+
{
37+
if(argValue == "1" || argValue.compare("true", Qt::CaseInsensitive) == 0)
38+
{
39+
static_cast<QCheckBox *>(checkBox)->setChecked(true);
40+
return true;
41+
}
42+
else if (argValue == "0" || argValue.compare("false", Qt::CaseInsensitive) == 0)
43+
{
44+
static_cast<QCheckBox *>(checkBox)->setChecked(false);
45+
return true;
46+
}
47+
else
48+
return false;
49+
}
50+
51+
bool argAction::setLineEdit(void *lineEdit, QString argValue)
52+
{
53+
static_cast<QLineEdit *>(lineEdit)->setText(argValue);
54+
55+
return true;
56+
}
57+
58+
bool argAction::setComboBox(void *comboBox, QString argValue)
59+
{
60+
int index = static_cast<QComboBox *>(comboBox)->findText(argValue, Qt::MatchFixedString);
61+
62+
if( index >= 0 )
63+
{
64+
static_cast<QComboBox *>(comboBox)->setCurrentIndex(index);
65+
return true;
66+
}
67+
else
68+
return false;
69+
}
70+
71+
bool argAction::setButtonGroup(void *buttonGroup, QString argValue)
72+
{
73+
if(argValue == "1" || argValue.compare("true", Qt::CaseInsensitive) == 0)
74+
{
75+
static_cast<QButtonGroup *>(buttonGroup)->button(0)->setChecked(true);
76+
return true;
77+
}
78+
else if(argValue == "0" || argValue.compare("false", Qt::CaseInsensitive) == 0)
79+
{
80+
static_cast<QButtonGroup *>(buttonGroup)->button(1)->setChecked(true);
81+
return true;
82+
}
83+
else
84+
return false;
85+
}
86+
87+
QString argAction::getDoubleSpinBox(void *doubleSpinBox)
88+
{
89+
return QString::number(static_cast<QDoubleSpinBox *>(doubleSpinBox)->value(), 'f', 4);
90+
}
91+
92+
QString argAction::getSpinBox(void *spinBox)
93+
{
94+
return QString::number(static_cast<QSpinBox *>(spinBox)->value(), 10);
95+
}
96+
97+
QString argAction::getCheckBox(void *checkBox)
98+
{
99+
if(static_cast<QCheckBox *>(checkBox)->isChecked())
100+
return "true";
101+
else
102+
return "false";
103+
}
104+
105+
QString argAction::getLineEdit(void *lineEdit)
106+
{
107+
return static_cast<QLineEdit *>(lineEdit)->text();
108+
}
109+
110+
QString argAction::getComboBox(void *comboBox)
111+
{
112+
return static_cast<QComboBox *>(comboBox)->currentText();
113+
}
114+
115+
QString argAction::getButtonGroup(void *buttonGroup)
116+
{
117+
if(static_cast<QButtonGroup *>(buttonGroup)->button(0)->isChecked())
118+
return "true";
119+
else
120+
return "false";
121+
}
122+
123+
bool argAction::setValue(QString key, QString value)
124+
{
125+
argElement element;
126+
127+
if( argList.contains(key) )
128+
{
129+
element = argList.value(key);
130+
return element.setFunction( element.object, value );
131+
}
132+
133+
return false;
134+
}
135+
136+
QStringList argAction::getAllArgs(bool getCommentedOptions, bool getDisabledObjects)
137+
{
138+
QStringList output;
139+
QString value;
140+
141+
for(QHash<QString, argElement>::const_iterator i = argList.constBegin(); i != argList.constEnd(); i++)
142+
{
143+
if( getDisabledObjects || static_cast<QWidget *>(i.value().object)->isEnabled() )
144+
{
145+
value = i.value().getFunction( i.value().object );
146+
if ( !value.isEmpty() )
147+
{
148+
if(i.value().commentedOption)
149+
{
150+
if(getCommentedOptions)
151+
output << "#@!" + i.key() + '=' + value;
152+
}
153+
else
154+
output << i.key() + '=' + value;
155+
}
156+
}
157+
}
158+
159+
return output;
160+
}

argaction.h

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#ifndef ARGACTION_H
2+
#define ARGACTION_H
3+
4+
#include <QHash>
5+
#include <QString>
6+
#include <QStringList>
7+
#include <QDoubleSpinBox>
8+
#include <QSpinBox>
9+
#include <QCheckBox>
10+
#include <QButtonGroup>
11+
#include <QLineEdit>
12+
#include <QComboBox>
13+
14+
class argAction
15+
{
16+
protected:
17+
18+
static bool setDoubleSpinBox(void *doubleSpinBox, QString argValue);
19+
static bool setSpinBox(void *spinBox, QString argValue);
20+
static bool setCheckBox(void *checkBox, QString argValue);
21+
static bool setLineEdit(void *lineEdit, QString argValue);
22+
static bool setComboBox(void *comboBox, QString argValue);
23+
static bool setButtonGroup(void *buttonGroup, QString argValue);
24+
static QString getDoubleSpinBox(void *doubleSpinBox);
25+
static QString getSpinBox(void *spinBox);
26+
static QString getCheckBox(void *checkBox);
27+
static QString getLineEdit(void *lineEdit);
28+
static QString getComboBox(void *comboBox);
29+
static QString getButtonGroup(void *buttonGroup);
30+
31+
struct argElement
32+
{
33+
bool (*setFunction)(void *object, QString argValue);
34+
QString (*getFunction)(void *object);
35+
void *object;
36+
bool commentedOption;
37+
};
38+
39+
QHash<QString,argElement> argList;
40+
41+
public:
42+
explicit argAction();
43+
~argAction();
44+
45+
bool setValue(QString key, QString value);
46+
47+
QStringList getAllArgs(bool getCommentedOptions = false, bool getDisabledObjects = false);
48+
49+
inline void insert( QString argName, QDoubleSpinBox *doubleSpinBox, bool commentedOption = false )
50+
{
51+
argList.insert( argName, (argElement){ .setFunction = &setDoubleSpinBox, .getFunction = &getDoubleSpinBox,
52+
.object = doubleSpinBox, .commentedOption = commentedOption } );
53+
}
54+
55+
inline void insert( QString argName, QSpinBox *spinBox, bool commentedOption = false )
56+
{
57+
argList.insert( argName, (argElement){ .setFunction = &setSpinBox, .getFunction = &getSpinBox,
58+
.object = spinBox, .commentedOption = commentedOption } );
59+
}
60+
61+
inline void insert( QString argName, QCheckBox *checkBox, bool commentedOption = false )
62+
{
63+
argList.insert( argName, (argElement){ .setFunction = &setCheckBox, .getFunction = &getCheckBox,
64+
.object = checkBox, .commentedOption = commentedOption } );
65+
}
66+
67+
inline void insert( QString argName, QLineEdit *lineEdit, bool commentedOption = false )
68+
{
69+
argList.insert( argName, (argElement){ .setFunction = &setLineEdit, .getFunction = &getLineEdit,
70+
.object = lineEdit, .commentedOption = commentedOption } );
71+
}
72+
73+
inline void insert( QString argName, QComboBox *comboBox, bool commentedOption = false )
74+
{
75+
argList.insert( argName, (argElement){ .setFunction = &setComboBox, .getFunction = &getComboBox,
76+
.object = comboBox, .commentedOption = commentedOption } );
77+
}
78+
79+
inline void insert( QString argName, QButtonGroup *buttonGroup, bool commentedOption = false )
80+
{
81+
argList.insert( argName, (argElement){ .setFunction = &setButtonGroup, .getFunction = &getButtonGroup,
82+
.object = buttonGroup, .commentedOption = commentedOption } );
83+
}
84+
85+
};
86+
87+
#endif // ARGACTION_H

main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "mainwindow.h"
2+
#include <QApplication>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
QApplication a(argc, argv);
7+
MainWindow w;
8+
w.show();
9+
10+
return a.exec();
11+
}

0 commit comments

Comments
 (0)