]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiView.C
* Gui.h: new closeAll() pure virtual method.
[features.git] / src / frontends / qt4 / GuiView.C
1 /**
2  * \file GuiView.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 Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiImplementation.h"
16
17 #include "GuiView.h"
18 #include "QLMenubar.h"
19 #include "QLToolbar.h"
20 #include "QCommandBuffer.h"
21 #include "qt_helpers.h"
22
23 #include "frontends/Application.h"
24 #include "frontends/Gui.h"
25 #include "frontends/WorkArea.h"
26
27 #include "support/filetools.h"
28 #include "support/convert.h"
29 #include "support/lstrings.h"
30
31 #include "BufferView.h"
32 #include "bufferlist.h"
33 #include "debug.h"
34 #include "funcrequest.h"
35 #include "lyx_cb.h"
36 #include "lyxrc.h"
37 #include "lyx_main.h"
38 #include "session.h"
39 #include "lyxfunc.h"
40 #include "MenuBackend.h"
41
42 #include <QAction>
43 #include <QApplication>
44 #include <QCloseEvent>
45 #include <QPixmap>
46 #include <QStatusBar>
47 #include <QToolBar>
48
49 #include <boost/bind.hpp>
50
51 using std::endl;
52 using std::string;
53 using std::vector;
54
55 namespace lyx {
56
57 using support::subst;
58 using support::libFileSearch;
59
60 namespace frontend {
61
62 namespace {
63
64 int const statusbar_timer_value = 3000;
65
66 } // namespace anon
67
68
69 GuiView::GuiView(int id)
70         : QMainWindow(), LyXView(id), commandbuffer_(0)
71 {
72         setAttribute(Qt::WA_DeleteOnClose, true);
73         setAttribute(Qt::WA_QuitOnClose, true);
74
75 //      setToolButtonStyle(Qt::ToolButtonIconOnly);
76 //      setIconSize(QSize(12,12));
77
78 //      bufferview_.reset(new BufferView(this, width, height));
79
80 #ifndef Q_WS_MACX
81         //  assign an icon to main form. We do not do it under Qt/Mac,
82         //  since the icon is provided in the application bundle.
83         string const iconname = libFileSearch("images", "lyx", "xpm");
84         if (!iconname.empty())
85                 setWindowIcon(QPixmap(toqstr(iconname)));
86 #endif
87 }
88
89
90 GuiView::~GuiView()
91 {
92 }
93
94
95 void GuiView::close()
96 {
97         QMainWindow::close();
98 }
99
100
101 void GuiView::init()
102 {
103         menubar_.reset(new QLMenubar(this, menubackend));
104         QObject::connect(menuBar(), SIGNAL(triggered(QAction *)),
105                 this, SLOT(updateMenu(QAction *)));
106
107         getToolbars().init();
108
109         statusBar()->setSizeGripEnabled(false);
110
111         QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
112                 this, SLOT(update_view_state_qt()));
113
114         // make sure the buttons are disabled if needed
115         updateToolbars();
116         updateLayoutChoice();
117         updateMenubar();
118 }
119
120
121 void GuiView::saveGeometry()
122 {
123         // FIXME:
124         // change the ifdef to 'geometry = normalGeometry();' only
125         // when Trolltech has fixed the broken normalGeometry on X11:
126         // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
127         // Then also the moveEvent, resizeEvent, and the
128         // code for floatingGeometry_ can be removed;
129         // adjust GuiView::setGeometry()
130 #ifdef Q_OS_WIN32
131         QRect geometry = normalGeometry();
132 #else
133         updateFloatingGeometry();
134         QRect geometry = floatingGeometry_;
135 #endif
136
137         // save windows size and position
138         Session & session = LyX::ref().session();
139         session.saveSessionInfo("WindowWidth", convert<string>(geometry.width()));
140         session.saveSessionInfo("WindowHeight", convert<string>(geometry.height()));
141         session.saveSessionInfo("WindowIsMaximized", (isMaximized() ? "yes" : "no"));
142         if (lyxrc.geometry_xysaved) {
143                 session.saveSessionInfo("WindowPosX", convert<string>(geometry.x()));
144                 session.saveSessionInfo("WindowPosY", convert<string>(geometry.y()));
145         }
146 }
147                                                   
148 void GuiView::setGeometry(unsigned int width,
149                                                                   unsigned int height,
150                                                                   int posx, int posy,
151                                                                   bool maximize)
152 {
153         // only true when the -geometry option was NOT used
154         if (width != 0 && height != 0) {
155                 if (posx != -1 && posy != -1) {
156 #ifdef Q_WS_WIN
157                         // FIXME: use only setGeoemtry when Trolltech has
158                         // fixed the qt4/X11 bug
159                         QMainWindow::setGeometry(posx, posy,width, height);
160 #else
161                         resize(width, height);
162                         move(posx, posy);
163 #endif
164                 } else {
165                         resize(width, height);
166                 }
167
168                 if (maximize)
169                         setWindowState(Qt::WindowMaximized);
170         }
171         
172         show();
173 }
174
175
176 void GuiView::updateMenu(QAction * /*action*/)
177 {
178         menubar_->update();
179 }
180
181
182 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
183 {
184         QMainWindow::setWindowTitle(toqstr(t));
185         QMainWindow::setWindowIconText(toqstr(it));
186 }
187
188
189 void GuiView::addCommandBuffer(QToolBar * toolbar)
190 {
191         commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
192         focus_command_buffer.connect(boost::bind(&GuiView::focus_command_widget, this));
193         toolbar->addWidget(commandbuffer_);
194 }
195
196
197 void GuiView::message(docstring const & str)
198 {
199         statusBar()->showMessage(toqstr(str));
200         statusbar_timer_.stop();
201         statusbar_timer_.start(statusbar_timer_value);
202 }
203
204
205 void GuiView::clearMessage()
206 {
207         update_view_state_qt();
208 }
209
210
211 void GuiView::focus_command_widget()
212 {
213         if (commandbuffer_)
214                 commandbuffer_->focus_command();
215 }
216
217
218 void GuiView::update_view_state_qt()
219 {
220         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
221         statusbar_timer_.stop();
222 }
223
224
225 void GuiView::updateStatusBar()
226 {
227         // let the user see the explicit message
228         if (statusbar_timer_.isActive())
229                 return;
230
231         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
232 }
233
234
235 void GuiView::activated(FuncRequest const & func)
236 {
237         dispatch(func);
238 }
239
240
241 bool GuiView::hasFocus() const
242 {
243         return qApp->activeWindow() == this;
244 }
245
246
247 void  GuiView::updateFloatingGeometry()
248 {
249         if (!isMaximized())
250                 floatingGeometry_ = QRect(x(), y(), width(), height());
251 }
252
253
254 void GuiView::resizeEvent(QResizeEvent *)
255 {
256         updateFloatingGeometry();
257 }
258
259
260 void GuiView::moveEvent(QMoveEvent *)
261 {
262         updateFloatingGeometry();
263 }
264
265
266 void GuiView::closeEvent(QCloseEvent * close_event)
267 {
268         GuiImplementation & gui 
269                 = static_cast<GuiImplementation &>(theApp->gui());
270
271         vector<int> const & view_ids = gui.viewIds();
272
273         if (view_ids.size() == 1 && !theBufferList().quitWriteAll()) {
274                 close_event->ignore();
275                 return;
276         }
277
278         saveGeometry();
279         gui.unregisterView(this);
280 }
281
282
283 void GuiView::show()
284 {
285         QMainWindow::setWindowTitle(qt_("LyX"));
286         QMainWindow::show();
287         updateFloatingGeometry();
288 }
289
290
291 void GuiView::busy(bool yes) const
292 {
293         if (yes)
294                 QApplication::setOverrideCursor(Qt::WaitCursor);
295         else
296                 QApplication::restoreOverrideCursor();
297 }
298
299
300 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarBackend::Toolbar const & tbb)
301 {
302         QLToolbar * Tb = new QLToolbar(tbb, *this);
303         static QLToolbar * lastTb = 0;
304
305         if (tbb.flags & ToolbarBackend::TOP) {
306                         addToolBar(Qt::TopToolBarArea, Tb);
307                         addToolBarBreak(Qt::TopToolBarArea);
308         }
309         if (tbb.flags & ToolbarBackend::BOTTOM) {
310                 addToolBar(Qt::BottomToolBarArea, Tb);
311                 if (lastTb)
312                         insertToolBarBreak(lastTb);
313                 lastTb = Tb;
314         }
315         if (tbb.flags & ToolbarBackend::LEFT) {
316                 addToolBar(Qt::LeftToolBarArea, Tb);
317         }
318         if (tbb.flags & ToolbarBackend::RIGHT) {
319                 addToolBar(Qt::RightToolBarArea, Tb);
320         }
321
322         return Toolbars::ToolbarPtr(Tb);
323 }
324
325 } // namespace frontend
326 } // namespace lyx
327
328 #include "GuiView_moc.cpp"