]> git.lyx.org Git - features.git/blob - src/frontends/qt/GuiPainter.h
Guard against possible referencing null.
[features.git] / src / frontends / qt / GuiPainter.h
1 // -*- C++ -*-
2 /**
3  * \file GuiPainter.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author John Levon
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #ifndef GUIPAINTER_H
14 #define GUIPAINTER_H
15
16 #include "Color.h"
17
18 #include "frontends/Painter.h"
19
20 #include <QPainter>
21 #include <QPainterPath>
22 #include <stack>
23
24 namespace lyx {
25
26 class FontInfo;
27
28 namespace frontend {
29
30 /**
31  * GuiPainter - a painter implementation for Qt
32  */
33 class GuiPainter : public QPainter, public Painter {
34 public:
35         GuiPainter(QPaintDevice *, double pixel_ratio, bool devel_mode);
36         virtual ~GuiPainter();
37
38         /// This painter paints
39         bool isNull() const override { return false; }
40
41         /// draw a line from point to point
42         void line(
43                 int x1, int y1,
44                 int x2, int y2,
45                 Color,
46                 line_style ls = line_solid,
47                 int lw = thin_line) override;
48
49         /**
50          * lines -  draw a set of lines
51          * @param xp array of points' x co-ords
52          * @param yp array of points' y co-ords
53          * @param np size of the points array
54          */
55         void lines(
56                 int const * xp,
57                 int const * yp,
58                 int np,
59                 Color,
60                 fill_style fs = fill_none,
61                 line_style ls = line_solid,
62                 int lw = thin_line) override;
63
64         /**
65          * path -  draw a path with bezier curves
66          * @param xp array of points' x co-ords
67          * @param yp array of points' y co-ords
68          * @param c1x array of first control points' x co-ords
69          * @param c1y array of first control points' y co-ords
70          * @param c2x array of second control points' x co-ords
71          * @param c2y array of second control points' y co-ords
72          * @param np size of the points array
73          */
74         void path(int const * xp, int const * yp,
75                 int const * c1x, int const * c1y,
76                 int const * c2x, int const * c2y,
77                 int np, Color,
78                 fill_style = fill_none, line_style = line_solid,
79                 int line_width = thin_line) override;
80
81         /// draw a rectangle
82         void rectangle(
83                 int x, int y,
84                 int w, int h,
85                 Color,
86                 line_style = line_solid,
87                 int lw = thin_line) override;
88
89         /// draw a filled rectangle
90         void fillRectangle(
91                 int x, int y,
92                 int w, int h,
93                 Color) override;
94
95         /// draw an arc
96         void arc(
97                 int x, int y,
98                 unsigned int w, unsigned int h,
99                 int a1, int a2,
100                 Color) override;
101
102         /// draw an ellipse
103         void ellipse(
104                 double x, double y,
105                 double rx, double ry,
106                 Color,
107                 fill_style fs = fill_none,
108                 line_style ls = line_solid,
109                 int lw = thin_line) override;
110
111         /// draw a pixel
112         void point(int x, int y, Color) override;
113
114         /// draw an image from the image cache
115         void image(int x, int y, int w, int h,
116                 lyx::graphics::Image const & image, bool const darkmode = false) override;
117
118         /// draw a string at position x, y (y is the baseline).
119         void text(int x, int y, docstring const & str, FontInfo const & f) override;
120
121         /// draw a char at position x, y (y is the baseline)
122         void text(int x, int y, char_type c, FontInfo const & f) override;
123
124         /** draw a string at position x, y (y is the baseline). The
125          * text direction is enforced by the \c Font.
126          */
127         void text(int x, int y, docstring const & str, Font const & f,
128                       double wordspacing, double textwidth) override;
129
130         /** draw a string at position x, y (y is the baseline), but
131          * make sure that the part between \c from and \c to is in
132          * \c other color. The text direction is enforced by the \c Font.
133          */
134         void text(int x, int y, docstring const & str, Font const & f,
135                           Color other, size_type from, size_type to,
136                       double wordspacing, double textwidth) override;
137
138         ///
139         void textDecoration(FontInfo const & f, int x, int y, int width) override;
140
141         /// draw a string and enclose it inside a button frame
142         void buttonText(int x, int baseline, docstring const & s,
143                 FontInfo const & font, Color back, Color frame, int offset) override;
144
145         /// start monochrome painting mode, i.e. map every color a shade of \c blend.
146         void enterMonochromeMode(Color const & blend) override;
147         /// leave monochrome painting mode
148         void leaveMonochromeMode() override;
149
150         /**
151          * Draw a string and enclose it inside a rectangle. If
152          * back color is specified, the background is cleared with
153          * the given color. If frame is specified, a thin frame is drawn
154          * around the text with the given color.
155          */
156         void rectText(int x, int baseline, docstring const & str,
157                 FontInfo const & font, Color back, Color frame) override;
158
159         /// draw a character of a preedit string for cjk support.
160         int preeditText(int x, int y,
161                 char_type c, FontInfo const & f, preedit_style style) override;
162
163         void wavyHorizontalLine(FontInfo const & f, int x, int y, int width, ColorCode col) override;
164
165 private:
166         /// check the font, and if set, draw an underline
167         void underline(FontInfo const & f,
168                        int x, int y, int width, line_style ls = line_solid);
169
170         /// check the font, and if set, draw an dashed underline
171         void dashedUnderline(FontInfo const & f,
172                 int x, int y, int width);
173
174         /// check the font, and if set, draw an strike-through line
175         void strikeoutLine(FontInfo const & f,
176                 int x, int y, int width);
177
178         /// check the font, and if set, draw cross-through lines
179         void crossoutLines(FontInfo const & f,
180                 int x, int y, int width);
181
182         /// check the font, and if set, draw double underline
183         void doubleUnderline(FontInfo const & f,
184                 int x, int y, int width);
185
186         /// set pen parameters
187         void setQPainterPen(QColor const & col,
188                 line_style ls = line_solid, int lw = thin_line,
189                 Qt::PenJoinStyle js = Qt::BevelJoin);
190
191         // Direction for painting text
192         enum Direction { LtR, RtL, Auto };
193
194         // Real text() method
195         void text(int x, int y, docstring const & s,
196               FontInfo const & f, Direction const dir,
197               double const wordspacing, double tw);
198
199         QColor current_color_;
200         Painter::line_style current_ls_;
201         int current_lw_;
202         ///
203         std::stack<QColor> monochrome_blend_;
204         /// convert into Qt color, possibly applying the monochrome mode
205         QColor computeColor(Color col);
206         /// possibly apply monochrome mode
207         QColor filterColor(QColor const & col);
208 };
209
210 } // namespace frontend
211 } // namespace lyx
212
213 #endif // GUIPAINTER_H