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