ResInsight/Fwk/AppFwk/cafUserInterface/cafPdmUiSliderEditor.cpp

181 lines
6.4 KiB
C++
Raw Permalink Normal View History

2012-06-26 09:10:41 -05:00
//##################################################################################################
//
// Custom Visualization Core library
// Copyright (C) 2011-2013 Ceetron AS
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// 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.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 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 Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
2012-06-26 09:10:41 -05:00
//
//##################################################################################################
#include "cafPdmUiSliderEditor.h"
2020-06-19 00:53:59 -05:00
#include "cafPdmField.h"
2012-06-26 09:10:41 -05:00
#include "cafPdmObject.h"
2020-06-19 00:53:59 -05:00
#include "cafPdmUiDefaultObjectEditor.h"
2012-06-26 09:10:41 -05:00
#include "cafPdmUiFieldEditorHandle.h"
#include "cafFactory.h"
#include "cafQShortenedLabel.h"
2012-06-26 09:10:41 -05:00
#include <QHBoxLayout>
#include <QIntValidator>
2020-06-19 00:53:59 -05:00
#include <QLabel>
#include <QLineEdit>
2012-06-26 09:10:41 -05:00
namespace caf
{
2020-06-19 00:53:59 -05:00
CAF_PDM_UI_FIELD_EDITOR_SOURCE_INIT( PdmUiSliderEditor );
2012-06-26 09:10:41 -05:00
//--------------------------------------------------------------------------------------------------
2020-06-19 00:53:59 -05:00
///
2012-06-26 09:10:41 -05:00
//--------------------------------------------------------------------------------------------------
2020-06-19 00:53:59 -05:00
void PdmUiSliderEditor::configureAndUpdateUi( const QString& uiConfigName )
2012-06-26 09:10:41 -05:00
{
2020-06-19 00:53:59 -05:00
CAF_ASSERT( !m_spinBox.isNull() );
2012-06-26 09:10:41 -05:00
2020-06-19 00:53:59 -05:00
PdmUiFieldEditorHandle::updateLabelFromField( m_label, uiConfigName );
2020-06-19 00:53:59 -05:00
m_spinBox->setEnabled( !uiField()->isUiReadOnly( uiConfigName ) );
m_spinBox->setToolTip( uiField()->uiToolTip( uiConfigName ) );
2020-06-19 00:53:59 -05:00
m_slider->setEnabled( !uiField()->isUiReadOnly( uiConfigName ) );
m_slider->setToolTip( uiField()->uiToolTip( uiConfigName ) );
2012-06-26 09:10:41 -05:00
2020-06-19 00:53:59 -05:00
caf::PdmUiObjectHandle* uiObject = uiObj( uiField()->fieldHandle()->ownerObject() );
if ( uiObject )
{
2020-06-19 00:53:59 -05:00
uiObject->editorAttribute( uiField()->fieldHandle(), uiConfigName, &m_attributes );
}
{
2020-06-19 00:53:59 -05:00
m_spinBox->blockSignals( true );
m_spinBox->setMinimum( m_attributes.m_minimum );
m_spinBox->setMaximum( m_attributes.m_maximum );
m_spinBox->setSingleStep( m_attributes.m_step );
2020-06-19 00:53:59 -05:00
QString textValue = uiField()->uiValue().toString();
2020-06-19 00:53:59 -05:00
m_spinBox->setValue( textValue.toInt() );
m_spinBox->blockSignals( false );
m_spinBox->setVisible( m_attributes.m_showSpinBox );
}
{
2020-06-19 00:53:59 -05:00
m_slider->blockSignals( true );
m_slider->setRange( m_attributes.m_minimum, m_attributes.m_maximum );
m_slider->setSingleStep( m_attributes.m_step );
updateSliderPosition();
2020-06-19 00:53:59 -05:00
m_slider->blockSignals( false );
}
2012-06-26 09:10:41 -05:00
}
//--------------------------------------------------------------------------------------------------
2020-06-19 00:53:59 -05:00
///
2012-06-26 09:10:41 -05:00
//--------------------------------------------------------------------------------------------------
2020-06-19 00:53:59 -05:00
QWidget* PdmUiSliderEditor::createEditorWidget( QWidget* parent )
2012-06-26 09:10:41 -05:00
{
2020-06-19 00:53:59 -05:00
QWidget* containerWidget = new QWidget( parent );
2012-06-26 09:10:41 -05:00
QHBoxLayout* layout = new QHBoxLayout();
layout->setContentsMargins( 0, 0, 0, 0 );
2020-06-19 00:53:59 -05:00
containerWidget->setLayout( layout );
m_spinBox = new QSpinBox( containerWidget );
m_spinBox->setMaximumWidth( 60 );
m_spinBox->setKeyboardTracking( false );
connect( m_spinBox, SIGNAL( valueChanged( int ) ), this, SLOT( slotSpinBoxValueChanged( int ) ) );
2012-06-26 09:10:41 -05:00
2020-06-19 00:53:59 -05:00
m_slider = new QSlider( Qt::Horizontal, containerWidget );
layout->addWidget( m_spinBox );
layout->addWidget( m_slider );
2012-06-26 09:10:41 -05:00
2020-06-19 00:53:59 -05:00
connect( m_slider, SIGNAL( valueChanged( int ) ), this, SLOT( slotSliderValueChanged( int ) ) );
2012-06-26 09:10:41 -05:00
return containerWidget;
}
//--------------------------------------------------------------------------------------------------
2020-06-19 00:53:59 -05:00
///
//--------------------------------------------------------------------------------------------------
2020-06-19 00:53:59 -05:00
QWidget* PdmUiSliderEditor::createLabelWidget( QWidget* parent )
{
2020-06-19 00:53:59 -05:00
m_label = new QShortenedLabel( parent );
return m_label;
2012-06-26 09:10:41 -05:00
}
//--------------------------------------------------------------------------------------------------
2020-06-19 00:53:59 -05:00
///
2012-06-26 09:10:41 -05:00
//--------------------------------------------------------------------------------------------------
2020-06-19 00:53:59 -05:00
void PdmUiSliderEditor::slotSliderValueChanged( int position )
2012-06-26 09:10:41 -05:00
{
2020-06-19 00:53:59 -05:00
m_spinBox->setValue( position );
2012-06-26 09:10:41 -05:00
writeValueToField();
2012-06-26 09:10:41 -05:00
}
//--------------------------------------------------------------------------------------------------
2020-06-19 00:53:59 -05:00
///
2012-06-26 09:10:41 -05:00
//--------------------------------------------------------------------------------------------------
2020-06-19 00:53:59 -05:00
void PdmUiSliderEditor::slotSpinBoxValueChanged( int spinBoxValue )
2012-06-26 09:10:41 -05:00
{
updateSliderPosition();
writeValueToField();
2012-06-26 09:10:41 -05:00
}
//--------------------------------------------------------------------------------------------------
2020-06-19 00:53:59 -05:00
///
2012-06-26 09:10:41 -05:00
//--------------------------------------------------------------------------------------------------
void PdmUiSliderEditor::updateSliderPosition()
{
QString textValue = m_spinBox->text();
2012-06-26 09:10:41 -05:00
2020-06-19 00:53:59 -05:00
bool convertOk = false;
int newSliderValue = textValue.toInt( &convertOk );
if ( convertOk )
2012-06-26 09:10:41 -05:00
{
2020-06-19 00:53:59 -05:00
newSliderValue = qBound( m_attributes.m_minimum, newSliderValue, m_attributes.m_maximum );
m_slider->setValue( newSliderValue );
2012-06-26 09:10:41 -05:00
}
}
//--------------------------------------------------------------------------------------------------
2020-06-19 00:53:59 -05:00
///
//--------------------------------------------------------------------------------------------------
void PdmUiSliderEditor::writeValueToField()
{
2020-06-19 00:53:59 -05:00
QString textValue = m_spinBox->text();
QVariant v;
v = textValue;
2020-06-19 00:53:59 -05:00
this->setValueToField( v );
}
2012-06-26 09:10:41 -05:00
} // end namespace caf