File indexing completed on 2024-04-28 16:24:36

0001 /* This file is part of the KDE project
0002    Copyright (C) 2012 C. Boemann <cbo@kogmbh.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 // clazy:excludeall=qstring-arg
0021 #include "kptpart.h"
0022 
0023 #include "config/ConfigDialog.h"
0024 #include "kptview.h"
0025 #include "kptmaindocument.h"
0026 #include "kptfactory.h"
0027 #include "welcome/WelcomeView.h"
0028 #include "Help.h"
0029 #include "calligraplansettings.h"
0030 #include "kptdebug.h"
0031 
0032 #include <KoComponentData.h>
0033 
0034 #include <KRecentFilesAction>
0035 #include <KXMLGUIFactory>
0036 #include <KConfigGroup>
0037 #include <KHelpClient>
0038 #include <KRun>
0039 #include <KDesktopFile>
0040 #include <KAboutData>
0041 
0042 #include <QStackedWidget>
0043 #include <QDesktopServices>
0044 
0045 using namespace KPlato;
0046 
0047 Part::Part(QObject *parent)
0048     : KoPart(Factory::global(), parent)
0049     , startUpWidget(0)
0050 {
0051     setTemplatesResourcePath(QLatin1String("calligraplan/templates/"));
0052 
0053     new Help(KPlatoSettings::contextPath(), KPlatoSettings::contextLanguage());
0054 }
0055 
0056 Part::~Part()
0057 {
0058 }
0059 
0060 void Part::setDocument(KPlato::MainDocument *document)
0061 {
0062     KoPart::setDocument(document);
0063     m_document = document;
0064 }
0065 
0066 KoView *Part::createViewInstance(KoDocument *document, QWidget *parent)
0067 {
0068     // synchronize view selector
0069     View *view = dynamic_cast<View*>(views().value(0));
0070     /*FIXME
0071     if (view && m_context) {
0072         QDomDocument doc = m_context->save(view);
0073         m_context->setContent(doc.toString());
0074     }*/
0075     view = new View(this, qobject_cast<MainDocument*>(document), parent);
0076 //    connect(view, SIGNAL(destroyed()), this, SLOT(slotViewDestroyed()));
0077 //    connect(document, SIGNAL(viewListItemAdded(const ViewListItem*,const ViewListItem*,int)), view, SLOT(addViewListItem(const ViewListItem*,const ViewListItem*,int)));
0078 //    connect(document, SIGNAL(viewListItemRemoved(const ViewListItem*)), view, SLOT(removeViewListItem(const ViewListItem*)));
0079     return view;
0080 }
0081 
0082 KoMainWindow *Part::createMainWindow()
0083 {
0084     KoMainWindow *w = new KoMainWindow(PLAN_MIME_TYPE, componentData());
0085     QAction *handbookAction = w->action("help_contents");
0086     if (handbookAction) {
0087         // we do not want to use khelpcenter as we do not install docs
0088         disconnect(handbookAction, 0, 0, 0);
0089         connect(handbookAction, &QAction::triggered, this, &Part::slotHelpContents);
0090     }
0091     return w;
0092 }
0093 
0094 void Part::slotHelpContents()
0095 {
0096     Help::invoke(QUrl::fromUserInput(KPlatoSettings::documentationPath()));
0097 }
0098 
0099 void Part::showStartUpWidget(KoMainWindow *parent)
0100 {
0101     m_toolbarVisible = parent->factory()->container("mainToolBar", parent)->isVisible();
0102     if (m_toolbarVisible) {
0103         parent->factory()->container("mainToolBar", parent)->hide();
0104     }
0105 
0106     if (startUpWidget) {
0107         startUpWidget->show();
0108     } else {
0109         createStarUpWidget(parent);
0110         parent->setCentralWidget(startUpWidget);
0111     }
0112 
0113     parent->setPartToOpen(this);
0114 
0115 }
0116 
0117 void Part::slotOpenTemplate(const QUrl &url)
0118 {
0119     openTemplate(url);
0120 }
0121 
0122 void Part::openTemplate(const QUrl &url)
0123 {
0124     debugPlan<<"Open shared resources template:"<<url;
0125     m_document->setLoadingTemplate(true);
0126     m_document->setLoadingSharedResourcesTemplate(url.fileName() == "SharedResources.plant");
0127     KoPart::openTemplate(url);
0128     m_document->setLoadingTemplate(false);
0129 }
0130 
0131 bool Part::openProjectTemplate(const QUrl &url)
0132 {
0133     QApplication::setOverrideCursor(Qt::BusyCursor);
0134     m_document->setLoadingTemplate(true);
0135     bool ok = m_document->loadNativeFormat(url.path());
0136     m_document->setModified(false);
0137     m_document->undoStack()->clear();
0138 
0139     if (ok) {
0140         m_document->resetURL();
0141         m_document->setEmpty();
0142     } else {
0143         m_document->showLoadingErrorDialog();
0144         m_document->initEmpty();
0145     }
0146     m_document->setLoadingTemplate(false);
0147     QApplication::restoreOverrideCursor();
0148     return ok;
0149 }
0150 
0151 void Part::openTaskModule(const QUrl &url)
0152 {
0153     Part *part = new Part(0);
0154     MainDocument *doc = new MainDocument(part);
0155     part->setDocument(doc);
0156     doc->setIsTaskModule(true);
0157     mainWindows().first()->openDocument(part, url);
0158 }
0159 
0160 void Part::createStarUpWidget(KoMainWindow *parent)
0161 {
0162     startUpWidget = new QStackedWidget(parent);
0163 
0164     startUpWidget->addWidget(createWelcomeView(parent));
0165 }
0166 
0167 void Part::finish()
0168 {
0169     mainWindows().first()->setRootDocument(document(), this);
0170     if (m_toolbarVisible) {
0171         KoPart::mainWindows().first()->factory()->container("mainToolBar", mainWindows().first())->show();
0172     }
0173 }
0174 
0175 QWidget *Part::createWelcomeView(KoMainWindow *mw)
0176 {
0177     MainDocument *doc = static_cast<MainDocument*>(document());
0178 
0179     WelcomeView *v = new WelcomeView(this, doc, startUpWidget);
0180     v->setProject(&(doc->getProject()));
0181 
0182     KSharedConfigPtr configPtr = Factory::global().config();
0183     KRecentFilesAction recent("x", 0);
0184     recent.loadEntries(configPtr->group("RecentFiles"));
0185     v->setRecentFiles(recent.actions());
0186 
0187     connect(v, &WelcomeView::loadSharedResources, doc, &MainDocument::insertResourcesFile);
0188     connect(v, &WelcomeView::recentProject, mw, &KoMainWindow::slotFileOpenRecent);
0189     connect(v, &WelcomeView::projectCreated, doc, &MainDocument::slotProjectCreated);
0190     connect(v, &WelcomeView::finished, this, &Part::finish);
0191 
0192     connect(v, &WelcomeView::openTemplate, this, &Part::slotOpenTemplate);
0193 
0194     return v;
0195 }
0196 
0197 void Part::configure(KoMainWindow *mw)
0198 {
0199     //debugPlan;
0200     if(KConfigDialog::showDialog("Plan Settings")) {
0201         return;
0202     }
0203     ConfigDialog *dialog = new ConfigDialog(mw, "Plan Settings", KPlatoSettings::self());
0204     connect(dialog, &ConfigDialog::settingsUpdated, this, &Part::slotSettingsUpdated, Qt::QueuedConnection);
0205     dialog->open();
0206 }
0207 
0208 void Part::slotSettingsUpdated()
0209 {
0210     new Help(KPlatoSettings::contextPath(), KPlatoSettings::contextLanguage());
0211     if (startUpWidget) {
0212         static_cast<WelcomeView*>(startUpWidget->widget(0))->setProjectTemplatesModel();
0213     }
0214 }