]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiPopupMenu.cpp
reduce line noise
[features.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 "GuiView.h"
15
16 #include "Action.h"
17 #include "GuiPopupMenu.h"
18 #include "GuiMenubar.h"
19 #include "qt_helpers.h"
20 #include "LyXFunc.h"
21 #include "MenuBackend.h"
22
23 #include "support/lstrings.h"
24 #include "debug.h"
25
26
27 using std::make_pair;
28 using std::string;
29 using std::pair;
30 using std::endl;
31
32
33 namespace lyx {
34 namespace frontend {
35
36 GuiPopupMenu::GuiPopupMenu(GuiMenubar * owner, MenuItem const & mi,
37                 bool topLevelMenu)
38         : owner_(owner)
39 {
40         name_ = mi.submenuname();
41
42         setTitle(toqstr(getLabel(mi)));
43
44         if (topLevelMenu)
45                 connect(this, SIGNAL(aboutToShow()), this, SLOT(updateView()));
46 }
47
48
49 void GuiPopupMenu::updateView()
50 {
51         LYXERR(Debug::GUI, "GuiPopupMenu::updateView()"
52                 << "\tTriggered menu: " << to_utf8(name_));
53
54         clear();
55
56         if (name_.empty())
57                 return;
58
59         // Here, We make sure that theLyXFunc points to the correct LyXView.
60         theLyXFunc().setLyXView(owner_->view());
61
62         Menu const & fromLyxMenu = owner_->backend().getMenu(name_);
63         owner_->backend().expand(fromLyxMenu, topLevelMenu_, owner_->view()->buffer());
64
65         if (!owner_->backend().hasMenu(topLevelMenu_.name())) {
66                 LYXERR(Debug::GUI, "\tWARNING: menu seems empty"
67                         << to_utf8(topLevelMenu_.name()));
68         }
69         populate(this, &topLevelMenu_);
70 }
71
72
73 void GuiPopupMenu::populate(QMenu * qMenu, Menu * menu)
74 {
75         LYXERR(Debug::GUI, "populating menu " << to_utf8(menu->name()));
76         if (menu->size() == 0) {
77                 LYXERR(Debug::GUI, "\tERROR: empty menu " << to_utf8(menu->name()));
78                 return;
79         }
80         LYXERR(Debug::GUI, " *****  menu entries " << menu->size());
81
82         Menu::const_iterator m = menu->begin();
83         Menu::const_iterator end = menu->end();
84
85         for (; m != end; ++m) {
86
87                 if (m->kind() == MenuItem::Separator) {
88
89                         qMenu->addSeparator();
90                         LYXERR(Debug::GUI, "adding Menubar Separator");
91
92                 } else if (m->kind() == MenuItem::Submenu) {
93
94                         LYXERR(Debug::GUI, "** creating New Sub-Menu "
95                                 << to_utf8(getLabel(*m)));
96                         QMenu * subMenu = qMenu->addMenu(toqstr(getLabel(*m)));
97                         populate(subMenu, m->submenu());
98
99                 } else { // we have a MenuItem::Command
100
101                         LYXERR(Debug::GUI, "creating Menu Item "
102                                 << to_utf8(m->label()));
103
104                         docstring label = getLabel(*m);
105                         addBinding(label, *m);
106
107                         Action * action = new Action(*(owner_->view()),
108                                 QIcon(), toqstr(label), m->func(), QString());
109                         qMenu->addAction(action);
110                 }
111         }
112 }
113
114
115 docstring const GuiPopupMenu::getLabel(MenuItem const & mi)
116 {
117         docstring const shortcut = mi.shortcut();
118         docstring label = support::subst(mi.label(),
119         from_ascii("&"), from_ascii("&&"));
120
121         if (!shortcut.empty()) {
122                 docstring::size_type pos = label.find(shortcut);
123                 if (pos != docstring::npos)
124                         label.insert(pos, 1, char_type('&'));
125         }
126
127         return label;
128 }
129
130
131 void GuiPopupMenu::addBinding(docstring & label, MenuItem const & mi)
132 {
133 #ifdef Q_WS_MACX
134         docstring const binding = mi.binding(false);
135 #else
136         docstring const binding = mi.binding(true);
137 #endif
138         if (!binding.empty())
139                 label += '\t' + binding;
140 }
141
142
143 } // namespace frontend
144 } // namespace lyx
145
146 #include "GuiPopupMenu_moc.cpp"