summaryrefslogtreecommitdiffstats
path: root/scribus/menumanager.cpp
blob: 4c3625043ca2d14b4833483897a29a241171fed5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
For general Scribus (>=1.3.2) copyright and licensing information please refer
to the COPYING file provided with the program. Following this notice may exist
a copyright and/or license notice that predates the release of Scribus 1.3.2
for which a new license (GPL+exception) is in place.
*/
/***************************************************************************
    begin                : Jan 2005
    copyright            : (C) 2005 by Craig Bradney
    email                : cbradney@zip.com.au
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include <QMenuBar>
#include <QMenu>
#include "scribus.h"
#include "menumanager.h"
#include "scmenu.h"
#include "scraction.h"
#include <QDebug>


MenuManager::MenuManager(QMenuBar* mb, QObject *parent) : QObject(parent)
{
	scribusMenuBar=mb;
	menuList.clear();
}

MenuManager::~MenuManager()
{
}

bool MenuManager::createMenu(const QString &menuName, const QString &menuText, const QString parent, bool checkable)
{
	bool retVal=false;
		
	ScrPopupMenu *newMenu = new ScrPopupMenu(NULL, menuName, menuText, parent);
	if (newMenu)
	{
		menuList.insert(menuName, newMenu);
		if (!parent.isNull() && menuList[parent])
			retVal=menuList[parent]->insertSubMenu(newMenu);
	}
	return retVal;
}

bool MenuManager::addMenuToMenu(const QString &child, const QString &parent)
{
	bool retVal=false;
	if (child==parent)
	{
		qDebug("%s", QString("Cannot add %1 menu to %2 menu (itself)").arg(child, parent).toAscii().constData());
		return false;	
	}
	if ((menuList.contains(child) && menuList[child]!=NULL) &&
		(menuList.contains(parent) && menuList[parent]!=NULL))
	{
		menuList.insert(child, menuList[child]);
		retVal=menuList[parent]->insertSubMenu(menuList[child]);
	}
	
	return retVal;
}

bool MenuManager::clearMenu(const QString &menuName)
{
	bool retVal=false;
	if (menuList.contains(menuName) && menuList[menuName]!=NULL)
	{
		menuList[menuName]->clear();
		retVal=true;
	}
	return retVal;
}

void MenuManager::setText(const QString &menuName, const QString &menuText)
{
	if (menuList.contains(menuName) && menuList[menuName]!=NULL)
	{
		menuList[menuName]->setText(menuText);
		QString parent=menuList[menuName]->getParentMenuName();
		if (!parent.isNull())
			menuList[parent]->repopulateLocalMenu();
		
		/*
		
		int id = menuList[menuName]->getMenuBarID();
		if (id!=-1)
// 			scribusMenuBar->actions()[id]->setText(menuText);
		{
			QIcon menuIcon = menuList[menuName]->getMenuIcon();
			scribusMenuBar->changeItem(id, menuIcon, menuText);
		}*/
	}
}

void MenuManager::setMenuIcon(const QString &menuName, const QIcon &menuIcon)
{
	if (menuList.contains(menuName) && menuList[menuName]!=NULL)
	{
		menuList[menuName]->setMenuIcon(menuIcon);
		QString parent=menuList[menuName]->getParentMenuName();
		if (!parent.isNull())
			menuList[parent]->repopulateLocalMenu();
		/*
		int id=menuList[menuName]->getMenuBarID();
		if (id!=-1)
// 			scribusMenuBar->actions()[id]->setIcon(menuIcon);
		{
			QString menuText = menuList[menuName]->getMenuText();
			scribusMenuBar->changeItem(id, menuIcon, menuText);
		}
		*/
	}
}

QMenu *MenuManager::getLocalPopupMenu(const QString &menuName)
{
	if (menuList.contains(menuName) && menuList[menuName]!=NULL)
		return menuList[menuName]->getLocalPopupMenu();
	return NULL;
}

bool MenuManager::deleteMenu(const QString &menuName, const QString &parent)
{
	if (!parent.isNull())
	{
		if (menuList[parent] && menuList[parent]->hasSubMenu(menuList[menuName]))
			menuList[parent]->removeSubMenu(menuList[menuName]);
	}
	menuList.remove(menuName);
	return true;
}

