]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiApplication.cpp
Rename .C ==> .cpp for files in src/frontends/qt4, part two
[lyx.git] / src / frontends / qt4 / GuiApplication.cpp
1 /**
2  * \file qt4/GuiApplication.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
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 "GuiApplication.h"
16
17 #include "qt_helpers.h"
18 #include "QLImage.h"
19 #include "socket_callback.h"
20
21 #include "frontends/LyXView.h"
22
23 #include "graphics/LoaderQueue.h"
24
25 #include "support/lstrings.h"
26 #include "support/os.h"
27 #include "support/package.h"
28
29 #include "BufferView.h"
30 #include "Color.h"
31 #include "debug.h"
32 #include "funcrequest.h"
33 #include "lyx_main.h"
34 #include "lyxfunc.h"
35 #include "lyxrc.h"
36
37 #include <QApplication>
38 #include <QClipboard>
39 #include <QEventLoop>
40 #include <QFileOpenEvent>
41 #include <QLocale>
42 #include <QLibraryInfo>
43 #include <QTextCodec>
44 #include <QTimer>
45 #include <QTranslator>
46 #include <QWidget>
47
48 #ifdef Q_WS_X11
49 #include <X11/Xatom.h>
50 #include <X11/Xlib.h>
51 #endif
52
53 #include <boost/bind.hpp>
54
55 #include <exception>
56
57 using std::string;
58 using std::endl;
59
60 ///////////////////////////////////////////////////////////////
61 // You can find other X11 specific stuff
62 // at the end of this file...
63 ///////////////////////////////////////////////////////////////
64
65 namespace {
66
67 int getDPI()
68 {
69         QWidget w;
70         return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
71 }
72
73 } // namespace anon
74
75
76 namespace lyx {
77
78 frontend::Application * createApplication(int & argc, char * argv[])
79 {
80         return new frontend::GuiApplication(argc, argv);
81 }
82
83
84 namespace frontend {
85
86 GuiApplication * guiApp;
87
88
89 GuiApplication::~GuiApplication()
90 {
91         socket_callbacks_.clear();
92 }
93
94
95 GuiApplication::GuiApplication(int & argc, char ** argv)
96         : QApplication(argc, argv), Application(argc, argv)
97 {
98         // Qt bug? setQuitOnLastWindowClosed(true); does not work
99         setQuitOnLastWindowClosed(false);
100
101 #ifdef Q_WS_X11
102         // doubleClickInterval() is 400 ms on X11 which is just too long.
103         // On Windows and Mac OS X, the operating system's value is used.
104         // On Microsoft Windows, calling this function sets the double
105         // click interval for all applications. So we don't!
106         QApplication::setDoubleClickInterval(300);
107 #endif
108
109         // install translation file for Qt built-in dialogs
110         QString language_name = QString("qt_") + QLocale::system().name();
111         language_name.truncate(5);
112         if (qt_trans_.load(language_name,
113                 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
114         {
115                 qApp->installTranslator(&qt_trans_);
116                 // even if the language calls for RtL, don't do that
117                 qApp->setLayoutDirection(Qt::LeftToRight);
118                 LYXERR(Debug::GUI)
119                         << "Successfully installed Qt translations for locale "
120                         << fromqstr(language_name) << std::endl;
121         } else
122                 LYXERR(Debug::GUI)
123                         << "Could not find  Qt translations for locale "
124                         << fromqstr(language_name) << std::endl;
125
126         using namespace lyx::graphics;
127
128         Image::newImage = boost::bind(&QLImage::newImage);
129         Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
130
131         // needs to be done before reading lyxrc
132         lyxrc.dpi = getDPI();
133
134         LoaderQueue::setPriority(10,100);
135
136         guiApp = this;
137 }
138
139
140 Clipboard& GuiApplication::clipboard()
141 {
142         return clipboard_;
143 }
144
145
146 Selection& GuiApplication::selection()
147 {
148         return selection_;
149 }
150
151
152 int const GuiApplication::exec()
153 {
154         QTimer::singleShot(1, this, SLOT(execBatchCommands()));
155         return QApplication::exec();
156 }
157
158
159 void GuiApplication::exit(int status)
160 {
161         QApplication::exit(status);
162 }
163
164
165 void GuiApplication::execBatchCommands()
166 {
167         LyX::ref().execBatchCommands();
168 }
169
170
171 string const GuiApplication::romanFontName()
172 {
173         QFont font;
174         font.setKerning(false);
175         font.setStyleHint(QFont::Serif);
176         font.setFamily("serif");
177
178         return fromqstr(QFontInfo(font).family());
179 }
180
181
182 string const GuiApplication::sansFontName()
183 {
184         QFont font;
185         font.setKerning(false);
186         font.setStyleHint(QFont::SansSerif);
187         font.setFamily("sans");
188
189         return fromqstr(QFontInfo(font).family());
190 }
191
192
193 string const GuiApplication::typewriterFontName()
194 {
195         QFont font;
196         font.setKerning(false);
197         font.setStyleHint(QFont::TypeWriter);
198         font.setFamily("monospace");
199
200         return fromqstr(QFontInfo(font).family());
201 }
202
203
204 bool GuiApplication::event(QEvent * e)
205 {
206         switch(e->type()) {
207         case QEvent::FileOpen: {
208                 // Open a file; this happens only on Mac OS X for now
209                 QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
210                 lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
211                                           fromqstr(foe->file())));
212                 return true;
213         }
214         default:
215                 return QApplication::event(e);
216         }
217 }
218
219
220 bool GuiApplication::notify(QObject * receiver, QEvent * event)
221 {
222         bool return_value;
223         try {
224                 return_value = QApplication::notify(receiver, event);
225         }
226         catch (std::exception  const & e) {
227                 lyxerr << "Caught \"normal\" exception: " << e.what() << endl;
228                 LyX::cref().emergencyCleanup();
229                 abort();
230         }
231         catch (...) {
232                 lyxerr << "Caught some really weird exception..." << endl;
233                 LyX::cref().emergencyCleanup();
234                 abort();
235         }
236
237         return return_value;
238 }
239
240
241 void GuiApplication::syncEvents()
242 {
243         // This is the ONLY place where processEvents may be called.
244         // During screen update/ redraw, this method is disabled to
245         // prevent keyboard events being handed to the LyX core, where
246         // they could cause re-entrant calls to screen update.
247         processEvents(QEventLoop::ExcludeUserInputEvents);
248 }
249
250
251 bool GuiApplication::getRgbColor(LColor_color col,
252         RGBColor & rgbcol)
253 {
254         QColor const & qcol = color_cache_.get(col);
255         if (!qcol.isValid()) {
256                 rgbcol.r = 0;
257                 rgbcol.g = 0;
258                 rgbcol.b = 0;
259                 return false;
260         }
261         rgbcol.r = qcol.red();
262         rgbcol.g = qcol.green();
263         rgbcol.b = qcol.blue();
264         return true;
265 }
266
267
268 string const GuiApplication::hexName(LColor_color col)
269 {
270         return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
271 }
272
273
274 void GuiApplication::updateColor(LColor_color)
275 {
276         // FIXME: Bleh, can't we just clear them all at once ?
277         color_cache_.clear();
278 }
279
280
281 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
282 {
283         socket_callbacks_[fd] =
284                 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
285 }
286
287
288 void GuiApplication::unregisterSocketCallback(int fd)
289 {
290         socket_callbacks_.erase(fd);
291 }
292
293 ////////////////////////////////////////////////////////////////////////
294 // X11 specific stuff goes here...
295 #ifdef Q_WS_X11
296 bool GuiApplication::x11EventFilter(XEvent * xev)
297 {
298         if (!currentView())
299                 return false;
300
301         switch (xev->type) {
302         case SelectionRequest: {
303                 if (xev->xselectionrequest.selection != XA_PRIMARY)
304                         break;
305                 LYXERR(Debug::GUI) << "X requested selection." << endl;
306                 BufferView * bv = currentView()->view();
307                 if (bv) {
308                         docstring const sel = bv->requestSelection();
309                         if (!sel.empty())
310                                 selection_.put(sel);
311                 }
312                 break;
313         }
314         case SelectionClear: {
315                 if (xev->xselectionclear.selection != XA_PRIMARY)
316                         break;
317                 LYXERR(Debug::GUI) << "Lost selection." << endl;
318                 BufferView * bv = currentView()->view();
319                 if (bv)
320                         bv->clearSelection();
321                 break;
322         }
323         }
324         return false;
325 }
326 #endif
327
328
329 } // namespace frontend
330 } // namespace lyx
331
332 #include "GuiApplication_moc.cpp"