File indexing completed on 2024-05-19 05:42:26

0001 // ct_lvtqtw_exportmanager.cpp                                         -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #include <ct_lvtqtw_exportmanager.h>
0021 
0022 #include <ct_lvtqtc_graphicsscene.h>
0023 #include <ct_lvtqtc_graphicsview.h>
0024 
0025 #include <QApplication>
0026 #include <QDebug>
0027 #include <QFile>
0028 #include <QFileDialog>
0029 #include <QImage>
0030 #include <QPainter>
0031 #include <QSize>
0032 #include <QSvgGenerator>
0033 #include <Qt> // Qt::White
0034 
0035 #include <cassert>
0036 #include <limits>
0037 #include <string>
0038 
0039 namespace Codethink::lvtqtw {
0040 
0041 struct ExportManager::Private {
0042     lvtqtc::GraphicsView *view;
0043     lvtqtc::GraphicsScene *scene;
0044 
0045     explicit Private(lvtqtc::GraphicsView *view): view(view)
0046     {
0047         if (view) {
0048             scene = qobject_cast<lvtqtc::GraphicsScene *>(view->scene());
0049         }
0050     }
0051 };
0052 
0053 // --------------------------------------------
0054 // class ExportManager
0055 // --------------------------------------------
0056 
0057 ExportManager::ExportManager(lvtqtc::GraphicsView *view): d(std::make_unique<ExportManager::Private>(view))
0058 {
0059 }
0060 
0061 ExportManager::~ExportManager() = default;
0062 
0063 namespace {
0064 QString getFilePath(const QString& caption, const QString& dir, const QString& filter)
0065 {
0066     return QFileDialog::getSaveFileName(QApplication::activeWindow(), caption, dir, filter);
0067 }
0068 } // namespace
0069 
0070 cpp::result<void, ExportError> ExportManager::exportSvg(const QString& path) const
0071 {
0072     assert(d->view && d->scene);
0073 
0074     QString filePath = !path.isEmpty()
0075         ? path
0076         : getFilePath(QObject::tr("Save image as"), QObject::tr("diagram.svg"), QObject::tr("SVG file (*.svg)"));
0077     // not an error, user cancelled.
0078     if (filePath.isEmpty()) {
0079         return {};
0080     }
0081 
0082     QSvgGenerator svgGen;
0083 
0084     const QSize rSize = d->scene->sceneRect().size().toSize();
0085     svgGen.setFileName(filePath);
0086     svgGen.setSize(rSize);
0087     svgGen.setViewBox(QRect(0, 0, rSize.width(), rSize.height()));
0088 
0089     QPainter painter(&svgGen);
0090     d->scene->render(&painter);
0091 
0092     return {};
0093 }
0094 
0095 } // end namespace Codethink::lvtqtw