]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QContentPane.C
dont use pragma impementation and interface anymore
[lyx.git] / src / frontends / qt2 / QContentPane.C
1 /**
2  * \file QContentPane.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  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13
14 #include "debug.h"
15
16 #include "QWorkArea.h"
17 #include "QLyXKeySym.h"
18 #include "funcrequest.h"
19 #include "qt_helpers.h"
20
21 #include <qevent.h>
22 #include <qpainter.h>
23 #include <qtimer.h>
24 #include <qapplication.h>
25 #include <qcursor.h>
26
27 using std::endl;
28
29 namespace {
30
31 /// return the LyX key state from Qt's
32 key_modifier::state q_key_state(Qt::ButtonState state)
33 {
34         key_modifier::state k = key_modifier::none;
35         if (state & Qt::ControlButton)
36                 k |= key_modifier::ctrl;
37         if (state & Qt::ShiftButton)
38                 k |= key_modifier::shift;
39         if (state & Qt::AltButton)
40                 k |= key_modifier::alt;
41         return k;
42 }
43
44
45 /// return the LyX mouse button state from Qt's
46 mouse_button::state q_button_state(Qt::ButtonState button)
47 {
48         mouse_button::state b = mouse_button::none;
49         switch (button) {
50                 case Qt::LeftButton:
51                         b = mouse_button::button1;
52                         break;
53                 case Qt::MidButton:
54                         b = mouse_button::button2;
55                         break;
56                 case Qt::RightButton:
57                         b = mouse_button::button3;
58                         break;
59                 default:
60                         break;
61         }
62         return b;
63 }
64
65
66 /// return the LyX mouse button state from Qt's
67 mouse_button::state q_motion_state(Qt::ButtonState state)
68 {
69         mouse_button::state b = mouse_button::none;
70         if (state & Qt::LeftButton)
71                 b |= mouse_button::button1;
72         if (state & Qt::MidButton)
73                 b |= mouse_button::button2;
74         if (state & Qt::RightButton)
75                 b |= mouse_button::button3;
76         return b;
77 }
78
79 } // namespace anon
80
81
82 QContentPane::QContentPane(QWorkArea * parent)
83         : QWidget(parent, "content_pane", WRepaintNoErase),
84         wa_(parent)
85 {
86         setFocusPolicy(QWidget::WheelFocus);
87         setFocus();
88         setCursor(ibeamCursor);
89
90         // stupid moc strikes again
91         connect(wa_->scrollbar_, SIGNAL(valueChanged(int)),
92                 this, SLOT(scrollBarChanged(int)));
93
94 }
95
96
97 void QContentPane::scrollBarChanged(int val)
98 {
99         wa_->scrollDocView(val);
100 }
101
102
103 void QContentPane::mousePressEvent(QMouseEvent * e)
104 {
105         if (dc_event_.active && dc_event_ == *e) {
106                 dc_event_.active = false;
107                 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
108                         dc_event_.x, dc_event_.y,
109                         q_button_state(dc_event_.state));
110                 wa_->dispatch(cmd);
111                 return;
112         }
113
114         FuncRequest cmd(LFUN_MOUSE_PRESS, e->x(), e->y(),
115                         q_button_state(e->button()));
116         wa_->dispatch(cmd);
117 }
118
119
120 void QContentPane::mouseReleaseEvent(QMouseEvent * e)
121 {
122         FuncRequest cmd(LFUN_MOUSE_RELEASE, e->x(), e->y(),
123                         q_button_state(e->button()));
124         wa_->dispatch(cmd);
125 }
126
127
128 void QContentPane::mouseMoveEvent(QMouseEvent * e)
129 {
130         FuncRequest cmd
131                 (LFUN_MOUSE_MOTION, e->x(), e->y(), q_motion_state(e->state()));
132         wa_->dispatch(cmd);
133 }
134
135
136 void QContentPane::wheelEvent(QWheelEvent * e)
137 {
138         wa_->scrollbar_->setValue(wa_->scrollbar_->value() - e->delta());
139 }
140
141
142 void QContentPane::keyPressEvent(QKeyEvent * e)
143 {
144         lyxerr[Debug::KEY] << "Press key " << e->key()
145                            << " text \""
146                            << (e->text().isEmpty() ?
147                                "none" :
148                                fromqstr(e->text()))
149                            << "\", ascii \"" << e->ascii() << '"' << endl;
150         QLyXKeySym * sym = new QLyXKeySym();
151         sym->set(e);
152         wa_->workAreaKeyPress(LyXKeySymPtr(sym), q_key_state(e->state()));
153 }
154
155
156 void QContentPane::doubleClickTimeout()
157 {
158         if (!dc_event_.active)
159                 return;
160
161         dc_event_.active = false;
162
163         FuncRequest cmd(LFUN_MOUSE_DOUBLE,
164                 dc_event_.x, dc_event_.y,
165                 q_button_state(dc_event_.state));
166         wa_->dispatch(cmd);
167 }
168
169
170 void QContentPane::mouseDoubleClickEvent(QMouseEvent * e)
171 {
172         dc_event_ = double_click(e);
173
174         // doubleClickInterval() is just too long.
175         QTimer::singleShot(int(QApplication::doubleClickInterval() / 1.5),
176                 this, SLOT(doubleClickTimeout()));
177 }
178
179
180 void QContentPane::resizeEvent(QResizeEvent *)
181 {
182         if (!pixmap_.get()) {
183                 pixmap_.reset(new QPixmap(width(), height()));
184         }
185
186         pixmap_->resize(width(), height());
187         wa_->workAreaResize();
188 }
189
190
191 void QContentPane::paintEvent(QPaintEvent * e)
192 {
193         if (!pixmap_.get()) {
194                 pixmap_.reset(new QPixmap(width(), height()));
195                 wa_->workAreaResize();
196                 return;
197         }
198
199         QRect r(e->rect());
200
201         lyxerr[Debug::GUI] << "repainting " << r.x()
202                 << ',' << r.y() << ' ' << r.width()
203                 << ',' << r.height() << endl;
204         QPainter q(this);
205         q.drawPixmap(QPoint(r.x(), r.y()),
206                 *pixmap_.get(), r);
207 }