]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiWorkArea.C
comment out debug statement.
[lyx.git] / src / frontends / qt4 / GuiWorkArea.C
1 /**
2  * \file GuiWorkArea.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "GuiWorkArea.h"
15
16 #include "GuiApplication.h"
17 #include "QLPainter.h"
18 #include "QLyXKeySym.h"
19 #include "qt_helpers.h"
20
21 #include "gettext.h"
22 #include "LyXView.h"
23
24 #include "BufferView.h"
25 #include "rowpainter.h"
26 #include "debug.h"
27 #include "funcrequest.h"
28 #include "LColor.h"
29 #include "version.h"
30 #include "lyxrc.h"
31
32 #include "support/filetools.h" // LibFileSearch
33 #include "support/os.h"
34 #include "support/convert.h"
35
36 #include "graphics/GraphicsImage.h"
37 #include "graphics/GraphicsLoader.h"
38
39 #include <QLayout>
40 #include <QMainWindow>
41 #include <QMimeData>
42 #include <QUrl>
43 #include <QDragEnterEvent>
44 #include <QPainter>
45 #include <QScrollBar>
46
47 #include <boost/bind.hpp>
48 #include <boost/current_function.hpp>
49
50 #ifdef Q_WS_WIN
51 int const CursorWidth = 2;
52 #else
53 int const CursorWidth = 1;
54 #endif
55
56
57 using std::endl;
58 using std::string;
59
60 namespace os = lyx::support::os;
61
62
63 namespace lyx {
64
65 using support::FileName;
66
67 /// return the LyX key state from Qt's
68 static key_modifier::state q_key_state(Qt::KeyboardModifiers state)
69 {
70         key_modifier::state k = key_modifier::none;
71         if (state & Qt::ControlModifier)
72                 k |= key_modifier::ctrl;
73         if (state & Qt::ShiftModifier)
74                 k |= key_modifier::shift;
75         if (state & Qt::AltModifier || state & Qt::MetaModifier)
76                 k |= key_modifier::alt;
77         return k;
78 }
79
80
81 /// return the LyX mouse button state from Qt's
82 static mouse_button::state q_button_state(Qt::MouseButton button)
83 {
84         mouse_button::state b = mouse_button::none;
85         switch (button) {
86                 case Qt::LeftButton:
87                         b = mouse_button::button1;
88                         break;
89                 case Qt::MidButton:
90                         b = mouse_button::button2;
91                         break;
92                 case Qt::RightButton:
93                         b = mouse_button::button3;
94                         break;
95                 default:
96                         break;
97         }
98         return b;
99 }
100
101
102 /// return the LyX mouse button state from Qt's
103 mouse_button::state q_motion_state(Qt::MouseButtons state)
104 {
105         mouse_button::state b = mouse_button::none;
106         if (state & Qt::LeftButton)
107                 b |= mouse_button::button1;
108         if (state & Qt::MidButton)
109                 b |= mouse_button::button2;
110         if (state & Qt::RightButton)
111                 b |= mouse_button::button3;
112         return b;
113 }
114
115
116 namespace frontend {
117
118 class CursorWidget {
119 public:
120         CursorWidget() {}
121
122         void draw(QPainter & painter)
123         {
124                 // FIXME: do something depending on the cursor shape.
125                 if (show_ && rect_.isValid())
126                         painter.fillRect(rect_, color_);
127         }
128
129         void update(int x, int y, int h, CursorShape shape)
130         {
131                 color_ = guiApp->colorCache().get(LColor::cursor);
132                 rect_ = QRect(x, y, CursorWidth, h);
133                 shape_ = shape;
134         }
135
136         void show(bool set_show = true) { show_ = set_show; }
137         void hide() { show_ = false; }
138
139         QRect const & rect() { return rect_; }
140
141 private:
142         ///
143         CursorShape shape_;
144         ///
145         bool show_;
146         ///
147         QColor color_;
148         ///
149         QRect rect_;
150 };
151
152
153 // This is a 'heartbeat' generating synthetic mouse move events when the
154 // cursor is at the top or bottom edge of the viewport. One scroll per 0.2 s
155 SyntheticMouseEvent::SyntheticMouseEvent()
156         : timeout(200), restart_timeout(true),
157           x_old(-1), y_old(-1), scrollbar_value_old(-1.0)
158 {}
159
160
161 GuiWorkArea::GuiWorkArea(int w, int h, int id, LyXView & lyx_view)
162         : WorkArea(id, lyx_view)
163 {
164         cursor_ = new frontend::CursorWidget();
165         cursor_->hide();
166
167         setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
168         setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
169         setAcceptDrops(true);
170         setMouseTracking(true);
171         setMinimumSize(100, 70);
172
173         viewport()->setAutoFillBackground(false);
174         // We don't need double-buffering nor SystemBackground on
175         // the viewport because we have our own backing pixmap.
176         viewport()->setAttribute(Qt::WA_NoSystemBackground);
177
178         setFocusPolicy(Qt::WheelFocus);
179
180         viewport()->setCursor(Qt::IBeamCursor);
181
182         resize(w, h);
183
184         synthetic_mouse_event_.timeout.timeout.connect(
185                 boost::bind(&GuiWorkArea::generateSyntheticMouseEvent,
186                             this));
187
188         // Initialize the vertical Scroll Bar
189         QObject::connect(verticalScrollBar(), SIGNAL(actionTriggered(int)),
190                 this, SLOT(adjustViewWithScrollBar(int)));
191
192         // PageStep only depends on the viewport height.
193         verticalScrollBar()->setPageStep(viewport()->height());
194
195         lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
196                 << "\n Area width\t" << width()
197                 << "\n Area height\t" << height()
198                 << "\n viewport width\t" << viewport()->width()
199                 << "\n viewport height\t" << viewport()->height()
200                 << endl;
201
202         // Enables input methods for asian languages.
203         // Must be set when creating custom text editing widgets.
204         setAttribute(Qt::WA_InputMethodEnabled, true);
205 }
206
207
208 void GuiWorkArea::setScrollbarParams(int h, int scroll_pos, int scroll_line_step)
209 {
210         if (verticalScrollBarPolicy() != Qt::ScrollBarAlwaysOn)
211                 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
212
213         verticalScrollBar()->setTracking(false);
214
215         // do what cursor movement does (some grey)
216         h += height() / 4;
217         int scroll_max_ = std::max(0, h - height());
218
219         verticalScrollBar()->setRange(0, scroll_max_);
220         verticalScrollBar()->setSliderPosition(scroll_pos);
221         verticalScrollBar()->setSingleStep(scroll_line_step);
222         verticalScrollBar()->setValue(scroll_pos);
223
224         verticalScrollBar()->setTracking(true);
225 }
226
227
228 void GuiWorkArea::adjustViewWithScrollBar(int)
229 {
230         scrollBufferView(verticalScrollBar()->sliderPosition());
231 }
232
233
234 void GuiWorkArea::dragEnterEvent(QDragEnterEvent * event)
235 {
236         if (event->mimeData()->hasUrls())
237                 event->accept();
238         /// \todo Ask lyx-devel is this is enough:
239         /// if (event->mimeData()->hasFormat("text/plain"))
240         ///     event->acceptProposedAction();
241 }
242
243
244 void GuiWorkArea::dropEvent(QDropEvent* event)
245 {
246         QList<QUrl> files = event->mimeData()->urls();
247         if (files.isEmpty())
248                 return;
249
250         lyxerr[Debug::GUI] << "GuiWorkArea::dropEvent: got URIs!" << endl;
251         for (int i = 0; i!=files.size(); ++i) {
252                 string const file = os::internal_path(fromqstr(files.at(i).toLocalFile()));
253                 if (!file.empty())
254                         dispatch(FuncRequest(LFUN_FILE_OPEN, file));
255         }
256 }
257
258
259 void GuiWorkArea::focusInEvent(QFocusEvent * /*event*/)
260 {
261         // No need to do anything if we didn't change views...
262 //      if (theApp() == 0 || &lyx_view_ == theApp()->currentView())
263 //              return;
264
265         theApp()->setCurrentView(lyx_view_);
266
267         // FIXME: it would be better to send a signal "newBuffer()"
268         // in BufferList that could be connected to the different tabbars.
269         lyx_view_.updateTab();
270
271         startBlinkingCursor();
272 }
273
274
275 void GuiWorkArea::focusOutEvent(QFocusEvent * /*event*/)
276 {
277         stopBlinkingCursor();
278 }
279
280
281 void GuiWorkArea::mousePressEvent(QMouseEvent * e)
282 {
283         if (dc_event_.active && dc_event_ == *e) {
284                 dc_event_.active = false;
285                 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
286                         dc_event_.x, dc_event_.y,
287                         q_button_state(dc_event_.state));
288                 dispatch(cmd);
289                 return;
290         }
291
292         FuncRequest const cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
293                               q_button_state(e->button()));
294         dispatch(cmd);
295 }
296
297
298 void GuiWorkArea::mouseReleaseEvent(QMouseEvent * e)
299 {
300         if (synthetic_mouse_event_.timeout.running())
301                 synthetic_mouse_event_.timeout.stop();
302
303         FuncRequest const cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
304                               q_button_state(e->button()));
305         dispatch(cmd);
306 }
307
308
309 void GuiWorkArea::mouseMoveEvent(QMouseEvent * e)
310 {
311         FuncRequest cmd(LFUN_MOUSE_MOTION, e->x(), e->y(),
312                               q_motion_state(e->buttons()));
313
314         // If we're above or below the work area...
315         if (e->y() <= 20 || e->y() >= viewport()->height() - 20) {
316                 // Make sure only a synthetic event can cause a page scroll,
317                 // so they come at a steady rate:
318                 if (e->y() <= 20)
319                         // _Force_ a scroll up:
320                         cmd.y = -40;
321                 else
322                         cmd.y = viewport()->height();
323                 // Store the event, to be handled when the timeout expires.
324                 synthetic_mouse_event_.cmd = cmd;
325
326                 if (synthetic_mouse_event_.timeout.running())
327                         // Discard the event. Note that it _may_ be handled
328                         // when the timeout expires if
329                         // synthetic_mouse_event_.cmd has not been overwritten.
330                         // Ie, when the timeout expires, we handle the
331                         // most recent event but discard all others that
332                         // occurred after the one used to start the timeout
333                         // in the first place.
334                         return;
335                 else {
336                         synthetic_mouse_event_.restart_timeout = true;
337                         synthetic_mouse_event_.timeout.start();
338                         // Fall through to handle this event...
339                 }
340
341         } else if (synthetic_mouse_event_.timeout.running()) {
342                 // Store the event, to be possibly handled when the timeout
343                 // expires.
344                 // Once the timeout has expired, normal control is returned
345                 // to mouseMoveEvent (restart_timeout = false).
346                 // This results in a much smoother 'feel' when moving the
347                 // mouse back into the work area.
348                 synthetic_mouse_event_.cmd = cmd;
349                 synthetic_mouse_event_.restart_timeout = false;
350                 return;
351         }
352
353         // Has anything changed on-screen since the last QMouseEvent
354         // was received?
355         double const scrollbar_value = verticalScrollBar()->value();
356         if (e->x() != synthetic_mouse_event_.x_old ||
357             e->y() != synthetic_mouse_event_.y_old ||
358             scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
359                 // Yes it has. Store the params used to check this.
360                 synthetic_mouse_event_.x_old = e->x();
361                 synthetic_mouse_event_.y_old = e->y();
362                 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
363
364                 // ... and dispatch the event to the LyX core.
365                 dispatch(cmd);
366         }
367 }
368
369
370 void GuiWorkArea::wheelEvent(QWheelEvent * e)
371 {
372         // Wheel rotation by one notch results in a delta() of 120 (see
373         // documentation of QWheelEvent)
374         int const lines = qApp->wheelScrollLines() * e->delta() / 120;
375         verticalScrollBar()->setValue(verticalScrollBar()->value() -
376                         lines *  verticalScrollBar()->singleStep());
377         adjustViewWithScrollBar();
378 }
379
380
381 void GuiWorkArea::generateSyntheticMouseEvent()
382 {
383 // Set things off to generate the _next_ 'pseudo' event.
384         if (synthetic_mouse_event_.restart_timeout)
385                 synthetic_mouse_event_.timeout.start();
386
387         // Has anything changed on-screen since the last timeout signal
388         // was received?
389         double const scrollbar_value = verticalScrollBar()->value();
390         if (scrollbar_value != synthetic_mouse_event_.scrollbar_value_old) {
391                 // Yes it has. Store the params used to check this.
392                 synthetic_mouse_event_.scrollbar_value_old = scrollbar_value;
393
394                 // ... and dispatch the event to the LyX core.
395                 dispatch(synthetic_mouse_event_.cmd);
396         }
397 }
398
399
400 void GuiWorkArea::keyPressEvent(QKeyEvent * e)
401 {
402         lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
403                 << " count=" << e->count()
404                 << " text=" << fromqstr(e->text())
405                 << " isAutoRepeat=" << e->isAutoRepeat()
406                 << " key=" << e->key()
407                 << endl;
408
409         boost::shared_ptr<QLyXKeySym> sym(new QLyXKeySym);
410         sym->set(e);
411         processKeySym(sym, q_key_state(e->modifiers()));
412 }
413
414
415 void GuiWorkArea::mouseDoubleClickEvent(QMouseEvent * e)
416 {
417         dc_event_ = double_click(e);
418
419         if (!dc_event_.active)
420                 return;
421
422         dc_event_.active = false;
423
424         FuncRequest cmd(LFUN_MOUSE_DOUBLE,
425                 dc_event_.x, dc_event_.y,
426                 q_button_state(dc_event_.state));
427         dispatch(cmd);
428 }
429
430
431 void GuiWorkArea::resizeEvent(QResizeEvent * ev)
432 {
433         stopBlinkingCursor();
434         screen_ = QPixmap(ev->size().width(), ev->size().height());
435         verticalScrollBar()->setPageStep(viewport()->height());
436         QAbstractScrollArea::resizeEvent(ev);
437         resizeBufferView();
438         startBlinkingCursor();
439 }
440
441
442 void GuiWorkArea::update(int x, int y, int w, int h)
443 {
444         viewport()->repaint(x, y, w, h);
445 }
446
447
448 void GuiWorkArea::doGreyOut(QLPainter & pain)
449 {
450         setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
451
452         pain.fillRectangle(0, 0, width(), height(),
453                 LColor::bottomarea);
454
455         //if (!lyxrc.show_banner)
456         //      return;
457         lyxerr[Debug::GUI] << "show banner: " << lyxrc.show_banner << endl;
458         /// The text to be written on top of the pixmap
459         QString const text = lyx_version ? QString(lyx_version) : qt_("unknown version");
460         FileName const file = support::libFileSearch("images", "banner", "ppm");
461         if (file.empty())
462                 return;
463
464         QPixmap pm(toqstr(file.absFilename()));
465         if (!pm) {
466                 lyxerr << "could not load splash screen: '" << file << "'" << endl;
467                 return;
468         }
469
470         QFont font;
471         // The font used to display the version info
472         font.setStyleHint(QFont::SansSerif);
473         font.setWeight(QFont::Bold);
474         font.setPointSize(convert<int>(lyxrc.font_sizes[LyXFont::SIZE_LARGE]));
475
476         int const w = pm.width();
477         int const h = pm.height();
478
479         int x = (width() - w) / 2;
480         int y = (height() - h) / 2;
481
482         pain.drawPixmap(x, y, pm);
483
484         x += 300;
485         y += 265;
486
487         pain.setPen(QColor(255, 255, 0));
488         pain.setFont(font);
489         pain.drawText(x, y, text);
490 }
491
492
493 void GuiWorkArea::paintEvent(QPaintEvent * ev)
494 {
495         QRect const rc = ev->rect(); 
496         /*
497         lyxerr[Debug::PAINTING] << "paintEvent begin: x: " << rc.x()
498                 << " y: " << rc.y()
499                 << " w: " << rc.width()
500                 << " h: " << rc.height() << endl;
501         */
502
503         QPainter pain(viewport());
504         pain.drawPixmap(rc, screen_, rc);
505         cursor_->draw(pain);
506 }
507
508
509 void GuiWorkArea::expose(int x, int y, int w, int h)
510 {
511         QLPainter pain(&screen_);
512
513         if (greyed_out_) {
514                 lyxerr[Debug::GUI] << "splash screen requested" << endl;
515                 doGreyOut(pain);
516                 verticalScrollBar()->hide();
517                 update(0, 0, width(), height());
518                 return;
519         }
520
521         verticalScrollBar()->show();
522         paintText(*buffer_view_, pain);
523         update(x, y, w, h);
524 }
525
526
527 void GuiWorkArea::showCursor(int x, int y, int h, CursorShape shape)
528 {
529         cursor_->update(x, y, h, shape);
530         cursor_->show();
531         viewport()->update(cursor_->rect());
532 }
533
534
535 void GuiWorkArea::removeCursor()
536 {
537         cursor_->hide();
538         //if (!qApp->focusWidget())
539                 viewport()->update(cursor_->rect());
540 }
541
542
543 void GuiWorkArea::inputMethodEvent(QInputMethodEvent * e)
544 {
545         QString const & text = e->commitString();
546         if (!text.isEmpty()) {
547
548                 lyxerr[Debug::KEY] << BOOST_CURRENT_FUNCTION
549                         << " preeditString =" << fromqstr(e->preeditString())
550                         << " commitString  =" << fromqstr(e->commitString())
551                         << endl;
552
553                 int key = 0;
554                 // needed to make math superscript work on some systems
555                 // ideally, such special coding should not be necessary
556                 if (text == "^")
557                         key = Qt::Key_AsciiCircum;
558                 // FIXME: Needs for investigation, this key is not really used,
559                 // the ctor below just check if key is different from 0.
560                 QKeyEvent ev(QEvent::KeyPress, key, Qt::NoModifier, text);
561                 keyPressEvent(&ev);
562         }
563         e->accept();
564 }
565
566 } // namespace frontend
567 } // namespace lyx
568
569 #include "GuiWorkArea_moc.cpp"