]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QLToolbar.C
enable Font cache only for MacOSX and inline width() for other platform.
[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_.centralWidget()->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         // allowing the toolbars to tear off is too easily done,
156         // and we don't save their orientation anyway. Disable the handle.
157         setMovable(false);
158
159         ToolbarBackend::item_iterator it = tbb.items.begin();
160         ToolbarBackend::item_iterator end = tbb.items.end();
161         for (; it != end; ++it)
162                 add(it->first, it->second);
163 }
164
165
166 void QLToolbar::add(FuncRequest const & func, string const & tooltip)
167 {
168         switch (func.action) {
169         case ToolbarBackend::SEPARATOR:
170                 addSeparator();
171                 break;
172         case ToolbarBackend::LAYOUTS:
173                 layout_.reset(new QLayoutBox(this, owner_));
174                 break;
175         case ToolbarBackend::MINIBUFFER:
176                 owner_.addCommandBuffer(this);
177                 /// \todo find a Qt4 equivalent to setHorizontalStretchable(true);
178                 //setHorizontalStretchable(true);
179                 break;
180         case LFUN_TABULAR_INSERT: {
181                 QToolButton * tb = new QToolButton;
182                 tb->setCheckable(true);
183                 tb->setIcon(QPixmap(toqstr(toolbarbackend.getIcon(func))));
184                 tb->setToolTip(toqstr(tooltip));
185                 tb->setFocusPolicy(Qt::NoFocus);
186                 InsertTableWidget * iv = new InsertTableWidget(owner_, tb);
187                 connect(tb, SIGNAL(toggled(bool)), iv, SLOT(show(bool)));
188                 connect(iv, SIGNAL(visible(bool)), tb, SLOT(setChecked(bool)));
189                 connect(this, SIGNAL(updated()), iv, SLOT(updateParent()));
190                 addWidget(tb);
191                 break;
192                 }
193         default: {
194                 if (owner_.getLyXFunc().getStatus(func).unknown())
195                         break;
196
197                 Action * action = new Action(owner_, toolbarbackend.getIcon(func), "", func, tooltip);
198                 addAction(action);
199                 ActionVector.push_back(action);
200                 break;
201                 }
202         }
203 }
204
205
206 void QLToolbar::hide(bool)
207 {
208         QToolBar::hide();
209 }
210
211
212 void QLToolbar::show(bool)
213 {
214         QToolBar::show();
215 }
216
217
218 void QLToolbar::update()
219 {
220         for (size_t i=0; i<ActionVector.size(); ++i)
221                 ActionVector[i]->update();
222
223         // emit signal
224         updated();
225 }
226
227
228 } // namespace frontend
229 } // namespace lyx
230
231 #include "QLToolbar_moc.cpp"