Skip to content
Permalink
Browse files

[needs-docs] Start on right click menu for layouts

Currently only contains option for removing the current
page and (non-functional) option for setting current page
properties
  • Loading branch information
nyalldawson committed Jul 22, 2017
1 parent e885966 commit a3e26785f8decca17e56081f088d6d11a3ccbb4d
@@ -96,6 +96,14 @@ class QgsLayoutPageCollection : QObject
Page numbers in collections begin at 0 - so a ``pageNumber`` of 0 will delete
the first page in the collection.

Calling deletePage() automatically triggers a reflow() of pages.
%End

void deletePage( QgsLayoutItemPage *page );
%Docstring
Deletes a page from the collection. The page will automatically be removed
from the collection's layout().

Calling deletePage() automatically triggers a reflow() of pages.
%End

@@ -157,6 +157,7 @@ SET(QGIS_APP_SRCS

layout/qgslayoutaddpagesdialog.cpp
layout/qgslayoutdesignerdialog.cpp
layout/qgslayoutappmenuprovider.cpp

locator/qgsinbuiltlocatorfilters.cpp
locator/qgslocatoroptionswidget.cpp
@@ -334,6 +335,7 @@ SET (QGIS_APP_MOC_HDRS
composer/qgsatlascompositionwidget.h

layout/qgslayoutaddpagesdialog.h
layout/qgslayoutappmenuprovider.h
layout/qgslayoutdesignerdialog.h

locator/qgsinbuiltlocatorfilters.h
@@ -0,0 +1,57 @@
/***************************************************************************
qgslayoutappmenuprovider.cpp
---------------------------
Date : July 2017
Copyright : (C) 2017 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 "qgslayoutappmenuprovider.h"
#include "qgslayoutitempage.h"
#include "qgslayout.h"
#include <QMenu>
#include <QMessageBox>

QgsLayoutAppMenuProvider::QgsLayoutAppMenuProvider( QObject *parent )
: QObject( parent )
{

}

QMenu *QgsLayoutAppMenuProvider::createContextMenu( QWidget *parent, QgsLayout *layout, QPointF layoutPoint ) const
{
QMenu *menu = new QMenu( parent );

// is a page under the mouse?
QgsLayoutItemPage *page = layout->pageCollection()->pageAtPoint( layoutPoint );
if ( page )
{
QAction *pagePropertiesAction = new QAction( tr( "Page Properties…" ), menu );
connect( pagePropertiesAction, &QAction::triggered, this, [page]()
{


} );
menu->addAction( pagePropertiesAction );
QAction *removePageAction = new QAction( tr( "Remove Page" ), menu );
connect( removePageAction, &QAction::triggered, this, [layout, page]()
{
if ( QMessageBox::question( nullptr, tr( "Remove Page" ),
tr( "Remove page from layout?" ),
QMessageBox::Yes | QMessageBox::No ) == QMessageBox::Yes )
{
layout->pageCollection()->deletePage( page );
}
} );
menu->addAction( removePageAction );
}

return menu;
}
@@ -0,0 +1,38 @@
/***************************************************************************
qgslayoutappmenuprovider.h
-------------------------
Date : July 2017
Copyright : (C) 2017 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 QGSLAYOUTAPPMENUPROVIDER_H
#define QGSLAYOUTAPPMENUPROVIDER_H

#include "qgis.h"
#include "qgslayoutview.h"
#include <QObject>

/**
* A menu provider for QgsLayoutView
*/
class QgsLayoutAppMenuProvider : public QObject, public QgsLayoutViewMenuProvider
{
Q_OBJECT

public:

QgsLayoutAppMenuProvider( QObject *parent = nullptr );

QMenu *createContextMenu( QWidget *parent, QgsLayout *layout, QPointF layoutPoint ) const override;

};

#endif // QGSLAYOUTAPPMENUPROVIDER_H
@@ -21,6 +21,7 @@
#include "qgisapp.h"
#include "qgslogger.h"
#include "qgslayout.h"
#include "qgslayoutappmenuprovider.h"
#include "qgslayoutview.h"
#include "qgslayoutviewtooladditem.h"
#include "qgslayoutviewtoolpan.h"
@@ -221,6 +222,9 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla

connect( mActionToggleFullScreen, &QAction::toggled, this, &QgsLayoutDesignerDialog::toggleFullScreen );

mMenuProvider = new QgsLayoutAppMenuProvider();
mView->setMenuProvider( mMenuProvider );

restoreWindowState();
}

@@ -31,6 +31,7 @@ class QgsLayoutRuler;
class QComboBox;
class QSlider;
class QLabel;
class QgsLayoutAppMenuProvider;

class QgsAppLayoutDesignerInterface : public QgsLayoutDesignerInterface
{
@@ -167,6 +168,8 @@ class QgsLayoutDesignerDialog: public QMainWindow, private Ui::QgsLayoutDesigner
QMap< QString, QToolButton * > mItemGroupToolButtons;
QMap< QString, QMenu * > mItemGroupSubmenus;

QgsLayoutAppMenuProvider *mMenuProvider;

//! Save window state
void saveWindowState();

@@ -190,6 +190,16 @@ void QgsLayoutPageCollection::deletePage( int pageNumber )
reflow();
}

void QgsLayoutPageCollection::deletePage( QgsLayoutItemPage *page )
{
if ( !mPages.contains( page ) )
return;

mPages.removeAll( page );
page->deleteLater();
reflow();
}

void QgsLayoutPageCollection::createDefaultPageStyleSymbol()
{
QgsStringMap properties;
@@ -113,6 +113,14 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject
*/
void deletePage( int pageNumber );

/**
* Deletes a page from the collection. The page will automatically be removed
* from the collection's layout().
*
* Calling deletePage() automatically triggers a reflow() of pages.
*/
void deletePage( QgsLayoutItemPage *page );

/**
* Sets the \a symbol to use for drawing pages in the collection.
*
@@ -230,7 +230,8 @@ void QgsLayoutRuler::paintEvent( QPaintEvent *event )
pageCoord += mmDisplay;
totalCoord += mmDisplay;
}
currentPageY += layout->pageCollection()->page( i )->rect().height() + layout->pageCollection()->spaceBetweenPages();
if ( i < endPage )
currentPageY += layout->pageCollection()->page( i )->rect().height() + layout->pageCollection()->spaceBetweenPages();
}
break;
}
@@ -129,6 +129,46 @@ def testPages(self):
self.assertTrue(sip.isdeleted(page))
self.assertTrue(sip.isdeleted(page2))

def testDeletePages(self):
"""
Test deleting pages from the collection
"""
p = QgsProject()
l = QgsLayout(p)
collection = l.pageCollection()

# add a page
page = QgsLayoutItemPage(l)
page.setPageSize('A4')
collection.addPage(page)
# add a second page
page2 = QgsLayoutItemPage(l)
page2.setPageSize('A5')
collection.addPage(page2)

# delete page
collection.deletePage(None)
self.assertEqual(collection.pageCount(), 2)

page3 = QgsLayoutItemPage(l)
# try deleting a page not in collection
collection.deletePage(page3)
QCoreApplication.sendPostedEvents(None, QEvent.DeferredDelete)
self.assertFalse(sip.isdeleted(page3))
self.assertEqual(collection.pageCount(), 2)

collection.deletePage(page)
self.assertEqual(collection.pageCount(), 1)
self.assertFalse(page in collection.pages())
QCoreApplication.sendPostedEvents(None, QEvent.DeferredDelete)
self.assertTrue(sip.isdeleted(page))

collection.deletePage(page2)
self.assertEqual(collection.pageCount(), 0)
self.assertFalse(collection.pages())
QCoreApplication.sendPostedEvents(None, QEvent.DeferredDelete)
self.assertTrue(sip.isdeleted(page2))

def testMaxPageWidth(self):
"""
Test calculating maximum page width

0 comments on commit a3e2678

Please sign in to comment.
You can’t perform that action at this time.