[needs-docs] Add a new item properties dialog

When adding a new item to a layout, if the user just does a single
click of the mouse (vs a click and drag to set the new item
position/size), then a dialog is shown asking to user for
the new item size/position/reference point.

This allows easy creation of items with an exact position/size
and is common behavior in most proper DTP/illustration apps.
This commit is contained in:
Nyall Dawson 2017-07-17 17:59:14 +10:00
parent b4f5025d4f
commit cdec70babe
7 changed files with 571 additions and 4 deletions

View File

@ -278,6 +278,7 @@
%Include layertree/qgslayertreeviewdefaultactions.sip
%Include layout/qgslayoutdesignerinterface.sip
%Include layout/qgslayoutitemguiregistry.sip
%Include layout/qgslayoutnewitempropertiesdialog.sip
%Include layout/qgslayoutruler.sip
%Include layout/qgslayoutview.sip
%Include layout/qgslayoutviewtool.sip

View File

@ -0,0 +1,46 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/layout/qgslayoutnewitempropertiesdialog.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
class QgsLayoutNewItemPropertiesDialog : QDialog
{
%Docstring
A dialog for configuring properties like the size and position of new layout items.
%End
%TypeHeaderCode
#include "qgslayoutnewitempropertiesdialog.h"
%End
public:
QgsLayoutNewItemPropertiesDialog( QWidget *parent = 0, Qt::WindowFlags flags = 0 );
void setInitialItemPosition( QPointF position );
QgsLayoutPoint itemPosition() const;
%Docstring
:rtype: QgsLayoutPoint
%End
QgsLayoutSize itemSize() const;
%Docstring
:rtype: QgsLayoutSize
%End
};
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/layout/qgslayoutnewitempropertiesdialog.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/

View File

