1 /* This file is part of the KDE project
2  * Copyright (C) 2001-2002 Lennart Kudling <kudling@kde.org>
3  * Copyright (C) 2001-2007 Rob Buis <buis@kde.org>
4  * Copyright (C) 2002-2006 Laurent Montel <montel@kde.org>
5  * Copyright (C) 2002 Werner Trobin <trobin@kde.org>
6  * Copyright (C) 2002-2006 David Faure <faure@kde.org>
7  * Copyright (C) 2002 Stephan Kulow <coolo@kde.org>
8  * Copyright (C) 2002 Benoit Vautrin <benoit.vautrin@free.fr>
9  * Copyright (C) 2003 Thomas Nagy <tnagyemail-mail@yahoo.fr>
10  * Copyright (C) 2003,2006 Dirk Mueller <mueller@kde.org>
11  * Copyright (C) 2004 Brad Hards <bradh@frogmouth.net>
12  * Copyright (C) 2004-2006 Peter Simonsson <psn@linux.se>
13  * Copyright (C) 2004-2005 Fredrik Edemar <f_edemar@linux.se>
14  * Copyright (C) 2005-2006 Tim Beaulen <tbscope@gmail.com>
15  * Copyright (C) 2005 Sven Langkamp <sven.langkamp@gmail.com>
16  * Copyright (C) 2005-2007 Jan Hambrecht <jaham@gmx.net>
17  * Copyright (C) 2005-2007 Thomas Zander <zander@kde.org>
18  * Copyright (C) 2005-2013 Inge Wallin <inge@lysator.liu.se>
19  * Copyright (C) 2005 Johannes Schaub <johannes.schaub@kdemail.net>
20  * Copyright (C) 2006 Gabor Lehel <illissius@gmail.com>
21  * Copyright (C) 2006 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
22  * Copyright (C) 2006 Jaison Lee <lee.jaison@gmail.com>
23  * Copyright (C) 2006 Casper Boemann <cbr@boemann.dk>
24  * Copyright (C) 2006-2007 Thorsten Zachmann <t.zachmann@zagge.de>
25  * Copyright (C) 2007 Matthias Kretz <kretz@kde.org>
26  * Copyright (C) 2012 Yue Liu <yue.liu@mail.com>
27  *
28  * This library is free software; you can redistribute it and/or
29  * modify it under the terms of the GNU Library General Public
30  * License as published by the Free Software Foundation; either
31  * version 2 of the License, or (at your option) any later version.
32  *
33  * This library is distributed in the hope that it will be useful,
34  * but WITHOUT ANY WARRANTY; without even the implied warranty of
35  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
36  * Library General Public License for more details.
37  *
38  * You should have received a copy of the GNU Library General Public License
39  * along with this library; see the file COPYING.LIB.  If not, write to
40  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
41  * Boston, MA 02110-1301, USA.
42  */
43 
44 #include "KarbonDocument.h"
45 #include "KarbonPart.h"
46 #include "KarbonFactory.h"
47 #include "KarbonView.h"
48 #include "KarbonUiDebug.h"
49 
50 #include <KoImageCollection.h>
51 #include <KoText.h>
52 #include <KoSelection.h>
53 #include <KoStyleManager.h>
54 #include <KoTextSharedLoadingData.h>
55 #include <KoOdfStylesReader.h>
56 #include <KoOdfLoadingContext.h>
57 #include <KoOdfReadStore.h>
58 #include <KoOdfWriteStore.h>
59 #include <KoShapeSavingContext.h>
60 #include <KoShapeLoadingContext.h>
61 #include <KoGridData.h>
62 #include <KoGuidesData.h>
63 #include <KoPageLayout.h>
64 #include <KoXmlWriter.h>
65 #include <KoXmlNS.h>
66 #include <KoGenStyles.h>
67 #include <KoOasisSettings.h>
68 #include <KoMainWindow.h>
69 #include <KoCanvasController.h>
70 #include <KoToolManager.h>
71 #include <KoShapeManager.h>
72 #include <KoShapeLayer.h>
73 #include <KoShapeRegistry.h>
74 #include <KoCanvasResourceManager.h>
75 #include <KoDocumentResourceManager.h>
76 #include <KoEmbeddedDocumentSaver.h>
77 #include <KoStoreDevice.h>
78 #include <KoStore.h>
79 #include <KoUnit.h>
80 #include <KoShapePainter.h>
81 #include <SvgShapeFactory.h>
82 
83 #include <kconfig.h>
84 #include <kconfiggroup.h>
85 #include <klocalizedstring.h>
86 #include <kundo2stack.h>
87 
88 #include <QLocale>
89 #include <QRectF>
90 #include <QPainter>
91 
92 // Make sure an appropriate DTD is available in www/calligra/DTD if changing this value
93 // static const char * CURRENT_DTD_VERSION = "1.2";
94 
95 class Q_DECL_HIDDEN KarbonDocument::Private
96 {
97 public:
Private()98     Private()
99             : showStatusBar(true),
100               merge(false),
101               maxRecentFiles(10)
102     {}
103 
104     // KarbonDocument document;  ///< store non-visual doc info
105 
106     bool showStatusBar;       ///< enable/disable status bar in attached view(s)
107     bool merge;
108     uint maxRecentFiles;      ///< max. number of files shown in open recent menu item
109 };
110 
111 
KarbonDocument(KarbonPart * part)112 KarbonDocument::KarbonDocument(KarbonPart* part)
113     : KoPADocument(part)
114     , d(new Private())
115 {
116     Q_ASSERT(part);
117 
118     initConfig();
119 
120     SvgShapeFactory::addToRegistry();
121 
122     // set as default paper
123     KoPageLayout pl = pageLayout();
124     pl.format = KoPageFormat::defaultFormat();
125     pl.orientation = KoPageFormat::Portrait;
126     pl.width = MM_TO_POINT(KoPageFormat::width(pl.format, pl.orientation));
127     pl.height = MM_TO_POINT(KoPageFormat::height(pl.format, pl.orientation));
128     setPageLayout(pl);
129 }
130 
~KarbonDocument()131 KarbonDocument::~KarbonDocument()
132 {
133     delete d;
134 }
135 
slotDocumentRestored()136 void KarbonDocument::slotDocumentRestored()
137 {
138     setModified(false);
139 }
140 
showStatusBar() const141 bool KarbonDocument::showStatusBar() const
142 {
143     return d->showStatusBar;
144 }
145 
setShowStatusBar(bool b)146 void KarbonDocument::setShowStatusBar(bool b)
147 {
148     d->showStatusBar = b;
149 }
150 
maxRecentFiles() const151 uint KarbonDocument::maxRecentFiles() const
152 {
153     return d->maxRecentFiles;
154 }
155 
reorganizeGUI()156 void KarbonDocument::reorganizeGUI()
157 {
158     foreach(KoView* view, documentPart()->views()) {
159         KarbonView * kv = qobject_cast<KarbonView*>(view);
160         if (kv) {
161             kv->reorganizeGUI();
162         }
163     }
164 }
165 
initConfig()166 void KarbonDocument::initConfig()
167 {
168     KSharedConfigPtr config = KarbonFactory::karbonConfig();
169 
170     // disable grid by default
171     gridData().setShowGrid(false);
172 
173     if (config->hasGroup("Interface")) {
174         KConfigGroup interfaceGroup = config->group("Interface");
175         setAutoSave(interfaceGroup.readEntry("AutoSave", defaultAutoSave() / 60) * 60);
176         d->maxRecentFiles = interfaceGroup.readEntry("NbRecentFile", 10);
177         setShowStatusBar(interfaceGroup.readEntry("ShowStatusBar" , true));
178         setBackupFile(interfaceGroup.readEntry("BackupFile", true));
179     }
180     int undos = 30;
181 
182     QString defaultUnitSymbol =
183         QLatin1String((QLocale().measurementSystem() == QLocale::ImperialSystem)?"in":"cm");
184 
185     if (config->hasGroup("Misc")) {
186         KConfigGroup miscGroup = config->group("Misc");
187         undos = miscGroup.readEntry("UndoRedo", -1);
188         defaultUnitSymbol = miscGroup.readEntry("Units", defaultUnitSymbol);
189     }
190     undoStack()->setUndoLimit(undos);
191     setUnit(KoUnit::fromSymbol(defaultUnitSymbol));
192 
193 }
194 
mergeNativeFormat(const QString & file)195 bool KarbonDocument::mergeNativeFormat(const QString &file)
196 {
197     d->merge = true;
198     bool result = loadNativeFormat(file);
199     if (!result)
200         showLoadingErrorDialog();
201     d->merge = false;
202     return result;
203 }
204 
documentType() const205 KoOdf::DocumentType KarbonDocument::documentType() const
206 {
207     return KoOdf::Graphics;
208 }
209 
odfTagName(bool withNamespace)210 const char * KarbonDocument::odfTagName(bool withNamespace)
211 {
212     return withNamespace ? "office:drawing": "drawing";
213 }
214