Skip to content

Commit

Permalink
Move composer title to composition name
Browse files Browse the repository at this point in the history
Allows the name to be retrieved from core code
  • Loading branch information
nyalldawson committed Mar 15, 2017
1 parent 4f3cf68 commit ae45d8d
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 145 deletions.
155 changes: 16 additions & 139 deletions python/core/composer/qgscomposition.sip
@@ -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
{
%TypeHeaderCode
Expand All @@ -12,15 +6,13 @@ class QgsComposition : QGraphicsScene, QgsExpressionContextGenerator

public:

/** \brief Plot type */
enum PlotStyle
{
Preview, // Use cache etc
Print, // Render well
Postscript // Fonts need different scaling!
Preview,
Print,
Postscript
};

/** Style to draw the snapping grid*/
enum GridStyle
{
Solid,
Expand All @@ -40,153 +32,40 @@ class QgsComposition : QGraphicsScene, QgsExpressionContextGenerator
Landscape
};

explicit QgsComposition( QgsProject* project );
explicit QgsComposition( QgsProject *project );

/** Composition atlas modes*/
enum AtlasMode
{
AtlasOff, // Composition is not being controlled by an atlas
PreviewAtlas, // An atlas composition is being previewed in the app
ExportAtlas // The composition is being exported as an atlas
AtlasOff,
PreviewAtlas,
ExportAtlas
};

~QgsComposition();

/**
* The project associated with the composition. Used to get access to layers, map themes,
* relations and various other bits. It is never null.
*
* \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
*/
QgsProject *project() const;
QString name() const;
void setName( const QString &name );
void setPaperSize( double width, double height,
bool keepRelativeItemPosition = true );

/** Height of paper item
* @returns height in mm
* @see paperWidth
* @see setPaperSize
*/
double paperHeight() const;

/** Width of paper item
* @returns width in mm
* @see paperHeight
* @see setPaperSize
*/
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,
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,
double marginBottom, double marginLeft );

/** Returns the resize to contents margins. These margins are saved in the composition
* 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
*/
void resizeToContentsMargins( double &marginTop /Out/, double &marginRight /Out/,
double &marginBottom /Out/, double &marginLeft /Out/ ) const;
double spaceBetweenPages() const;

/** Sets the number of pages for the composition.
* @param pages number of pages
* @see numPages
*/
void setNumPages( const int pages );

/** Returns the number of pages in the composition.
* @returns number of pages
* @see setNumPages
*/
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;

/** 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;

/** Note: added in version 2.1*/
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
*/
void setPageStyleSymbol( QgsFillSymbol *symbol );
QgsFillSymbol *pageStyleSymbol();
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;

/** 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 setStatusMessage( const QString &message );
void updateSettings();

void setSnapToGridEnabled( const bool b );
Expand All @@ -195,7 +74,6 @@ class QgsComposition : QGraphicsScene, QgsExpressionContextGenerator
void setGridVisible( const bool b );
bool gridVisible() const;

/** Hides / shows custom snap lines*/
void setSnapLinesVisible( const bool visible );
bool snapLinesVisible() const;

Expand Down Expand Up @@ -777,10 +655,9 @@ class QgsComposition : QGraphicsScene, QgsExpressionContextGenerator


signals:
void nameChanged( const QString& name );
void paperSizeChanged();
void nPagesChanged();

/** Is emitted when the compositions print resolution changes*/
void printResolutionChanged();

/** Is emitted when selected item changed. If 0, no item is selected*/
Expand Down
2 changes: 2 additions & 0 deletions src/app/composer/qgscomposer.cpp
Expand Up @@ -548,6 +548,7 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString &title )

//init undo/redo buttons
mComposition = new QgsComposition( QgsProject::instance() );
mComposition->setName( title );

mActionUndo->setEnabled( false );
mActionRedo->setEnabled( false );
Expand Down Expand Up @@ -791,6 +792,7 @@ void QgsComposer::connectCompositionSlots()
return;
}

