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