From 31b540128ab50206ffd4027aef757a00320b57db Mon Sep 17 00:00:00 2001 From: falkTX Date: Sun, 27 Jan 2013 11:12:15 +0000 Subject: [PATCH] Update ParamSpinBox --- source/carla_shared.py | 136 +++++----- source/widgets/paramspinbox.py | 472 +++++++++++++++++---------------- 2 files changed, 310 insertions(+), 298 deletions(-) diff --git a/source/carla_shared.py b/source/carla_shared.py index cbabaf33b..6c3893bde 100644 --- a/source/carla_shared.py +++ b/source/carla_shared.py @@ -4,17 +4,17 @@ # Common Carla code # Copyright (C) 2011-2013 Filipe Coelho # -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# any later version. +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 2 of +# the License, or any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # -# For a full copy of the GNU General Public License see the COPYING file +# For a full copy of the GNU General Public License see the GPL.txt file # ------------------------------------------------------------------------------------------------------------ # Imports (Global) @@ -724,11 +724,11 @@ class PluginParameter(QWidget): pType = pInfo['type'] pHints = pInfo['hints'] - self.m_midiCC = -1 - self.m_midiChannel = 1 - self.m_pluginId = pluginId - self.m_parameterId = pInfo['index'] - self.m_tabIndex = tabIndex + self.fMidiCC = -1 + self.fMidiChannel = 1 + self.fParameterId = pInfo['index'] + self.fPluginId = pluginId + self.fTabIndex = tabIndex self.ui.label.setText(pInfo['name']) @@ -736,18 +736,18 @@ class PluginParameter(QWidget): self.ui.combo.addItem(MIDI_CC) if pType == PARAMETER_INPUT: - self.ui.widget.set_minimum(pInfo['minimum']) - self.ui.widget.set_maximum(pInfo['maximum']) - self.ui.widget.set_default(pInfo['default']) - self.ui.widget.set_value(pInfo['current'], False) - self.ui.widget.set_label(pInfo['unit']) - self.ui.widget.set_step(pInfo['step']) - self.ui.widget.set_step_small(pInfo['stepSmall']) - self.ui.widget.set_step_large(pInfo['stepLarge']) - self.ui.widget.set_scalepoints(pInfo['scalepoints'], bool(pHints & PARAMETER_USES_SCALEPOINTS)) + self.ui.widget.setMinimum(pInfo['minimum']) + self.ui.widget.setMaximum(pInfo['maximum']) + self.ui.widget.setDefault(pInfo['default']) + self.ui.widget.setValue(pInfo['current'], False) + self.ui.widget.setLabel(pInfo['unit']) + self.ui.widget.setStep(pInfo['step']) + self.ui.widget.setStepSmall(pInfo['stepSmall']) + self.ui.widget.setStepLarge(pInfo['stepLarge']) + self.ui.widget.setScalePoints(pInfo['scalepoints'], bool(pHints & PARAMETER_USES_SCALEPOINTS)) if not pHints & PARAMETER_IS_ENABLED: - self.ui.widget.set_read_only(True) + self.ui.widget.setReadOnly(True) self.ui.combo.setEnabled(False) self.ui.sb_channel.setEnabled(False) @@ -756,11 +756,11 @@ class PluginParameter(QWidget): self.ui.sb_channel.setEnabled(False) elif pType == PARAMETER_OUTPUT: - self.ui.widget.set_minimum(pInfo['minimum']) - self.ui.widget.set_maximum(pInfo['maximum']) - self.ui.widget.set_value(pInfo['current'], False) - self.ui.widget.set_label(pInfo['unit']) - self.ui.widget.set_read_only(True) + self.ui.widget.setMinimum(pInfo['minimum']) + self.ui.widget.setMaximum(pInfo['maximum']) + self.ui.widget.setValue(pInfo['current'], False) + self.ui.widget.setLabel(pInfo['unit']) + self.ui.widget.setReadOnly(True) if not pHints & PARAMETER_IS_AUTOMABLE: self.ui.combo.setEnabled(False) @@ -771,8 +771,11 @@ class PluginParameter(QWidget): self.ui.combo.setVisible(False) self.ui.sb_channel.setVisible(False) - self.set_parameter_midi_cc(pInfo['midiCC']) - self.set_parameter_midi_channel(pInfo['midiChannel']) + if pHints & PARAMETER_USES_CUSTOM_TEXT: + self.ui.widget.setTextCallback(self._textCallBack) + + self.setMidiCC(pInfo['midiCC']) + self.setMidiChannel(pInfo['midiChannel']) self.connect(self.ui.widget, SIGNAL("valueChanged(double)"), SLOT("slot_valueChanged(double)")) self.connect(self.ui.sb_channel, SIGNAL("valueChanged(int)"), SLOT("slot_midiChannelChanged(int)")) @@ -781,65 +784,62 @@ class PluginParameter(QWidget): #if force_parameters_style: #self.widget.force_plastique_style() - if pHints & PARAMETER_USES_CUSTOM_TEXT: - self.ui.widget.set_text_call(self.textCallBack) - self.ui.widget.updateAll() - def setDefaultValue(self, value): - self.ui.widget.set_default(value) - - def set_parameter_value(self, value, send=True): - self.ui.widget.set_value(value, send) + def setDefault(self, value): + self.ui.widget.setDefault(value) - def set_parameter_midi_cc(self, cc): - self.m_midiCC = cc - self.set_MIDI_CC_in_ComboBox(cc) + def setValue(self, value, send=True): + self.ui.widget.setValue(value, send) - def set_parameter_midi_channel(self, channel): - self.m_midiChannel = channel+1 - self.ui.sb_channel.setValue(channel+1) - - def set_MIDI_CC_in_ComboBox(self, cc): - for i in range(len(MIDI_CC_LIST)): - ccText = MIDI_CC_LIST[i].split(" ")[0] - if int(ccText, 16) == cc: - ccIndex = i - break - else: - ccIndex = -1 + def setMidiCC(self, cc): + self.fMidiCC = cc + self._setMidiCcInComboBox(cc) - self.ui.combo.setCurrentIndex(ccIndex+1) + def setMidiChannel(self, channel): + self.fMidiChannel = channel + self.ui.sb_channel.setValue(channel) def tabIndex(self): - return self.m_tabIndex - - def textCallBack(self): - return cString(Carla.host.get_parameter_text(self.m_pluginId, self.m_parameterId)) + return self.fTabIndex @pyqtSlot(float) def slot_valueChanged(self, value): - self.emit(SIGNAL("valueChanged(int, double)"), self.m_parameterId, value) + self.emit(SIGNAL("valueChanged(int, double)"), self.fParameterId, value) @pyqtSlot(int) def slot_midiCcChanged(self, ccIndex): if ccIndex <= 0: cc = -1 else: - ccText = MIDI_CC_LIST[ccIndex - 1].split(" ")[0] - cc = int(ccText, 16) + ccStr = MIDI_CC_LIST[ccIndex - 1].split(" ")[0] + cc = int(ccStr, 16) - if self.m_midiCC != cc: - self.emit(SIGNAL("midiCcChanged(int, int)"), self.m_parameterId, cc) + if self.fMidiCC != cc: + self.emit(SIGNAL("midiCcChanged(int, int)"), self.fParameterId, cc) - self.m_midiCC = cc + self.fMidiCC = cc @pyqtSlot(int) def slot_midiChannelChanged(self, channel): - if self.m_midiChannel != channel: - self.emit(SIGNAL("midiChannelChanged(int, int)"), self.m_parameterId, channel) + if self.fMidiChannel != channel: + self.emit(SIGNAL("midiChannelChanged(int, int)"), self.fParameterId, channel) + + self.fMidiChannel = channel + + def _setMidiCcInComboBox(self, cc): + for i in range(len(MIDI_CC_LIST)): + ccStr = MIDI_CC_LIST[i].split(" ")[0] + if int(ccStr, 16) == cc: + ccIndex = i+1 + break + else: + ccIndex = 0 - self.m_midiChannel = channel + self.ui.combo.setCurrentIndex(ccIndex) + + def _textCallBack(self): + return cString(Carla.host.get_parameter_text(self.fPluginId, self.fParameterId)) # ------------------------------------------------------------------------------------------------------------ # TESTING @@ -856,8 +856,8 @@ ptest = { 'default': 0.3, 'minimum': 0.0, 'maximum': 1.0, - 'midiChannel': 1, - 'midiCC': -1, + 'midiChannel': 7, + 'midiCC': 2, 'type': PARAMETER_INPUT, 'hints': PARAMETER_IS_ENABLED | PARAMETER_IS_AUTOMABLE, 'scalepoints': [], @@ -868,8 +868,8 @@ ptest = { } app = QApplication(sys.argv) -gui1 = CarlaAboutW(None) +#gui1 = CarlaAboutW(None) gui2 = PluginParameter(None, ptest, 0, 0) -gui1.show() +#gui1.show() gui2.show() app.exec_() diff --git a/source/widgets/paramspinbox.py b/source/widgets/paramspinbox.py index 686987c79..7ba134e3f 100644 --- a/source/widgets/paramspinbox.py +++ b/source/widgets/paramspinbox.py @@ -4,24 +4,24 @@ # Parameter SpinBox, a custom Qt4 widget # Copyright (C) 2011-2013 Filipe Coelho # -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# any later version. +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 2 of +# the License, or any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # -# For a full copy of the GNU General Public License see the COPYING file +# For a full copy of the GNU General Public License see the GPL.txt file # ------------------------------------------------------------------------------------------------------------ # Imports (Global) from PyQt4.QtCore import pyqtSlot, Qt, QTimer, SIGNAL, SLOT from PyQt4.QtGui import QAbstractSpinBox, QComboBox, QCursor, QDialog, QInputDialog, QMenu, QPainter, QProgressBar, QValidator -from PyQt4.QtGui import QStyleFactory +#from PyQt4.QtGui import QStyleFactory from math import isnan # ------------------------------------------------------------------------------------------------------------ @@ -29,135 +29,140 @@ from math import isnan import ui_inputdialog_value -def fix_value(value, minimum, maximum): +def fixValue(value, minimum, maximum): if isnan(value): print("Parameter is NaN! - %f" % value) return minimum - elif value < minimum: + if value < minimum: print("Parameter too low! - %f/%f" % (value, minimum)) return minimum - elif value > maximum: + if value > maximum: print("Parameter too high! - %f/%f" % (value, maximum)) return maximum - else: - return value + return value #QPlastiqueStyle = QStyleFactory.create("Plastique") # ------------------------------------------------------------------------------------------------------------ # Custom InputDialog with Scale Points support -class CustomInputDialog(QDialog, ui_inputdialog_value.Ui_Dialog): +class CustomInputDialog(QDialog): def __init__(self, parent, label, current, minimum, maximum, step, scalePoints): QDialog.__init__(self, parent) - self.setupUi(self) + self.ui = ui_inputdialog_value.Ui_Dialog() + self.ui.setupUi(self) - self.label.setText(label) - self.doubleSpinBox.setMinimum(minimum) - self.doubleSpinBox.setMaximum(maximum) - self.doubleSpinBox.setValue(current) - self.doubleSpinBox.setSingleStep(step) - - self.ret_value = current + self.ui.label.setText(label) + self.ui.doubleSpinBox.setMinimum(minimum) + self.ui.doubleSpinBox.setMaximum(maximum) + self.ui.doubleSpinBox.setValue(current) + self.ui.doubleSpinBox.setSingleStep(step) if not scalePoints: - self.groupBox.setVisible(False) + self.ui.groupBox.setVisible(False) self.resize(200, 0) else: text = "" for scalePoint in scalePoints: text += "" % (scalePoint['value'], scalePoint['label']) text += "
%f - %s
" - self.textBrowser.setText(text) + self.ui.textBrowser.setText(text) self.resize(200, 300) + self.fRetValue = current + self.connect(self, SIGNAL("accepted()"), self.setReturnValue) + def returnValue(self): + return self.fRetValue + def setReturnValue(self): - self.ret_value = self.doubleSpinBox.value() + self.fRetValue = self.ui.doubleSpinBox.value() def done(self, r): QDialog.done(self, r) self.close() # ------------------------------------------------------------------------------------------------------------ -# Progress-Bar used for ParamSpinBox +# ProgressBar used for ParamSpinBox class ParamProgressBar(QProgressBar): def __init__(self, parent): QProgressBar.__init__(self, parent) - self.m_leftClickDown = False + self.fLeftClickDown = False - self.m_minimum = 0.0 - self.m_maximum = 1.0 - self.m_rvalue = 0.0 + self.fMinimum = 0.0 + self.fMaximum = 1.0 + self.fRealValue = 0.0 - self.m_label = "" - self.m_preLabel = " " - self.m_textCall = None + self.fLabel = "" + self.fPreLabel = " " + self.fTextCall = None - self.setMinimum(0) - self.setMaximum(1000) - self.setValue(0) self.setFormat("(none)") - def set_minimum(self, value): - self.m_minimum = value + # Fake internal value, 10'000 precision + QProgressBar.setMinimum(self, 0) + QProgressBar.setMaximum(self, 10000) + QProgressBar.setValue(self, 0) + + def setMinimum(self, value): + self.fMinimum = value - def set_maximum(self, value): - self.m_maximum = value + def setMaximum(self, value): + self.fMaximum = value - def set_value(self, value): - self.m_rvalue = value - vper = (value - self.m_minimum) / (self.m_maximum - self.m_minimum) - self.setValue(vper * 1000) + def setValue(self, value): + self.fRealValue = value + vper = float(value - self.fMinimum) / float(self.fMaximum - self.fMinimum) + QProgressBar.setValue(self, int(vper * 10000)) - def set_label(self, label): - self.m_label = label.strip() + def setLabel(self, label): + self.fLabel = label.strip() - if self.m_label == "(coef)": - self.m_label = "" - self.m_preLabel = "*" + if self.fLabel == "(coef)": + self.fLabel = "" + self.fPreLabel = "*" self.update() - def set_text_call(self, textCall): - self.m_textCall = textCall + def setTextCall(self, textCall): + self.fTextCall = textCall def handleMouseEventPos(self, pos): - xper = float(pos.x()) / self.width() - value = xper * (self.m_maximum - self.m_minimum) + self.m_minimum + xper = float(pos.x()) / float(self.width()) + value = xper * (self.fMaximum - self.fMinimum) + self.fMinimum - if value < self.m_minimum: - value = self.m_minimum - elif value > self.m_maximum: - value = self.m_maximum + if value < self.fMinimum: + value = self.fMinimum + elif value > self.fMaximum: + value = self.fMaximum - self.emit(SIGNAL("valueChangedFromBar(double)"), value) + self.emit(SIGNAL("valueChanged(double)"), value) def mousePressEvent(self, event): if event.button() == Qt.LeftButton: self.handleMouseEventPos(event.pos()) - self.m_leftClickDown = True + self.fLeftClickDown = True else: - self.m_leftClickDown = False + self.fLeftClickDown = False QProgressBar.mousePressEvent(self, event) def mouseMoveEvent(self, event): - if self.m_leftClickDown: + if self.fLeftClickDown: self.handleMouseEventPos(event.pos()) QProgressBar.mouseMoveEvent(self, event) def mouseReleaseEvent(self, event): - self.m_leftClickDown = False + self.fLeftClickDown = False QProgressBar.mouseReleaseEvent(self, event) def paintEvent(self, event): - if self.m_textCall: - self.setFormat("%s %s %s" % (self.m_preLabel, self.m_textCall(), self.m_label)) + if self.fTextCall is not None: + self.setFormat("%s %s %s" % (self.fPreLabel, self.fTextCall(), self.fLabel)) else: - self.setFormat("%s %f %s" % (self.m_preLabel, self.m_rvalue, self.m_label)) + self.setFormat("%s %f %s" % (self.fPreLabel, self.fRealValue, self.fLabel)) QProgressBar.paintEvent(self, event) @@ -168,241 +173,248 @@ class ParamSpinBox(QAbstractSpinBox): def __init__(self, parent): QAbstractSpinBox.__init__(self, parent) - self._minimum = 0.0 - self._maximum = 1.0 - self._default = 0.0 - self._value = None - self._step = 0.0 - self._step_small = 0.0 - self._step_large = 0.0 + self.fMinimum = 0.0 + self.fMaximum = 1.0 + self.fDefault = 0.0 + self.fValue = None + self.fStep = 0.0 + self.fStepSmall = 0.0 + self.fStepLarge = 0.0 - self._read_only = False - self._scalepoints = None - self._have_scalepoints = False + self.fReadOnly = False + self.fScalePoints = None + self.fHaveScalePoints = False - self.bar = ParamProgressBar(self) - self.bar.setContextMenuPolicy(Qt.NoContextMenu) - self.bar.show() + self.fBar = ParamProgressBar(self) + self.fBar.setContextMenuPolicy(Qt.NoContextMenu) + self.fBar.show() self.lineEdit().setVisible(False) - self.connect(self.bar, SIGNAL("valueChangedFromBar(double)"), self.handleValueChangedFromBar) - self.connect(self, SIGNAL("customContextMenuRequested(QPoint)"), self.showCustomMenu) + self.connect(self, SIGNAL("customContextMenuRequested(QPoint)"), SLOT("slot_showCustomMenu()")) + self.connect(self.fBar, SIGNAL("valueChanged(double)"), SLOT("slot_progressBarValueChanged(double)")) - QTimer.singleShot(0, self, SLOT("slot_updateBarGeometry()")) + QTimer.singleShot(0, self, SLOT("slot_updateProgressBarGeometry()")) #def force_plastique_style(self): #self.setStyle(QPlastiqueStyle) - def set_minimum(self, value): - self._minimum = value - self.bar.set_minimum(value) + def setDefault(self, value): + value = fixValue(value, self.fMinimum, self.fMaximum) + self.fDefault = value - def set_maximum(self, value): - self._maximum = value - self.bar.set_maximum(value) + def setMinimum(self, value): + self.fMinimum = value + self.fBar.setMinimum(value) - def set_default(self, value): - value = fix_value(value, self._minimum, self._maximum) - self._default = value + def setMaximum(self, value): + self.fMaximum = value + self.fBar.setMaximum(value) - def set_value(self, value, send=True): - value = fix_value(value, self._minimum, self._maximum) - if self._value != value: - self._value = value - self.bar.set_value(value) + def setValue(self, value, send=True): + value = fixValue(value, self.fMinimum, self.fMaximum) - if self._have_scalepoints: - self.set_scalepoint_value(value) + if self.fValue == value: + return False - if send: - self.emit(SIGNAL("valueChanged(double)"), value) + self.fValue = value + self.fBar.setValue(value) - self.update() + if self.fHaveScalePoints: + self._setScalePointValue(value) - return True + if send: + self.emit(SIGNAL("valueChanged(double)"), value) - else: - return False + self.update() + + return True - def set_step(self, value): + def setStep(self, value): if value == 0.0: - self._step = 0.001 + self.fStep = 0.001 else: - self._step = value + self.fStep = value - if self._step_small > value: - self._step_small = value - if self._step_large < value: - self._step_large = value + if self.fStepSmall > value: + self.fStepSmall = value + if self.fStepLarge < value: + self.fStepLarge = value - def set_step_small(self, value): + def setStepSmall(self, value): if value == 0.0: - self._step_small = 0.0001 - elif value > self._step: - self._step_small = self._step + self.fStepSmall = 0.0001 + elif value > self.fStep: + self.fStepSmall = self.fStep else: - self._step_small = value + self.fStepSmall = value - def set_step_large(self, value): + def setStepLarge(self, value): if value == 0.0: - self._step_large = 0.1 - elif value < self._step: - self._step_large = self._step + self.fStepLarge = 0.1 + elif value < self.fStep: + self.fStepLarge = self.fStep else: - self._step_large = value + self.fStepLarge = value - def set_label(self, label): - self.bar.set_label(label) + def setLabel(self, label): + self.fBar.setLabel(label) - def set_text_call(self, textCall): - self.bar.set_text_call(textCall) + def setTextCallback(self, textCall): + self.fBar.setTextCall(textCall) - def set_read_only(self, yesno): - self.setButtonSymbols(QAbstractSpinBox.UpDownArrows if yesno else QAbstractSpinBox.NoButtons) - self._read_only = yesno - self.setReadOnly(yesno) + def setReadOnly(self, yesNo): + self.setButtonSymbols(QAbstractSpinBox.UpDownArrows if yesNo else QAbstractSpinBox.NoButtons) + self.fReadOnly = yesNo + ParamSpinBox.setReadOnly(self, yesNo) - def set_scalepoints(self, scalepoints, use_scalepoints): - if len(scalepoints) > 0: - self._scalepoints = scalepoints - self._have_scalepoints = use_scalepoints + def setScalePoints(self, scalePoints, useScalePoints): + if len(scalePoints) == 0: + self.fScalePoints = None + self.fHaveScalePoints = False + return - if use_scalepoints: - # Hide ProgressBar and create a ComboBox - self.bar.close() - self.box = QComboBox(self) - self.box.setContextMenuPolicy(Qt.NoContextMenu) - self.box.show() - self.slot_updateBarGeometry() + self.fScalePoints = scalePoints + self.fHaveScalePoints = useScalePoints - for scalepoint in scalepoints: - self.box.addItem("%f - %s" % (scalepoint['value'], scalepoint['label'])) + if useScalePoints: + # Hide ProgressBar and create a ComboBox + self.fBar.close() + self.fBox = QComboBox(self) + self.fBox.setContextMenuPolicy(Qt.NoContextMenu) + self.fBox.show() + self.slot_updateProgressBarGeometry() - if self._value != None: - self.set_scalepoint_value(self._value) + for scalePoint in scalePoints: + self.fBox.addItem("%f - %s" % (scalePoint['value'], scalePoint['label'])) - self.connect(self.box, SIGNAL("currentIndexChanged(QString)"), self.handleValueChangedFromBox) + if self.fValue != None: + self._setScalePointValue(self.fValue) - else: - self._scalepoints = None + self.connect(self.fBox, SIGNAL("currentIndexChanged(QString)"), SLOT("slot_comboBoxIndexChanged(QString)")) - def set_scalepoint_value(self, value): - value = self.get_nearest_scalepoint(value) - for i in range(self.box.count()): - if float(self.box.itemText(i).split(" - ", 1)[0] == value): - self.box.setCurrentIndex(i) - break + def stepBy(self, steps): + if steps == 0 or self.fValue is None: + return - def get_nearest_scalepoint(self, real_value): - final_value = 0.0 - for i in range(len(self._scalepoints)): - scale_value = self._scalepoints[i]['value'] - if i == 0: - final_value = scale_value - else: - srange1 = abs(real_value - scale_value) - srange2 = abs(real_value - final_value) + value = self.fValue + (self.fStep * steps) - if srange2 > srange1: - final_value = scale_value + if value < self.fMinimum: + value = self.fMinimum + elif value > self.fMaximum: + value = self.fMaximum - return final_value + self.setValue(value) - def handleValueChangedFromBar(self, value): - if self._read_only: - return + def stepEnabled(self): + if self.fReadOnly or self.fValue is None: + return QAbstractSpinBox.StepNone + if self.fValue <= self.fMinimum: + return QAbstractSpinBox.StepUpEnabled + if self.fValue >= self.fMaximum: + return QAbstractSpinBox.StepDownEnabled + return (QAbstractSpinBox.StepUpEnabled | QAbstractSpinBox.StepDownEnabled) - step = int(0.5 + ((value - self._minimum) / self._step)) - real_value = self._minimum + (step * self._step) + def updateAll(self): + self.update() + self.fBar.update() + if self.fHaveScalePoints: + self.fBox.update() - self.set_value(real_value) + def resizeEvent(self, event): + QTimer.singleShot(0, self, SLOT("slot_updateProgressBarGeometry()")) + QAbstractSpinBox.resizeEvent(self, event) - def handleValueChangedFromBox(self, box_text): - if self._read_only: + @pyqtSlot(str) + def slot_comboBoxIndexChanged(self, boxText): + if self.fReadOnly: return - value = float(box_text.split(" - ", 1)[0]) - last_scale_value = self._scalepoints[len(self._scalepoints) - 1]['value'] + value = float(boxText.split(" - ", 1)[0]) + lastScaleValue = self.fScalePoints[-1]["value"] - if value == last_scale_value: - value = self._maximum + if value == lastScaleValue: + value = self.fMaximum - self.set_value(value) + self.setValue(value) - def showCustomMenu(self, pos): - menu = QMenu(self) - act_x_reset = menu.addAction(self.tr("Reset (%f)" % self._default)) + @pyqtSlot(float) + def slot_progressBarValueChanged(self, value): + if self.fReadOnly: + return + + step = int((value - self.fMinimum) / self.fStep + 0.5) + realValue = self.fMinimum + (step * self.fStep) + + self.setValue(realValue) + + @pyqtSlot() + def slot_showCustomMenu(self): + menu = QMenu(self) + actReset = menu.addAction(self.tr("Reset (%f)" % self.fDefault)) menu.addSeparator() - act_x_copy = menu.addAction(self.tr("Copy (%f)" % self._value)) - if False and not self._read_only: - act_x_paste = menu.addAction(self.tr("Paste (%s)" % "TODO")) + actCopy = menu.addAction(self.tr("Copy (%f)" % self.fValue)) + + if True or self.fReadOnly: + actPaste = menu.addAction(self.tr("Paste")) else: - act_x_paste = menu.addAction(self.tr("Paste")) - act_x_paste.setEnabled(False) + actPaste = menu.addAction(self.tr("Paste (%s)" % "TODO")) + menu.addSeparator() - act_x_set = menu.addAction(self.tr("Set value...")) - if self._read_only: - act_x_reset.setEnabled(False) - act_x_paste.setEnabled(False) - act_x_set.setEnabled(False) + actSet = menu.addAction(self.tr("Set value...")) + + if self.fReadOnly: + actReset.setEnabled(False) + actPaste.setEnabled(False) + actSet.setEnabled(False) # TODO - NOT IMPLEMENTED YET - act_x_copy.setEnabled(False) + actCopy.setEnabled(False) - act_x_sel = menu.exec_(QCursor.pos()) + actSel = menu.exec_(QCursor.pos()) - if act_x_sel == act_x_set: - dialog = CustomInputDialog(self, self.parent().label.text(), self._value, self._minimum, self._maximum, self._step, self._scalepoints) + if actSel == actSet: + dialog = CustomInputDialog(self, self.parent().label.text(), self.fValue, self.fMinimum, self.fMaximum, self.fStep, self.fScalePoints) if dialog.exec_(): - value = dialog.ret_value - self.set_value(value) + value = dialog.returnValue() + self.setValue(value) - elif act_x_sel == act_x_copy: + elif actSel == actCopy: pass - elif act_x_sel == act_x_paste: + elif actSel == actPaste: pass - elif act_x_sel == act_x_reset: - self.set_value(self._default) - - def stepBy(self, steps): - if steps == 0 or self._value == None: - return + elif actSel == actReset: + self.setValue(self.fDefault) - value = self._value + (steps * self._step) + @pyqtSlot() + def slot_updateProgressBarGeometry(self): + self.fBar.setGeometry(self.lineEdit().geometry()) + if self.fHaveScalePoints: + self.fBox.setGeometry(self.lineEdit().geometry()) - if value < self._minimum: - value = self._minimum - elif value > self._maximum: - value = self._maximum + def _getNearestScalePoint(self, realValue): + finalValue = 0.0 - self.set_value(value) + for i in range(len(self.fScalePoints)): + scaleValue = self.fScalePoints[i]["value"] + if i == 0: + finalValue = scaleValue + else: + srange1 = abs(realValue - scaleValue) + srange2 = abs(realValue - finalValue) - def stepEnabled(self): - if self._read_only or self._value == None: - return QAbstractSpinBox.StepNone - elif self._value <= self._minimum: - return QAbstractSpinBox.StepUpEnabled - elif self._value >= self._maximum: - return QAbstractSpinBox.StepDownEnabled - else: - return QAbstractSpinBox.StepUpEnabled | QAbstractSpinBox.StepDownEnabled + if srange2 > srange1: + finalValue = scaleValue - def updateAll(self): - self.update() - self.bar.update() - if self._have_scalepoints: - self.box.update() + return finalValue - @pyqtSlot() - def slot_updateBarGeometry(self): - self.bar.setGeometry(self.lineEdit().geometry()) - if self._have_scalepoints: - self.box.setGeometry(self.lineEdit().geometry()) + def _setScalePointValue(self, value): + value = self._getNearestScalePoint(value) - def resizeEvent(self, event): - QTimer.singleShot(0, self, SLOT("slot_updateBarGeometry()")) - QAbstractSpinBox.resizeEvent(self, event) + for i in range(self.fBox.count()): + if float(self.fBox.itemText(i).split(" - ", 1)[0] == value): + self.fBox.setCurrentIndex(i) + break