25#include "kptfactory.h"
26#include "kptproject.h"
27#include "kptprojectdialog.h"
28#include "kptresource.h"
29#include "kptcontext.h"
30#include "kptganttview.h"
31#include "KDGanttViewTaskLink.h"
34#include <tqfileinfo.h>
39#include <tdemessagebox.h>
40#include <kstandarddirs.h>
42#include <KoTemplateChooseDia.h>
43#include <KoCommandHistory.h>
46#define CURRENT_SYNTAX_VERSION "0.5"
51Part::Part(TQWidget *parentWidget,
const char *widgetName,
52 TQObject *parent,
const char *name,
bool singleViewMode)
53 : KoDocument(parentWidget, widgetName, parent, name, singleViewMode),
54 m_project(0), m_projectDialog(0), m_parentWidget(parentWidget), m_view(0),
55 m_embeddedGanttView(new GanttView(parentWidget)),
56 m_embeddedContext(new Context()), m_embeddedContextInitialized(false),
57 m_context(0), m_xmlLoader()
59 m_update = m_calculate =
false;
60 m_commandHistory =
new KoCommandHistory(actionCollection());
62 setInstance(Factory::global());
63 setTemplateType(
"kplato_template");
64 m_config.setReadWrite(isReadWrite()|| !isEmbedded());
68 m_project =
new Project();
70 connect(m_commandHistory, TQ_SIGNAL(commandExecuted()), TQ_SLOT(slotCommandExecuted()));
71 connect(m_commandHistory, TQ_SIGNAL(documentRestored()), TQ_SLOT(slotDocumentRestored()));
75 TQTimer* timer =
new TQTimer(
this,
"context update timer");
76 connect(timer,TQ_SIGNAL(timeout()),
this,TQ_SLOT(slotCopyContextFromView()));
83 delete m_commandHistory;
85 delete m_projectDialog;
86 if (m_embeddedGanttView) {
87 delete m_embeddedGanttView;
88 m_embeddedGanttView = 0L;
90 if (m_embeddedContext) {
91 delete m_embeddedContext;
92 m_embeddedContext = 0L;
97bool Part::initDoc(InitDocFlags flags, TQWidget* parentWidget) {
100 if (flags==KoDocument::InitDocEmpty)
103 m_project =
new Project();
109 TQString templateDoc;
110 KoTemplateChooseDia::ReturnType ret;
111 KoTemplateChooseDia::DialogType dlgtype;
112 if (flags != KoDocument::InitDocFileNew )
113 dlgtype = KoTemplateChooseDia::Everything;
115 dlgtype = KoTemplateChooseDia::OnlyTemplates;
117 ret = KoTemplateChooseDia::choose(Factory::global(), templateDoc,
121 if (ret == KoTemplateChooseDia::Template) {
123 result = loadNativeFormat(templateDoc);
125 showLoadingErrorDialog();
126 }
else if (ret == KoTemplateChooseDia::File) {
127 KURL url(templateDoc);
128 kdDebug() <<
"Part::initDoc opening URL " << url.prettyURL() <<endl;
129 result = openURL(url);
130 }
else if (ret == KoTemplateChooseDia::Empty) {
133 m_project =
new Project();
148KoView *Part::createViewInstance(TQWidget *parent,
const char *name) {
149 m_view =
new View(
this, parent, name);
150 connect(m_view,TQ_SIGNAL(destroyed()),
this,TQ_SLOT(slotViewDestroyed()));
155 if (m_projectDialog != 0) {
156 kdDebug() <<
"Deleting m_projectDialog because of new ViewInstance\n";
157 delete m_projectDialog;
161 m_view->setContext( *m_context );
162 else if (m_embeddedContext && m_embeddedContextInitialized)
163 m_view->setContext( *m_embeddedContext );
167 m_view->setTaskActionsEnabled(
true);
174void Part::editProject() {
176 TQWidget* parent = m_parentWidget;
180 if (m_projectDialog == 0)
182 m_projectDialog =
new ProjectDialog(*m_project, parent);
184 m_projectDialog->exec();
188bool Part::loadXML(TQIODevice *,
const TQDomDocument &document) {
191 emit sigProgress( 0 );
194 TQDomElement plan = document.documentElement();
197 value = plan.attribute(
"mime", TQString());
198 if (value.isEmpty()) {
199 kdError() <<
"No mime type specified!" << endl;
200 setErrorMessage(i18n(
"Invalid document. No mimetype specified."));
203 else if (value !=
"application/x-vnd.kde.kplato") {
204 kdError() <<
"Unknown mime type " << value << endl;
205 setErrorMessage(i18n(
"Invalid document. Expected mimetype application/x-vnd.kde.kplato, got %1").arg(value));
208 TQString m_syntaxVersion = plan.attribute(
"version", CURRENT_SYNTAX_VERSION);
209 if (m_syntaxVersion > CURRENT_SYNTAX_VERSION) {
210 int ret = KMessageBox::warningContinueCancel(
211 0, i18n(
"This document was created with a newer version of KPlato (syntax version: %1)\n"
212 "Opening it in this version of KPlato will lose some information.").arg(m_syntaxVersion),
213 i18n(
"File-Format Mismatch"), i18n(
"Continue") );
214 if (ret == KMessageBox::Cancel)
216 setErrorMessage(
"USER_CANCELED");
222 TQDomNodeList list = plan.childNodes();
223 if (list.count() > 2) {
225 kdDebug() <<
"*** Error ***\n";
226 kdDebug() <<
" Children count should be 1 but is " << list.count()
230 m_xmlLoader.startLoad();
231 for (
unsigned int i = 0; i < list.count(); ++i) {
232 if (list.item(i).isElement()) {
233 TQDomElement e = list.item(i).toElement();
235 if (e.tagName() ==
"context") {
237 m_context =
new Context();
239 }
else if (e.tagName() ==
"project") {
240 Project *newProject =
new Project();
241 if (newProject->load(e)) {
244 m_project = newProject;
245 delete m_projectDialog;
250 m_xmlLoader.addMsg(XMLLoaderObject::Errors,
"Loading of project failed");
256 m_xmlLoader.stopLoad();
257 emit sigProgress(100);
259 kdDebug() <<
"Loading took " << (float)(dt.elapsed()) / 1000 <<
" seconds" << endl;
262 emit sigProgress(-1);
264 m_commandHistory->clear();
265 m_commandHistory->documentSaved();
266 setModified(
false );
268 m_view->slotUpdate(
false);
272TQDomDocument Part::saveXML() {
273 TQDomDocument document(
"kplato");
275 document.appendChild(document.createProcessingInstruction(
277 "version=\"1.0\" encoding=\"UTF-8\""));
279 TQDomElement doc = document.createElement(
"kplato");
280 doc.setAttribute(
"editor",
"KPlato");
281 doc.setAttribute(
"mime",
"application/x-vnd.kde.kplato");
282 doc.setAttribute(
"version", CURRENT_SYNTAX_VERSION);
283 document.appendChild(doc);
288 m_context =
new Context();
289 m_view->getContext(*m_context);
292 m_context->save(doc);
295 m_project->save(doc);
297 m_commandHistory->documentSaved();
302void Part::slotDocumentRestored() {
308void Part::paintContent(TQPainter &painter,
const TQRect &rect,
310 double zoomX,
double )
312 kdDebug() <<
"----------- KPlato: Part::paintContent ------------" << endl;
313 if (isEmbedded() && m_embeddedGanttView && m_project)
315 if (m_embeddedContext)
317 int ganttsize = m_embeddedContext->ganttview.ganttviewsize;
318 int tasksize = m_embeddedContext->ganttview.taskviewsize;
319 bool showtaskname = m_embeddedContext->ganttview.showTaskName;
323 m_embeddedContext->ganttview.showTaskName =
true;
325 m_embeddedGanttView->setContext( m_embeddedContext->ganttview, *m_project );
327 m_embeddedContext->ganttview.ganttviewsize = ganttsize;
328 m_embeddedContext->ganttview.taskviewsize = tasksize;
329 m_embeddedContext->ganttview.showTaskName = showtaskname;
333 kdWarning() <<
"Don't have any context to set!" << endl;
335 painter.setClipRect(rect, TQPainter::CoordPainter);
338 setZoomAndResolution(100, KoGlobal::dpiX(), KoGlobal::dpiY());
339 if ( m_zoomedResolutionX != zoomX ) {
340 d_zoom *= ( zoomX / m_zoomedResolutionX );
341 painter.scale(d_zoom, d_zoom);
344 m_embeddedGanttView->clear();
345 m_embeddedGanttView->draw(*m_project);
346 m_embeddedGanttView->drawOnPainter(&painter,rect);
363void Part::addCommand(KCommand * cmd,
bool execute)
365 m_commandHistory->addCommand(cmd, execute);
368void Part::slotCommandExecuted() {
371 kdDebug() <<
"------- KPlato, is embedded: " << isEmbedded() << endl;
376 m_view->slotUpdate(
false);
378 m_view->slotUpdate(
false);
381 m_view->setBaselineMode(getProject().isBaselined());
383 m_update = m_calculate = m_baseline =
false;
386void Part::slotCopyContextFromView()
391 this->m_view->getContext( *m_embeddedContext );
392 this->m_embeddedContextInitialized =
true;
402void Part::slotViewDestroyed()
407void Part::setCommandType(
int type) {
417void Part::generateWBS() {
418 m_project->generateWBS(1, m_wbsDefinition);
423#include "kptpart.moc"