Rev 13576 | Rev 17936 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4430 | cbradney | 1 | /* |
2 | For general Scribus (>=1.3.2) copyright and licensing information please refer |
||
3 | to the COPYING file provided with the program. Following this notice may exist |
||
4 | a copyright and/or license notice that predates the release of Scribus 1.3.2 |
||
5 | for which a new license (GPL+exception) is in place. |
||
6 | */ |
||
1209 | cbradney | 7 | /*************************************************************************** |
8 | begin : Jan 2005 |
||
9 | copyright : (C) 2005 by Craig Bradney |
||
10 | email : cbradney@zip.com.au |
||
11 | ***************************************************************************/ |
||
12 | |||
13 | /*************************************************************************** |
||
14 | * * |
||
15 | * This program is free software; you can redistribute it and/or modify * |
||
16 | * it under the terms of the GNU General Public License as published by * |
||
17 | * the Free Software Foundation; either version 2 of the License, or * |
||
18 | * (at your option) any later version. * |
||
19 | * * |
||
20 | ***************************************************************************/ |
||
9768 | cbradney | 21 | #include <QObject> |
22 | #include <QMetaObject> |
||
23 | #include <QMenu> |
||
24 | #include <QList> |
||
1209 | cbradney | 25 | #include "scmenu.h" |
26 | #include "scraction.h" |
||
3552 | avox | 27 | #include "util.h" |
10228 | avox | 28 | #include "util_icon.h" |
1209 | cbradney | 29 | |
10228 | avox | 30 | |
10558 | cbradney | 31 | ScrPopupMenu::ScrPopupMenu ( QWidget * parent, const QString pMenuName, const QString pMenuText, const QString parentName, bool pCheckable ) : QObject(parent) |
1209 | cbradney | 32 | { |
33 | parentMenuName=parentName; |
||
2178 | cbradney | 34 | parentMenuID=-1; |
1209 | cbradney | 35 | menuText=pMenuText; |
36 | menuName=pMenuName; |
||
37 | menuBarID=-1; |
||
38 | menuItemList.clear(); |
||
9768 | cbradney | 39 | localPopupMenu=new QMenu(parent);//, name); |
3584 | avox | 40 | // localPopupMenu->setCheckable(pCheckable); |
1209 | cbradney | 41 | enabled=true; |
3580 | avox | 42 | checkable = pCheckable; |
1209 | cbradney | 43 | } |
44 | |||
45 | ScrPopupMenu::~ScrPopupMenu() |
||
46 | { |
||
47 | delete localPopupMenu; |
||
48 | } |
||
49 | |||
50 | const QString ScrPopupMenu::getMenuText() |
||
51 | { |
||
52 | return menuText; |
||
53 | } |
||
54 | |||
10427 | cbradney | 55 | void ScrPopupMenu::setText(const QString pMenuText) |
2178 | cbradney | 56 | { |
57 | menuText=pMenuText; |
||
10687 | cbradney | 58 | localPopupMenu->menuAction()->setText(menuText); |
2178 | cbradney | 59 | } |
60 | |||
8501 | cbradney | 61 | const QIcon ScrPopupMenu::getMenuIcon() |
3552 | avox | 62 | { |
63 | return menuIcon; |
||
64 | } |
||
65 | |||
8501 | cbradney | 66 | void ScrPopupMenu::setMenuIcon(const QIcon pMenuIcon) |
3552 | avox | 67 | { |
68 | menuIcon=pMenuIcon; |
||
69 | } |
||
70 | |||
1478 | cbradney | 71 | const QString ScrPopupMenu::getMenuName() |
72 | { |
||
73 | return menuName; |
||
74 | } |
||
75 | |||
2178 | cbradney | 76 | const QString ScrPopupMenu::getParentMenuName() |
77 | { |
||
78 | return parentMenuName; |
||
79 | } |
||
80 | |||
10558 | cbradney | 81 | // void ScrPopupMenu::setMenuBarID(int id) |
82 | // { |
||
83 | // menuBarID=id; |
||
84 | // } |
||
85 | // |
||
86 | // int ScrPopupMenu::getMenuBarID() |
||
87 | // { |
||
88 | // return menuBarID; |
||
89 | // } |
||
1209 | cbradney | 90 | |
9768 | cbradney | 91 | QMenu *ScrPopupMenu::getLocalPopupMenu() |
1209 | cbradney | 92 | { |
93 | return localPopupMenu; |
||
94 | } |
||
95 | |||
1478 | cbradney | 96 | bool ScrPopupMenu::hasSubMenu(ScrPopupMenu* subMenu) |
97 | { |
||
8501 | cbradney | 98 | return menuItemList.contains(QPointer<QObject>(subMenu)); |
1478 | cbradney | 99 | } |
100 | |||
10575 | cbradney | 101 | // void ScrPopupMenu::setParentMenuID(int id) |
102 | // { |
||
103 | // parentMenuID=id; |
||
104 | // } |
||
105 | // |
||
106 | // |
||
107 | // const int ScrPopupMenu::getParentMenuID() |
||
108 | // { |
||
109 | // return parentMenuID; |
||
110 | // } |
||
2178 | cbradney | 111 | |
1209 | cbradney | 112 | bool ScrPopupMenu::insertSubMenu(ScrPopupMenu* newSubMenu) |
113 | { |
||
1227 | cbradney | 114 | if (newSubMenu) |
115 | { |
||
116 | menuItemList.append(newSubMenu); |
||
10581 | cbradney | 117 | // localPopupMenu->insertItem(newSubMenu->getMenuIcon(), newSubMenu->getMenuText(), newSubMenu->getLocalPopupMenu()); |
118 | QAction *m=localPopupMenu->addMenu(newSubMenu->getLocalPopupMenu()); |
||
119 | if (m) |
||
120 | m->setText(newSubMenu->getMenuText()); |
||
1227 | cbradney | 121 | return true; |
122 | } |
||
123 | else |
||
124 | return false; |
||
1209 | cbradney | 125 | } |
126 | |||
127 | bool ScrPopupMenu::removeSubMenu(ScrPopupMenu* subMenu) |
||
128 | { |
||
10581 | cbradney | 129 | if (menuItemList.removeAll(subMenu)) |
1209 | cbradney | 130 | { |
131 | repopulateLocalMenu(); |
||
132 | return true; |
||
133 | } |
||
134 | return false; |
||
135 | } |
||
136 | |||
137 | bool ScrPopupMenu::insertMenuItem(ScrAction *newMenuAction) |
||
138 | { |
||
1227 | cbradney | 139 | if (newMenuAction) |
140 | { |
||
3587 | avox | 141 | |
142 | #ifdef Q_WS_MAC |
||
3914 | avox | 143 | bool menuListHasNoIcons = true; |
3587 | avox | 144 | // look for ScrAction or ScrPopupMenu from the end of the list |
9777 | cbradney | 145 | QList< QPointer<QObject> >::Iterator it = menuItemList.end(); |
146 | int s=menuItemList.size()-1; |
||
147 | for (int i=s; i>=0; --i) { |
||
148 | QObject* menuItem = menuItemList[i]; |
||
10760 | cbradney | 149 | QString menuItemListClassName = menuItemList[i]->metaObject()->className(); |
3587 | avox | 150 | if (menuItemListClassName == "ScrAction") |
151 | { |
||
152 | ScrAction * act = dynamic_cast<ScrAction *>(menuItem); |
||
9788 | cbradney | 153 | if (act!=NULL) |
10760 | cbradney | 154 | menuListHasNoIcons = act->icon().isNull(); |
3587 | avox | 155 | break; |
156 | } |
||
9777 | cbradney | 157 | |
3587 | avox | 158 | else if (menuItemListClassName == "ScrPopupMenu") |
159 | { |
||
160 | ScrPopupMenu * men = dynamic_cast<ScrPopupMenu *>(menuItem); |
||
9788 | cbradney | 161 | if (men!=NULL) |
162 | menuListHasNoIcons = men->getMenuIcon().isNull(); |
||
3587 | avox | 163 | break; |
164 | } |
||
165 | } |
||
10760 | cbradney | 166 | if (newMenuAction->icon().isNull() && ! menuListHasNoIcons) |
167 | newMenuAction->setIcon(loadIcon("noicon.xpm")); |
||
3587 | avox | 168 | #endif |
169 | |||
1227 | cbradney | 170 | menuItemList.append(newMenuAction); |
9800 | cbradney | 171 | localPopupMenu->addAction(newMenuAction); |
1227 | cbradney | 172 | return true; |
173 | } |
||
174 | else |
||
175 | return false; |
||
1209 | cbradney | 176 | } |
177 | |||
8586 | cbradney | 178 | /* Qt4 |
1236 | cbradney | 179 | bool ScrPopupMenu::insertMenuItem(QWidget *widget) |
180 | { |
||
181 | if (widget) |
||
182 | { |
||
183 | ScrAction *widgetAction = new ScrAction( NULL, "widget_action" ); |
||
184 | menuItemList.append(widgetAction); |
||
185 | localPopupMenu->insertItem(widget); |
||
186 | return true; |
||
187 | } |
||
188 | else |
||
189 | return false; |
||
190 | } |
||
8586 | cbradney | 191 | */ |
1236 | cbradney | 192 | |
1209 | cbradney | 193 | bool ScrPopupMenu::insertMenuItemAfter(ScrAction *newMenuAction, ScrAction *afterMenuAction) |
194 | { |
||
195 | //Allow duplicate menu items ??? |
||
196 | //if (menuItemList.findRef(newMenuAction)!=-1) |
||
197 | // return false; |
||
198 | |||
3580 | avox | 199 | #ifdef Q_WS_MAC |
16035 | craig | 200 | if (newMenuAction && afterMenuAction) |
201 | if (newMenuAction->icon().isNull() && ! (afterMenuAction->icon().isNull())) |
||
202 | newMenuAction->setIcon(loadIcon("noicon.xpm")); |
||
3580 | avox | 203 | #endif |
204 | |||
10581 | cbradney | 205 | int pos=menuItemList.indexOf(QPointer<QObject>(afterMenuAction)); |
206 | menuItemList.insert(++pos, QPointer<QObject>(newMenuAction)); |
||
1478 | cbradney | 207 | /* |
1209 | cbradney | 208 | if (index==-1) |
209 | return false; |
||
210 | int newIndex=++index; |
||
211 | bool insSuccess=menuItemList.insert(newIndex, newMenuAction); |
||
1478 | cbradney | 212 | |
1209 | cbradney | 213 | if (!insSuccess) |
214 | return false; |
||
1478 | cbradney | 215 | */ |
1209 | cbradney | 216 | return repopulateLocalMenu(); |
217 | } |
||
218 | |||
219 | bool ScrPopupMenu::removeMenuItem(ScrAction *menuAction) |
||
220 | { |
||
10581 | cbradney | 221 | if (menuItemList.removeAll(menuAction)) |
1209 | cbradney | 222 | { |
223 | repopulateLocalMenu(); |
||
224 | return true; |
||
225 | } |
||
226 | return false; |
||
227 | } |
||
228 | /* |
||
229 | bool ScrPopupMenu::insertMenuItemAfter(ScrAction *newMenuAction, const QString afterMenuName) |
||
230 | { |
||
231 | bool retVal=true; |
||
232 | bool found=false; |
||
233 | QPtrListIterator<QObject> menuItemListIt(menuItemList); |
||
234 | QObject *object; |
||
235 | while ( (object = menuItemListIt.current()) != 0 ) |
||
236 | { |
||
237 | QString menuItemListClassName=object->className(); |
||
238 | int index=-1; |
||
239 | //index=menuItemListIt.at(); |
||
240 | if (index=-1) |
||
241 | { |
||
242 | retVal=false; |
||
243 | break; |
||
244 | } |
||
245 | if (menuItemListClassName=="ScrAction") |
||
246 | { |
||
247 | qDebug("ScrAction found"); |
||
248 | if(QString(object->name())==QString("separator_action")) |
||
249 | bool blah=true;//localPopupMenu->insertSeparator(); |
||
250 | else |
||
251 | { |
||
252 | //Grab the action from the list, break and call the other after insert function |
||
253 | |||
254 | int newIndex=++index; |
||
255 | bool insSuccess=menuItemList.insert(newIndex, newMenuAction); |
||
256 | if (!insSuccess) |
||
257 | return false; |
||
258 | |||
259 | } |
||
260 | } |
||
261 | } |
||
262 | if (!found) |
||
263 | return false; |
||
264 | if (retVal==false) |
||
265 | return false; |
||
266 | |||
267 | return repopulateLocalMenu(); |
||
268 | } |
||
269 | */ |
||
1236 | cbradney | 270 | |
271 | //CB TODO Does NOT handle rebuilding with widgets |
||
1209 | cbradney | 272 | bool ScrPopupMenu::repopulateLocalMenu() |
273 | { |
||
274 | localPopupMenu->clear(); |
||
9768 | cbradney | 275 | QList< QPointer<QObject> >::Iterator menuItemListIt = menuItemList.begin(); |
1493 | cbradney | 276 | while (menuItemListIt!=menuItemList.end()) |
1209 | cbradney | 277 | { |
1478 | cbradney | 278 | QObject *listObj=(*menuItemListIt); |
279 | if (listObj==NULL) |
||
280 | { |
||
9768 | cbradney | 281 | QList< QPointer<QObject> >::Iterator menuItemListItToDelete = menuItemListIt; |
1493 | cbradney | 282 | ++menuItemListIt; |
10581 | cbradney | 283 | menuItemList.removeAll(*menuItemListItToDelete); |
1478 | cbradney | 284 | continue; |
285 | } |
||
286 | |||
10581 | cbradney | 287 | QString menuItemListClassName=listObj->metaObject()->className(); |
1209 | cbradney | 288 | if (menuItemListClassName=="ScrAction") |
289 | { |
||
9883 | cbradney | 290 | ScrAction * act = dynamic_cast<ScrAction *>(listObj); |
291 | if (act!=NULL) |
||
292 | localPopupMenu->addAction(act); |
||
1209 | cbradney | 293 | } |
1478 | cbradney | 294 | else |
295 | { |
||
296 | if (menuItemListClassName=="ScrPopupMenu") |
||
1209 | cbradney | 297 | { |
3580 | avox | 298 | ScrPopupMenu * men = dynamic_cast<ScrPopupMenu *>(listObj); |
9883 | cbradney | 299 | if (men!=NULL) |
10581 | cbradney | 300 | { |
301 | // localPopupMenu->insertItem(men->getMenuIcon(), men->getMenuText(), men->getLocalPopupMenu()); |
||
302 | QAction *m=localPopupMenu->addMenu(men->getLocalPopupMenu()); |
||
303 | if (m) |
||
304 | m->setText(men->getMenuText()); |
||
305 | } |
||
1209 | cbradney | 306 | } |
307 | else |
||
10581 | cbradney | 308 | sDebug(QString("Alien found: %1").arg((*menuItemListIt)->metaObject()->className())); |
1478 | cbradney | 309 | } |
1493 | cbradney | 310 | ++menuItemListIt; |
1209 | cbradney | 311 | } |
312 | return true; |
||
313 | } |
||
314 | |||
1437 | cbradney | 315 | bool ScrPopupMenu::generateEntryList(QStringList *actNames) |
316 | { |
||
9768 | cbradney | 317 | QList< QPointer<QObject> >::Iterator menuItemListIt = menuItemList.begin(); |
1493 | cbradney | 318 | while (menuItemListIt!=menuItemList.end()) |
1437 | cbradney | 319 | { |
1478 | cbradney | 320 | QObject *listObj=(*menuItemListIt); |
321 | if (listObj==NULL) |
||
322 | { |
||
9768 | cbradney | 323 | QList< QPointer<QObject> >::Iterator menuItemListItToDelete = menuItemListIt; |
1493 | cbradney | 324 | ++menuItemListIt; |
10581 | cbradney | 325 | menuItemList.removeAll(*menuItemListItToDelete); |
1478 | cbradney | 326 | continue; |
327 | } |
||
328 | |||
10581 | cbradney | 329 | QString menuItemListClassName=listObj->metaObject()->className(); |
1437 | cbradney | 330 | if (menuItemListClassName=="ScrAction") |
331 | { |
||
9883 | cbradney | 332 | ScrAction *sca=dynamic_cast<ScrAction *>(listObj); |
333 | if (sca!=NULL) |
||
10581 | cbradney | 334 | actNames->append(sca->objectName()); |
1437 | cbradney | 335 | } |
1478 | cbradney | 336 | else |
337 | if (menuItemListClassName=="ScrPopupMenu") |
||
1437 | cbradney | 338 | { |
1478 | cbradney | 339 | ScrPopupMenu *scp=dynamic_cast<ScrPopupMenu *>(listObj); |
9883 | cbradney | 340 | if (scp!=NULL) |
1437 | cbradney | 341 | scp->generateEntryList(actNames); |
342 | } |
||
1493 | cbradney | 343 | ++menuItemListIt; |
1437 | cbradney | 344 | } |
345 | return true; |
||
346 | |||
347 | } |
||
348 | |||
1209 | cbradney | 349 | bool ScrPopupMenu::clear() |
350 | { |
||
351 | menuItemList.clear(); //CB TODO leaking separators here ? |
||
352 | localPopupMenu->clear(); |
||
1437 | cbradney | 353 | return true; |
1209 | cbradney | 354 | } |
355 | |||
356 | bool ScrPopupMenu::insertMenuSeparator() |
||
357 | { |
||
9857 | cbradney | 358 | ScrAction *sepAction = new ScrAction( NULL ); |
359 | sepAction->setSeparator(true); |
||
1209 | cbradney | 360 | menuItemList.append(sepAction); |
10581 | cbradney | 361 | localPopupMenu->addSeparator(); |
1209 | cbradney | 362 | return true; |
363 | } |
||
364 | |||
365 | void ScrPopupMenu::setEnabled(bool menuEnabled) |
||
366 | { |
||
367 | enabled=menuEnabled; |
||
368 | localPopupMenu->setEnabled(enabled); |
||
369 | } |
||
4522 | cbradney | 370 | |
371 | void ScrPopupMenu::setParentMenuName(const QString& newParentMenuName) |
||
372 | { |
||
373 | if (!parentMenuName.isEmpty()) |
||
374 | parentMenuName=newParentMenuName; |
||
4695 | cbradney | 375 | } |