Skip to content

Commit

Permalink
Merge pull request #2498 from elpaso/pythoninitcode
Browse files Browse the repository at this point in the history
Refactored the python init function selector
  • Loading branch information
elpaso committed Dec 5, 2015
2 parents de3ce15 + 3ca5441 commit 3d311d6
Show file tree
Hide file tree
Showing 9 changed files with 393 additions and 183 deletions.
45 changes: 32 additions & 13 deletions python/core/qgseditformconfig.sip
Expand Up @@ -57,6 +57,18 @@ class QgsEditFormConfig : QObject
SuppressOff = 2 //!< Do not suppress feature form
};

/**
* The python init code source options.
*/
enum PythonInitCodeSource
{
CodeSourceNone = 0, //!< Do not use python code at all
CodeSourceFile = 1, //!< Load the python code from a file
CodeSourceDialog = 2, //!< Use the python code provided in the dialog
CodeSourceEnvironment = 3 //!< Use the python code available in the python environment
};


/**
* This is only useful in combination with EditorLayout::TabLayout.
*
Expand Down Expand Up @@ -208,15 +220,6 @@ class QgsEditFormConfig : QObject
void setLabelOnTop( int idx, bool onTop );











// Python stuff


Expand All @@ -237,22 +240,38 @@ class QgsEditFormConfig : QObject
void setInitFunction( const QString& function );

/**
* Get python code for edit form initialization.
* Get python code for edit form initialization from the configuration dialog.
*/
QString initCode() const;

/**
* Get python external file path for edit form initialization.
*/
QString initFilePath() const;

/**
* Set python external file path for edit form initialization.
* Make sure that you also set the appropriate function name in
* @link setInitFunction @endlink
*/
void setInitFilePath( const QString& filePath );

/**
* Get python code for edit form initialization.
* Make sure that you also set the appropriate function name in
* @link setInitFunction @endlink
*/
void setInitCode( const QString& code );

/** Return if python code shall be loaded for edit form initialization */
bool useInitCode() const;
/** Return python code source for edit form initialization
* (if it shall be loaded from a file, read from the
* provided dialog editor or just read from the environment
*/
PythonInitCodeSource initCodeSource() const;

/** Set if python code shall be used for edit form initialization */
void setUseInitCode( const bool useCode );
void setInitCodeSource( const PythonInitCodeSource initCodeSource );


/** Type of feature form pop-up suppression after feature creation (overrides app setting) */
FeatureFormSuppress suppress() const;
Expand Down
64 changes: 49 additions & 15 deletions src/app/qgsfieldsproperties.cpp
Expand Up @@ -115,6 +115,12 @@ QgsFieldsProperties::QgsFieldsProperties( QgsVectorLayer *layer, QWidget* parent
mRelationsList->setHorizontalHeaderItem( RelFieldCol, new QTableWidgetItem( tr( "Field" ) ) );
mRelationsList->verticalHeader()->hide();

// Init function stuff
mInitCodeSourceComboBox->addItem( tr( "" ) );
mInitCodeSourceComboBox->addItem( tr( "Load from external file" ) );
mInitCodeSourceComboBox->addItem( tr( "Provide code in this dialog" ) );
mInitCodeSourceComboBox->addItem( tr( "Load from the environment" ) );

loadRelations();

updateButtons();
Expand Down Expand Up @@ -183,11 +189,15 @@ QTreeWidgetItem *QgsFieldsProperties::loadAttributeEditorTreeItem( QgsAttributeE
return newWidget;
}

void QgsFieldsProperties::setEditFormInit( const QString &editForm, const QString &editFormInit, const QString &editFormInitCode, const bool editFormInitUseCode )
void QgsFieldsProperties::setEditFormInit( const QString &editForm,
const QString &initFunction,
const QString &initCode,
const QString &initFilePath,
const QgsEditFormConfig::PythonInitCodeSource &codeSource )
{

// Python init function and code
QString code( editFormInitCode );
QString code( initCode );
if ( code.isEmpty( ) )
{
code.append( tr( "# -*- coding: utf-8 -*-\n\"\"\"\n"
Expand All @@ -206,12 +216,11 @@ void QgsFieldsProperties::setEditFormInit( const QString &editForm, const QStrin
"\tcontrol = dialog.findChild(QWidget, \"MyLineEdit\")\n" ) );

}
leEditForm->setText( editForm );
leEditFormInitCode->setText( code );
leEditFormInit->setText( editFormInit );
leEditFormInitUseCode->setChecked( editFormInitUseCode );
// Show or hide as needed
mPythonInitCodeGroupBox->setVisible( editFormInitUseCode );
mEditFormLineEdit->setText( editForm );
mInitFilePathLineEdit->setText( initFilePath );
mInitCodeEditorPython->setText( code );
mInitFunctionLineEdit->setText( initFunction );
mInitCodeSourceComboBox->setCurrentIndex( codeSource );
}


Expand Down Expand Up @@ -453,9 +462,14 @@ void QgsFieldsProperties::on_mMoveUpItem_clicked()
}
}

