File: VariableEditor.cpp

package info (click to toggle)
structure-synth 1.0.0-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,072 kB
  • ctags: 1,176
  • sloc: cpp: 7,070; python: 167; makefile: 66; lisp: 25
file content (162 lines) | stat: -rw-r--r-- 4,305 bytes parent folder | download
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
#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 {

		/// The supported types of variables.
		enum VariableType { StringVariable, FloatVariable };

		/// A widget editor for a constant variable.
		class VariableWidget : public QWidget {
		public:
			/// FloatVariable constructor.
			VariableWidget(QWidget* parent, QString name) : QWidget(parent), name(name) {};

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

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


	

		/// A widget editor for a constant 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);
					//setLayout(l);
					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;
		};


		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);

						//INFO("Creating : " + ps[i]->getName());
						substitutions[name] = fw->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;
		}

			

	}
}