1 /*
2 SPDX-License-Identifier: GPL-2.0-or-later
3 SPDX-FileCopyrightText: 2006-2020 Umbrello UML Modeller Authors <umbrello-devel@kde.org>
4 */
5
6 // own header
7 #include "umlviewimageexporterall.h"
8
9 // application specific includes
10 #include "diagramprintpage.h"
11 #include "exportallviewsdialog.h"
12 #include "umlviewimageexportermodel.h"
13 #include "uml.h"
14 #include "umldoc.h"
15
16 // kde include files
17 #include <KComboBox>
18 #include <KLocalizedString>
19 #if QT_VERSION < 0x050000
20 #include <kfilefiltercombo.h>
21 #include <kurl.h>
22 #endif
23 #include <kurlrequester.h>
24 #include <KMessageBox>
25
26 // Qt include files
27 #include <QString>
28 #include <QStringList>
29 #if QT_VERSION >= 0x050000
30 #include <QUrl>
31 #endif
32
33 /**
34 * Constructor for UMLViewImageExporterAll
35 */
UMLViewImageExporterAll()36 UMLViewImageExporterAll::UMLViewImageExporterAll()
37 {
38 m_dialog = new ExportAllViewsDialog(0, "exportAllViewsDialog");
39 }
40
41 /**
42 * Destructor for UMLViewImageExporterAll
43 */
~UMLViewImageExporterAll()44 UMLViewImageExporterAll::~UMLViewImageExporterAll()
45 {
46 delete m_dialog;
47 }
48
49 /**
50 * Export views selected by the DiagramPrintPage instance.
51 *
52 * See @ref exportViews(const UMLViewList &views) for more details.
53 *
54 * @param selectPage instance of DiagramPrintPage
55 */
exportViews(DiagramPrintPage * selectPage)56 void UMLViewImageExporterAll::exportViews(DiagramPrintPage *selectPage)
57 {
58 UMLViewList views;
59 int count = selectPage->printUmlCount();
60 for (int i = 0; i < count; ++i) {
61 QString sID = selectPage->printUmlDiagram(i);
62 Uml::ID::Type id = Uml::ID::fromString(sID);
63 UMLView *v = UMLApp::app()->document()->findView(id);
64 if (v)
65 views.append(v);
66 }
67 exportViews(views);
68 }
69
70 /**
71 * Shows a dialog to the user to get the needed parameters and then exports
72 * the views.
73 * The dialog remembers values between calls (in the same application instance,
74 * although it's not persistent between Umbrello executions).
75 *
76 * Once the export begins, it can't be stopped until it ends itself. The status
77 * bar shows an information message until the export finishes.
78 *
79 * If something went wrong while exporting, an error dialog is shown to the
80 * user with the error messages explaining the problems occurred.
81 */
exportViews(const UMLViewList & views)82 void UMLViewImageExporterAll::exportViews(const UMLViewList &views)
83 {
84 UMLApp* app = UMLApp::app();
85 UMLDoc* umlDoc = app->document();
86
87 // default url can't be set when creating the action because the
88 // document wasn't loaded
89 if (m_dialog->m_kURL->url().isEmpty()) {
90 #if QT_VERSION >= 0x050000
91 QUrl directory(umlDoc->url());
92 m_dialog->m_kURL->setUrl(directory.adjusted(QUrl::RemoveFilename));
93 #else
94 m_dialog->m_kURL->setUrl(umlDoc->url().directory());
95 #endif
96 }
97
98 if (m_dialog->exec() == QDialog::Rejected) {
99 return;
100 }
101
102 app->setImageMimeType(m_dialog->m_imageType->currentType());
103 float resolution = m_dialog->m_imageResolution->currentResolution();
104 // export all views
105 umlDoc->writeToStatusBar(i18n("Exporting all views..."));
106 QStringList errors = UMLViewImageExporterModel(resolution).exportViews(views,
107 #if QT_VERSION >= 0x050000
108 UMLViewImageExporterModel::mimeTypeToImageType(m_dialog->m_imageType->currentType()),
109 QUrl(m_dialog->m_kURL->url()),
110 #else
111 UMLViewImageExporterModel::mimeTypeToImageType(m_dialog->m_imageType->currentType()),
112 KUrl(m_dialog->m_kURL->url()),
113 #endif
114 m_dialog->m_useFolders->isChecked());
115 if (!errors.empty()) {
116 KMessageBox::errorList(app, i18n("Some errors happened when exporting the images:"), errors);
117 }
118 umlDoc->writeToStatusBar(i18nc("reset status bar", "Ready."));
119 }
120