void MenuManager::setMenuEnabled(const QString &menuName, const bool enabled)
{
	if (menuList.contains(menuName) && menuList[menuName]!=NULL)
	{
		menuList[menuName]->setEnabled(enabled);
		/*
		int mainID=menuList[menuName]->getMenuBarID();
		if (mainID!=-1)
// 			scribusMenuBar->actions()[mainID]->setEnabled(enabled);
			scribusMenuBar->setItemEnabled(mainID, enabled);
		*/
	}
}

bool MenuManager::addMenuToMenuBar(const QString &menuName)
{
	bool retVal=false;
	if (menuList.contains(menuName) && menuList[menuName]!=NULL)
	{
// 		qDebug() << "addMenuToMenuBar:" << menuName << " with text " << menuList[menuName]->getMenuText();
		QAction* t=scribusMenuBar->addMenu(menuList[menuName]->getLocalPopupMenu());
		if (t!=NULL)
		{
			t->setText(menuList[menuName]->getMenuText());
// 			qDebug() << "t" << t->text();
			retVal=true;
		}
		/*
		int id=scribusMenuBar->insertItem( menuList[menuName]->getMenuIcon(), menuList[menuName]->getMenuText(), menuList[menuName]->getLocalPopupMenu());
// 		menuList[menuName]->setMenuBarID(id);
		*/
	}
	return retVal;
}

bool MenuManager::addMenuToMenuBarBefore(const QString &menuName, const QString &afterMenuName)
{
	bool retVal=false;
	if (menuList.contains(menuName) && menuList[menuName]!=NULL)
	{
		if (menuList[afterMenuName])
		{
// 			qDebug() << "addMenuToMenuBarAfter:" << menuName << " with text " << menuList[menuName]->getMenuText() << " after " << menuList[afterMenuName]->getMenuText();
			QAction* t=scribusMenuBar->insertMenu(menuList[afterMenuName]->getLocalPopupMenu()->menuAction(), menuList[menuName]->getLocalPopupMenu());
			if (t!=NULL)
			{
				t->setText(menuList[menuName]->getMenuText());
// 				qDebug() << "t" << t->text();
				retVal=true;
			}
			/*
			int afterID=menuList[afterMenuName]->getMenuBarID();
			if (afterID!=-1)
			{
				int id=scribusMenuBar->insertItem( menuList[menuName]->getMenuIcon(), menuList[menuName]->getMenuText(), menuList[menuName]->getLocalPopupMenu(), scribusMenuBar->indexOf(afterID)+1, scribusMenuBar->indexOf(afterID)+1);
// 				menuList[menuName]->setMenuBarID(id);
				retVal=true;
			}*/
		}
	}
	return retVal;
}

bool MenuManager::removeMenuFromMenuBar(const QString &menuName)
{
	bool retVal=false;
	if (menuList.contains(menuName) && menuList[menuName]!=NULL)
	{
		if (menuList[menuName]->getLocalPopupMenu()->menuAction())
			scribusMenuBar->removeAction(menuList[menuName]->getLocalPopupMenu()->menuAction());
		/*
		int id=menuList[menuName]->getMenuBarID();
		if (id!=-1)
			scribusMenuBar->removeItem( id );
		*/
		retVal=true;
	}
	return retVal;
}

bool MenuManager::addMenuToWidgetOfAction(const QString &menuName, ScrAction *action)
{
	bool retVal=false;
	if (menuList.contains(menuName) && menuList[menuName]!=NULL && action!=NULL)
	{
		/* //qt4 cb replace with qwidgetaction or similar
		QWidget *w=action->getWidgetAddedTo();
		if (w)
		{
			QString menuItemListClassName=w->className();
			if (menuItemListClassName=="QToolButton")
			{
				QToolButton *toolButton=dynamic_cast<QToolButton *>(w);
				if (toolButton!=NULL)
				{
					toolButton->setPopup(menuList[menuName]->getLocalPopupMenu());
					retVal=true;
				}
			}
		}
		*/
	}
	return retVal;
}

