]> git.lyx.org Git - lyx.git/blob - src/frontends/qt3/QtView.C
This commit introduces the FontLoader interface class. In the future, I intend to...
[lyx.git] / src / frontends / qt3 / 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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "BufferView.h"
15 #include "lyx_cb.h"
16 #include "lyxrc.h"
17 #include "lyx_main.h"
18 #include "session.h"
19 #include "lyxfunc.h"
20 #include "MenuBackend.h"
21 #include "funcrequest.h"
22
23 #include "frontends/Toolbars.h"
24
25 #include "support/filetools.h"
26 #include "support/convert.h"
27
28 #include <boost/bind.hpp>
29
30 #include "QtView.h"
31 #include "QLMenubar.h"
32 #include "QLToolbar.h"
33 #include "qfont_loader.h"
34 #include "QCommandBuffer.h"
35 #include "qt_helpers.h"
36
37 #include <qapplication.h>
38 #include <qpixmap.h>
39 #include <qstatusbar.h>
40
41 using std::string;
42
43 GuiFontLoader fontloader;
44
45 namespace lyx {
46
47 using support::libFileSearch;
48
49 namespace frontend {
50
51 namespace {
52
53 int const statusbar_timer_value = 3000;
54
55 } // namespace anon
56
57
58 QtView::QtView()
59         : QMainWindow(), LyXView(), commandbuffer_(0)
60 {
61         qApp->setMainWidget(this);
62
63 #ifndef Q_WS_MACX
64         //  assign an icon to main form. We do not do it under Qt/Mac,
65         //  since the icon is provided in the application bundle.
66         string const iconname = libFileSearch("images", "lyx", "xpm");
67         if (!iconname.empty())
68                 setIcon(QPixmap(toqstr(iconname)));
69 #endif
70 }
71
72
73 QtView::~QtView()
74 {
75 }
76
77
78 void QtView::init()
79 {
80         menubar_.reset(new QLMenubar(this, menubackend));
81         getToolbars().init();
82
83         statusBar()->setSizeGripEnabled(false);
84
85         connect(&statusbar_timer_, SIGNAL(timeout()), this, SLOT(update_view_state_qt()));
86
87         // make sure the buttons are disabled if needed
88         updateToolbars();
89
90         // allowing the toolbars to tear off is too easily done,
91         // and we don't save their orientation anyway. Disable the handle.
92         setToolBarsMovable(false);
93         
94         updateLayoutChoice();
95         updateMenubar();
96 }
97
98
99 void QtView::setGeometry(unsigned int width,
100                                                  unsigned int height,
101                                                  int posx, int posy,
102                                                  bool maximize)
103 {
104         // only true when the -geometry option was NOT used
105         if (width != 0 && height != 0) {
106                 initFloatingGeometry(QRect(posx, posy, width, height));
107                 resize(width, height);
108                 if (posx != -1 && posy != -1)
109                         move(posx, posy);
110                 show();
111                 if (maximize)
112                         setWindowState(Qt::WindowMaximized);
113         } else
114                 show();
115 }
116
117
118 void QtView::setWindowTitle(docstring const & t, docstring const & it)
119 {
120         setCaption(toqstr(t));
121         setIconText(toqstr(it));
122 }
123
124
125 void QtView::addCommandBuffer(QWidget * parent)
126 {
127         commandbuffer_ = new QCommandBuffer(this, parent, *controlcommand_);
128         focus_command_buffer.connect(boost::bind(&QtView::focus_command_widget, this));
129 }
130
131
132 void QtView::message(docstring const & str)
133 {
134         statusBar()->message(toqstr(str));
135         statusbar_timer_.stop();
136         statusbar_timer_.start(statusbar_timer_value);
137 }
138
139
140 void QtView::clearMessage()
141 {
142         update_view_state_qt();
143 }
144
145
146 void QtView::focus_command_widget()
147 {
148         if (commandbuffer_)
149                 commandbuffer_->focus_command();
150 }
151
152
153 void QtView::update_view_state_qt()
154 {
155         statusBar()->message(toqstr(getLyXFunc().viewStatusMessage()));
156         statusbar_timer_.stop();
157 }
158
159
160 void QtView::updateStatusBar()
161 {
162         // let the user see the explicit message
163         if (statusbar_timer_.isActive())
164                 return;
165
166         statusBar()->message(toqstr(getLyXFunc().viewStatusMessage()));
167 }
168
169
170 void QtView::activated(FuncRequest const & func)
171 {
172         getLyXFunc().dispatch(func);
173 }
174
175
176 bool QtView::hasFocus() const
177 {
178         return qApp->activeWindow() == this;
179 }
180
181
182 void QtView::initFloatingGeometry(QRect const & g)
183 {
184         floatingGeometry_ = g;
185         maxWidth = QApplication::desktop()->width() - 20;
186 }
187
188
189 void QtView::updateFloatingGeometry()
190 {
191         if (width() < maxWidth && frameGeometry().x() > 0)
192                 floatingGeometry_ = QRect(x(), y(), width(), height());
193 }
194
195
196 void QtView::resizeEvent(QResizeEvent *)
197 {
198         maxWidth = std::max(width(), maxWidth);
199
200         updateFloatingGeometry();
201 }
202
203
204 void QtView::moveEvent(QMoveEvent *)
205 {
206         updateFloatingGeometry();
207 }
208
209
210 void QtView::closeEvent(QCloseEvent *)
211 {
212         updateFloatingGeometry();
213         QRect geometry = floatingGeometry_;
214
215         Session & session = LyX::ref().session();
216         session.saveSessionInfo("WindowIsMaximized", (isMaximized() ? "yes" : "no"));
217         // save windows size and position
218         session.saveSessionInfo("WindowWidth", convert<string>(geometry.width()));
219         session.saveSessionInfo("WindowHeight", convert<string>(geometry.height()));
220         if (lyxrc.geometry_xysaved) {
221                 session.saveSessionInfo("WindowPosX", convert<string>(geometry.x()));
222                 session.saveSessionInfo("WindowPosY", convert<string>(geometry.y()));
223         }
224         // trigger LFUN_LYX_QUIT instead of quit directly
225         // since LFUN_LYX_QUIT may have more cleanup stuff
226         getLyXFunc().dispatch(FuncRequest(LFUN_LYX_QUIT));
227 }
228
229
230 void QtView::show()
231 {
232         setCaption(qt_("LyX"));
233         QMainWindow::show();
234         updateFloatingGeometry();
235 }
236
237
238 void QtView::busy(bool yes) const
239 {
240         if (yes)
241                 QApplication::setOverrideCursor(Qt::waitCursor);
242         else
243                 QApplication::restoreOverrideCursor();
244 }
245
246
247 Toolbars::ToolbarPtr QtView::makeToolbar(ToolbarBackend::Toolbar const & tbb)
248 {
249         return make_toolbar(tbb, *this);
250 }
251
252 } // namespace frontend
253 } // namespace lyx
254
255 #include "QtView_moc.cpp"