Skip to content

Commit

Permalink
[needs-docs] Use a QgsFileWidget for template file selector
Browse files Browse the repository at this point in the history
Rename "Add" button to "Create"
  • Loading branch information
nyalldawson committed Dec 6, 2017
1 parent a30dd8a commit e5e37fc
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 46 deletions.
4 changes: 2 additions & 2 deletions python/gui/qgsfilewidget.sip
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ returns if the relative path is with respect to the project path or the default
determines if the relative path is with respect to the project path or the default path
%End

QLineEdit *lineEdit();
QgsFilterLineEdit *lineEdit();
%Docstring
Returns a pointer to the widget's line edit, which can be used to customize
the appearance and behavior of the line edit portion of the widget.
.. versionadded:: 3.0
:rtype: QLineEdit
:rtype: QgsFilterLineEdit
%End

signals:
Expand Down
41 changes: 17 additions & 24 deletions src/app/layout/qgslayoutmanagerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,25 @@ QgsLayoutManagerDialog::QgsLayoutManagerDialog( QWidget *parent, Qt::WindowFlags
setupUi( this );
connect( mAddButton, &QPushButton::clicked, this, &QgsLayoutManagerDialog::mAddButton_clicked );
connect( mTemplate, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutManagerDialog::mTemplate_currentIndexChanged );
connect( mTemplatePathBtn, &QPushButton::pressed, this, &QgsLayoutManagerDialog::mTemplatePathBtn_pressed );
connect( mTemplatesDefaultDirBtn, &QPushButton::pressed, this, &QgsLayoutManagerDialog::mTemplatesDefaultDirBtn_pressed );
connect( mTemplatesUserDirBtn, &QPushButton::pressed, this, &QgsLayoutManagerDialog::mTemplatesUserDirBtn_pressed );

QgsGui::instance()->enableAutoGeometryRestore( this );
mTemplateFileWidget->setStorageMode( QgsFileWidget::GetFile );
mTemplateFileWidget->setFilter( tr( "Layout templates" ) + QStringLiteral( " (*.qpt *.QPT)" ) );
mTemplateFileWidget->setDialogTitle( tr( "Select a Template" ) );
mTemplateFileWidget->lineEdit()->setShowClearButton( false );
QgsSettings settings;
mTemplateFileWidget->setDefaultRoot( settings.value( QStringLiteral( "UI/lastComposerTemplateDir" ), QString() ).toString() );
mTemplateFileWidget->setFilePath( settings.value( QStringLiteral( "UI/ComposerManager/templatePath" ), QString() ).toString() );

connect( mTemplateFileWidget, &QgsFileWidget::fileChanged, this, [ = ]
{
QgsSettings settings;
settings.setValue( QStringLiteral( "UI/ComposerManager/templatePath" ), mTemplateFileWidget->filePath() );
QFileInfo tmplFileInfo( mTemplateFileWidget->filePath() );
settings.setValue( QStringLiteral( "UI/lastComposerTemplateDir" ), tmplFileInfo.absolutePath() );
} );

mModel = new QgsLayoutManagerModel( QgsProject::instance()->layoutManager(),
this );
Expand Down Expand Up @@ -85,9 +99,6 @@ QgsLayoutManagerDialog::QgsLayoutManagerDialog( QWidget *parent, Qt::WindowFlags
this->addTemplates( defaultTemplateMap );
this->addTemplates( this->otherTemplates() );

QgsSettings settings;
mTemplatePathLineEdit->setText( settings.value( QStringLiteral( "UI/ComposerManager/templatePath" ), QString() ).toString() );

toggleButtons();
}

Expand Down Expand Up @@ -193,7 +204,7 @@ void QgsLayoutManagerDialog::mAddButton_clicked()
{
if ( mTemplate->currentIndex() == 1 )
{
templateFile.setFileName( mTemplatePathLineEdit->text() );
templateFile.setFileName( mTemplateFileWidget->filePath() );
}
else
{
Expand Down Expand Up @@ -254,25 +265,7 @@ void QgsLayoutManagerDialog::mAddButton_clicked()
void QgsLayoutManagerDialog::mTemplate_currentIndexChanged( int indx )
{
bool specific = ( indx == 1 ); // comes just after empty template
mTemplatePathLineEdit->setEnabled( specific );
mTemplatePathBtn->setEnabled( specific );
}

void QgsLayoutManagerDialog::mTemplatePathBtn_pressed()
{
QgsSettings settings;
QString lastTmplDir = settings.value( QStringLiteral( "UI/lastComposerTemplateDir" ), QDir::homePath() ).toString();
QString tmplPath = QFileDialog::getOpenFileName( this,
tr( "Choose template" ),
lastTmplDir,
tr( "Layout templates" ) + QStringLiteral( " (*.qpt *.QPT)" ) );
if ( !tmplPath.isEmpty() )
{
mTemplatePathLineEdit->setText( tmplPath );
settings.setValue( QStringLiteral( "UI/ComposerManager/templatePath" ), tmplPath );
QFileInfo tmplFileInfo( tmplPath );
settings.setValue( QStringLiteral( "UI/lastComposerTemplateDir" ), tmplFileInfo.absolutePath() );
}
mTemplateFileWidget->setEnabled( specific );
}

void QgsLayoutManagerDialog::mTemplatesDefaultDirBtn_pressed()
Expand Down
2 changes: 0 additions & 2 deletions src/app/layout/qgslayoutmanagerdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ class QgsLayoutManagerDialog: public QDialog, private Ui::QgsLayoutManagerBase
void mAddButton_clicked();
//! Slot to track combobox to use specific template path
void mTemplate_currentIndexChanged( int indx );
//! Slot to choose path to template
void mTemplatePathBtn_pressed();
//! Slot to open default templates dir with user's system
void mTemplatesDefaultDirBtn_pressed();
//! Slot to open user templates dir with user's system
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsfilewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ void QgsFileWidget::setRelativeStorage( QgsFileWidget::RelativeStorage relativeS
mRelativeStorage = relativeStorage;
}

QLineEdit *QgsFileWidget::lineEdit()
QgsFilterLineEdit *QgsFileWidget::lineEdit()
{
return mLineEdit;
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/qgsfilewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class GUI_EXPORT QgsFileWidget : public QWidget
* the appearance and behavior of the line edit portion of the widget.
* \since QGIS 3.0
*/
QLineEdit *lineEdit();
QgsFilterLineEdit *lineEdit();

signals:
//! emitted as soon as the current file or directory is changed
Expand Down
33 changes: 17 additions & 16 deletions src/ui/layout/qgslayoutmanagerbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,14 @@
</sizepolicy>
</property>
<property name="text">
<string>Add</string>
<string>Create</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QComboBox" name="mTemplate"/>
</item>
<item row="1" column="0">
<widget class="QLineEdit" name="mTemplatePathLineEdit"/>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="mTemplatePathBtn">
<property name="text">
<string>…</string>
<item row="1" column="0" colspan="3">
<widget class="QgsFileWidget" name="mTemplateFileWidget" native="true">
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
</widget>
</item>
Expand Down Expand Up @@ -98,7 +92,7 @@
</sizepolicy>
</property>
<property name="text">
<string>user</string>
<string>User</string>
</property>
</widget>
</item>
Expand All @@ -111,7 +105,7 @@
</sizepolicy>
</property>
<property name="text">
<string>default</string>
<string>Default</string>
</property>
</widget>
</item>
Expand All @@ -130,6 +124,9 @@
</item>
</layout>
</item>
<item row="0" column="0" colspan="2">
<widget class="QComboBox" name="mTemplate"/>
</item>
</layout>
</widget>
</item>
Expand All @@ -152,17 +149,21 @@
<header>qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsFileWidget</class>
<extends>QWidget</extends>
<header>qgsfilewidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>mLayoutListView</tabstop>
<tabstop>mTemplateGrpBox</tabstop>
<tabstop>mTemplate</tabstop>
<tabstop>mAddButton</tabstop>
<tabstop>mTemplatePathLineEdit</tabstop>
<tabstop>mTemplatePathBtn</tabstop>
<tabstop>mTemplateFileWidget</tabstop>
<tabstop>mTemplatesUserDirBtn</tabstop>
<tabstop>mTemplatesDefaultDirBtn</tabstop>
<tabstop>mButtonBox</tabstop>
</tabstops>
<resources/>
<connections>
Expand Down

0 comments on commit e5e37fc

Please sign in to comment.