]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/Toolbar_pimpl.C
display shortcut on layout combox action
[lyx.git] / src / frontends / qt2 / Toolbar_pimpl.C
1 /**
2  * \file Toolbar_pimpl.C
3  * See the file COPYING.
4  *
5  * \author Lars Gullik Bjønnes, larsbj@lyx.org
6  * \uathor John Levon <moz@compsoc.man.ac.uk>
7  */
8
9 #include <config.h>
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "ToolbarDefaults.h"
16 #include "debug.h"
17 #include "lyxfunc.h"
18 #include "FuncStatus.h"
19 #include "BufferView.h"
20 #include "buffer.h"
21 #include "LyXAction.h"
22 #include "gettext.h"
23
24 #include "support/LAssert.h"
25 #include "support/filetools.h"
26 #include "support/lstrings.h" 
27
28 #include "QtView.h"
29  
30 #include "Toolbar_pimpl.h"
31  
32 #include <boost/tuple/tuple.hpp>
33  
34 #include <qtoolbar.h>
35 #include <qcombobox.h>
36 #include <qtooltip.h>
37 #include <qsizepolicy.h>
38  
39 using std::endl;
40
41 namespace {
42  
43 QPixmap getIconPixmap(int action)
44 {
45         FuncRequest f = lyxaction.retrieveActionArg(action);
46  
47         string xpm_name;
48
49         if (f.action == LFUN_INSERT_MATH && !f.argument.empty()) {
50                 xpm_name = "math/" + subst(f.argument.substr(1), ' ', '_');
51         } else {
52                 string const name = lyxaction.getActionName(f.action);
53                 if (!f.argument.empty())
54                         xpm_name = subst(name + ' ' + f.argument, ' ','_');
55                 else 
56                         xpm_name = name;
57         }
58
59         string fullname = LibFileSearch("images", xpm_name, "xpm");
60
61         if (!fullname.empty()) {
62                 lyxerr[Debug::GUI] << "Full icon name is `" 
63                                        << fullname << "'" << endl;
64                 return QPixmap(fullname.c_str());
65         }
66
67         lyxerr << "Unable to find icon `" << xpm_name << "'" << endl;
68         fullname = LibFileSearch("images", "unknown", "xpm");
69         if (!fullname.empty()) {
70                 lyxerr[Debug::GUI] << "Using default `unknown' icon" 
71                                        << endl;
72         }
73         return QPixmap(fullname.c_str());
74 }
75
76 } // namespace anon
77
78  
79 class QLComboBox : public QComboBox {
80 public:
81         QLComboBox(QWidget * parent) : QComboBox(parent) {}
82         void popup() { QComboBox::popup(); }
83 };
84
85  
86 Toolbar::Pimpl::Pimpl(LyXView * o, int, int)
87         : owner_(static_cast<QtView *>(o)), 
88         combo_(0)
89 {
90         proxy_.reset(new ToolbarProxy(*this));
91 }
92
93
94 Toolbar::Pimpl::~Pimpl()
95 {
96 }
97
98
99 void Toolbar::Pimpl::update()
100 {
101         ButtonMap::const_iterator p = map_.begin();
102         ButtonMap::const_iterator end = map_.end();
103  
104         for (; p != end; ++p) {
105                 QToolButton * button = p->first;
106                 int action = p->second;
107  
108                 FuncStatus const status = 
109                         owner_->getLyXFunc().getStatus(action);
110  
111                 button->setToggleButton(true);
112                 button->setOn(status.onoff(true));
113                 button->setEnabled(!status.disabled());
114         }
115
116         if (combo_)
117                 combo_->setEnabled(!owner_->getLyXFunc().getStatus(LFUN_LAYOUT).disabled());
118 }
119
120
121 void Toolbar::Pimpl::button_selected(QToolButton * button)
122 {
123         ButtonMap::const_iterator cit = map_.find(button);
124
125         if (cit == map_.end()) {
126                 lyxerr << "non existent tool button selected !" << endl;
127                 return;
128         }
129
130         owner_->getLyXFunc().dispatch(cit->second, true);
131 }
132  
133
134 void Toolbar::Pimpl::changed_layout(string const & sel)
135 {
136         owner_->centralWidget()->setFocus();
137  
138         LyXTextClass const & tc =
139                 owner_->buffer()->params.getLyXTextClass();
140         
141         LyXTextClass::const_iterator end = tc.end();
142         for (LyXTextClass::const_iterator cit = tc.begin();
143              cit != end; ++cit) {
144                 if (_((*cit)->name()) == sel) {
145                         owner_->getLyXFunc().dispatch(FuncRequest(LFUN_LAYOUT, (*cit)->name()), true);
146                         return;
147                 }
148         }
149         lyxerr << "ERROR (Toolbar::Pimpl::layoutSelected): layout not found!"
150                << endl;
151 }
152  
153
154 void Toolbar::Pimpl::setLayout(string const & layout)
155 {
156         LyXTextClass const & tc =
157                 owner_->buffer()->params.getLyXTextClass();
158  
159         string const & name = _(tc[layout]->name());
160  
161         int i;
162  
163         for (i = 0; i < combo_->count(); ++i) {
164                 if (name == combo_->text(i).latin1())
165                         break;
166         }
167
168         if (i == combo_->count()) {
169                 lyxerr << "Trying to select non existent layout type "
170                         << name << endl;
171                 return;
172         }
173
174         combo_->setCurrentItem(i);
175 }
176
177
178 void Toolbar::Pimpl::updateLayoutList(bool force)
179 {
180         // if we don't need an update, don't ... 
181         if (combo_->count() && !force)
182                 return;
183  
184         LyXTextClass const & tc =
185                 owner_->buffer()->params.getLyXTextClass();
186  
187         combo_->setUpdatesEnabled(false); 
188  
189         combo_->clear();
190  
191         LyXTextClass::const_iterator cit = tc.begin();
192         LyXTextClass::const_iterator end = tc.end();
193         for (; cit != end; ++cit) {
194                 // ignore obsolete entries
195                 if ((*cit)->obsoleted_by().empty())
196                         combo_->insertItem(_((*cit)->name()).c_str());
197         }
198
199         // needed to recalculate size hint
200         combo_->hide();
201         combo_->setMinimumWidth(combo_->sizeHint().width());
202         combo_->show();
203
204         combo_->setUpdatesEnabled(true);
205         combo_->update();
206 }
207
208
209 void Toolbar::Pimpl::clearLayoutList()
210 {
211         combo_->clear();
212 }
213
214
215 void Toolbar::Pimpl::openLayoutList()
216 {
217         combo_->popup();
218 }
219
220
221 void Toolbar::Pimpl::add(int action)
222 {
223         if (!toolbars_.size()) {
224                 toolbars_.push_back(new QToolBar(owner_));
225         }
226  
227         switch (action) {
228         case ToolbarDefaults::SEPARATOR:
229                 toolbars_.back()->addSeparator();
230                 break;
231         case ToolbarDefaults::NEWLINE:
232                 toolbars_.push_back(new QToolBar(owner_));
233                 break;
234         case ToolbarDefaults::LAYOUTS: {
235                 combo_ = new QLComboBox(toolbars_.back());
236                 QSizePolicy p(QSizePolicy::Minimum, QSizePolicy::Fixed);
237                 combo_->setSizePolicy(p);
238                 combo_->setFocusPolicy(QWidget::ClickFocus);
239                 combo_->setMinimumWidth(combo_->sizeHint().width());
240  
241                 QObject::connect(combo_, SIGNAL(activated(const QString &)),
242                         proxy_.get(), SLOT(layout_selected(const QString &)));
243                 break;
244         }
245         default: {
246                 QToolButton * tb = 
247                         new QToolButton(getIconPixmap(action),
248                         _(lyxaction.helpText(action)).c_str(), "",
249                         proxy_.get(), SLOT(button_selected()), toolbars_.back());
250
251                 map_[tb] = action;
252                 break;
253         }
254         }
255 }