2 // C++ Implementation: %{MODULE}
7 // Author: %{AUTHOR} <%{EMAIL}>, (C) %{YEAR}
9 // Copyright: See COPYING file that comes with this distribution
14 #include <QtCore/QFile>
20 #include <kstandarddirs.h>
22 // App specific includes
24 #include "contextmenuhandler.h"
29 ContextActionHandler::ContextActionHandler(ContextMenuHandler
*menuhandler
)
30 : QXmlDefaultHandler(), m_menuhandler(menuhandler
)
35 m_popup
= menuhandler
->m_popupmenu
;
40 ContextActionHandler::~ContextActionHandler()
45 void ContextActionHandler::setSearchID( const QString xmlID
)
51 bool ContextActionHandler::startElement( const QString
&,
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 );
66 if ( !(m_hit
&& m_menuhandler
->m_popupmenu
) )
69 if ( qName
== "Action" ){
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
);
82 bool ContextActionHandler::endElement( const QString
&,
84 const QString
&qName
)
86 if ( !(m_hit
&& m_menuhandler
->m_popupmenu
) )
89 if ( qName
== "Action" ){
90 KAction
* newAction
= m_menuhandler
->ActionFactory(m_actionName
, m_qty
);
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
;
104 bool ContextActionHandler::characters( const QString
&ch
)
115 ContextMenuHandler::ContextMenuHandler(QObject
*parent
, const char *name
)
116 : QObject(parent
, name
), m_DocTreeView(parent
)
122 m_XmlFilePath
= KGlobal::dirs()->findResource("data", "ksayit/ContextMenus.xml");
126 ContextMenuHandler::~ContextMenuHandler()
133 delete newKeywordSet
;
136 delete newAuthorGroup
;
139 delete newReleaseInfo
;
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
)
239 QString xmlID
= item
->getXmlContextName();
240 if ( xmlID
.isNull() )
243 // delete old popup menu and create new one
248 m_popupmenu
= new KMenu(0);
251 res
= parseXmlFile(xmlID
);
263 void ContextMenuHandler::registerPopupMenu(ListViewInterface
*item
)
270 QString xmlID
= item
->getXmlContextName();
271 if ( xmlID
.isNull() )
274 // delete old popup menu
280 // if m_popupmenu == NULL, the parser should not create a popupmenu
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
)
302 if ( qty
.toLower() == "n" ){
303 // unlimited don't check return enabled action
306 // check if qty is within allowed limits
307 ListViewInterface
*i
= static_cast<ListViewInterface
*>(m_item
->firstChild());
309 QString name
= QString();
311 name
= i
->getXmlContextName();
312 if ( name
.toLower() == actionName
.toLower() ){
315 i
= static_cast<ListViewInterface
*>(i
->nextSibling());
317 enabled
= count
< qty
.toInt() ? true : false;
320 if ( actionName
== "Rename" ){
321 renameItem
->setEnabled( enabled
);
323 } else if ( actionName
== "Delete" ){
324 deleteItem
->setEnabled( enabled
);
326 } else if ( actionName
== "BookInfo" ){
327 newBookInfo
->setEnabled( enabled
);
329 } else if ( actionName
== "Chapter" ){
330 newChapter
->setEnabled( enabled
);
332 } else if ( actionName
== "KeywordSet" ){
333 newKeywordSet
->setEnabled( enabled
);
334 return newKeywordSet
;
335 } else if ( actionName
== "Keyword" ){
336 newKeyword
->setEnabled( enabled
);
338 } else if ( actionName
== "Abstract" ){
339 newAbstract
->setEnabled( enabled
);
341 } else if ( actionName
== "AuthorGroup" ){
342 newAuthorGroup
->setEnabled( enabled
);
343 return newAuthorGroup
;
344 } else if ( actionName
== "Author" ){
345 newAuthor
->setEnabled( enabled
);
347 } else if ( actionName
== "Date" ){
348 newDate
->setEnabled( enabled
);
350 } else if ( actionName
== "ReleaseInfo" ){
351 newReleaseInfo
->setEnabled( enabled
);
352 return newReleaseInfo
;
353 } else if ( actionName
== "Title" ){
354 newTitle
->setEnabled( enabled
);
356 } else if ( actionName
== "Paragraph" ){
357 newParagraph
->setEnabled( enabled
);
359 } else if ( actionName
== "Section_1" ){
360 newSection_1
->setEnabled( enabled
);
362 } else if ( actionName
== "Section_2" ){
363 newSection_2
->setEnabled( enabled
);
365 } else if ( actionName
== "Section_3" ){
366 newSection_3
->setEnabled( enabled
);
368 } else if ( actionName
== "Section_4" ){
369 newSection_4
->setEnabled( enabled
);
371 } else if ( actionName
== "Section_5" ){
372 newSection_5
->setEnabled( enabled
);
375 kDebug(100200) << "ERROR!!! ContextMenuHandler::ActionFactory()";
380 KMenu
* ContextMenuHandler::SubMenuFactory(KMenu
*parent
)
382 return new KMenu(parent
);
386 void ContextMenuHandler::setItemEditable( bool editable
)
389 m_item
->setEditable( editable
);
394 #include "contextmenuhandler.moc"