]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiView.C
* GuiView.C (setGeometry): whitespace and warning.
[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  * \author Peter Kümmel
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "GuiView.h"
17
18 #include "GuiImplementation.h"
19 #include "GuiWorkArea.h"
20 #include "QLyXKeySym.h"
21 #include "QLMenubar.h"
22 #include "QLToolbar.h"
23 #include "QCommandBuffer.h"
24 #include "qt_helpers.h"
25
26 #include "frontends/Application.h"
27 #include "frontends/Gui.h"
28 #include "frontends/WorkArea.h"
29
30 #include "support/filetools.h"
31 #include "support/convert.h"
32 #include "support/lstrings.h"
33
34 #include "BufferView.h"
35 #include "bufferlist.h"
36 #include "debug.h"
37 #include "funcrequest.h"
38 #include "lyx_cb.h"
39 #include "lyxrc.h"
40 #include "lyx_main.h"
41 #include "session.h"
42 #include "lyxfunc.h"
43 #include "MenuBackend.h"
44 #include "buffer.h"
45 #include "bufferlist.h"
46
47 #include <QAction>
48 #include <QApplication>
49 #include <QCloseEvent>
50 #include <QPixmap>
51 #include <QStatusBar>
52 #include <QToolBar>
53 #include <QTabBar>
54 #include <QDesktopWidget>
55 #include <QVBoxLayout>
56
57 #include <boost/bind.hpp>
58 #include <boost/shared_ptr.hpp>
59
60 using std::endl;
61 using std::string;
62 using std::vector;
63
64 namespace lyx {
65
66 using support::FileName;
67 using support::onlyFilename;
68 using support::subst;
69 using support::libFileSearch;
70
71 namespace frontend {
72
73 namespace {
74
75 int const statusbar_timer_value = 3000;
76
77 class TabWidget : public QWidget
78 {
79 public:
80         QTabBar* tabbar;
81
82         TabWidget(QWidget* w, bool topTabBar)
83         {
84                 tabbar = new QTabBar;
85                 QVBoxLayout* layout = new QVBoxLayout;
86                 if (topTabBar) {
87                         layout->addWidget(tabbar);
88                         layout->addWidget(w);
89                 } else {
90                         tabbar->setShape(QTabBar::RoundedSouth);
91                         layout->addWidget(w);
92                         layout->addWidget(tabbar);
93                 }
94                 layout->setMargin(0);
95                 setLayout(layout);
96         }
97
98         void clearTabbar()
99         {
100                 for (int i = tabbar->count() - 1; i >= 0; --i) 
101                         tabbar->removeTab(i);
102         }
103 };
104
105 } // namespace anon
106
107
108 struct GuiView::GuiViewPrivate
109 {
110         std::vector<std::string> tabnames;
111
112         TabWidget* tabWidget;
113
114         int posx_offset;
115         int posy_offset;
116
117         GuiViewPrivate() : tabWidget(0), posx_offset(0), posy_offset(0)
118         {}
119
120         unsigned int smallIconSize;
121         unsigned int normalIconSize;
122         unsigned int bigIconSize;
123         // static needed by "New Window"
124         static unsigned int lastIconSize;
125
126         QMenu* toolBarPopup(GuiView *parent)
127         {
128                 // FIXME: translation 
129                 QMenu* menu = new QMenu(parent);
130                 QActionGroup *iconSizeGroup = new QActionGroup(parent);
131
132                 QAction *smallIcons = new QAction(iconSizeGroup);
133                 smallIcons->setText(qt_("Small-sized icons"));
134                 smallIcons->setCheckable(true);
135                 QObject::connect(smallIcons, SIGNAL(triggered()), parent, SLOT(smallSizedIcons()));
136                 menu->addAction(smallIcons);
137
138                 QAction *normalIcons = new QAction(iconSizeGroup);
139                 normalIcons->setText(qt_("Normal-sized icons"));
140                 normalIcons->setCheckable(true);
141                 QObject::connect(normalIcons, SIGNAL(triggered()), parent, SLOT(normalSizedIcons()));
142                 menu->addAction(normalIcons);
143
144
145                 QAction *bigIcons = new QAction(iconSizeGroup);
146                 bigIcons->setText(qt_("Big-sized icons"));
147                 bigIcons->setCheckable(true);
148                 QObject::connect(bigIcons, SIGNAL(triggered()), parent, SLOT(bigSizedIcons()));
149                 menu->addAction(bigIcons);
150
151                 unsigned int cur = parent->iconSize().width();
152                 if ( cur == parent->d.smallIconSize)
153                         smallIcons->setChecked(true);
154                 else if (cur == parent->d.normalIconSize)
155                         normalIcons->setChecked(true);
156                 else if (cur == parent->d.bigIconSize)
157                         bigIcons->setChecked(true);
158
159                 return menu;
160         }
161 };
162
163
164 unsigned int GuiView::GuiViewPrivate::lastIconSize = 0;
165
166
167 GuiView::GuiView(int id)
168         : QMainWindow(), LyXView(id), commandbuffer_(0), quitting_by_menu_(false),
169           d(*new GuiViewPrivate)
170 {
171         // Qt bug? signal lastWindowClosed does not work
172         setAttribute(Qt::WA_QuitOnClose, false);
173         setAttribute(Qt::WA_DeleteOnClose, true);
174
175         // hardcode here the platform specific icon size
176         d.smallIconSize = 14;   // scaling problems
177         d.normalIconSize = 20;  // ok, default
178         d.bigIconSize = 26;             // better for some math icons
179
180 #ifndef Q_WS_MACX
181         //  assign an icon to main form. We do not do it under Qt/Mac,
182         //  since the icon is provided in the application bundle.
183         FileName const iconname = libFileSearch("images", "lyx", "xpm");
184         if (!iconname.empty())
185                 setWindowIcon(QPixmap(toqstr(iconname.absFilename())));
186 #endif
187 }
188
189
190 GuiView::~GuiView()
191 {
192         menubar_.reset();
193         delete &d;
194 }
195
196
197 void GuiView::close()
198 {
199         quitting_by_menu_ = true;
200         QMainWindow::close();
201         quitting_by_menu_ = false;
202 }
203
204
205 void GuiView::setFocus()
206 {
207         BOOST_ASSERT(work_area_);
208         static_cast<GuiWorkArea *>(work_area_)->setFocus();
209 }
210
211
212 QMenu* GuiView::createPopupMenu()
213 {
214         return d.toolBarPopup(this);
215 }
216
217
218 void GuiView::init()
219 {
220         menubar_.reset(new QLMenubar(this, menubackend));
221         QObject::connect(menuBar(), SIGNAL(triggered(QAction *)),
222                 this, SLOT(updateMenu(QAction *)));
223
224         getToolbars().init();
225
226         statusBar()->setSizeGripEnabled(false);
227
228         QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
229                 this, SLOT(update_view_state_qt()));
230
231         BOOST_ASSERT(work_area_);
232         if (!work_area_->bufferView().buffer() && !theBufferList().empty())
233                 setBuffer(theBufferList().first());
234
235         // make sure the buttons are disabled if needed
236         updateToolbars();
237         updateLayoutChoice();
238         updateMenubar();
239 }
240
241
242 void GuiView::closeEvent(QCloseEvent * close_event)
243 {
244         // we may have been called through the close window button
245         // which bypasses the LFUN machinery.
246         if (!quitting_by_menu_) {
247                 if (!theBufferList().quitWriteAll()) {
248                         close_event->ignore();
249                         return;
250                 }
251         }
252         if (view()->buffer()) {
253                 // save cursor position for opened files to .lyx/session
254                 LyX::ref().session().lastFilePos().save(
255                         FileName(buffer()->fileName()),
256                         boost::tie(view()->cursor().pit(),
257                         view()->cursor().pos()));
258         }
259         theApp()->gui().unregisterView(id());   
260         if (theApp()->gui().viewIds().empty())
261         {
262                 // this is the place where we leave the frontend.
263                 // it is the only point at which we start quitting.
264                 saveGeometry();
265                 close_event->accept();
266                 // quit the event loop
267                 qApp->quit();
268         }
269         close_event->accept();
270 }
271
272
273 void GuiView::saveGeometry()
274 {
275         static bool done = false;
276         if (done)
277                 return;
278         else
279                 done = true;
280
281         // FIXME:
282         // change the ifdef to 'geometry = normalGeometry();' only
283         // when Trolltech has fixed the broken normalGeometry on X11:
284         // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
285         // Then also the moveEvent, resizeEvent, and the
286         // code for floatingGeometry_ can be removed;
287         // adjust GuiView::setGeometry()
288 #ifdef Q_WS_WIN
289         QRect geometry = normalGeometry();
290 #else
291         updateFloatingGeometry();
292         QRect geometry = floatingGeometry_;
293 #endif
294
295         // save windows size and position
296         Session & session = LyX::ref().session();
297         session.sessionInfo().save("WindowWidth", convert<string>(geometry.width()));
298         session.sessionInfo().save("WindowHeight", convert<string>(geometry.height()));
299         session.sessionInfo().save("WindowIsMaximized", (isMaximized() ? "yes" : "no"));
300         session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
301         if (lyxrc.geometry_xysaved) {
302                 session.sessionInfo().save("WindowPosX", convert<string>(geometry.x() + d.posx_offset));
303                 session.sessionInfo().save("WindowPosY", convert<string>(geometry.y() + d.posy_offset));
304         }
305         getToolbars().saveToolbarInfo();
306 }
307                                                   
308
309 void GuiView::setGeometry(unsigned int width,
310                           unsigned int height,
311                           int posx, int posy,
312                           bool maximize,
313                           unsigned int iconSizeXY,
314                           const string & geometryArg)
315 {
316         // use last value (not at startup)
317         if (d.lastIconSize != 0)
318                 setIconSize(d.lastIconSize);
319         else if (iconSizeXY != 0)
320                 setIconSize(iconSizeXY);
321         else
322                 setIconSize(d.normalIconSize);
323
324         // only true when the -geometry option was NOT used
325         if (width != 0 && height != 0) {
326                 if (posx != -1 && posy != -1) {
327                         // if there are ever startup positioning problems 
328                         // on a virtual desktop then check the 6 lines below
329                         // http://doc.trolltech.com/4.2/qdesktopwidget.html 
330                         QDesktopWidget& dw = *qApp->desktop();
331                         QRect desk = dw.availableGeometry(dw.primaryScreen());
332                         (posx >= desk.width() ? posx = 50 : true);
333                         (posy >= desk.height()? posy = 50 : true);
334 #ifdef Q_WS_WIN
335                         // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
336                         QWidget::setGeometry(posx, posy, width, height);
337 #else
338                         resize(width, height);
339                         move(posx, posy);
340 #endif
341                 } else {
342                         resize(width, height);
343                 }
344
345                 if (maximize)
346                         setWindowState(Qt::WindowMaximized);
347         }
348         else
349         {
350                 // FIXME: move this code into parse_geometry() (lyx_main.C)
351 #ifdef Q_WS_WIN
352                 int x, y;
353                 int w, h;
354                 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
355                 re.indexIn(toqstr(geometryArg.c_str()));
356                 w = re.cap(1).toInt();
357                 h = re.cap(2).toInt();
358                 x = re.cap(3).toInt();
359                 y = re.cap(4).toInt();
360                 QWidget::setGeometry( x, y, w, h );
361 #else
362                 // silence warning
363                 (void)geometryArg;
364 #endif
365         }
366
367         show();
368
369         // For an unknown reason, the Window title update is not effective for
370         // the second windows up until it is shown on screen (Qt bug?).
371         updateWindowTitle();
372
373         // after show geometry() has changed (Qt bug?)
374         // we compensate the drift when storing the position
375         d.posx_offset = 0;
376         d.posy_offset = 0;
377         if (width != 0 && height != 0) 
378                 if (posx != -1 && posy != -1) {
379 #ifdef Q_WS_WIN
380                         d.posx_offset = posx - normalGeometry().x();
381                         d.posy_offset = posy - normalGeometry().y();
382 #else
383 #ifndef Q_WS_MACX
384                         if (!maximize) {
385                                 d.posx_offset = posx - geometry().x();
386                                 d.posy_offset = posy - geometry().y();
387                         }
388 #endif
389 #endif
390                 }
391 }
392
393
394 void GuiView::updateMenu(QAction * /*action*/)
395 {
396         menubar_->update();
397 }
398
399
400 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
401 {
402         QString title = windowTitle();
403         QString new_title = toqstr(t);
404         if (title != new_title) {
405                 QMainWindow::setWindowTitle(new_title);
406                 QMainWindow::setWindowIconText(toqstr(it));
407         }
408 }
409
410
411 void GuiView::addCommandBuffer(QToolBar * toolbar)
412 {
413         commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
414         focus_command_buffer.connect(boost::bind(&GuiView::focus_command_widget, this));
415         toolbar->addWidget(commandbuffer_);
416 }
417
418
419 void GuiView::message(docstring const & str)
420 {
421         statusBar()->showMessage(toqstr(str));
422         statusbar_timer_.stop();
423         statusbar_timer_.start(statusbar_timer_value);
424 }
425
426
427 void GuiView::clearMessage()
428 {
429         update_view_state_qt();
430 }
431
432
433 void GuiView::setIconSize(unsigned int size)
434 {
435         d.lastIconSize = size;
436         QMainWindow::setIconSize(QSize(size, size));
437 }
438
439
440 void GuiView::smallSizedIcons()
441 {
442         setIconSize(d.smallIconSize);
443 }
444
445
446 void GuiView::normalSizedIcons()
447 {
448         setIconSize(d.normalIconSize);
449 }
450
451
452 void GuiView::bigSizedIcons()
453 {
454         setIconSize(d.bigIconSize);
455 }
456
457
458 void GuiView::focus_command_widget()
459 {
460         if (commandbuffer_)
461                 commandbuffer_->focus_command();
462 }
463
464
465 void GuiView::update_view_state_qt()
466 {
467         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
468         statusbar_timer_.stop();
469 }
470
471
472 void GuiView::initTab(QWidget* workarea)
473 {
474         // construct the TabWidget with 'false' to have the tabbar at the bottom
475         d.tabWidget = new TabWidget(workarea, true);
476         setCentralWidget(d.tabWidget);
477         QObject::connect(d.tabWidget->tabbar, SIGNAL(currentChanged(int)),
478                         this, SLOT(currentTabChanged(int)));
479 }
480
481
482 void GuiView::updateTab()
483 {
484         std::vector<string> const & names = theBufferList().getFileNames();
485
486         // avoid unnecessary tabbar rebuild: 
487         // check if something has changed
488         if (d.tabnames == names) 
489                 return;
490         d.tabnames = names;
491
492         QTabBar & tabbar = *d.tabWidget->tabbar;
493
494         // update when all is done
495         tabbar.blockSignals(true);
496
497         // remove all tab bars
498         d.tabWidget->clearTabbar();
499
500         string cur_title;
501         if (view()->buffer()) {
502                 cur_title = view()->buffer()->fileName();
503         }
504
505         // rebuild tabbar and function map from scratch
506         if (names.size() > 1) {
507                 for(size_t i = 0; i < names.size(); i++) {
508                         tabbar.addTab(toqstr(onlyFilename(names[i]))); 
509                         // set current tab
510                         if (names[i] == cur_title)
511                                 tabbar.setCurrentIndex(i);
512                 }
513         }
514         tabbar.blockSignals(false);
515 }
516
517
518 void GuiView::currentTabChanged(int i)
519 {
520         BOOST_ASSERT(i >= 0 && size_type(i) < d.tabnames.size());
521         dispatch(FuncRequest(LFUN_BUFFER_SWITCH, d.tabnames[i]));
522 }
523
524
525 void GuiView::updateStatusBar()
526 {
527         // let the user see the explicit message
528         if (statusbar_timer_.isActive())
529                 return;
530
531         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
532 }
533
534
535 void GuiView::activated(FuncRequest const & func)
536 {
537         dispatch(func);
538 }
539
540
541 bool GuiView::hasFocus() const
542 {
543         return qApp->activeWindow() == this;
544 }
545
546
547 void  GuiView::updateFloatingGeometry()
548 {
549         if (!isMaximized())
550                 floatingGeometry_ = QRect(x(), y(), width(), height());
551 }
552
553
554 void GuiView::resizeEvent(QResizeEvent *)
555 {
556         updateFloatingGeometry();
557 }
558
559
560 void GuiView::moveEvent(QMoveEvent *)
561 {
562         updateFloatingGeometry();
563 }
564
565
566 bool GuiView::event(QEvent * e)
567 {
568         // Useful debug code:
569         /*
570         switch (e->type())
571         {
572         case QEvent::WindowActivate:
573         case QEvent::ActivationChange:
574         case QEvent::WindowDeactivate:
575         case QEvent::Paint:
576         case QEvent::Enter:
577         case QEvent::Leave:
578         case QEvent::HoverEnter:
579         case QEvent::HoverLeave:
580         case QEvent::HoverMove:
581         case QEvent::StatusTip:
582                 break;
583         default:
584         */
585
586         if (e->type() == QEvent::ShortcutOverride) {
587                 QKeyEvent * ke = static_cast<QKeyEvent*>(e);
588                 if (ke->key() == Qt::Key_Tab || ke->key() == Qt::Key_Backtab) {
589                         boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
590                         sym->set(ke);
591                         BOOST_ASSERT(work_area_);
592                         work_area_->processKeySym(sym, key_modifier::none);
593                         e->accept();
594                         return true;
595                 }
596         }
597         //} for the debug switch above.
598
599         return QMainWindow::event(e);
600 }
601
602
603 bool GuiView::focusNextPrevChild(bool /*next*/)
604 {
605         setFocus();
606         return true;
607 }
608
609
610 void GuiView::show()
611 {
612         QMainWindow::setWindowTitle(qt_("LyX"));
613         QMainWindow::show();
614         updateFloatingGeometry();
615 }
616
617
618 void GuiView::busy(bool yes)
619 {
620         BOOST_ASSERT(work_area_);
621         static_cast<GuiWorkArea *>(work_area_)->setUpdatesEnabled(!yes);
622
623         if (yes) {
624                 work_area_->stopBlinkingCursor();
625                 QApplication::setOverrideCursor(Qt::WaitCursor);
626         }
627         else {
628                 work_area_->startBlinkingCursor();
629                 QApplication::restoreOverrideCursor();
630         }
631 }
632
633
634 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarBackend::Toolbar const & tbb, bool newline)
635 {
636         QLToolbar * Tb = new QLToolbar(tbb, *this);
637
638         if (tbb.flags & ToolbarBackend::TOP) {
639                 if (newline)
640                         addToolBarBreak(Qt::TopToolBarArea);
641                 addToolBar(Qt::TopToolBarArea, Tb);
642         }
643
644         if (tbb.flags & ToolbarBackend::BOTTOM) {
645 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
646 #if (QT_VERSION >= 0x040202)
647                 if (newline)
648                         addToolBarBreak(Qt::BottomToolBarArea);
649 #endif
650                 addToolBar(Qt::BottomToolBarArea, Tb);
651         }
652
653         if (tbb.flags & ToolbarBackend::LEFT) {
654 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
655 #if (QT_VERSION >= 0x040202)
656                 if (newline)
657                         addToolBarBreak(Qt::LeftToolBarArea);
658 #endif
659                 addToolBar(Qt::LeftToolBarArea, Tb);
660         }
661
662         if (tbb.flags & ToolbarBackend::RIGHT) {
663 // Qt < 4.2.2 cannot handle ToolBarBreak on non-TOP dock.
664 #if (QT_VERSION >= 0x040202)
665                 if (newline)
666                         addToolBarBreak(Qt::RightToolBarArea);
667 #endif
668                 addToolBar(Qt::RightToolBarArea, Tb);
669         }
670
671         // The following does not work so I can not restore to exact toolbar location
672         /*
673         ToolbarSection::ToolbarInfo & info = LyX::ref().session().toolbars().load(tbb.name);
674         Tb->move(info.posx, info.posy);
675         */
676
677         return Toolbars::ToolbarPtr(Tb);
678 }
679
680 } // namespace frontend
681 } // namespace lyx
682
683 #include "GuiView_moc.cpp"