• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kig

  • sources
  • kde-4.12
  • kdeedu
  • kig
  • misc
lists.cc
Go to the documentation of this file.
1 // Copyright (C) 2003 Dominique Devriese <devriese@kde.org>
2 
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
7 
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 // 02110-1301, USA.
17 
18 #include "lists.h"
19 
20 #include "object_constructor.h"
21 #include "guiaction.h"
22 #include "object_hierarchy.h"
23 #include "../kig/kig_part.h"
24 
25 #include <klocale.h>
26 #include <kmessagebox.h>
27 #include <qfile.h>
28 #include <qtextstream.h>
29 #include <qdom.h>
30 #include <qregexp.h>
31 #include <algorithm>
32 #include <iterator>
33 using namespace std;
34 
35 template<typename T>
36 void vect_remove( std::vector<T>& v, const T& t )
37 {
38  typename std::vector<T>::iterator new_end = std::remove( v.begin(), v.end(), t );
39  v.erase( new_end, v.end() );
40 }
41 
42 GUIActionList* GUIActionList::instance()
43 {
44  static GUIActionList l;
45  return &l;
46 }
47 
48 GUIActionList::~GUIActionList()
49 {
50  for ( avectype::iterator i = mactions.begin(); i != mactions.end(); ++i )
51  delete *i;
52 }
53 
54 GUIActionList::GUIActionList()
55 {
56 }
57 
58 void GUIActionList::regDoc( KigPart* d )
59 {
60  mdocs.insert( d );
61 }
62 
63 void GUIActionList::unregDoc( KigPart* d )
64 {
65  mdocs.erase( d );
66 }
67 
68 void GUIActionList::add( const std::vector<GUIAction*>& a )
69 {
70  copy( a.begin(), a.end(), inserter( mactions, mactions.begin() ) );
71  for ( dvectype::iterator i = mdocs.begin(); i != mdocs.end(); ++i )
72  {
73  KigPart::GUIUpdateToken t = (*i)->startGUIActionUpdate();
74  for ( uint j = 0; j < a.size(); ++j )
75  (*i)->actionAdded( a[j], t );
76  (*i)->endGUIActionUpdate( t );
77  };
78 }
79 
80 void GUIActionList::add( GUIAction* a )
81 {
82  mactions.insert( a );
83  for ( dvectype::iterator i = mdocs.begin(); i != mdocs.end(); ++i )
84  {
85  KigPart::GUIUpdateToken t = (*i)->startGUIActionUpdate();
86  (*i)->actionAdded( a, t );
87  (*i)->endGUIActionUpdate( t );
88  };
89 }
90 
91 void GUIActionList::remove( const std::vector<GUIAction*>& a )
92 {
93  for ( uint i = 0; i < a.size(); ++i )
94  {
95  mactions.erase( a[i] );
96  };
97  for ( dvectype::iterator i = mdocs.begin(); i != mdocs.end(); ++i )
98  {
99  KigPart::GUIUpdateToken t = (*i)->startGUIActionUpdate();
100  for ( uint j = 0; j < a.size(); ++j )
101  (*i)->actionRemoved( a[j], t );
102  (*i)->endGUIActionUpdate( t );
103  };
104  delete_all( a.begin(), a.end() );
105 }
106 
107 void GUIActionList::remove( GUIAction* a )
108 {
109  mactions.erase( a );
110  for ( dvectype::iterator i = mdocs.begin(); i != mdocs.end(); ++i )
111  {
112  KigPart::GUIUpdateToken t = (*i)->startGUIActionUpdate();
113  (*i)->actionRemoved( a, t );
114  (*i)->endGUIActionUpdate( t );
115  };
116  delete a;
117 }
118 
119 ObjectConstructorList::ObjectConstructorList()
120 {
121 }
122 
123 ObjectConstructorList::~ObjectConstructorList()
124 {
125  for ( vectype::iterator i = mctors.begin(); i != mctors.end(); ++i )
126  delete *i;
127 }
128 
129 ObjectConstructorList* ObjectConstructorList::instance()
130 {
131  static ObjectConstructorList s;
132  return &s;
133 }
134 
135 ObjectConstructorList::vectype ObjectConstructorList::ctorsThatWantArgs(
136  const std::vector<ObjectCalcer*>& os, const KigDocument& d,
137  const KigWidget& w, bool co ) const
138 {
139  vectype ret;
140  for ( vectype::const_iterator i = mctors.begin(); i != mctors.end(); ++i )
141  {
142  int r = (*i)->wantArgs( os, d, w );
143  if ( r == ArgsParser::Complete || ( !co && r == ArgsParser::Valid ) )
144  ret.push_back( *i );
145  };
146  return ret;
147 }
148 
149 void ObjectConstructorList::remove( ObjectConstructor* a )
150 {
151  vect_remove( mctors, a );
152  delete a;
153 }
154 
155 void ObjectConstructorList::add( ObjectConstructor* a )
156 {
157  mctors.push_back( a );
158 }
159 
160 Macro::Macro( GUIAction* a, MacroConstructor* c )
161  : action( a ), ctor( c )
162 {
163 }
164 
165 bool operator==( const Macro& l, const Macro& r )
166 {
167  return ( l.action->descriptiveName() == r.action->descriptiveName() ) &&
168  ( l.action->description() == r.action->description() ) &&
169  ( l.action->iconFileName() == r.action->iconFileName() );
170 }
171 
172 MacroList::MacroList()
173 {
174 }
175 
176 MacroList::~MacroList()
177 {
178  std::vector<GUIAction*> actions;
179  std::vector<ObjectConstructor*> ctors;
180  for ( vectype::iterator i = mdata.begin(); i != mdata.end(); ++i )
181  {
182  Macro* m = *i;
183  GUIAction* a = m->action;
184  actions.push_back( a );
185  ObjectConstructor* c = m->ctor;
186  ctors.push_back( c );
187  delete m;
188  };
189  mdata.clear();
190  GUIActionList::instance()->remove( actions );
191  for ( uint i = 0; i < ctors.size(); ++i )
192  ObjectConstructorList::instance()->remove( ctors[i] );
193 }
194 
195 MacroList* MacroList::instance()
196 {
197  static MacroList t;
198  return &t;
199 }
200 
201 void MacroList::add( const std::vector<Macro*>& ms )
202 {
203  copy( ms.begin(), ms.end(), back_inserter( mdata ) );
204  std::vector<GUIAction*> acts;
205  for ( uint i = 0; i < ms.size(); ++i )
206  {
207  ObjectConstructorList::instance()->add( ms[i]->ctor );
208  acts.push_back( ms[i]->action );
209  };
210  GUIActionList::instance()->add( acts );
211 }
212 
213 void MacroList::add( Macro* m )
214 {
215  mdata.push_back( m );
216  ObjectConstructorList::instance()->add( m->ctor );
217  GUIActionList::instance()->add( m->action );
218 }
219 
220 void MacroList::remove( Macro* m )
221 {
222  GUIAction* a = m->action;
223  ObjectConstructor* c = m->ctor;
224  mdata.erase( std::remove( mdata.begin(), mdata.end(), m ),
225  mdata.end() );
226  delete m;
227  GUIActionList::instance()->remove( a );
228  ObjectConstructorList::instance()->remove( c );
229 }
230 
231 const MacroList::vectype& MacroList::macros() const
232 {
233  return mdata;
234 }
235 
236 Macro::~Macro()
237 {
238 }
239 
240 bool MacroList::save( Macro* m, const QString& f )
241 {
242  std::vector<Macro*> ms;
243  ms.push_back( m );
244  return save( ms, f );
245 }
246 
247 bool MacroList::save( const std::vector<Macro*>& ms, const QString& f )
248 {
249  QDomDocument doc( "KigMacroFile" );
250 
251  QDomElement docelem = doc.createElement( "KigMacroFile" );
252  docelem.setAttribute( "Version", KIGVERSION );
253  docelem.setAttribute( "Number", static_cast<uint>( ms.size() ) );
254 
255  for ( uint i = 0; i < ms.size(); ++i )
256  {
257  MacroConstructor* ctor = ms[i]->ctor;
258 
259  QDomElement macroelem = doc.createElement( "Macro" );
260 
261  // name
262  QDomElement nameelem = doc.createElement( "Name" );
263  nameelem.appendChild( doc.createTextNode( ctor->descriptiveName() ) );
264  macroelem.appendChild( nameelem );
265 
266  // desc
267  QDomElement descelem = doc.createElement( "Description" );
268  descelem.appendChild( doc.createTextNode( ctor->description() ) );
269  macroelem.appendChild( descelem );
270 
271  // icon
272  QByteArray icon = ctor->iconFileName( true );
273  if ( !icon.isNull() )
274  {
275  QDomElement descelem = doc.createElement( "IconFileName" );
276  descelem.appendChild( doc.createTextNode( icon ) );
277  macroelem.appendChild( descelem );
278  }
279 
280  // data
281  QDomElement hierelem = doc.createElement( "Construction" );
282  ctor->hierarchy().serialize( hierelem, doc );
283  macroelem.appendChild( hierelem );
284 
285  docelem.appendChild( macroelem );
286  };
287 
288  doc.appendChild( docelem );
289 
290  QFile file( f );
291  if ( ! file.open( QIODevice::WriteOnly ) )
292  return false;
293  QTextStream stream( &file );
294  stream << doc.toByteArray();
295  return true;
296 }
297 
298 bool MacroList::load( const QString& f, std::vector<Macro*>& ret, const KigPart& kdoc )
299 {
300  QFile file( f );
301  if ( ! file.open( QIODevice::ReadOnly ) )
302  {
303  KMessageBox::sorry( 0, i18n( "Could not open macro file '%1'", f ) );
304  return false;
305  }
306  QDomDocument doc( "KigMacroFile" );
307  if ( !doc.setContent( &file ) )
308  {
309  KMessageBox::sorry( 0, i18n( "Could not open macro file '%1'", f ) );
310  return false;
311  }
312  file.close();
313  QDomElement main = doc.documentElement();
314 
315  if ( main.tagName() == "KigMacroFile" )
316  return loadNew( main, ret, kdoc );
317  else
318  {
319  KMessageBox::detailedSorry(
320  0, i18n( "Kig cannot open the macro file \"%1\".", f ),
321  i18n( "This file was created by a very old Kig version (pre-0.4). "
322  "Support for this format has been removed from recent Kig versions. "
323  "You can try to import this macro using a previous Kig version "
324  "(0.4 to 0.6) and then export it again in the new format." ),
325  i18n( "Not Supported" ) );
326  return false;
327  }
328 }
329 
330 bool MacroList::loadNew( const QDomElement& docelem, std::vector<Macro*>& ret, const KigPart& )
331 {
332  bool sok = true;
333  // unused..
334 // int number = docelem.attribute( "Number" ).toInt( &sok );
335  if ( ! sok ) return false;
336 
337  QString version = docelem.attribute( "Version" );
338 // QRegExp re( "(\\d+)\\.(\\d+)\\.(\\d+)" );
339 // re.match( version );
340  // unused..
341 // int major = re.cap( 1 ).toInt( &sok );
342 // int minor = re.cap( 2 ).toInt( &sok );
343 // int mminor = re.cap( 3 ).toInt( &sok );
344 // if ( ! sok ) return false;
345 
346  int unnamedindex = 1;
347  QString tmp;
348 
349  for ( QDomElement macroelem = docelem.firstChild().toElement();
350  ! macroelem.isNull(); macroelem = macroelem.nextSibling().toElement() )
351  {
352  QString name, description;
353  ObjectHierarchy* hierarchy = 0;
354  QByteArray actionname;
355  QByteArray iconfile( "system-run" );
356  if ( macroelem.tagName() != "Macro" ) continue; // forward compat ?
357  for ( QDomElement dataelem = macroelem.firstChild().toElement();
358  ! dataelem.isNull(); dataelem = dataelem.nextSibling().toElement() )
359  {
360  if ( dataelem.tagName() == "Name" )
361  name = dataelem.text();
362  else if ( dataelem.tagName() == "Description" )
363  description = dataelem.text();
364  else if ( dataelem.tagName() == "Construction" )
365  hierarchy = ObjectHierarchy::buildSafeObjectHierarchy( dataelem, tmp );
366  else if ( dataelem.tagName() == "ActionName" )
367  actionname = dataelem.text().toLatin1();
368  else if ( dataelem.tagName() == "IconFileName" )
369  iconfile = dataelem.text().toLatin1();
370  else continue;
371  };
372  assert( hierarchy );
373  // if the macro has no name, we give it a bogus name...
374  bool name_i18ned = false;
375  if ( name.isEmpty() )
376  {
377  name = i18n( "Unnamed Macro #%1", unnamedindex++ );
378  name_i18ned = true;
379  }
380  MacroConstructor* ctor =
381  new MacroConstructor( *hierarchy, name_i18ned ? name : i18n( name.toUtf8() ),
382  description.isEmpty() ? QString() : i18n( description.toUtf8() ),
383  iconfile );
384  delete hierarchy;
385  GUIAction* act = new ConstructibleAction( ctor, actionname );
386  Macro* macro = new Macro( act, ctor );
387  ret.push_back( macro );
388  };
389  return true;
390 }
391 
392 const ObjectConstructorList::vectype& ObjectConstructorList::constructors() const
393 {
394  return mctors;
395 }
GUIActionList
List of GUIActions for the parts to show.
Definition: lists.h:38
ObjectHierarchy
Definition: object_hierarchy.h:30
Macro::~Macro
~Macro()
Definition: lists.cc:236
lists.h
MacroConstructor::descriptiveName
const QString descriptiveName() const
Definition: object_constructor.cc:374
ObjectHierarchy::buildSafeObjectHierarchy
static ObjectHierarchy * buildSafeObjectHierarchy(const QDomElement &parent, QString &error)
Deserialize the ObjectHierarchy data from the xml element parent .
Definition: object_hierarchy.cc:482
MacroConstructor::hierarchy
const ObjectHierarchy & hierarchy() const
Definition: object_constructor.cc:498
MacroList::macros
const vectype & macros() const
get access to the list of macro's.
Definition: lists.cc:231
Macro::Macro
Macro(GUIAction *a, MacroConstructor *c)
Definition: lists.cc:160
GUIActionList::remove
void remove(GUIAction *a)
Definition: lists.cc:107
main
int main(int argc, char **argv)
Definition: main.cpp:97
GUIActionList::regDoc
void regDoc(KigPart *d)
register this document, so that it receives notifications for added and removed actions.
Definition: lists.cc:58
ObjectConstructorList
The list of object constructors for use in various places, e.g.
Definition: lists.h:69
vect_remove
void vect_remove(std::vector< T > &v, const T &t)
Definition: lists.cc:36
KigPart::GUIUpdateToken
std::vector< KigGUIAction * > GUIUpdateToken
the "token" keeps some objects that should be deleted, we only delete them after we replug the action...
Definition: kig_part.h:263
ObjectHierarchy::serialize
void serialize(QDomElement &parent, QDomDocument &doc) const
saves the ObjectHierarchy data in children xml tags of parent .
Definition: object_hierarchy.cc:414
MacroConstructor::description
const QString description() const
Definition: object_constructor.cc:379
ArgsParser::Complete
Definition: argsparser.h:112
Macro::action
GUIAction * action
Definition: lists.h:94
GUIActionList::unregDoc
void unregDoc(KigPart *d)
Definition: lists.cc:63
ObjectConstructorList::add
void add(ObjectConstructor *a)
Definition: lists.cc:155
Macro::ctor
MacroConstructor * ctor
Definition: lists.h:95
MacroConstructor::iconFileName
const QByteArray iconFileName(const bool canBeNull=false) const
Definition: object_constructor.cc:384
ObjectConstructorList::ctorsThatWantArgs
vectype ctorsThatWantArgs(const std::vector< ObjectCalcer * > &, const KigDocument &, const KigWidget &, bool completeOnly=false) const
Definition: lists.cc:135
ObjectConstructor
This class represents a way to construct a set of objects from a set of other objects.
Definition: object_constructor.h:44
delete_all
void delete_all(T begin, T end)
Definition: objects/common.h:53
MacroList::add
void add(Macro *m)
Add a Macro m .
Definition: lists.cc:213
MacroConstructor
MacroConstructor is a class that represents Kig macro's: these are constructed by the user...
Definition: object_constructor.h:336
KigWidget
This class is the real widget showing the document.
Definition: kig_view.h:50
ObjectConstructorList::instance
static ObjectConstructorList * instance()
Definition: lists.cc:129
ObjectConstructorList::vectype
std::vector< ObjectConstructor * > vectype
Definition: lists.h:72
ObjectConstructorList::constructors
const vectype & constructors() const
Definition: lists.cc:392
MacroList::vectype
std::vector< Macro * > vectype
Definition: lists.h:113
ConstructibleAction
Definition: guiaction.h:68
GUIAction::iconFileName
virtual QByteArray iconFileName(const bool canBeNull=false) const =0
MacroList
This class keeps a list of all macro's, and allows them to be easily accessed, added etc...
Definition: lists.h:110
ObjectConstructorList::remove
void remove(ObjectConstructor *a)
Definition: lists.cc:149
MacroList::save
bool save(Macro *m, const QString &f)
Save macro m to file f .
Definition: lists.cc:240
GUIAction::descriptiveName
virtual QString descriptiveName() const =0
Macro
this is just a simple data struct.
Definition: lists.h:91
MacroList::load
bool load(const QString &f, vectype &ret, const KigPart &)
load macro's from file f .
Definition: lists.cc:298
KigDocument
KigDocument is the class holding the real data in a Kig document.
Definition: kig_document.h:36
GUIActionList::instance
static GUIActionList * instance()
Definition: lists.cc:42
guiaction.h
KigPart
This is a "Part".
Definition: kig_part.h:68
MacroList::remove
void remove(Macro *m)
Remove macro m .
Definition: lists.cc:220
GUIActionList::add
void add(GUIAction *a)
Definition: lists.cc:80
ArgsParser::Valid
Definition: argsparser.h:112
GUIAction
Definition: guiaction.h:51
object_hierarchy.h
operator==
bool operator==(const Macro &l, const Macro &r)
a simply equality operator for Macro class.
Definition: lists.cc:165
uint
unsigned int uint
Definition: object_imp.h:87
MacroList::instance
static MacroList * instance()
MacroList is a singleton.
Definition: lists.cc:195
GUIAction::description
virtual QString description() const =0
object_constructor.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:35:39 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kig

Skip menu "kig"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal