Skip to content

Commit 701ba52

Browse files
author
mhugent
committed
Add a new composer item to draw basic shapes (atm ellipse, rectangle, triangle), fix some composer related bugs and bring rotation to composer item level
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12183 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent c9c6ffc commit 701ba52

31 files changed

+982
-165
lines changed
974 Bytes
Loading

python/core/qgscomposeritem.sip

+43-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** \ingroup MapComposer
22
* A item that forms part of a map composition.
33
*/
4-
class QgsComposerItem: QGraphicsRectItem
4+
class QgsComposerItem: public QObject, public QGraphicsRectItem
55
{
66
%TypeHeaderCode
77
#include <qgscomposeritem.h>
@@ -130,6 +130,23 @@ class QgsComposerItem: QGraphicsRectItem
130130
/**Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE*/
131131
QFont scaledFontPixelSize( const QFont& font ) const;
132132

133+
/**Locks / unlocks the item position for mouse drags
134+
@note this method was added in version 1.2*/
135+
void setPositionLock( bool lock );
136+
137+
/**Returns position lock for mouse drags (true means locked)
138+
@note this method was added in version 1.2*/
139+
bool positionLock() const;
140+
141+
/**Update mouse cursor at (item) position
142+
@note this method was added in version 1.2*/
143+
void updateCursor( const QPointF& itemPos );
144+
145+
double rotation() const;
146+
147+
public slots:
148+
void setRotation( double r);
149+
133150
protected:
134151

135152
//event handlers
@@ -163,4 +180,28 @@ class QgsComposerItem: QGraphicsRectItem
163180

164181
/**Draw background*/
165182
virtual void drawBackground( QPainter* p );
166-
};
183+
184+
/**Returns the current (zoom level dependent) tolerance to decide if mouse position is close enough to the \
185+
item border for resizing*/
186+
double rectHandlerBorderTolerance() const;
187+
188+
/**Returns the size of the lock symbol depending on the composer zoom level and the item size
189+
@note: this function was introduced in version 1.2*/
190+
double lockSymbolSize() const;
191+
192+
/**Returns the zoom factor of the graphics view.
193+
@return the factor or -1 in case of error (e.g. graphic view does not exist)
194+
@note: this function was introduced in version 1.2*/
195+
double horizontalViewScaleFactor() const;
196+
197+
/**Calculates width and hight of the picture (in mm) such that it fits into the item frame with the given rotation*/
198+
bool imageSizeConsideringRotation( double& width, double& height ) const;
199+
/**Calculates corner point after rotation and scaling*/
200+
bool cornerPointOnRotatedAndScaledRect( double& x, double& y, double width, double height ) const;
201+
/**Returns a point on the line from startPoint to directionPoint that is a certain distance away from the starting point*/
202+
QPointF pointOnLineWithDistance( const QPointF& startPoint, const QPointF& directionPoint, double distance ) const;
203+
204+
signals:
205+
/**Is emitted on rotation change to notify north arrow pictures*/
206+
void rotationChanged( double newRotation );
207+
};