@ -159,6 +159,7 @@ SET(QGIS_GUI_SRCS
layertree/qgslayertreeviewdefaultactions.cpp
layout/qgslayoutitemguiregistry.cpp
layout/qgslayoutnewitempropertiesdialog.cpp
layout/qgslayoutruler.cpp
layout/qgslayoutview.cpp
layout/qgslayoutviewmouseevent.cpp
@ -647,6 +648,7 @@ SET(QGIS_GUI_MOC_HDRS
layout/qgslayoutdesignerinterface.h
layout/qgslayoutitemguiregistry.h
layout/qgslayoutnewitempropertiesdialog.h
layout/qgslayoutruler.h
layout/qgslayoutview.h
layout/qgslayoutviewtool.h

View File

@ -0,0 +1,44 @@
/***************************************************************************
qgslayoutnewitempropertiesdialog.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 "qgslayoutnewitempropertiesdialog.h"
#include "qgssettings.h"
QgsLayoutNewItemPropertiesDialog::QgsLayoutNewItemPropertiesDialog( QWidget *parent, Qt::WindowFlags flags )
: QDialog( parent, flags )
{
setupUi( this );
QgsSettings settings;
double lastWidth = settings.value( QStringLiteral( "LayoutDesigner/lastItemWidth" ), QStringLiteral( "50" ) ).toDouble();
double lastHeight = settings.value( QStringLiteral( "LayoutDesigner/lastItemHeight" ), QStringLiteral( "50" ) ).toDouble();
mWidthSpin->setValue( lastWidth );
mHeightSpin->setValue( lastHeight );
}
void QgsLayoutNewItemPropertiesDialog::setInitialItemPosition( QPointF position )
{
mXPosSpin->setValue( position.x() );
mYPosSpin->setValue( position.y() );
}
QgsLayoutPoint QgsLayoutNewItemPropertiesDialog::itemPosition() const
{
return QgsLayoutPoint( mXPosSpin->value(), mYPosSpin->value() );
}
QgsLayoutSize QgsLayoutNewItemPropertiesDialog::itemSize() const
{
return QgsLayoutSize( mWidthSpin->value(), mHeightSpin->value() );
}

View File

@ -0,0 +1,47 @@
/***************************************************************************
qgslayoutnewitempropertiesdialog.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 QGSLAYOUTNEWITEMPROPERTIESDIALOG_H
#define QGSLAYOUTNEWITEMPROPERTIESDIALOG_H
#include "qgis.h"
#include "qgis_gui.h"
#include "ui_qgslayoutnewitemproperties.h"
#include "qgslayoutsize.h"
#include "qgslayoutpoint.h"
/**
* \ingroup gui
* \brief A dialog for configuring properties like the size and position of new layout items.
*/
class GUI_EXPORT QgsLayoutNewItemPropertiesDialog : public QDialog, private Ui::QgsLayoutNewItemPropertiesDialog
{
Q_OBJECT
public:
QgsLayoutNewItemPropertiesDialog( QWidget *parent = nullptr, Qt::WindowFlags flags = 0 );
void setInitialItemPosition( QPointF position );
QgsLayoutPoint itemPosition() const;
QgsLayoutSize itemSize() const;
};
#endif // QGSLAYOUTNEWITEMPROPERTIESDIALOG_H

View File

@ -24,6 +24,8 @@
#include "qgslayoutviewrubberband.h"
#include "qgsgui.h"
#include "qgslayoutitemguiregistry.h"
#include "qgslayoutnewitempropertiesdialog.h"
#include "qgssettings.h"
#include <QGraphicsRectItem>
#include <QPen>
#include <QBrush>
@ -81,13 +83,37 @@ void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *even
QRectF rect = mRubberBand->finish( event->layoutPoint(), event->modifiers() );
QgsLayoutItem *item = QgsApplication::layoutItemRegistry()->createItem( mItemType, layout() );
// click? or click-and-drag?
bool clickOnly = !isClickAndDrag( mMousePressStartPos, event->pos() );
Q_UNUSED( clickOnly );
if ( clickOnly )
{
QgsLayoutNewItemPropertiesDialog dlg( view() );
dlg.setInitialItemPosition( event->layoutPoint() );
if ( dlg.exec() )
{
item->attemptResize( dlg.itemSize() );
item->attemptMove( dlg.itemPosition() );
}
else
{
delete item;
return;
}
}
else
{
item->attemptResize( QgsLayoutSize( rect.width(), rect.height(), QgsUnitTypes::LayoutMillimeters ) );
item->attemptMove( QgsLayoutPoint( rect.left(), rect.top(), QgsUnitTypes::LayoutMillimeters ) );
}
// record last created item size
QgsSettings settings;
settings.setValue( QStringLiteral( "LayoutDesigner/lastItemWidth" ), item->sizeWithUnits().width() );
settings.setValue( QStringLiteral( "LayoutDesigner/lastItemHeight" ), item->sizeWithUnits().height() );
QgsLayoutItem *item = QgsApplication::layoutItemRegistry()->createItem( mItemType, layout() );
item->attemptResize( QgsLayoutSize( rect.width(), rect.height(), QgsUnitTypes::LayoutMillimeters ) );
item->attemptMove( QgsLayoutPoint( rect.left(), rect.top(), QgsUnitTypes::LayoutMillimeters ) );
layout()->addItem( item );
}

View File

@ -0,0 +1,401 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>QgsLayoutNewItemPropertiesDialog</class>
<widget class="QDialog" name="QgsLayoutNewItemPropertiesDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>391</width>
<height>263</height>
</rect>
</property>
<property name="windowTitle">
<string>New Item Properties</string>
</property>
<layout class="QGridLayout" name="gridLayout_4" rowstretch="0,1,0" columnstretch="1,0">
<item row="0" column="0">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Position and size</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout_3" columnstretch="0,0,0">
<item row="3" column="1" colspan="2">
<widget class="QgsDoubleSpinBox" name="mHeightSpin">
<property name="suffix">
<string> mm</string>
</property>
<property name="decimals">
<number>3</number>
</property>
<property name="maximum">
<double>9999999.000000000000000</double>
</property>
<property name="value">
<double>100.000000000000000</double>
</property>
<property name="showClearButton" stdset="0">
<bool>false</bool>
</property>
</widget>
</item>
<item row="1" column="1" colspan="2">
<widget class="QgsDoubleSpinBox" name="mYPosSpin">
<property name="suffix">
<string> mm</string>
</property>
<property name="decimals">
<number>3</number>
</property>
<property name="minimum">
<double>-9999999.000000000000000</double>
</property>
<property name="maximum">
<double>9999999.000000000000000</double>
</property>
<property name="showClearButton" stdset="0">
<bool>false</bool>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="mHeightLabel">
<property name="text">
<string>Height</string>
</property>
</widget>
</item>
<item row="2" column="1" colspan="2">
<widget class="QgsDoubleSpinBox" name="mWidthSpin">
<property name="suffix">
<string> mm</string>
</property>
<property name="decimals">
<number>3</number>
</property>
<property name="maximum">
<double>9999999.000000000000000</double>
</property>
<property name="value">
<double>100.000000000000000</double>
</property>
<property name="showClearButton" stdset="0">
<bool>false</bool>
</property>
</widget>
</item>
<item row="0" column="1" colspan="2">
<widget class="QgsDoubleSpinBox" name="mXPosSpin">
<property name="suffix">
<string> mm</string>
</property>
<property name="decimals">
<number>3</number>
</property>
<property name="minimum">
<double>-9999999.000000000000000</double>
</property>
<property name="maximum">
<double>9999999.000000000000000</double>
</property>
<property name="showClearButton" stdset="0">
<bool>false</bool>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="mWidthLabel">
<property name="text">
<string>Width</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="mYLabel">
<property name="text">
<string>Y</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="mXLabel">
<property name="text">
<string>X</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item row="0" column="1">
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Reference point</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="1" column="2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="1">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
<widget class="QCheckBox" name="mUpperMiddleCheckBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QCheckBox" name="mMiddleRightCheckBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="mMiddleCheckBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QCheckBox" name="mUpperRightCheckBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QCheckBox" name="mLowerMiddleCheckBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QCheckBox" name="mLowerRightCheckBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QCheckBox" name="mUpperLeftCheckBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QCheckBox" name="mLowerLeftCheckBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="mMiddleLeftCheckBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="1">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="1">
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item row="1" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="0" colspan="2">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QgsDoubleSpinBox</class>
<extends>QDoubleSpinBox</extends>
<header>qgsdoublespinbox.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>mXPosSpin</tabstop>
<tabstop>mYPosSpin</tabstop>
<tabstop>mWidthSpin</tabstop>
<tabstop>mHeightSpin</tabstop>
<tabstop>mUpperLeftCheckBox</tabstop>
<tabstop>mUpperMiddleCheckBox</tabstop>
<tabstop>mUpperRightCheckBox</tabstop>
<tabstop>mMiddleLeftCheckBox</tabstop>
<tabstop>mMiddleCheckBox</tabstop>
<tabstop>mMiddleRightCheckBox</tabstop>
<tabstop>mLowerLeftCheckBox</tabstop>
<tabstop>mLowerMiddleCheckBox</tabstop>
<tabstop>mLowerRightCheckBox</tabstop>
</tabstops>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>QgsLayoutNewItemPropertiesDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>QgsLayoutNewItemPropertiesDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>