File indexing completed on 2024-09-22 05:16:06

0001 /*
0002     This file is part of the Kasten Framework, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2008, 2011 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "exportcontroller.hpp"
0010 
0011 // lib
0012 #include "exportdialog.hpp"
0013 // Kasten gui
0014 #include <Kasten/ModelCodecViewManager>
0015 #include <Kasten/DataSelectable>
0016 #include <Kasten/AbstractModelExporterConfigEditor>
0017 // Kasten core
0018 #include <Kasten/ModelCodecManager>
0019 #include <Kasten/AbstractDocument>
0020 #include <Kasten/AbstractModelExporter>
0021 #include <Kasten/AbstractModelSelection>
0022 // KF
0023 #include <KXMLGUIClient>
0024 #include <KXMLGUIFactory>
0025 #include <KActionCollection>
0026 #include <KLocalizedString>
0027 #include <KSelectAction>
0028 // Qt
0029 #include <QApplication>
0030 
0031 namespace Kasten {
0032 
0033 ExportController::ExportController(ModelCodecViewManager* modelCodecViewManager,
0034                                    ModelCodecManager* modelCodecManager,
0035                                    KXMLGUIClient* guiClient)
0036     : mModelCodecViewManager(modelCodecViewManager)
0037     , mModelCodecManager(modelCodecManager)
0038 {
0039     mExportSelectAction = new KSelectAction(QIcon::fromTheme(QStringLiteral("document-export")),
0040                                             i18nc("@title:menu", "Export"),
0041                                             this);
0042     mExportSelectAction->setToolBarMode(KSelectAction::MenuMode);
0043     connect(mExportSelectAction, qOverload<QAction*>(&KSelectAction::triggered),
0044             this, &ExportController::onActionTriggered);
0045 
0046     guiClient->actionCollection()->addAction(QStringLiteral("export"), mExportSelectAction);
0047 
0048     setTargetModel(nullptr);
0049 }
0050 
0051 void ExportController::setTargetModel(AbstractModel* model)
0052 {
0053     if (mModel) {
0054         mModel->disconnect(this);
0055     }
0056 
0057     mModel = model ? model->findBaseModelWithInterface<If::DataSelectable*>() : nullptr;
0058     mSelectionControl = mModel ? qobject_cast<If::DataSelectable*>(mModel) : nullptr;
0059 
0060     if (mSelectionControl) {
0061         // TODO: only fill the list on menu call...
0062         connect(mModel, SIGNAL(hasSelectedDataChanged(bool)), SLOT(updateActions()));
0063     }
0064 
0065     updateActions();
0066 }
0067 
0068 void ExportController::updateActions()
0069 {
0070     mExportSelectAction->removeAllActions();
0071 
0072     const AbstractModelSelection* selection = mSelectionControl ? mSelectionControl->modelSelection() : nullptr;
0073 
0074     const QVector<AbstractModelExporter*> exporterList =
0075         mModelCodecManager->exporterList(mModel, selection);
0076     const bool hasExporters = (!exporterList.isEmpty());
0077 
0078     if (hasExporters) {
0079         for (auto* exporter : exporterList) {
0080             const QString title = exporter->remoteTypeName();
0081             auto* action = new QAction(title, mExportSelectAction);
0082 
0083             action->setData(QVariant::fromValue(exporter));
0084             mExportSelectAction->addAction(action);
0085         }
0086     } else {
0087         auto* noneAction = new QAction(i18nc("@item There are no exporters.", "Not available."), mExportSelectAction);
0088         noneAction->setEnabled(false);
0089         mExportSelectAction->addAction(noneAction);
0090     }
0091 
0092     mExportSelectAction->setEnabled(mModel != nullptr);
0093 }
0094 
0095 void ExportController::onActionTriggered(QAction* action)
0096 {
0097     auto* exporter = action->data().value<AbstractModelExporter*>();
0098 
0099     const AbstractModelSelection* selection = mSelectionControl ? mSelectionControl->modelSelection() : nullptr;
0100 
0101     AbstractModelExporterConfigEditor* configEditor =
0102         mModelCodecViewManager->createConfigEditor(exporter);
0103 
0104     if (configEditor) {
0105         auto* dialog = new ExportDialog(exporter->remoteTypeName(), configEditor, exporter,
0106                                         QApplication::activeWindow());
0107         dialog->setData(mModel, selection);
0108         connect(dialog, &ExportDialog::exportAccepted, this, &ExportController::triggerExecution);
0109         dialog->open();
0110         return;
0111     }
0112 
0113     triggerExecution(exporter, selection);
0114 }
0115 
0116 void ExportController::triggerExecution(AbstractModelExporter* exporter,
0117                                         const AbstractModelSelection* selection)
0118 {
0119     mModelCodecManager->exportDocument(exporter, mModel, selection);
0120 }
0121 
0122 }
0123 
0124 #include "moc_exportcontroller.cpp"