python/core/qgscomposerlegend.sip

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** \ingroup MapComposer
22
* A legend that can be placed onto a map composition
33
*/
4-
class QgsComposerLegend: QObject, QgsComposerItem
4+
class QgsComposerLegend: QgsComposerItem
55
{
66
%TypeHeaderCode
77
#include <qgscomposerlegend.h>

python/core/qgscomposermap.sip

+1-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* \brief Object representing map window.
44
*/
55
// NOTE: QgsComposerMapBase must be first, otherwise does not compile
6-
class QgsComposerMap : QObject, QgsComposerItem
6+
class QgsComposerMap : QgsComposerItem
77
{
88
%TypeHeaderCode
99
#include <qgscomposermap.h>
@@ -211,11 +211,6 @@ class QgsComposerMap : QObject, QgsComposerItem
211211
@note this function was added in version 1.4*/
212212
void updateBoundingRect();
213213

214-
/**Sets the rotation of the map content
215-
@note this function was added in version 1.4*/
216-
void setRotation(double r);
217-
double rotation() const;
218-
219214
/**Sets length of the cros segments (if grid style is cross)
220215
@note this function was added in version 1.4*/
221216
void setCrossLength(double l);
@@ -231,6 +226,4 @@ class QgsComposerMap : QObject, QgsComposerItem
231226
signals:
232227
/**Is emitted when width/height is changed as a result of user interaction*/
233228
void extentChanged();
234-
/**Is emitted on rotation change to notify north arrow pictures*/
235-
void rotationChanged( double newRotation );
236229
};

python/core/qgscomposerpicture.sip

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** \ingroup MapComposer
22
* A composer class that displays svg files or raster format (jpg, png, ...)
33
* */
4-
class QgsComposerPicture: QObject, QgsComposerItem
4+
class QgsComposerPicture: QgsComposerItem
55
{
66

77
%TypeHeaderCode
@@ -23,10 +23,6 @@ class QgsComposerPicture: QObject, QgsComposerItem
2323
corresponds to 1 scene size unit*/
2424
void setSceneRect( const QRectF& rectangle );
2525

26-
void setRotation( double rotation );
27-
28-
double rotation() const;
29-
3026
/** stores state in Dom node
3127
* @param node is Dom node corresponding to 'Composer' tag
3228
* @param temp write template file

python/core/qgscomposerscalebar.sip

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* A scale bar item that can be added to a map composition.
33
*/
44

5-
class QgsComposerScaleBar: QObject, QgsComposerItem
5+
class QgsComposerScaleBar: QgsComposerItem
66
{
77
%TypeHeaderCode
88
#include "qgscomposerscalebar.h"

python/core/qgscomposershape.sip

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**A composer items that draws common shapes (ellipse, triangle, rectangle)*/
2+
class QgsComposerShape: public QgsComposerItem
3+
{
4+
%TypeHeaderCode
5+
#include "qgscomposershape.h"
6+
%End
7+
public:
8+
9+
enum Shape
10+
{
11+
Ellipse,
12+
Rectangle,
13+
Triangle
14+
};
15+
16+
QgsComposerShape( QgsComposition* composition /TransferThis/);
17+
QgsComposerShape( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition );
18+
~QgsComposerShape();
19+
20+
/** \brief Reimplementation of QCanvasItem::paint - draw on canvas */
21+
void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget );
22+
23+
/** stores state in Dom node
24+
* @param node is Dom node corresponding to 'Composer' tag
25+
* @param temp write template file
26+
*/
27+
bool writeXML( QDomElement& elem, QDomDocument & doc ) const;
28+
29+
/** sets state from Dom document
30+
* @param itemElem is Dom node corresponding to item tag
31+
*/
32+
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
33+
34+
//setters and getters
35+
void setLineWidth( double width );
36+
double lineWidth() const;
37+
void setOutlineColor( const QColor& color );
38+
QColor outlineColor() const;
39+
void setFillColor( const QColor& color );
40+
QColor fillColor() const;
41+
QgsComposerShape::Shape shapeType() const;
42+
void setShapeType( QgsComposerShape::Shape s );
43+
};

src/app/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ SET(QGIS_APP_SRCS
8080
composer/qgscomposermanager.cpp
8181
composer/qgscomposermapwidget.cpp
8282
composer/qgscomposerscalebarwidget.cpp
83+
composer/qgscomposershapewidget.cpp
8384
composer/qgscomposerlegenditemdialog.cpp
8485
composer/qgscomposerlegendwidget.cpp
8586
composer/qgscompositionwidget.cpp
@@ -179,6 +180,7 @@ SET (QGIS_APP_MOC_HDRS
179180
composer/qgscomposermapwidget.h
180181
composer/qgscomposerpicturewidget.h
181182
composer/qgscomposerscalebarwidget.h
183+
composer/qgscomposershapewidget.h
182184
composer/qgscompositionwidget.h
183185
composer/qgsitempositiondialog.h
184186

src/app/composer/qgscomposer.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include "qgscomposerpicturewidget.h"
3232
#include "qgscomposerscalebar.h"
3333
#include "qgscomposerscalebarwidget.h"
34+
#include "qgscomposershape.h"
35+
#include "qgscomposershapewidget.h"
3436
#include "qgsexception.h"
3537
#include "qgsproject.h"
3638
#include "qgsmapcanvas.h"
@@ -106,6 +108,7 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title ): QMainWindow(),
106108
toggleActionGroup->addAction( mActionAddNewScalebar );
107109
toggleActionGroup->addAction( mActionAddImage );
108110
toggleActionGroup->addAction( mActionSelectMoveItem );
111+
toggleActionGroup->addAction( mActionAddBasicShape );
109112
toggleActionGroup->setExclusive( true );
110113

111114
mActionAddNewMap->setCheckable( true );
@@ -115,6 +118,7 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title ): QMainWindow(),
115118
mActionAddNewScalebar->setCheckable( true );
116119
mActionAddImage->setCheckable( true );
117120
mActionMoveItemContent->setCheckable( true );
121+
mActionAddBasicShape->setCheckable( true );
118122

119123
#ifdef Q_WS_MAC
120124
QMenu *appMenu = menuBar()->addMenu( tr( "QGIS" ) );
@@ -150,6 +154,7 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title ): QMainWindow(),
150154
layoutMenu->addAction( mActionAddImage );
151155
layoutMenu->addAction( mActionSelectMoveItem );
152156
layoutMenu->addAction( mActionMoveItemContent );
157+
layoutMenu->addAction( mActionAddBasicShape );
153158
layoutMenu->addSeparator();
154159
layoutMenu->addAction( mActionGroupItems );
155160
layoutMenu->addAction( mActionUngroupItems );
@@ -243,6 +248,7 @@ void QgsComposer::setupTheme()
243248
mActionAddNewLabel->setIcon( QgisApp::getThemeIcon( "/mActionLabel.png" ) );
244249
mActionAddNewLegend->setIcon( QgisApp::getThemeIcon( "/mActionAddLegend.png" ) );
245250
mActionAddNewScalebar->setIcon( QgisApp::getThemeIcon( "/mActionScaleBar.png" ) );
251+
mActionAddBasicShape->setIcon( QgisApp::getThemeIcon( "/mActionAddBasicShape.png" ) );
246252
mActionSelectMoveItem->setIcon( QgisApp::getThemeIcon( "/mActionSelectPan.png" ) );
247253
mActionMoveItemContent->setIcon( QgisApp::getThemeIcon( "/mActionMoveItemContent.png" ) );
248254
mActionGroupItems->setIcon( QgisApp::getThemeIcon( "/mActionGroupItems.png" ) );
@@ -268,6 +274,7 @@ void QgsComposer::connectSlots()
268274
connect( mView, SIGNAL( composerScaleBarAdded( QgsComposerScaleBar* ) ), this, SLOT( addComposerScaleBar( QgsComposerScaleBar* ) ) );
269275
connect( mView, SIGNAL( composerLegendAdded( QgsComposerLegend* ) ), this, SLOT( addComposerLegend( QgsComposerLegend* ) ) );
270276
connect( mView, SIGNAL( composerPictureAdded( QgsComposerPicture* ) ), this, SLOT( addComposerPicture( QgsComposerPicture* ) ) );
277+
connect( mView, SIGNAL( composerShapeAdded( QgsComposerShape* ) ), this, SLOT( addComposerShape( QgsComposerShape* ) ) );
271278
connect( mView, SIGNAL( actionFinished() ), this, SLOT( setSelectionTool() ) );
272279
}
273280

