Move composer title to composition name

Allows the name to be retrieved from core code
This commit is contained in:
Nyall Dawson 2017-03-15 14:44:58 +10:00
parent 4f3cf68897
commit ae45d8d490
6 changed files with 76 additions and 145 deletions

View File

@ -1,9 +1,3 @@
/** \ingroup core
* Graphics scene for map printing. The class manages the paper item which always
* is the item in the back (z-value 0). It maintains the z-Values of the items and stores
* them in a list in ascending z-Order. This list can be changed to lower/raise items one position
* or to bring them to front/back.
* */
class QgsComposition : QGraphicsScene, QgsExpressionContextGenerator class QgsComposition : QGraphicsScene, QgsExpressionContextGenerator
{ {
%TypeHeaderCode %TypeHeaderCode
@ -12,15 +6,13 @@ class QgsComposition : QGraphicsScene, QgsExpressionContextGenerator
public: public:
/** \brief Plot type */
enum PlotStyle enum PlotStyle
{ {
Preview, // Use cache etc Preview,
Print, // Render well Print,
Postscript // Fonts need different scaling! Postscript
}; };
/** Style to draw the snapping grid*/
enum GridStyle enum GridStyle
{ {
Solid, Solid,
@ -40,153 +32,40 @@ class QgsComposition : QGraphicsScene, QgsExpressionContextGenerator
Landscape Landscape
}; };
explicit QgsComposition( QgsProject* project ); explicit QgsComposition( QgsProject *project );
/** Composition atlas modes*/
enum AtlasMode enum AtlasMode
{ {
AtlasOff, // Composition is not being controlled by an atlas AtlasOff,
PreviewAtlas, // An atlas composition is being previewed in the app PreviewAtlas,
ExportAtlas // The composition is being exported as an atlas ExportAtlas
}; };
~QgsComposition(); ~QgsComposition();
/** QgsProject *project() const;
* The project associated with the composition. Used to get access to layers, map themes, QString name() const;
* relations and various other bits. It is never null. void setName( const QString &name );
*
* \note Added in QGIS 3.0
*/
QgsProject* project() const;
/** Changes size of paper item.
* @param width page width in mm
* @param height page height in mm
* @param keepRelativeItemPosition if true, all items and guides will be moved so that they retain
* their same relative position to the top left corner of their current page.
* @see paperHeight
* @see paperWidth
*/
void setPaperSize( double width, double height, void setPaperSize( double width, double height,
bool keepRelativeItemPosition = true ); bool keepRelativeItemPosition = true );
/** Height of paper item
* @returns height in mm
* @see paperWidth
* @see setPaperSize
*/
double paperHeight() const; double paperHeight() const;
/** Width of paper item
* @returns width in mm
* @see paperHeight
* @see setPaperSize
*/
double paperWidth() const; double paperWidth() const;
/** Resizes the composition page to fit the current contents of the composition.
* Calling this method resets the number of pages to 1, with the size set to the
* minimum size required to fit all existing composer items. Items will also be
* repositioned so that the new top-left bounds of the composition is at the point
* (marginLeft, marginTop). An optional margin can be specified.
* @param marginTop top margin (millimeters)
* @param marginRight right margin (millimeters)
* @param marginBottom bottom margin (millimeters)
* @param marginLeft left margin (millimeters)
* @note added in QGIS 2.12
* @see setResizeToContentsMargins()
* @see resizeToContentsMargins()
*/
void resizePageToContents( double marginTop = 0.0, double marginRight = 0.0, void resizePageToContents( double marginTop = 0.0, double marginRight = 0.0,
double marginBottom = 0.0, double marginLeft = 0.0 ); double marginBottom = 0.0, double marginLeft = 0.0 );
/** Sets the resize to contents margins. These margins are saved in the composition
* so that they can be restored with the composer.
* @param marginTop top margin (millimeters)
* @param marginRight right margin (millimeters)
* @param marginBottom bottom margin (millimeters)
* @param marginLeft left margin (millimeters)
* @note added in QGIS 2.12
* @see resizePageToContents()
* @see resizeToContentsMargins()
*/
void setResizeToContentsMargins( double marginTop, double marginRight, void setResizeToContentsMargins( double marginTop, double marginRight,
double marginBottom, double marginLeft ); double marginBottom, double marginLeft );
void resizeToContentsMargins( double &marginTop /Out/, double &marginRight /Out/,
/** Returns the resize to contents margins. These margins are saved in the composition double &marginBottom /Out/, double &marginLeft /Out/ ) const;
* so that they can be restored with the composer.
* @param marginTop reference for top margin (millimeters)
* @param marginRight reference for right margin (millimeters)
* @param marginBottom reference for bottom margin (millimeters)
* @param marginLeft reference for left margin (millimeters)
* @note added in QGIS 2.12
* @see resizePageToContents()
* @see setResizeToContentsMargins()
*/
void resizeToContentsMargins( double& marginTop /Out/, double& marginRight /Out/,
double& marginBottom /Out/, double& marginLeft /Out/ ) const;
/** Returns the vertical space between pages in a composer view
* @returns space between pages in mm
*/
double spaceBetweenPages() const; double spaceBetweenPages() const;
/** Sets the number of pages for the composition.
* @param pages number of pages
* @see numPages
*/
void setNumPages( const int pages ); void setNumPages( const int pages );
/** Returns the number of pages in the composition.
* @returns number of pages
* @see setNumPages
*/
int numPages() const; int numPages() const;
/** Returns whether a page is empty, ie, it contains no items except for the background
* paper item.
* @param page page number, starting with 1
* @returns true if page is empty
* @note added in QGIS 2.5
* @see numPages
* @see setNumPages
* @see shouldExportPage
*/
bool pageIsEmpty( const int page ) const; bool pageIsEmpty( const int page ) const;
/** Returns whether a specified page number should be included in exports of the composition.
* @param page page number, starting with 1
* @returns true if page should be exported
* @note added in QGIS 2.5
* @see numPages
* @see pageIsEmpty
*/
bool shouldExportPage( const int page ) const; bool shouldExportPage( const int page ) const;
void setPageStyleSymbol( QgsFillSymbol *symbol );
/** Note: added in version 2.1*/ QgsFillSymbol *pageStyleSymbol();
void setPageStyleSymbol( QgsFillSymbol* symbol );
/** Note: added in version 2.1*/
QgsFillSymbol* pageStyleSymbol();
/** Returns the position within a page of a point in the composition
@note Added in QGIS 2.1
*/
QPointF positionOnPage( QPointF position ) const; QPointF positionOnPage( QPointF position ) const;
/** Returns the page number corresponding to a point in the composition
@note Added in QGIS 2.1
*/
int pageNumberForPoint( QPointF position ) const; int pageNumberForPoint( QPointF position ) const;
void setStatusMessage( const QString &message );
/** Sets the status bar message for the composer window
@note Added in QGIS 2.1
*/
void setStatusMessage( const QString & message );
/** Refreshes the composition when composer related options change
@note added in version 2.1
*/
void updateSettings(); void updateSettings();
void setSnapToGridEnabled( const bool b ); void setSnapToGridEnabled( const bool b );
@ -195,7 +74,6 @@ class QgsComposition : QGraphicsScene, QgsExpressionContextGenerator
void setGridVisible( const bool b ); void setGridVisible( const bool b );
bool gridVisible() const; bool gridVisible() const;
/** Hides / shows custom snap lines*/
void setSnapLinesVisible( const bool visible ); void setSnapLinesVisible( const bool visible );
bool snapLinesVisible() const; bool snapLinesVisible() const;
@ -777,10 +655,9 @@ class QgsComposition : QGraphicsScene, QgsExpressionContextGenerator
signals: signals:
void nameChanged( const QString& name );
void paperSizeChanged(); void paperSizeChanged();
void nPagesChanged(); void nPagesChanged();
/** Is emitted when the compositions print resolution changes*/
void printResolutionChanged(); void printResolutionChanged();
/** Is emitted when selected item changed. If 0, no item is selected*/ /** Is emitted when selected item changed. If 0, no item is selected*/

View File

@ -548,6 +548,7 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString &title )
//init undo/redo buttons //init undo/redo buttons
mComposition = new QgsComposition( QgsProject::instance() ); mComposition = new QgsComposition( QgsProject::instance() );
mComposition->setName( title );
mActionUndo->setEnabled( false ); mActionUndo->setEnabled( false );
mActionRedo->setEnabled( false ); mActionRedo->setEnabled( false );
@ -791,6 +792,7 @@ void QgsComposer::connectCompositionSlots()
return; return;
} }
connect( mComposition, &QgsComposition::nameChanged, this, &QgsComposer::setWindowTitle );
connect( mComposition, SIGNAL( selectedItemChanged( QgsComposerItem * ) ), this, SLOT( showItemOptions( QgsComposerItem * ) ) ); connect( mComposition, SIGNAL( selectedItemChanged( QgsComposerItem * ) ), this, SLOT( showItemOptions( QgsComposerItem * ) ) );
connect( mComposition, SIGNAL( composerArrowAdded( QgsComposerArrow * ) ), this, SLOT( addComposerArrow( QgsComposerArrow * ) ) ); connect( mComposition, SIGNAL( composerArrowAdded( QgsComposerArrow * ) ), this, SLOT( addComposerArrow( QgsComposerArrow * ) ) );
connect( mComposition, SIGNAL( composerPolygonAdded( QgsComposerPolygon * ) ), this, SLOT( addComposerPolygon( QgsComposerPolygon * ) ) ); connect( mComposition, SIGNAL( composerPolygonAdded( QgsComposerPolygon * ) ), this, SLOT( addComposerPolygon( QgsComposerPolygon * ) ) );

