1
/* This file is part of the KDE project
2
* Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
3
* Copyright (C) 2000-2005 David Faure <faure@kde.org>
4
* Copyright (C) 2007-2008 Thorsten Zachmann <zachmann@kde.org>
5
* Copyright (C) 2010-2012 Boudewijn Rempt <boud@kogmbh.com>
6
* Copyright (C) 2011 Inge Wallin <ingwa@kogmbh.com>
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Library General Public
10
* License as published by the Free Software Foundation; either
11
* version 2 of the License, or (at your option) any later version.
13
* This library is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Library General Public License for more details.
18
* You should have received a copy of the GNU Library General Public License
19
* along with this library; see the file COPYING.LIB. If not, write to
20
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21
* Boston, MA 02110-1301, USA.
26
#include "KoApplication.h"
27
#include "KoMainWindow.h"
28
#include "KoDocument.h"
30
#include "KoFilterManager.h"
31
#include <KoComponentData.h>
33
//#include <KoCanvasController.h>
34
//#include <KoCanvasControllerWidget.h>
35
#include <KoResourcePaths.h>
37
#include <MainDebug.h>
38
#include <kxmlguifactory.h>
39
#include <kdesktopfile.h>
40
#include <kconfiggroup.h>
41
#include <ksharedconfig.h>
44
#include <QGraphicsScene>
45
#include <QGraphicsProxyWidget>
46
#include <QMimeDatabase>
49
#include <QDBusConnection>
50
#include "KoPartAdaptor.h"
53
class Q_DECL_HIDDEN KoPart::Private
56
Private(const KoComponentData &componentData_, KoPart *_parent)
59
, componentData(componentData_)
65
/// FIXME ok, so this is obviously bad to leave like this
66
// For now, this is undeleted, but only to avoid an odd double
67
// delete condition. Until that's discovered, we'll need this
68
// to avoid crashes in Gemini
75
QList<KoMainWindow*> mainWindows;
77
QList<KoDocument*> documents;
78
QString templatesResourcePath;
80
KoComponentData componentData;
84
KoPart::KoPart(const KoComponentData &componentData, QObject *parent)
86
, d(new Private(componentData, this))
89
new KoPartAdaptor(this);
90
QDBusConnection::sessionBus().registerObject('/' + objectName(), this);
96
// Tell our views that the document is already destroyed and
97
// that they shouldn't try to access it.
98
foreach(KoView *view, views()) {
99
view->setDocumentDeleted();
102
while (!d->mainWindows.isEmpty()) {
103
delete d->mainWindows.takeFirst();
109
KoComponentData KoPart::componentData() const
111
return d->componentData;
114
void KoPart::setDocument(KoDocument *document)
117
d->document = document;
120
KoDocument *KoPart::document() const
125
KoView *KoPart::createView(KoDocument *document, QWidget *parent)
127
KoView *view = createViewInstance(document, parent);
128
addView(view, document);
129
if (!d->documents.contains(document)) {
130
d->documents.append(document);
135
void KoPart::addView(KoView *view, KoDocument *document)
140
if (!d->views.contains(view)) {
141
d->views.append(view);
143
if (!d->documents.contains(document)) {
144
d->documents.append(document);
147
view->updateReadWrite(document->isReadWrite());
149
if (d->views.size() == 1) {
150
KoApplication *app = qobject_cast<KoApplication*>(qApp);
152
emit app->documentOpened('/'+objectName());
157
void KoPart::removeView(KoView *view)
159
d->views.removeAll(view);
161
if (d->views.isEmpty()) {
162
KoApplication *app = qobject_cast<KoApplication*>(qApp);
164
emit app->documentClosed('/'+objectName());
169
QList<KoView*> KoPart::views() const
174
int KoPart::viewCount() const
176
return d->views.count();
179
QGraphicsItem *KoPart::createCanvasItem(KoDocument *document)
181
/* KoView *view = createView(document);
182
QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
183
QWidget *canvasController = view->findChild<KoCanvasControllerWidget*>();
184
proxy->setWidget(canvasController);
189
void KoPart::addMainWindow(KoMainWindow *mainWindow)
191
if (d->mainWindows.indexOf(mainWindow) == -1) {
192
debugMain <<"mainWindow" << (void*)mainWindow <<"added to doc" << this;
193
d->mainWindows.append(mainWindow);
197
void KoPart::removeMainWindow(KoMainWindow *mainWindow)
199
debugMain <<"mainWindow" << (void*)mainWindow <<"removed from doc" << this;
201
d->mainWindows.removeAll(mainWindow);
205
const QList<KoMainWindow*>& KoPart::mainWindows() const
207
return d->mainWindows;
210
int KoPart::mainwindowCount() const
212
return d->mainWindows.count();
216
KoMainWindow *KoPart::currentMainwindow() const
218
QWidget *widget = qApp->activeWindow();
219
KoMainWindow *mainWindow = qobject_cast<KoMainWindow*>(widget);
220
while (!mainWindow && widget) {
221
widget = widget->parentWidget();
222
mainWindow = qobject_cast<KoMainWindow*>(widget);
225
if (!mainWindow && mainWindows().size() > 0) {
226
mainWindow = mainWindows().first();
232
void KoPart::openExistingFile(const QUrl &url)
234
QApplication::setOverrideCursor(Qt::BusyCursor);
235
d->document->openUrl(url);
236
d->document->setModified(false);
237
QApplication::restoreOverrideCursor();
240
void KoPart::openTemplate(const QUrl &url)
242
QApplication::setOverrideCursor(Qt::BusyCursor);
243
bool ok = d->document->loadNativeFormat(url.toLocalFile());
244
d->document->setModified(false);
245
d->document->undoStack()->clear();
248
QString mimeType = QMimeDatabase().mimeTypeForUrl(url).name();
249
// in case this is a open document template remove the -template from the end
250
mimeType.remove( QRegExp( "-template$" ) );
251
d->document->setMimeTypeAfterLoading(mimeType);
252
d->document->resetURL();
253
d->document->setEmpty();
255
d->document->showLoadingErrorDialog();
256
d->document->initEmpty();
258
QApplication::restoreOverrideCursor();
261
void KoPart::addRecentURLToAllMainWindows(const QUrl &url)
263
// Add to recent actions list in our mainWindows
264
foreach(KoMainWindow *mainWindow, d->mainWindows) {
265
mainWindow->addRecentURL(url);
270
void KoPart::setTemplatesResourcePath(const QString &templatesResourcePath)
272
Q_ASSERT(!templatesResourcePath.isEmpty());
273
Q_ASSERT(templatesResourcePath.endsWith(QLatin1Char('/')));
275
d->templatesResourcePath = templatesResourcePath;
278
QString KoPart::templatesResourcePath() const
280
return d->templatesResourcePath;