]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QContentPane.C
the qtclean-2 patch with some very small changes
[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::keyPressEvent(QKeyEvent * e)
137 {
138         lyxerr[Debug::KEY] << "Press key " << e->key()
139                 << " text \"" << (e->text().isEmpty() ? "none" : e->text().latin1())
140                 << "\", ascii \"" << e->ascii() << "\"" << endl;
141         QLyXKeySym * sym = new QLyXKeySym();
142         sym->set(e);
143         wa_->workAreaKeyPress(LyXKeySymPtr(sym), q_key_state(e->state()));
144 }
145
146
147 void QContentPane::doubleClickTimeout()
148 {
149         if (!dc_event_.active)
150                 return;
151
152         dc_event_.active = false;
153
154         FuncRequest cmd(LFUN_MOUSE_DOUBLE,
155                 dc_event_.x, dc_event_.y,
156                 q_button_state(dc_event_.state));
157         wa_->dispatch(cmd);
158 }
159
160
161 void QContentPane::mouseDoubleClickEvent(QMouseEvent * e)
162 {
163         dc_event_ = double_click(e);
164
165         // doubleClickInterval() is just too long.
166         QTimer::singleShot(int(QApplication::doubleClickInterval() / 1.5),
167                 this, SLOT(doubleClickTimeout()));
168 }
169
170
171 void QContentPane::resizeEvent(QResizeEvent *)
172 {
173         if (!pixmap_.get()) {
174                 pixmap_.reset(new QPixmap(width(), height()));
175         }
176
177         pixmap_->resize(width(), height());
178         wa_->workAreaResize();
179 }
180
181
182 void QContentPane::paintEvent(QPaintEvent * e)
183 {
184         if (!pixmap_.get()) {
185                 pixmap_.reset(new QPixmap(width(), height()));
186                 wa_->workAreaResize();
187                 return;
188         }
189
190         QRect r(e->rect());
191
192         lyxerr[Debug::GUI] << "repainting " << r.x()
193                 << "," << r.y() << " " << r.width()
194                 << "," << r.height() << endl;
195         QPainter q(this);
196         q.drawPixmap(QPoint(r.x(), r.y()),
197                 *pixmap_.get(), r);
198 }