void QgsFieldsProperties::on_leEditFormInitUseCode_toggled( bool checked )
void QgsFieldsProperties::on_mInitCodeSourceComboBox_currentIndexChanged( int codeSource )
{
mPythonInitCodeGroupBox->setVisible( checked );
// Show or hide ui elements as needed
mInitFunctionContainer->setVisible( codeSource != QgsEditFormConfig::PythonInitCodeSource::CodeSourceNone );
mPythonInitCodeGroupBox->setVisible( codeSource == QgsEditFormConfig::PythonInitCodeSource::CodeSourceDialog );
mInitFilePathLineEdit->setVisible( codeSource == QgsEditFormConfig::PythonInitCodeSource::CodeSourceFile );
mInitFilePathLabel->setVisible( codeSource == QgsEditFormConfig::PythonInitCodeSource::CodeSourceFile );
pbtnSelectInitFilePath->setVisible( codeSource == QgsEditFormConfig::PythonInitCodeSource::CodeSourceFile );
}

void QgsFieldsProperties::attributeTypeDialog()
Expand Down Expand Up @@ -805,6 +819,22 @@ QgsAttributeEditorElement* QgsFieldsProperties::createAttributeEditorWidget( QTr
return widgetDef;
}


void QgsFieldsProperties::on_pbtnSelectInitFilePath_clicked()
{
QSettings myQSettings;
QString lastUsedDir = myQSettings.value( "style/lastUIDir", "." ).toString();
QString pyfilename = QFileDialog::getOpenFileName( this, tr( "Select Python file" ), lastUsedDir, tr( "Python file" ) + " (*.py)" );

if ( pyfilename.isNull() )
return;

QFileInfo fi( pyfilename );
myQSettings.setValue( "style/lastUIDir", fi.path() );
mInitFilePathLineEdit->setText( pyfilename );
}


void QgsFieldsProperties::on_pbnSelectEditForm_clicked()
{
QSettings myQSettings;
Expand All @@ -816,7 +846,7 @@ void QgsFieldsProperties::on_pbnSelectEditForm_clicked()

QFileInfo fi( uifilename );
myQSettings.setValue( "style/lastUIDir", fi.path() );
leEditForm->setText( uifilename );
mEditFormLineEdit->setText( uifilename );
}

void QgsFieldsProperties::on_mEditorLayoutComboBox_currentIndexChanged( int index )
Expand Down Expand Up @@ -875,10 +905,14 @@ void QgsFieldsProperties::apply()

mLayer->editFormConfig()->setLayout(( QgsEditFormConfig::EditorLayout ) mEditorLayoutComboBox->currentIndex() );
if ( mEditorLayoutComboBox->currentIndex() == QgsEditFormConfig::UiFileLayout )
mLayer->editFormConfig()->setUiForm( leEditForm->text() );
mLayer->editFormConfig()->setInitFunction( leEditFormInit->text() );
mLayer->editFormConfig()->setUseInitCode( leEditFormInitUseCode->isChecked() );
mLayer->editFormConfig()->setInitCode( leEditFormInitCode->text() );
mLayer->editFormConfig()->setUiForm( mEditFormLineEdit->text() );

// Init function configuration
mLayer->editFormConfig()->setInitFunction( mInitFunctionLineEdit->text( ) );
mLayer->editFormConfig()->setInitCode( mInitCodeEditorPython->text( ) );
mLayer->editFormConfig()->setInitFilePath( mInitFilePathLineEdit->text( ) );
mLayer->editFormConfig()->setInitCodeSource(( QgsEditFormConfig::PythonInitCodeSource )mInitCodeSourceComboBox->currentIndex() );

mLayer->editFormConfig()->setSuppress(( QgsEditFormConfig::FeatureFormSuppress )mFormSuppressCmbBx->currentIndex() );

mLayer->setExcludeAttributesWMS( excludeAttributesWMS );
Expand Down
16 changes: 11 additions & 5 deletions src/app/qgsfieldsproperties.h
Expand Up @@ -163,11 +163,16 @@ class APP_EXPORT QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPrope
/**
* @brief setEditFormInit set the private ui fields
* @param editForm
* @param editFormInit
* @param editFormInitCode
* @param editFormInitUseCode
* @param initFunction
* @param initCode
* @param initFilePath
* @param codeSource
*/
void setEditFormInit( const QString &editForm, const QString &editFormInit, const QString &editFormInitCode, const bool editFormInitUseCode );
void setEditFormInit( const QString &editForm,
const QString &initFunction,
const QString &initCode,
const QString &initFilePath,
const QgsEditFormConfig::PythonInitCodeSource &codeSource );

