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