23 #include "../kig/kig_part.h"
26 #include <kmessagebox.h>
28 #include <qtextstream.h>
38 typename std::vector<T>::iterator new_end = std::remove( v.begin(), v.end(), t );
39 v.erase( new_end, v.end() );
48 GUIActionList::~GUIActionList()
50 for ( avectype::iterator i = mactions.begin(); i != mactions.end(); ++i )
54 GUIActionList::GUIActionList()
70 copy( a.begin(), a.end(), inserter( mactions, mactions.begin() ) );
71 for ( dvectype::iterator i = mdocs.begin(); i != mdocs.end(); ++i )
74 for (
uint j = 0; j < a.size(); ++j )
75 (*i)->actionAdded( a[j], t );
76 (*i)->endGUIActionUpdate( t );
83 for ( dvectype::iterator i = mdocs.begin(); i != mdocs.end(); ++i )
86 (*i)->actionAdded( a, t );
87 (*i)->endGUIActionUpdate( t );
93 for (
uint i = 0; i < a.size(); ++i )
95 mactions.erase( a[i] );
97 for ( dvectype::iterator i = mdocs.begin(); i != mdocs.end(); ++i )
100 for (
uint j = 0; j < a.size(); ++j )
101 (*i)->actionRemoved( a[j], t );
102 (*i)->endGUIActionUpdate( t );
110 for ( dvectype::iterator i = mdocs.begin(); i != mdocs.end(); ++i )
113 (*i)->actionRemoved( a, t );
114 (*i)->endGUIActionUpdate( t );
119 ObjectConstructorList::ObjectConstructorList()
123 ObjectConstructorList::~ObjectConstructorList()
125 for ( vectype::iterator i = mctors.begin(); i != mctors.end(); ++i )
136 const std::vector<ObjectCalcer*>& os,
const KigDocument& d,
140 for ( vectype::const_iterator i = mctors.begin(); i != mctors.end(); ++i )
142 int r = (*i)->wantArgs( os, d, w );
157 mctors.push_back( a );
161 : action( a ), ctor( c )
172 MacroList::MacroList()
176 MacroList::~MacroList()
178 std::vector<GUIAction*> actions;
179 std::vector<ObjectConstructor*> ctors;
180 for ( vectype::iterator i = mdata.begin(); i != mdata.end(); ++i )
184 actions.push_back( a );
186 ctors.push_back( c );
191 for (
uint i = 0; i < ctors.size(); ++i )
203 copy( ms.begin(), ms.end(), back_inserter( mdata ) );
204 std::vector<GUIAction*> acts;
205 for (
uint i = 0; i < ms.size(); ++i )
208 acts.push_back( ms[i]->action );
215 mdata.push_back( m );
224 mdata.erase( std::remove( mdata.begin(), mdata.end(), m ),
242 std::vector<Macro*> ms;
244 return save( ms, f );
249 QDomDocument doc(
"KigMacroFile" );
251 QDomElement docelem = doc.createElement(
"KigMacroFile" );
252 docelem.setAttribute(
"Version", KIGVERSION );
253 docelem.setAttribute(
"Number", static_cast<uint>( ms.size() ) );
255 for (
uint i = 0; i < ms.size(); ++i )
259 QDomElement macroelem = doc.createElement(
"Macro" );
262 QDomElement nameelem = doc.createElement(
"Name" );
264 macroelem.appendChild( nameelem );
267 QDomElement descelem = doc.createElement(
"Description" );
268 descelem.appendChild( doc.createTextNode( ctor->
description() ) );
269 macroelem.appendChild( descelem );
273 if ( !icon.isNull() )
275 QDomElement descelem = doc.createElement(
"IconFileName" );
276 descelem.appendChild( doc.createTextNode( icon ) );
277 macroelem.appendChild( descelem );
281 QDomElement hierelem = doc.createElement(
"Construction" );
283 macroelem.appendChild( hierelem );
285 docelem.appendChild( macroelem );
288 doc.appendChild( docelem );
291 if ( ! file.open( QIODevice::WriteOnly ) )
293 QTextStream stream( &file );
294 stream << doc.toByteArray();
301 if ( ! file.open( QIODevice::ReadOnly ) )
303 KMessageBox::sorry( 0, i18n(
"Could not open macro file '%1'", f ) );
306 QDomDocument doc(
"KigMacroFile" );
307 if ( !doc.setContent( &file ) )
309 KMessageBox::sorry( 0, i18n(
"Could not open macro file '%1'", f ) );
313 QDomElement
main = doc.documentElement();
315 if ( main.tagName() ==
"KigMacroFile" )
316 return loadNew( main, ret, kdoc );
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" ) );
330 bool MacroList::loadNew(
const QDomElement& docelem, std::vector<Macro*>& ret,
const KigPart& )
335 if ( ! sok )
return false;
337 QString version = docelem.attribute(
"Version" );
346 int unnamedindex = 1;
349 for ( QDomElement macroelem = docelem.firstChild().toElement();
350 ! macroelem.isNull(); macroelem = macroelem.nextSibling().toElement() )
352 QString name, description;
354 QByteArray actionname;
355 QByteArray iconfile(
"system-run" );
356 if ( macroelem.tagName() !=
"Macro" )
continue;
357 for ( QDomElement dataelem = macroelem.firstChild().toElement();
358 ! dataelem.isNull(); dataelem = dataelem.nextSibling().toElement() )
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" )
366 else if ( dataelem.tagName() ==
"ActionName" )
367 actionname = dataelem.text().toLatin1();
368 else if ( dataelem.tagName() ==
"IconFileName" )
369 iconfile = dataelem.text().toLatin1();
374 bool name_i18ned =
false;
375 if ( name.isEmpty() )
377 name = i18n(
"Unnamed Macro #%1", unnamedindex++ );
381 new MacroConstructor( *hierarchy, name_i18ned ? name : i18n( name.toUtf8() ),
382 description.isEmpty() ? QString() : i18n( description.toUtf8() ),
387 ret.push_back( macro );
List of GUIActions for the parts to show.
const QString descriptiveName() const
static ObjectHierarchy * buildSafeObjectHierarchy(const QDomElement &parent, QString &error)
Deserialize the ObjectHierarchy data from the xml element parent .
const ObjectHierarchy & hierarchy() const
const vectype & macros() const
get access to the list of macro's.
Macro(GUIAction *a, MacroConstructor *c)
void remove(GUIAction *a)
int main(int argc, char **argv)
void regDoc(KigPart *d)
register this document, so that it receives notifications for added and removed actions.
The list of object constructors for use in various places, e.g.
void vect_remove(std::vector< T > &v, const T &t)
std::vector< KigGUIAction * > GUIUpdateToken
the "token" keeps some objects that should be deleted, we only delete them after we replug the action...
void serialize(QDomElement &parent, QDomDocument &doc) const
saves the ObjectHierarchy data in children xml tags of parent .
const QString description() const
void unregDoc(KigPart *d)
void add(ObjectConstructor *a)
const QByteArray iconFileName(const bool canBeNull=false) const
vectype ctorsThatWantArgs(const std::vector< ObjectCalcer * > &, const KigDocument &, const KigWidget &, bool completeOnly=false) const
This class represents a way to construct a set of objects from a set of other objects.
void delete_all(T begin, T end)
void add(Macro *m)
Add a Macro m .
MacroConstructor is a class that represents Kig macro's: these are constructed by the user...
static ObjectConstructorList * instance()
std::vector< ObjectConstructor * > vectype
const vectype & constructors() const
std::vector< Macro * > vectype
virtual QByteArray iconFileName(const bool canBeNull=false) const =0
This class keeps a list of all macro's, and allows them to be easily accessed, added etc...
void remove(ObjectConstructor *a)
bool save(Macro *m, const QString &f)
Save macro m to file f .
virtual QString descriptiveName() const =0
this is just a simple data struct.
bool load(const QString &f, vectype &ret, const KigPart &)
load macro's from file f .
KigDocument is the class holding the real data in a Kig document.
static GUIActionList * instance()
void remove(Macro *m)
Remove macro m .
bool operator==(const Macro &l, const Macro &r)
a simply equality operator for Macro class.
static MacroList * instance()
MacroList is a singleton.
virtual QString description() const =0