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