]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/QContentPane.C
the workarea changes plus small math stuff
[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  
24 using std::endl;
25
26 namespace {
27  
28 /// return the LyX key state from Qt's
29 key_modifier::state q_key_state(Qt::ButtonState state)
30 {
31         key_modifier::state k = key_modifier::none;
32         if (state & Qt::ControlButton)
33                 k |= key_modifier::ctrl;
34         if (state & Qt::ShiftButton)
35                 k |= key_modifier::shift;
36         if (state & Qt::AltButton)
37                 k |= key_modifier::alt;
38         return k;
39 }
40
41 /// return the LyX mouse button state from Qt's
42 mouse_button::state q_button_state(Qt::ButtonState button)
43 {
44         mouse_button::state b = mouse_button::none;
45         switch (button) {
46                 case Qt::LeftButton:
47                         b = mouse_button::button1;
48                         break;
49                 case Qt::MidButton:
50                         b = mouse_button::button2;
51                         break;
52                 case Qt::RightButton:
53                         b = mouse_button::button3;
54                         break;
55                 default:
56                         break;
57         }
58         return b;
59 }
60  
61  
62 /// return the LyX mouse button state from Qt's
63 mouse_button::state q_motion_state(Qt::ButtonState state)
64 {
65         mouse_button::state b = mouse_button::none;
66         if (state & Qt::LeftButton)
67                 b |= mouse_button::button1;
68         if (state & Qt::MidButton)
69                 b |= mouse_button::button2;
70         if (state & Qt::RightButton)
71                 b |= mouse_button::button3;
72         return b;
73 }
74
75 } // namespace anon
76  
77
78 QContentPane::QContentPane(QWorkArea * parent)
79         : QWidget(parent, "content_pane", WRepaintNoErase), 
80         wa_(parent)
81 {
82         setFocusPolicy(QWidget::WheelFocus);
83         setFocus();
84
85         // stupid moc strikes again
86         connect(wa_->scrollbar_, SIGNAL(valueChanged(int)), 
87                 this, SLOT(scrollBarChanged(int)));
88  
89 }
90
91
92 void QContentPane::scrollBarChanged(int val)
93 {
94         wa_->scrollDocView(val); 
95 }
96  
97  
98 void QContentPane::mousePressEvent(QMouseEvent * e)
99 {
100         FuncRequest cmd
101                 (LFUN_MOUSE_PRESS, e->x(), e->y(), q_button_state(e->button()));
102         wa_->dispatch(cmd);
103 }
104
105
106 void QContentPane::mouseReleaseEvent(QMouseEvent * e)
107 {
108         FuncRequest cmd
109                 (LFUN_MOUSE_RELEASE, e->x(), e->y(), q_button_state(e->button()));
110         wa_->dispatch(cmd);
111 }
112
113  
114 void QContentPane::mouseMoveEvent(QMouseEvent * e)
115 {
116         FuncRequest cmd
117                 (LFUN_MOUSE_RELEASE, e->x(), e->y(), q_motion_state(e->button()));
118         wa_->dispatch(cmd);
119 }
120
121
122 void QContentPane::keyPressEvent(QKeyEvent * e)
123 {
124         lyxerr[Debug::KEY] << "Press key " << e->key()
125                 << " text \"" << (e->text().isEmpty() ? "none" : e->text().latin1())
126                 << "\", ascii \"" << e->ascii() << "\"" << endl;
127         QLyXKeySym * sym = new QLyXKeySym();
128         sym->set(e);
129         wa_->workAreaKeyPress(LyXKeySymPtr(sym), q_key_state(e->state()));
130 }
131
132  
133 void QContentPane::mouseDoubleClickEvent(QMouseEvent * e)
134 {
135         FuncRequest cmd
136                 (LFUN_MOUSE_DOUBLE, e->x(), e->y(), q_button_state(e->button()));
137         wa_->dispatch(cmd);
138         // FIXME: triple click 
139 }
140  
141  
142 void QContentPane::resizeEvent(QResizeEvent *)
143 {
144         if (!pixmap_.get()) {
145                 pixmap_.reset(new QPixmap(width(), height()));
146         }
147
148         pixmap_->resize(width(), height());
149         wa_->workAreaResize();
150 }
151
152  
153 void QContentPane::paintEvent(QPaintEvent * e)
154 {
155         if (!pixmap_.get()) {
156                 pixmap_.reset(new QPixmap(width(), height()));
157                 wa_->workAreaResize();
158                 return;
159         }
160
161         QRect r(e->rect());
162  
163         lyxerr[Debug::GUI] << "repainting " << r.x() 
164                 << "," << r.y() << " " << r.width() 
165                 << "," << r.height() << endl;
166         QPainter q(this);
167         q.drawPixmap(QPoint(r.x(), r.y()),
168                 *pixmap_.get(), r); 
169 }