From 25d808f7ea5e070d3b3293c6d43f9da7582e2a93 Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Mon, 1 Sep 2014 23:06:23 +0200 Subject: [PATCH] Added first iteration of the file path property manager / editor / factory. --HG-- branch : dfighter-tools --- .../src/plugins/georges_editor/CMakeLists.txt | 1 + .../georges_editor/dfn_browser_ctrl.cpp | 19 +- .../plugins/georges_editor/dfn_browser_ctrl.h | 5 + .../filepath_property_manager.cpp | 302 ++++++++++++++++++ .../filepath_property_manager.h | 98 ++++++ 5 files changed, 422 insertions(+), 3 deletions(-) create mode 100644 code/studio/src/plugins/georges_editor/filepath_property_manager.cpp create mode 100644 code/studio/src/plugins/georges_editor/filepath_property_manager.h diff --git a/code/studio/src/plugins/georges_editor/CMakeLists.txt b/code/studio/src/plugins/georges_editor/CMakeLists.txt index 62b9de685..36b3fd735 100644 --- a/code/studio/src/plugins/georges_editor/CMakeLists.txt +++ b/code/studio/src/plugins/georges_editor/CMakeLists.txt @@ -20,6 +20,7 @@ SET(OVQT_PLUG_GEORGES_EDITOR_HDR georges_editor_plugin.h browser_ctrl_pvt.h dfn_browser_ctrl.h georges_dfn_dialog.h + filepath_property_manager.h ) SET(OVQT_PLUG_GEORGES_EDITOR_UIS georges_editor_form.ui diff --git a/code/studio/src/plugins/georges_editor/dfn_browser_ctrl.cpp b/code/studio/src/plugins/georges_editor/dfn_browser_ctrl.cpp index fbe672554..398794fb5 100644 --- a/code/studio/src/plugins/georges_editor/dfn_browser_ctrl.cpp +++ b/code/studio/src/plugins/georges_editor/dfn_browser_ctrl.cpp @@ -4,6 +4,8 @@ #include "3rdparty/qtpropertybrowser/qteditorfactory.h" #include "3rdparty/qtpropertybrowser/qtpropertymanager.h" +#include "filepath_property_manager.h" + #include "nel/georges/form_dfn.h" namespace @@ -60,6 +62,8 @@ QObject( parent ) m_factory = new QtVariantEditorFactory(); m_enumMgr = new QtEnumPropertyManager(); m_enumFactory = new QtEnumEditorFactory(); + m_fileMgr = new FileManager(); + m_fileFactory = new FileEditFactory(); } DFNBrowserCtrl::~DFNBrowserCtrl() @@ -75,6 +79,10 @@ DFNBrowserCtrl::~DFNBrowserCtrl() m_enumMgr = NULL; delete m_enumFactory; m_enumFactory = NULL; + delete m_fileMgr; + m_fileMgr = NULL; + delete m_fileFactory; + m_fileFactory = NULL; } void DFNBrowserCtrl::onElementSelected( int idx ) @@ -84,6 +92,7 @@ void DFNBrowserCtrl::onElementSelected( int idx ) m_browser->clear(); m_browser->setFactoryForManager( m_manager, m_factory ); m_browser->setFactoryForManager( m_enumMgr, m_enumFactory ); + m_browser->setFactoryForManager( m_fileMgr, m_fileFactory ); QtVariantProperty *p = NULL; QtProperty *prop = NULL; @@ -111,9 +120,13 @@ void DFNBrowserCtrl::onElementSelected( int idx ) m_browser->addProperty( prop ); - p = m_manager->addProperty( QVariant::String, "value" ); - p->setValue( entry.getFilename().c_str() ); - m_browser->addProperty( p ); + prop = m_fileMgr->addProperty( "value" ); + m_fileMgr->setValue( prop, entry.getFilename().c_str() ); + m_browser->addProperty( prop ); + + //p = m_manager->addProperty( QVariant::String, "value" ); + //p->setValue( entry.getFilename().c_str() ); + //m_browser->addProperty( p ); p = m_manager->addProperty( QVariant::String, "default" ); p->setValue( entry.getDefault().c_str() ); diff --git a/code/studio/src/plugins/georges_editor/dfn_browser_ctrl.h b/code/studio/src/plugins/georges_editor/dfn_browser_ctrl.h index 178c35977..26fdf91e3 100644 --- a/code/studio/src/plugins/georges_editor/dfn_browser_ctrl.h +++ b/code/studio/src/plugins/georges_editor/dfn_browser_ctrl.h @@ -16,6 +16,8 @@ class QtProperty; class QtEnumPropertyManager; class QtEnumEditorFactory; +class FileManager; +class FileEditFactory; class DFNBrowserCtrl : public QObject { @@ -38,6 +40,9 @@ private: QtEnumPropertyManager *m_enumMgr; QtEnumEditorFactory *m_enumFactory; + + FileManager *m_fileMgr; + FileEditFactory *m_fileFactory; }; #endif diff --git a/code/studio/src/plugins/georges_editor/filepath_property_manager.cpp b/code/studio/src/plugins/georges_editor/filepath_property_manager.cpp new file mode 100644 index 000000000..30d0afcfe --- /dev/null +++ b/code/studio/src/plugins/georges_editor/filepath_property_manager.cpp @@ -0,0 +1,302 @@ +#include "filepath_property_manager.h" + +#include +#include +#include +#include +#include + + +///////////////////////////////////////////////////////////////////// Manager //////////////////////////////////////////////////////////////////// + + +class FileManagerPvt +{ +public: + QMap< const QtProperty*, QString > values; +}; + +FileManager::FileManager( QObject *parent ) : +QtAbstractPropertyManager( parent ) +{ + m_pvt = new FileManagerPvt(); +} + +FileManager::~FileManager() +{ + delete m_pvt; + m_pvt = NULL; +} + +QString FileManager::value( const QtProperty *p ) const +{ + QMap< const QtProperty*, QString >::const_iterator itr = m_pvt->values.find( p ); + if( itr == m_pvt->values.end() ) + return ""; + else + return itr.value(); +} + +void FileManager::setValue( QtProperty *p, const QString &v ) +{ + if( !m_pvt->values.contains( p ) ) + return; + + if( m_pvt->values[ p ] == v ) + return; + + m_pvt->values[ p ] = v; + + Q_EMIT propertyChanged( p ); + Q_EMIT valueChanged( p, v ); +} + +bool FileManager::hasValue( const QtProperty *p ) const +{ + if( m_pvt->values.contains( p ) ) + return true; + else + return false; +} + +QString FileManager::valueText( const QtProperty *p ) const +{ + return value( p ); +} + +void FileManager::initializeProperty( QtProperty *p ) +{ + if( m_pvt->values.contains( p ) ) + return; + + m_pvt->values[ p ] = ""; +} + +void FileManager::uninitializeProperty( QtProperty *p ) +{ + m_pvt->values.remove( p ); +} + +///////////////////////////////////////////////////////////////// Factory /////////////////////////////////////////////////////////////////////////////// + +class FileEditFactoryPvt +{ +public: + QMap< QtProperty*, QList< FileEdit* > > createdEditors; + QMap< FileEdit*, QtProperty* > editorToProperty; + + void addEditor( QtProperty *p, FileEdit *editor ) + { + editorToProperty[ editor ] = p; + + QMap< QtProperty*, QList< FileEdit* > >::iterator itr = createdEditors.find( p ); + if( itr == createdEditors.end() ) + { + QList< FileEdit* > l; + l.push_back( editor ); + createdEditors[ p ] = l; + } + else + { + QList< FileEdit* > &l = itr.value(); + l.push_back( editor ); + } + } + + void removeEditor( QObject *o ) + { + // Find in editorToProperty + QMap< FileEdit*, QtProperty* >::iterator itr = editorToProperty.begin(); + while( itr != editorToProperty.end() ) + { + if( itr.key() == o ) + break; + ++itr; + } + + // Store the property, and remove the editor from editorToProperty + QtProperty *p = NULL; + if( itr != editorToProperty.end() ) + { + p = itr.value(); + editorToProperty.erase( itr ); + } + + // Find the property in createdEditors + QMap< QtProperty*, QList< FileEdit* > >::iterator mitr = createdEditors.find( p ); + QList< FileEdit* > &l = mitr.value(); + + // Find the editor in the list + QList< FileEdit* >::iterator litr = l.begin(); + while( litr != l.end() ) + { + if( o == *litr ) + break; + litr++; + } + + // Remove the editor and remove the list too if it's empty + if( litr != l.end() ) + l.erase( litr ); + + if( l.isEmpty() ) + createdEditors.erase( mitr ); + } +}; + + +FileEditFactory::FileEditFactory( QObject *parent ) : +QtAbstractEditorFactory( parent ) +{ + m_pvt = new FileEditFactoryPvt(); +} + +FileEditFactory::~FileEditFactory() +{ + delete m_pvt; + m_pvt = NULL; +} + +void FileEditFactory::connectPropertyManager( FileManager *manager ) +{ + connect( manager, SIGNAL( valueChanged( QtProperty*, const QString& ) ), + this, SLOT( onPropertyChanged( QtProperty*, const QString& ) ) ); +} + +void FileEditFactory::disconnectPropertyManager( FileManager *manager ) +{ + disconnect( manager, SIGNAL( valueChanged( QtProperty*, const QString& ) ), + this, SLOT( onPropertyChanged( QtProperty*, const QString& ) ) ); +} + +QWidget* FileEditFactory::createEditor( FileManager *manager, QtProperty *p, QWidget *parent ) +{ + FileEdit *editor = new FileEdit( parent ); + editor->setValue( p->valueText() ); + + connect( editor, SIGNAL( valueChanged( const QString& ) ), this, SLOT( onSetValue( const QString& ) ) ); + connect( editor, SIGNAL( destroyed( QObject* ) ), this, SLOT( onEditorDestroyed( QObject* ) ) ); + + m_pvt->addEditor( p, editor ); + + return editor; +} + +void FileEditFactory::onPropertyChanged( QtProperty *p, const QString &v ) +{ + QMap< QtProperty*, QList< FileEdit* > >::iterator itr = m_pvt->createdEditors.find( p ); + if( itr == m_pvt->createdEditors.end() ) + return; + + QList< FileEdit* > &l = itr.value(); + QList< FileEdit* >::iterator litr = l.begin(); + while( litr != l.end() ) + { + FileEdit *editor = *litr; + editor->blockSignals( true ); + editor->setValue( v ); + editor->blockSignals( false ); + + ++litr; + } +} + +void FileEditFactory::onSetValue( const QString& v ) +{ + QObject *s = sender(); + FileEdit *editor = qobject_cast< FileEdit* >( s ); + if( editor == NULL ) + return; + + QMap< FileEdit*, QtProperty* >::iterator itr = m_pvt->editorToProperty.find( editor ); + if( itr == m_pvt->editorToProperty.end() ) + return; + + QtProperty *p = *itr; + FileManager *manager = qobject_cast< FileManager* >( p->propertyManager() ); + if( manager == NULL ) + return; + + blockSignals( true ); + manager->setValue( p, v ); + blockSignals( false ); +} + +void FileEditFactory::onEditorDestroyed( QObject *editor ) +{ + m_pvt->removeEditor( editor ); +} + + +//////////////////////////////////////////////////////////////// Editor ///////////////////////////////////////////////////////////////////////////////// + + +class FileEditPvt +{ +public: + QLineEdit *lineEdit; + QToolButton *toolButton; +}; + + + +FileEdit::FileEdit( QWidget *parent ) : +QWidget( parent ) +{ + m_pvt = new FileEditPvt(); + + setupUI(); + setupConnections(); +} + +FileEdit::~FileEdit() +{ + delete m_pvt; + m_pvt = NULL; + + Q_EMIT destroyed( this ); +} + +void FileEdit::setValue( const QString &value ) +{ + m_pvt->lineEdit->setText( value ); +} + +void FileEdit::onButtonClicked() +{ + QString file = QFileDialog::getOpenFileName( this, + tr( "" ), + tr( "" ) ); + if( file.isEmpty() ) + return; + + if( m_pvt->lineEdit->text() == file ) + return; + + m_pvt->lineEdit->setText( file ); + + Q_EMIT valueChanged( file ); +} + +void FileEdit::setupUI() +{ + m_pvt->lineEdit = new QLineEdit( this ); + m_pvt->toolButton = new QToolButton( this ); + m_pvt->toolButton->setText( "..." ); + + QHBoxLayout *layout = new QHBoxLayout( this ); + layout->setContentsMargins( 0, 0, 0, 0 ); + layout->setSpacing( 0 ); + layout->addWidget( m_pvt->lineEdit ); + layout->addWidget( m_pvt->toolButton ); + setLayout( layout ); + + setFocusProxy( m_pvt->lineEdit ); + setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Fixed ); +} + +void FileEdit::setupConnections() +{ + connect( m_pvt->toolButton, SIGNAL( clicked( bool ) ), this, SLOT( onButtonClicked() ) ); +} + diff --git a/code/studio/src/plugins/georges_editor/filepath_property_manager.h b/code/studio/src/plugins/georges_editor/filepath_property_manager.h new file mode 100644 index 000000000..0b1f5f469 --- /dev/null +++ b/code/studio/src/plugins/georges_editor/filepath_property_manager.h @@ -0,0 +1,98 @@ +#ifndef FILEPATH_PROPERTY_MANAGER +#define FILEPATH_PROPERTY_MANAGER + +#define QT_QTPROPERTYBROWSER_IMPORT + +#include +#include <3rdparty/qtpropertybrowser/qtpropertymanager.h> + +/////////////////////////////////////////////////////////////////////// Manager ///////////////////////////////////////////////////////////////// + +class FileManagerPvt; + +class FileManager : public QtAbstractPropertyManager +{ + Q_OBJECT +public: + FileManager( QObject *parent = NULL ); + ~FileManager(); + + QString value( const QtProperty *p ) const; + +public Q_SLOTS: + void setValue( QtProperty *p, const QString &v ); + +Q_SIGNALS: + void valueChanged( QtProperty *p, const QString &v ); + +protected: + bool hasValue( const QtProperty *p ) const; + QString valueText( const QtProperty *p ) const; + void initializeProperty( QtProperty *p ); + void uninitializeProperty( QtProperty *p ); + +private: + FileManagerPvt *m_pvt; + + Q_DISABLE_COPY( FileManager ); +}; + + + +//////////////////////////////////////////////////////// Factory /////////////////////////////////////////////////////////////////// + +class FileEditFactoryPvt; + +class FileEditFactory : public QtAbstractEditorFactory< FileManager > +{ + Q_OBJECT +public: + FileEditFactory( QObject *parent = NULL ); + ~FileEditFactory(); + +protected: + void connectPropertyManager( FileManager *manager ); + void disconnectPropertyManager( FileManager *manager ); + QWidget* createEditor( FileManager *manager, QtProperty *p, QWidget *parent ); + +private Q_SLOTS: + void onPropertyChanged( QtProperty *p, const QString &value ); + void onSetValue( const QString &value ); + void onEditorDestroyed( QObject *editor ); + +private: + FileEditFactoryPvt *m_pvt; + + Q_DISABLE_COPY( FileEditFactory ); +}; + + +//////////////////////////////////////////////////////// Editor //////////////////////////////////////////////////////////////////// + +class FileEditPvt; + +class FileEdit : public QWidget +{ + Q_OBJECT +public: + FileEdit( QWidget *parent = NULL ); + ~FileEdit(); + + void setValue( const QString &value ); + +Q_SIGNALS: + void valueChanged( const QString &value ); + void destroyed( QObject *editor ); + +private Q_SLOTS: + void onButtonClicked(); + +private: + void setupUI(); + void setupConnections(); + FileEditPvt *m_pvt; + + Q_DISABLE_COPY( FileEdit ); +}; + +#endif