Debian Codegraph @

src/qutecombobox.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 "qutecombobox.h"
24
25 QuteComboBox::QuteComboBox(QWidget *parent) : QuteWidget(parent)
26 {
27 m_widget = new QComboBox(this);
28 auto w = static_cast<QComboBox*>(m_widget);
29 // Since combobox menus use native widgets we should not mess with
30 // styling and let the underlying engine handle that.
31 #ifdef Q_OS_LINUX
32 w->setStyleSheet("padding-left: 2px;");
33 #endif
34 m_widget->setContextMenuPolicy(Qt::NoContextMenu);
35 m_widget->setMouseTracking(true); // Necessary to pass mouse tracking to widget panel for _MouseX channels
36 // canFocus(false);
37 // connect((QComboBox *)m_widget, SIGNAL(released()), this, SLOT(buttonReleased()));
38 connect(static_cast<QComboBox *>(m_widget), SIGNAL(currentIndexChanged(int)),
39 this, SLOT(indexChanged(int)));
40 setProperty("QCS_selectedIndex", 0);
41 setProperty("QCS_randomizable", false);
42 setProperty("QCS_randomizableGroup", 0);
43 }
44
45 QuteComboBox::~QuteComboBox()
46 {
47 }
48
49 QString QuteComboBox::getWidgetLine()
50 {
51 #ifdef USE_WIDGET_MUTEX
52 widgetLock.lockForRead();
53 #endif
54 QString line = "ioMenu {" + QString::number(x()) + ", " + QString::number(y()) + "} ";
55 line += "{"+ QString::number(width()) +", "+ QString::number(height()) +"} ";
56 line += QString::number(((QComboBox *)m_widget)->currentIndex()) + " ";
57 line += "303 ";
58 line += "\"" + itemList() + "\" ";
59 line += m_channel;
60 #ifdef USE_WIDGET_MUTEX
61 widgetLock.unlock();
62 #endif
63 return line;
64 }
65
66 QString QuteComboBox::getCabbageLine()
67 {
68 #ifdef USE_WIDGET_MUTEX
69 widgetLock.lockForRead();
70 #endif
71 QString line = "combobox channel(\"" + m_channel + "\"), ";
72 line += QString("bounds(%1,%2,%3,%4), ").arg(x()).arg(y()).arg(width()).arg(height());
73 line += "value(" + QString::number(m_value) + "), ";
74 line += "items(\"" + itemList() + "\")";
75 if (property("QCS_midicc").toInt() >= 0 && property("QCS_midichan").toInt()>0) { // insert only if midi channel is above 0
76 line += ", midiCtrl(\"" + QString::number(property("QCS_midichan").toInt()) + ",";
77 line += QString::number(property("QCS_midicc").toInt()) + "\")";
78 }
79 #ifdef USE_WIDGET_MUTEX
80 widgetLock.unlock();
81 #endif
82 return line;
83 }
84
85 QString QuteComboBox::getWidgetXmlText()
86 {
87 xmlText = "";
88 QXmlStreamWriter s(&xmlText);
89 createXmlWriter(s);
90
91 #ifdef USE_WIDGET_MUTEX
92 widgetLock.lockForRead();
93 #endif
94
95 s.writeStartElement("bsbDropdownItemList");
96 for (int i = 0; i < static_cast<QComboBox *>(m_widget)->count(); i++) {
97 s.writeStartElement("bsbDropdownItem");
98 s.writeTextElement("name", static_cast<QComboBox *>(m_widget)->itemText(i));
99 s.writeTextElement("value", QString::number(static_cast<QComboBox *>(m_widget)->itemData(i).toInt()) );
100 s.writeTextElement("stringvalue", stringValues[i]);
101 s.writeEndElement();
102 }
103 s.writeEndElement();
104 s.writeTextElement("selectedIndex", QString::number(((QComboBox *)m_widget)->currentIndex()));
105 s.writeStartElement("randomizable");
106 s.writeAttribute("group", QString::number(property("QCS_randomizableGroup").toInt()));
107 s.writeCharacters(property("QCS_randomizable").toBool() ? "true": "false");
108 s.writeEndElement();
109 s.writeEndElement();
110 #ifdef USE_WIDGET_MUTEX
111 widgetLock.unlock();
112 #endif
113 return xmlText;
114 }
115
116 QString QuteComboBox::getWidgetType()
117 {
118 return QString("BSBDropdown");
119 }
120
121 QString QuteComboBox::getQml()
122 {
123 QString qml = QString();
124 #ifdef USE_WIDGET_MUTEX
125 widgetLock.lockForWrite();
126 #endif
127
128 // create model
129 QString list = "";
130 for (int i = 0; i < static_cast<QComboBox *>(m_widget)->count(); i++) {
131 list += "\"" + static_cast<QComboBox *>(m_widget)->itemText(i) + "\",";
132 }
133 list.chop(1); //remove last comma
134
135 qml = QString(R"(
136 ComboBox {
137 property string channel: "%1"
138 x: %2 * scaleItem.scale
139 y: %3 * scaleItem.scale
140 width: %4 * scaleItem.scale
141 height: %5 * scaleItem.scale
142 model: [ %6 ]
143 currentIndex: %7
144 onCurrentIndexChanged: if (typeof(csound)!='undefined') csound.setControlChannel(channel, currentIndex);
145 }
146 )").arg(m_channel).arg(x()).arg(y()).arg(width()).arg(height()).
147 arg(list).arg(((QComboBox *)m_widget)->currentIndex());
148 #ifdef USE_WIDGET_MUTEX
149 widgetLock.unlock();
150 #endif
151 return qml;
152 }
153
154 void QuteComboBox::setMidiValue(int value)
155 {
156 QComboBox * cb = static_cast<QComboBox *>(m_widget);
157
158 int index = int (value/127.0 * (cb->count()-1));
159 cb->setCurrentIndex(index);
160
161 // not sure if this is necessary
162 double newval = static_cast<double>(index);
163 setValue( newval );
164 QPair<QString, double> channelValue(m_channel, newval);
165 emit newValue(channelValue);
166
167 }
168
169 QString QuteComboBox::itemList()
170 {
171 // For old format
172 QString list = "";
173 for (int i = 0; i < static_cast<QComboBox *>(m_widget)->count(); i++) {
174 list += static_cast<QComboBox *>(m_widget)->itemText(i) + ",";
175 }
176 list.chop(1); //remove last comma
177 return list;
178 }
179
180 void QuteComboBox::setText(QString text)
181 {
182 #ifdef USE_WIDGET_MUTEX
183 widgetLock.lockForWrite();
184 #endif
185 clearItems();
186 QStringList items = text.split(",");
187 int counter = 0;
188 foreach (QString item, items) {
189 addItem(item, counter++, "");
190 }
191 #ifdef USE_WIDGET_MUTEX
192 widgetLock.unlock();
193 #endif
194 }
195
196 void QuteComboBox::clearItems()
197 {
198 m_widget->blockSignals(true);
199 static_cast<QComboBox *>(m_widget)->clear();
200 m_widget->blockSignals(false);
201 }
202
203 void QuteComboBox::addItem(QString text, double value, QString stringvalue)
204 {
205 m_widget->blockSignals(true);
206 static_cast<QComboBox *>(m_widget)->addItem(text, value);
207 stringValues.append(stringvalue);
208 m_widget->blockSignals(false);
209 }
210
211 void QuteComboBox::refreshWidget()
212 {
213 #ifdef USE_WIDGET_MUTEX
214 widgetLock.lockForRead();
215 #endif
216 int val = (int) m_value;
217 m_valueChanged = false;
218 #ifdef USE_WIDGET_MUTEX
219 widgetLock.unlock();
220 #endif
221 m_widget->blockSignals(true);
222 static_cast<QComboBox *>(m_widget)->setCurrentIndex(val);
223 m_widget->blockSignals(false);
224 }
225
226 void QuteComboBox::applyInternalProperties()
227 {
228 QuteWidget::applyInternalProperties();
229 setValue(property("QCS_selectedIndex").toInt());
230 }
231
232 void QuteComboBox::createPropertiesDialog()
233 {
234 QuteWidget::createPropertiesDialog();
235 dialog->setWindowTitle("Menu");
236 QLabel *label = new QLabel(dialog);
237
238 auto spacer = new QSpacerItem(20, 20, QSizePolicy::Expanding);
239 layout->addItem(spacer, 4, 4, Qt::AlignLeft);
240
241 label = new QLabel(dialog);
242 label->setText("Items (comma-separated)");
243 layout->addWidget(label, 5, 0, Qt::AlignRight|Qt::AlignVCenter);
244
245 text = new QLineEdit(dialog);
246 text->setToolTip("Items in the menu, separated by commas");
247 text->setMinimumWidth(420);
248 text->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
249 layout->addWidget(text, 5,1,1,4, Qt::AlignLeft|Qt::AlignVCenter);
250 // text->setSizePolicy()
251 #ifdef USE_WIDGET_MUTEX
252 widgetLock.lockForRead();
253 #endif
254 text->setText(itemList());
255 #ifdef USE_WIDGET_MUTEX
256 widgetLock.unlock();
257 #endif
258 }
259
260 void QuteComboBox::applyProperties()
261 {
262 setText(text->text()); // This line can't be locked, as itlocks itself internally
263
264 //#ifdef USE_WIDGET_MUTEX
265 //#endif
266 // widgetLock.lockForWrite();
267 // widgetLock.unlock();
268 // setWidgetGeometry(xSpinBox->value(), ySpinBox->value(), wSpinBox->value(), hSpinBox->value());
269 QuteWidget::applyProperties(); //Must be last to make sure the widgetChanged signal is last
270 }
271
272
273 void QuteComboBox::indexChanged(int value)
274 {
275 // qDebug() << "QuteComboBox::indexChanged" << value;
276
277 #ifdef USE_WIDGET_MUTEX
278 widgetLock.lockForRead();
279 #endif
280 m_value = value;
281 m_valueChanged = true;
282 QPair<QString, double> channelValue(m_channel, m_value);
283 #ifdef USE_WIDGET_MUTEX
284 widgetLock.unlock();
285 #endif
286
287 emit newValue(channelValue);
288 }