connect( mComposition, &QgsComposition::nameChanged, this, &QgsComposer::setWindowTitle );
connect( mComposition, SIGNAL( selectedItemChanged( QgsComposerItem * ) ), this, SLOT( showItemOptions( QgsComposerItem * ) ) );
connect( mComposition, SIGNAL( composerArrowAdded( QgsComposerArrow * ) ), this, SLOT( addComposerArrow( QgsComposerArrow * ) ) );
connect( mComposition, SIGNAL( composerPolygonAdded( QgsComposerPolygon * ) ), this, SLOT( addComposerPolygon( QgsComposerPolygon * ) ) );
Expand Down
12 changes: 6 additions & 6 deletions src/app/composer/qgscomposermanager.cpp
Expand Up @@ -108,7 +108,7 @@ void QgsComposerManager::refreshComposers()
QSet<QgsComposer *>::const_iterator it = composers.constBegin();
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 );
mItemComposerMap.insert( item, *it );
}
Expand Down Expand Up @@ -461,7 +461,7 @@ void QgsComposerManager::duplicate_clicked()
if ( it != mItemComposerMap.constEnd() )
{
currentComposer = it.value();
currentTitle = it.value()->title();
currentTitle = it.value()->composition()->name();
}
else
{
Expand Down Expand Up @@ -512,7 +512,7 @@ void QgsComposerManager::rename_clicked()
if ( it != mItemComposerMap.constEnd() )
{
currentComposer = it.value();
currentTitle = it.value()->title();
currentTitle = it.value()->composition()->name();
}
else
{
Expand All @@ -524,7 +524,7 @@ void QgsComposerManager::rename_clicked()
{
return;
}
currentComposer->setTitle( newTitle );
currentComposer->composition()->setName( newTitle );
item->setText( newTitle );

mComposerListWidget->sortItems();
Expand All @@ -535,7 +535,7 @@ void QgsComposerManager::on_mComposerListWidget_itemChanged( QListWidgetItem *it
QMap<QListWidgetItem *, QgsComposer *>::const_iterator it = mItemComposerMap.constFind( item );
if ( it != mItemComposerMap.constEnd() )
{
it.value()->setTitle( item->text() );
it.value()->composition()->setName( item->text() );
}
mComposerListWidget->sortItems();
}
Expand Down Expand Up @@ -580,7 +580,7 @@ void QgsComposerNameDelegate::setModelData( QWidget *editor, QAbstractItemModel
QStringList cNames;
Q_FOREACH ( QgsComposer *c, QgisApp::instance()->printComposers() )
{
cNames << c->title();
cNames << c->composition()->name();
}
if ( changed && cNames.contains( value ) )
{
Expand Down
9 changes: 9 additions & 0 deletions src/core/composer/qgscomposition.cpp
Expand Up @@ -165,6 +165,12 @@ QgsProject *QgsComposition::project() const
return mProject;
}

void QgsComposition::setName( const QString &name )
{
mName = name;
emit nameChanged( name );
}

void QgsComposition::loadDefaults()
{
QgsSettings settings;
Expand Down Expand Up @@ -820,6 +826,7 @@ bool QgsComposition::writeXml( QDomElement &composerElem, QDomDocument &doc )
}

QDomElement compositionElem = doc.createElement( QStringLiteral( "Composition" ) );
compositionElem.setAttribute( QStringLiteral( "name" ), mName );
compositionElem.setAttribute( QStringLiteral( "paperWidth" ), QString::number( mPageWidth ) );
compositionElem.setAttribute( QStringLiteral( "paperHeight" ), QString::number( mPageHeight ) );
compositionElem.setAttribute( QStringLiteral( "numPages" ), mPages.size() );
Expand Down Expand Up @@ -922,6 +929,8 @@ bool QgsComposition::readXml( const QDomElement &compositionElem, const QDomDocu
return false;
}

setName( compositionElem.attribute( QStringLiteral( "name" ) ) );

//create pages
bool widthConversionOk, heightConversionOk;
mPageWidth = compositionElem.attribute( QStringLiteral( "paperWidth" ) ).toDouble( &widthConversionOk );
Expand Down
26 changes: 26 additions & 0 deletions src/core/composer/qgscomposition.h
Expand Up @@ -74,6 +74,8 @@ class QgsPaperItem;
class CORE_EXPORT QgsComposition : public QGraphicsScene, public QgsExpressionContextGenerator
{
Q_OBJECT
Q_PROPERTY( QString name READ name WRITE setName NOTIFY nameChanged )

public:

//! \brief Plot type
Expand Down Expand Up @@ -127,6 +129,20 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene, public QgsExpressionCo
*/
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.
* @param width page width in mm
* @param height page height in mm
Expand Down Expand Up @@ -864,6 +880,8 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene, public QgsExpressionCo
//! Pointer to associated project (not null)
QgsProject *mProject = nullptr;

QString mName;

QgsComposition::PlotStyle mPlotStyle;
double mPageWidth;
double mPageHeight;
Expand Down Expand Up @@ -994,6 +1012,14 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene, public QgsExpressionCo
void prepareAllDataDefinedExpressions();

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 nPagesChanged();

Expand Down
17 changes: 17 additions & 0 deletions tests/src/python/test_qgscomposition.py
Expand Up @@ -28,6 +28,7 @@
from qgis.testing import start_app, unittest
from qgis.testing.mocked import get_iface
from utilities import unitTestDataPath
from qgis.PyQt.QtXml import QDomDocument

start_app()
TEST_DATA_DIR = unitTestDataPath()
Expand Down Expand Up @@ -133,6 +134,22 @@ def testPrintMapFromTemplate(self):
(myExpectedFileSize, myFileSize, myImagePath))
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__':
unittest.main()

0 comments on commit ae45d8d

Please sign in to comment.