summaryrefslogtreecommitdiff
path: root/src/fqterm/fqterm_screen.h
blob: 9658ec59783b3a9739e4791a6025abd17af0128c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// SPDX-License-Identifier: GPL-2.0-or-later

#ifndef FQTERM_SCREEN_H
#define FQTERM_SCREEN_H

#include <QWidget>
#include <QVector>
#include <QList>
#include "fqterm_param.h"
#include "fqterm_convert.h"

class QTextCodec;
class QColor;
class QPainter;
class QInputMethodEvent;
class QPixmap;
class QScrollBar;
class QShortcut;

namespace FQTerm {

class FQTermWindow;
class FQTermBuffer;
class FQTermSession;
class FQTermParam;

class PreeditLine: public QWidget {
public:
  PreeditLine(QWidget *parent,const QColor *colors);
  ~PreeditLine();
  
  void setPreeditText(QInputMethodEvent *e, const QFont *font);

  void paintEvent(QPaintEvent * event);

  int getCaretOffset() const {return caret_offset_;}

private:
  QPixmap *pixmap_;
  int caret_offset_;

  const QColor *colors_;
};

class FQTermScreen: public QWidget {
  Q_OBJECT;
 public:

  enum PaintState {
     System = 0, Repaint = 1, NewData = 2, Blink = 4, Cursor = 8, Widget = 16
  };
  enum MouseState {
    Enter, Press, Move, Release, Leave
  };

  FQTermScreen(QWidget *parent, FQTermSession *session);
  ~FQTermScreen();

  void setSchema();
  void setTermFont(bool isEnglish, const QFont& font);

  QFont termFont(bool isEnglish);


  void setPaintState(PaintState ps) {
    paintState_ |= ps;
  }

  void clearPaintState(PaintState ps) {
    paintState_ &= ~ps;
  }

  bool testPaintState(PaintState ps) {
    return paintState_ & ps;
  }


  /*void clearPaintState() {
      paintState_ = None;
  }*/
  // Buffer cell coordinate to screen pixel.
  int getBufferStart() {return bufferStart_;}
  QPoint mapToPixel(const QPoint &);

  // Screen pixel coordinate to buffer cell coordinate.
  QPoint mapToChar(const QPoint &);

  // Buffer cell coordinate to screen pixel.
  QRect mapToRect(int x, int y, int width, int height);
  QRect mapToRect(const QRect &);
 private:
  int paintState_;
  void updateWidgetRect(QPainter& painter);
  void refreshScreen(QPainter &painter);
  void blinkScreen(QPainter &painter);
  void updateCursor(QPainter &painter);
  void repaintScreen(QPaintEvent *pe, QPainter &painter);
  void syncBufferAndScreen();
  void updateBackgroundPixmap();
  void drawBackground(QPainter& painter, const QRect& rect, int colorIndex);
  void drawBackgroundPixmap(QPainter& painter, const QRect& rect);
  int getVerticalSetting() const;

 signals:
  // 0 - enter  1 - press  2 - move  3 - release 4 - leave
  void mouseAction(int, QMouseEvent*);
  void inputEvent(const QString&);
  void termFontChange(bool isEnglish, QFont font);

 public slots:
  void bufferSizeChanged();
  void termSizeChanged(int column, int row);
  void bossColor();
  void updateScrollBar();
  void setFontAntiAliasing(bool on = true);
  void widgetHideAt(const QRect& rect);


 protected:
  void initFontMetrics();

  bool event(QEvent *e);

  void resizeEvent(QResizeEvent*);
  void focusInEvent(QFocusEvent*);
  void focusOutEvent(QFocusEvent*);

  void paintEvent(QPaintEvent*);

  // mouse
  void enterEvent(QEvent*);
  void leaveEvent(QEvent*);
  void mousePressEvent(QMouseEvent*);
  void mouseMoveEvent(QMouseEvent*);
  void mouseReleaseEvent(QMouseEvent*);
  void wheelEvent(QWheelEvent*);

  // display
  void eraseRect(QPainter &, int, int, int, int, short);
  void drawStr(QPainter &painter, const QString &str, 
               int x, int y, int length,
               unsigned char color, unsigned char attr,
               bool transparent, bool selected = false);
  void drawLine(QPainter &, int index, int startx = -1, int endx = -1, 
                bool complete = true);
  void drawCaret(QPainter &, bool);
  QRect drawMenuSelect(QPainter &, int);
  void drawUnderLine(QPainter &, const QPoint& startPoint, const QPoint& endPoint);
  
  // auxiluary
  int getPosX(int xChar) {
    return xChar *charWidth_;
  }
  int getPosY(int yLine) {
    return yLine *charHeight_;
  }

  //screen size, in pixel.
  QSize getScreenSize() const;



  void updateFont();
  void setFontMetrics();

  QImage &fade(QImage &, float, const QColor &);

  void inputMethodEvent(QInputMethodEvent *e);
  QVariant inputMethodQuery(Qt::InputMethodQuery property)const;


 protected slots:
  void blinkEvent();
  void cursorEvent();
  void scrollChanged(int);
  void prevPage();
  void nextPage();
  void prevLine();
  void nextLine();
  void scrollLine(int);
 protected:
   void drawSingleUnderLine(QPainter &, const QPoint& startPoint, const QPoint& endPoint);

  QRect clientRectangle_; // the display area
  QRect menuRect_;
  QRect widgetRect_; //we need to redraw this rect since a widget just disappeared.

  int scrollBarWidth_;

  QScrollBar *scrollBar_;

  QTimer *blinkTimer_;
  QTimer *cursorTimer_;

  bool isBlinkScreen_;
  bool isBlinkCursor_;
  bool hasBlinkText_;

  FQTermWindow *termWindow_;
  FQTermSession *session_;
  const FQTermBuffer *termBuffer_;
  FQTermParam *param_;

  bool is_light_background_mode_;

  QColor colors_[16];
  QFont *englishFont_;
  QFont *nonEnglishFont_;

  double fontAscent_, fontDescent_;
  double charWidth_, charHeight_;
  double cnLetterSpacing_;
  double enLetterSpacing_;
  double spLetterSpacing_;

  void updateFixedPitchInfo();
  bool cnFixedPitch_;
  bool enFixedPitch_;
  int lineSpacing_; // for future

  bool *areLinesBlink_;
  bool isCursorShown_;

  // background
  bool hasBackground_;
  QPixmap backgroundPixmap_;
  QPixmap originBackgroundPixmap_;
  //0 -- tile 1 -- center 2 -- stretch
  int backgroundRenderOption_;
  //0 -- Whole screen, 1 -- Only padding
  int backgroundCoverage_;
  bool backgroundUseAlpha_;
  int backgroundAlpha_;
  

  // the range of the buffer's lines to be displayed
  int bufferStart_;
  int bufferEnd_;

  QShortcut *gotoPrevPage_;
  QShortcut *gotoNextPage_;
  QShortcut *gotoPrevLine_;
  QShortcut *gotoNextLine_;

  FQTermConvert encodingConverter_;

  PreeditLine *preedit_line_;
  
  QString *tmp_im_query_;

  //TODO: change to bit field
  enum TextRenderingType{
    HalfAndAlign, FullAndAlign, FullNotAlign, HalfAndSpace
  };
  TextRenderingType charRenderingType(const QChar& c);

  friend class FQTermWindow;
  // for test only
};

}  // namespace FQTerm

#endif  // FQTERM_SCREEN_H