bool MenuManager::addMenuItem(ScrAction *menuAction, const QString &parent)
{
	bool retVal=false;
	if (menuList.contains(parent) && menuList[parent]!=NULL)
		retVal=menuList[parent]->insertMenuItem(menuAction);
	return retVal;
}

/* Qt4
bool MenuManager::addMenuItem(QWidget *widget, const QString &parent)
{
	bool retVal=false;
	if (menuList.contains(parent) && menuList[parent]!=NULL)
		retVal=menuList[parent]->insertMenuItem(widget);
	return retVal;
}
*/

bool MenuManager::addMenuItemAfter(ScrAction *menuAction, const QString &parent, ScrAction *afterMenuAction)
{
	bool retVal=false;
	if (menuList.contains(parent) && menuList[parent]!=NULL)
		retVal=menuList[parent]->insertMenuItemAfter(menuAction, afterMenuAction);
	return retVal;
}

// bool MenuManager::addMenuItemAfter(ScrAction *menuAction, const QString &parent, const QString &afterMenuName)
// {
// 	ScrAction *actionFromName=NULL;
// 	//quick hack to make this work for existing plugins for now
// // 	if (parent=="File" && afterMenuName=="New")
// // 		actionFromName=m_ScMW->scrActions["fileNew"];
// // 	if (parent=="File" && afterMenuName=="Print")
// // 		actionFromName=m_ScMW->scrActions["filePrint"];
// // 	if (parent=="File" && afterMenuName=="SaveAs")
// // 		actionFromName=m_ScMW->scrActions["fileSaveAs"];
// // 	if (parent=="Help" && afterMenuName=="Manual")
// // 		actionFromName=m_ScMW->scrActions["helpManual"];
// 	bool retVal=false;
// 	if (menuList.contains(parent) && menuList[parent]!=NULL)
// 		retVal=menuList[parent]->insertMenuItemAfter(menuAction, actionFromName);
// 	return retVal;
// }


bool MenuManager::addMenuSeparator(const QString &parent)
{
	bool retVal=false;
	if (menuList.contains(parent) && menuList[parent]!=NULL)
		retVal=menuList[parent]->insertMenuSeparator();
	return retVal;
	
}

bool MenuManager::removeMenuItem(ScrAction *menuAction, const QString &parent)
{
	bool retVal=false;
	if (menuList.contains(parent) && menuList[parent]!=NULL)
		retVal=menuList[parent]->removeMenuItem(menuAction);
	return retVal;
}

void MenuManager::runMenuAtPos(const QString &menuName, const QPoint position)
{
	if (menuList.contains(menuName) && menuList[menuName]!=NULL)
	{	
		QMenu *popupmenu=menuList[menuName]->getLocalPopupMenu();
		if (popupmenu)
			popupmenu->exec(position);
	}
}

// Used to generate a list of entries from the menu system into the keyboard shortcut manager
void MenuManager::generateKeyManList(QStringList *actionNames)
{
	if (actionNames)
	{
		if (scribusMenuBar)
		{
			/*
			QMap<QString, ScrPopupMenu *>::Iterator menuListIt;
			for (uint menuBarCount=0; menuBarCount<scribusMenuBar->count(); ++menuBarCount)
			{
				int menuBarMenuID=scribusMenuBar->idAt(menuBarCount);
				bool menuBarItemFound=false;
				for ( menuListIt = menuList.begin(); menuListIt!=menuList.end(); ++menuListIt)
				{
					if(menuListIt.value()->getMenuBarID()==menuBarMenuID)
					{
						menuBarItemFound=true;
						break;
					}
				}
				if (menuBarItemFound)
				{
					if (menuListIt.value())
					{
						ScrPopupMenu *currentMenu=menuListIt.value();
						currentMenu->generateEntryList(actionNames);
					}
				}
			}*/
		}
	}
}

bool MenuManager::empty()
{
	return menuList.empty();
}

bool MenuManager::menuExists(const QString &menuName)
{
	return menuList.contains(menuName);
}