[rkward-cvs] rkward/rkward/plugin rkcomponentmap.cpp,1.4,1.5 rkcomponentmap.h,1.2,1.3
Thomas Friedrichsmeier
tfry at users.sourceforge.net
Sun Oct 23 22:07:26 UTC 2005
Update of /cvsroot/rkward/rkward/rkward/plugin
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30735/rkward/plugin
Modified Files:
rkcomponentmap.cpp rkcomponentmap.h
Log Message:
Use KXMLGUI for placement of plugins in the menu. Lots of cleanups still needed, but basically working
Index: rkcomponentmap.h
===================================================================
RCS file: /cvsroot/rkward/rkward/rkward/plugin/rkcomponentmap.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** rkcomponentmap.h 11 Sep 2005 18:53:04 -0000 1.2
--- rkcomponentmap.h 23 Oct 2005 22:07:24 -0000 1.3
***************
*** 45,49 ****
bool isPlugin ();
! static RKComponentHandle* createComponentHandle (const QString &filename, RKComponentType type);
private:
/** The filename of the description file for this comonent */
--- 45,49 ----
bool isPlugin ();
! static RKComponentHandle* createComponentHandle (const QString &filename, RKComponentType type, const QString& id);
private:
/** The filename of the description file for this comonent */
***************
*** 53,56 ****
--- 53,57 ----
#include <qmap.h>
+ #include <kxmlguiclient.h>
class QDomElement;
***************
*** 63,69 ****
The RKComponentMap provides convenience functions for adding or removing a .pluginmap-file to/from the list of components, and looking up RKComponentHandle for a given component name.
@author Thomas Friedrichsmeier
*/
! class RKComponentMap {
public:
RKComponentMap ();
--- 64,72 ----
The RKComponentMap provides convenience functions for adding or removing a .pluginmap-file to/from the list of components, and looking up RKComponentHandle for a given component name.
+ // TODO: maybe we do not need to keep a map at all, but only create the handles! Think about this one day!
+
@author Thomas Friedrichsmeier
*/
! class RKComponentMap : public KXMLGUIClient {
public:
RKComponentMap ();
***************
*** 74,77 ****
--- 77,81 ----
@returns number of plugins (i.e. stand-alone components/menu-entries) added successfully */
int addPluginMap (const QString& plugin_map_file);
+ // int addPluginMapNew (const QString& plugin_map_file);
/** clears out (and deletes) all components / plugins */
void clear ();
***************
*** 84,88 ****
@param element the QDomElement containing the description for the new submenu
@returns number of plugins/menu-entries added successfully */
! int addSubMenu (RKMenu* parent, const QDomElement& element, const QString& cnamespace);
/** typedef for easy reference to iterator */
--- 88,95 ----
@param element the QDomElement containing the description for the new submenu
@returns number of plugins/menu-entries added successfully */
! // int addSubMenu (RKMenu* parent, const QDomElement& element, const QString& cnamespace);
! int addSubMenu (QDomElement& parent, const QDomElement& description, const QString& cnamespace);
!
! QDomElement findOrCreateElement (QDomElement& parent, const QString& tagname, const QString& name, const QString& label, int index);
/** typedef for easy reference to iterator */
Index: rkcomponentmap.cpp
===================================================================
RCS file: /cvsroot/rkward/rkward/rkward/plugin/rkcomponentmap.cpp,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** rkcomponentmap.cpp 29 Sep 2005 16:02:51 -0000 1.4
--- rkcomponentmap.cpp 23 Oct 2005 22:07:24 -0000 1.5
***************
*** 30,34 ****
#include "rkpluginhandle.h"
! RKComponentMap::RKComponentMap () {
RK_TRACE (PLUGIN);
}
--- 30,34 ----
#include "rkpluginhandle.h"
! RKComponentMap::RKComponentMap () : KXMLGUIClient () {
RK_TRACE (PLUGIN);
}
***************
*** 37,41 ****
RK_TRACE (PLUGIN);
}
!
int RKComponentMap::addPluginMap (const QString& plugin_map_file) {
RK_TRACE (PLUGIN);
--- 37,41 ----
RK_TRACE (PLUGIN);
}
! /*
int RKComponentMap::addPluginMap (const QString& plugin_map_file) {
RK_TRACE (PLUGIN);
***************
*** 75,80 ****
counter += addSubMenu (0, (*it), cnamespace);
}
return counter;
! }
void RKComponentMap::clear () {
--- 75,84 ----
counter += addSubMenu (0, (*it), cnamespace);
}
+
+ addPluginMapNew (plugin_map_file);
+ qDebug ("after %s", xmlguiBuildDocument ().toString ().latin1 ());
+
return counter;
! } */
void RKComponentMap::clear () {
***************
*** 86,89 ****
--- 90,99 ----
}
components.clear ();
+
+ // TODO!
+ QDomDocument doc;
+ doc.setContent (QString ("<!DOCTYPE kpartgui>\n<kpartgui name=\"rkwardcomponents\" version=\"0.3.4\">\n<MenuBar>\n\n</MenuBar>\n</kpartgui>"));
+ /*<Menu name=\"file\"><text>&File</text></Menu>\n<Menu name=\"analysis\"><text>&Analysis</text>\n</Menu>*/
+ setXMLGUIBuildDocument (doc);
}
***************
*** 93,97 ****
return (components[id]);
}
!
int RKComponentMap::addSubMenu (RKMenu* parent, const QDomElement& element, const QString& cnamespace) {
RK_TRACE (PLUGIN);
--- 103,107 ----
return (components[id]);
}
! /*
int RKComponentMap::addSubMenu (RKMenu* parent, const QDomElement& element, const QString& cnamespace) {
RK_TRACE (PLUGIN);
***************
*** 130,133 ****
--- 140,252 ----
}
return counter;
+ } */
+
+ //static
+ QDomElement RKComponentMap::findOrCreateElement (QDomElement& parent, const QString& tagname, const QString& name, const QString& label, int index) {
+ RK_TRACE (PLUGIN);
+
+ XMLHelper* xml = XMLHelper::getStaticHelper ();
+ XMLChildList list = xml->getChildElements (parent, tagname, DL_INFO);
+ QDomElement insert_after_element;
+ for (XMLChildList::const_iterator it=list.begin (); it != list.end (); ++it) {
+ if (name == xml->getStringAttribute ((*it), "name", "", DL_ERROR)) {
+ return (*it);
+ } else {
+ if ((index >= 0) && insert_after_element.isNull ()) {
+ if (index > xml->getIntAttribute ((*it), "index", 1000000, DL_INFO)) {
+ insert_after_element = *it;
+ }
+ }
+ }
+ }
+
+ // element not found. Create a new one instead
+ QDomElement ret = xmlguiBuildDocument ().createElement (tagname);
+ ret.setAttribute ("name", name);
+ ret.setAttribute ("index", index);
+ QDomElement text = xmlguiBuildDocument ().createElement ("text");
+ text.appendChild (xmlguiBuildDocument ().createTextNode (label));
+ ret.appendChild (text);
+ parent.insertAfter (ret, insert_after_element); // if index_after_element.isNull, this add the new element as the last child of parent!
+
+ return ret;
+ }
+
+ int RKComponentMap::addSubMenu (QDomElement& parent, const QDomElement& description, const QString& cnamespace) {
+ RK_TRACE (PLUGIN);
+
+ int counter = 0;
+ XMLHelper* xml = XMLHelper::getStaticHelper ();
+
+ // 1: check whether menu already exists, and create new menu otherwise
+ QDomElement menu_element = findOrCreateElement (parent, "Menu", xml->getStringAttribute (description, "id", "none", DL_ERROR), xml->getStringAttribute (description, "label", i18n ("(no label)"), DL_WARNING), xml->getIntAttribute (description, "index", -1, DL_INFO));
+
+ // 2: recurse into submenus (of element to add!)
+ XMLChildList list = xml->getChildElements (description, "menu", DL_INFO);
+ for (XMLChildList::const_iterator it=list.begin (); it != list.end (); ++it) {
+ counter += addSubMenu (menu_element, (*it), cnamespace);
+ }
+
+ // 3: add entries
+ list = xml->getChildElements (description, "entry", DL_INFO);
+ for (XMLChildList::const_iterator it=list.begin (); it != list.end (); ++it) {
+ QString id = cnamespace + xml->getStringAttribute ((*it), "component", "#invalid#", DL_ERROR);
+
+ RKComponentHandle* handle = components[id];
+
+ if ((!handle) || (!handle->isPlugin ())) {
+ RK_DO (qDebug ("No such component found while creating menu-entries or component is not a standalone plugin: \"%s\". No entry created.", id.latin1 ()), PLUGIN, DL_ERROR);
+ } else {
+ findOrCreateElement (menu_element, "Action", id, xml->getStringAttribute ((*it), "label", i18n ("(no label)"), DL_WARNING), xml->getIntAttribute ((*it), "index", -1, DL_INFO));
+ action (id)->setText (xml->getStringAttribute ((*it), "label", i18n ("(no label)"), DL_WARNING));
+ counter++;
+ }
+ }
+ return counter;
+ }
+
+ int RKComponentMap::addPluginMap (const QString& plugin_map_file) {
+ RK_TRACE (PLUGIN);
+
+ XMLHelper* xml = XMLHelper::getStaticHelper ();
+ QDomElement element;
+ XMLChildList list;
+
+ QDomElement document_element = xml->openXMLFile (plugin_map_file, DL_ERROR);
+ if (xml->highestError () >= DL_ERROR) return (0);
+
+ QString prefix = QFileInfo (plugin_map_file).dirPath (true) + "/" + xml->getStringAttribute(document_element, "base_prefix", QString::null, DL_WARNING);
+ QString cnamespace = xml->getStringAttribute(document_element, "namespace", "rkward", DL_WARNING) + "::";
+
+ // step 1: create (list of) components
+ element = xml->getChildElement (document_element, "components", DL_ERROR);
+ list = xml->getChildElements (element, "component", DL_ERROR);
+
+ for (XMLChildList::const_iterator it=list.begin (); it != list.end (); ++it) {
+ QString filename = prefix + xml->getStringAttribute((*it), "file", QString::null, DL_WARNING);
+ QString id = cnamespace + xml->getStringAttribute((*it), "id", QString::null, DL_WARNING);
+ int type = xml->getMultiChoiceAttribute ((*it), "type", "standard", 0, DL_WARNING);
+
+ if (components.contains (id)) {
+ RK_DO (qDebug ("RKComponentMap already contains a component with id \"%s\". Ignoring second entry.", id.latin1 ()), PLUGIN, DL_WARNING);
+ } else if (!QFileInfo (filename).isReadable ()) {
+ RK_DO (qDebug ("Specified file '%s' for component id \"%s\" does not exist or is not readable. Ignoring.", filename.latin1 (), id.latin1 ()), PLUGIN, DL_ERROR);
+ } else {
+ components.insert (id, RKComponentHandle::createComponentHandle (filename, (RKComponentType) type, id));
+ }
+ }
+
+ // step 2: create / insert into menus
+ qDebug ("before %s", xmlguiBuildDocument ().toString ().latin1 ());
+ QDomElement xmlgui_menubar_elem = xml->getChildElement (xmlguiBuildDocument ().documentElement (), "MenuBar", DL_ERROR);
+
+ element = xml->getChildElement (document_element, "hierarchy", DL_ERROR);
+ list = xml->getChildElements (element, "menu", DL_ERROR);
+ int counter = 0;
+ for (XMLChildList::const_iterator it=list.begin (); it != list.end (); ++it) {
+ counter += addSubMenu (xmlgui_menubar_elem, (*it), cnamespace);
+ }
+
+ return counter;
}
***************
*** 147,153 ****
//static
! RKComponentHandle* RKComponentHandle::createComponentHandle (const QString &filename, RKComponentType type) {
if (type == (int) Standard) {
! return (new RKPluginHandle (filename, type));
}
// TODO: create an RKPluginHandle instead!
--- 266,274 ----
//static
! RKComponentHandle* RKComponentHandle::createComponentHandle (const QString &filename, RKComponentType type, const QString& id) {
if (type == (int) Standard) {
! RKPluginHandle *ret = new RKPluginHandle (filename, type);
! new KAction ("", 0, ret, SLOT (activated ()), RKGlobals::componentMap ()->actionCollection (), id);
! return (ret);
}
// TODO: create an RKPluginHandle instead!
More information about the rkward-tracker
mailing list