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
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/*
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 <QObject>
#include <QMetaObject>
#include <QMenu>
#include <QList>
#include "scmenu.h"
#include "scraction.h"
#include "util.h"
#include "util_icon.h"
ScrPopupMenu::ScrPopupMenu ( QWidget * parent, const QString pMenuName, const QString pMenuText, const QString parentName, bool pCheckable ) : QObject(parent)
{
parentMenuName=parentName;
parentMenuID=-1;
menuText=pMenuText;
menuName=pMenuName;
menuBarID=-1;
menuItemList.clear();
localPopupMenu=new QMenu(parent);//, name);
// localPopupMenu->setCheckable(pCheckable);
enabled=true;
checkable = pCheckable;
}
ScrPopupMenu::~ScrPopupMenu()
{
delete localPopupMenu;
}
const QString ScrPopupMenu::getMenuText()
{
return menuText;
}
void ScrPopupMenu::setText(const QString pMenuText)
{
menuText=pMenuText;
localPopupMenu->menuAction()->setText(menuText);
}
const QIcon ScrPopupMenu::getMenuIcon()
{
return menuIcon;
}
void ScrPopupMenu::setMenuIcon(const QIcon pMenuIcon)
{
menuIcon=pMenuIcon;
}
const QString ScrPopupMenu::getMenuName()
{
return menuName;
}
const QString ScrPopupMenu::getParentMenuName()
{
return parentMenuName;
}
// void ScrPopupMenu::setMenuBarID(int id)
// {
// menuBarID=id;
// }
//
// int ScrPopupMenu::getMenuBarID()
// {
// return menuBarID;
// }
QMenu *ScrPopupMenu::getLocalPopupMenu()
{
return localPopupMenu;
}
bool ScrPopupMenu::hasSubMenu(ScrPopupMenu* subMenu)
{
return menuItemList.contains(QPointer<QObject>(subMenu));
}
// void ScrPopupMenu::setParentMenuID(int id)
// {
// parentMenuID=id;
// }
//
//
// const int ScrPopupMenu::getParentMenuID()
// {
// return parentMenuID;
// }
bool ScrPopupMenu::insertSubMenu(ScrPopupMenu* newSubMenu)
{
if (newSubMenu)
{
menuItemList.append(newSubMenu);
// localPopupMenu->insertItem(newSubMenu->getMenuIcon(), newSubMenu->getMenuText(), newSubMenu->getLocalPopupMenu());
QAction *m=localPopupMenu->addMenu(newSubMenu->getLocalPopupMenu());
if (m)
m->setText(newSubMenu->getMenuText());
return true;
}
else
return false;
}
bool ScrPopupMenu::removeSubMenu(ScrPopupMenu* subMenu)
{
if (menuItemList.removeAll(subMenu))
{
repopulateLocalMenu();
return true;
}
return false;
}
bool ScrPopupMenu::insertMenuItem(ScrAction *newMenuAction)
{
if (newMenuAction)
{
#ifdef Q_WS_MAC
bool menuListHasNoIcons = true;
// look for ScrAction or ScrPopupMenu from the end of the list
QList< QPointer<QObject> >::Iterator it = menuItemList.end();
int s=menuItemList.size()-1;
for (int i=s; i>=0; --i) {
QObject* menuItem = menuItemList[i];
QString menuItemListClassName = menuItemList[i]->metaObject()->className();
if (menuItemListClassName == "ScrAction")
{
ScrAction * act = dynamic_cast<ScrAction *>(menuItem);
if (act!=NULL)
menuListHasNoIcons = act->icon().isNull();
break;
}
else if (menuItemListClassName == "ScrPopupMenu")
{
ScrPopupMenu * men = dynamic_cast<ScrPopupMenu *>(menuItem);
if (men!=NULL)
menuListHasNoIcons = men->getMenuIcon().isNull();
break;
}
}
if (newMenuAction->icon().isNull() && ! menuListHasNoIcons)
newMenuAction->setIcon(loadIcon("noicon.xpm"));
#endif
menuItemList.append(newMenuAction);
localPopupMenu->addAction(newMenuAction);
return true;
}
else
return false;
}
/* Qt4
bool ScrPopupMenu::insertMenuItem(QWidget *widget)
{
if (widget)
{
ScrAction *widgetAction = new ScrAction( NULL, "widget_action" );
menuItemList.append(widgetAction);
localPopupMenu->insertItem(widget);
return true;
}
else
return false;
}
*/
bool ScrPopupMenu::insertMenuItemAfter(ScrAction *newMenuAction, ScrAction *afterMenuAction)
{
//Allow duplicate menu items ???
//if (menuItemList.findRef(newMenuAction)!=-1)
// return false;
#ifdef Q_WS_MAC
if (newMenuAction->icon().isNull() && ! (afterMenuAction->icon().isNull()))
newMenuAction->setIcon(loadIcon("noicon.xpm"));
#endif
int pos=menuItemList.indexOf(QPointer<QObject>(afterMenuAction));
menuItemList.insert(++pos, QPointer<QObject>(newMenuAction));
/*
if (index==-1)
return false;
int newIndex=++index;
bool insSuccess=menuItemList.insert(newIndex, newMenuAction);
if (!insSuccess)
return false;
*/
return repopulateLocalMenu();
}
bool ScrPopupMenu::removeMenuItem(ScrAction *menuAction)
{
if (menuItemList.removeAll(menuAction))
{
repopulateLocalMenu();
return true;
}
return false;
}
/*
bool ScrPopupMenu::insertMenuItemAfter(ScrAction *newMenuAction, const QString afterMenuName)
{
bool retVal=true;
bool found=false;
QPtrListIterator<QObject> menuItemListIt(menuItemList);
QObject *object;
while ( (object = menuItemListIt.current()) != 0 )
{
QString menuItemListClassName=object->className();
int index=-1;
//index=menuItemListIt.at();
if (index=-1)
{
retVal=false;
break;
}
if (menuItemListClassName=="ScrAction")
{
qDebug("ScrAction found");
if(QString(object->name())==QString("separator_action"))
bool blah=true;//localPopupMenu->insertSeparator();
else
{
//Grab the action from the list, break and call the other after insert function
int newIndex=++index;
bool insSuccess=menuItemList.insert(newIndex, newMenuAction);
if (!insSuccess)
return false;
}
}
}
if (!found)
return false;
if (retVal==false)
return false;
return repopulateLocalMenu();
}
*/
//CB TODO Does NOT handle rebuilding with widgets
bool ScrPopupMenu::repopulateLocalMenu()
{
localPopupMenu->clear();
QList< QPointer<QObject> >::Iterator menuItemListIt = menuItemList.begin();
while (menuItemListIt!=menuItemList.end())
{
QObject *listObj=(*menuItemListIt);
if (listObj==NULL)
{
QList< QPointer<QObject> >::Iterator menuItemListItToDelete = menuItemListIt;
++menuItemListIt;
menuItemList.removeAll(*menuItemListItToDelete);
continue;
}
QString menuItemListClassName=listObj->metaObject()->className();
if (menuItemListClassName=="ScrAction")
{
ScrAction * act = dynamic_cast<ScrAction *>(listObj);
if (act!=NULL)
localPopupMenu->addAction(act);
}
else
{
if (menuItemListClassName=="ScrPopupMenu")
{
ScrPopupMenu * men = dynamic_cast<ScrPopupMenu *>(listObj);
if (men!=NULL)
{
// localPopupMenu->insertItem(men->getMenuIcon(), men->getMenuText(), men->getLocalPopupMenu());
QAction *m=localPopupMenu->addMenu(men->getLocalPopupMenu());
if (m)
m->setText(men->getMenuText());
}
}
else
sDebug(QString("Alien found: %1").arg((*menuItemListIt)->metaObject()->className()));
}
++menuItemListIt;
}
return true;
}
bool ScrPopupMenu::generateEntryList(QStringList *actNames)
{
QList< QPointer<QObject> >::Iterator menuItemListIt = menuItemList.begin();
while (menuItemListIt!=menuItemList.end())
{
QObject *listObj=(*menuItemListIt);
if (listObj==NULL)
{
QList< QPointer<QObject> >::Iterator menuItemListItToDelete = menuItemListIt;
++menuItemListIt;
menuItemList.removeAll(*menuItemListItToDelete);
continue;
}
QString menuItemListClassName=listObj->metaObject()->className();
if (menuItemListClassName=="ScrAction")
{
ScrAction *sca=dynamic_cast<ScrAction *>(listObj);
if (sca!=NULL)
actNames->append(sca->objectName());
}
else
if (menuItemListClassName=="ScrPopupMenu")
{
ScrPopupMenu *scp=dynamic_cast<ScrPopupMenu *>(listObj);
if (scp!=NULL)
scp->generateEntryList(actNames);
}
++menuItemListIt;
}
return true;
}
bool ScrPopupMenu::clear()
{
menuItemList.clear(); //CB TODO leaking separators here ?
localPopupMenu->clear();
return true;
}
bool ScrPopupMenu::insertMenuSeparator()
{
ScrAction *sepAction = new ScrAction( NULL );
sepAction->setSeparator(true);
menuItemList.append(sepAction);
localPopupMenu->addSeparator();
return true;
}
void ScrPopupMenu::setEnabled(bool menuEnabled)
{
enabled=menuEnabled;
localPopupMenu->setEnabled(enabled);
}
void ScrPopupMenu::setParentMenuName(const QString& newParentMenuName)
{
if (!parentMenuName.isEmpty())
parentMenuName=newParentMenuName;
}
|