Debian Codegraph @

src/quteslider.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2008, 2009 Andres Cabrera
3 [email protected]
4
5 This file is part of CsoundQt.
6
7 CsoundQt is free software; you can redistribute it
8 and/or modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 CsoundQt is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with Csound; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA
21 */
22
23 #include "quteslider.h"
24 #ifdef Q_OS_MAC
25 #include "myslider.h"
26 #endif
27
28 QuteSlider::QuteSlider(QWidget *parent) : QuteWidget(parent)
29 {
30 #ifdef Q_OS_MAC
31 m_widget = new MySlider(this);
32 #else
33 m_widget = new QSlider(this);
34 #endif
35
36 m_widget->setContextMenuPolicy(Qt::NoContextMenu);
37 m_widget->setMouseTracking(true); // Necessary to pass mouse tracking to widget panel for _MouseX channels
38 canFocus(false);
39 if (width() > height()) {
40 static_cast<QSlider *>(m_widget)->setOrientation(Qt::Horizontal);
41 } else {
42 static_cast<QSlider *>(m_widget)->setOrientation(Qt::Vertical);
43 }
44
45 connect(static_cast<QSlider *>(m_widget), SIGNAL(valueChanged(int)), this, SLOT(sliderChanged(int)));
46
47 setProperty("QCS_minimum", 0.0);
48 setProperty("QCS_maximum", 1.0);
49 setProperty("QCS_value", 0.0);
50 setProperty("QCS_mode", "lin");
51 setProperty("QCS_mouseControl", "continuous");
52 setProperty("QCS_mouseControlAct", "jump");
53 setProperty("QCS_resolution", -1.0);
54 setProperty("QCS_randomizable", false);
55 setProperty("QCS_randomizableGroup", 0);
56 }
57
58 QuteSlider::~QuteSlider()
59 {
60 }
61
62 //void QuteSlider::sliderMoved(int value)
63 //{
64 // QuteWidget::valueChanged(m_value);
65 //}
66
67 void QuteSlider::setValue(double value)
68 {
69 #ifdef USE_WIDGET_MUTEX
70 widgetLock.lockForWrite();
71 #endif
72 setInternalValue(value);
73 #ifdef USE_WIDGET_MUTEX
74 widgetLock.unlock();
75 #endif
76 }
77
78 void QuteSlider::setMidiValue(int value)
79 { //qDebug()<<Q_FUNC_INFO<<value;
80 double max = property("QCS_maximum").toDouble();
81 double min = property("QCS_minimum").toDouble();
82 double newval = min + ((value / 127.0)* (max - min));
83 setValue(newval);
84 QPair<QString, double> channelValue(m_channel, newval);
85 emit newValue(channelValue);
86 }
87
88 void QuteSlider::refreshWidget()
89 {
90 #ifdef USE_WIDGET_MUTEX
91 widgetLock.lockForRead();
92 #endif
93 // qDebug() << "QuteSlider::refreshWidget " << m_value;
94 double min = property("QCS_minimum").toDouble();
95 double max = property("QCS_maximum").toDouble();
96 int val = (int) (m_len * (m_value - min)/(max- min));
97 m_valueChanged = false;
98 #ifdef USE_WIDGET_MUTEX
99 widgetLock.unlock();
100 #endif
101 m_widget->blockSignals(true);
102 static_cast<QSlider *>(m_widget)->setValue(val);
103 m_widget->blockSignals(false);
104 }
105
106 void QuteSlider::applyInternalProperties()
107 {
108 QuteWidget::applyInternalProperties();
109 // qDebug() << "QuteSlider::applyInternalProperties()";
110
111 m_value = property("QCS_value").toDouble();
112 // m_value2 = property("QCS_value2").toDouble();
113 // m_stringValue = property("QCS_stringValue").toString();
114 double max = property("QCS_maximum").toDouble();
115 double min = property("QCS_minimum").toDouble();
116 if (max < min) {
117 double temp = max;
118 max = min;
119 min = temp;
120 }
121 }
122
123 void QuteSlider::setWidgetGeometry(int x, int y, int w, int h)
124 {
125 QuteWidget::setWidgetGeometry(x,y,w,h);
126 m_widget->blockSignals(true);
127 if (width() > height()) {
128 static_cast<QSlider *>(m_widget)->setOrientation(Qt::Horizontal);
129 static_cast<QSlider *>(m_widget)->setMaximum(w);
130 m_len = w;
131 }
132 else {
133 static_cast<QSlider *>(m_widget)->setOrientation(Qt::Vertical);
134 static_cast<QSlider *>(m_widget)->setMaximum(h);
135 m_len = h;
136 }
137 m_widget->blockSignals(false);
138 }
139
140 QString QuteSlider::getWidgetLine()
141 {
142 #ifdef USE_WIDGET_MUTEX
143 widgetLock.lockForRead();
144 #endif
145 QString line = "ioSlider {" + QString::number(x()) + ", " + QString::number(y()) + "} ";
146 line += "{"+ QString::number(width()) +", "+ QString::number(height()) +"} ";
147 line += QString::number(property("QCS_minimum").toDouble(), 'f', 6) + " ";
148 line += QString::number(property("QCS_maximum").toDouble(), 'f', 6) + " ";
149 line += QString::number(m_value, 'f', 6) + " " + m_channel;
150 // qDebug("QuteSlider::getWidgetLine() %s", line.toStdString().c_str());
151 #ifdef USE_WIDGET_MUTEX
152 widgetLock.unlock();
153 #endif
154 return line;
155 }
156
157 QString QuteSlider::getCabbageLine()
158 {
159 #ifdef USE_WIDGET_MUTEX
160 widgetLock.lockForWrite();
161 #endif
162 QString line = "";
163 if (m_widget->width() > m_widget->height()) {
164 line += "hslider ";
165 }
166 else {
167 line += "vslider ";
168 }
169 line += "channel(\"" + m_channel + "\"), ";
170 line += QString("bounds(%1,%2,%3,%4), ").arg(x()).arg(y()).arg(width()).arg(height());
171 line += QString("range(%1,%2,%3) " ).arg(property("QCS_minimum").toDouble()).arg(property("QCS_maximum").toDouble()).arg(m_value);
172 //line += QString(", text(%1), ").arg(m_channel); // Is it good idea to put channel as name - not really since geometry probably does not allow it
173 if (property("QCS_midicc").toInt() >= 0 && property("QCS_midichan").toInt()>0) { // insert only if midi channel is above 0
174 line += ", midiCtrl(\"" + QString::number(property("QCS_midichan").toInt()) + ",";
175 line += QString::number(property("QCS_midicc").toInt()) + "\")";
176 }
177 #ifdef USE_WIDGET_MUTEX
178 widgetLock.unlock();
179 #endif
180 return line;
181 }
182
183
184 QString QuteSlider::getQml()
185 {
186 QString qml = QString();
187 #ifdef USE_WIDGET_MUTEX
188 widgetLock.lockForWrite();
189 #endif
190 qml = "\n\tSlider {\n";
191 //qml += QString("\t\t\id: %1Slider\n").arg(m_channel);
192 qml += QString("\t\tx: %1 * scaleItem.scale\n").arg(x());
193 qml += QString("\t\ty: %1 * scaleItem.scale\n").arg(y());
194 qml += QString("\t\twidth: %1 * scaleItem.scale\n").arg(width());
195 qml += QString("\t\theight: %1 * scaleItem.scale\n").arg(height());
196 qml += QString("\t\tfrom: %1\n").arg(property("QCS_minimum").toString());
197 qml += QString("\t\tto: %1\n").arg(property("QCS_maximum").toString());
198 qml += QString("\t\tvalue: %1\n").arg(getValue());
199 if ( width() > height() ) {
200 qml += "\t\torientation: Qt.Horizontal\n";
201 } else {
202 qml += "\t\torientation: Qt.Vertical\n";
203 }
204 qml += QString("\t\tonPositionChanged: csound.setControlChannel(\"%1\", valueAt(position))\n").arg(m_channel); // NB! this is for QtQuick.Controls 2! since onValueChanged works onlu on drag end
205
206 qml += "\t}";
207 #ifdef USE_WIDGET_MUTEX
208 widgetLock.unlock();
209 #endif
210
211 return qml;
212 }
213
214 QString QuteSlider::getCsladspaLine()
215 {
216 #ifdef USE_WIDGET_MUTEX
217 widgetLock.lockForRead();
218 #endif
219 QString line = "ControlPort=" + m_channel + "|" + m_channel + "\n";
220 line += "Range=" + QString::number(property("QCS_minimum").toDouble(), 'f', 8)
221 + "|" + QString::number(property("QCS_maximum").toDouble(), 'f', 8);
222
223 #ifdef USE_WIDGET_MUTEX
224 widgetLock.unlock();
225 #endif
226 return line;
227 }
228
229 QString QuteSlider::getWidgetXmlText()
230 {
231 xmlText = "";
232 QXmlStreamWriter s(&xmlText);
233 createXmlWriter(s);
234
235 #ifdef USE_WIDGET_MUTEX
236 widgetLock.lockForWrite();
237 #endif
238 s.writeTextElement("minimum", QString::number(property("QCS_minimum").toDouble(), 'f', 8));
239 s.writeTextElement("maximum", QString::number(property("QCS_maximum").toDouble(), 'f', 8));
240 s.writeTextElement("value", QString::number(m_value, 'f', 8));
241 s.writeTextElement("mode", property("QCS_mode").toString());
242
243 s.writeStartElement("mouseControl");
244 s.writeAttribute("act", property("QCS_mouseControlAct").toString());
245 s.writeCharacters(property("QCS_mouseControl").toString());
246 s.writeEndElement();
247 s.writeTextElement("resolution", QString::number(property("QCS_resolution").toDouble(), 'f', 8));
248 s.writeStartElement("randomizable");
249 s.writeAttribute("group", QString::number(property("QCS_randomizableGroup").toInt()));
250 s.writeCharacters(property("QCS_randomizable").toBool() ? "true": "false");
251 s.writeEndElement();
252
253 s.writeEndElement();
254 #ifdef USE_WIDGET_MUTEX
255 widgetLock.unlock();
256 #endif
257 return xmlText;
258 }
259
260
261 QString QuteSlider::getWidgetType()
262 {
263 return (width() > height()? QString("BSBHSlider"):QString("BSBVSlider"));
264 }
265
266 void QuteSlider::createPropertiesDialog()
267 {
268 QuteWidget::createPropertiesDialog();
269 dialog->setWindowTitle("Slider");
270 QLabel *label = new QLabel(dialog);
271 label->setText("Min =");
272 layout->addWidget(label, 2, 0, Qt::AlignRight|Qt::AlignVCenter);
273 minSpinBox = new QDoubleSpinBox(dialog);
274 minSpinBox->setDecimals(6);
275 minSpinBox->setRange(-99999.0, 99999.0);
276 layout->addWidget(minSpinBox, 2,1, Qt::AlignLeft|Qt::AlignVCenter);
277 label = new QLabel(dialog);
278 label->setText("Max =");
279 layout->addWidget(label, 2, 2, Qt::AlignRight|Qt::AlignVCenter);
280 maxSpinBox = new QDoubleSpinBox(dialog);
281 maxSpinBox->setDecimals(6);
282 maxSpinBox->setRange(-99999.0, 99999.0);
283 layout->addWidget(maxSpinBox, 2,3, Qt::AlignLeft|Qt::AlignVCenter);
284 #ifdef USE_WIDGET_MUTEX
285 widgetLock.lockForWrite();
286 #endif
287 minSpinBox->setValue(property("QCS_minimum").toDouble());
288 maxSpinBox->setValue(property("QCS_maximum").toDouble());
289 // setProperty("QCS_value", m_value);
290 #ifdef USE_WIDGET_MUTEX
291 widgetLock.unlock();
292 #endif
293 }
294
295 void QuteSlider::applyProperties()
296 {
297 #ifdef USE_WIDGET_MUTEX
298 widgetLock.lockForWrite();
299 #endif
300 setProperty("QCS_maximum", maxSpinBox->value());
301 setProperty("QCS_minimum", minSpinBox->value());
302 #ifdef USE_WIDGET_MUTEX
303 widgetLock.unlock();
304 #endif
305 QuteWidget::applyProperties(); // Must be last to make sure the widgetChanged signal is last
306 }
307
308 void QuteSlider::sliderChanged(int value)
309 {
310 #ifdef USE_WIDGET_MUTEX
311 widgetLock.lockForRead();
312 #endif
313 double normalized = (double) value / (double) m_len;
314 double min = property("QCS_minimum").toDouble();
315 double max = property("QCS_maximum").toDouble();
316 double scaledValue = min + (normalized * (max-min));
317 setInternalValue(scaledValue);
318 QPair<QString, double> channelValue(m_channel, m_value);
319 #ifdef USE_WIDGET_MUTEX
320 widgetLock.unlock();
321 #endif
322 emit newValue(channelValue);
323 }
324
325 void QuteSlider::setInternalValue(double value)
326 {
327 double max = property("QCS_maximum").toDouble();
328 double min = property("QCS_minimum").toDouble();
329 if (value > max)
330 m_value = max;
331 else if (value < min)
332 m_value = min;
333 else
334 m_value = value;
335 m_valueChanged = true;
336 // setProperty("QCS_value", m_value);
337 }