Skip to content

Commit ae45d8d

Browse files
committed
Move composer title to composition name
Allows the name to be retrieved from core code
1 parent 4f3cf68 commit ae45d8d

File tree

6 files changed

+76
-145
lines changed

6 files changed

+76
-145
lines changed

python/core/composer/qgscomposition.sip

Lines changed: 16 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
/** \ingroup core
2-
* Graphics scene for map printing. The class manages the paper item which always
3-
* is the item in the back (z-value 0). It maintains the z-Values of the items and stores
4-
* them in a list in ascending z-Order. This list can be changed to lower/raise items one position
5-
* or to bring them to front/back.
6-
* */
71
class QgsComposition : QGraphicsScene, QgsExpressionContextGenerator
82
{
93
%TypeHeaderCode
@@ -12,15 +6,13 @@ class QgsComposition : QGraphicsScene, QgsExpressionContextGenerator
126

137
public:
148

15-
/** \brief Plot type */
169
enum PlotStyle
1710
{
18-
Preview, // Use cache etc
19-
Print, // Render well
20-
Postscript // Fonts need different scaling!
11+
Preview,
12+
Print,
13+
Postscript
2114
};
2215

23-
/** Style to draw the snapping grid*/
2416
enum GridStyle
2517
{
2618
Solid,
@@ -40,153 +32,40 @@ class QgsComposition : QGraphicsScene, QgsExpressionContextGenerator
4032
Landscape
4133
};
4234

43-
explicit QgsComposition( QgsProject* project );
35+
explicit QgsComposition( QgsProject *project );
4436

45-
/** Composition atlas modes*/
4637
enum AtlasMode
4738
{
48-
AtlasOff, // Composition is not being controlled by an atlas
49-
PreviewAtlas, // An atlas composition is being previewed in the app
50-
ExportAtlas // The composition is being exported as an atlas
39+
AtlasOff,
40+
PreviewAtlas,
41+
ExportAtlas
5142
};
5243

5344
~QgsComposition();
5445

55-
/**
56-
* The project associated with the composition. Used to get access to layers, map themes,
57-
* relations and various other bits. It is never null.
58-
*
59-
* \note Added in QGIS 3.0
60-
*/
61-
QgsProject* project() const;
62-
63-
/** Changes size of paper item.
64-
* @param width page width in mm
65-
* @param height page height in mm
66-
* @param keepRelativeItemPosition if true, all items and guides will be moved so that they retain
67-
* their same relative position to the top left corner of their current page.
68-
* @see paperHeight
69-
* @see paperWidth
70-
*/
46+
QgsProject *project() const;
47+
QString name() const;
48+
void setName( const QString &name );
7149
void setPaperSize( double width, double height,
7250
bool keepRelativeItemPosition = true );
73-
74-
/** Height of paper item
75-
* @returns height in mm
76-
* @see paperWidth
77-
* @see setPaperSize
78-
*/
7951
double paperHeight() const;
80-
81-
/** Width of paper item
82-
* @returns width in mm
83-
* @see paperHeight
84-
* @see setPaperSize
85-
*/
8652
double paperWidth() const;
87-
88-
/** Resizes the composition page to fit the current contents of the composition.
89-
* Calling this method resets the number of pages to 1, with the size set to the
90-
* minimum size required to fit all existing composer items. Items will also be
91-
* repositioned so that the new top-left bounds of the composition is at the point
92-
* (marginLeft, marginTop). An optional margin can be specified.
93-
* @param marginTop top margin (millimeters)
94-
* @param marginRight right margin (millimeters)
95-
* @param marginBottom bottom margin (millimeters)
96-
* @param marginLeft left margin (millimeters)
97-
* @note added in QGIS 2.12
98-
* @see setResizeToContentsMargins()
99-
* @see resizeToContentsMargins()
100-
*/
10153
void resizePageToContents( double marginTop = 0.0, double marginRight = 0.0,
10254
double marginBottom = 0.0, double marginLeft = 0.0 );
103-
104-
/** Sets the resize to contents margins. These margins are saved in the composition
105-
* so that they can be restored with the composer.
106-
* @param marginTop top margin (millimeters)
107-
* @param marginRight right margin (millimeters)
108-
* @param marginBottom bottom margin (millimeters)
109-
* @param marginLeft left margin (millimeters)
110-
* @note added in QGIS 2.12
111-
* @see resizePageToContents()
112-
* @see resizeToContentsMargins()
113-
*/
11455
void setResizeToContentsMargins( double marginTop, double marginRight,
11556
double marginBottom, double marginLeft );
116-
117-
/** Returns the resize to contents margins. These margins are saved in the composition
118-
* so that they can be restored with the composer.
119-
* @param marginTop reference for top margin (millimeters)
120-
* @param marginRight reference for right margin (millimeters)
121-
* @param marginBottom reference for bottom margin (millimeters)
122-
* @param marginLeft reference for left margin (millimeters)
123-
* @note added in QGIS 2.12
124-
* @see resizePageToContents()
125-
* @see setResizeToContentsMargins()
126-
*/
127-
void resizeToContentsMargins( double& marginTop /Out/, double& marginRight /Out/,
128-
double& marginBottom /Out/, double& marginLeft /Out/ ) const;
129-
130-
/** Returns the vertical space between pages in a composer view
131-
* @returns space between pages in mm
132-
*/
57+
void resizeToContentsMargins( double &marginTop /Out/, double &marginRight /Out/,
58+
double &marginBottom /Out/, double &marginLeft /Out/ ) const;
13359
double spaceBetweenPages() const;
134-
135-
/** Sets the number of pages for the composition.
136-
* @param pages number of pages
137-
* @see numPages
138-
*/
13960
void setNumPages( const int pages );
140-
141-
/** Returns the number of pages in the composition.
142-
* @returns number of pages
143-
* @see setNumPages
144-
*/
14561
int numPages() const;
146-
147-
/** Returns whether a page is empty, ie, it contains no items except for the background
148-
* paper item.
149-
* @param page page number, starting with 1
150-
* @returns true if page is empty
151-
* @note added in QGIS 2.5
152-
* @see numPages
153-
* @see setNumPages
154-
* @see shouldExportPage
155-
*/
15662
bool pageIsEmpty( const int page ) const;
157-
158-
/** Returns whether a specified page number should be included in exports of the composition.
159-
* @param page page number, starting with 1
160-
* @returns true if page should be exported
161-
* @note added in QGIS 2.5
162-
* @see numPages
163-
* @see pageIsEmpty
164-
*/
16563
bool shouldExportPage( const int page ) const;
166-
167-
/** Note: added in version 2.1*/
168-
void setPageStyleSymbol( QgsFillSymbol* symbol );
169-
/** Note: added in version 2.1*/
170-
QgsFillSymbol* pageStyleSymbol();
171-
172-
/** Returns the position within a page of a point in the composition
173-
@note Added in QGIS 2.1
174-
*/
64+
void setPageStyleSymbol( QgsFillSymbol *symbol );
65+
QgsFillSymbol *pageStyleSymbol();
17566
QPointF positionOnPage( QPointF position ) const;
176-
177-
/** Returns the page number corresponding to a point in the composition
178-
@note Added in QGIS 2.1
179-
*/
18067
int pageNumberForPoint( QPointF position ) const;
181-
182-
/** Sets the status bar message for the composer window
183-
@note Added in QGIS 2.1
184-
*/
185-
void setStatusMessage( const QString & message );
186-
187-
/** Refreshes the composition when composer related options change
188-
@note added in version 2.1
189-
*/
68+
void setStatusMessage( const QString &message );
19069
void updateSettings();
19170

19271
void setSnapToGridEnabled( const bool b );
@@ -195,7 +74,6 @@ class QgsComposition : QGraphicsScene, QgsExpressionContextGenerator
19574
void setGridVisible( const bool b );
19675
bool gridVisible() const;
19776

198-
/** Hides / shows custom snap lines*/
19977
void setSnapLinesVisible( const bool visible );
20078
bool snapLinesVisible() const;
20179

@@ -777,10 +655,9 @@ class QgsComposition : QGraphicsScene, QgsExpressionContextGenerator
777655

778656

779657
signals:
658+
void nameChanged( const QString& name );
780659
void paperSizeChanged();
781660
void nPagesChanged();
782-
783-
/** Is emitted when the compositions print resolution changes*/
784661
void printResolutionChanged();
785662

786663
/** Is emitted when selected item changed. If 0, no item is selected*/

src/app/composer/qgscomposer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString &title )
548548

