]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QContentPane.C
Get rid of the static_casts.
[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         QLyXKeySym * sym = new QLyXKeySym();
145         sym->set(e);
146         wa_->workAreaKeyPress(LyXKeySymPtr(sym), q_key_state(e->state()));
147 }
148
149
150 void QContentPane::doubleClickTimeout()
151 {
152         if (!dc_event_.active)
153                 return;
154
155         dc_event_.active = false;
156
157         FuncRequest cmd(LFUN_MOUSE_DOUBLE,
158                 dc_event_.x, dc_event_.y,
159                 q_button_state(dc_event_.state));
160         wa_->dispatch(cmd);
161 }
162
163
164 void QContentPane::mouseDoubleClickEvent(QMouseEvent * e)
165 {
166         dc_event_ = double_click(e);
167
168         // doubleClickInterval() is just too long.
169         QTimer::singleShot(int(QApplication::doubleClickInterval() / 1.5),
170                 this, SLOT(doubleClickTimeout()));
171 }
172
173
174 void QContentPane::resizeEvent(QResizeEvent *)
175 {
176         if (!pixmap_.get()) {
177                 pixmap_.reset(new QPixmap(width(), height()));
178         }
179
180         pixmap_->resize(width(), height());
181         wa_->workAreaResize();
182 }
183
184
185 void QContentPane::paintEvent(QPaintEvent * e)
186 {
187         if (!pixmap_.get()) {
188                 pixmap_.reset(new QPixmap(width(), height()));
189                 wa_->workAreaResize();
190                 return;
191         }
192
193         QRect r(e->rect());
194
195         QPainter q(this);
196         q.drawPixmap(QPoint(r.x(), r.y()),
197                 *pixmap_.get(), r);
198 }