1 /* This file is part of the KDE project
2    Copyright (C) 2012 C. Boemann <cbo@kogmbh.com>
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13 
14    You should have received a copy of the GNU Library General Public License
15    along with this library; see the file COPYING.LIB.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18 */
19 
20 #include "KWPart.h"
21 
22 #include "KWView.h"
23 #include "KWDocument.h"
24 #include "KWCanvasItem.h"
25 #include "KWFactory.h"
26 #include "frames/KWFrameSet.h"
27 #include "frames/KWTextFrameSet.h"
28 #include "dialogs/KWStartupWidget.h"
29 
30 #include <KoCanvasBase.h>
31 #include <KoSelection.h>
32 #include <KoToolManager.h>
33 #include <KoInteractionTool.h>
34 #include <KoShapeRegistry.h>
35 #include <KoShapeManager.h>
36 #include <KoComponentData.h>
37 
38 #include <kmessagebox.h>
39 
KWPart(QObject * parent)40 KWPart::KWPart(QObject *parent)
41     : KoPart(KWFactory::componentData(), parent)
42     , m_document(0)
43 {
44     setTemplatesResourcePath(QLatin1String("calligrawords/templates/"));
45 }
46 
KWPart(const KoComponentData & componentData,QObject * parent)47 KWPart::KWPart(const KoComponentData &componentData, QObject *parent)
48     : KoPart(componentData, parent)
49     , m_document(0)
50 {
51     setTemplatesResourcePath(QLatin1String("calligrawords/templates/"));
52 }
53 
~KWPart()54 KWPart::~KWPart()
55 {
56 }
57 
setDocument(KWDocument * document)58 void KWPart::setDocument(KWDocument *document)
59 {
60     KoPart::setDocument(document);
61     m_document = document;
62 }
63 
document() const64 KWDocument *KWPart::document() const
65 {
66     return m_document;
67 }
68 
createViewInstance(KoDocument * document,QWidget * parent)69 KoView *KWPart::createViewInstance(KoDocument *document, QWidget *parent)
70 {
71     KWView *view = new KWView(this, qobject_cast<KWDocument*>(document), parent);
72     setupViewInstance(document, view);
73     return view;
74 }
75 
setupViewInstance(KoDocument * document,KWView * view)76 void KWPart::setupViewInstance(KoDocument *document, KWView *view)
77 {
78     connect(document, SIGNAL(shapeAdded(KoShape*,KoShapeManager::Repaint)), view->canvasBase()->shapeManager(), SLOT(addShape(KoShape*,KoShapeManager::Repaint)));
79     connect(document, SIGNAL(shapeRemoved(KoShape*)), view->canvasBase()->shapeManager(), SLOT(remove(KoShape*)));
80     connect(document, SIGNAL(resourceChanged(int,QVariant)), view->canvasBase()->resourceManager(), SLOT(setResource(int,QVariant)));
81 
82     bool switchToolCalled = false;
83     foreach (KWFrameSet *fs, qobject_cast<KWDocument*>(document)->frameSets()) {
84         if (fs->shapeCount() == 0)
85             continue;
86         foreach (KoShape *shape, fs->shapes())
87             view->canvasBase()->shapeManager()->addShape(shape, KoShapeManager::AddWithoutRepaint);
88         if (switchToolCalled)
89             continue;
90         KWTextFrameSet *tfs = dynamic_cast<KWTextFrameSet*>(fs);
91         if (tfs && tfs->textFrameSetType() == Words::MainTextFrameSet) {
92             KoSelection *selection = view->canvasBase()->shapeManager()->selection();
93             selection->select(fs->shapes().first());
94 
95             KoToolManager::instance()->switchToolRequested(
96                 KoToolManager::instance()->preferredToolForSelection(selection->selectedShapes()));
97             switchToolCalled = true;
98         }
99     }
100     if (!switchToolCalled)
101         KoToolManager::instance()->switchToolRequested(KoInteractionTool_ID);
102 }
103 
createCanvasItem(KoDocument * document)104 QGraphicsItem *KWPart::createCanvasItem(KoDocument *document)
105 {
106     // caller owns the canvas item
107     KWCanvasItem *item = new KWCanvasItem(QString(), qobject_cast<KWDocument*>(document));
108     foreach (KWFrameSet *fs, qobject_cast<KWDocument*>(document)->frameSets()) {
109         if (fs->shapeCount() == 0) {
110             continue;
111         }
112         foreach (KoShape *shape, fs->shapes()) {
113             item->shapeManager()->addShape(shape, KoShapeManager::AddWithoutRepaint);
114         }
115     }
116     return item;
117 }
118 
createCustomDocumentWidgets(QWidget * parent)119 QList<KoPart::CustomDocumentWidgetItem> KWPart::createCustomDocumentWidgets(QWidget *parent)
120 {
121     KoColumns columns;
122     columns.count = 1;
123     columns.gapWidth = 20;
124 
125     QList<KoPart::CustomDocumentWidgetItem> widgetList;
126     KoPart::CustomDocumentWidgetItem item;
127     item.widget = new KWStartupWidget(parent, m_document, columns);
128     widgetList << item;
129     return widgetList;
130 }
131 
createMainWindow()132 KoMainWindow *KWPart::createMainWindow()
133 {
134     return new KoMainWindow(WORDS_MIME_TYPE, componentData());
135 }
136 
showStartUpWidget(KoMainWindow * parent,bool alwaysShow)137 void KWPart::showStartUpWidget(KoMainWindow *parent, bool alwaysShow)
138 {
139     // print error if kotext not available
140     if (KoShapeRegistry::instance()->value(TextShape_SHAPEID) == 0)
141         // need to wait 1 event since exiting here would not work.
142         QTimer::singleShot(0, this, SLOT(showErrorAndDie()));
143     else
144         KoPart::showStartUpWidget(parent, alwaysShow);
145 }
146 
showErrorAndDie()147 void KWPart::showErrorAndDie()
148 {
149     KMessageBox::error(0,
150                        i18n("Can not find needed text component, Words will quit now"),
151                        i18n("Installation Error"));
152     QCoreApplication::exit(10);
153 }
154