549549
//init undo/redo buttons
550550
mComposition = new QgsComposition( QgsProject::instance() );
551+
mComposition->setName( title );
551552

552553
mActionUndo->setEnabled( false );
553554
mActionRedo->setEnabled( false );
@@ -791,6 +792,7 @@ void QgsComposer::connectCompositionSlots()
791792
return;
792793
}
793794

795+
connect( mComposition, &QgsComposition::nameChanged, this, &QgsComposer::setWindowTitle );
794796
connect( mComposition, SIGNAL( selectedItemChanged( QgsComposerItem * ) ), this, SLOT( showItemOptions( QgsComposerItem * ) ) );
795797
connect( mComposition, SIGNAL( composerArrowAdded( QgsComposerArrow * ) ), this, SLOT( addComposerArrow( QgsComposerArrow * ) ) );
796798
connect( mComposition, SIGNAL( composerPolygonAdded( QgsComposerPolygon * ) ), this, SLOT( addComposerPolygon( QgsComposerPolygon * ) ) );

src/app/composer/qgscomposermanager.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ void QgsComposerManager::refreshComposers()
108108
QSet<QgsComposer *>::const_iterator it = composers.constBegin();
109109
for ( ; it != composers.constEnd(); ++it )
110110
{
111-
QListWidgetItem *item = new QListWidgetItem( ( *it )->title(), mComposerListWidget );
111+
QListWidgetItem *item = new QListWidgetItem( ( *it )->composition()->name(), mComposerListWidget );
112112
item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable );
113113
mItemComposerMap.insert( item, *it );
114114
}
@@ -461,7 +461,7 @@ void QgsComposerManager::duplicate_clicked()
461461
if ( it != mItemComposerMap.constEnd() )
462462
{
463463
currentComposer = it.value();
464-
currentTitle = it.value()->title();
464+
currentTitle = it.value()->composition()->name();
465465
}
466466
else
467467
{
@@ -512,7 +512,7 @@ void QgsComposerManager::rename_clicked()
512512
if ( it != mItemComposerMap.constEnd() )
513513
{
514514
currentComposer = it.value();
515-
currentTitle = it.value()->title();
515+
currentTitle = it.value()->composition()->name();
516516
}
517517
else
518518
{
@@ -524,7 +524,7 @@ void QgsComposerManager::rename_clicked()
524524
{
525525
return;
526526
}
527-
currentComposer->setTitle( newTitle );
527+
currentComposer->composition()->setName( newTitle );
528528
item->setText( newTitle );
529529

530530
mComposerListWidget->sortItems();
@@ -535,7 +535,7 @@ void QgsComposerManager::on_mComposerListWidget_itemChanged( QListWidgetItem *it
535535
QMap<QListWidgetItem *, QgsComposer *>::const_iterator it = mItemComposerMap.constFind( item );
536536
if ( it != mItemComposerMap.constEnd() )
537537
{
538-
it.value()->setTitle( item->text() );
538+
it.value()->composition()->setName( item->text() );
539539
}
540540
mComposerListWidget->sortItems();
541541
}
@@ -580,7 +580,7 @@ void QgsComposerNameDelegate::setModelData( QWidget *editor, QAbstractItemModel
580580
QStringList cNames;
581581
Q_FOREACH ( QgsComposer *c, QgisApp::instance()->printComposers() )
582582
{
583-
cNames << c->title();
583+
cNames << c->composition()->name();
584584
}
585585
if ( changed && cNames.contains( value ) )
586586
{

src/core/composer/qgscomposition.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ QgsProject *QgsComposition::project() const
165165
return mProject;
166166
}
167167

168+
void QgsComposition::setName( const QString &name )
169+
{
170+
mName = name;
171+
emit nameChanged( name );
172+
}
173+
168174
void QgsComposition::loadDefaults()
169175
{
170176
QgsSettings settings;
@@ -820,6 +826,7 @@ bool QgsComposition::writeXml( QDomElement &composerElem, QDomDocument &doc )
820826
}
821827

822828
QDomElement compositionElem = doc.createElement( QStringLiteral( "Composition" ) );
829+
compositionElem.setAttribute( QStringLiteral( "name" ), mName );
823830
compositionElem.setAttribute( QStringLiteral( "paperWidth" ), QString::number( mPageWidth ) );
824831
compositionElem.setAttribute( QStringLiteral( "paperHeight" ), QString::number( mPageHeight ) );
825832
compositionElem.setAttribute( QStringLiteral( "numPages" ), mPages.size() );
@@ -922,6 +929,8 @@ bool QgsComposition::readXml( const QDomElement &compositionElem, const QDomDocu
922929
return false;
923930
}
924931

932+
setName( compositionElem.attribute( QStringLiteral( "name" ) ) );
933+
925934
//create pages
926935
bool widthConversionOk, heightConversionOk;
927936
mPageWidth = compositionElem.attribute( QStringLiteral( "paperWidth" ) ).toDouble( &widthConversionOk );

src/core/composer/qgscomposition.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class QgsPaperItem;
7474
class CORE_EXPORT QgsComposition : public QGraphicsScene, public QgsExpressionContextGenerator
7575
{
7676
Q_OBJECT
77+
Q_PROPERTY( QString name READ name WRITE setName NOTIFY nameChanged )
78+
7779
public:
7880

7981
//! \brief Plot type
@@ -127,6 +129,20 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene, public QgsExpressionCo
127129
*/
128130
QgsProject *project() const;
129131

132+
/**
133+
* Returns the composition's name.
134+
* @see setName()
135+
* @note added in QGIS 3.0
136+
*/
137+
QString name() const { return mName; }
138+
139+
/**
140+
* Sets the composition's name.
141+
* @see name()
142+
* @note added in QGIS 3.0
143+
*/
144+
void setName( const QString &name );
145+
130146
/** Changes size of paper item.
131147
* @param width page width in mm
132148
* @param height page height in mm
@@ -864,6 +880,8 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene, public QgsExpressionCo
864880
//! Pointer to associated project (not null)
865881
QgsProject *mProject = nullptr;
866882

883+
QString mName;
884+
867885
QgsComposition::PlotStyle mPlotStyle;
868886
double mPageWidth;
869887
double mPageHeight;
@@ -994,6 +1012,14 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene, public QgsExpressionCo
9941012
void prepareAllDataDefinedExpressions();
9951013

9961014
signals:
1015+
1016+
/**
1017+
* Emitted when the composition's name is changed.
1018+
* @note added in QGIS 3.0
1019+
* @see setName()
1020+
*/
1021+
void nameChanged( const QString &name );
1022+
9971023
void paperSizeChanged();
9981024
void nPagesChanged();
9991025

tests/src/python/test_qgscomposition.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from qgis.testing import start_app, unittest
2929
from qgis.testing.mocked import get_iface
3030
from utilities import unitTestDataPath
31+
from qgis.PyQt.QtXml import QDomDocument
3132

3233
start_app()
3334
TEST_DATA_DIR = unitTestDataPath()
@@ -133,6 +134,22 @@ def testPrintMapFromTemplate(self):
133134
(myExpectedFileSize, myFileSize, myImagePath))
134135
assert myFileSize > myExpectedFileSize, myMessage
135136

137+
def testSaveRestore(self):
138+
# test that properties are restored correctly from XML
139+
composition = QgsComposition(QgsProject.instance())
140+
composition.setName('test composition')
141+
142+
doc = QDomDocument("testdoc")
143+
elem = doc.createElement("qgis")
144+
doc.appendChild(elem)
145+
elem = doc.createElement("composer")
146+
self.assertTrue(composition.writeXml(elem, doc))
147+
148+
composition2 = QgsComposition(QgsProject.instance())
149+
self.assertTrue(composition2.readXml(elem, doc))
150+
151+
self.assertEqual(composition.name(), 'test composition')
152+
136153

137154
if __name__ == '__main__':
138155
unittest.main()

0 commit comments

Comments
 (0)