mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-13 00:03:09 -04:00
New gui class QgsLayoutComboBox
Shows a list of layouts
This commit is contained in:
parent
cba22770dc
commit
8d77a6bdcc
95
python/gui/auto_generated/layout/qgslayoutcombobox.sip.in
Normal file
95
python/gui/auto_generated/layout/qgslayoutcombobox.sip.in
Normal file
@ -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 *
|
||||
************************************************************************/
|
@ -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
|
||||
|
@ -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
|
||||
@ -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
|
||||
|
122
src/gui/layout/qgslayoutcombobox.cpp
Normal file
122
src/gui/layout/qgslayoutcombobox.cpp
Normal file
@ -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
src/gui/layout/qgslayoutcombobox.h
Normal file
105
src/gui/layout/qgslayoutcombobox.h
Normal file
@ -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
|
@ -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)
|
||||
@ -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)
|
||||
|
||||
|
109
tests/src/python/test_qgslayoutcombobox.py
Normal file
109
tests/src/python/test_qgslayoutcombobox.py
Normal file
@ -0,0 +1,109 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""QGIS Unit tests for QgsLayoutComboBox
|
||||
|
||||
.. note:: 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.
|
||||
"""
|
||||
__author__ = '(C) 2019 by Nyall Dawson'
|
||||
__date__ = '11/03/2019'
|
||||
__copyright__ = 'Copyright 2019, The QGIS Project'
|
||||
# This will get replaced with a git SHA1 when you do a git archive
|
||||
__revision__ = '$Format:%H$'
|
||||
|
||||
import qgis # NOQA
|
||||
|
||||
from qgis.PyQt.QtXml import QDomDocument
|
||||
|
||||
from qgis.core import (QgsPrintLayout,
|
||||
QgsLayoutManager,
|
||||
QgsLayoutManagerModel,
|
||||
QgsLayoutManagerProxyModel,
|
||||
QgsProject,
|
||||
QgsReport,
|
||||
QgsMasterLayoutInterface)
|
||||
from qgis.gui import QgsLayoutComboBox
|
||||
from qgis.PyQt.QtCore import Qt, QModelIndex
|
||||
from qgis.testing import start_app, unittest
|
||||
from utilities import unitTestDataPath
|
||||
from qgis.PyQt.QtXml import QDomDocument
|
||||
from qgis.PyQt.QtTest import QSignalSpy
|
||||
|
||||
start_app()
|
||||
TEST_DATA_DIR = unitTestDataPath()
|
||||
|
||||
|
||||
class TestQgsLayoutComboBox(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
"""Run before each test."""
|
||||
self.manager = None
|
||||
self.aboutFired = False
|
||||
|
||||
def tearDown(self):
|
||||
"""Run after each test."""
|
||||
pass
|
||||
|
||||
def testCombo(self):
|
||||
project = QgsProject()
|
||||
manager = QgsLayoutManager(project)
|
||||
layout = QgsPrintLayout(project)
|
||||
layout.setName('ccc')
|
||||
self.assertTrue(manager.addLayout(layout))
|
||||
layout2 = QgsPrintLayout(project)
|
||||
layout2.setName('bbb')
|
||||
self.assertTrue(manager.addLayout(layout2))
|
||||
r = QgsReport(project)
|
||||
r.setName('ddd')
|
||||
manager.addLayout(r)
|
||||
|
||||
combo = QgsLayoutComboBox(None, manager)
|
||||
spy = QSignalSpy(combo.layoutChanged)
|
||||
self.assertEqual(combo.count(), 3)
|
||||
|
||||
self.assertEqual(combo.itemText(0), 'bbb')
|
||||
self.assertEqual(combo.itemText(1), 'ccc')
|
||||
self.assertEqual(combo.itemText(2), 'ddd')
|
||||
|
||||
self.assertEqual(combo.layout(0), layout2)
|
||||
self.assertEqual(combo.layout(1), layout)
|
||||
self.assertEqual(combo.layout(2), r)
|
||||
|
||||
combo.setCurrentLayout(None)
|
||||
self.assertEqual(combo.currentLayout(), None)
|
||||
self.assertEqual(len(spy), 1)
|
||||
combo.setCurrentLayout(layout)
|
||||
self.assertEqual(combo.currentLayout(), layout)
|
||||
self.assertEqual(len(spy), 2)
|
||||
combo.setCurrentLayout(r)
|
||||
self.assertEqual(combo.currentLayout(), r)
|
||||
self.assertEqual(len(spy), 3)
|
||||
combo.setCurrentLayout(layout2)
|
||||
self.assertEqual(combo.currentLayout(), layout2)
|
||||
self.assertEqual(len(spy), 4)
|
||||
|
||||
combo.setAllowEmptyLayout(True)
|
||||
self.assertEqual(combo.count(), 4)
|
||||
self.assertEqual(combo.itemText(0), '')
|
||||
self.assertEqual(combo.itemText(1), 'bbb')
|
||||
self.assertEqual(combo.itemText(2), 'ccc')
|
||||
self.assertEqual(combo.itemText(3), 'ddd')
|
||||
combo.setCurrentLayout(None)
|
||||
self.assertEqual(combo.currentIndex(), 0)
|
||||
|
||||
combo.setFilters(QgsLayoutManagerProxyModel.FilterPrintLayouts)
|
||||
self.assertEqual(combo.count(), 3)
|
||||
self.assertEqual(combo.itemText(0), '')
|
||||
self.assertEqual(combo.itemText(1), 'bbb')
|
||||
self.assertEqual(combo.itemText(2), 'ccc')
|
||||
|
||||
combo.setFilters(QgsLayoutManagerProxyModel.FilterReports)
|
||||
self.assertEqual(combo.filters(), QgsLayoutManagerProxyModel.FilterReports)
|
||||
self.assertEqual(combo.count(), 2)
|
||||
self.assertEqual(combo.itemText(0), '')
|
||||
self.assertEqual(combo.itemText(1), 'ddd')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
x
Reference in New Issue
Block a user