Subversion Repositories Scribus

Rev

Rev 24623 | 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
 ***************************************************************************/
9776 cbradney 21
#include <QMenuBar>
19080 craig 22
 
1209 cbradney 23
#include "menumanager.h"
13576 cbradney 24
#include "ui/scmenu.h"
1209 cbradney 25
#include "scraction.h"
10558 cbradney 26
#include <QDebug>
1209 cbradney 27
 
28
 
10527 fschmid 29
MenuManager::MenuManager(QMenuBar* mb, QObject *parent) : QObject(parent)
1209 cbradney 30
{
24624 jghali 31
        scribusMenuBar = mb;
18533 craig 32
        menuStrings.clear();
24624 jghali 33
        m_undoMenu = new QMenu("undo");
34
        m_redoMenu = new QMenu("redo");
18580 craig 35
        rememberedMenus.clear();
1209 cbradney 36
}
37
 
38
MenuManager::~MenuManager()
39
{
18556 craig 40
        m_undoMenu->deleteLater();
41
        m_redoMenu->deleteLater();
1209 cbradney 42
}
43
 
22635 craig 44
bool MenuManager::createMenu(const QString &menuName, const QString &menuText, const QString& parent, bool checkable, bool rememberMenu)
1209 cbradney 45
{
24624 jghali 46
        bool retVal = false;
18533 craig 47
        QList<QString> menuEntries;
48
        menuStrings.insert(menuName, menuEntries);
49
        menuStringTexts.insert(menuName, menuText);
18580 craig 50
        if (rememberMenu)
51
        {
22516 craig 52
                rememberedMenus.insert(menuName, nullptr);
18580 craig 53
        }
1209 cbradney 54
        return retVal;
55
}
56
 
1325 cbradney 57
 
1359 cbradney 58
bool MenuManager::clearMenu(const QString &menuName)
1209 cbradney 59
{
24624 jghali 60
        bool retVal = false;
18533 craig 61
        if (menuBarMenus.contains(menuName))
62
                menuBarMenus[menuName]->clear();
1236 cbradney 63
        return retVal;
1209 cbradney 64
}
65
 
10427 cbradney 66
void MenuManager::setText(const QString &menuName, const QString &menuText)
2178 cbradney 67
{
18556 craig 68
        if (menuStringTexts.contains(menuName))
69
                menuStringTexts.insert(menuName, menuText);
70
        //TODO rebuild all menus
22516 craig 71
//      if (menuList.contains(menuName) && menuList[menuName]!=nullptr)
18556 craig 72
//      {
73
//              menuList[menuName]->setText(menuText);
74
//              QString parent=menuList[menuName]->getParentMenuName();
75
//              if (!parent.isNull())
76
//                      menuList[parent]->repopulateLocalMenu();
77
//      }
2178 cbradney 78
}
79
 
9768 cbradney 80
QMenu *MenuManager::getLocalPopupMenu(const QString &menuName)
1260 cbradney 81
{
22516 craig 82
        if (menuBarMenus.contains(menuName) && menuBarMenus.value(menuName)!=nullptr)
18556 craig 83
                return menuBarMenus.value(menuName);
22516 craig 84
        return nullptr;
1260 cbradney 85
}
86
 
23017 craig 87
void MenuManager::setMenuEnabled(const QString &menuName, bool enabled)
1209 cbradney 88
{
18905 craig 89
        // OSX UI rules don't allow this so let's not do it elsewhere.
22516 craig 90
        //if (menuBarMenus.contains(menuName) && menuBarMenus.value(menuName)!=nullptr)
18905 craig 91
        //      menuBarMenus.value(menuName)->setEnabled(enabled);
1209 cbradney 92
}
93
 
18583 craig 94
bool MenuManager::addMenuStringToMenuBar(const QString &menuName, bool rememberMenu)
18533 craig 95
{
22900 jghali 96
        if (!menuStrings.contains(menuName))
97
                return false;
98
 
99
        QMenu *m = scribusMenuBar->addMenu(menuStringTexts[menuName]);
100
        menuBarMenus.insert(menuName, m);
101
        if (rememberMenu)
102
                rememberedMenus.insert(menuName, m);
103
        return true;
18533 craig 104
}
105
 
