2 // C++ Implementation: %{MODULE}
7 // Author: %{AUTHOR} <%{EMAIL}>, (C) %{YEAR}
9 // Copyright: See COPYING file that comes with this distribution
20 #include <kstandarddirs.h>
22 // App specific includes
24 #include "contextmenuhandler.h"
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
;
38 m_searchID
= QString::null
;
41 ContextActionHandler::~ContextActionHandler()
46 void ContextActionHandler::setSearchID( const QString xmlID
)
52 bool ContextActionHandler::startElement( const QString
&,
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 );
67 if ( !(m_hit
&& m_menuhandler
->m_popupmenu
) )
70 if ( qName
== "Action" ){
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
);
83 bool ContextActionHandler::endElement( const QString
&,
85 const QString
&qName
)
87 if ( !(m_hit
&& m_menuhandler
->m_popupmenu
) )
90 if ( qName
== "Action" ){
91 KAction
* newAction
= m_menuhandler
->ActionFactory(m_actionName
, m_qty
);
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
;
105 bool ContextActionHandler::characters( const QString
&ch
)
116 ContextMenuHandler::ContextMenuHandler(QObject
*parent
, const char *name
)
117 : QObject(parent
, name
), m_DocTreeView(parent
)
122 m_XmlFilePath
= KGlobal::dirs()->findResource("data", "ksayit/ContextMenus.xml");
126 ContextMenuHandler::~ContextMenuHandler()
132 delete newKeywordSet
;
135 delete newAuthorGroup
;
138 delete newReleaseInfo
;
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
)
236 QString xmlID
= item
->getXmlContextName();
237 if ( xmlID
.isNull() )
240 // delete old popup menu and create new one
245 m_popupmenu
= new KMenu(0);
248 res
= parseXmlFile(xmlID
);
260 void ContextMenuHandler::registerPopupMenu(ListViewInterface
*item
)
267 QString xmlID
= item
->getXmlContextName();
268 if ( xmlID
.isNull() )
271 // delete old popup menu
277 // if m_popupmenu == NULL, the parser should not create a popupmenu
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
)
299 if ( qty
.toLower() == "n" ){
300 // unlimited don't check return enabled action
303 // check if qty is within allowed limits
304 ListViewInterface
*i
= static_cast<ListViewInterface
*>(m_item
->firstChild());
306 QString name
= QString::null
;
308 name
= i
->getXmlContextName();
309 if ( name
.toLower() == actionName
.toLower() ){
312 i
= static_cast<ListViewInterface
*>(i
->nextSibling());
314 enabled
= count
< qty
.toInt() ? true : false;
317 if ( actionName
== "Rename" ){
318 renameItem
->setEnabled( enabled
);
320 } else if ( actionName
== "Delete" ){
321 deleteItem
->setEnabled( enabled
);
323 } else if ( actionName
== "BookInfo" ){
324 newBookInfo
->setEnabled( enabled
);
326 } else if ( actionName
== "Chapter" ){
327 newChapter
->setEnabled( enabled
);
329 } else if ( actionName
== "KeywordSet" ){
330 newKeywordSet
->setEnabled( enabled
);
331 return newKeywordSet
;
332 } else if ( actionName
== "Keyword" ){
333 newKeyword
->setEnabled( enabled
);
335 } else if ( actionName
== "Abstract" ){
336 newAbstract
->setEnabled( enabled
);
338 } else if ( actionName
== "AuthorGroup" ){
339 newAuthorGroup
->setEnabled( enabled
);
340 return newAuthorGroup
;
341 } else if ( actionName
== "Author" ){
342 newAuthor
->setEnabled( enabled
);
344 } else if ( actionName
== "Date" ){
345 newDate
->setEnabled( enabled
);
347 } else if ( actionName
== "ReleaseInfo" ){
348 newReleaseInfo
->setEnabled( enabled
);
349 return newReleaseInfo
;
350 } else if ( actionName
== "Title" ){
351 newTitle
->setEnabled( enabled
);
353 } else if ( actionName
== "Paragraph" ){
354 newParagraph
->setEnabled( enabled
);
356 } else if ( actionName
== "Section_1" ){
357 newSection_1
->setEnabled( enabled
);
359 } else if ( actionName
== "Section_2" ){
360 newSection_2
->setEnabled( enabled
);
362 } else if ( actionName
== "Section_3" ){
363 newSection_3
->setEnabled( enabled
);
365 } else if ( actionName
== "Section_4" ){
366 newSection_4
->setEnabled( enabled
);
368 } else if ( actionName
== "Section_5" ){
369 newSection_5
->setEnabled( enabled
);
372 kdDebug(100200) << "ERROR!!! ContextMenuHandler::ActionFactory()" << endl
;
377 KMenu
* ContextMenuHandler::SubMenuFactory(KMenu
*parent
)
379 return new KMenu(parent
);
383 void ContextMenuHandler::setItemEditable( bool editable
)
386 m_item
->setEditable( editable
);
391 #include "contextmenuhandler.moc"