File: VariableEditor.cpp

package info (click to toggle)
structure-synth 1.5.0-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,268 kB
  • ctags: 1,966
  • sloc: cpp: 10,209; python: 164; makefile: 71; sh: 15
file content (180 lines) | stat: -rw-r--r-- 5,084 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "VariableEditor.h"

#include <QPushButton>
#include <QSlider>
#include <QDoubleSpinBox>
#include <QHBoxLayout>
#include <QLabel>

#include "../../SyntopiaCore/Logging/ListWidgetLogger.h"

using namespace SyntopiaCore::Logging;


namespace StructureSynth {
	namespace GUI {

	
		/// Widget editor base class.
		class VariableWidget : public QWidget {
		public:
			VariableWidget(QWidget* parent, QString name) : QWidget(parent), name(name) {};

			virtual QString getValueAsText() { return ""; };
			QString getName() const { return name; };
			bool isUpdated() const { return updated; };
			void setUpdated(bool value) { updated = value; };

		private:
			QString name;
			bool updated;
			QWidget* widget;
		};


	

		/// A widget editor for a float variable.
		class FloatWidget : public VariableWidget {
		public:
			/// FloatVariable constructor.
			FloatWidget(QWidget* parent, QString name, double defaultValue, double min, double max) 
				: VariableWidget(parent, name)  {
					QHBoxLayout* l = new QHBoxLayout(this);
					l->setSpacing(2);
					setContentsMargins (0,0,0,0);
					QLabel* label = new QLabel(this);
					label->setText(name);
					l->addWidget(label);
					comboSlider = new ComboSlider(parent, defaultValue, min, max);
					l->addWidget(comboSlider);
			};

			virtual QString getValueAsText() { return QString::number(comboSlider->getValue()); };

		private:
			ComboSlider* comboSlider;
		};


		class IntWidget : public VariableWidget {
		public:
			/// FloatVariable constructor.
			IntWidget(QWidget* parent, QString name, int defaultValue, int min, int max) 
				: VariableWidget(parent, name)  {
					QHBoxLayout* l = new QHBoxLayout(this);
					l->setSpacing(2);
					setContentsMargins (0,0,0,0);
					QLabel* label = new QLabel(this);
					label->setText(name);
					l->addWidget(label);
					comboSlider = new IntComboSlider(parent, defaultValue, min, max);
					l->addWidget(comboSlider);
			};

			virtual QString getValueAsText() { return QString::number(comboSlider->getValue()); };

		private:
			IntComboSlider* comboSlider;
		};


		VariableEditor::VariableEditor(QWidget* parent) : QWidget(parent) {
			layout = new QVBoxLayout(this);
			layout->setSpacing(1);
			spacer=0;
		};	

	
		QString VariableEditor::updateFromPreprocessor(Parser::Preprocessor* pp, QString in, bool* showGUI) {
			if (spacer) {
				layout->removeItem(spacer);
				delete(spacer);
				spacer = 0;
			}
			QVector<Parser::GuiParameter*> ps = pp->getParameters();
			QMap<QString, QString> substitutions;


			for (int i = 0; i < variables.count(); i++) {
				QString name = variables[i]->getName();
				variables[i]->setUpdated(false);
			}

			for (int i = 0; i < ps.count(); i++) {
				bool found = false;
				for (int j = 0; j < variables.count(); j++) {
					QString name = variables[j]->getName();
					if (name == ps[i]->getName()) {
						substitutions[name] = variables[j]->getValueAsText();
						found = true;
						variables[j]->setUpdated(true);
						//INFO("Found existing: " + variables[j]->getName() + QString(" value: %1").arg(variables[j]->getValueAsText()));
					}
				}

				if (!found) {
					if (dynamic_cast<Parser::FloatParameter*>(ps[i])) {
						Parser::FloatParameter* fp = dynamic_cast<Parser::FloatParameter*>(ps[i]);
						QString name = fp->getName();
						FloatWidget* fw = new FloatWidget(this, name, fp->getDefaultValue(), fp->getFrom(), fp->getTo());
						variables.append(fw);
						fw->setUpdated(true);
						layout->addWidget(fw);
						substitutions[name] = fw->getValueAsText();
					}

					if (dynamic_cast<Parser::IntParameter*>(ps[i])) {
						Parser::IntParameter* ip = dynamic_cast<Parser::IntParameter*>(ps[i]);
						QString name = ip->getName();
						IntWidget* iw = new IntWidget(this, name, ip->getDefaultValue(), ip->getFrom(), ip->getTo());
						variables.append(iw);
						iw->setUpdated(true);
						layout->addWidget(iw);
						substitutions[name] = iw->getValueAsText();
					}
				}
			}

			for (int i = 0; i < variables.count(); ) {
				if (!variables[i]->isUpdated()) {
					//INFO("Deleting : " + variables[i]->getName());
					delete(variables[i]);
					variables.remove(i);
					i = 0;

				} else {
					i++;
				}
			}

			if (showGUI) (*showGUI) = (variables.count() != 0);

			QMap<QString, QString>::const_iterator it2 = substitutions.constBegin();
			int subst = 0;
			while (it2 != substitutions.constEnd()) {
				if (subst>100) {
					WARNING("More than 100 recursive preprocessor substitutions... breaking.");
					break;
				}
				if (in.contains(it2.key())) {
					//INFO("Replacing: " + it2.key() + " with " + it2.value());
					in.replace(it2.key(), it2.value());

					it2 = substitutions.constBegin();
					subst++;
				} else {
					it2++;
				}
			}
			spacer = new QSpacerItem(1,1, QSizePolicy::Minimum,QSizePolicy::Expanding);
			layout->addItem(spacer);
		
			return in;
		}

			

	}
}