]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QContentPane.C
Lots and lots of little trivial bits.
[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 /// 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
89         // stupid moc strikes again
90         connect(wa_->scrollbar_, SIGNAL(valueChanged(int)), 
91                 this, SLOT(scrollBarChanged(int)));
92  
93 }
94
95
96 void QContentPane::scrollBarChanged(int val)
97 {
98         wa_->scrollDocView(val); 
99 }
100  
101  
102 void QContentPane::mousePressEvent(QMouseEvent * e)
103 {
104         if (dc_event_.active && dc_event_ == *e) {
105                 dc_event_.active = false;
106                 FuncRequest cmd(LFUN_MOUSE_TRIPLE,
107                         dc_event_.x, dc_event_.y,
108                         q_button_state(dc_event_.state));
109                 wa_->dispatch(cmd);
110                 return;
111         }
112  
113         FuncRequest cmd
114                 (LFUN_MOUSE_PRESS, e->x(), e->y(), q_button_state(e->button()));
115         wa_->dispatch(cmd);
116 }
117
118
119 void QContentPane::mouseReleaseEvent(QMouseEvent * e)
120 {
121         FuncRequest cmd
122                 (LFUN_MOUSE_RELEASE, e->x(), e->y(), q_button_state(e->button()));
123         wa_->dispatch(cmd);
124 }
125
126  
127 void QContentPane::mouseMoveEvent(QMouseEvent * e)
128 {
129         FuncRequest cmd
130                 (LFUN_MOUSE_MOTION, e->x(), e->y(), q_motion_state(e->state()));
131         wa_->dispatch(cmd);
132 }
133
134
135 void QContentPane::keyPressEvent(QKeyEvent * e)
136 {
137         lyxerr[Debug::KEY] << "Press key " << e->key()
138                 << " text \"" << (e->text().isEmpty() ? "none" : e->text().latin1())
139                 << "\", ascii \"" << e->ascii() << "\"" << endl;
140         QLyXKeySym * sym = new QLyXKeySym();
141         sym->set(e);
142         wa_->workAreaKeyPress(LyXKeySymPtr(sym), q_key_state(e->state()));
143 }
144
145  
146 void QContentPane::doubleClickTimeout()
147 {
148         if (!dc_event_.active)
149                 return;
150
151         dc_event_.active = false;
152  
153         FuncRequest cmd(LFUN_MOUSE_DOUBLE,
154                 dc_event_.x, dc_event_.y,
155                 q_button_state(dc_event_.state));
156         wa_->dispatch(cmd);
157 }
158
159
160 void QContentPane::mouseDoubleClickEvent(QMouseEvent * e)
161 {
162         dc_event_ = double_click(e);
163
164         // doubleClickInterval() is just too long.
165         QTimer::singleShot(int(QApplication::doubleClickInterval() / 1.5),
166                 this, SLOT(doubleClickTimeout()));
167 }
168  
169  
170 void QContentPane::resizeEvent(QResizeEvent *)
171 {
172         if (!pixmap_.get()) {
173                 pixmap_.reset(new QPixmap(width(), height()));
174         }
175
176         pixmap_->resize(width(), height());
177         wa_->workAreaResize();
178 }
179
180  
181 void QContentPane::paintEvent(QPaintEvent * e)
182 {
183         if (!pixmap_.get()) {
184                 pixmap_.reset(new QPixmap(width(), height()));
185                 wa_->workAreaResize();
186                 return;
187         }
188
189         QRect r(e->rect());
190  
191         lyxerr[Debug::GUI] << "repainting " << r.x() 
192                 << "," << r.y() << " " << r.width() 
193                 << "," << r.height() << endl;
194         QPainter q(this);
195         q.drawPixmap(QPoint(r.x(), r.y()),
196                 *pixmap_.get(), r); 
197 }