@@ -330,6 +337,10 @@ void QgsComposer::setTitle( const QString& title )
330337
{
331338
mTitle = title;
332339
setWindowTitle( mTitle );
340+
if ( mWindowAction )
341+
{
342+
mWindowAction->setText( title );
343+
}
333344
}
334345

335346
void QgsComposer::showCompositionOptions( QWidget *w )
@@ -786,6 +797,14 @@ void QgsComposer::on_mActionAddImage_triggered()
786797
}
787798
}
788799

800+
void QgsComposer::on_mActionAddBasicShape_triggered()
801+
{
802+
if ( mView )
803+
{
804+
mView->setCurrentTool( QgsComposerView::AddShape );
805+
}
806+
}
807+
789808
void QgsComposer::on_mActionSaveAsTemplate_triggered()
790809
{
791810
//show file dialog
@@ -1015,6 +1034,16 @@ void QgsComposer::writeXML( QDomNode& parentNode, QDomDocument& doc )
10151034
{
10161035
QDomElement composerElem = doc.createElement( "Composer" );
10171036
composerElem.setAttribute( "title", mTitle );
1037+
1038+
//store if composer is open or closed
1039+
if ( isVisible() )
1040+
{
1041+
composerElem.setAttribute( "visible", 1 );
1042+
}
1043+
else
1044+
{
1045+
composerElem.setAttribute( "visible", 0 );
1046+
}
10181047
parentNode.appendChild( composerElem );
10191048

10201049
//store composer items:
@@ -1173,6 +1202,21 @@ void QgsComposer::readXML( const QDomElement& composerElem, const QDomDocument&
11731202
showItemOptions( newPicture );
11741203
}
11751204

1205+
//composer shapes
1206+
QDomNodeList composerShapeList = composerElem.elementsByTagName( "ComposerShape" );
1207+
for ( int i = 0; i < composerShapeList.size(); ++i )
1208+
{
1209+
QDomElement currentShapeElem = composerShapeList.at( i ).toElement();
1210+
QgsComposerShape* newShape = new QgsComposerShape( mComposition );
1211+
newShape->readXML( currentShapeElem, doc );
1212+
addComposerShape( newShape );
1213+
mComposition->addItem( newShape );
1214+
mComposition->update();
1215+
mComposition->clearSelection();
1216+
newShape->setSelected( true );
1217+
showItemOptions( newShape );
1218+
}
1219+
11761220
mComposition->sortZList();
11771221
mView->setComposition( mComposition );
11781222

@@ -1247,6 +1291,16 @@ void QgsComposer::addComposerPicture( QgsComposerPicture* picture )
12471291
mItemWidgetMap.insert( picture, pWidget );
12481292
}
12491293

1294+
void QgsComposer::addComposerShape( QgsComposerShape* shape )
1295+
{
1296+
if ( !shape )
1297+
{
1298+
return;
1299+
}
1300+
QgsComposerShapeWidget* sWidget = new QgsComposerShapeWidget( shape );
1301+
mItemWidgetMap.insert( shape, sWidget );
1302+
}
1303+
12501304
void QgsComposer::deleteItem( QgsComposerItem* item )
12511305
{
12521306
QMap<QgsComposerItem*, QWidget*>::iterator it = mItemWidgetMap.find( item );

src/app/composer/qgscomposer.h

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class QgsComposerLegend;
2727
class QgsComposerMap;
2828
class QgsComposerPicture;
2929
class QgsComposerScaleBar;
30+
class QgsComposerShape;
3031
class QgsComposerView;
3132
class QgsComposition;
3233
class QgsMapCanvas;
@@ -142,6 +143,9 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
142143
//! Add new picture
143144
void on_mActionAddImage_triggered();
144145

146+
//! Add ellipse shape item
147+
void on_mActionAddBasicShape_triggered();
148+
145149
//! Save composer as template
146150
void on_mActionSaveAsTemplate_triggered();
147151

@@ -204,6 +208,9 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
204208
/**Adds a composer picture to the item/widget map and creates a configuration widget*/
205209
void addComposerPicture( QgsComposerPicture* picture );
206210

211+
/**Adds a composer shape to the item/widget map and creates a configuration widget*/
212+
void addComposerShape( QgsComposerShape* shape );
213+
207214
/**Removes item from the item/widget map and deletes the configuration widget*/
208215
void deleteItem( QgsComposerItem* item );
209216

src/app/composer/qgscomposermapwidget.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ void QgsComposerMapWidget::on_mRotationSpinBox_valueChanged( int value )
162162
return;
163163
}
164164

165-
mComposerMap->setRotation( value );
165+
mComposerMap->setMapRotation( value );
166166
mComposerMap->cache();
167167
mComposerMap->update();
168168
}

src/app/composer/qgscomposerpicturewidget.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ QgsComposerPictureWidget::QgsComposerPictureWidget( QgsComposerPicture* picture
4646
//add preview icons
4747
addStandardDirectoriesToPreview();
4848
connect( mPicture, SIGNAL( settingsChanged() ), this, SLOT( setGuiElementValues() ) );
49+
connect( mPicture, SIGNAL( rotationChanged( double ) ), this, SLOT( setGuiElementValues() ) );
4950
}
5051

5152
QgsComposerPictureWidget::~QgsComposerPictureWidget()

0 commit comments

Comments
 (0)