]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QContentPane.C
Patch from Philippe Elie for making mouse wheel do
[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 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "debug.h"
18
19 #include "QWorkArea.h"
20 #include "QLyXKeySym.h"
21 #include "funcrequest.h"
22
23 #include <qevent.h>
24 #include <qpainter.h>
25 #include <qtimer.h>
26 #include <qapplication.h>
27
28 using std::endl;
29
30 namespace {
31
32 /// return the LyX key state from Qt's
33 key_modifier::state q_key_state(Qt::ButtonState state)
34 {
35         key_modifier::state k = key_modifier::none;
36         if (state & Qt::ControlButton)
37                 k |= key_modifier::ctrl;
38         if (state & Qt::ShiftButton)
39                 k |= key_modifier::shift;
40         if (state & Qt::AltButton)
41                 k |= key_modifier::alt;
42         return k;
43 }
44
45
46 /// return the LyX mouse button state from Qt's
47 mouse_button::state q_button_state(Qt::ButtonState button)
48 {
49         mouse_button::state b = mouse_button::none;
50         switch (button) {
51                 case Qt::LeftButton:
52                         b = mouse_button::button1;
53                         break;
54                 case Qt::MidButton:
55                         b = mouse_button::button2;
56                         break;
57                 case Qt::RightButton:
58                         b = mouse_button::button3;
59                         break;
60                 default:
61                         break;
62         }
63         return b;
64 }
65
66
67 /// return the LyX mouse button state from Qt's
68 mouse_button::state q_motion_state(Qt::ButtonState state)
69 {
70         mouse_button::state b = mouse_button::none;
71         if (state & Qt::LeftButton)
72                 b |= mouse_button::button1;
73         if (state & Qt::MidButton)
74                 b |= mouse_button::button2;
75         if (state & Qt::RightButton)
76                 b |= mouse_button::button3;
77         return b;
78 }
79
80 } // namespace anon
81
82
83 QContentPane::QContentPane(QWorkArea * parent)
84         : QWidget(parent, "content_pane", WRepaintNoErase),
85         wa_(parent)
86 {
87         setFocusPolicy(QWidget::WheelFocus);
88         setFocus();
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 \"" << (e->text().isEmpty() ? "none" : e->text().latin1())
146                 << "\", ascii \"" << e->ascii() << "\"" << endl;
147         QLyXKeySym * sym = new QLyXKeySym();
148         sym->set(e);
149         wa_->workAreaKeyPress(LyXKeySymPtr(sym), q_key_state(e->state()));
150 }
151
152
153 void QContentPane::doubleClickTimeout()
154 {
155         if (!dc_event_.active)
156                 return;
157
158         dc_event_.active = false;
159
160         FuncRequest cmd(LFUN_MOUSE_DOUBLE,
161                 dc_event_.x, dc_event_.y,
162                 q_button_state(dc_event_.state));
163         wa_->dispatch(cmd);
164 }
165
166
167 void QContentPane::mouseDoubleClickEvent(QMouseEvent * e)
168 {
169         dc_event_ = double_click(e);
170
171         // doubleClickInterval() is just too long.
172         QTimer::singleShot(int(QApplication::doubleClickInterval() / 1.5),
173                 this, SLOT(doubleClickTimeout()));
174 }
175
176
177 void QContentPane::resizeEvent(QResizeEvent *)
178 {
179         if (!pixmap_.get()) {
180                 pixmap_.reset(new QPixmap(width(), height()));
181         }
182
183         pixmap_->resize(width(), height());
184         wa_->workAreaResize();
185 }
186
187
188 void QContentPane::paintEvent(QPaintEvent * e)
189 {
190         if (!pixmap_.get()) {
191                 pixmap_.reset(new QPixmap(width(), height()));
192                 wa_->workAreaResize();
193                 return;
194         }
195
196         QRect r(e->rect());
197
198         lyxerr[Debug::GUI] << "repainting " << r.x()
199                 << "," << r.y() << " " << r.width()
200                 << "," << r.height() << endl;
201         QPainter q(this);
202         q.drawPixmap(QPoint(r.x(), r.y()),
203                 *pixmap_.get(), r);
204 }