18548 craig 106
bool MenuManager::addMenuStringToMenuBarBefore(const QString &menuName, const QString &beforeMenuName)
18544 craig 107
{
22666 jghali 108
        bool retVal = false;
109
 
110
        if (!menuStrings.contains(menuName))
111
                return false;
112
 
113
        QList<QAction*> mba = scribusMenuBar->actions();
24158 jghali 114
        QAction* beforeAct = nullptr;
22666 jghali 115
        foreach (beforeAct, mba)
18544 craig 116
        {
24158 jghali 117
                if (beforeMenuName == beforeAct->text().remove('&').remove("..."))
22666 jghali 118
                        break;
18544 craig 119
        }
22666 jghali 120
        if (beforeAct)
121
        {
24158 jghali 122
                auto *m = new QMenu(menuStringTexts[menuName]);
22666 jghali 123
                scribusMenuBar->insertMenu(beforeAct, m);
124
                menuBarMenus.insert(menuName, m);
24158 jghali 125
                retVal = true;
22666 jghali 126
        }
18544 craig 127
        return retVal;
128
}
129
 
18706 craig 130
void MenuManager::clear()
131
{
132
        menuStrings.clear();
133
        rememberedMenus.clear();
134
}
18544 craig 135
 
18706 craig 136
 
22900 jghali 137
void MenuManager::addMenuItemStringsToMenuBar(const QString &menuName, const QMap<QString, QPointer<ScrAction> > &menuActions)
18533 craig 138
{
22814 jghali 139
        if (!menuStrings.contains(menuName) || !menuBarMenus.contains(menuName))
140
                return;
141
 
22899 jghali 142
        const auto menuStringList = menuStrings[menuName];
143
        for (const QString& menuString : menuStringList)
18533 craig 144
        {
22814 jghali 145
                //Add Separators
22899 jghali 146
                if (menuString == "SEPARATOR")
18533 craig 147
                {
22814 jghali 148
                        menuBarMenus[menuName]->addSeparator();
149
                        continue;
150
                }
151
 
152
                //Add Menu Items
22899 jghali 153
                if (menuActions.contains(menuString))
22814 jghali 154
                {
22899 jghali 155
                        menuBarMenus[menuName]->addAction(menuActions[menuString]);
22814 jghali 156
                        continue;
157
                }
158
 
159
                //Add Sub Menus
22899 jghali 160
                if (menuStrings.contains(menuString))
22814 jghali 161
                {
22899 jghali 162
                        QMenu *subMenu = menuBarMenus[menuName]->addMenu(menuStringTexts[menuString]);
163
                        if (!subMenu)
164
                                continue;
24623 craig 165
                        //#16020 and related Qt bugs for QMenu/QAction Text Heuristics messing up detection. Turn off the role detection except where we explicitly set it in ActionManager
166
                        if (subMenu->menuAction()->menuRole() == QAction::TextHeuristicRole)
167
                                subMenu->menuAction()->setMenuRole(QAction::NoRole);
22899 jghali 168
                        menuBarMenus.insert(menuString, subMenu);
169
                        if (rememberedMenus.contains(menuString))
170
                                rememberedMenus.insert(menuString, subMenu);
22900 jghali 171
                        addMenuItemStringsToMenu(menuString, subMenu, menuActions);
18533 craig 172
                }
173
        }
174
}
175
 
176
 
22900 jghali 177
void MenuManager::addMenuItemStringsToMenu(const QString &menuName, QMenu *menuToAddTo, const QMap<QString, QPointer<ScrAction> > &menuActions)
18533 craig 178
{
22899 jghali 179
        if (!menuStrings.contains(menuName))
180
                return;
181
 
182
        const auto menuStringList = menuStrings[menuName];
183
        for (const QString& menuString : menuStringList)
18533 craig 184
        {
22899 jghali 185
                //Add Separators
186
                if (menuString == "SEPARATOR")
18533 craig 187
                {
22899 jghali 188
                        menuToAddTo->addSeparator();
189
                        continue;
18533 craig 190
                }
22899 jghali 191
 
192
                //Add Menu Items
193
                if (menuActions.contains(menuString))
194
                {
195
                        menuToAddTo->addAction(menuActions[menuString]);
196
                        continue;
197
                }
198
 
199
                //Add Sub Menus
200
                if (menuStrings.contains(menuString))
201
                {
202
                        QMenu *subMenu = menuToAddTo->addMenu(menuStringTexts[menuString]);
203
                        if (!subMenu)
204
                                continue;
24623 craig 205
                        //#16020 and related Qt bugs for QMenu/QAction Text Heuristics messing up detection. Turn off the role detection except where we explicitly set it in ActionManager
206
                        if (subMenu->menuAction()->menuRole() == QAction::TextHeuristicRole)
207
                                subMenu->menuAction()->setMenuRole(QAction::NoRole);
22899 jghali 208
                        menuBarMenus.insert(menuString, subMenu);
209
                        if (rememberedMenus.contains(menuString))
210
                                rememberedMenus.insert(menuString, subMenu);
22900 jghali 211
                        addMenuItemStringsToMenu(menuString, subMenu, menuActions);
22899 jghali 212
                }
18533 craig 213
        }
214
}
215
 
