]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiView.C
* qt_helpers.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  * \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         // hardcode here the platform specific icon size
159         d.smallIconSize = 14;   // scaling problems
160         d.normalIconSize = 20;  // ok, default
161         d.bigIconSize = 26;             // better for some math icons
162
163         //bufferview_.reset(new BufferView(this, width, height));
164
165 #ifndef Q_WS_MACX
166         //  assign an icon to main form. We do not do it under Qt/Mac,
167         //  since the icon is provided in the application bundle.
168         FileName const iconname = libFileSearch("images", "lyx", "xpm");
169         if (!iconname.empty())
170                 setWindowIcon(QPixmap(toqstr(iconname.absFilename())));
171 #endif
172 }
173
174
175 GuiView::~GuiView()
176 {
177         delete &d;
178 }
179
180
181 void GuiView::close()
182 {
183         QMainWindow::close();
184 }
185
186 QMenu* GuiView::createPopupMenu()
187 {
188         return d.toolBarPopup(this);
189 }
190
191 void GuiView::init()
192 {
193         menubar_.reset(new QLMenubar(this, menubackend));
194         QObject::connect(menuBar(), SIGNAL(triggered(QAction *)),
195                 this, SLOT(updateMenu(QAction *)));
196
197         getToolbars().init();
198
199         statusBar()->setSizeGripEnabled(false);
200
201         QObject::connect(&statusbar_timer_, SIGNAL(timeout()),
202                 this, SLOT(update_view_state_qt()));
203
204         if (!work_area_->bufferView().buffer() && !theBufferList().empty())
205                 setBuffer(theBufferList().first());
206
207         // make sure the buttons are disabled if needed
208         updateToolbars();
209         updateLayoutChoice();
210         updateMenubar();
211 }
212
213
214 void GuiView::saveGeometry()
215 {
216         // FIXME:
217         // change the ifdef to 'geometry = normalGeometry();' only
218         // when Trolltech has fixed the broken normalGeometry on X11:
219         // http://www.trolltech.com/developer/task-tracker/index_html?id=119684+&method=entry
220         // Then also the moveEvent, resizeEvent, and the
221         // code for floatingGeometry_ can be removed;
222         // adjust GuiView::setGeometry()
223 #ifdef Q_WS_WIN
224         QRect geometry = normalGeometry();
225 #else
226         updateFloatingGeometry();
227         QRect geometry = floatingGeometry_;
228 #endif
229
230         // save windows size and position
231         Session & session = LyX::ref().session();
232         session.sessionInfo().save("WindowWidth", convert<string>(geometry.width()));
233         session.sessionInfo().save("WindowHeight", convert<string>(geometry.height()));
234         session.sessionInfo().save("WindowIsMaximized", (isMaximized() ? "yes" : "no"));
235         session.sessionInfo().save("IconSizeXY", convert<string>(iconSize().width()));
236         if (lyxrc.geometry_xysaved) {
237                 session.sessionInfo().save("WindowPosX", convert<string>(geometry.x() + d.posx_offset));
238                 session.sessionInfo().save("WindowPosY", convert<string>(geometry.y() + d.posy_offset));
239         }
240         getToolbars().saveToolbarInfo();
241 }
242                                                   
243 void GuiView::setGeometry(unsigned int width,
244                                                                   unsigned int height,
245                                                                   int posx, int posy,
246                                                                   bool maximize,
247                                                                   unsigned int iconSizeXY,
248                                                                   const std::string & geometryArg)
249 {
250         // use last value (not at startup)
251         if (d.lastIconSize != 0)
252                 setIconSize(d.lastIconSize);
253         else if (iconSizeXY != 0)
254                 setIconSize(iconSizeXY);
255         else
256                 setIconSize(d.normalIconSize);
257
258         // only true when the -geometry option was NOT used
259         if (width != 0 && height != 0) {
260                 if (posx != -1 && posy != -1) {
261                         // if there are ever startup positioning problems 
262                         // on a virtual desktop then check the 6 lines below
263                         // http://doc.trolltech.com/4.2/qdesktopwidget.html 
264                         QDesktopWidget& dw = *qApp->desktop();
265                         QRect desk = dw.availableGeometry(dw.primaryScreen());
266                         (posx >= desk.width() ? posx = 50 : true);
267                         (posy >= desk.height()? posy = 50 : true);
268 #ifdef Q_WS_WIN
269                         // FIXME: use setGeometry only when Trolltech has fixed the qt4/X11 bug
270                         QWidget::setGeometry(posx, posy, width, height);
271 #else
272                         resize(width, height);
273                         move(posx, posy);
274 #endif
275                 } else {
276                         resize(width, height);
277                 }
278
279                 if (maximize)
280                         setWindowState(Qt::WindowMaximized);
281         }
282         else
283         {
284                 // FIXME: move this code into parse_geometry() (lyx_main.C)
285 #ifdef Q_WS_WIN
286                 int x, y;
287                 int w, h;
288                 QRegExp re( "[=]*(?:([0-9]+)[xX]([0-9]+)){0,1}[ ]*(?:([+-][0-9]*)([+-][0-9]*)){0,1}" );
289                 re.indexIn( toqstr(geometryArg.c_str()));
290                 w = re.cap( 1 ).toInt();
291                 h = re.cap( 2 ).toInt();
292                 x = re.cap( 3 ).toInt();
293                 y = re.cap( 4 ).toInt();
294                 QWidget::setGeometry( x, y, w, h );
295 #endif
296         }
297
298         show();
299
300         // For an unknown reason, the Window title update is not effective for
301         // the second windows up until it is shown on screen (Qt bug?).
302         updateWindowTitle();
303
304         // after show geometry() has changed (Qt bug?)
305         // we compensate the drift when storing the position
306         d.posx_offset = 0;
307         d.posy_offset = 0;
308         if (width != 0 && height != 0) 
309                 if (posx != -1 && posy != -1) {
310 #ifdef Q_WS_WIN
311                         d.posx_offset = posx - normalGeometry().x();
312                         d.posy_offset = posy - normalGeometry().y();
313 #else
314                         if (!maximize) {
315                                 d.posx_offset = posx - geometry().x();
316                                 d.posy_offset = posy - geometry().y();
317                         }
318 #endif
319                 }
320 }
321
322
323 void GuiView::updateMenu(QAction * /*action*/)
324 {
325         menubar_->update();
326 }
327
328
329 void GuiView::setWindowTitle(docstring const & t, docstring const & it)
330 {
331         QString title = windowTitle();
332         QString new_title = toqstr(t);
333         if (title != new_title) {
334                 QMainWindow::setWindowTitle(new_title);
335                 QMainWindow::setWindowIconText(toqstr(it));
336         }
337 }
338
339
340 void GuiView::addCommandBuffer(QToolBar * toolbar)
341 {
342         commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
343         focus_command_buffer.connect(boost::bind(&GuiView::focus_command_widget, this));
344         toolbar->addWidget(commandbuffer_);
345 }
346
347
348 void GuiView::message(docstring const & str)
349 {
350         statusBar()->showMessage(toqstr(str));
351         statusbar_timer_.stop();
352         statusbar_timer_.start(statusbar_timer_value);
353 }
354
355
356 void GuiView::clearMessage()
357 {
358         update_view_state_qt();
359 }
360
361 void GuiView::setIconSize(unsigned int size)
362 {
363         d.lastIconSize = size;
364         QMainWindow::setIconSize(QSize(size, size));
365 }
366
367 void GuiView::smallSizedIcons()
368 {
369         setIconSize(d.smallIconSize);
370 }
371
372 void GuiView::normalSizedIcons()
373 {
374         setIconSize(d.normalIconSize);
375 }
376
377 void GuiView::bigSizedIcons()
378 {
379         setIconSize(d.bigIconSize);
380 }
381
382
383 void GuiView::focus_command_widget()
384 {
385         if (commandbuffer_)
386                 commandbuffer_->focus_command();
387 }
388
389
390 void GuiView::update_view_state_qt()
391 {
392         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
393         statusbar_timer_.stop();
394 }
395
396 void GuiView::initTab(QWidget* workarea)
397 {
398         d.wt = new WidgetWithTabBar(workarea);
399         setCentralWidget(d.wt);
400         QObject::connect(d.wt->tabbar, SIGNAL(currentChanged(int)),
401                         this, SLOT(currentTabChanged(int)));
402 }
403
404 void GuiView::updateTab()
405 {
406         QTabBar& tb = *d.wt->tabbar;
407
408         // update when all  is done
409         tb.blockSignals(true);
410
411         typedef std::vector<string> Strings;
412         Strings const names = theBufferList().getFileNames();
413         size_t n_size = names.size();
414
415         Strings addtab;
416         // show tabs only when there is more 
417         // than one file opened
418         if (n_size > 1)
419         {
420                 for (size_t i = 0; i != n_size; i++) 
421                         if (d.namemap.find(names[i]) == d.namemap.end())
422                                 addtab.push_back(names.at(i));
423         }
424
425         for(size_t i = 0; i<addtab.size(); i++)
426         {
427                 QString tab_name = lyx::toqstr(onlyFilename(addtab.at(i))); 
428                 d.namemap.insert(GuiViewPrivate::NameMapPair(addtab.at(i), tab_name));
429                 tb.addTab(tab_name);
430         }
431
432         // check if all names showed by the tabs
433         // are also in the current bufferlist
434         Strings removetab;
435         bool notall = true;
436         if (n_size < 2)
437                 notall = false;
438         std::map<string, QString>::iterator tabit = d.namemap.begin();
439         for (;tabit != d.namemap.end(); ++tabit)
440         {
441                 bool found = false;
442                 for (size_t i = 0; i != n_size; i++) 
443                         if (tabit->first == names.at(i) && notall)
444                                 found = true;
445                 if (!found)
446                         removetab.push_back(tabit->first);
447         }
448         
449
450         // remove tabs
451         for(size_t i = 0; i<removetab.size(); i++)
452         {
453                 if (d.namemap.find(removetab.at(i)) != d.namemap.end())
454                 {
455                         tabit = d.namemap.find(removetab.at(i));
456                         for (int i = 0; i < tb.count(); i++)
457                                 if (tb.tabText(i) == tabit->second)
458                                 {
459                                         tb.removeTab(i);
460                                         break;
461                                 }
462                         d.namemap.erase(tabit);
463                 }
464         }
465
466         // rebuild func map
467         if (removetab.size() > 0 || addtab.size() > 0)
468         {
469                 d.funcmap.clear();
470                 tabit = d.namemap.begin();
471                 for (;tabit != d.namemap.end(); ++tabit)
472                 {
473                         QTabBar& tb = *d.wt->tabbar;
474                         for (int i = 0; i < tb.count(); i++)
475                         {
476                                 if (tb.tabText(i) == tabit->second)
477                                 {
478                                         FuncRequest func(LFUN_BUFFER_SWITCH, tabit->first);
479                                         d.funcmap.insert(GuiViewPrivate::FuncMapPair(i, func));
480                                         break;
481                                 }
482                         }
483                 }
484         }
485
486         // set current tab
487         if (view()->buffer()) 
488         {
489                 string cur_title = view()->buffer()->fileName();
490                 if (d.namemap.find(cur_title) != d.namemap.end())
491                 {
492                         QString tabname = d.namemap.find(cur_title)->second;
493                         for (int i = 0; i < tb.count(); i++)
494                                 if (tb.tabText(i) == tabname)
495                                 {
496                                         tb.setCurrentIndex(i);
497                                         break;
498                                 }
499                 }
500         }
501
502         tb.blockSignals(false);
503         d.wt->update();
504 }
505
506 void GuiView::currentTabChanged (int index)
507 {
508         std::map<int, FuncRequest>::const_iterator it = d.funcmap.find(index);
509         if (it != d.funcmap.end())
510                 activated(it->second);
511 }
512
513
514 void GuiView::updateStatusBar()
515 {
516         // let the user see the explicit message
517         if (statusbar_timer_.isActive())
518                 return;
519
520         statusBar()->showMessage(toqstr(theLyXFunc().viewStatusMessage()));
521 }
522
523
524 void GuiView::activated(FuncRequest const & func)
525 {
526         dispatch(func);
527 }
528
529
530 bool GuiView::hasFocus() const
531 {
532         return qApp->activeWindow() == this;
533 }
534
535
536 void  GuiView::updateFloatingGeometry()
537 {
538         if (!isMaximized())
539                 floatingGeometry_ = QRect(x(), y(), width(), height());
540 }
541
542
543 void GuiView::resizeEvent(QResizeEvent *)
544 {
545         updateFloatingGeometry();
546 }
547
548
549 void GuiView::moveEvent(QMoveEvent *)
550 {
551         updateFloatingGeometry();
552 }
553
554
555 void GuiView::closeEvent(QCloseEvent * close_event)
556 {
557         GuiImplementation & gui 
558                 = static_cast<GuiImplementation &>(theApp()->gui());
559
560         vector<int> const & view_ids = gui.viewIds();
561
562         if (view_ids.size() == 1 && !theBufferList().quitWriteAll()) {
563                 close_event->ignore();
564                 return;
565         }
566
567         saveGeometry();
568         hide(); // don't remove this hide, it prevents a crash on exit
569         gui.unregisterView(this);       
570 }
571
572
573 void GuiView::show()
574 {
575         QMainWindow::setWindowTitle(qt_("LyX"));
576         QMainWindow::show();
577         updateFloatingGeometry();
578 }
579
580
581 void GuiView::busy(bool yes)
582 {
583         static_cast<GuiWorkArea *>(work_area_)->setUpdatesEnabled(!yes);
584
585         if (yes) {
586                 work_area_->stopBlinkingCursor();
587                 QApplication::setOverrideCursor(Qt::WaitCursor);
588         }
589         else {
590                 work_area_->startBlinkingCursor();
591                 QApplication::restoreOverrideCursor();
592         }
593 }
594
595
596 Toolbars::ToolbarPtr GuiView::makeToolbar(ToolbarBackend::Toolbar const & tbb)
597 {
598         QLToolbar * Tb = new QLToolbar(tbb, *this);
599         //static QLToolbar * lastTb = 0;
600
601         if (tbb.flags & ToolbarBackend::TOP) {
602                         addToolBar(Qt::TopToolBarArea, Tb);
603                         addToolBarBreak(Qt::TopToolBarArea);
604         }
605         if (tbb.flags & ToolbarBackend::BOTTOM) {
606                 addToolBar(Qt::BottomToolBarArea, Tb);
607                 /*
608                 // Qt bug:
609                 // http://www.trolltech.com/developer/task-tracker/index_html?id=137015&method=entry
610                 // Doesn't work because the toolbar will evtl. be hidden.
611                 if (lastTb)
612                         insertToolBarBreak(lastTb);
613                 lastTb = Tb;
614                 */
615         }
616         if (tbb.flags & ToolbarBackend::LEFT) {
617                 addToolBar(Qt::LeftToolBarArea, Tb);
618         }
619         if (tbb.flags & ToolbarBackend::RIGHT) {
620                 addToolBar(Qt::RightToolBarArea, Tb);
621         }
622
623         return Toolbars::ToolbarPtr(Tb);
624 }
625
626 } // namespace frontend
627 } // namespace lyx
628
629 #include "GuiView_moc.cpp"