/
qgslayoutdesignerdialog.cpp
438 lines (374 loc) · 15.5 KB
/
qgslayoutdesignerdialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/***************************************************************************
qgslayoutdesignerdialog.cpp
---------------------------
begin : July 2017
copyright : (C) 2017 by 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 "qgslayoutdesignerdialog.h"
#include "qgslayoutitemregistry.h"
#include "qgssettings.h"
#include "qgisapp.h"
#include "qgslogger.h"
#include "qgslayout.h"
#include "qgslayoutview.h"
#include "qgslayoutviewtooladditem.h"
#include "qgslayoutviewtoolpan.h"
#include "qgslayoutviewtoolzoom.h"
#include "qgslayoutviewtoolselect.h"
#include "qgsgui.h"
#include "qgslayoutitemguiregistry.h"
#include "qgslayoutruler.h"
#include <QShortcut>
#include <QComboBox>
#include <QLineEdit>
#include <QDesktopWidget>
#include <QSlider>
#include <QLabel>
//add some nice zoom levels for zoom comboboxes
QList<double> QgsLayoutDesignerDialog::sStatusZoomLevelsList { 0.125, 0.25, 0.5, 1.0, 2.0, 4.0, 8.0};
#define FIT_LAYOUT -101
#define FIT_LAYOUT_WIDTH -102
QgsAppLayoutDesignerInterface::QgsAppLayoutDesignerInterface( QgsLayoutDesignerDialog *dialog )
: QgsLayoutDesignerInterface( dialog )
, mDesigner( dialog )
{}
QgsLayout *QgsAppLayoutDesignerInterface::layout()
{
return mDesigner->currentLayout();
}
QgsLayoutView *QgsAppLayoutDesignerInterface::view()
{
return mDesigner->view();
}
void QgsAppLayoutDesignerInterface::close()
{
mDesigner->close();
}
QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFlags flags )
: QMainWindow( parent, flags )
, mInterface( new QgsAppLayoutDesignerInterface( this ) )
, mToolsActionGroup( new QActionGroup( this ) )
{
QgsSettings settings;
int size = settings.value( QStringLiteral( "IconSize" ), QGIS_ICON_SIZE ).toInt();
setIconSize( QSize( size, size ) );
setStyleSheet( QgisApp::instance()->styleSheet() );
setupUi( this );
setWindowTitle( tr( "QGIS Layout Designer" ) );
setAttribute( Qt::WA_DeleteOnClose );
#if QT_VERSION >= 0x050600
setDockOptions( dockOptions() | QMainWindow::GroupedDragging ) ;
#endif
//create layout view
QGridLayout *viewLayout = new QGridLayout();
viewLayout->setSpacing( 0 );
viewLayout->setMargin( 0 );
viewLayout->setContentsMargins( 0, 0, 0, 0 );
centralWidget()->layout()->setSpacing( 0 );
centralWidget()->layout()->setMargin( 0 );
centralWidget()->layout()->setContentsMargins( 0, 0, 0, 0 );
mHorizontalRuler = new QgsLayoutRuler( nullptr, Qt::Horizontal );
mVerticalRuler = new QgsLayoutRuler( nullptr, Qt::Vertical );
mRulerLayoutFix = new QWidget();
mRulerLayoutFix->setAttribute( Qt::WA_NoMousePropagation );
mRulerLayoutFix->setBackgroundRole( QPalette::Window );
mRulerLayoutFix->setFixedSize( mVerticalRuler->rulerSize(), mHorizontalRuler->rulerSize() );
viewLayout->addWidget( mRulerLayoutFix, 0, 0 );
viewLayout->addWidget( mHorizontalRuler, 0, 1 );
viewLayout->addWidget( mVerticalRuler, 1, 0 );
//initial state of rulers
bool showRulers = settings.value( QStringLiteral( "LayoutDesigner/showRulers" ), true ).toBool();
mActionShowRulers->setChecked( showRulers );
mHorizontalRuler->setVisible( showRulers );
mVerticalRuler->setVisible( showRulers );
mRulerLayoutFix->setVisible( showRulers );
mActionShowRulers->blockSignals( false );
connect( mActionShowRulers, &QAction::triggered, this, &QgsLayoutDesignerDialog::showRulers );
mView = new QgsLayoutView();
//mView->setMapCanvas( mQgis->mapCanvas() );
mView->setContentsMargins( 0, 0, 0, 0 );
mView->setHorizontalRuler( mHorizontalRuler );
mView->setVerticalRuler( mVerticalRuler );
viewLayout->addWidget( mView, 1, 1 );
//view does not accept focus via tab
mView->setFocusPolicy( Qt::ClickFocus );
mViewFrame->setLayout( viewLayout );
mViewFrame->setContentsMargins( 0, 0, 0, 1 ); // 1 is deliberate!
mView->setFrameShape( QFrame::NoFrame );
connect( mActionClose, &QAction::triggered, this, &QWidget::close );
// populate with initial items...
Q_FOREACH ( int type, QgsGui::layoutItemGuiRegistry()->itemTypes() )
{
itemTypeAdded( type );
}
//..and listen out for new item types
connect( QgsGui::layoutItemGuiRegistry(), &QgsLayoutItemGuiRegistry::typeAdded, this, &QgsLayoutDesignerDialog::itemTypeAdded );
mAddItemTool = new QgsLayoutViewToolAddItem( mView );
mPanTool = new QgsLayoutViewToolPan( mView );
mPanTool->setAction( mActionPan );
mToolsActionGroup->addAction( mActionPan );
connect( mActionPan, &QAction::triggered, mPanTool, [ = ] { mView->setTool( mPanTool ); } );
mZoomTool = new QgsLayoutViewToolZoom( mView );
mZoomTool->setAction( mActionZoomTool );
mToolsActionGroup->addAction( mActionZoomTool );
connect( mActionZoomTool, &QAction::triggered, mZoomTool, [ = ] { mView->setTool( mZoomTool ); } );
mSelectTool = new QgsLayoutViewToolSelect( mView );
mSelectTool->setAction( mActionSelectMoveItem );
mToolsActionGroup->addAction( mActionSelectMoveItem );
connect( mActionSelectMoveItem, &QAction::triggered, mSelectTool, [ = ] { mView->setTool( mSelectTool ); } );
//Ctrl+= should also trigger zoom in
QShortcut *ctrlEquals = new QShortcut( QKeySequence( QStringLiteral( "Ctrl+=" ) ), this );
connect( ctrlEquals, &QShortcut::activated, mActionZoomIn, &QAction::trigger );
connect( mActionZoomIn, &QAction::triggered, mView, &QgsLayoutView::zoomIn );
connect( mActionZoomOut, &QAction::triggered, mView, &QgsLayoutView::zoomOut );
connect( mActionZoomAll, &QAction::triggered, mView, &QgsLayoutView::zoomFull );
connect( mActionZoomActual, &QAction::triggered, mView, &QgsLayoutView::zoomActual );
connect( mActionZoomToWidth, &QAction::triggered, mView, &QgsLayoutView::zoomWidth );
//create status bar labels
mStatusCursorXLabel = new QLabel( mStatusBar );
mStatusCursorXLabel->setMinimumWidth( 100 );
mStatusCursorYLabel = new QLabel( mStatusBar );
mStatusCursorYLabel->setMinimumWidth( 100 );
mStatusCursorPageLabel = new QLabel( mStatusBar );
mStatusCursorPageLabel->setMinimumWidth( 100 );
mStatusBar->addPermanentWidget( mStatusCursorXLabel );
mStatusBar->addPermanentWidget( mStatusCursorXLabel );
mStatusBar->addPermanentWidget( mStatusCursorYLabel );
mStatusBar->addPermanentWidget( mStatusCursorPageLabel );
mStatusZoomCombo = new QComboBox();
mStatusZoomCombo->setEditable( true );
mStatusZoomCombo->setInsertPolicy( QComboBox::NoInsert );
mStatusZoomCombo->setCompleter( nullptr );
mStatusZoomCombo->setMinimumWidth( 100 );
//zoom combo box accepts decimals in the range 1-9999, with an optional decimal point and "%" sign
QRegularExpression zoomRx( "\\s*\\d{1,4}(\\.\\d?)?\\s*%?" );
QValidator *zoomValidator = new QRegularExpressionValidator( zoomRx, mStatusZoomCombo );
mStatusZoomCombo->lineEdit()->setValidator( zoomValidator );
Q_FOREACH ( double level, sStatusZoomLevelsList )
{
mStatusZoomCombo->insertItem( 0, tr( "%1%" ).arg( level * 100.0, 0, 'f', 1 ), level );
}
mStatusZoomCombo->insertItem( 0, tr( "Fit Layout" ), FIT_LAYOUT );
mStatusZoomCombo->insertItem( 0, tr( "Fit Layout Width" ), FIT_LAYOUT_WIDTH );
connect( mStatusZoomCombo, static_cast<void ( QComboBox::* )( int )>( &QComboBox::activated ), this, &QgsLayoutDesignerDialog::statusZoomCombo_currentIndexChanged );
connect( mStatusZoomCombo->lineEdit(), &QLineEdit::returnPressed, this, &QgsLayoutDesignerDialog::statusZoomCombo_zoomEntered );
mStatusZoomSlider = new QSlider();
mStatusZoomSlider->setFixedWidth( mStatusZoomCombo->width() );
mStatusZoomSlider->setOrientation( Qt::Horizontal );
mStatusZoomSlider->setMinimum( 20 );
mStatusZoomSlider->setMaximum( 800 );
connect( mStatusZoomSlider, &QSlider::valueChanged, this, &QgsLayoutDesignerDialog::sliderZoomChanged );
mStatusZoomCombo->setToolTip( tr( "Zoom level" ) );
mStatusZoomSlider->setToolTip( tr( "Zoom level" ) );
mStatusBar->addPermanentWidget( mStatusZoomCombo );
mStatusBar->addPermanentWidget( mStatusZoomSlider );
//hide borders from child items in status bar under Windows
mStatusBar->setStyleSheet( QStringLiteral( "QStatusBar::item {border: none;}" ) );
mView->setTool( mSelectTool );
mView->setFocus();
connect( mView, &QgsLayoutView::zoomLevelChanged, this, &QgsLayoutDesignerDialog::updateStatusZoom );
connect( mView, &QgsLayoutView::cursorPosChanged, this, &QgsLayoutDesignerDialog::updateStatusCursorPos );
//also listen out for position updates from the horizontal/vertical rulers
connect( mHorizontalRuler, &QgsLayoutRuler::cursorPosChanged, this, &QgsLayoutDesignerDialog::updateStatusCursorPos );
connect( mVerticalRuler, &QgsLayoutRuler::cursorPosChanged, this, &QgsLayoutDesignerDialog::updateStatusCursorPos );
// Panel and toolbar submenus
QMenu *toolbarMenu = new QMenu( tr( "&Toolbars" ), this );
toolbarMenu->setObjectName( QStringLiteral( "mToolbarMenu" ) );
mMenuView->addSeparator();
mMenuView->addMenu( toolbarMenu );
// toolBar already exists, add other widgets as they are created
toolbarMenu->addAction( mLayoutToolbar->toggleViewAction() );
toolbarMenu->addAction( mNavigationToolbar->toggleViewAction() );
toolbarMenu->addAction( mToolsToolbar->toggleViewAction() );
restoreWindowState();
}
QgsAppLayoutDesignerInterface *QgsLayoutDesignerDialog::iface()
{
return mInterface;
}
QgsLayout *QgsLayoutDesignerDialog::currentLayout()
{
return mLayout;
}
void QgsLayoutDesignerDialog::setCurrentLayout( QgsLayout *layout )
{
mLayout = layout;
mView->setCurrentLayout( layout );
}
void QgsLayoutDesignerDialog::setIconSizes( int size )
{
//Set the icon size of for all the toolbars created in the future.
setIconSize( QSize( size, size ) );
//Change all current icon sizes.
QList<QToolBar *> toolbars = findChildren<QToolBar *>();
Q_FOREACH ( QToolBar *toolbar, toolbars )
{
toolbar->setIconSize( QSize( size, size ) );
}
}
void QgsLayoutDesignerDialog::open()
{
show();
activate();
mView->zoomFull(); // zoomFull() does not work properly until we have called show()
#if 0 // TODO
if ( mView )
{
mView->updateRulers();
}
#endif
}
void QgsLayoutDesignerDialog::activate()
{
// bool shown = isVisible();
show();
raise();
setWindowState( windowState() & ~Qt::WindowMinimized );
activateWindow();
#if 0 // TODO
if ( !shown )
{
on_mActionZoomAll_triggered();
}
#endif
}
void QgsLayoutDesignerDialog::showRulers( bool visible )
{
//show or hide rulers
mHorizontalRuler->setVisible( visible );
mVerticalRuler->setVisible( visible );
mRulerLayoutFix->setVisible( visible );
QgsSettings settings;
settings.setValue( QStringLiteral( "LayoutDesigner/showRulers" ), visible );
}
void QgsLayoutDesignerDialog::closeEvent( QCloseEvent * )
{
emit aboutToClose();
saveWindowState();
}
void QgsLayoutDesignerDialog::itemTypeAdded( int type )
{
QString name = QgsApplication::layoutItemRegistry()->itemMetadata( type )->visibleName();
// update UI for new item type
QAction *action = new QAction( tr( "Add %1" ).arg( name ), this );
action->setToolTip( tr( "Adds a new %1 to the layout" ).arg( name ) );
action->setCheckable( true );
action->setData( type );
action->setIcon( QgsGui::layoutItemGuiRegistry()->itemMetadata( type )->creationIcon() );
mToolsActionGroup->addAction( action );
mItemMenu->addAction( action );
mToolsToolbar->addAction( action );
connect( action, &QAction::triggered, this, [this, type]()
{
activateNewItemCreationTool( type );
} );
}
void QgsLayoutDesignerDialog::statusZoomCombo_currentIndexChanged( int index )
{
QVariant data = mStatusZoomCombo->itemData( index );
if ( data.toInt() == FIT_LAYOUT )
{
mView->zoomFull();
}
else if ( data.toInt() == FIT_LAYOUT_WIDTH )
{
mView->zoomWidth();
}
else
{
double selectedZoom = data.toDouble();
if ( mView )
{
mView->setZoomLevel( selectedZoom );
//update zoom combobox text for correct format (one decimal place, trailing % sign)
whileBlocking( mStatusZoomCombo )->lineEdit()->setText( tr( "%1%" ).arg( selectedZoom * 100.0, 0, 'f', 1 ) );
}
}
}
void QgsLayoutDesignerDialog::statusZoomCombo_zoomEntered()
{
if ( !mView )
{
return;
}
//need to remove spaces and "%" characters from input text
QString zoom = mStatusZoomCombo->currentText().remove( QChar( '%' ) ).trimmed();
mView->setZoomLevel( zoom.toDouble() / 100 );
}
void QgsLayoutDesignerDialog::sliderZoomChanged( int value )
{
mView->setZoomLevel( value / 100.0 );
}
void QgsLayoutDesignerDialog::updateStatusZoom()
{
double dpi = QgsApplication::desktop()->logicalDpiX();
//monitor dpi is not always correct - so make sure the value is sane
if ( ( dpi < 60 ) || ( dpi > 1200 ) )
dpi = 72;
//pixel width for 1mm on screen
double scale100 = dpi / 25.4;
//current zoomLevel
double zoomLevel = mView->transform().m11() * 100 / scale100;
whileBlocking( mStatusZoomCombo )->lineEdit()->setText( tr( "%1%" ).arg( zoomLevel, 0, 'f', 1 ) );
whileBlocking( mStatusZoomSlider )->setValue( zoomLevel );
}
void QgsLayoutDesignerDialog::updateStatusCursorPos( QPointF position )
{
if ( !mView->currentLayout() )
{
return;
}
//convert cursor position to position on current page
#if 0 // TODO
QPointF pagePosition = mView->currentLayout()->positionOnPage( cursorPosition );
int currentPage = mView->currentLayout()->pageNumberForPoint( cursorPosition );
#endif
QPointF pagePosition = position;
int currentPage = 1;
mStatusCursorXLabel->setText( QString( tr( "x: %1 mm" ) ).arg( pagePosition.x() ) );
mStatusCursorYLabel->setText( QString( tr( "y: %1 mm" ) ).arg( pagePosition.y() ) );
mStatusCursorPageLabel->setText( QString( tr( "page: %1" ) ).arg( currentPage ) );
}
QgsLayoutView *QgsLayoutDesignerDialog::view()
{
return mView;
}
void QgsLayoutDesignerDialog::saveWindowState()
{
QgsSettings settings;
settings.setValue( QStringLiteral( "LayoutDesigner/geometry" ), saveGeometry() );
// store the toolbar/dock widget settings using Qt settings API
settings.setValue( QStringLiteral( "LayoutDesigner/state" ), saveState() );
}
void QgsLayoutDesignerDialog::restoreWindowState()
{
// restore the toolbar and dock widgets positions using Qt settings API
QgsSettings settings;
//TODO - defaults
if ( !restoreState( settings.value( QStringLiteral( "LayoutDesigner/state" ) /*, QByteArray::fromRawData( ( char * )defaultComposerUIstate, sizeof defaultComposerUIstate ) */ ).toByteArray() ) )
{
QgsDebugMsg( "restore of layout UI state failed" );
}
// restore window geometry
if ( !restoreGeometry( settings.value( QStringLiteral( "LayoutDesigner/geometry" ) /*, QByteArray::fromRawData( ( char * )defaultComposerUIgeometry, sizeof defaultComposerUIgeometry ) */ ).toByteArray() ) )
{
QgsDebugMsg( "restore of layout UI geometry failed" );
}
}
void QgsLayoutDesignerDialog::activateNewItemCreationTool( int type )
{
mAddItemTool->setItemType( type );
if ( mView )
{
mView->setTool( mAddItemTool );
}
}