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