]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QtView.C
Fix bug 2485 and crash on middle mouse paste on math
[lyx.git] / src / frontends / qt4 / QtView.C
1 /**
2  * \file QtView.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  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "BufferView.h"
16 #include "lyx_cb.h"
17 #include "lyxrc.h"
18 #include "lyx_main.h"
19 #include "session.h"
20 #include "lyxfunc.h"
21 #include "MenuBackend.h"
22 #include "funcrequest.h"
23 #include "funcrequest.h"
24
25 #include "debug.h"
26
27 #include "frontends/Toolbars.h"
28
29 #include "support/filetools.h"
30
31 #include "support/convert.h"
32 #include <boost/bind.hpp>
33
34 #include "QtView.h"
35 #include "QLMenubar.h"
36 #include "qfont_loader.h"
37 #include "QCommandBuffer.h"
38 #include "qt_helpers.h"
39
40 #include <QApplication>
41 #include <QPixmap>
42 #include <QStatusBar>
43 #include <QToolBar>
44 #include <QCloseEvent>
45 #include <QAction>
46 #include <QMenu>
47 #include <QMenuBar>
48
49 #include "support/lstrings.h"
50
51
52 using std::string;
53 using std::endl;
54
55 FontLoader fontloader;
56
57 namespace lyx {
58
59 using support::subst;
60 using support::LibFileSearch;
61
62 namespace frontend {
63
64 namespace {
65
66 int const statusbar_timer_value = 3000;
67
68 } // namespace anon
69
70
71 QtView::QtView(unsigned int width, unsigned int height)
72         : QMainWindow(), LyXView(), commandbuffer_(0)
73 {
74         resize(width, height);
75
76         qApp->setMainWidget(this);
77
78 //      setToolButtonStyle(Qt::ToolButtonIconOnly);
79 //      setIconSize(QSize(12,12));
80
81         bufferview_.reset(new BufferView(this, width, height));
82
83         menubar_.reset(new QLMenubar(this, menubackend));
84         connect(menuBar(), SIGNAL(triggered(QAction *)), this, SLOT(updateMenu(QAction *)));
85
86         getToolbars().init();
87
88         statusBar()->setSizeGripEnabled(false);
89
90         view_state_changed.connect(boost::bind(&QtView::update_view_state, this));
91         connect(&statusbar_timer_, SIGNAL(timeout()), this, SLOT(update_view_state_qt()));
92
93 #ifndef Q_WS_MACX
94         //  assign an icon to main form. We do not do it under Qt/Mac,
95         //  since the icon is provided in the application bundle.
96         string const iconname = LibFileSearch("images", "lyx", "xpm");
97         if (!iconname.empty())
98                 setIcon(QPixmap(toqstr(iconname)));
99 #endif
100
101         // make sure the buttons are disabled if needed
102         updateToolbars();
103 }
104
105
106 QtView::~QtView()
107 {
108 }
109
110 void QtView::updateMenu(QAction *action)
111 {
112         menubar_->update();
113 }
114
115 void QtView::setWindowTitle(string const & t, string const & it)
116 {
117         QMainWindow::setWindowTitle(toqstr(t));
118         QMainWindow::setWindowIconText(toqstr(it));
119 }
120
121
122 void QtView::addCommandBuffer(QToolBar * toolbar)
123 {
124         commandbuffer_ = new QCommandBuffer(this, *controlcommand_);
125         focus_command_buffer.connect(boost::bind(&QtView::focus_command_widget, this));
126         toolbar->addWidget(commandbuffer_);
127 }
128
129
130 void QtView::message(string const & str)
131 {
132         statusBar()->message(toqstr(str));
133         statusbar_timer_.stop();
134         statusbar_timer_.start(statusbar_timer_value);
135 }
136
137
138 void QtView::clearMessage()
139 {
140         update_view_state_qt();
141 }
142
143
144 void QtView::focus_command_widget()
145 {
146         if (commandbuffer_)
147                 commandbuffer_->focus_command();
148 }
149
150
151 void QtView::update_view_state_qt()
152 {
153         statusBar()->message(toqstr(getLyXFunc().viewStatusMessage()));
154         statusbar_timer_.stop();
155 }
156
157
158 void QtView::update_view_state()
159 {
160         // let the user see the explicit message
161         if (statusbar_timer_.isActive())
162                 return;
163
164         statusBar()->message(toqstr(getLyXFunc().viewStatusMessage()));
165 }
166
167
168 void QtView::activated(FuncRequest const & func)
169 {
170         getLyXFunc().dispatch(func);
171 }
172
173
174 bool QtView::hasFocus() const
175 {
176         return qApp->activeWindow() == this;
177 }
178
179
180 void QtView::closeEvent(QCloseEvent *)
181 {
182         // save windows size and position
183         LyX::ref().session().saveSessionInfo("WindowWidth", convert<string>(width()));
184         LyX::ref().session().saveSessionInfo("WindowHeight", convert<string>(height()));
185         if (lyxrc.geometry_xysaved) {
186                 LyX::ref().session().saveSessionInfo("WindowPosX", convert<string>(x()));
187                 LyX::ref().session().saveSessionInfo("WindowPosY", convert<string>(y()));
188         }
189         // trigger LFUN_QUIT instead of quit directly
190         // since LFUN_QUIT may have more cleanup stuff
191         getLyXFunc().dispatch(FuncRequest(LFUN_QUIT));
192 }
193
194
195 void QtView::show()
196 {
197         setCaption(qt_("LyX"));
198         QMainWindow::show();
199 }
200
201
202 void QtView::busy(bool yes) const
203 {
204         if (yes)
205                 QApplication::setOverrideCursor(Qt::waitCursor);
206         else
207                 QApplication::restoreOverrideCursor();
208 }
209
210 } // namespace frontend
211 } // namespace lyx