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