]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiApplication.C
* qt_helpers.h:
[lyx.git] / src / frontends / qt4 / GuiApplication.C
1 /**
2  * \file qt4/GuiApplication.C
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/Xlib.h>
50 #endif
51
52 #include <boost/bind.hpp>
53
54 using std::string;
55 using std::endl;
56
57 // in QLyXKeySym.C
58 extern void initEncodings();
59
60 ///////////////////////////////////////////////////////////////
61 // You can find other X11 and MACX 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 #ifdef Q_WS_X11
99         // doubleClickInterval() is 400 ms on X11 which is just too long.
100         // On Windows and Mac OS X, the operating system's value is used.
101         // On Microsoft Windows, calling this function sets the double
102         // click interval for all applications. So we don't!
103         QApplication::setDoubleClickInterval(300);
104 #endif
105
106         // install translation file for Qt built-in dialogs
107         QTranslator qt_trans;
108         QString language_name = QString("qt_") + QLocale::system().name();
109         language_name.truncate(5);
110         if (qt_trans.load(language_name,
111                 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
112         {
113                 qApp->installTranslator(&qt_trans);
114                 // even if the language calls for RtL, don't do that
115                 qApp->setLayoutDirection(Qt::LeftToRight);
116                 lyxerr[Debug::GUI]
117                         << "Successfully installed Qt translations for locale "
118                         << fromqstr(language_name) << std::endl;
119         } else
120                 lyxerr[Debug::GUI]
121                         << "Could not find  Qt translations for locale "
122                         << fromqstr(language_name) << std::endl;
123
124 /*#ifdef Q_WS_MACX
125         // These translations are meant to break Qt/Mac menu merging
126         // algorithm on some entries. It lists the menu names that
127         // should not be moved to the LyX menu
128         QTranslator aqua_trans(0);
129         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
130                                              "do_not_merge_me"));
131         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
132                                              "do_not_merge_me"));
133         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
134                                              "do_not_merge_me"));
135         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
136                                              "do_not_merge_me"));
137
138         qApp->installTranslator(&aqua_trans);
139 #endif
140 */
141         using namespace lyx::graphics;
142
143         Image::newImage = boost::bind(&QLImage::newImage);
144         Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
145
146         // needs to be done before reading lyxrc
147         lyxrc.dpi = getDPI();
148
149         LoaderQueue::setPriority(10,100);
150
151         guiApp = this;
152 }
153
154 Clipboard& GuiApplication::clipboard()
155 {
156         return clipboard_;
157 }
158
159
160 Selection& GuiApplication::selection()
161 {
162         return selection_;
163 }
164
165
166 int const GuiApplication::exec()
167 {
168         QTimer::singleShot(1, this, SLOT(execBatchCommands()));
169         return QApplication::exec();
170 }
171
172
173 void GuiApplication::exit(int status)
174 {
175         QApplication::exit(status);
176 }
177
178
179 void GuiApplication::execBatchCommands()
180 {
181         LyX::ref().execBatchCommands();
182 }
183
184
185 string const GuiApplication::romanFontName()
186 {
187         QFont font;
188         font.setKerning(false);
189         font.setStyleHint(QFont::Serif);
190         font.setFamily("serif");
191
192         return fromqstr(QFontInfo(font).family());
193 }
194
195
196 string const GuiApplication::sansFontName()
197 {
198         QFont font;
199         font.setKerning(false);
200         font.setStyleHint(QFont::SansSerif);
201         font.setFamily("sans");
202
203         return fromqstr(QFontInfo(font).family());
204 }
205
206
207 string const GuiApplication::typewriterFontName()
208 {
209         QFont font;
210         font.setKerning(false);
211         font.setStyleHint(QFont::TypeWriter);
212         font.setFamily("monospace");
213
214         return fromqstr(QFontInfo(font).family());
215 }
216
217
218 bool GuiApplication::event(QEvent * e)
219 {
220         switch(e->type()) {
221         case QEvent::FileOpen: {
222                 // Open a file; this happens only on Mac OS X for now
223                 QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
224                 lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
225                                           fromqstr(foe->file())));
226                 return true;
227         }
228         default:
229                 return false;
230         }
231 }
232
233
234 void GuiApplication::syncEvents()
235 {
236         // This is the ONLY place where processEvents may be called.
237         // During screen update/ redraw, this method is disabled to
238         // prevent keyboard events being handed to the LyX core, where
239         // they could cause re-entrant calls to screen update.
240         processEvents(QEventLoop::ExcludeUserInputEvents);
241 }
242
243
244 bool GuiApplication::getRgbColor(LColor_color col,
245         lyx::RGBColor & rgbcol)
246 {
247         QColor const & qcol = color_cache_.get(col);
248         if (!qcol.isValid()) {
249                 rgbcol.r = 0;
250                 rgbcol.g = 0;
251                 rgbcol.b = 0;
252                 return false;
253         }
254         rgbcol.r = qcol.red();
255         rgbcol.g = qcol.green();
256         rgbcol.b = qcol.blue();
257         return true;
258 }
259
260
261 string const GuiApplication::hexName(LColor_color col)
262 {
263         return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
264 }
265
266
267 void GuiApplication::updateColor(LColor_color)
268 {
269         // FIXME: Bleh, can't we just clear them all at once ?
270         color_cache_.clear();
271 }
272
273
274 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
275 {
276         socket_callbacks_[fd] =
277                 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
278 }
279
280 template<>
281 void Application::unregisterSocketCallback<int>(int fd)
282 {
283         GuiApplication* ptr = static_cast<GuiApplication*>(this);
284         ptr->unregisterSocketCallbackImpl(fd);
285 }
286
287 void GuiApplication::unregisterSocketCallbackImpl(int fd)
288 {
289         socket_callbacks_.erase(fd);
290 }
291
292 ////////////////////////////////////////////////////////////////////////
293 // X11 specific stuff goes here...
294 #ifdef Q_WS_X11
295 bool GuiApplication::x11EventFilter(XEvent * xev)
296 {
297         BufferView * bv = currentView().view();
298
299         switch (xev->type) {
300         case SelectionRequest:
301                 lyxerr[Debug::GUI] << "X requested selection." << endl;
302                 if (bv) {
303                         lyx::docstring const sel = bv->requestSelection();
304                         if (!sel.empty())
305                                 selection_.put(sel);
306                 }
307                 break;
308         case SelectionClear:
309                 lyxerr[Debug::GUI] << "Lost selection." << endl;
310                 if (bv)
311                         bv->clearSelection();
312                 break;
313         }
314         return false;
315 }
316 #endif
317
318
319 } // namespace frontend
320 } // namespace lyx
321
322 #include "GuiApplication_moc.cpp"