Remove this line
[kdeaccessibility.git] / ksayit / src / contextmenuhandler.cpp
bloba47d286aa8e8ebd7e4d8448b8fecb97f1a981b5f
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 <QtCore/QFile>
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"
26 /**
27 * XML-ActionHandler
29 ContextActionHandler::ContextActionHandler(ContextMenuHandler *menuhandler)
30 : QXmlDefaultHandler(), m_menuhandler(menuhandler)
32 m_subName.clear();
33 m_actionName.clear();
34 m_qty.clear();
35 m_popup = menuhandler->m_popupmenu;
36 m_hit = false;
37 m_searchID.clear();
40 ContextActionHandler::~ContextActionHandler()
45 void ContextActionHandler::setSearchID( const QString xmlID )
47 m_searchID = xmlID;
51 bool ContextActionHandler::startElement( const QString &,
52 const QString &,
53 const QString &qName,
54 const QXmlAttributes &atts )
56 if ( qName == "Item" ){
57 if ( atts.value("id") == m_searchID ){
58 if ( atts.value("editable") == "yes"){
59 m_menuhandler->setItemEditable( true );
61 m_hit = true;
62 } else {
63 m_hit = false;
66 if ( !(m_hit && m_menuhandler->m_popupmenu) )
67 return true;
69 if ( qName == "Action" ){
70 m_actionName = "";
71 m_qty = atts.value("qty");
72 } else if ( qName == "Submenu" ){
73 if ( !atts.value("name").isEmpty() ){
74 m_subName = atts.value("name");
75 m_popup = m_menuhandler->SubMenuFactory(m_popup);
78 return true;
82 bool ContextActionHandler::endElement( const QString &,
83 const QString &,
84 const QString &qName )
86 if ( !(m_hit && m_menuhandler->m_popupmenu) )
87 return true;
89 if ( qName == "Action" ){
90 KAction* newAction = m_menuhandler->ActionFactory(m_actionName, m_qty);
91 if ( newAction )
92 newAction->plug( m_popup );
93 } else if ( qName == "Submenu" ){
94 if ( m_subName == "New" ){
95 m_menuhandler->m_popupmenu->insertItem( i18n("New"), m_popup);
97 // only one sublevel spported -> back to toplevel menu
98 m_popup = m_menuhandler->m_popupmenu;
100 return true;
104 bool ContextActionHandler::characters( const QString &ch )
106 m_actionName += ch;
107 return true;
113 * MenuHandler
115 ContextMenuHandler::ContextMenuHandler(QObject *parent, const char *name)
116 : QObject(parent, name), m_DocTreeView(parent)
118 m_popupmenu = NULL;
119 m_item = NULL;
120 m_ac = NULL;
121 initActions();
122 m_XmlFilePath = KGlobal::dirs()->findResource("data", "ksayit/ContextMenus.xml");
126 ContextMenuHandler::~ContextMenuHandler()
128 delete m_ac;
129 delete renameItem;
130 delete deleteItem;
131 delete newBookInfo;
132 delete newChapter;
133 delete newKeywordSet;
134 delete newKeyword;
135 delete newAbstract;
136 delete newAuthorGroup;
137 delete newAuthor;
138 delete newDate;
139 delete newReleaseInfo;
140 delete newTitle;
141 delete newParagraph;
142 delete newSection_1;
143 delete newSection_2;
144 delete newSection_3;
145 delete newSection_4;
146 delete newSection_5;
148 if (m_popupmenu)
149 delete m_popupmenu;
153 void ContextMenuHandler::initActions()
155 KActionCollection *m_ac = new KActionCollection(this);
157 // User defined actions
158 renameItem = new KAction (i18n("Rename..."), m_ac, "renameItem");
159 connect(renameItem, SIGNAL(triggered(bool)),
160 m_DocTreeView, SLOT(slotRenameItem()));
162 deleteItem = new KAction (i18n("Delete..."), m_ac, "deleteItem");
163 connect(deleteItem, SIGNAL(triggered(bool)),
164 m_DocTreeView, SLOT(slotDeleteItem()));
166 newBookInfo = new KAction ( i18n("Overview"), m_ac, "newBookInfo");
167 connect(newBookInfo, SIGNAL(triggered(bool)),
168 m_DocTreeView, SLOT(slotNewBookInfo()));
170 newChapter = new KAction ( i18n("Chapter"), m_ac, "newChapter");
171 connect(newChapter, SIGNAL(triggered(bool)),
172 m_DocTreeView, SLOT(slotNewChapter()));
174 newKeywordSet = new KAction ( i18n("Keywords"), m_ac, "newKeywordSet");
175 connect(newKeywordSet, SIGNAL(triggered(bool)),
176 m_DocTreeView, SLOT(slotNewKeywordSet()));
178 newKeyword = new KAction ( i18n("Keyword"), m_ac, "newKeyword");
179 connect(newKeyword, SIGNAL(triggered(bool)),
180 m_DocTreeView, SLOT(slotNewKeyword()));
182 newAbstract = new KAction ( i18n("Abstract"), m_ac, "newAbstract");
183 connect(newAbstract, SIGNAL(triggered(bool)),
184 m_DocTreeView, SLOT(slotNewAbstract()));
186 newAuthorGroup = new KAction ( i18n("Authors"), m_ac, "newAuthorGroup");
187 connect(newAuthorGroup, SIGNAL(triggered(bool)),
188 m_DocTreeView, SLOT(slotNewAuthorGroup()));
190 newAuthor = new KAction ( i18n("Author"), m_ac, "newAuthor");
191 connect(newAuthor, SIGNAL(triggered(bool)),
192 m_DocTreeView, SLOT(slotNewAuthor()));
194 newDate = new KAction ( i18n("Date"), m_ac, "newDate");
195 connect(newDate, SIGNAL(triggered(bool)),
196 m_DocTreeView, SLOT(slotNewDate()));
198 newReleaseInfo = new KAction ( i18n("Release Info"), m_ac, "newReleaseInfo");
199 connect(newReleaseInfo, SIGNAL(triggered(bool)),
200 m_DocTreeView, SLOT(slotNewReleaseInfo()));
202 newTitle = new KAction ( i18n("Title"), m_ac, "newTitle");
203 connect(newTitle, SIGNAL(triggered(bool)),
204 m_DocTreeView, SLOT(slotNewTitle()));
206 newParagraph = new KAction ( i18n("Paragraph"), m_ac, "newParagraph");
207 connect(newParagraph, SIGNAL(triggered(bool)),
208 m_DocTreeView, SLOT(slotNewParagraph()));
210 newSection_1 = new KAction ( i18n("Section Level 1"), m_ac, "newSection_1");
211 connect(newSection_1, SIGNAL(triggered(bool)),
212 m_DocTreeView, SLOT(slotNewSection_1()));
214 newSection_2 = new KAction ( i18n("Section Level 2"), m_ac, "newSection_2");
215 connect(newSection_2, SIGNAL(triggered(bool)),
216 m_DocTreeView, SLOT(slotNewSection_2()));
218 newSection_3 = new KAction ( i18n("Section Level 3"), m_ac, "newSection_3");
219 connect(newSection_3, SIGNAL(triggered(bool)),
220 m_DocTreeView, SLOT(slotNewSection_3()));
222 newSection_4 = new KAction ( i18n("Section Level 4"), m_ac, "newSection_4");
223 connect(newSection_4, SIGNAL(triggered(bool)),
224 m_DocTreeView, SLOT(slotNewSection_4()));
226 newSection_5 = new KAction ( i18n("Section Level 5"), m_ac, "newSection_5");
227 connect(newSection_5, SIGNAL(triggered(bool)),
228 m_DocTreeView, SLOT(slotNewSection_5()));
232 KMenu* ContextMenuHandler::getPopupMenu(ListViewInterface *item)
234 if ( !item )
235 return NULL;
237 m_item = item;
239 QString xmlID = item->getXmlContextName();
240 if ( xmlID.isNull() )
241 return NULL;
243 // delete old popup menu and create new one
244 if ( m_popupmenu ){
245 delete m_popupmenu;
246 m_popupmenu = NULL;
248 m_popupmenu = new KMenu(0);
250 bool res;
251 res = parseXmlFile(xmlID);
253 if ( res ){
254 return m_popupmenu;
255 } else {
256 delete m_popupmenu;
257 m_popupmenu = NULL;
258 return NULL;
263 void ContextMenuHandler::registerPopupMenu(ListViewInterface *item)
265 if ( !item )
266 return;
268 m_item = item;
270 QString xmlID = item->getXmlContextName();
271 if ( xmlID.isNull() )
272 return;
274 // delete old popup menu
275 if ( m_popupmenu ){
276 delete m_popupmenu;
277 m_popupmenu = NULL;
280 // if m_popupmenu == NULL, the parser should not create a popupmenu
281 parseXmlFile(xmlID);
285 bool ContextMenuHandler::parseXmlFile(const QString &xmlID)
287 kDebug(100200) << "ContextMenuHandler::parseXmlFile()";
288 kDebug(100200) << "XML-File: " << m_XmlFilePath;
290 QFile xmlFile( m_XmlFilePath );
291 QXmlSimpleReader parser;
292 ContextActionHandler handler(this);
293 handler.setSearchID(xmlID);
294 parser.setContentHandler( &handler );
295 return parser.parse( &xmlFile );
299 KAction* ContextMenuHandler::ActionFactory( const QString &actionName, const QString &qty )
301 bool enabled;
302 if ( qty.toLower() == "n" ){
303 // unlimited don't check return enabled action
304 enabled = true;
305 } else {
306 // check if qty is within allowed limits
307 ListViewInterface *i = static_cast<ListViewInterface*>(m_item->firstChild());
308 int count = 0;
309 QString name = QString();
310 while (i){
311 name = i->getXmlContextName();
312 if ( name.toLower() == actionName.toLower() ){
313 count++;
315 i = static_cast<ListViewInterface*>(i->nextSibling());
317 enabled = count < qty.toInt() ? true : false;
320 if ( actionName == "Rename" ){
321 renameItem->setEnabled( enabled );
322 return renameItem;
323 } else if ( actionName == "Delete" ){
324 deleteItem->setEnabled( enabled );
325 return deleteItem;
326 } else if ( actionName == "BookInfo" ){
327 newBookInfo->setEnabled( enabled );
328 return newBookInfo;
329 } else if ( actionName == "Chapter" ){
330 newChapter->setEnabled( enabled );
331 return newChapter;
332 } else if ( actionName == "KeywordSet" ){
333 newKeywordSet->setEnabled( enabled );
334 return newKeywordSet;
335 } else if ( actionName == "Keyword" ){
336 newKeyword->setEnabled( enabled );
337 return newKeyword;
338 } else if ( actionName == "Abstract" ){
339 newAbstract->setEnabled( enabled );
340 return newAbstract;
341 } else if ( actionName == "AuthorGroup" ){
342 newAuthorGroup->setEnabled( enabled );
343 return newAuthorGroup;
344 } else if ( actionName == "Author" ){
345 newAuthor->setEnabled( enabled );
346 return newAuthor;
347 } else if ( actionName == "Date" ){
348 newDate->setEnabled( enabled );
349 return newDate;
350 } else if ( actionName == "ReleaseInfo" ){
351 newReleaseInfo->setEnabled( enabled );
352 return newReleaseInfo;
353 } else if ( actionName == "Title" ){
354 newTitle->setEnabled( enabled );
355 return newTitle;
356 } else if ( actionName == "Paragraph" ){
357 newParagraph->setEnabled( enabled );
358 return newParagraph;
359 } else if ( actionName == "Section_1" ){
360 newSection_1->setEnabled( enabled );
361 return newSection_1;
362 } else if ( actionName == "Section_2" ){
363 newSection_2->setEnabled( enabled );
364 return newSection_2;
365 } else if ( actionName == "Section_3" ){
366 newSection_3->setEnabled( enabled );
367 return newSection_3;
368 } else if ( actionName == "Section_4" ){
369 newSection_4->setEnabled( enabled );
370 return newSection_4;
371 } else if ( actionName == "Section_5" ){
372 newSection_5->setEnabled( enabled );
373 return newSection_5;
375 kDebug(100200) << "ERROR!!! ContextMenuHandler::ActionFactory()";
376 return NULL;
380 KMenu* ContextMenuHandler::SubMenuFactory(KMenu *parent)
382 return new KMenu(parent);
386 void ContextMenuHandler::setItemEditable( bool editable )
388 if ( m_item )
389 m_item->setEditable( editable );
394 #include "contextmenuhandler.moc"