kivio

kivio_map.cpp
1/*
2 * Kivio - Visual Modelling and Flowcharting
3 * Copyright (C) 2000-2001 theKompany.com & Dave Marotti
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19#include <tqprinter.h>
20#include <tqdom.h>
21#include <tqmessagebox.h>
22
23#include <KoStore.h>
24#include <KoXmlWriter.h>
25#include <KoGenStyles.h>
26
27#include "kivio_map.h"
28#include "kivio_doc.h"
29#include "kivio_view.h"
30#include "kivio_canvas.h"
31#include "kivio_page.h"
32#include "KIvioMapIface.h"
33
34#include <time.h>
35#include <stdlib.h>
36
37KivioMap::KivioMap( KivioDoc* doc, const char* name )
38: TQObject(doc,name)
39{
40 m_pDoc = doc;
41 m_lstPages.setAutoDelete(true);
42 m_dcop = 0;
43
44}
45
46KivioMap::~KivioMap()
47{
48 delete m_dcop;
49
50}
51
52void KivioMap::takePage( KivioPage* page )
53{
54 int pos=m_lstPages.findRef(page);
55 m_lstPages.take( pos );
56 m_lstDeletedPages.append( page );
57}
58
59void KivioMap::insertPage( KivioPage* page )
60{
61 int pos=m_lstDeletedPages.findRef(page);
62 if ( pos != -1 )
63 m_lstDeletedPages.take( pos);
64 m_lstPages.append(page);
65}
66
67void KivioMap::addPage( KivioPage* page )
68{
69 m_lstPages.append(page);
70}
71
72void KivioMap::movePage( const TQString& fromPageName, const TQString& toPageName, bool before )
73{
74 KivioPage* pagefrom = findPage(fromPageName);
75 KivioPage* pageto = findPage(toPageName);
76
77 int from = m_lstPages.find(pagefrom);
78 int to = m_lstPages.find(pageto);
79 if (!before)
80 ++to;
81
82 if ( to > (int)m_lstPages.count() ) {
83 m_lstPages.append(pagefrom);
84 m_lstPages.take(from);
85 } else
86 if ( from < to ) {
87 m_lstPages.insert(to,pagefrom);
88 m_lstPages.take(from);
89 } else {
90 m_lstPages.take(from);
91 m_lstPages.insert(to,pagefrom);
92 }
93}
94
95TQDomElement KivioMap::save( TQDomDocument& doc )
96{
97 int next = 1;
98
99 TQDomElement mymap = doc.createElement("KivioMap");
100
101 // Before we save, tell all the pages/layers/stencil/targets/connectors to generate
102 // their ids so we can restore connections when reloaded.
103 TQPtrListIterator<KivioPage> it2(m_lstPages);
104 for( ; it2.current(); ++it2 )
105 {
106 next = it2.current()->generateStencilIds( next );
107 }
108
109 // Now save the pages
110 TQPtrListIterator<KivioPage> it(m_lstPages);
111 for( ; it.current(); ++it )
112 {
113 TQDomElement e = it.current()->save(doc);
114 if (e.isNull())
115 return e;
116 mymap.appendChild(e);
117 }
118
119 return mymap;
120}
121
122void KivioMap::saveOasis(KoStore* store, KoXmlWriter* docWriter, KoGenStyles* styles)
123{
124 TQPtrListIterator<KivioPage> it(m_lstPages);
125
126 for( ; it.current(); ++it )
127 {
128 it.current()->saveOasis(store, docWriter, styles);
129 }
130}
131
132bool KivioMap::loadXML( const TQDomElement& mymap )
133{
134 m_lstPages.clear();
135 m_lstDeletedPages.clear();
136
137 // FIXME: make this load the real page units and whatever
138 // else
139 TQDomNode n = mymap.firstChild();
140 while( !n.isNull() ) {
141 TQDomElement e = n.toElement();
142 if ( !e.isNull() && e.tagName() == "KivioPage" ) {
143 KivioPage *t = m_pDoc->createPage();
144 m_pDoc->addPage( t );
145 if ( !t->loadXML( e ) )
146 return false;
147 }
148 n = n.nextSibling();
149 }
150 return true;
151}
152
153void KivioMap::update()
154{
155 TQPtrListIterator<KivioPage> it( m_lstPages );
156 for( ; it.current(); ++it )
157 it.current()->update();
158}
159
160KivioPage* KivioMap::findPage( const TQString& name )
161{
162 KivioPage *t;
163
164 for ( t = m_lstPages.first(); t; t = m_lstPages.next() ) {
165 if ( name == t->pageName() )
166 return t;
167 }
168
169 return 0L;
170}
171
172KivioDoc* KivioMap::doc()const
173{
174 return m_pDoc;
175}
176
177KivioPage* KivioMap::firstPage()
178{
179 return m_lstPages.first();
180}
181
182KivioPage* KivioMap::lastPage()
183{
184 return m_lstPages.last();
185}
186
187KivioPage* KivioMap::nextPage()
188{
189 return m_lstPages.next();
190}
191
192int KivioMap::count() const
193{
194 return m_lstPages.count();
195}
196
197DCOPObject* KivioMap::dcopObject()
198{
199 if ( !m_dcop )
200 m_dcop = new KIvioMapIface( this );
201
202 return m_dcop;
203}
204
205TQStringList KivioMap::visiblePages() const
206{
207 TQStringList pages;
208
209 TQPtrListIterator<KivioPage> it( m_lstPages );
210 for( ; it.current(); ++it )
211 {
212 KivioPage* page = it.current();
213 if( !page->isHidden() )
214 pages.append( page->pageName() );
215 }
216
217 return pages;
218}
219
220TQStringList KivioMap::hiddenPages() const
221{
222 TQStringList pages;
223
224 TQPtrListIterator<KivioPage> it( m_lstPages );
225 for( ; it.current(); ++it )
226 {
227 KivioPage* page = it.current();
228 if( page->isHidden() )
229 pages.append( page->pageName() );
230 }
231
232 return pages;
233}
234
235void KivioMap::clear()
236{
237 m_lstPages.clear();
238 m_lstDeletedPages.clear();
239}