Skip to content

Commit

Permalink
New gui class QgsLayoutComboBox
Browse files Browse the repository at this point in the history
Shows a list of layouts
  • Loading branch information
nyalldawson committed Mar 11, 2019
1 parent cba2277 commit 8d77a6b
Show file tree
Hide file tree
Showing 7 changed files with 436 additions and 0 deletions.
95 changes: 95 additions & 0 deletions python/gui/auto_generated/layout/qgslayoutcombobox.sip.in
@@ -0,0 +1,95 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/layout/qgslayoutcombobox.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/



class QgsLayoutComboBox : QComboBox
{
%Docstring
The QgsLayoutComboBox class is a combo box which displays available layouts from a QgsLayoutManager.

.. versionadded:: 3.8
%End

%TypeHeaderCode
#include "qgslayoutcombobox.h"
%End
public:

explicit QgsLayoutComboBox( QWidget *parent /TransferThis/ = 0, QgsLayoutManager *manager = 0 );
%Docstring
QgsLayoutComboBox creates a combo box to display a list of items in a
layout ``manager``. The layouts can optionally be filtered by type.
%End

void setLayoutManager( QgsLayoutManager *manager );
%Docstring
Sets the layout ``manager`` containing the layouts to list in the combo box.
%End

QgsLayoutManagerProxyModel::Filters filters() const;
%Docstring
Returns the current filters used for filtering available layouts.

.. seealso:: :py:func:`setFilters`
%End

void setFilters( QgsLayoutManagerProxyModel::Filters filters );
%Docstring
Sets the current ``filters`` used for filtering available layouts.

.. seealso:: :py:func:`filters`
%End

void setAllowEmptyLayout( bool allowEmpty );
%Docstring
Sets whether an optional empty layout ("not set") option is present in the combobox.

.. seealso:: :py:func:`allowEmptyLayout`
%End

bool allowEmptyLayout() const;
%Docstring
Returns ``True`` if the combobox includes the empty layout ("not set") choice.

.. seealso:: :py:func:`setAllowEmptyLayout`
%End

QgsMasterLayoutInterface *currentLayout() const;
%Docstring
Returns the layout currently selected in the combo box.
%End

QgsMasterLayoutInterface *layout( int index ) const;
%Docstring
Returns the layout at the specified ``index``.
%End

public slots:

void setCurrentLayout( QgsMasterLayoutInterface *layout );
%Docstring
Sets the currently selected ``layout`` in the combo box.
%End

signals:

void layoutChanged( QgsMasterLayoutInterface *layout );
%Docstring
Emitted whenever the currently selected layout changes
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/layout/qgslayoutcombobox.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
1 change: 1 addition & 0 deletions python/gui/gui_auto.sip
Expand Up @@ -302,6 +302,7 @@
%Include auto_generated/layertree/qgslayertreeview.sip
%Include auto_generated/layertree/qgslayertreeviewdefaultactions.sip
%Include auto_generated/layertree/qgslayertreeviewindicator.sip
%Include auto_generated/layout/qgslayoutcombobox.sip
%Include auto_generated/layout/qgslayoutcustomdrophandler.sip
%Include auto_generated/layout/qgslayoutdesignerinterface.sip
%Include auto_generated/layout/qgslayoutitemcombobox.sip
Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -165,6 +165,7 @@ SET(QGIS_GUI_SRCS
layertree/qgslayertreeviewindicator.cpp
layertree/qgslayertreeviewitemdelegate.cpp

layout/qgslayoutcombobox.cpp
layout/qgslayoutcustomdrophandler.cpp
layout/qgslayoutitemguiregistry.cpp
layout/qgslayoutitemcombobox.cpp
Expand Down Expand Up @@ -715,6 +716,7 @@ SET(QGIS_GUI_MOC_HDRS
layertree/qgslayertreeviewindicator.h
layertree/qgslayertreeviewitemdelegate.h

layout/qgslayoutcombobox.h
layout/qgslayoutcustomdrophandler.h
layout/qgslayoutdesignerinterface.h
layout/qgslayoutitemcombobox.h
Expand Down
122 changes: 122 additions & 0 deletions src/gui/layout/qgslayoutcombobox.cpp
@@ -0,0 +1,122 @@
/***************************************************************************
qgslayoutcombobox.cpp
--------------------------------------
Date : March 2019
Copyright : (C) 2019 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/


#include "qgslayoutcombobox.h"
#include "qgslayoutmodel.h"

QgsLayoutComboBox::QgsLayoutComboBox( QWidget *parent, QgsLayoutManager *manager )
: QComboBox( parent )
{
setLayoutManager( manager );

connect( this, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsLayoutComboBox::indexChanged );
}

void QgsLayoutComboBox::setLayoutManager( QgsLayoutManager *manager )
{
if ( mModel )
mModel->deleteLater();
if ( mProxyModel )
mProxyModel->deleteLater();

mModel = new QgsLayoutManagerModel( manager, this );
mProxyModel = new QgsLayoutManagerProxyModel( this );
mProxyModel->setSourceModel( mModel );

connect( mProxyModel, &QAbstractItemModel::rowsInserted, this, &QgsLayoutComboBox::rowsChanged );
connect( mProxyModel, &QAbstractItemModel::rowsRemoved, this, &QgsLayoutComboBox::rowsChanged );
setModel( mProxyModel );
mProxyModel->sort( 0, Qt::AscendingOrder );
}

QgsLayoutManagerProxyModel::Filters QgsLayoutComboBox::filters() const
{
return mProxyModel->filters();
}

void QgsLayoutComboBox::setFilters( QgsLayoutManagerProxyModel::Filters filters )
{
mProxyModel->setFilters( filters );
}

void QgsLayoutComboBox::setAllowEmptyLayout( bool allowEmpty )
{
mModel->setAllowEmptyLayout( allowEmpty );
}

bool QgsLayoutComboBox::allowEmptyLayout() const
{
return mModel->allowEmptyLayout();
}

void QgsLayoutComboBox::setCurrentLayout( QgsMasterLayoutInterface *layout )
{
if ( !mModel )
return;

QModelIndex idx = mModel->indexFromLayout( layout );
if ( idx.isValid() )
{
QModelIndex proxyIdx = mProxyModel->mapFromSource( idx );
if ( proxyIdx.isValid() )
{
setCurrentIndex( proxyIdx.row() );
return;
}
}
setCurrentIndex( allowEmptyLayout() ? 0 : -1 );
}

QgsMasterLayoutInterface *QgsLayoutComboBox::currentLayout() const
{
return layout( currentIndex() );
}

void QgsLayoutComboBox::indexChanged( int i )
{
Q_UNUSED( i );
emit layoutChanged( currentLayout() );
}

void QgsLayoutComboBox::rowsChanged()
{
if ( count() == 1 )
{
//currently selected item has changed
emit layoutChanged( currentLayout() );
}
else if ( count() == 0 )
{
emit layoutChanged( nullptr );
}
}

QgsMasterLayoutInterface *QgsLayoutComboBox::layout( int index ) const
{
const QModelIndex proxyIndex = mProxyModel->index( index, 0 );
if ( !proxyIndex.isValid() )
{
return nullptr;
}

QModelIndex sourceIndex = mProxyModel->mapToSource( proxyIndex );
if ( !sourceIndex.isValid() )
{
return nullptr;
}

return mModel->layoutFromIndex( sourceIndex );
}
105 changes: 105 additions & 0 deletions src/gui/layout/qgslayoutcombobox.h
@@ -0,0 +1,105 @@
/***************************************************************************
qgslayoutcombobox.h
--------------------------------------
Date : March 2019
Copyright : (C) 2019 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSLAYOUTCOMBOBOX_H
#define QGSLAYOUTCOMBOBOX_H

#include <QComboBox>
#include "qgis_sip.h"
#include "qgis_gui.h"
#include "qgslayoutmanager.h"

/**
* \class QgsLayoutComboBox
* \ingroup gui
* \brief The QgsLayoutComboBox class is a combo box which displays available layouts from a QgsLayoutManager.
* \since QGIS 3.8
*/
class GUI_EXPORT QgsLayoutComboBox : public QComboBox
{
Q_OBJECT

public:

/**
* QgsLayoutComboBox creates a combo box to display a list of items in a
* layout \a manager. The layouts can optionally be filtered by type.
*/
explicit QgsLayoutComboBox( QWidget *parent SIP_TRANSFERTHIS = nullptr, QgsLayoutManager *manager = nullptr );

/**
* Sets the layout \a manager containing the layouts to list in the combo box.
*/
void setLayoutManager( QgsLayoutManager *manager );

/**
* Returns the current filters used for filtering available layouts.
*
* \see setFilters()
*/
QgsLayoutManagerProxyModel::Filters filters() const;

/**
* Sets the current \a filters used for filtering available layouts.
*
* \see filters()
*/
void setFilters( QgsLayoutManagerProxyModel::Filters filters );

/**
* Sets whether an optional empty layout ("not set") option is present in the combobox.
* \see allowEmptyLayout()
*/
void setAllowEmptyLayout( bool allowEmpty );

/**
* Returns TRUE if the combobox includes the empty layout ("not set") choice.
* \see setAllowEmptyLayout()
*/
bool allowEmptyLayout() const;

/**
* Returns the layout currently selected in the combo box.
*/
QgsMasterLayoutInterface *currentLayout() const;

/**
* Returns the layout at the specified \a index.
*/
QgsMasterLayoutInterface *layout( int index ) const;

public slots:

/**
* Sets the currently selected \a layout in the combo box.
*/
void setCurrentLayout( QgsMasterLayoutInterface *layout );

signals:

//! Emitted whenever the currently selected layout changes
void layoutChanged( QgsMasterLayoutInterface *layout );

private slots:
void indexChanged( int i );
void rowsChanged();

private:
QgsLayoutManagerModel *mModel = nullptr;
QgsLayoutManagerProxyModel *mProxyModel = nullptr;

};

#endif // QGSLAYOUTCOMBOBOX_H
2 changes: 2 additions & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -87,6 +87,7 @@ ADD_PYTHON_TEST(PyQgsLayerTreeView test_qgslayertreeview.py)
ADD_PYTHON_TEST(PyQgsLayout test_qgslayout.py)
ADD_PYTHON_TEST(PyQgsLayoutAlign test_qgslayoutaligner.py)
ADD_PYTHON_TEST(PyQgsLayoutAtlas test_qgslayoutatlas.py)
ADD_PYTHON_TEST(PyQgsLayoutComboBox test_qgslayoutcombobox.py)
ADD_PYTHON_TEST(PyQgsLayoutExporter test_qgslayoutexporter.py)
ADD_PYTHON_TEST(PyQgsLayoutFrame test_qgslayoutframe.py)
ADD_PYTHON_TEST(PyQgsLayoutManager test_qgslayoutmanager.py)
Expand Down Expand Up @@ -307,3 +308,4 @@ IF (WITH_SERVER)
ADD_PYTHON_TEST(PyQgsServerRequest test_qgsserver_request.py)
ADD_PYTHON_TEST(PyQgsServerResponse test_qgsserver_response.py)
ENDIF (WITH_SERVER)

0 comments on commit 8d77a6b

Please sign in to comment.