Minor fix
[kdeaccessibility.git] / ksayit / src / contextmenuhandler.cpp
blob35e32b0083327e05478282a32ed6bf006b2b32e4
1 //
2 // C++ Implementation: %{MODULE}
3 //
4 // Description:
5 //
6 //
7 // Author: %{AUTHOR} <%{EMAIL}>, (C) %{YEAR}
8 //
9 // Copyright: See COPYING file that comes with this distribution
13 // Qt includes
14 #include <qfile.h>
16 // KDE includes
17 #include <kdebug.h>
18 #include <klocale.h>
19 #include <kglobal.h>
20 #include <kstandarddirs.h>
22 // App specific includes
23 #include "Types.h"
24 #include "contextmenuhandler.h"
27 /**
28 * XML-ActionHandler
30 ContextActionHandler::ContextActionHandler(ContextMenuHandler *menuhandler)
31 : QXmlDefaultHandler(), m_menuhandler(menuhandler)
33 m_subName = QString::null;
34 m_actionName = QString::null;
35 m_qty = QString::null;
36 m_popup = menuhandler->m_popupmenu;
37 m_hit = false;
38 m_searchID = QString::null;
41 ContextActionHandler::~ContextActionHandler()
46 void ContextActionHandler::setSearchID( const QString xmlID )
48 m_searchID = xmlID;
52 bool ContextActionHandler::startElement( const QString &,
53 const QString &,
54 const QString &qName,
55 const QXmlAttributes &atts )
57 if ( qName == "Item" ){
58 if ( atts.value("id") == m_searchID ){
59 if ( atts.value("editable") == "yes"){
60 m_menuhandler->setItemEditable( true );
62 m_hit = true;
63 } else {
64 m_hit = false;
67 if ( !(m_hit && m_menuhandler->m_popupmenu) )
68 return true;
70 if ( qName == "Action" ){
71 m_actionName = "";
72 m_qty = atts.value("qty");
73 } else if ( qName == "Submenu" ){
74 if ( atts.value("name") != "" ){
75 m_subName = atts.value("name");
76 m_popup = m_menuhandler->SubMenuFactory(m_popup);
79 return true;
83 bool ContextActionHandler::endElement( const QString &,
84 const QString &,
85 const QString &qName )
87 if ( !(m_hit && m_menuhandler->m_popupmenu) )
88 return true;
90 if ( qName == "Action" ){
91 KAction* newAction = m_menuhandler->ActionFactory(m_actionName, m_qty);
92 if ( newAction )
93 newAction->plug( m_popup );
94 } else if ( qName == "Submenu" ){
95 if ( m_subName == "New" ){
96 m_menuhandler->m_popupmenu->insertItem( i18n("New"), m_popup);
98 // only one sublevel spported -> back to toplevel menu
99 m_popup = m_menuhandler->m_popupmenu;
101 return true;
105 bool ContextActionHandler::characters( const QString &ch )
107 m_actionName += ch;
108 return true;
114 * MenuHandler
116 ContextMenuHandler::ContextMenuHandler(QObject *parent, const char *name)
117 : QObject(parent, name), m_DocTreeView(parent)
119 m_popupmenu = NULL;
120 m_item = NULL;
121 initActions();
122 m_XmlFilePath = KGlobal::dirs()->findResource("data", "ksayit/ContextMenus.xml");
126 ContextMenuHandler::~ContextMenuHandler()
128 delete renameItem;
129 delete deleteItem;
130 delete newBookInfo;
131 delete newChapter;
132 delete newKeywordSet;
133 delete newKeyword;
134 delete newAbstract;
135 delete newAuthorGroup;
136 delete newAuthor;
137 delete newDate;
138 delete newReleaseInfo;
139 delete newTitle;
140 delete newParagraph;
141 delete newSection_1;
142 delete newSection_2;
143 delete newSection_3;
144 delete newSection_4;
145 delete newSection_5;
147 if (m_popupmenu)
148 delete m_popupmenu;
152 void ContextMenuHandler::initActions()
154 // User defined actions
155 renameItem = new KAction (i18n("Rename..."),
157 m_DocTreeView, SLOT (slotRenameItem()), NULL );
159 deleteItem = new KAction (i18n("Delete..."),
161 m_DocTreeView, SLOT (slotDeleteItem()), NULL );
163 newBookInfo = new KAction ( i18n("Overview"),
165 m_DocTreeView, SLOT (slotNewBookInfo()), NULL );
167 newChapter = new KAction ( i18n("Chapter"),
169 m_DocTreeView, SLOT (slotNewChapter()), NULL );
171 newKeywordSet = new KAction ( i18n("Keywords"),
173 m_DocTreeView, SLOT (slotNewKeywordSet()), NULL );
175 newKeyword = new KAction ( i18n("Keyword"),
177 m_DocTreeView, SLOT (slotNewKeyword()), NULL );
179 newAbstract = new KAction ( i18n("Abstract"),
181 m_DocTreeView, SLOT (slotNewAbstract()), NULL );
183 newAuthorGroup = new KAction ( i18n("Authors"),
185 m_DocTreeView, SLOT (slotNewAuthorGroup()), NULL );
187 newAuthor = new KAction ( i18n("Author"),
189 m_DocTreeView, SLOT (slotNewAuthor()), NULL );
191 newDate = new KAction ( i18n("Date"),
193 m_DocTreeView, SLOT (slotNewDate()), NULL );
195 newReleaseInfo = new KAction ( i18n("Release Info"),
197 m_DocTreeView, SLOT (slotNewReleaseInfo()), NULL );
199 newTitle = new KAction ( i18n("Title"),
201 m_DocTreeView, SLOT (slotNewTitle()), NULL );
203 newParagraph = new KAction ( i18n("Paragraph"),
205 m_DocTreeView, SLOT (slotNewParagraph()), NULL );
207 newSection_1 = new KAction ( i18n("Section Level 1"),
209 m_DocTreeView, SLOT (slotNewSection_1()), NULL );
211 newSection_2 = new KAction ( i18n("Section Level 2"),
213 m_DocTreeView, SLOT (slotNewSection_2()), NULL );
215 newSection_3 = new KAction ( i18n("Section Level 3"),
217 m_DocTreeView, SLOT (slotNewSection_3()), NULL );
219 newSection_4 = new KAction ( i18n("Section Level 4"),
221 m_DocTreeView, SLOT (slotNewSection_4()), NULL );
223 newSection_5 = new KAction ( i18n("Section Level 5"),
225 m_DocTreeView, SLOT (slotNewSection_5()), NULL );
229 KMenu* ContextMenuHandler::getPopupMenu(ListViewInterface *item)
231 if ( !item )
232 return NULL;
234 m_item = item;
236 QString xmlID = item->getXmlContextName();
237 if ( xmlID.isNull() )
238 return NULL;
240 // delete old popup menu and create new one
241 if ( m_popupmenu ){
242 delete m_popupmenu;
243 m_popupmenu = NULL;
245 m_popupmenu = new KMenu(0);
247 bool res;
248 res = parseXmlFile(xmlID);
250 if ( res ){
251 return m_popupmenu;
252 } else {
253 delete m_popupmenu;
254 m_popupmenu = NULL;
255 return NULL;
260 void ContextMenuHandler::registerPopupMenu(ListViewInterface *item)
262 if ( !item )
263 return;
265 m_item = item;
267 QString xmlID = item->getXmlContextName();
268 if ( xmlID.isNull() )
269 return;
271 // delete old popup menu
272 if ( m_popupmenu ){
273 delete m_popupmenu;
274 m_popupmenu = NULL;
277 // if m_popupmenu == NULL, the parser should not create a popupmenu
278 parseXmlFile(xmlID);
282 bool ContextMenuHandler::parseXmlFile(const QString &xmlID)
284 kdDebug(100200) << "ContextMenuHandler::parseXmlFile()" << endl;
285 kdDebug(100200) << "XML-File: " << m_XmlFilePath << endl;
287 QFile xmlFile( m_XmlFilePath );
288 QXmlSimpleReader parser;
289 ContextActionHandler handler(this);
290 handler.setSearchID(xmlID);
291 parser.setContentHandler( &handler );
292 return parser.parse( &xmlFile );
296 KAction* ContextMenuHandler::ActionFactory( const QString &actionName, const QString &qty )
298 bool enabled;
299 if ( qty.toLower() == "n" ){
300 // unlimited don't check return enabled action
301 enabled = true;
302 } else {
303 // check if qty is within allowed limits
304 ListViewInterface *i = static_cast<ListViewInterface*>(m_item->firstChild());
305 int count = 0;
306 QString name = QString::null;
307 while (i){
308 name = i->getXmlContextName();
309 if ( name.toLower() == actionName.toLower() ){
310 count++;
312 i = static_cast<ListViewInterface*>(i->nextSibling());
314 enabled = count < qty.toInt() ? true : false;
317 if ( actionName == "Rename" ){
318 renameItem->setEnabled( enabled );
319 return renameItem;
320 } else if ( actionName == "Delete" ){
321 deleteItem->setEnabled( enabled );
322 return deleteItem;
323 } else if ( actionName == "BookInfo" ){
324 newBookInfo->setEnabled( enabled );
325 return newBookInfo;
326 } else if ( actionName == "Chapter" ){
327 newChapter->setEnabled( enabled );
328 return newChapter;
329 } else if ( actionName == "KeywordSet" ){
330 newKeywordSet->setEnabled( enabled );
331 return newKeywordSet;
332 } else if ( actionName == "Keyword" ){
333 newKeyword->setEnabled( enabled );
334 return newKeyword;
335 } else if ( actionName == "Abstract" ){
336 newAbstract->setEnabled( enabled );
337 return newAbstract;
338 } else if ( actionName == "AuthorGroup" ){
339 newAuthorGroup->setEnabled( enabled );
340 return newAuthorGroup;
341 } else if ( actionName == "Author" ){
342 newAuthor->setEnabled( enabled );
343 return newAuthor;
344 } else if ( actionName == "Date" ){
345 newDate->setEnabled( enabled );
346 return newDate;
347 } else if ( actionName == "ReleaseInfo" ){
348 newReleaseInfo->setEnabled( enabled );
349 return newReleaseInfo;
350 } else if ( actionName == "Title" ){
351 newTitle->setEnabled( enabled );
352 return newTitle;
353 } else if ( actionName == "Paragraph" ){
354 newParagraph->setEnabled( enabled );
355 return newParagraph;
356 } else if ( actionName == "Section_1" ){
357 newSection_1->setEnabled( enabled );
358 return newSection_1;
359 } else if ( actionName == "Section_2" ){
360 newSection_2->setEnabled( enabled );
361 return newSection_2;
362 } else if ( actionName == "Section_3" ){
363 newSection_3->setEnabled( enabled );
364 return newSection_3;
365 } else if ( actionName == "Section_4" ){
366 newSection_4->setEnabled( enabled );
367 return newSection_4;
368 } else if ( actionName == "Section_5" ){
369 newSection_5->setEnabled( enabled );
370 return newSection_5;
372 kdDebug(100200) << "ERROR!!! ContextMenuHandler::ActionFactory()" << endl;
373 return NULL;
377 KMenu* ContextMenuHandler::SubMenuFactory(KMenu *parent)
379 return new KMenu(parent);
383 void ContextMenuHandler::setItemEditable( bool editable )
385 if ( m_item )
386 m_item->setEditable( editable );
391 #include "contextmenuhandler.moc"