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