/****************************************************************************** * Handles the mouse up event. ******************************************************************************/ void SpinnerWidget::mouseReleaseEvent(QMouseEvent* event) { if(_upperBtnPressed || _lowerBtnPressed) { if(_upperBtnPressed == _lowerBtnPressed) { Q_EMIT spinnerDragStop(); } else { FloatType newValue; if(_upperBtnPressed) { if(unit()) newValue = unit()->roundValue(floatValue() + unit()->stepSize(floatValue(), true)); else newValue = floatValue() + 1.0f; } else { if(unit()) newValue = unit()->roundValue(floatValue() - unit()->stepSize(floatValue(), false)); else newValue = floatValue() - 1.0f; } setFloatValue(newValue, true); } _upperBtnPressed = false; _lowerBtnPressed = false; // Repaint spinner. update(); } releaseMouse(); }
/****************************************************************************** * Handles the mouse down event. ******************************************************************************/ void SpinnerWidget::mousePressEvent(QMouseEvent* event) { if(event->button() == Qt::LeftButton && !_upperBtnPressed && !_lowerBtnPressed) { // Backup current value. _oldValue = floatValue(); OVITO_ASSERT(_lowerBtnPressed == false && _upperBtnPressed == false); if(event->y() <= height()/2) _upperBtnPressed = true; else _lowerBtnPressed = true; _currentStepSize = unit() ? unit()->stepSize(floatValue(), _upperBtnPressed) : 1; if(textBox()) textBox()->setFocus(Qt::OtherFocusReason); grabMouse(); repaint(); } else if(event->button() == Qt::RightButton) { // restore old value setFloatValue(_oldValue, true); if(_upperBtnPressed == _lowerBtnPressed) { Q_EMIT spinnerDragAbort(); } _upperBtnPressed = false; _lowerBtnPressed = false; releaseMouse(); update(); } }
bool ExpressionItem::setValue(void* value_str,const data_type type) { switch(type) { case t_int: { setIntValue(*(int *)value_str); break; } case t_float: { setFloatValue(*(float *)value_str); break; } case t_smallInt: { setSmallIntValue(*(short *)value_str); break; } case t_double: { setDoubleValue(*(double *)value_str); break; } case t_u_long: { setULongValue(*(unsigned long*)value_str); break; } case t_string: { setStringValue(string((char *)value_str)); break; } case t_decimal: { setDecimalValue((const char *)value_str); break; } case t_datetime: { setDatetimeValue((const char *)value_str); break; } case t_date: { setDateValue((const char *)value_str); break; } case t_time: { setTimeValue((const char *)value_str); break; } case t_boolean: { setBooleanValue(*(bool *)value_str); break; } default: { cout<<"no matching operator exists!!!"<<endl; /* * In the debug phase, it's better to show the assert failure in case of unexpected input. * The bug can be found much more easily in such a way. */ assert(false); break; } } return true; }
/****************************************************************************** * Handles the mouse move event. ******************************************************************************/ void SpinnerWidget::mouseMoveEvent(QMouseEvent* event) { if(_upperBtnPressed || _lowerBtnPressed) { if(_upperBtnPressed && !_lowerBtnPressed) { if(event->y() > height()/2 || event->y() < 0) { _lowerBtnPressed = true; _lastMouseY = _startMouseY = mapToGlobal(event->pos()).y(); update(); Q_EMIT spinnerDragStart(); } } else if(!_upperBtnPressed && _lowerBtnPressed) { if(event->y() <= height()/2 || event->y() > height()) { _upperBtnPressed = true; _lastMouseY = _startMouseY = mapToGlobal(event->pos()).y(); update(); Q_EMIT spinnerDragStart(); } } else { QPoint cursorPos = QCursor::pos(); int screenY = cursorPos.y(); if(screenY != _lastMouseY) { int screenHeight = QApplication::desktop()->screenGeometry().height(); if(screenY <= 5 && _lastMouseY == screenHeight-1) return; if(screenY >= screenHeight - 5 && _lastMouseY == 0) return; FloatType newVal = _oldValue + _currentStepSize * (FloatType)(_startMouseY - screenY) * 0.1f; if(unit()) newVal = unit()->roundValue(newVal); if(screenY < _lastMouseY && screenY <= 5) { _lastMouseY = screenHeight-1; _startMouseY += _lastMouseY - screenY; QCursor::setPos(cursorPos.x(), _lastMouseY); } else if(screenY > _lastMouseY && screenY >= screenHeight - 5) { _lastMouseY = 0; _startMouseY += _lastMouseY - screenY; QCursor::setPos(cursorPos.x(), _lastMouseY); } else _lastMouseY = screenY; if(newVal != floatValue()) { setFloatValue(newVal, true); // Repaint viewports for immediate visual feedback when changing a parameter. if(MainWindow* mainWindow = qobject_cast<MainWindow*>(window())) mainWindow->processViewportUpdates(); // Also repaint text box for immediate visual updates. if(textBox()) textBox()->repaint(); } } } } }
/****************************************************************************** * Will be called when the user has entered a new text into the text box. * The text will be parsed and taken as the new value of the spinner. ******************************************************************************/ void SpinnerWidget::onTextChanged() { OVITO_CHECK_POINTER(textBox()); FloatType newValue; try { if(textBox()->text() == _originalText) return; if(unit()) { newValue = unit()->parseString(textBox()->text()); setFloatValue(unit()->userToNative(newValue), true); } else { bool ok; newValue = textBox()->text().toDouble(&ok); if(!ok) throw Exception(tr("Invalid floating-point value: %1").arg(textBox()->text())); setFloatValue(newValue, true); } } catch(const Exception&) { // Ignore invalid value and restore old text content. updateTextBox(); } }
bool _GuiSliderBase::onEvent(const GEvent& event) { if (! m_visible) { return false; } if (event.type == GEventType::MOUSE_BUTTON_DOWN) { Vector2 mouse = Vector2(event.button.x, event.button.y); float v = floatValue(); Rect2D thumbRect = theme()->horizontalSliderToThumbBounds(m_rect, v, m_captionSize); Rect2D trackRect = theme()->horizontalSliderToTrackBounds(m_rect, m_captionSize); if (thumbRect.contains(mouse)) { // Begin drag m_inDrag = true; m_dragStart = mouse; m_dragStartValue = v; GEvent response; response.gui.type = GEventType::GUI_DOWN; response.gui.control = m_eventSource; m_gui->fireEvent(response); response.gui.type = GEventType::GUI_CHANGE; response.gui.control = m_eventSource; m_gui->fireEvent(response); return true; } else if (trackRect.contains(mouse)) { // Jump to this position float p = clamp((mouse.x - trackRect.x0()) / trackRect.width(), 0.0f, 1.0f); setFloatValue(p); m_inDrag = false; GEvent response; response.gui.type = GEventType::GUI_CHANGE; response.gui.control = m_eventSource; m_gui->fireEvent(response); fireEvent(GEventType::GUI_ACTION); return true; } } else if (event.type == GEventType::MOUSE_BUTTON_UP) { if (m_inDrag) { // End the drag m_inDrag = false; fireEvent(GEventType::GUI_DOWN); fireEvent(GEventType::GUI_ACTION); return true; } } else if (m_inDrag && (event.type == GEventType::MOUSE_MOTION)) { // We'll only receive these events if we have the keyFocus, but we can't // help receiving the key focus if the user clicked on the control! Vector2 mouse = Vector2(event.button.x, event.button.y); Rect2D trackRect = theme()->horizontalSliderToTrackBounds(m_rect, m_captionSize); float delta = (mouse.x - m_dragStart.x) / trackRect.width(); float p = clamp(m_dragStartValue + delta, 0.0f, 1.0f); setFloatValue(p); GEvent response; response.gui.type = GEventType::GUI_CHANGE; response.gui.control = m_eventSource; m_gui->fireEvent(response); return true; } return false; }
void KisGmicSettingsWidget::createSettingsWidget(ROLE role) { QList<Parameter *> parameters = m_commandDefinition->m_parameters; if (parameters.size() == 0) { dbgPlugins << "No parameters!"; return; } QGridLayout * gridLayout(0); if (role == CreateRole) { gridLayout = new QGridLayout(this); } int row = 0; for (int i = 0; i < parameters.size();i++) { Parameter * p = parameters.at(i); dbgPlugins << "Processing: " << qPrintable(p->typeName()) << " " << qPrintable(p->toString()); switch (p->m_type) { case Parameter::INT_P: { IntParameter * intParam = static_cast<IntParameter *>(p); KisSliderSpinBox * spinBox(0); if (role == CreateRole) { spinBox = new KisSliderSpinBox; spinBox->setMinimum(intParam->m_minValue); spinBox->setMaximum(intParam->m_maxValue); // map widget to parameter index m_widgetToParameterIndexMapper[spinBox] = i; mapParameterWidget(intParam, spinBox); // TODO: check if it should update preview!!!, only then recompute preview connect(spinBox, SIGNAL(valueChanged(int)), this, SIGNAL(sigConfigurationItemChanged())); connect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(setIntValue(int))); gridLayout->addWidget(new QLabel(intParam->name()), row, 0); gridLayout->addWidget(spinBox, row, 1, 1, 3); row++; } else if (role == LoadRole) { spinBox = qobject_cast<KisSliderSpinBox *>(widget(intParam)); } if (spinBox) { spinBox->setValue(intParam->m_value); } else { dbgPlugins << "Widget not available; Role: " << role << p->m_type; } break; } case Parameter::FLOAT_P: { FloatParameter * floatParam = static_cast<FloatParameter *>(p); KisDoubleSliderSpinBox * spinBox(0); if (role == CreateRole) { spinBox = new KisDoubleSliderSpinBox; // TODO: check if 2 decimals is correct spinBox->setRange(floatParam->m_minValue, floatParam->m_maxValue, 2); m_widgetToParameterIndexMapper[spinBox] = i; mapParameterWidget(floatParam, spinBox); connect(spinBox, SIGNAL(valueChanged(qreal)), this, SIGNAL(sigConfigurationItemChanged())); connect(spinBox, SIGNAL(valueChanged(qreal)), this, SLOT(setFloatValue(qreal))); gridLayout->addWidget(new QLabel(floatParam->name()), row, 0); gridLayout->addWidget(spinBox, row, 1, 1, 3); row++; } else if (role == LoadRole) { spinBox = qobject_cast<KisDoubleSliderSpinBox *>(widget(floatParam)); } if (spinBox) { spinBox->setValue(floatParam->m_value); } else { dbgPlugins << "Widget not available; Role: " << role << " TYPE " <<p->m_type; } break; } case Parameter::CHOICE_P: { ChoiceParameter * choiceParam = static_cast<ChoiceParameter *>(p); QComboBox * combo(0); if (role == CreateRole) { combo = new QComboBox; QStringListModel *model = new QStringListModel(); model->setStringList(choiceParam->m_choices); combo->setModel(model); m_widgetToParameterIndexMapper[combo] = i; mapParameterWidget(choiceParam, combo); connect(combo, SIGNAL(currentIndexChanged(QString)), this, SIGNAL(sigConfigurationItemChanged())); connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(setChoiceIndex(int))); gridLayout->addWidget(new QLabel(choiceParam->name()), row, 0); gridLayout->addWidget(combo, row, 1, 1, 3); row++; } else if (role == LoadRole)
bool GuiScrollBar::onEvent(const GEvent& event) { if (! m_visible) { return false; } float m = maxValue(); Rect2D topB; Rect2D bottomB; Rect2D barBounds; Rect2D thumbBounds; getAllBounds(topB, bottomB, barBounds, thumbBounds); if (event.type == GEventType::MOUSE_BUTTON_DOWN) { const Vector2& mouse = Vector2(event.button.x, event.button.y); if(bottomB.contains(mouse)) { *m_value = min<float>( m * m_extent, *m_value + m_extent * BUTTON_PRESS_SCROLL); m_state = ARROW_DOWN_SCROLLING; GEvent response; response.gui.type = GEventType::GUI_CHANGE; response.gui.control = m_eventSource; m_gui->fireEvent(response); fireEvent(GEventType::GUI_ACTION); return true; } else if (topB.contains(mouse)) { *m_value = max<float>( 0.0f, *m_value - m_extent*BUTTON_PRESS_SCROLL); m_state = ARROW_UP_SCROLLING; GEvent response; response.gui.type = GEventType::GUI_CHANGE; response.gui.control = m_eventSource; m_gui->fireEvent(response); fireEvent(GEventType::GUI_ACTION); return true; } else if (thumbBounds.contains(mouse)) { m_state = IN_DRAG; m_dragStart = mouse; m_dragStartValue = floatValue(); GEvent response; response.gui.type = GEventType::GUI_DOWN; response.gui.control = m_eventSource; m_gui->fireEvent(response); response.gui.type = GEventType::GUI_CHANGE; response.gui.control = m_eventSource; m_gui->fireEvent(response); return true; } else if (barBounds.contains(mouse)) { // Jump to this position float p; if(m_vertical) { p = ( mouse.y - (float)barBounds.y0() ) / ((float)barBounds.height()/(m + 1)) - .5f; } else { p = ( mouse.x - (float)barBounds.x0() ) / ((float)barBounds.width()/(m + 1)) - .5f; } p = max<float>(0, p); p = min<float>(p, m); setFloatValue(p); m_state = NONE; GEvent response; response.gui.type = GEventType::GUI_CHANGE; response.gui.control = m_eventSource; m_gui->fireEvent(response); fireEvent(GEventType::GUI_ACTION); return true; } return false; } else if (event.type == GEventType::MOUSE_BUTTON_UP) { m_state = NONE; fireEvent(GEventType::GUI_ACTION); if (m_state == IN_DRAG) { // End the drag fireEvent(GEventType::GUI_DOWN); fireEvent(GEventType::GUI_ACTION); return true; } } else if (event.type == GEventType::MOUSE_MOTION) { // We'll only receive these events if we have the keyFocus, but we can't // help receiving the key focus if the user clicked on the control! const Vector2& mouse = Vector2(event.button.x, event.button.y); if (m_state == IN_DRAG) { float delta; if(m_vertical) { delta = (mouse.y - m_dragStart.y) / (barBounds.height()/(m + 1)); } else { delta = (mouse.x - m_dragStart.x) / (barBounds.width()/(m + 1)); } float p = m_dragStartValue + delta; p = max<float>(0, p); p = min<float>(p, m); setFloatValue(p); GEvent response; response.gui.type = GEventType::GUI_CHANGE; response.gui.control = m_eventSource; m_gui->fireEvent(response); return true; } else if (m_state == ARROW_UP_SCROLLING || m_state == ARROW_UP) { if(topB.contains(mouse)) { m_state = ARROW_UP_SCROLLING; } else { m_state = ARROW_UP; } return true; } else if (m_state == ARROW_DOWN_SCROLLING || m_state == ARROW_DOWN) { if(bottomB.contains(mouse)) { m_state = ARROW_DOWN_SCROLLING; } else { m_state = ARROW_DOWN; } return true; } } return false; }
bool FGPropertyManager::SetFloat (const string &name, float val) { return setFloatValue(name.c_str(), val); }