signals:
void toggleEditing();
Expand All @@ -177,9 +182,10 @@ class APP_EXPORT QgsFieldsProperties : public QWidget, private Ui_QgsFieldsPrope
void on_mDeleteAttributeButton_clicked();
void on_mCalculateFieldButton_clicked();
void onAttributeSelectionChanged();
void on_pbtnSelectInitFilePath_clicked();
void on_pbnSelectEditForm_clicked();
void on_mEditorLayoutComboBox_currentIndexChanged( int index );
void on_leEditFormInitUseCode_toggled( bool checked );
void on_mInitCodeSourceComboBox_currentIndexChanged( int codeSource );
void attributeAdded( int idx );
void attributeDeleted( int idx );
void attributeTypeDialog();
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -1317,5 +1317,5 @@ void QgsVectorLayerProperties::updateVariableEditor()
void QgsVectorLayerProperties::updateFieldsPropertiesDialog()
{
QgsEditFormConfig* cfg = layer->editFormConfig();
mFieldsPropertiesDialog->setEditFormInit( cfg->uiForm(), cfg->initFunction(), cfg->initCode(), cfg->useInitCode() );
mFieldsPropertiesDialog->setEditFormInit( cfg->uiForm(), cfg->initFunction(), cfg->initCode(), cfg->initFilePath(), cfg->initCodeSource() );
}
2 changes: 1 addition & 1 deletion src/core/qgseditformconfig.cpp
Expand Up @@ -4,8 +4,8 @@
QgsEditFormConfig::QgsEditFormConfig( QObject* parent )
: QObject( parent )
, mEditorLayout( GeneratedLayout )
, mInitCodeSource( QgsEditFormConfig::PythonInitCodeSource::CodeSourceNone )
, mFeatureFormSuppress( SuppressDefault )
, mUseInitCode( false )
{
connect( QgsProject::instance()->relationManager(), SIGNAL( relationsLoaded() ), this, SLOT( onRelationsLoaded() ) );
}
Expand Down
62 changes: 43 additions & 19 deletions src/core/qgseditformconfig.h
Expand Up @@ -271,6 +271,7 @@ class CORE_EXPORT QgsEditFormConfig : public QObject
Q_OBJECT

public:

/** The different types to layout the attribute editor. */
enum EditorLayout
{
Expand Down Expand Up @@ -308,6 +309,17 @@ class CORE_EXPORT QgsEditFormConfig : public QObject
SuppressOff = 2 //!< Do not suppress feature form
};

/**
* The python init code source options.
*/
enum PythonInitCodeSource
{
CodeSourceNone = 0, //!< Do not use python code at all
CodeSourceFile = 1, //!< Load the python code from an external file
CodeSourceDialog = 2, //!< Use the python code provided in the dialog
CodeSourceEnvironment = 3 //!< Use the python code available in the python environment
};

/**
* This is only useful in combination with EditorLayout::TabLayout.
*
Expand Down Expand Up @@ -459,17 +471,7 @@ class CORE_EXPORT QgsEditFormConfig : public QObject
void setLabelOnTop( int idx, bool onTop );











// Python stuff

// Python form init function stuff

/**
* Get python function for edit form initialization.
Expand All @@ -490,20 +492,35 @@ class CORE_EXPORT QgsEditFormConfig : public QObject
/**
* Get python code for edit form initialization.
*/
QString initCode() const { return mEditFormInitCode; }
QString initCode() const { return mInitCode; }

/**
* Get python code for edit form initialization.
* Set python code for edit form initialization.
* Make sure that you also set the appropriate function name in
* @link setInitFunction @endlink
*/
void setInitCode( const QString& code ) { mEditFormInitCode = code; }
void setInitCode( const QString& code ) { mInitCode = code; }

/**
* Get python external file path for edit form initialization.
*/
QString initFilePath() const { return mInitFilePath; }

/** Return if python code shall be loaded for edit form initialization */
bool useInitCode() const { return mUseInitCode; }
/**
* Set python external file path for edit form initialization.
* Make sure that you also set the appropriate function name in
* @link setInitFunction @endlink
*/
void setInitFilePath( const QString& filePath ) { mInitFilePath = filePath; }

/** Return python code source for edit form initialization
* (if it shall be loaded from a file, read from the
* provided dialog editor or inherited from the environment)
*/
PythonInitCodeSource initCodeSource() const { return mInitCodeSource; }

/** Set if python code shall be used for edit form initialization */
void setUseInitCode( const bool useCode ) { mUseInitCode = useCode; }
/** Set if python code shall be used for edit form initialization and its origin */
void setInitCodeSource( const PythonInitCodeSource initCodeSource ) { mInitCodeSource = initCodeSource; }

/** Type of feature form pop-up suppression after feature creation (overrides app setting) */
FeatureFormSuppress suppress() const { return mFeatureFormSuppress; }
Expand Down Expand Up @@ -547,9 +564,16 @@ class CORE_EXPORT QgsEditFormConfig : public QObject
/** Defines the default layout to use for the attribute editor (Drag and drop, UI File, Generated) */
EditorLayout mEditorLayout;

/** Init form instance */
QString mEditForm;
/** Name of the python form init function */
QString mInitFunction;
QString mEditFormInitCode;
/** Path of the python external file to be loaded */
QString mInitFilePath;
/** Choose the source of the init founction */
PythonInitCodeSource mInitCodeSource;
/** Python init code provided in the dialog */
QString mInitCode;

/** Type of feature form suppression after feature creation */
FeatureFormSuppress mFeatureFormSuppress;
Expand Down

0 comments on commit 3d311d6

Please sign in to comment.