/
MDIViewPage.cpp
1273 lines (1107 loc) · 44.7 KB
/
MDIViewPage.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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (c) 2007 Jürgen Riegel <juergen.riegel@web.de> *
* Copyright (c) 2013 Luke Parry <l.parry@warwick.ac.uk> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <cmath>
# include <QAction>
# include <QApplication>
# include <QContextMenuEvent>
# include <QMenu>
# include <QMessageBox>
# include <QPageLayout>
# include <QPageSize>
# include <QPaintEngine>
# include <QPainter>
# include <QPdfWriter>
# include <QPrinter>
# include <QPrintDialog>
# include <QPrintPreviewDialog>
# include <boost_signals2.hpp>
#endif
#include <App/Application.h>
#include <App/Document.h>
#include <App/DocumentObject.h>
#include <Base/Console.h>
#include <Base/Stream.h>
#include <Base/Tools.h>
#include <Gui/Application.h>
#include <Gui/Command.h>
#include <Gui/Document.h>
#include <Gui/FileDialog.h>
#include <Gui/MainWindow.h>
#include <Gui/Selection.h>
#include <Gui/SelectionObject.h>
#include <Gui/ViewProvider.h>
#include <Gui/WaitCursor.h>
#include <Gui/Window.h>
#include <Mod/TechDraw/App/DrawPage.h>
#include <Mod/TechDraw/App/DrawPagePy.h>
#include <Mod/TechDraw/App/DrawTemplate.h>
#include <Mod/TechDraw/App/Preferences.h>
#include "MDIViewPage.h"
#include "QGIEdge.h"
#include "QGIFace.h"
#include "QGIVertex.h"
#include "QGIView.h"
#include "QGIViewBalloon.h"
#include "QGIViewDimension.h"
#include "QGMText.h"
#include "QGSPage.h"
#include "QGVPage.h"
#include "Rez.h"
#include "ViewProviderPage.h"
using namespace TechDrawGui;
using namespace TechDraw;
namespace bp = boost::placeholders;
/* TRANSLATOR TechDrawGui::MDIViewPage */
TYPESYSTEM_SOURCE_ABSTRACT(TechDrawGui::MDIViewPage, Gui::MDIView)
MDIViewPage::MDIViewPage(ViewProviderPage *pageVp, Gui::Document* doc, QWidget* parent)
: Gui::MDIView(doc, parent),
m_vpPage(pageVp),
m_orientation(QPageLayout::Landscape),
m_paperSize(QPageSize::A4),
m_pagewidth(0.0),
m_pageheight(0.0)
{
setMouseTracking(true);
m_toggleKeepUpdatedAction = new QAction(tr("Toggle &Keep Updated"), this);
connect(m_toggleKeepUpdatedAction, SIGNAL(triggered()), this, SLOT(toggleKeepUpdated()));
m_toggleFrameAction = new QAction(tr("Toggle &Frames"), this);
connect(m_toggleFrameAction, SIGNAL(triggered()), this, SLOT(toggleFrame()));
m_exportSVGAction = new QAction(tr("&Export SVG"), this);
connect(m_exportSVGAction, SIGNAL(triggered()), this, SLOT(saveSVG()));
m_exportDXFAction = new QAction(tr("Export DXF"), this);
connect(m_exportDXFAction, SIGNAL(triggered()), this, SLOT(saveDXF()));
m_exportPDFAction = new QAction(tr("Export PDF"), this);
connect(m_exportPDFAction, SIGNAL(triggered()), this, SLOT(savePDF()));
m_printAllAction = new QAction(tr("Print All Pages"), this);
connect(m_printAllAction, SIGNAL(triggered()), this, SLOT(printAll()));
isSelectionBlocked = false;
QString tabText = QString::fromUtf8(pageVp->getDrawPage()->getNameInDocument());
tabText += QString::fromUtf8("[*]");
setWindowTitle(tabText);
//get informed by App side about deleted DocumentObjects
App::Document* appDoc = m_vpPage->getDocument()->getDocument();
auto bnd = boost::bind(&MDIViewPage::onDeleteObject, this, bp::_1);
connectDeletedObject = appDoc->signalDeletedObject.connect(bnd);
}
MDIViewPage::~MDIViewPage()
{
connectDeletedObject.disconnect();
}
void MDIViewPage::setScene(QGSPage* scene, QGVPage* viewWidget)
{
m_scene = scene;
setCentralWidget(viewWidget); //this makes viewWidget a Qt child of MDIViewPage
QObject::connect(m_scene, SIGNAL(selectionChanged()),
this, SLOT (sceneSelectionChanged()));
}
void MDIViewPage::setDocumentObject(const std::string& name)
{
m_objectName = name;
setObjectName(Base::Tools::fromStdString(name));
}
void MDIViewPage::setDocumentName(const std::string& name)
{
m_documentName = name;
}
void MDIViewPage::closeEvent(QCloseEvent* event)
{
// Base::Console().Message("MDIVP::closeEvent()\n");
MDIView::closeEvent(event);
if (!event->isAccepted())
return;
detachSelection();
blockSceneSelection(true);
// when closing the view from GUI notify the view provider to mark it invisible
if (_pcDocument && !m_objectName.empty()) {
App::Document* doc = _pcDocument->getDocument();
if (doc) {
App::DocumentObject* obj = doc->getObject(m_objectName.c_str());
Gui::ViewProvider* vp = _pcDocument->getViewProvider(obj);
if (vp)
vp->hide();
}
}
blockSceneSelection(false);
}
void MDIViewPage::onDeleteObject(const App::DocumentObject& obj)
{
//if this page has a QView for this obj, delete it.
blockSceneSelection(true);
if (obj.isDerivedFrom(TechDraw::DrawView::getClassTypeId())) {
(void) m_scene->removeQViewByName(obj.getNameInDocument());
}
blockSceneSelection(false);
}
bool MDIViewPage::onMsg(const char *pMsg, const char **)
{
Gui::Document *doc(getGuiDocument());
if (!doc) {
return false;
} else if (strcmp("ViewFit", pMsg) == 0) {
viewAll();
return true;
} else if (strcmp("Save", pMsg) == 0 ) {
doc->save();
return true;
} else if (strcmp("SaveAs", pMsg) == 0 ) {
doc->saveAs();
return true;
} else if (strcmp("Undo", pMsg) == 0 ) {
doc->undo(1);
Gui::Command::updateActive();
return true;
} else if (strcmp("Redo", pMsg) == 0 ) {
doc->redo(1);
Gui::Command::updateActive();
return true;
}
return false;
}
bool MDIViewPage::onHasMsg(const char* pMsg) const
{
if (strcmp("ViewFit", pMsg) == 0)
return true;
else if(strcmp("Redo", pMsg) == 0 && getAppDocument()->getAvailableRedos() > 0)
return true;
else if(strcmp("Undo", pMsg) == 0 && getAppDocument()->getAvailableUndos() > 0)
return true;
else if (strcmp("Print", pMsg) == 0)
return true;
else if (strcmp("Save", pMsg) == 0)
return true;
else if (strcmp("SaveAs", pMsg) == 0)
return true;
else if (strcmp("PrintPreview", pMsg) == 0)
return true;
else if (strcmp("PrintPdf",pMsg) == 0)
return true;
else if (strcmp("PrintAll",pMsg) == 0)
return true;
return false;
}
//called by ViewProvider when Page feature Label changes
void MDIViewPage::setTabText(std::string tabText)
{
if (!isPassive() && !tabText.empty()) {
QString cap = QString::fromLatin1("%1 [*]")
.arg(QString::fromUtf8(tabText.c_str()));
setWindowTitle(cap);
}
}
//**** printing routines
void MDIViewPage::getPaperAttributes()
{
App::DocumentObject *obj = m_vpPage->getDrawPage()->Template.getValue();
auto pageTemplate( dynamic_cast<TechDraw::DrawTemplate *>(obj) );
if( pageTemplate ) {
m_pagewidth = pageTemplate->Width.getValue();
m_pageheight = pageTemplate->Height.getValue();
}
m_paperSize = QPageSize::id(QSizeF(m_pagewidth, m_pageheight), QPageSize::Millimeter, QPageSize::FuzzyOrientationMatch);
if (m_pagewidth > m_pageheight) {
m_orientation = QPageLayout::Landscape;
} else {
m_orientation = QPageLayout::Portrait;
}
}
void MDIViewPage::printPdf()
{
QStringList filter;
filter << QObject::tr("PDF (*.pdf)");
filter << QObject::tr("All Files (*.*)");
QString fn = Gui::FileDialog::getSaveFileName(Gui::getMainWindow(), QObject::tr("Export Page As PDF"),
QString(), filter.join(QLatin1String(";;")));
if (fn.isEmpty()) {
return;
}
Gui::WaitCursor wc;
std::string utf8Content = fn.toUtf8().constData();
printPdf(utf8Content);
}
void MDIViewPage::printPdf(std::string file)
{
if (file.empty()) {
Base::Console().Warning("MDIViewPage - no file specified\n");
return;
}
getPaperAttributes();
QString filename = QString::fromUtf8(file.data(), file.size());
QPrinter printer(QPrinter::HighResolution);
printer.setFullPage(true);
printer.setOutputFileName(filename);
if (m_paperSize == QPageSize::Ledger) {
printer.setPageOrientation((QPageLayout::Orientation) (1 - m_orientation)); //reverse 0/1
} else {
printer.setPageOrientation(m_orientation);
}
if (m_paperSize == QPageSize::Custom) {
printer.setPageSize(QPageSize(QSizeF(m_pagewidth, m_pageheight), QPageSize::Millimeter));
} else {
printer.setPageSize(QPageSize(m_paperSize));
}
print(&printer);
}
void MDIViewPage::print()
{
// Base::Console().Message("MDIVP::print()\n");
getPaperAttributes();
QPrinter printer(QPrinter::HighResolution);
printer.setFullPage(true);
if (m_paperSize == QPageSize::Custom) {
printer.setPageSize(QPageSize(QSizeF(m_pagewidth, m_pageheight), QPageSize::Millimeter));
} else {
printer.setPageSize(QPageSize(m_paperSize));
}
printer.setPageOrientation(m_orientation);
QPrintDialog dlg(&printer, this);
if (dlg.exec() == QDialog::Accepted) {
print(&printer);
}
}
void MDIViewPage::printPreview()
{
getPaperAttributes();
QPrinter printer(QPrinter::HighResolution);
printer.setFullPage(true);
if (m_paperSize == QPageSize::Custom) {
printer.setPageSize(QPageSize(QSizeF(m_pagewidth, m_pageheight), QPageSize::Millimeter));
} else {
printer.setPageSize(QPageSize(m_paperSize));
}
printer.setPageOrientation(m_orientation);
QPrintPreviewDialog dlg(&printer, this);
connect(&dlg, SIGNAL(paintRequested(QPrinter*)),
this, SLOT(print(QPrinter*)));
dlg.exec();
}
void MDIViewPage::print(QPrinter* printer)
{
// Base::Console().Message("MDIVP::print(printer)\n");
getPaperAttributes();
// As size of the render area paperRect() should be used. When performing a real
// print pageRect() may also work but the output is cropped at the bottom part.
// So, independent whether pageRect() or paperRect() is used there is no scaling effect.
// However, when using a different paper size as set in the drawing template (e.g.
// DIN A5 instead of DIN A4) then the output is scaled.
//
// When creating a PDF file there seems to be no difference between pageRect() and
// paperRect().
//
// When showing the preview of a print paperRect() must be used because with pageRect()
// a certain scaling effect can be observed and the content becomes smaller.
QPaintEngine::Type paintType = printer->paintEngine()->type();
if (printer->outputFormat() == QPrinter::NativeFormat) {
QPageSize::PageSizeId psPrtSetting = printer->pageLayout().pageSize().id();
// for the preview a 'Picture' paint engine is used which we don't
// care if it uses wrong printer settings
bool doPrint = paintType != QPaintEngine::Picture;
if (doPrint && printer->pageLayout().orientation() != m_orientation) {
int ret = QMessageBox::warning(this, tr("Different orientation"),
tr("The printer uses a different orientation than the drawing.\n"
"Do you want to continue?"),
QMessageBox::Yes | QMessageBox::No);
if (ret != QMessageBox::Yes)
return;
}
if (doPrint && psPrtSetting != m_paperSize) {
int ret = QMessageBox::warning(this, tr("Different paper size"),
tr("The printer uses a different paper size than the drawing.\n"
"Do you want to continue?"),
QMessageBox::Yes | QMessageBox::No);
if (ret != QMessageBox::Yes)
return;
}
}
QPainter p(printer);
if (!p.isActive() && !printer->outputFileName().isEmpty()) {
qApp->setOverrideCursor(Qt::ArrowCursor);
QMessageBox::critical(this, tr("Opening file failed"),
tr("Can not open file %1 for writing.").arg(printer->outputFileName()));
qApp->restoreOverrideCursor();
return;
}
QRect targetRect = printer->pageLayout().fullRectPixels(printer->resolution());
#ifdef Q_OS_WIN32
// On Windows the preview looks broken when using paperRect as render area.
// Although the picture is scaled when using pageRect, it looks just fine.
if (paintType == QPaintEngine::Picture)
targetRect = printer->pageLayout().paintRectPixels(printer->resolution());
#endif
//bool block =
static_cast<void> (blockSelection(true)); // avoid to be notified by itself
Gui::Selection().clearSelection();
bool saveState = m_vpPage->getFrameState();
m_vpPage->setFrameState(false);
m_vpPage->setTemplateMarkers(false);
m_scene->refreshViews();
Gui::Selection().clearSelection();
App::DocumentObject *obj = m_vpPage->getDrawPage()->Template.getValue();
auto pageTemplate( dynamic_cast<TechDraw::DrawTemplate *>(obj) );
double width = 0.0;
double height = 0.0;
if( pageTemplate ) {
width = Rez::guiX(pageTemplate->Width.getValue());
height = Rez::guiX(pageTemplate->Height.getValue());
}
QRectF sourceRect(0.0, -height, width, height);
m_scene->render(&p, targetRect, sourceRect);
// Reset
m_vpPage->setFrameState(saveState);
m_vpPage->setTemplateMarkers(saveState);
m_scene->refreshViews();
//bool block =
static_cast<void> (blockSelection(false));
}
//static routine to print all pages in a document
void MDIViewPage::printAll(QPrinter* printer,
App::Document* doc)
{
// Base::Console().Message("MDIVP::printAll()\n");
QPainter painter(printer);
QPageLayout pageLayout = printer->pageLayout();
bool firstTime = true;
std::vector<App::DocumentObject*> docObjs = doc->getObjectsOfType(TechDraw::DrawPage::getClassTypeId());
for (auto& obj: docObjs) {
Gui::ViewProvider* vp = Gui::Application::Instance->getViewProvider(obj);
if (!vp) {
continue; // can't print this one
}
TechDrawGui::ViewProviderPage* vpp = dynamic_cast<TechDrawGui::ViewProviderPage*>(vp);
if (!vpp) {
continue; // can't print this one
}
TechDraw::DrawPage* dp = static_cast<TechDraw::DrawPage*>(obj);
double width = 297.0; //default to A4 Landscape 297 x 210
double height = 210.0;
setPageLayout(pageLayout, dp, width, height);
printer->setPageLayout(pageLayout);
//for some reason the first page doesn't obey the pageLayout, so we have to print
//a sacrificial blank page, but we make it a feature instead of a bug by printing a
//table of contents on the sacrificial page.
if (firstTime) {
firstTime = false;
printBannerPage(printer, painter, pageLayout, doc, docObjs);
}
printer->newPage();
QRectF sourceRect(0.0, Rez::guiX(-height), Rez::guiX(width), Rez::guiX(height));
QRect targetRect = printer->pageLayout().fullRectPixels(printer->resolution());
renderPage(vpp, painter, sourceRect, targetRect);
}
painter.end();
}
//static routine to print all pages in a document to pdf
void MDIViewPage::printAllPdf(QPrinter* printer,
App::Document* doc)
{
// Base::Console().Message("MDIVP::printAllPdf()\n");
QString outputFile = printer->outputFileName();
QString documentName = QString::fromUtf8(doc->getName());
QPdfWriter pdfWriter(outputFile);
pdfWriter.setTitle(documentName);
pdfWriter.setResolution(printer->resolution());
QPainter painter(&pdfWriter);
QPageLayout pageLayout = printer->pageLayout();
double dpmm = printer->resolution() / 25.4;
bool firstTime = true;
std::vector<App::DocumentObject*> docObjs = doc->getObjectsOfType(TechDraw::DrawPage::getClassTypeId());
for (auto& obj: docObjs) {
Gui::ViewProvider* vp = Gui::Application::Instance->getViewProvider(obj);
if (!vp) {
continue; // can't print this one
}
TechDrawGui::ViewProviderPage* vpp = dynamic_cast<TechDrawGui::ViewProviderPage*>(vp);
if (!vpp) {
continue; // can't print this one
}
TechDraw::DrawPage* dp = static_cast<TechDraw::DrawPage*>(obj);
double width = 297.0; //default to A4 Landscape 297 x 210
double height = 210.0;
setPageLayout(pageLayout, dp, width, height);
pdfWriter.setPageLayout(pageLayout);
//for some reason the first page doesn't obey the pageLayout, so we have to print
//a sacrificial blank page, but we make it a feature instead of a bug by printing a
//table of contents on the sacrificial page.
if (firstTime) {
firstTime = false;
printBannerPage(printer, painter, pageLayout, doc, docObjs);
}
pdfWriter.newPage();
QRectF sourceRect(0.0, Rez::guiX(-height), Rez::guiX(width), Rez::guiX(height));
QRect targetRect(0, 0, width * dpmm, height * dpmm);
renderPage(vpp, painter, sourceRect, targetRect);
}
painter.end();
}
//static
void MDIViewPage::printBannerPage(QPrinter* printer, QPainter& painter,
QPageLayout& pageLayout,
App::Document* doc,
std::vector<App::DocumentObject*>& docObjs)
{
QFont savePainterFont = painter.font();
QFont painterFont;
painterFont.setFamily(Preferences::labelFontQString());
int fontSizeMM = Preferences::labelFontSizeMM();
double dpmm = printer->resolution() / 25.4;
int fontSizePx = fontSizeMM * dpmm;
painterFont.setPixelSize(fontSizePx);
painter.setFont(painterFont);
//print a header
QString docLine = QObject::tr("Document Name: ") + QString::fromUtf8(doc->getName());
int leftMargin = pageLayout.margins().left() * dpmm + 5 * dpmm; //layout margin + 5mm
int verticalPos = pageLayout.margins().top() * dpmm + 20 * dpmm; //layout margin + 20mm
int verticalSpacing = 2; //double space
painter.drawText(leftMargin, verticalPos, docLine);
//leave some blank space between document name and page entries
verticalPos += 2 * verticalSpacing * fontSizePx;
for (auto& obj : docObjs) {
//print a line for each page
QString pageLine = QString::fromUtf8(obj->getNameInDocument()) +
QString::fromUtf8(" / ") +
QString::fromUtf8(obj->Label.getValue());
painter.drawText(leftMargin, verticalPos, pageLine);
verticalPos += verticalSpacing * fontSizePx;
}
painter.setFont(savePainterFont); //restore the original font
}
//static
void MDIViewPage::renderPage(ViewProviderPage* vpp,
QPainter& painter,
QRectF& sourceRect,
QRect& targetRect)
{
bool saveState = vpp->getFrameState();
//turn off view frames for print
vpp->setFrameState(false);
vpp->setTemplateMarkers(false);
vpp->getQGSPage()->refreshViews();
vpp->getQGSPage()->render(&painter, targetRect, sourceRect);
// Reset
vpp->setFrameState(saveState);
vpp->setTemplateMarkers(saveState);
vpp->getQGSPage()->refreshViews();
}
//static
void MDIViewPage::setPageLayout(QPageLayout& pageLayout,
TechDraw::DrawPage* dPage,
double& width, double& height)
{
auto pageTemplate( dynamic_cast<TechDraw::DrawTemplate *>(dPage->Template.getValue()) );
if( pageTemplate ) {
width = pageTemplate->Width.getValue();
height = pageTemplate->Height.getValue();
}
//Qt's page size determination assumes Portrait orientation. To get the right paper size
//we need to ask in the proper form.
QPageSize::PageSizeId paperSizeID =
QPageSize::id(QSizeF(std::min(width, height), std::max(width, height)),
QPageSize::Millimeter,
QPageSize::FuzzyOrientationMatch);
if (paperSizeID == QPageSize::Custom) {
pageLayout.setPageSize(QPageSize(QSizeF(std::min(width, height), std::max(width, height)),
QPageSize::Millimeter));
} else {
pageLayout.setPageSize(QPageSize(paperSizeID));
}
pageLayout.setOrientation((QPageLayout::Orientation) dPage->getOrientation());
}
PyObject* MDIViewPage::getPyObject()
{
if (!pythonObject)
pythonObject = new MDIViewPagePy(this);
Py_INCREF(pythonObject);
return pythonObject;
}
void MDIViewPage::contextMenuEvent(QContextMenuEvent *event)
{
// Base::Console().Message("MDIVP::contextMenuEvent() - reason: %d\n", event->reason());
QMenu menu;
menu.addAction(m_toggleFrameAction);
menu.addAction(m_toggleKeepUpdatedAction);
menu.addAction(m_exportSVGAction);
menu.addAction(m_exportDXFAction);
menu.addAction(m_exportPDFAction);
menu.addAction(m_printAllAction);
menu.exec(event->globalPos());
}
void MDIViewPage::toggleFrame()
{
m_vpPage->toggleFrameState();
}
void MDIViewPage::toggleKeepUpdated()
{
bool state = m_vpPage->getDrawPage()->KeepUpdated.getValue();
m_vpPage->getDrawPage()->KeepUpdated.setValue(!state);
}
void MDIViewPage::viewAll()
{
m_vpPage->getQGVPage()->fitInView(m_scene->itemsBoundingRect(), Qt::KeepAspectRatio);
}
void MDIViewPage::saveSVG()
{
QStringList filter;
filter << QObject::tr("SVG (*.svg)");
filter << QObject::tr("All Files (*.*)");
QString fn = Gui::FileDialog::getSaveFileName(Gui::getMainWindow(), QObject::tr("Export page as SVG"),
QString(), filter.join(QLatin1String(";;")));
if (fn.isEmpty()) {
return;
}
static_cast<void> (blockSelection(true)); // avoid to be notified by itself
m_scene->saveSvg(fn);
}
void MDIViewPage::saveSVG(std::string file)
{
if (file.empty()) {
Base::Console().Warning("MDIViewPage - no file specified\n");
return;
}
QString filename = QString::fromUtf8(file.data(), file.size());
m_scene->saveSvg(filename);
}
void MDIViewPage::saveDXF()
{
QString defaultDir;
QString fileName = Gui::FileDialog::getSaveFileName(Gui::getMainWindow(),
QString::fromUtf8(QT_TR_NOOP("Save DXF file")),
defaultDir,
QString::fromUtf8(QT_TR_NOOP("DXF (*.dxf)")));
if (fileName.isEmpty()) {
return;
}
std::string sFileName = fileName.toUtf8().constData();
saveDXF(sFileName);
}
void MDIViewPage::saveDXF(std::string fileName)
{
TechDraw::DrawPage* page = m_vpPage->getDrawPage();
std::string PageName = page->getNameInDocument();
fileName = Base::Tools::escapeEncodeFilename(fileName);
Gui::Command::openCommand(QT_TRANSLATE_NOOP("Command", "Save page to dxf"));
Gui::Command::doCommand(Gui::Command::Doc, "import TechDraw");
Gui::Command::doCommand(Gui::Command::Doc, "TechDraw.writeDXFPage(App.activeDocument().%s, u\"%s\")",
PageName.c_str(), (const char*)fileName.c_str());
Gui::Command::commitCommand();
}
void MDIViewPage::savePDF()
{
printPdf();
}
void MDIViewPage::savePDF(std::string file)
{
printPdf(file);
}
//mdiviewpage method for printAll action
void MDIViewPage::printAll()
{
// Base::Console().Message("MDIVP::printAll()\n");
printAllPages();
}
//static routine for PrintAll command
void MDIViewPage::printAllPages()
{
QPrinter printer(QPrinter::HighResolution);
printer.setFullPage(true);
QPrintDialog dlg(&printer, Gui::getMainWindow());
if (dlg.exec() == QDialog::Accepted) {
App::Document* doc = App::GetApplication().getActiveDocument();
if (!doc) {
return;
}
if (printer.outputFileName().isEmpty()) {
printAll(&printer, doc);
} else {
printAllPdf(&printer, doc);
}
}
}
/////////////// Selection Routines ///////////////////
// wf: this is never executed???
// needs a signal from Scene? hoverEvent? Scene does not emit signal for "preselect"
// there is no "preSelect" signal from Gui either.
void MDIViewPage::preSelectionChanged(const QPoint &pos)
{
QObject *obj = QObject::sender();
if(!obj)
return;
auto view( dynamic_cast<QGIView *>(obj) );
if(!view)
return;
QGraphicsItem* parent = view->parentItem();
if(!parent)
return;
TechDraw::DrawView *viewObj = view->getViewObject();
std::stringstream ss;
QGIFace *face = dynamic_cast<QGIFace *>(obj);
QGIEdge *edge = dynamic_cast<QGIEdge *>(obj);
QGIVertex *vert = dynamic_cast<QGIVertex *>(obj);
if(edge) {
ss << "Edge" << edge->getProjIndex();
//bool accepted =
static_cast<void> (Gui::Selection().setPreselect(viewObj->getDocument()->getName()
,viewObj->getNameInDocument()
,ss.str().c_str()
,pos.x()
,pos.y()
,0));
} else if(vert) {
ss << "Vertex" << vert->getProjIndex();
//bool accepted =
static_cast<void> (Gui::Selection().setPreselect(viewObj->getDocument()->getName()
,viewObj->getNameInDocument()
,ss.str().c_str()
,pos.x()
,pos.y()
,0));
} else if(face) {
ss << "Face" << face->getProjIndex(); //TODO: SectionFaces have ProjIndex = -1. (but aren't selectable?) Problem?
//bool accepted =
static_cast<void> (Gui::Selection().setPreselect(viewObj->getDocument()->getName()
,viewObj->getNameInDocument()
,ss.str().c_str()
,pos.x()
,pos.y()
,0));
} else {
ss << "";
Gui::Selection().setPreselect(viewObj->getDocument()->getName()
,viewObj->getNameInDocument()
,ss.str().c_str()
,pos.x()
,pos.y()
,0);
}
}
//flag to prevent selection activity within mdivp
void MDIViewPage::blockSceneSelection(const bool isBlocked)
{
isSelectionBlocked = isBlocked;
}
//Set all QGIViews to unselected state
void MDIViewPage::clearSceneSelection()
{
// Base::Console().Message("MDIVP::clearSceneSelection()\n");
blockSceneSelection(true);
m_qgSceneSelected.clear();
std::vector<QGIView *> views = m_scene->getViews();
// Iterate through all views and unselect all
for (std::vector<QGIView *>::iterator it = views.begin(); it != views.end(); ++it) {
QGIView *item = *it;
bool state = item->isSelected();
//handle oddballs
QGIViewDimension* dim = dynamic_cast<QGIViewDimension*>(*it);
if (dim) {
state = dim->getDatumLabel()->isSelected();
} else {
QGIViewBalloon* bal = dynamic_cast<QGIViewBalloon*>(*it);
if (bal) {
state = bal->getBalloonLabel()->isSelected();
}
}
if (state) {
item->setGroupSelection(false);
item->updateView();
}
}
blockSceneSelection(false);
}
//!Update QGIView's selection state based on Selection made outside Drawing Interface
void MDIViewPage::selectQGIView(App::DocumentObject *obj, const bool isSelected)
{
QGIView *view = m_scene->findQViewForDocObj(obj);
blockSceneSelection(true);
if(view) {
view->setGroupSelection(isSelected);
view->updateView();
}
blockSceneSelection(false);
}
//! invoked by selection change made in Tree via father MDIView
//really "onTreeSelectionChanged"
void MDIViewPage::onSelectionChanged(const Gui::SelectionChanges& msg)
{
// Base::Console().Message("MDIVP::onSelectionChanged()\n");
std::vector<Gui::SelectionSingleton::SelObj> selObjs = Gui::Selection().getSelection(msg.pDocName);
if (msg.Type == Gui::SelectionChanges::ClrSelection) {
clearSceneSelection();
} else if(msg.Type == Gui::SelectionChanges::SetSelection) { //replace entire selection set
clearSceneSelection();
blockSceneSelection(true);
for (auto& so: selObjs){
if (so.pObject->isDerivedFrom(TechDraw::DrawView::getClassTypeId())) {
selectQGIView(so.pObject, true);
}
}
blockSceneSelection(false);
} else if(msg.Type == Gui::SelectionChanges::AddSelection) {
blockSceneSelection(true);
for (auto& so: selObjs){
if (so.pObject->isDerivedFrom(TechDraw::DrawView::getClassTypeId())) {
selectQGIView(so.pObject, true);
}
}
blockSceneSelection(false);
} else {
Base::Console().Log("MDIVP::onSelectionChanged - unhandled: %d\n", msg.Type);
}
}
//! maintain QGScene selected items in selection order
void MDIViewPage::sceneSelectionManager()
{
// Base::Console().Message("MDIVP::sceneSelectionManager()\n");
QList<QGraphicsItem*> sceneSel = m_scene->selectedItems();
if (sceneSel.isEmpty()) {
m_qgSceneSelected.clear(); //TODO: need to signal somebody? Tree? handled elsewhere
//clearSelection
return;
}
if (m_qgSceneSelected.isEmpty() &&
!sceneSel.isEmpty()) {
m_qgSceneSelected.push_back(sceneSel.front());
return;
}
//add to m_qgSceneSelected anything that is in q_sceneSel
for (auto qts: sceneSel) {
bool found = false;
for (auto ms: qAsConst(m_qgSceneSelected)) {
if ( qts == ms ) {
found = true;
break;
}
}
if (!found) {
m_qgSceneSelected.push_back(qts);
break;
}
}
//remove items from m_qgSceneSelected that are not in q_sceneSel
QList<QGraphicsItem*> m_new;
for (auto m: qAsConst(m_qgSceneSelected)) {
for (auto q: sceneSel) {
if (m == q) {
m_new.push_back(m);
break;
}
}
}
m_qgSceneSelected = m_new;
}
//! update Tree Selection from QGraphicsScene selection
//triggered by m_scene signal
void MDIViewPage::sceneSelectionChanged()
{
sceneSelectionManager();
if(isSelectionBlocked) {
return;
}
std::vector<Gui::SelectionObject> treeSel = Gui::Selection().getSelectionEx();
QList<QGraphicsItem*> sceneSel = m_qgSceneSelected;
//check if really need to change selection
bool sameSel = compareSelections(treeSel, sceneSel);
if (sameSel) {
return;
}
setTreeToSceneSelect();
}
//Note: Qt says: "no guarantee of selection order"!!!
void MDIViewPage::setTreeToSceneSelect()
{
// Base::Console().Message("MDIVP::setTreeToSceneSelect()\n");
bool saveBlock = blockSelection(true); // block selectionChanged signal from Tree/Observer
blockSceneSelection(true);
Gui::Selection().clearSelection();
QList<QGraphicsItem*> sceneSel = m_qgSceneSelected;
for (QList<QGraphicsItem*>::iterator it = sceneSel.begin(); it != sceneSel.end(); ++it) {
QGIView *itemView = dynamic_cast<QGIView *>(*it);
if (!itemView) {
QGIEdge *edge = dynamic_cast<QGIEdge *>(*it);
if(edge) {
QGraphicsItem*parent = edge->parentItem();
if(!parent)
continue;
QGIView *viewItem = dynamic_cast<QGIView *>(parent);
if(!viewItem)
continue;
TechDraw::DrawView *viewObj = viewItem->getViewObject();
std::stringstream ss;
ss << "Edge" << edge->getProjIndex();
//bool accepted =
static_cast<void> (Gui::Selection().addSelection(viewObj->getDocument()->getName(),
viewObj->getNameInDocument(),
ss.str().c_str()));
showStatusMsg(viewObj->getDocument()->getName(),
viewObj->getNameInDocument(),
ss.str().c_str());
continue;
}
QGIVertex *vert = dynamic_cast<QGIVertex *>(*it);
if(vert) {
QGraphicsItem*parent = vert->parentItem();
if(!parent)
continue;
QGIView *viewItem = dynamic_cast<QGIView *>(parent);
if(!viewItem)
continue;
TechDraw::DrawView *viewObj = viewItem->getViewObject();
std::stringstream ss;
ss << "Vertex" << vert->getProjIndex();
//bool accepted =
static_cast<void> (Gui::Selection().addSelection(viewObj->getDocument()->getName(),
viewObj->getNameInDocument(),
ss.str().c_str()));
showStatusMsg(viewObj->getDocument()->getName(),
viewObj->getNameInDocument(),