mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Added slider editor for double values
p4#: 19089
This commit is contained in:
parent
65acad250c
commit
a4ff9bd41c
@ -19,6 +19,8 @@ set( QOBJECT_HEADERS
|
||||
cafPdmUiFilePathEditor.h
|
||||
cafPdmUiListEditor.h
|
||||
cafPdmUiSliderEditor.h
|
||||
cafPdmUiDoubleSliderEditor.h
|
||||
|
||||
cafPdmUiColorEditor.h
|
||||
|
||||
cafPdmUiPropertyView.h
|
||||
@ -43,6 +45,7 @@ add_library( ${PROJECT_NAME}
|
||||
cafPdmUiFilePathEditor.cpp
|
||||
cafPdmUiListEditor.cpp
|
||||
cafPdmUiSliderEditor.cpp
|
||||
cafPdmUiDoubleSliderEditor.cpp
|
||||
cafPdmUiColorEditor.cpp
|
||||
|
||||
cafPdmUiPropertyView.cpp
|
||||
|
218
cafUserInterface/cafPdmUiDoubleSliderEditor.cpp
Normal file
218
cafUserInterface/cafPdmUiDoubleSliderEditor.cpp
Normal file
@ -0,0 +1,218 @@
|
||||
//##################################################################################################
|
||||
//
|
||||
// Custom Visualization Core library
|
||||
// Copyright (C) 2011-2012 Ceetron AS
|
||||
//
|
||||
// This library 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 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library 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 at <<http://www.gnu.org/licenses/gpl.html>>
|
||||
// for more details.
|
||||
//
|
||||
//##################################################################################################
|
||||
|
||||
#include "cafPdmUiDoubleSliderEditor.h"
|
||||
|
||||
#include "cafPdmUiDefaultObjectEditor.h"
|
||||
#include "cafPdmObject.h"
|
||||
#include "cafPdmUiFieldEditorHandle.h"
|
||||
#include "cafPdmField.h"
|
||||
|
||||
#include "cafFactory.h"
|
||||
|
||||
#include <QLineEdit>
|
||||
#include <QLabel>
|
||||
#include <QHBoxLayout>
|
||||
#include <QDoubleValidator>
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
class PdmDoubleValidator : public QDoubleValidator
|
||||
{
|
||||
public:
|
||||
PdmDoubleValidator(QObject * parent = 0) : QDoubleValidator(parent)
|
||||
{
|
||||
}
|
||||
|
||||
PdmDoubleValidator(double bottom, double top, int decimals, QObject * parent)
|
||||
: QDoubleValidator(bottom, top, decimals, parent)
|
||||
{
|
||||
}
|
||||
|
||||
~PdmDoubleValidator()
|
||||
{
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
virtual void fixup(QString& stringValue) const
|
||||
{
|
||||
double doubleValue = stringValue.toDouble();
|
||||
doubleValue = qBound(bottom(), doubleValue, top());
|
||||
|
||||
stringValue = QString::number(doubleValue, 'g', decimals());
|
||||
}
|
||||
};
|
||||
|
||||
namespace caf
|
||||
{
|
||||
|
||||
CAF_PDM_UI_FIELD_EDITOR_SOURCE_INIT(PdmUiDoubleSliderEditor);
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void PdmUiDoubleSliderEditor::configureAndUpdateUi(const QString& uiConfigName)
|
||||
{
|
||||
assert(!m_lineEdit.isNull());
|
||||
|
||||
QIcon ic = field()->uiIcon(uiConfigName);
|
||||
if (!ic.isNull())
|
||||
{
|
||||
m_label->setPixmap(ic.pixmap(ic.actualSize(QSize(64, 64))));
|
||||
}
|
||||
else
|
||||
{
|
||||
m_label->setText(field()->uiName(uiConfigName));
|
||||
}
|
||||
|
||||
m_label->setVisible(!field()->isUiHidden(uiConfigName));
|
||||
m_label->setEnabled(!field()->isUiReadOnly(uiConfigName));
|
||||
|
||||
m_lineEdit->setEnabled(!field()->isUiReadOnly(uiConfigName));
|
||||
m_slider->setEnabled(!field()->isUiReadOnly(uiConfigName));
|
||||
|
||||
field()->ownerObject()->editorAttribute(field(), uiConfigName, &m_attributes);
|
||||
|
||||
PdmDoubleValidator* pdmValidator = new PdmDoubleValidator(m_attributes.m_minimum, m_attributes.m_maximum, m_attributes.m_decimals, this);
|
||||
m_lineEdit->setValidator(pdmValidator);
|
||||
|
||||
QString textValue = field()->uiValue().toString();
|
||||
pdmValidator->fixup(textValue);
|
||||
|
||||
m_lineEdit->setText(textValue);
|
||||
updateSliderPosition();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QWidget* PdmUiDoubleSliderEditor::createEditorWidget(QWidget * parent)
|
||||
{
|
||||
QWidget* containerWidget = new QWidget(parent);
|
||||
|
||||
QHBoxLayout* layout = new QHBoxLayout();
|
||||
layout->setMargin(0);
|
||||
containerWidget->setLayout(layout);
|
||||
|
||||
m_lineEdit = new QLineEdit(containerWidget);
|
||||
m_lineEdit->setMaximumWidth(100);
|
||||
connect(m_lineEdit, SIGNAL(editingFinished()), this, SLOT(slotEditingFinished()));
|
||||
|
||||
m_slider = new QSlider(Qt::Horizontal, containerWidget);
|
||||
layout->addWidget(m_lineEdit);
|
||||
layout->addWidget(m_slider);
|
||||
|
||||
connect(m_slider, SIGNAL(valueChanged(int)), this, SLOT(slotSliderValueChanged(int)));
|
||||
|
||||
return containerWidget;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QWidget* PdmUiDoubleSliderEditor::createLabelWidget(QWidget * parent)
|
||||
{
|
||||
m_label = new QLabel(parent);
|
||||
return m_label;
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void PdmUiDoubleSliderEditor::slotEditingFinished()
|
||||
{
|
||||
updateSliderPosition();
|
||||
|
||||
writeValueToField();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void PdmUiDoubleSliderEditor::slotSliderValueChanged(int value)
|
||||
{
|
||||
double newDoubleValue = convertFromSliderValue(value);
|
||||
m_lineEdit->setText(QString::number(newDoubleValue));
|
||||
|
||||
writeValueToField();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void PdmUiDoubleSliderEditor::updateSliderPosition()
|
||||
{
|
||||
QString textValue = m_lineEdit->text();
|
||||
|
||||
bool convertOk = false;
|
||||
double newSliderValue = textValue.toDouble(&convertOk);
|
||||
|
||||
int newSliderPosition = convertToSliderValue(newSliderValue);
|
||||
if (m_slider->value() != newSliderPosition)
|
||||
{
|
||||
m_slider->blockSignals(true);
|
||||
m_slider->setValue(newSliderPosition);
|
||||
m_slider->blockSignals(false);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void PdmUiDoubleSliderEditor::writeValueToField()
|
||||
{
|
||||
QString textValue = m_lineEdit->text();
|
||||
QVariant v;
|
||||
v = textValue;
|
||||
this->setValueToField(v);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
int PdmUiDoubleSliderEditor::convertToSliderValue(double value)
|
||||
{
|
||||
double exactSliderValue = m_slider->maximum() * (value - m_attributes.m_minimum) / (m_attributes.m_maximum - m_attributes.m_minimum);
|
||||
|
||||
int sliderValue = exactSliderValue;
|
||||
sliderValue = qBound(m_slider->minimum(), sliderValue, m_slider->maximum());
|
||||
|
||||
return sliderValue;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
double PdmUiDoubleSliderEditor::convertFromSliderValue(int sliderValue)
|
||||
{
|
||||
double newDoubleValue = m_attributes.m_minimum + sliderValue * (m_attributes.m_maximum - m_attributes.m_minimum) / m_slider->maximum();
|
||||
newDoubleValue = qBound(m_attributes.m_minimum, newDoubleValue, m_attributes.m_maximum);
|
||||
|
||||
return newDoubleValue;
|
||||
}
|
||||
|
||||
|
||||
} // end namespace caf
|
90
cafUserInterface/cafPdmUiDoubleSliderEditor.h
Normal file
90
cafUserInterface/cafPdmUiDoubleSliderEditor.h
Normal file
@ -0,0 +1,90 @@
|
||||
//##################################################################################################
|
||||
//
|
||||
// Custom Visualization Core library
|
||||
// Copyright (C) 2011-2012 Ceetron AS
|
||||
//
|
||||
// This library 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 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library 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 at <<http://www.gnu.org/licenses/gpl.html>>
|
||||
// for more details.
|
||||
//
|
||||
//##################################################################################################
|
||||
|
||||
#pragma once
|
||||
#include "cafPdmUiFieldEditorHandle.h"
|
||||
|
||||
#include <QString>
|
||||
#include <QLabel>
|
||||
#include <QWidget>
|
||||
#include <QPointer>
|
||||
#include <QLineEdit>
|
||||
#include <QGroupBox>
|
||||
#include <QSlider>
|
||||
|
||||
|
||||
namespace caf
|
||||
{
|
||||
|
||||
//==================================================================================================
|
||||
///
|
||||
//==================================================================================================
|
||||
|
||||
class PdmUiDoubleSliderEditorAttribute : public PdmUiEditorAttribute
|
||||
{
|
||||
public:
|
||||
PdmUiDoubleSliderEditorAttribute()
|
||||
{
|
||||
m_minimum = 0;
|
||||
m_maximum = 10;
|
||||
m_decimals = 6;
|
||||
}
|
||||
|
||||
public:
|
||||
double m_minimum;
|
||||
double m_maximum;
|
||||
int m_decimals;
|
||||
};
|
||||
|
||||
|
||||
class PdmUiDoubleSliderEditor : public PdmUiFieldEditorHandle
|
||||
{
|
||||
Q_OBJECT
|
||||
CAF_PDM_UI_FIELD_EDITOR_HEADER_INIT;
|
||||
|
||||
public:
|
||||
PdmUiDoubleSliderEditor() {}
|
||||
virtual ~PdmUiDoubleSliderEditor() {}
|
||||
|
||||
protected:
|
||||
virtual void configureAndUpdateUi(const QString& uiConfigName);
|
||||
virtual QWidget* createEditorWidget(QWidget * parent);
|
||||
virtual QWidget* createLabelWidget(QWidget * parent);
|
||||
|
||||
protected slots:
|
||||
void slotEditingFinished();
|
||||
void slotSliderValueChanged(int value);
|
||||
|
||||
private:
|
||||
void updateSliderPosition();
|
||||
void writeValueToField();
|
||||
|
||||
int convertToSliderValue(double value);
|
||||
double convertFromSliderValue(int sliderValue);
|
||||
|
||||
private:
|
||||
QPointer<QLineEdit> m_lineEdit;
|
||||
QPointer<QSlider> m_slider;
|
||||
QPointer<QLabel> m_label;
|
||||
|
||||
PdmUiDoubleSliderEditorAttribute m_attributes;
|
||||
};
|
||||
|
||||
|
||||
} // end namespace caf
|
Loading…
Reference in New Issue
Block a user