]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiPopupMenu.cpp
less string conversions as long as we stay in the frontend
[lyx.git] / src / frontends / qt4 / GuiPopupMenu.cpp
1 /**
2  * \file GuiPopupMenu.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiPopupMenu.h"
15
16 #include "Action.h"
17 #include "GuiApplication.h"
18 #include "GuiView.h"
19 #include "qt_helpers.h"
20
21 #include "LyXFunc.h"
22
23 #include "support/debug.h"
24
25 namespace lyx {
26 namespace frontend {
27
28 GuiPopupMenu::GuiPopupMenu(GuiView * owner, MenuItem const & mi,
29                 bool topLevelMenu)
30         : QMenu(owner), owner_(owner)
31 {
32         name_ = mi.submenuname();
33
34         setTitle(label(mi));
35
36         if (topLevelMenu)
37                 connect(this, SIGNAL(aboutToShow()), this, SLOT(updateView()));
38 }
39
40
41 void GuiPopupMenu::updateView()
42 {
43         LYXERR(Debug::GUI, "GuiPopupMenu::updateView()"
44                 << "\tTriggered menu: " << fromqstr(name_));
45
46         clear();
47
48         if (name_.isEmpty())
49                 return;
50
51         // Here, We make sure that theLyXFunc points to the correct LyXView.
52         theLyXFunc().setLyXView(owner_);
53
54         Menus const & menus = guiApp->menus();
55         Menu const & fromLyxMenu = menus.getMenu(name_);
56         menus.expand(fromLyxMenu, topLevelMenu_, owner_->buffer());
57
58         if (!menus.hasMenu(topLevelMenu_.name())) {
59                 LYXERR(Debug::GUI, "\tWARNING: menu seems empty"
60                         << fromqstr(topLevelMenu_.name()));
61         }
62         populate(this, &topLevelMenu_);
63 }
64
65
66 void GuiPopupMenu::populate(QMenu * qMenu, Menu * menu)
67 {
68         LYXERR(Debug::GUI, "populating menu " << fromqstr(menu->name()));
69         if (menu->size() == 0) {
70                 LYXERR(Debug::GUI, "\tERROR: empty menu " << fromqstr(menu->name()));
71                 return;
72         }
73         LYXERR(Debug::GUI, " *****  menu entries " << menu->size());
74
75         Menu::const_iterator m = menu->begin();
76         Menu::const_iterator end = menu->end();
77
78         for (; m != end; ++m) {
79
80                 if (m->kind() == MenuItem::Separator) {
81
82                         qMenu->addSeparator();
83                         LYXERR(Debug::GUI, "adding Menubar Separator");
84
85                 } else if (m->kind() == MenuItem::Submenu) {
86
87                         LYXERR(Debug::GUI, "** creating New Sub-Menu "
88                                 << fromqstr(label(*m)));
89                         QMenu * subMenu = qMenu->addMenu(label(*m));
90                         populate(subMenu, m->submenu());
91
92                 } else { // we have a MenuItem::Command
93
94                         LYXERR(Debug::GUI, "creating Menu Item "
95                                 << fromqstr(m->label()));
96
97                         Action * action = new Action(*owner_,
98                                 QIcon(), label(*m), m->func(), QString());
99                         qMenu->addAction(action);
100                 }
101         }
102 }
103
104
105 QString GuiPopupMenu::label(MenuItem const & mi) const
106 {
107         QString label = mi.label();
108         label.replace("&", "&&");
109
110         QString shortcut = mi.shortcut();
111         if (!shortcut.isEmpty()) {
112                 int pos = label.indexOf(shortcut);
113                 if (pos != -1)
114                         //label.insert(pos, 1, char_type('&'));
115                         label.replace(pos, 0, "&");
116         }
117
118         QString const binding = mi.binding();
119         if (!binding.isEmpty())
120                 label += '\t' + binding;
121
122         return label;
123 }
124
125
126 } // namespace frontend
127 } // namespace lyx
128
129 #include "GuiPopupMenu_moc.cpp"