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