Skip to content

Commit

Permalink
Add a shortcut menu to items panel, with option to show
Browse files Browse the repository at this point in the history
item properties

Refs #11581
  • Loading branch information
nyalldawson committed Nov 7, 2017
1 parent f69771c commit 91c3b5d
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 26 deletions.
8 changes: 8 additions & 0 deletions python/core/layout/qgslayoutmodel.sip
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,17 @@ class QgsLayoutModel: QAbstractItemModel



QgsLayoutItem *itemFromIndex( const QModelIndex &index ) const;
%Docstring
Returns the QgsLayoutItem corresponding to a QModelIndex ``index``, if possible.
.. seealso:: indexForItem()
:rtype: QgsLayoutItem
%End

QModelIndex indexForItem( QgsLayoutItem *item, const int column = 0 );
%Docstring
Returns the QModelIndex corresponding to a QgsLayoutItem ``item`` and ``column``, if possible.
.. seealso:: itemFromIndex()
:rtype: QModelIndex
%End

Expand Down
2 changes: 2 additions & 0 deletions src/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ SET(QGIS_APP_SRCS
layout/qgslayoutapputils.cpp
layout/qgslayoutdesignerdialog.cpp
layout/qgslayoutguidewidget.cpp
layout/qgslayoutitemslistview.cpp
layout/qgslayoutappmenuprovider.cpp
layout/qgslayoutmapwidget.cpp
layout/qgslayoutpagepropertieswidget.cpp
Expand Down Expand Up @@ -374,6 +375,7 @@ SET (QGIS_APP_MOC_HDRS
layout/qgslayoutappmenuprovider.h
layout/qgslayoutdesignerdialog.h
layout/qgslayoutguidewidget.h
layout/qgslayoutitemslistview.h
layout/qgslayoutmapwidget.h
layout/qgslayoutpagepropertieswidget.h
layout/qgslayoutpropertieswidget.h
Expand Down
23 changes: 3 additions & 20 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "qgslayoutguidewidget.h"
#include "qgslayoutmousehandles.h"
#include "qgslayoutmodel.h"
#include "qgslayoutitemslistview.h"
#include <QShortcut>
#include <QComboBox>
#include <QLineEdit>
Expand Down Expand Up @@ -495,16 +496,7 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla
mPanelsMenu->addAction( mItemsDock->toggleViewAction() );

//items tree widget
mItemsTreeView = new QTreeView( mItemsDock );

mItemsTreeView->setColumnWidth( 0, 30 );
mItemsTreeView->setColumnWidth( 1, 30 );
mItemsTreeView->setDragEnabled( true );
mItemsTreeView->setAcceptDrops( true );
mItemsTreeView->setDropIndicatorShown( true );
mItemsTreeView->setDragDropMode( QAbstractItemView::InternalMove );

mItemsTreeView->setIndentation( 0 );
mItemsTreeView = new QgsLayoutItemsListView( mItemsDock, this );
mItemsDock->setWidget( mItemsTreeView );

const QList<QDockWidget *> docks = findChildren<QDockWidget *>();
Expand Down Expand Up @@ -582,19 +574,10 @@ void QgsLayoutDesignerDialog::setCurrentLayout( QgsLayout *layout )
mUndoView->setStack( mLayout->undoStack()->stack() );

mSelectTool->setLayout( layout );

mItemsTreeView->setModel( mLayout->itemsModel() );
mItemsTreeView->setCurrentLayout( mLayout );
#ifdef ENABLE_MODELTEST
new ModelTest( mLayout->itemsModel(), this );
#endif
mItemsTreeView->header()->setSectionResizeMode( 0, QHeaderView::Fixed );
mItemsTreeView->header()->setSectionResizeMode( 1, QHeaderView::Fixed );
mItemsTreeView->setColumnWidth( 0, Qgis::UI_SCALE_FACTOR * fontMetrics().width( QStringLiteral( "xxxx" ) ) );
mItemsTreeView->setColumnWidth( 1, Qgis::UI_SCALE_FACTOR * fontMetrics().width( QStringLiteral( "xxxx" ) ) );
mItemsTreeView->header()->setSectionsMovable( false );

connect( mItemsTreeView->selectionModel(), &QItemSelectionModel::currentChanged, mLayout->itemsModel(), &QgsLayoutModel::setSelected );


createLayoutPropertiesWidget();
}
Expand Down
3 changes: 2 additions & 1 deletion src/app/layout/qgslayoutdesignerdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class QgsPanelWidgetStack;
class QgsDockWidget;
class QUndoView;
class QTreeView;
class QgsLayoutItemsListView;

class QgsAppLayoutDesignerInterface : public QgsLayoutDesignerInterface
{
Expand Down Expand Up @@ -281,7 +282,7 @@ class QgsLayoutDesignerDialog: public QMainWindow, private Ui::QgsLayoutDesigner
QgsDockWidget *mUndoDock = nullptr;

QgsDockWidget *mItemsDock = nullptr;
QTreeView *mItemsTreeView = nullptr;
QgsLayoutItemsListView *mItemsTreeView = nullptr;

QAction *mUndoAction = nullptr;
QAction *mRedoAction = nullptr;
Expand Down
69 changes: 69 additions & 0 deletions src/app/layout/qgslayoutitemslistview.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/***************************************************************************
qgslayoutitemslistview.cpp
--------------------------
Date : October 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 "qgslayoutitemslistview.h"
#include "qgslayout.h"
#include "qgslayoutmodel.h"
#include "qgslayoutdesignerdialog.h"
#include <QHeaderView>

QgsLayoutItemsListView::QgsLayoutItemsListView( QWidget *parent, QgsLayoutDesignerDialog *designer )
: QTreeView( parent )
, mDesigner( designer )
{
setColumnWidth( 0, 30 );
setColumnWidth( 1, 30 );
setDragEnabled( true );
setAcceptDrops( true );
setDropIndicatorShown( true );
setDragDropMode( QAbstractItemView::InternalMove );
setContextMenuPolicy( Qt::CustomContextMenu );
setIndentation( 0 );
connect( this, &QWidget::customContextMenuRequested, this, &QgsLayoutItemsListView::showContextMenu );
}

void QgsLayoutItemsListView::setCurrentLayout( QgsLayout *layout )
{
mLayout = layout;
mModel = layout->itemsModel();
setModel( mModel );

header()->setSectionResizeMode( 0, QHeaderView::Fixed );
header()->setSectionResizeMode( 1, QHeaderView::Fixed );
setColumnWidth( 0, Qgis::UI_SCALE_FACTOR * fontMetrics().width( QStringLiteral( "xxxx" ) ) );
setColumnWidth( 1, Qgis::UI_SCALE_FACTOR * fontMetrics().width( QStringLiteral( "xxxx" ) ) );
header()->setSectionsMovable( false );

connect( selectionModel(), &QItemSelectionModel::currentChanged, mLayout->itemsModel(), &QgsLayoutModel::setSelected );
}

void QgsLayoutItemsListView::showContextMenu( QPoint point )
{
QModelIndex index = indexAt( point );
QgsLayoutItem *item = mModel->itemFromIndex( index );
if ( !item )
return;

QMenu *menu = new QMenu( this );

QAction *itemPropertiesAction = new QAction( tr( "Item Properties…" ), menu );
connect( itemPropertiesAction, &QAction::triggered, this, [this, item]()
{
mDesigner->showItemOptions( item, true );
} );
menu->addAction( itemPropertiesAction );

menu->popup( mapToGlobal( point ) );
}
53 changes: 53 additions & 0 deletions src/app/layout/qgslayoutitemslistview.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/***************************************************************************
qgslayoutitemslistview.h
------------------------
Date : October 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 QGSLAYOUTITEMSLISTVIEW_H
#define QGSLAYOUTITEMSLISTVIEW_H

#include "qgis.h"
#include <QTreeView>

class QgsLayout;
class QgsLayoutDesignerDialog;
class QgsLayoutModel;

/**
* A list view for showing items in a layout
*/
class QgsLayoutItemsListView : public QTreeView
{
Q_OBJECT

public:

/**
* Constructor for QgsLayoutItemsListView.
*/
QgsLayoutItemsListView( QWidget *parent, QgsLayoutDesignerDialog *designer );

void setCurrentLayout( QgsLayout *layout );

private slots:

void showContextMenu( QPoint point );

private:

QgsLayout *mLayout = nullptr;
QgsLayoutModel *mModel = nullptr;
QgsLayoutDesignerDialog *mDesigner = nullptr;
};

#endif // QGSLAYOUTITEMSLISTVIEW_H
12 changes: 7 additions & 5 deletions src/core/layout/qgslayoutmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,15 @@ class CORE_EXPORT QgsLayoutModel: public QAbstractItemModel
#endif
///@endcond

/**
* Returns the QgsLayoutItem corresponding to a QModelIndex \a index, if possible.
* \see indexForItem()
*/
QgsLayoutItem *itemFromIndex( const QModelIndex &index ) const;

/**
* Returns the QModelIndex corresponding to a QgsLayoutItem \a item and \a column, if possible.
* \see itemFromIndex()
*/
QModelIndex indexForItem( QgsLayoutItem *item, const int column = 0 );

Expand All @@ -267,11 +274,6 @@ class CORE_EXPORT QgsLayoutModel: public QAbstractItemModel
//! Parent layout
QgsLayout *mLayout = nullptr;

/**
* Returns the QgsLayoutItem corresponding to a QModelIndex \a index, if possible
*/
QgsLayoutItem *itemFromIndex( const QModelIndex &index ) const;

/**
* Rebuilds the list of all layout items which are present in the layout. This is
* called when the stacking of order changes or when items are removed/restored to the
Expand Down

0 comments on commit 91c3b5d

Please sign in to comment.