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