View File

@ -108,7 +108,7 @@ void QgsComposerManager::refreshComposers()
QSet<QgsComposer *>::const_iterator it = composers.constBegin(); QSet<QgsComposer *>::const_iterator it = composers.constBegin();
for ( ; it != composers.constEnd(); ++it ) for ( ; it != composers.constEnd(); ++it )
{ {
QListWidgetItem *item = new QListWidgetItem( ( *it )->title(), mComposerListWidget ); QListWidgetItem *item = new QListWidgetItem( ( *it )->composition()->name(), mComposerListWidget );
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable ); item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable );
mItemComposerMap.insert( item, *it ); mItemComposerMap.insert( item, *it );
} }
@ -461,7 +461,7 @@ void QgsComposerManager::duplicate_clicked()
if ( it != mItemComposerMap.constEnd() ) if ( it != mItemComposerMap.constEnd() )
{ {
currentComposer = it.value(); currentComposer = it.value();
currentTitle = it.value()->title(); currentTitle = it.value()->composition()->name();
} }
else else
{ {
@ -512,7 +512,7 @@ void QgsComposerManager::rename_clicked()
if ( it != mItemComposerMap.constEnd() ) if ( it != mItemComposerMap.constEnd() )
{ {
currentComposer = it.value(); currentComposer = it.value();
currentTitle = it.value()->title(); currentTitle = it.value()->composition()->name();
} }
else else
{ {
@ -524,7 +524,7 @@ void QgsComposerManager::rename_clicked()
{ {
return; return;
} }
currentComposer->setTitle( newTitle ); currentComposer->composition()->setName( newTitle );
item->setText( newTitle ); item->setText( newTitle );
mComposerListWidget->sortItems(); mComposerListWidget->sortItems();
@ -535,7 +535,7 @@ void QgsComposerManager::on_mComposerListWidget_itemChanged( QListWidgetItem *it
QMap<QListWidgetItem *, QgsComposer *>::const_iterator it = mItemComposerMap.constFind( item ); QMap<QListWidgetItem *, QgsComposer *>::const_iterator it = mItemComposerMap.constFind( item );
if ( it != mItemComposerMap.constEnd() ) if ( it != mItemComposerMap.constEnd() )
{ {
it.value()->setTitle( item->text() ); it.value()->composition()->setName( item->text() );
} }
mComposerListWidget->sortItems(); mComposerListWidget->sortItems();
} }
@ -580,7 +580,7 @@ void QgsComposerNameDelegate::setModelData( QWidget *editor, QAbstractItemModel
QStringList cNames; QStringList cNames;
Q_FOREACH ( QgsComposer *c, QgisApp::instance()->printComposers() ) Q_FOREACH ( QgsComposer *c, QgisApp::instance()->printComposers() )
{ {
cNames << c->title(); cNames << c->composition()->name();
} }
if ( changed && cNames.contains( value ) ) if ( changed && cNames.contains( value ) )
{ {

View File

@ -165,6 +165,12 @@ QgsProject *QgsComposition::project() const
return mProject; return mProject;
} }
void QgsComposition::setName( const QString &name )
{
mName = name;
emit nameChanged( name );
}
void QgsComposition::loadDefaults() void QgsComposition::loadDefaults()
{ {
QgsSettings settings; QgsSettings settings;
@ -820,6 +826,7 @@ bool QgsComposition::writeXml( QDomElement &composerElem, QDomDocument &doc )
} }
QDomElement compositionElem = doc.createElement( QStringLiteral( "Composition" ) ); QDomElement compositionElem = doc.createElement( QStringLiteral( "Composition" ) );
compositionElem.setAttribute( QStringLiteral( "name" ), mName );
compositionElem.setAttribute( QStringLiteral( "paperWidth" ), QString::number( mPageWidth ) ); compositionElem.setAttribute( QStringLiteral( "paperWidth" ), QString::number( mPageWidth ) );
compositionElem.setAttribute( QStringLiteral( "paperHeight" ), QString::number( mPageHeight ) ); compositionElem.setAttribute( QStringLiteral( "paperHeight" ), QString::number( mPageHeight ) );
compositionElem.setAttribute( QStringLiteral( "numPages" ), mPages.size() ); compositionElem.setAttribute( QStringLiteral( "numPages" ), mPages.size() );
@ -922,6 +929,8 @@ bool QgsComposition::readXml( const QDomElement &compositionElem, const QDomDocu
return false; return false;
} }
setName( compositionElem.attribute( QStringLiteral( "name" ) ) );
//create pages //create pages
bool widthConversionOk, heightConversionOk; bool widthConversionOk, heightConversionOk;
mPageWidth = compositionElem.attribute( QStringLiteral( "paperWidth" ) ).toDouble( &widthConversionOk ); mPageWidth = compositionElem.attribute( QStringLiteral( "paperWidth" ) ).toDouble( &widthConversionOk );

View File

@ -74,6 +74,8 @@ class QgsPaperItem;
class CORE_EXPORT QgsComposition : public QGraphicsScene, public QgsExpressionContextGenerator class CORE_EXPORT QgsComposition : public QGraphicsScene, public QgsExpressionContextGenerator
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY( QString name READ name WRITE setName NOTIFY nameChanged )
public: public:
//! \brief Plot type //! \brief Plot type
@ -127,6 +129,20 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene, public QgsExpressionCo
*/ */
QgsProject *project() const; QgsProject *project() const;
/**
* Returns the composition's name.
* @see setName()
* @note added in QGIS 3.0
*/
QString name() const { return mName; }
/**
* Sets the composition's name.
* @see name()
* @note added in QGIS 3.0
*/
void setName( const QString &name );
/** Changes size of paper item. /** Changes size of paper item.
* @param width page width in mm * @param width page width in mm
* @param height page height in mm * @param height page height in mm
@ -864,6 +880,8 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene, public QgsExpressionCo
//! Pointer to associated project (not null) //! Pointer to associated project (not null)
QgsProject *mProject = nullptr; QgsProject *mProject = nullptr;
QString mName;
QgsComposition::PlotStyle mPlotStyle; QgsComposition::PlotStyle mPlotStyle;
double mPageWidth; double mPageWidth;
double mPageHeight; double mPageHeight;
@ -994,6 +1012,14 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene, public QgsExpressionCo
void prepareAllDataDefinedExpressions(); void prepareAllDataDefinedExpressions();
signals: signals:
/**
* Emitted when the composition's name is changed.
* @note added in QGIS 3.0
* @see setName()
*/
void nameChanged( const QString &name );
void paperSizeChanged(); void paperSizeChanged();
void nPagesChanged(); void nPagesChanged();

View File

@ -28,6 +28,7 @@ from qgis.core import (QgsComposition,
from qgis.testing import start_app, unittest from qgis.testing import start_app, unittest
from qgis.testing.mocked import get_iface from qgis.testing.mocked import get_iface
from utilities import unitTestDataPath from utilities import unitTestDataPath
from qgis.PyQt.QtXml import QDomDocument
start_app() start_app()
TEST_DATA_DIR = unitTestDataPath() TEST_DATA_DIR = unitTestDataPath()
@ -133,6 +134,22 @@ class TestQgsComposition(unittest.TestCase):
(myExpectedFileSize, myFileSize, myImagePath)) (myExpectedFileSize, myFileSize, myImagePath))
assert myFileSize > myExpectedFileSize, myMessage assert myFileSize > myExpectedFileSize, myMessage
def testSaveRestore(self):
# test that properties are restored correctly from XML
composition = QgsComposition(QgsProject.instance())
composition.setName('test composition')
doc = QDomDocument("testdoc")
elem = doc.createElement("qgis")
doc.appendChild(elem)
elem = doc.createElement("composer")
self.assertTrue(composition.writeXml(elem, doc))
composition2 = QgsComposition(QgsProject.instance())
self.assertTrue(composition2.readXml(elem, doc))
self.assertEqual(composition.name(), 'test composition')
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()