| 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 "quteknob.h" |
| 24 | |
| 25 | QVdial::~QVdial() {}; |
| 26 | |
| 27 | |
| 28 | void QVdial::mousePressEvent (QMouseEvent *event) { |
| 29 | if(!(event->button() & Qt::LeftButton)) { |
| 30 | return; |
| 31 | } |
| 32 | m_mouse_press_point = event->pos(); |
| 33 | m_dragging = true; |
| 34 | m_base_value = value(); |
| 35 | m_temp_scale_factor = event->modifiers() & Qt::AltModifier ? 0.25 : 1.0; |
| 36 | } |
| 37 | |
| 38 | void QVdial::mouseReleaseEvent (QMouseEvent *event) { |
| 39 | m_dragging = false; |
| 40 | } |
| 41 | |
| 42 | void QVdial::mouseMoveEvent (QMouseEvent *event) { |
| 43 | if (!m_dragging) |
| 44 | return; |
| 45 | int mouse_distance = m_mouse_press_point.y() - event->y(); |
| 46 | double delta = mouse_distance / (double)this->height() * (double)this->maximum(); |
| 47 | double fvalue = m_base_value + delta * m_temp_scale_factor * m_scale_factor; |
| 48 | setValue((int)fvalue); |
| 49 | } |
| 50 | |
| 51 | void QVdial::mouseDoubleClickEvent (QMouseEvent *event) { |
| 52 | if(!event->modifiers()) |
| 53 | emit doubleClick(); |
| 54 | else |
| 55 | this->QDial::mouseDoubleClickEvent(event); |
| 56 | } |
| 57 | |
| 58 | QPointF find_ellipse_coords(const QRectF &r, qreal angle) { |
| 59 | QPainterPath path; |
| 60 | path.arcMoveTo(r, angle); |
| 61 | return path.currentPosition(); |
| 62 | } |
| 63 | |
| 64 | void drawKnob(QPainter &painter, QVdial *knob, int border, QColor fg, QColor bg, QColor borderColor) { |
| 65 | int totaldegrees = knob->getDegreeRange(); |
| 66 | double relvalue = (double)knob->value() / knob->maximum(); |
| 67 | int steps = (int)(relvalue * totaldegrees); |
| 68 | |
| 69 | int const startAngle360 = 90 + (360 - totaldegrees) / 2; |
| 70 | int const startAngle = startAngle360; |
| 71 | QPoint center = knob->rect().center(); |
| 72 | |
| 73 | // fgPen.setCosmetic(true); |
| 74 | double r = qMin(knob->rect().height(), knob->rect().width()) / 2.0; |
| 75 | r = r * 0.97 - border; |
| 76 | double inner_r = r*0.67; |
| 77 | QRectF const rect(center.x() - r, center.y() - r, r * 2, r * 2); |
| 78 | QRectF outer_rect(center.x() - r, center.y() - r, r * 2, r * 2); |
| 79 | QRectF inner_rect(center.x() - inner_r, center.y() - inner_r, inner_r*2, inner_r*2); |
| 80 | |
| 81 | // background |
| 82 | // this could be cached, since it only changes when the knob is resized |
| 83 | QPainterPath bgpath; |
| 84 | |
| 85 | bgpath.arcMoveTo(outer_rect, -startAngle); |
| 86 | bgpath.arcTo(outer_rect, -startAngle, -totaldegrees); |
| 87 | bgpath.lineTo(find_ellipse_coords(inner_rect, -startAngle - totaldegrees)); |
| 88 | bgpath.arcTo(inner_rect, -startAngle-totaldegrees, totaldegrees); |
| 89 | bgpath.closeSubpath(); |
| 90 | painter.fillPath(bgpath, bg); |
| 91 | |
| 92 | // foreground |
| 93 | QPainterPath path; |
| 94 | path.arcMoveTo(outer_rect, -startAngle); |
| 95 | path.arcTo(outer_rect, -startAngle, -steps); |
| 96 | path.lineTo(find_ellipse_coords(inner_rect, -startAngle - steps)); |
| 97 | path.arcTo(inner_rect, -startAngle-steps, steps); |
| 98 | path.closeSubpath(); |
| 99 | |
| 100 | painter.fillPath(path, fg); |
| 101 | |
| 102 | // border at the end |
| 103 | if(border) { |
| 104 | QPen borderPen(borderColor, border, Qt::SolidLine, Qt::FlatCap); |
| 105 | if(border == 1) { |
| 106 | borderPen.setWidth(0); |
| 107 | borderPen.setCosmetic(true); |
| 108 | } |
| 109 | painter.setPen(borderPen); |
| 110 | painter.drawPath(bgpath); |
| 111 | } |
| 112 | |
| 113 | return ; |
| 114 | |
| 115 | } |
| 116 | |
| 117 | void QVdial::paintEvent(QPaintEvent *event) { |
| 118 | if(!m_flat) |
| 119 | return this->QDial::paintEvent(event); |
| 120 | |
| 121 | QPainter painter(this); |
| 122 | painter.setRenderHint(QPainter::Antialiasing); |
| 123 | QColor bgcolor = m_color.darker(330); |
| 124 | double r = qMin(this->rect().height(), this->rect().width()) / 2.0; |
| 125 | drawKnob(painter, this, m_border, m_color, bgcolor, m_bordercolor); |
| 126 | |
| 127 | if(m_draw_value && r >= 8) { |
| 128 | double fvalue = static_cast<double>(this->value()); |
| 129 | double display_value = (fvalue/this->maximum()) * |
| 130 | (m_display_max - m_display_min) + m_display_min; |
| 131 | int decimals = m_intDisplay ? 0 : m_decimals; |
| 132 | QString strValue = QString::number(display_value, 'f', decimals); |
| 133 | double numChars = static_cast<double>( strValue.size() ); |
| 134 | if(decimals > 0) { |
| 135 | // do not account for decimal . |
| 136 | numChars -= 0.4; |
| 137 | } |
| 138 | if(display_value < 0) { |
| 139 | numChars -= 0.5; |
| 140 | } |
| 141 | numChars = numChars > 4 ? numChars : 4; |
| 142 | double fontScale = 0.40 * 4/numChars; |
| 143 | int fontSize = (int) ((r - m_border) * fontScale); |
| 144 | if(fontSize >= 6) { |
| 145 | painter.setFont({"Helvetica", fontSize}); |
| 146 | painter.setPen(m_textcolor); |
| 147 | painter.drawText(this->rect(), Qt::AlignCenter, strValue); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | |
| 153 | // ------------------------------------------------------ |
| 154 | |
| 155 | |
| 156 | QuteKnob::QuteKnob(QWidget *parent) : QuteWidget(parent) |
| 157 | { |
| 158 | //TODO add resolution to config dialog and set these values accordingly |
| 159 | m_widget = new QVdial(this); |
| 160 | int const maximum = 10000; |
| 161 | auto w = static_cast<QVdial *>(m_widget); |
| 162 | w->setMinimum(0); |
| 163 | w->setMaximum(maximum); |
| 164 | w->setNotchTarget(maximum / 10); |
| 165 | w->setNotchesVisible(true); |
| 166 | w->setDrawValue(true); |
| 167 | |
| 168 | m_widget->setPalette(QPalette(Qt::gray)); |
| 169 | m_widget->setContextMenuPolicy(Qt::NoContextMenu); |
| 170 | // Necessary to pass mouse tracking to widget panel for _MouseX channels |
| 171 | m_widget->setMouseTracking(true); |
| 172 | setProperty("QCS_minimum", 0.0); |
| 173 | setProperty("QCS_maximum", 1.0); |
| 174 | setProperty("QCS_value", 0.0); |
| 175 | setProperty("QCS_mode", "lin"); |
| 176 | setProperty("QCS_mouseControl", "continuous"); |
| 177 | // setProperty("QCS_mouseControlAct", "jump"); |
| 178 | setProperty("QCS_resolution", 0.01); |
| 179 | setProperty("QCS_randomizable", false); |
| 180 | setProperty("QCS_randomizableGroup", 0); |
| 181 | setColor(QColor(245, 124, 0)); |
| 182 | setTextColor(QColor(81, 41, 0)); |
| 183 | auto borderColor = QColor(81, 41, 0); |
| 184 | setProperty("QCS_borderColor", borderColor.name()); |
| 185 | setProperty("QCS_border", 0); |
| 186 | w->setBorder(0, borderColor); |
| 187 | |
| 188 | setProperty("QCS_showvalue", true); |
| 189 | setProperty("QCS_flatstyle", true); |
| 190 | setProperty("QCS_integerMode", false); |
| 191 | connect(w, SIGNAL(valueChanged(int)), |
| 192 | this, SLOT(knobChanged(int))); |
| 193 | connect(w, SIGNAL(doubleClick()), |
| 194 | this, SLOT(setValueFromDialog())); |
| 195 | } |
| 196 | |
| 197 | QuteKnob::~QuteKnob() {} |
| 198 | |
| 199 | void QuteKnob::setRange(double min, double max) |
| 200 | { |
| 201 | // TODO when knob is resized, its internal range should be adjusted... |
| 202 | if (max < min) { |
| 203 | double temp = max; |
| 204 | max = min; |
| 205 | min = temp; |
| 206 | } |
| 207 | if (m_value > max) |
| 208 | m_value = max; |
| 209 | else if (m_value > min) |
| 210 | m_value = min; |
| 211 | setProperty("QCS_maximum", max); |
| 212 | setProperty("QCS_minimum", min); |
| 213 | m_valueChanged = true; |
| 214 | static_cast<QVdial *>(m_widget)->setDisplayRange(min, max); |
| 215 | |
| 216 | } |
| 217 | |
| 218 | void QuteKnob::setMidiValue(int value) |
| 219 | { |
| 220 | double max = property("QCS_maximum").toDouble(); |
| 221 | double min = property("QCS_minimum").toDouble(); |
| 222 | double newval= min + ((value / 127.0)* (max - min)); |
| 223 | setValue(newval); |
| 224 | QPair<QString, double> channelValue(m_channel, newval); |
| 225 | emit newValue(channelValue); |
| 226 | } |
| 227 | |
| 228 | void QuteKnob::refreshWidget() |
| 229 | { |
| 230 | #ifdef USE_WIDGET_MUTEX |
| 231 | widgetLock.lockForRead(); |
| 232 | #endif |
| 233 | auto w = static_cast<QVdial *>(m_widget); |
| 234 | double max = property("QCS_maximum").toDouble(); |
| 235 | double min = property("QCS_minimum").toDouble(); |
| 236 | |
| 237 | int val = (int)(w->maximum() * (m_value - min)/(max-min)); |
| 238 | m_valueChanged = false; |
| 239 | #ifdef USE_WIDGET_MUTEX |
| 240 | widgetLock.unlock(); |
| 241 | #endif |
| 242 | w->blockSignals(true); |
| 243 | w->setValue(val); |
| 244 | w->blockSignals(false); |
| 245 | } |
| 246 | |
| 247 | void QuteKnob::applyInternalProperties() |
| 248 | { |
| 249 | QuteWidget::applyInternalProperties(); |
| 250 | setValue(property("QCS_value").toDouble()); |
| 251 | auto w = static_cast<QVdial*>(m_widget); |
| 252 | |
| 253 | w->setColor(property("QCS_color").value<QColor>()); |
| 254 | w->setTextColor(QColor(property("QCS_textcolor").toString())); |
| 255 | w->setDrawValue(property("QCS_showvalue").toBool()); |
| 256 | w->setFlatStyle(property("QCS_flatstyle").toBool()); |
| 257 | w->setIntegerMode(property("QCS_integerMode").toBool()); |
| 258 | w->setDisplayRange(property("QCS_minimum").toDouble(), |
| 259 | property("QCS_maximum").toDouble()); |
| 260 | w->setBorder(property("QCS_border").toInt(), property("QCS_borderColor").toString()); |
| 261 | } |
| 262 | |
| 263 | void QuteKnob::setWidgetGeometry(int x, int y, int width, int height) |
| 264 | { |
| 265 | QuteWidget::setWidgetGeometry(x,y,width,height); |
| 266 | m_widget->blockSignals(true); |
| 267 | m_widget->setFixedSize(width, height); |
| 268 | m_widget->blockSignals(false); |
| 269 | } |
| 270 | |
| 271 | QString QuteKnob::getWidgetLine() |
| 272 | { |
| 273 | #ifdef USE_WIDGET_MUTEX |
| 274 | widgetLock.lockForRead(); |
| 275 | #endif |
| 276 | QString line = "ioKnob {" + QString::number(x()) + ", " + QString::number(y()) + "} "; |
| 277 | line += "{"+ QString::number(width()) +", "+ QString::number(height()) +"} "; |
| 278 | line += QString::number(property("QCS_maximum").toDouble(), 'f', 6) + " "; |
| 279 | line += QString::number(property("QCS_minimum").toDouble(), 'f', 6) + " "; |
| 280 | line += QString::number(property("QCS_resolution").toDouble(), 'f', 6) + " "; |
| 281 | line += QString::number(m_value, 'f', 6) + " " + m_channel; |
| 282 | // qDebug("QuteKnob::getWidgetLine() %s", line.toStdString().c_str()); |
| 283 | #ifdef USE_WIDGET_MUTEX |
| 284 | widgetLock.unlock(); |
| 285 | #endif |
| 286 | return line; |
| 287 | } |
| 288 | |
| 289 | QString QuteKnob::getCsladspaLine() |
| 290 | { |
| 291 | #ifdef USE_WIDGET_MUTEX |
| 292 | widgetLock.lockForRead(); |
| 293 | #endif |
| 294 | QString line = "ControlPort=" + m_channel + "|" + m_channel + "\n"; |
| 295 | line += "Range=" + QString::number(property("QCS_minimum").toDouble(), 'f', 8); |
| 296 | line += "|" + QString::number(property("QCS_maximum").toDouble(), 'f', 8); |
| 297 | #ifdef USE_WIDGET_MUTEX |
| 298 | widgetLock.unlock(); |
| 299 | #endif |
| 300 | return line; |
| 301 | } |
| 302 | |
| 303 | |
| 304 | QString QuteKnob::getCabbageLine() |
| 305 | { |
| 306 | #ifdef USE_WIDGET_MUTEX |
| 307 | widgetLock.lockForWrite(); |
| 308 | #endif |
| 309 | QString line = "rslider "; |
| 310 | line += "channel(\"" + m_channel + "\"), "; |
| 311 | line += QString("bounds(%1,%2,%3,%4), ").arg(x()).arg(y()).arg(width()).arg(height()); |
| 312 | line += QString("range(%1,%2,%3), ").arg(property("QCS_minimum").toDouble()).arg(property("QCS_maximum").toDouble()).arg(m_value); |
| 313 | // insert only if midi channel is above 0 |
| 314 | if (property("QCS_midicc").toInt() >= 0 && property("QCS_midichan").toInt() > 0) { |
| 315 | line += ", midiCtrl(\"" + QString::number(property("QCS_midichan").toInt()) + ","; |
| 316 | line += QString::number(property("QCS_midicc").toInt()) + "\")"; |
| 317 | } |
| 318 | #ifdef USE_WIDGET_MUTEX |
| 319 | widgetLock.unlock(); |
| 320 | #endif |
| 321 | return line; |
| 322 | } |
| 323 | |
| 324 | QString QuteKnob::getQml() |
| 325 | { |
| 326 | QString qml = QString(); |
| 327 | #ifdef USE_WIDGET_MUTEX |
| 328 | widgetLock.lockForWrite(); |
| 329 | #endif |
| 330 | qml = "\n\tDial {\n"; |
| 331 | //qml += QString("\t\tid: %1Dial\n").arg(m_channel); |
| 332 | qml += QString("\t\tx: %1 * scaleItem.scale\n").arg(x()); |
| 333 | qml += QString("\t\ty: %1 * scaleItem.scale\n").arg(y()); |
| 334 | qml += QString("\t\twidth: %1 * scaleItem.scale\n").arg(width()); |
| 335 | qml += QString("\t\theight: %1 * scaleItem.scale\n").arg(height()); |
| 336 | qml += QString("\t\tfrom: %1\n").arg(property("QCS_minimum").toString()); |
| 337 | qml += QString("\t\tto: %1\n").arg(property("QCS_maximum").toString()); |
| 338 | qml += QString("\t\tvalue: %1\n").arg(getValue()); |
| 339 | qml += QString("\t\tonPositionChanged: csound.setControlChannel(\"%1\", from + position*(to-from))\n").arg(m_channel); // NB! this is for QtQuick.Controls 2! since onValueChanged works onlu on drag end |
| 340 | |
| 341 | qml += "\t}"; |
| 342 | #ifdef USE_WIDGET_MUTEX |
| 343 | widgetLock.unlock(); |
| 344 | #endif |
| 345 | |
| 346 | return qml; |
| 347 | |
| 348 | } |
| 349 | |
| 350 | QString QuteKnob::getWidgetXmlText() |
| 351 | { |
| 352 | xmlText = ""; |
| 353 | QXmlStreamWriter s(&xmlText); |
| 354 | createXmlWriter(s); |
| 355 | #ifdef USE_WIDGET_MUTEX |
| 356 | widgetLock.lockForRead(); |
| 357 | #endif |
| 358 | |
| 359 | s.writeTextElement("minimum", QString::number(property("QCS_minimum").toDouble(), 'f', 8)); |
| 360 | s.writeTextElement("maximum", QString::number(property("QCS_maximum").toDouble(), 'f', 8)); |
| 361 | s.writeTextElement("value", QString::number(m_value, 'f', 8)); |
| 362 | s.writeTextElement("mode", property("QCS_mode").toString()); |
| 363 | s.writeStartElement("mouseControl"); |
| 364 | s.writeAttribute("act", property("QCS_mouseControlAct").toString()); |
| 365 | s.writeCharacters(property("QCS_mouseControl").toString()); |
| 366 | s.writeEndElement(); |
| 367 | s.writeTextElement("resolution", |
| 368 | QString::number(property("QCS_resolution").toDouble(), 'f', 8)); |
| 369 | s.writeStartElement("randomizable"); |
| 370 | s.writeAttribute("group", QString::number(property("QCS_randomizableGroup").toInt())); |
| 371 | s.writeCharacters(property("QCS_randomizable").toBool() ? "true": "false"); |
| 372 | s.writeEndElement(); |
| 373 | |
| 374 | QColor color = property("QCS_color").value<QColor>(); |
| 375 | s.writeStartElement("color"); |
| 376 | s.writeTextElement("r", QString::number(color.red())); |
| 377 | s.writeTextElement("g", QString::number(color.green())); |
| 378 | s.writeTextElement("b", QString::number(color.blue())); |
| 379 | s.writeEndElement(); |
| 380 | |
| 381 | // we save textcolor as a hex value (QColor::name() returns the hex |
| 382 | // representation, which can be used to create a QColor also |
| 383 | QColor textcolor = static_cast<QVdial*>(m_widget)->getTextColor(); |
| 384 | s.writeTextElement("textcolor", textcolor.name()); |
| 385 | s.writeTextElement("border", QString::number(property("QCS_border").toInt())); |
| 386 | s.writeTextElement("borderColor", property("QCS_borderColor").toString()); |
| 387 | s.writeTextElement("showvalue", |
| 388 | property("QCS_showvalue").toBool() ? "true" : "false"); |
| 389 | s.writeTextElement("flatstyle", |
| 390 | property("QCS_flatstyle").toBool() ? "true" : "false"); |
| 391 | s.writeTextElement("integerMode", |
| 392 | property("QCS_integerMode").toBool() ? "true" : "false"); |
| 393 | s.writeEndElement(); |
| 394 | #ifdef USE_WIDGET_MUTEX |
| 395 | widgetLock.unlock(); |
| 396 | #endif |
| 397 | return xmlText; |
| 398 | } |
| 399 | |
| 400 | QString QuteKnob::getWidgetType() |
| 401 | { |
| 402 | return QString("BSBKnob"); |
| 403 | } |
| 404 | |
| 405 | void buttonSetColorIcon(QPushButton *button, QColor color, int size) { |
| 406 | QPixmap pixmap(size, size); |
| 407 | pixmap.fill(color); |
| 408 | button->setPalette(QPalette(color)); |
| 409 | button->setIcon(pixmap); |
| 410 | } |
| 411 | |
| 412 | void QuteKnob::selectKnobColor() { |
| 413 | auto currentColor = m_widget->palette().color(QPalette::WindowText); |
| 414 | QColor color = QColorDialog::getColor(currentColor, this); |
| 415 | setColor(color); |
| 416 | if(knobColorButton != nullptr) { |
| 417 | buttonSetColorIcon(knobColorButton, color, 64); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | void QuteKnob::selectKnobTextColor() { |
| 422 | auto currentColor = m_widget->palette().color(QPalette::WindowText); |
| 423 | QColor c = QColorDialog::getColor(currentColor, this); |
| 424 | setTextColor(c); |
| 425 | if(knobColorButton != nullptr) |
| 426 | buttonSetColorIcon(knobTextColorButton, c, 64); |
| 427 | } |
| 428 | |
| 429 | void QuteKnob::createPropertiesDialog() |
| 430 | { |
| 431 | QuteWidget::createPropertiesDialog(); |
| 432 | #ifdef USE_WIDGET_MUTEX |
| 433 | widgetLock.lockForRead(); |
| 434 | #endif |
| 435 | dialog->setWindowTitle("Knob"); |
| 436 | QLabel *label; |
| 437 | |
| 438 | label = new QLabel("Min =", dialog); |
| 439 | layout->addWidget(label, 2, 0, Qt::AlignRight|Qt::AlignVCenter); |
| 440 | |
| 441 | minSpinBox = new QDoubleSpinBox(dialog); |
| 442 | minSpinBox->setDecimals(4); |
| 443 | minSpinBox->setRange(-99999.0, 99999.0); |
| 444 | minSpinBox->setValue(property("QCS_minimum").toDouble()); |
| 445 | layout->addWidget(minSpinBox, 2,1, Qt::AlignLeft|Qt::AlignVCenter); |
| 446 | |
| 447 | label = new QLabel("Max =", dialog); |
| 448 | layout->addWidget(label, 2, 2, Qt::AlignRight|Qt::AlignVCenter); |
| 449 | |
| 450 | maxSpinBox = new QDoubleSpinBox(dialog); |
| 451 | maxSpinBox->setDecimals(4); |
| 452 | maxSpinBox->setRange(-99999.0, 99999.0); |
| 453 | maxSpinBox->setValue(property("QCS_maximum").toDouble()); |
| 454 | layout->addWidget(maxSpinBox, 2, 3, Qt::AlignLeft|Qt::AlignVCenter); |
| 455 | |
| 456 | // label->setText("Resolution"); |
| 457 | // layout->addWidget(label, 4, 0, Qt::AlignRight|Qt::AlignVCenter); |
| 458 | |
| 459 | flatStyleCheckBox = new QCheckBox("Flat style", dialog); |
| 460 | flatStyleCheckBox->setToolTip("If checked, knob is drawn in a flat style. " |
| 461 | "Otherwise, the native Qt look is used"); |
| 462 | flatStyleCheckBox->setCheckState( |
| 463 | property("QCS_flatstyle").toBool()?Qt::Checked:Qt::Unchecked); |
| 464 | layout->addWidget(flatStyleCheckBox, 5, 1, Qt::AlignLeft|Qt::AlignVCenter); |
| 465 | |
| 466 | displayValueCheckBox = new QCheckBox("Show value", dialog); |
| 467 | displayValueCheckBox->setToolTip("Show value as text inside the knob"); |
| 468 | displayValueCheckBox->setCheckState( |
| 469 | property("QCS_showvalue").toBool()?Qt::Checked:Qt::Unchecked); |
| 470 | layout->addWidget(displayValueCheckBox, 6, 1, Qt::AlignLeft|Qt::AlignVCenter); |
| 471 | |
| 472 | intModeCheckBox = new QCheckBox("Integer", dialog); |
| 473 | intModeCheckBox->setToolTip("Constrain displayed value to nearest integer"); |
| 474 | intModeCheckBox->setCheckState( |
| 475 | property("QCS_integerMode").toBool()?Qt::Checked:Qt::Unchecked); |
| 476 | layout->addWidget(intModeCheckBox, 6, 2, Qt::AlignLeft|Qt::AlignVCenter); |
| 477 | |
| 478 | // Color |
| 479 | label = new QLabel("Color", dialog); |
| 480 | layout->addWidget(label, 7, 0, Qt::AlignRight|Qt::AlignVCenter); |
| 481 | |
| 482 | knobColorButton = new SelectColorButton(dialog); |
| 483 | knobColorButton->setColor(property("QCS_color").value<QColor>()); |
| 484 | layout->addWidget(knobColorButton, 7, 1, Qt::AlignLeft | Qt::AlignVCenter); |
| 485 | |
| 486 | // Text Color |
| 487 | label = new QLabel("Text Color", dialog); |
| 488 | layout->addWidget(label, 7, 2, Qt::AlignRight|Qt::AlignVCenter); |
| 489 | |
| 490 | knobTextColorButton = new SelectColorButton(dialog); |
| 491 | knobTextColorButton->setColor(QColor(property("QCS_textcolor").toString())); |
| 492 | layout->addWidget(knobTextColorButton, 7, 3, Qt::AlignLeft | Qt::AlignVCenter); |
| 493 | |
| 494 | // Border |
| 495 | label = new QLabel("Border", dialog); |
| 496 | layout->addWidget(label, 8, 0, Qt::AlignRight|Qt::AlignVCenter); |
| 497 | |
| 498 | borderWidthSpinBox = new QSpinBox(dialog); |
| 499 | borderWidthSpinBox->unsetLocale(); |
| 500 | borderWidthSpinBox->setValue(property("QCS_border").toInt()); |
| 501 | borderWidthSpinBox->setRange(0, 8); |
| 502 | borderWidthSpinBox->setToolTip("Set to 1 or more to draw a border around the knob in flat mode"); |
| 503 | layout->addWidget(borderWidthSpinBox, 8, 1, Qt::AlignLeft|Qt::AlignVCenter); |
| 504 | |
| 505 | label = new QLabel("Border Color", dialog); |
| 506 | layout->addWidget(label, 8, 2, Qt::AlignRight|Qt::AlignVCenter); |
| 507 | |
| 508 | borderColorButton = new SelectColorButton(dialog); |
| 509 | borderColorButton->setColor(property("QCS_borderColor").value<QColor>()); |
| 510 | layout->addWidget(borderColorButton, 8, 3, Qt::AlignLeft|Qt::AlignVCenter); |
| 511 | |
| 512 | connect(flatStyleCheckBox, SIGNAL(toggled(bool)), |
| 513 | displayValueCheckBox, SLOT(setEnabled(bool))); |
| 514 | connect(flatStyleCheckBox, SIGNAL(toggled(bool)), |
| 515 | knobColorButton, SLOT(setEnabled(bool))); |
| 516 | connect(flatStyleCheckBox, SIGNAL(toggled(bool)), |
| 517 | knobColorButton, SLOT(setEnabled(bool))); |
| 518 | connect(flatStyleCheckBox, SIGNAL(toggled(bool)), |
| 519 | knobTextColorButton, SLOT(setEnabled(bool))); |
| 520 | connect(flatStyleCheckBox, SIGNAL(toggled(bool)), |
| 521 | label, SLOT(setEnabled(bool))); |
| 522 | connect(flatStyleCheckBox, SIGNAL(toggled(bool)), |
| 523 | intModeCheckBox, SLOT(setEnabled(bool))); |
| 524 | |
| 525 | |
| 526 | #ifdef USE_WIDGET_MUTEX |
| 527 | widgetLock.unlock(); |
| 528 | #endif |
| 529 | } |
| 530 | |
| 531 | void QuteKnob::applyProperties() |
| 532 | { |
| 533 | #ifdef USE_WIDGET_MUTEX |
| 534 | widgetLock.lockForRead(); |
| 535 | #endif |
| 536 | auto w = static_cast<QVdial*>(m_widget); |
| 537 | |
| 538 | setProperty("QCS_maximum", maxSpinBox->value()); |
| 539 | setProperty("QCS_minimum", minSpinBox->value()); |
| 540 | |
| 541 | // QColor color = knobColorButton->palette().color(QPalette::Window); |
| 542 | QColor color = knobColorButton->getColor(); |
| 543 | setProperty("QCS_color", color); |
| 544 | w->setColor(color); |
| 545 | |
| 546 | QColor textcolor = knobTextColorButton->getColor(); |
| 547 | setProperty("QCS_textcolor", textcolor.name()); |
| 548 | qDebug() << "knob textcolor" << textcolor; |
| 549 | w->setTextColor(textcolor); |
| 550 | |
| 551 | w->setDisplayRange(minSpinBox->value(), maxSpinBox->value()); |
| 552 | |
| 553 | bool showvalue = displayValueCheckBox->checkState(); |
| 554 | setProperty("QCS_showvalue", showvalue); |
| 555 | w->setDrawValue(showvalue); |
| 556 | |
| 557 | bool flatstyle = flatStyleCheckBox->checkState(); |
| 558 | setProperty("QCS_flatstyle", flatstyle); |
| 559 | w->setFlatStyle(flatstyle); |
| 560 | |
| 561 | bool intmode = intModeCheckBox->checkState(); |
| 562 | setProperty("QCS_integerMode", intmode); |
| 563 | w->setIntegerMode(intmode); |
| 564 | |
| 565 | QColor bordercolor = borderColorButton->getColor(); |
| 566 | setProperty("QCS_borderColor", bordercolor.name()); |
| 567 | int borderwidth = borderWidthSpinBox->value(); |
| 568 | setProperty("QCS_border", borderwidth); |
| 569 | w->setBorder(borderwidth, bordercolor); |
| 570 | |
| 571 | #ifdef USE_WIDGET_MUTEX |
| 572 | widgetLock.unlock(); |
| 573 | #endif |
| 574 | //Must be last to make sure the widgetChanged signal is last |
| 575 | QuteWidget::applyProperties(); |
| 576 | } |
| 577 | |
| 578 | void QuteKnob::knobChanged(int value) |
| 579 | { |
| 580 | #ifdef USE_WIDGET_MUTEX |
| 581 | widgetLock.lockForWrite(); |
| 582 | #endif |
| 583 | double min = property("QCS_minimum").toDouble(); |
| 584 | double max = property("QCS_maximum").toDouble(); |
| 585 | QDial *knob = static_cast<QDial *>(m_widget); |
| 586 | double normalized = (double) (value - knob->minimum()) |
| 587 | / (double) (knob->maximum() - knob->minimum()); |
| 588 | m_value = min + (normalized * (max-min)); |
| 589 | // setInternalValue(scaledValue); |
| 590 | m_valueChanged = true; |
| 591 | QPair<QString, double> channelValue(m_channel, m_value); |
| 592 | #ifdef USE_WIDGET_MUTEX |
| 593 | widgetLock.unlock(); |
| 594 | #endif |
| 595 | emit newValue(channelValue); |
| 596 | } |
| 597 | |
| 598 | void QuteKnob::setColor(QColor c) { |
| 599 | setProperty("QCS_color", c); |
| 600 | static_cast<QVdial*>(m_widget)->setColor(c); |
| 601 | } |
| 602 | |
| 603 | void QuteKnob::setTextColor(QColor c) { |
| 604 | setProperty("QCS_textcolor", c.name()); |
| 605 | static_cast<QVdial*>(m_widget)->setTextColor(c); |
| 606 | } |
| 607 | |
| 608 | void QuteKnob::setValueFromDialog() { |
| 609 | qDebug() << "knob value dialog" |
| 610 | << m_value |
| 611 | << property("QCS_minimum").toDouble() |
| 612 | << property("QCS_maximum").toDouble(); |
| 613 | double newvalue = QInputDialog::getDouble(this, tr("Enter New Value"), tr("Value"), |
| 614 | m_value, property("QCS_minimum").toDouble(), |
| 615 | property("QCS_maximum").toDouble(), 3); |
| 616 | setValue(newvalue); |
| 617 | QPair<QString, double> channelValue(m_channel, m_value); |
| 618 | emit newValue(channelValue); |
| 619 | } |
| 620 |