22900 jghali 216
void MenuManager::addMenuItemStringsToRememberedMenu(const QString &menuName, const QMap<QString, QPointer<ScrAction> > &menuActions)
18580 craig 217
{
218
        if (rememberedMenus.contains(menuName))
22516 craig 219
                if (rememberedMenus.value(menuName)!=nullptr)
22900 jghali 220
                        addMenuItemStringsToMenu(menuName, rememberedMenus.value(menuName), menuActions);
18580 craig 221
}
18533 craig 222
 
223
void MenuManager::clearMenuStrings(const QString &menuName)
224
{
22902 jghali 225
        QMenu* menu = rememberedMenus.value(menuName, nullptr);
226
        if (menu != nullptr)
227
                menu->clear();
1209 cbradney 228
}
229
 
18533 craig 230
void MenuManager::addMenuItemString(const QString& s, const QString &parent)
231
{
232
        if (menuStrings.contains(parent))
233
                menuStrings[parent].append(s);
234
}
235
 
236
void MenuManager::addMenuItemStringAfter(const QString& s, const QString& after, const QString &parent)
237
{
238
        if (menuStrings.contains(parent))
239
        {
240
                int i=menuStrings[parent].indexOf(after);
241
                menuStrings[parent].insert(i+1, s);
242
        }
243
}
244
 
19962 fschmid 245
void MenuManager::removeMenuItem(const QString& s, ScrAction *menuAction, const QString &parent)
246
{
247
        if (menuStrings.contains(parent))
248
        {
249
                if (menuStrings[parent].contains(s))
250
                {
251
                        menuBarMenus[parent]->removeAction(menuAction);
252
                        menuStrings[parent].removeAll(s);
253
                }
254
        }
255
}
256
 
1359 cbradney 257
bool MenuManager::removeMenuItem(ScrAction *menuAction, const QString &parent)
1236 cbradney 258
{
24624 jghali 259
        bool retVal = false;
18557 craig 260
        /*
22516 craig 261
         if (menuList.contains(parent) && menuList[parent]!=nullptr)
1236 cbradney 262
                retVal=menuList[parent]->removeMenuItem(menuAction);
18557 craig 263
        */
1236 cbradney 264
        return retVal;
265
}
266
 
23017 craig 267
void MenuManager::runMenuAtPos(const QString &menuName, const QPoint& position)
1209 cbradney 268
{
18557 craig 269
        /*
22516 craig 270
        if (menuList.contains(menuName) && menuList[menuName]!=nullptr)
1209 cbradney 271
        {      
9768 cbradney 272
                QMenu *popupmenu=menuList[menuName]->getLocalPopupMenu();
1227 cbradney 273
                if (popupmenu)
274
                        popupmenu->exec(position);
1209 cbradney 275
        }
18557 craig 276
        */
1209 cbradney 277
}
1437 cbradney 278
 
2178 cbradney 279
bool MenuManager::empty()
280
{
18556 craig 281
        return menuStrings.empty();
2178 cbradney 282
}
11050 fschmid 283
 
284
bool MenuManager::menuExists(const QString &menuName)
285
{
18556 craig 286
        return menuStrings.contains(menuName);
11050 fschmid 287
}
18533 craig 288
 
289
void MenuManager::dumpMenuStrings()
290
{
291
        QMapIterator<QString, QList<QString> > i(menuStrings);
18706 craig 292
        while (i.hasNext())
293
        {
18533 craig 294
                i.next();
295
                qDebug() << "Menu name:"<<i.key();// << ": " << i.value() << endl;
296
 
297
                QListIterator<QString> li (i.value());
18706 craig 298
                while (li.hasNext())
299
                {
18533 craig 300
                        qDebug() << "Menu entry:"<<li.next();
301
                }
302
        }
303
}
18706 craig 304
 
305
void MenuManager::languageChange()
306
{
22666 jghali 307
        const auto menuBarKeys = menuBarMenus.keys();
308
        for (const QString &menuName : menuBarKeys)
18706 craig 309
        {
22666 jghali 310
                QMenu *m = menuBarMenus.value(menuName);
18758 craig 311
                if (m && menuStringTexts.contains(menuName))
18706 craig 312
                        m->setTitle(menuStringTexts[menuName]);
313
        }
314
}