~ubuntu-branches/debian/sid/calligraplan/sid

« back to all changes in this revision

Viewing changes to src/libs/main/KoPart.cpp

  • Committer: Package Import Robot
  • Author(s): Pino Toscano
  • Date: 2018-02-01 18:20:19 UTC
  • Revision ID: package-import@ubuntu.com-20180201182019-1qo7qaim5wejm5k9
Tags: upstream-3.1.0
ImportĀ upstreamĀ versionĀ 3.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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>
 
7
 *
 
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.
 
12
 *
 
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.
 
17
 *
 
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.
 
22
 */
 
23
 
 
24
#include "KoPart.h"
 
25
 
 
26
#include "KoApplication.h"
 
27
#include "KoMainWindow.h"
 
28
#include "KoDocument.h"
 
29
#include "KoView.h"
 
30
#include "KoFilterManager.h"
 
31
#include <KoComponentData.h>
 
32
 
 
33
//#include <KoCanvasController.h>
 
34
//#include <KoCanvasControllerWidget.h>
 
35
#include <KoResourcePaths.h>
 
36
 
 
37
#include <MainDebug.h>
 
38
#include <kxmlguifactory.h>
 
39
#include <kdesktopfile.h>
 
40
#include <kconfiggroup.h>
 
41
#include <ksharedconfig.h>
 
42
 
 
43
#include <QFileInfo>
 
44
#include <QGraphicsScene>
 
45
#include <QGraphicsProxyWidget>
 
46
#include <QMimeDatabase>
 
47
 
 
48
#ifndef QT_NO_DBUS
 
49
#include <QDBusConnection>
 
50
#include "KoPartAdaptor.h"
 
51
#endif
 
52
 
 
53
class Q_DECL_HIDDEN KoPart::Private
 
54
{
 
55
public:
 
56
    Private(const KoComponentData &componentData_, KoPart *_parent)
 
57
        : parent(_parent)
 
58
        , document(0)
 
59
        , componentData(componentData_)
 
60
    {
 
61
    }
 
62
 
 
63
    ~Private()
 
64
    {
 
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
 
69
        //delete canvasItem;
 
70
    }
 
71
 
 
72
    KoPart *parent;
 
73
 
 
74
    QList<KoView*> views;
 
75
    QList<KoMainWindow*> mainWindows;
 
76
    KoDocument *document;
 
77
    QList<KoDocument*> documents;
 
78
    QString templatesResourcePath;
 
79
 
 
80
    KoComponentData componentData;
 
81
};
 
82
 
 
83
 
 
84
KoPart::KoPart(const KoComponentData &componentData, QObject *parent)
 
85
        : QObject(parent)
 
86
        , d(new Private(componentData, this))
 
87
{
 
88
#ifndef QT_NO_DBUS
 
89
    new KoPartAdaptor(this);
 
90
    QDBusConnection::sessionBus().registerObject('/' + objectName(), this);
 
91
#endif
 
92
}
 
93
 
 
94
KoPart::~KoPart()
 
95
{
 
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();
 
100
    }
 
101
 
 
102
    while (!d->mainWindows.isEmpty()) {
 
103
        delete d->mainWindows.takeFirst();
 
104
    }
 
105
 
 
106
    delete d;
 
107
}
 
108
 
 
109
KoComponentData KoPart::componentData() const
 
110
{
 
111
    return d->componentData;
 
112
}
 
113
 
 
114
void KoPart::setDocument(KoDocument *document)
 
115
{
 
116
    Q_ASSERT(document);
 
117
    d->document = document;
 
118
}
 
119
 
 
120
KoDocument *KoPart::document() const
 
121
{
 
122
    return d->document;
 
123
}
 
124
 
 
125
KoView *KoPart::createView(KoDocument *document, QWidget *parent)
 
126
{
 
127
    KoView *view = createViewInstance(document, parent);
 
128
    addView(view, document);
 
129
    if (!d->documents.contains(document)) {
 
130
        d->documents.append(document);
 
131
    }
 
132
    return view;
 
133
}
 
134
 
 
135
void KoPart::addView(KoView *view, KoDocument *document)
 
136
{
 
137
    if (!view)
 
138
        return;
 
139
 
 
140
    if (!d->views.contains(view)) {
 
141
        d->views.append(view);
 
142
    }
 
143
    if (!d->documents.contains(document)) {
 
144
        d->documents.append(document);
 
145
    }
 
146
 
 
147
    view->updateReadWrite(document->isReadWrite());
 
148
 
 
149
    if (d->views.size() == 1) {
 
150
        KoApplication *app = qobject_cast<KoApplication*>(qApp);
 
151
        if (0 != app) {
 
152
            emit app->documentOpened('/'+objectName());
 
153
        }
 
154
    }
 
155
}
 
