]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QLToolbar.C
Fix unreported bug related to 3246 by Richard Heck:
[lyx.git] / src / frontends / qt4 / QLToolbar.C
1 /**
2  * \file qt4/QLToolbar.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  * \author Jean-Marc Lasgouttes
9  * \author Angus Leeming
10  * \author Abdelrazak Younes
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "buffer.h"
18 #include "bufferparams.h"
19 #include "debug.h"
20 #include "funcrequest.h"
21 #include "FuncStatus.h"
22 #include "gettext.h"
23 #include "lyxfunc.h"
24
25 #include "GuiView.h"
26 #include "QLToolbar.h"
27 #include "Action.h"
28 #include "qt_helpers.h"
29 #include "InsertTableWidget.h"
30
31 #include <QComboBox>
32 #include <QToolBar>
33 #include <QToolButton>
34 #include <QAction>
35 #include <QPixmap>
36
37 using std::endl;
38 using std::string;
39
40 namespace lyx {
41 namespace frontend {
42
43 namespace {
44
45 LyXTextClass const & getTextClass(LyXView const & lv)
46 {
47         return lv.buffer()->params().getLyXTextClass();
48 }
49
50
51 } // namespace anon
52
53
54 QLayoutBox::QLayoutBox(QToolBar * toolbar, GuiView & owner)
55         : owner_(owner)
56 {
57         combo_ = new QComboBox;
58         combo_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
59         combo_->setFocusPolicy(Qt::ClickFocus);
60         combo_->setMinimumWidth(combo_->sizeHint().width());
61         combo_->setMaxVisibleItems(100);
62
63         QObject::connect(combo_, SIGNAL(activated(const QString &)),
64                          this, SLOT(selected(const QString &)));
65
66         toolbar->addWidget(combo_);
67 }
68
69
70 void QLayoutBox::set(string const & layout)
71 {
72         LyXTextClass const & tc = getTextClass(owner_);
73
74         QString const & name = qt_(tc[layout]->name());
75
76         int i = 0;
77         for (; i < combo_->count(); ++i) {
78                 if (name == combo_->itemText(i))
79                         break;
80         }
81
82         if (i == combo_->count()) {
83                 lyxerr << "Trying to select non existent layout type "
84                         << fromqstr(name) << endl;
85                 return;
86         }
87
88         combo_->setCurrentIndex(i);
89 }
90
91
92 void QLayoutBox::update()
93 {
94         LyXTextClass const & tc = getTextClass(owner_);
95
96         combo_->setUpdatesEnabled(false);
97
98         combo_->clear();
99
100         LyXTextClass::const_iterator it = tc.begin();
101         LyXTextClass::const_iterator const end = tc.end();
102         for (; it != end; ++it) {
103                 // ignore obsolete entries
104                 if ((*it)->obsoleted_by().empty())
105                         combo_->addItem(qt_((*it)->name()));
106         }
107
108         // needed to recalculate size hint
109         combo_->hide();
110         combo_->setMinimumWidth(combo_->sizeHint().width());
111         combo_->show();
112
113         combo_->setUpdatesEnabled(true);
114         combo_->update();
115 }
116
117
118 void QLayoutBox::clear()
119 {
120         combo_->clear();
121 }
122
123
124 void QLayoutBox::open()
125 {
126         combo_->showPopup();
127 }
128
129
130 void QLayoutBox::setEnabled(bool enable)
131 {
132         // Workaround for Qt bug where setEnabled(true) closes
133         // the popup
134         if (enable != combo_->isEnabled())
135                 combo_->setEnabled(enable);
136 }
137
138
139 void QLayoutBox::selected(const QString & str)
140 {
141         string const sel = fromqstr(str);
142
143         owner_.setFocus();
144
145         layoutSelected(owner_, sel);
146 }
147
148
149 QLToolbar::QLToolbar(ToolbarBackend::Toolbar const & tbb, GuiView & owner)
150         : QToolBar(qt_(tbb.gui_name), &owner), owner_(owner)
151 {
152         // give visual separation between adjacent toolbars
153         addSeparator();
154
155         // TODO: save toolbar position
156         setMovable(true);
157
158         ToolbarBackend::item_iterator it = tbb.items.begin();
159         ToolbarBackend::item_iterator end = tbb.items.end();
160         for (; it != end; ++it)
161                 add(it->first, it->second);
162 }
163
164
165 void QLToolbar::add(FuncRequest const & func, docstring const & tooltip)
166 {
167         switch (func.action) {
168         case ToolbarBackend::SEPARATOR:
169                 addSeparator();
170                 break;
171         case ToolbarBackend::LAYOUTS:
172                 layout_.reset(new QLayoutBox(this, owner_));
173                 break;
174         case ToolbarBackend::MINIBUFFER:
175                 owner_.addCommandBuffer(this);
176                 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
177                 //setHorizontalStretchable(true);
178                 break;
179         case LFUN_TABULAR_INSERT: {
180                 QToolButton * tb = new QToolButton;
181                 tb->setCheckable(true);
182                 tb->setIcon(QPixmap(toqstr(toolbarbackend.getIcon(func))));
183                 tb->setToolTip(toqstr(tooltip));
184                 tb->setFocusPolicy(Qt::NoFocus);
185                 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
186                 connect(tb, SIGNAL(clicked(bool)), iv, SLOT(show(bool)));
187                 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
188                 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
189                 addWidget(tb);
190                 break;
191                 }
192         default: {
193                 if (lyx::getStatus(func).unknown())
194                         break;
195
196                 Action * action = new Action(owner_,
197                         toolbarbackend.getIcon(func),
198                         tooltip,
199                         func,
200                         tooltip);
201                 addAction(action);
202                 ActionVector.push_back(action);
203                 break;
204                 }
205         }
206 }
207
208
209 void QLToolbar::hide(bool)
210 {
211         QToolBar::hide();
212 }
213
214
215 void QLToolbar::show(bool)
216 {
217         QToolBar::show();
218 }
219
220
221 void QLToolbar::saveInfo(ToolbarSection::ToolbarInfo & info)
222 {
223         // if info.state == auto *do not* set on/off
224         if (info.state != ToolbarSection::ToolbarInfo::AUTO) {
225                 if (QLToolbar::isVisible())
226                         info.state = ToolbarSection::ToolbarInfo::ON;
227                 else
228                         info.state = ToolbarSection::ToolbarInfo::OFF;
229         }
230         //      
231         // no need to save it here.
232         Qt::ToolBarArea loc = owner_.toolBarArea(this);
233
234         if (loc == Qt::TopToolBarArea)
235                 info.location = ToolbarSection::ToolbarInfo::TOP;
236         else if (loc == Qt::BottomToolBarArea)
237                 info.location = ToolbarSection::ToolbarInfo::BOTTOM;
238         else if (loc == Qt::RightToolBarArea)
239                 info.location = ToolbarSection::ToolbarInfo::RIGHT;
240         else if (loc == Qt::LeftToolBarArea)
241                 info.location = ToolbarSection::ToolbarInfo::LEFT;
242         else
243                 info.location = ToolbarSection::ToolbarInfo::NOTSET;
244         
245         // save toolbar position. They are not used to restore toolbar position 
246         // now because move(x,y) does not work for toolbar.
247         info.posx = pos().x();
248         info.posy = pos().y();
249 }
250
251
252 void QLToolbar::update()
253 {
254         // This is a speed bottleneck because this is called on every keypress
255         // and update calls getStatus, which copies the cursor at least two times
256         for (size_t i = 0; i < ActionVector.size(); ++i)
257                 ActionVector[i]->update();
258
259         // emit signal
260         updated();
261 }
262
263
264 } // namespace frontend
265 } // namespace lyx
266
267 #include "QLToolbar_moc.cpp"