]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/qscreen.C
Added initial qt4 work by Abdelrazak Younes
[lyx.git] / src / frontends / qt4 / qscreen.C
1 /**
2  * \file qscreen.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  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "QWorkArea.h"
15 #include "qscreen.h"
16 //Added by qt3to4:
17 #include <QPixmap>
18 #include <QPainter>
19
20 #include "debug.h"
21 #include "lcolorcache.h"
22
23 #include <QApplication>
24
25
26 namespace {
27
28 } // namespace anon
29
30
31 QScreen::QScreen(QWorkArea & o)
32         : LyXScreen(), owner_(o), nocursor_pixmap_(0,0)
33 {
34 }
35
36
37 QScreen::~QScreen()
38 {
39 }
40
41
42 WorkArea & QScreen::workarea() const
43 {
44         return owner_;
45 }
46
47 void QScreen::expose(int x, int y, int w, int h)
48 {
49         lyxerr[Debug::GUI] << "expose " << w << 'x' << h                << '+' << x << '+' << y << std::endl;
50
51         owner_.viewport()->update(x, y, w, h);
52 //      owner_.update();
53 }
54
55
56 void QScreen::showCursor(int x, int y, int h, Cursor_Shape shape)
57 {
58         if (!qApp->focusWidget())
59                 return;
60
61         if (x==cursor_x_ && y==cursor_y_ && h==cursor_h_) {
62                 // Draw the new (vertical) cursor using the cached store.
63                 QLPainter * lp = (QLPainter *) &(owner_.getPainter());
64                 lp->pixmap(cursor_x_, cursor_y_, vcursor_pixmap_);
65                 owner_.viewport()->update(
66                         cursor_x_, cursor_y_,
67                         cursor_w_, cursor_h_);
68                 return;
69         }
70                 
71         // Cache the dimensions of the cursor.
72         cursor_x_ = x;
73         cursor_y_ = y;
74         cursor_h_ = h;
75
76         switch (shape) {
77         case BAR_SHAPE:
78                 cursor_w_ = 2;
79                 break;
80         case L_SHAPE:
81                 cursor_w_ = cursor_h_ / 3;
82                 break;
83         case REVERSED_L_SHAPE:
84                 cursor_w_ = cursor_h_ / 3;
85                 cursor_x_ = x - cursor_w_ + 1;
86                 break;
87         }
88
89
90         // We cache three pixmaps:
91         // 1 the rectangle of the original screen.
92         // 2 the vertical line of the cursor.
93         // 3 the horizontal line of the L-shaped cursor (if necessary).
94
95         // Initialise storage for these pixmaps as necessary.
96         if (cursor_w_ != nocursor_pixmap_.width() ||
97             cursor_h_ != nocursor_pixmap_.height()) {
98                 nocursor_pixmap_.resize(cursor_w_, cursor_h_);
99         }
100
101         QColor const & required_color = lcolorcache.get(LColor::cursor);
102         bool const cursor_color_changed = required_color != cursor_color_;
103         if (cursor_color_changed)
104                 cursor_color_ = required_color;
105
106 //      if (cursor_h_ != vcursor_pixmap_.height() || cursor_color_changed) {
107 //              if (cursor_h_ != vcursor_pixmap_.height())
108                         vcursor_pixmap_.resize(cursor_w_, cursor_h_);
109                 vcursor_pixmap_.fill(cursor_color_);
110 //      }
111
112         switch (shape) {
113         case BAR_SHAPE:
114                 break;
115         case REVERSED_L_SHAPE:
116         case L_SHAPE:
117                 if (cursor_w_ != hcursor_pixmap_.width() ||
118                     cursor_color_changed) {
119                         if (cursor_w_ != hcursor_pixmap_.width())
120                                 hcursor_pixmap_.resize(cursor_w_, 1);
121                         hcursor_pixmap_.fill(cursor_color_);
122                 }
123                 break;
124         }
125
126         // Save the old area (no cursor).
127         QPainter qp(&nocursor_pixmap_);
128         qp.drawPixmap(0, 0, *owner_.pixmap(),
129                cursor_x_, cursor_y_, cursor_w_, cursor_h_);
130
131         // Draw the new (vertical) cursor using the cached store.
132         QLPainter * lp = (QLPainter *) &(owner_.getPainter());
133         lp->pixmap(cursor_x_, cursor_y_, vcursor_pixmap_);
134
135         // Draw the new (horizontal) cursor if necessary.
136         switch (shape) {
137         case BAR_SHAPE:
138                 break;
139         case REVERSED_L_SHAPE:
140         case L_SHAPE:
141                 lp->pixmap(cursor_x_, y + h - 1, hcursor_pixmap_);
142                 break;
143         }
144
145         owner_.viewport()->update(
146                 cursor_x_, cursor_y_,
147                 cursor_w_, cursor_h_);
148 }
149
150
151 void QScreen::removeCursor()
152 {
153         // before first showCursor
154         if (nocursor_pixmap_.isNull())
155                 return;
156
157         QLPainter * lp = (QLPainter *) &(owner_.getPainter());
158         lp->pixmap(cursor_x_, cursor_y_, nocursor_pixmap_);
159
160         owner_.viewport()->update(
161                 cursor_x_, cursor_y_,
162                 cursor_w_, cursor_h_);
163 }