File indexing completed on 2024-05-12 16:37:13

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 #include "KWPart.h"
0021 
0022 #include "KWView.h"
0023 #include "KWDocument.h"
0024 #include "KWCanvasItem.h"
0025 #include "KWFactory.h"
0026 #include "frames/KWFrameSet.h"
0027 #include "frames/KWTextFrameSet.h"
0028 #include "dialogs/KWStartupWidget.h"
0029 
0030 #include <KoCanvasBase.h>
0031 #include <KoSelection.h>
0032 #include <KoToolManager.h>
0033 #include <KoInteractionTool.h>
0034 #include <KoShapeRegistry.h>
0035 #include <KoShapeManager.h>
0036 #include <KoComponentData.h>
0037 
0038 #include <kmessagebox.h>
0039 
0040 KWPart::KWPart(QObject *parent)
0041     : KoPart(KWFactory::componentData(), parent)
0042     , m_document(0)
0043 {
0044     setTemplatesResourcePath(QLatin1String("calligrawords/templates/"));
0045 }
0046 
0047 KWPart::KWPart(const KoComponentData &componentData, QObject *parent)
0048     : KoPart(componentData, parent)
0049     , m_document(0)
0050 {
0051     setTemplatesResourcePath(QLatin1String("calligrawords/templates/"));
0052 }
0053 
0054 KWPart::~KWPart()
0055 {
0056 }
0057 
0058 void KWPart::setDocument(KWDocument *document)
0059 {
0060     KoPart::setDocument(document);
0061     m_document = document;
0062 }
0063 
0064 KWDocument *KWPart::document() const
0065 {
0066     return m_document;
0067 }
0068 
0069 KoView *KWPart::createViewInstance(KoDocument *document, QWidget *parent)
0070 {
0071     KWView *view = new KWView(this, qobject_cast<KWDocument*>(document), parent);
0072     setupViewInstance(document, view);
0073     return view;
0074 }
0075 
0076 void KWPart::setupViewInstance(KoDocument *document, KWView *view)
0077 {
0078     connect(document, SIGNAL(shapeAdded(KoShape*,KoShapeManager::Repaint)), view->canvasBase()->shapeManager(), SLOT(addShape(KoShape*,KoShapeManager::Repaint)));
0079     connect(document, SIGNAL(shapeRemoved(KoShape*)), view->canvasBase()->shapeManager(), SLOT(remove(KoShape*)));
0080     connect(document, SIGNAL(resourceChanged(int,QVariant)), view->canvasBase()->resourceManager(), SLOT(setResource(int,QVariant)));
0081 
0082     bool switchToolCalled = false;
0083     foreach (KWFrameSet *fs, qobject_cast<KWDocument*>(document)->frameSets()) {
0084         if (fs->shapeCount() == 0)
0085             continue;
0086         foreach (KoShape *shape, fs->shapes())
0087             view->canvasBase()->shapeManager()->addShape(shape, KoShapeManager::AddWithoutRepaint);
0088         if (switchToolCalled)
0089             continue;
0090         KWTextFrameSet *tfs = dynamic_cast<KWTextFrameSet*>(fs);
0091         if (tfs && tfs->textFrameSetType() == Words::MainTextFrameSet) {
0092             KoSelection *selection = view->canvasBase()->shapeManager()->selection();
0093             selection->select(fs->shapes().first());
0094 
0095             KoToolManager::instance()->switchToolRequested(
0096                 KoToolManager::instance()->preferredToolForSelection(selection->selectedShapes()));
0097             switchToolCalled = true;
0098         }
0099     }
0100     if (!switchToolCalled)
0101         KoToolManager::instance()->switchToolRequested(KoInteractionTool_ID);
0102 }
0103 
0104 QGraphicsItem *KWPart::createCanvasItem(KoDocument *document)
0105 {
0106     // caller owns the canvas item
0107     KWCanvasItem *item = new KWCanvasItem(QString(), qobject_cast<KWDocument*>(document));
0108     foreach (KWFrameSet *fs, qobject_cast<KWDocument*>(document)->frameSets()) {
0109         if (fs->shapeCount() == 0) {
0110             continue;
0111         }
0112         foreach (KoShape *shape, fs->shapes()) {
0113             item->shapeManager()->addShape(shape, KoShapeManager::AddWithoutRepaint);
0114         }
0115     }
0116     return item;
0117 }
0118 
0119 QList<KoPart::CustomDocumentWidgetItem> KWPart::createCustomDocumentWidgets(QWidget *parent)
0120 {
0121     KoColumns columns;
0122     columns.count = 1;
0123     columns.gapWidth = 20;
0124 
0125     QList<KoPart::CustomDocumentWidgetItem> widgetList;
0126     KoPart::CustomDocumentWidgetItem item;
0127     item.widget = new KWStartupWidget(parent, m_document, columns);
0128     widgetList << item;
0129     return widgetList;
0130 }
0131 
0132 KoMainWindow *KWPart::createMainWindow()
0133 {
0134     return new KoMainWindow(WORDS_MIME_TYPE, componentData());
0135 }
0136 
0137 void KWPart::showStartUpWidget(KoMainWindow *parent, bool alwaysShow)
0138 {
0139     // print error if kotext not available
0140     if (KoShapeRegistry::instance()->value(TextShape_SHAPEID) == 0)
0141         // need to wait 1 event since exiting here would not work.
0142         QTimer::singleShot(0, this, SLOT(showErrorAndDie()));
0143     else
0144         KoPart::showStartUpWidget(parent, alwaysShow);
0145 }
0146 
0147 void KWPart::showErrorAndDie()
0148 {
0149     KMessageBox::error(0,
0150                        i18n("Can not find needed text component, Words will quit now"),
0151                        i18n("Installation Error"));
0152     QCoreApplication::exit(10);
0153 }