2012-06-26 09:10:41 -05:00
//##################################################################################################
//
// Custom Visualization Core library
2013-09-20 08:22:29 -05:00
// 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
//
//##################################################################################################
2013-09-20 08:22:29 -05:00
2012-06-26 09:10:41 -05:00
# include "cafPdmUiLineEditor.h"
2015-07-29 07:19:43 -05:00
# include "cafFactory.h"
# include "cafPdmField.h"
2012-06-26 09:10:41 -05:00
# include "cafPdmObject.h"
2015-07-29 07:19:43 -05:00
# include "cafPdmUiDefaultObjectEditor.h"
2012-06-26 09:10:41 -05:00
# include "cafPdmUiFieldEditorHandle.h"
# include "cafPdmUiOrdering.h"
2015-07-29 07:19:43 -05:00
# include "cafSelectionManager.h"
2012-06-26 09:10:41 -05:00
2017-02-20 08:32:23 -06:00
# include <QApplication>
2012-06-26 09:10:41 -05:00
# include <QIntValidator>
2015-07-29 07:19:43 -05:00
# include <QLabel>
# include <QLineEdit>
# include <QMainWindow>
2017-02-20 08:32:23 -06:00
# include <QMessageBox>
# include <QPalette>
2015-07-29 07:19:43 -05:00
# include <QStatusBar>
2017-02-20 08:32:23 -06:00
# include <QString>
2012-06-26 09:10:41 -05:00
2015-07-29 07:19:43 -05:00
class PdmUniqueIdValidator : public QValidator
{
public :
PdmUniqueIdValidator ( const std : : set < int > & usedIds , bool multipleSelectionOfSameFieldsSelected , const QString & errorMessage , QObject * parent )
2017-03-06 12:15:33 -06:00
: QValidator ( parent ) ,
m_usedIds ( usedIds ) ,
m_nextValidValue ( 0 ) ,
m_multipleSelectionOfSameFieldsSelected ( multipleSelectionOfSameFieldsSelected ) ,
m_errorMessage ( errorMessage )
2015-07-29 07:19:43 -05:00
{
computeNextValidId ( ) ;
}
virtual State validate ( QString & currentString , int & ) const
{
if ( m_multipleSelectionOfSameFieldsSelected )
{
return QValidator : : Invalid ;
}
if ( currentString . isEmpty ( ) )
{
return QValidator : : Intermediate ;
}
bool isValidInteger = false ;
int currentValue = currentString . toInt ( & isValidInteger ) ;
if ( ! isValidInteger )
{
return QValidator : : Invalid ;
}
if ( currentValue < 0 )
{
return QValidator : : Invalid ;
}
if ( m_usedIds . find ( currentValue ) ! = m_usedIds . end ( ) )
{
QApplication * qapplication = qobject_cast < QApplication * > ( qApp ) ;
foreach ( QWidget * widget , qapplication - > topLevelWidgets ( ) )
{
if ( widget - > inherits ( " QMainWindow " ) )
{
QMainWindow * mainWindow = qobject_cast < QMainWindow * > ( widget ) ;
if ( mainWindow & & mainWindow - > statusBar ( ) )
{
mainWindow - > statusBar ( ) - > showMessage ( m_errorMessage , 3000 ) ;
}
}
}
return QValidator : : Intermediate ;
}
return QValidator : : Acceptable ;
}
virtual void fixup ( QString & editorText ) const
{
editorText = QString : : number ( m_nextValidValue ) ;
}
private :
int computeNextValidId ( )
{
if ( ! m_usedIds . empty ( ) )
{
m_nextValidValue = * m_usedIds . rbegin ( ) ;
}
else
{
m_nextValidValue = 1 ;
}
return m_nextValidValue ;
}
private :
std : : set < int > m_usedIds ;
int m_nextValidValue ;
bool m_multipleSelectionOfSameFieldsSelected ;
QString m_errorMessage ;
} ;
2012-06-26 09:10:41 -05:00
namespace caf
{
CAF_PDM_UI_FIELD_EDITOR_SOURCE_INIT ( PdmUiLineEditor ) ;
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiLineEditor : : configureAndUpdateUi ( const QString & uiConfigName )
{
2015-07-29 07:19:43 -05:00
if ( ! m_label . isNull ( ) )
2012-06-26 09:10:41 -05:00
{
2015-07-29 07:19:43 -05:00
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 - > setEnabled ( ! field ( ) - > isUiReadOnly ( uiConfigName ) ) ;
m_label - > setToolTip ( field ( ) - > uiToolTip ( uiConfigName ) ) ;
2012-06-26 09:10:41 -05:00
}
2015-07-29 07:19:43 -05:00
if ( ! m_lineEdit . isNull ( ) )
2012-06-26 09:10:41 -05:00
{
2017-02-20 08:32:23 -06:00
bool isReadOnly = field ( ) - > isUiReadOnly ( uiConfigName ) ;
if ( isReadOnly )
{
m_lineEdit - > setReadOnly ( true ) ;
m_lineEdit - > setStyleSheet ( " QLineEdit { "
" color: #808080; "
" background-color: #F0F0F0;} " ) ;
}
else
{
m_lineEdit - > setReadOnly ( false ) ;
m_lineEdit - > setStyleSheet ( " " ) ;
}
2015-07-29 07:19:43 -05:00
m_lineEdit - > setToolTip ( field ( ) - > uiToolTip ( uiConfigName ) ) ;
2012-06-26 09:10:41 -05:00
2015-07-29 07:19:43 -05:00
{
PdmUiLineEditorAttribute leab ;
caf : : PdmUiObjectHandle * uiObject = uiObj ( field ( ) - > fieldHandle ( ) - > ownerObject ( ) ) ;
if ( uiObject )
{
uiObject - > editorAttribute ( field ( ) - > fieldHandle ( ) , uiConfigName , & leab ) ;
}
2013-09-26 03:41:08 -05:00
2015-07-29 07:19:43 -05:00
if ( leab . useRangeValidator )
{
m_lineEdit - > setValidator ( new QIntValidator ( leab . minValue , leab . maxValue , this ) ) ;
}
}
2012-06-26 09:10:41 -05:00
2015-07-29 07:19:43 -05:00
{
PdmUiLineEditorAttributeUniqueValues leab ;
caf : : PdmUiObjectHandle * uiObject = uiObj ( field ( ) - > fieldHandle ( ) - > ownerObject ( ) ) ;
if ( uiObject )
{
uiObject - > editorAttribute ( field ( ) - > fieldHandle ( ) , uiConfigName , & leab ) ;
}
if ( leab . usedIds . size ( ) > 0 )
{
if ( isMultipleFieldsWithSameKeywordSelected ( field ( ) - > fieldHandle ( ) ) )
{
QMessageBox : : information ( NULL , " Invalid operation " , " The field you are manipulating is defined to have unique values. A selection of multiple fields is detected. Please select a single item. " ) ;
}
2012-06-26 09:10:41 -05:00
2015-07-29 07:19:43 -05:00
m_lineEdit - > setValidator ( new PdmUniqueIdValidator ( leab . usedIds , isMultipleFieldsWithSameKeywordSelected ( field ( ) - > fieldHandle ( ) ) , leab . errorMessage , this ) ) ;
}
}
2017-02-09 06:15:36 -06:00
bool fromMenuOnly = true ;
2015-07-29 07:19:43 -05:00
QList < PdmOptionItemInfo > enumNames = field ( ) - > valueOptions ( & fromMenuOnly ) ;
2017-03-08 01:19:51 -06:00
CAF_ASSERT ( fromMenuOnly ) ; // Not supported
2017-02-09 06:15:36 -06:00
2015-07-29 07:19:43 -05:00
if ( ! enumNames . isEmpty ( ) & & fromMenuOnly = = true )
{
int enumValue = field ( ) - > uiValue ( ) . toInt ( ) ;
2015-10-23 02:19:17 -05:00
m_lineEdit - > setText ( enumNames [ enumValue ] . optionUiText ) ;
2015-07-29 07:19:43 -05:00
}
else
{
PdmUiLineEditorAttributeUiDisplayString leab ;
caf : : PdmUiObjectHandle * uiObject = uiObj ( field ( ) - > fieldHandle ( ) - > ownerObject ( ) ) ;
if ( uiObject )
{
uiObject - > editorAttribute ( field ( ) - > fieldHandle ( ) , uiConfigName , & leab ) ;
}
QString displayString ;
if ( leab . m_displayString . isEmpty ( ) )
{
displayString = field ( ) - > uiValue ( ) . toString ( ) ;
}
else
{
displayString = leab . m_displayString ;
}
m_lineEdit - > setText ( displayString ) ;
}
2012-06-26 09:10:41 -05:00
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QWidget * PdmUiLineEditor : : createEditorWidget ( QWidget * parent )
{
m_lineEdit = new QLineEdit ( parent ) ;
2017-02-20 08:32:23 -06:00
2012-06-26 09:10:41 -05:00
connect ( m_lineEdit , SIGNAL ( editingFinished ( ) ) , this , SLOT ( slotEditingFinished ( ) ) ) ;
2017-02-20 08:32:23 -06:00
2012-06-26 09:10:41 -05:00
return m_lineEdit ;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QWidget * PdmUiLineEditor : : createLabelWidget ( QWidget * parent )
{
m_label = new QLabel ( parent ) ;
return m_label ;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiLineEditor : : slotEditingFinished ( )
{
QVariant v ;
QString textValue = m_lineEdit - > text ( ) ;
v = textValue ;
this - > setValueToField ( v ) ;
}
2015-07-29 07:19:43 -05:00
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool PdmUiLineEditor : : isMultipleFieldsWithSameKeywordSelected ( PdmFieldHandle * editorField ) const
{
std : : vector < PdmFieldHandle * > fieldsToUpdate ;
fieldsToUpdate . push_back ( editorField ) ;
// For current selection, find all fields with same keyword
std : : vector < PdmUiItem * > items ;
SelectionManager : : instance ( ) - > selectedItems ( items , SelectionManager : : CURRENT ) ;
2015-08-13 08:23:33 -05:00
for ( size_t i = 0 ; i < items . size ( ) ; i + + )
2015-07-29 07:19:43 -05:00
{
PdmUiFieldHandle * uiField = dynamic_cast < PdmUiFieldHandle * > ( items [ i ] ) ;
if ( ! uiField ) continue ;
PdmFieldHandle * field = uiField - > fieldHandle ( ) ;
if ( field & & field ! = editorField & & field - > keyword ( ) = = editorField - > keyword ( ) )
{
fieldsToUpdate . push_back ( field ) ;
}
}
if ( fieldsToUpdate . size ( ) > 1 )
{
return true ;
}
return false ;
}
2012-06-26 09:10:41 -05:00
} // end namespace caf