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