156
 
 
157
void KoPart::removeView(KoView *view)
 
158
{
 
159
    d->views.removeAll(view);
 
160
 
 
161
    if (d->views.isEmpty()) {
 
162
        KoApplication *app = qobject_cast<KoApplication*>(qApp);
 
163
        if (0 != app) {
 
164
            emit app->documentClosed('/'+objectName());
 
165
        }
 
166
    }
 
167
}
 
168
 
 
169
QList<KoView*> KoPart::views() const
 
170
{
 
171
    return d->views;
 
172
}
 
173
 
 
174
int KoPart::viewCount() const
 
175
{
 
176
    return d->views.count();
 
177
}
 
178
 
 
179
QGraphicsItem *KoPart::createCanvasItem(KoDocument *document)
 
180
{
 
181
/*    KoView *view = createView(document);
 
182
    QGraphicsProxyWidget *proxy = new QGraphicsProxyWidget();
 
183
    QWidget *canvasController = view->findChild<KoCanvasControllerWidget*>();
 
184
    proxy->setWidget(canvasController);
 
185
    return proxy;*/
 
186
    return 0;
 
187
}
 
188
 
 
189
void KoPart::addMainWindow(KoMainWindow *mainWindow)
 
190
{
 
191
    if (d->mainWindows.indexOf(mainWindow) == -1) {
 
192
        debugMain <<"mainWindow" << (void*)mainWindow <<"added to doc" << this;
 
193
        d->mainWindows.append(mainWindow);
 
194
    }
 
195
}
 
196
 
 
197
void KoPart::removeMainWindow(KoMainWindow *mainWindow)
 
198
{
 
199
    debugMain <<"mainWindow" << (void*)mainWindow <<"removed from doc" << this;
 
200
    if (mainWindow) {
 
201
        d->mainWindows.removeAll(mainWindow);
 
202
    }
 
203
}
 
204
 
 
205
const QList<KoMainWindow*>& KoPart::mainWindows() const
 
206
{
 
207
    return d->mainWindows;
 
208
}
 
209
 
 
210
int KoPart::mainwindowCount() const
 
211
{
 
212
    return d->mainWindows.count();
 
213
}
 
214
 
 
215
 
 
216
KoMainWindow *KoPart::currentMainwindow() const
 
217
{
 
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);
 
223
    }
 
224
 
 
225
    if (!mainWindow && mainWindows().size() > 0) {
 
226
        mainWindow = mainWindows().first();
 
227
    }
 
228
    return mainWindow;
 
229
 
 
230
}
 
231
 
 
232
void KoPart::openExistingFile(const QUrl &url)
 
233
{
 
234
    QApplication::setOverrideCursor(Qt::BusyCursor);
 
235
    d->document->openUrl(url);
 
236
    d->document->setModified(false);
 
237
    QApplication::restoreOverrideCursor();
 
238
}
 
239
 
 
240
void KoPart::openTemplate(const QUrl &url)
 
241
{
 
242
    QApplication::setOverrideCursor(Qt::BusyCursor);
 
243
    bool ok = d->document->loadNativeFormat(url.toLocalFile());
 
244
    d->document->setModified(false);
 
245
    d->document->undoStack()->clear();
 
246
 
 
247
    if (ok) {
 
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();
 
254
    } else {
 
255
        d->document->showLoadingErrorDialog();
 
256
        d->document->initEmpty();
 
257
    }
 
258
    QApplication::restoreOverrideCursor();
 
259
}
 
260
 
 
261
void KoPart::addRecentURLToAllMainWindows(const QUrl &url)
 
262
{
 
263
    // Add to recent actions list in our mainWindows
 
264
    foreach(KoMainWindow *mainWindow, d->mainWindows) {
 
265
        mainWindow->addRecentURL(url);
 
266
    }
 
267
 
 
268
}
 
269
 
 
270
void KoPart::setTemplatesResourcePath(const QString &templatesResourcePath)
 
271
{
 
272
    Q_ASSERT(!templatesResourcePath.isEmpty());
 
273
    Q_ASSERT(templatesResourcePath.endsWith(QLatin1Char('/')));
 
274
 
 
275
    d->templatesResourcePath = templatesResourcePath;
 
276
}
 
277
 
 
278
QString KoPart::templatesResourcePath() const
 
279
{
 
280
    return d->templatesResourcePath;
 
281
}