2
This file is part of KTerminal, QML plugin of the Konsole,
3
which is a terminal emulator from KDE.
5
Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
6
Copyright 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
8
Rewritten for QT5/QML by Dmitry Zagnoyko <hiroshidi@gmail.com>, Copyright (C) 2013
10
This program is free software; you can redistribute it and/or modify
11
it under the terms of the GNU General Public License as published by
12
the Free Software Foundation; either version 2 of the License, or
13
(at your option) any later version.
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
GNU General Public License for more details.
20
You should have received a copy of the GNU General Public License
21
along with this program; if not, write to the Free Software
22
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26
#ifndef TERMINALDISPLAY_H
27
#define TERMINALDISPLAY_H
29
#include <QtGui/QColor>
30
#include <QtCore/QPointer>
34
#include "Character.h"
36
//#include "konsole_export.h"
37
#define KONSOLEPRIVATE_EXPORT
39
#include <QtQuick/QQuickItem>
40
#include <QtQuick/QQuickPaintedItem>
48
extern unsigned short vt100_graphics[32];
53
* A widget which displays output from a terminal emulation and sends input keypresses and mouse activity
56
* When the terminal emulation receives new output from the program running in the terminal,
57
* it will update the display by calling updateImage().
59
* TODO More documentation
62
class KONSOLEPRIVATE_EXPORT KTerminalDisplay : public QQuickPaintedItem
66
Q_PROPERTY(KSession *session READ getSession WRITE setSession NOTIFY changedSession)
67
Q_PROPERTY(QString colorScheme READ scheme() WRITE setColorScheme NOTIFY changedScheme)
68
Q_PROPERTY(QFont font READ getVTFont WRITE setVTFont )
69
Q_PROPERTY(bool activeFocusOnClick READ autoFocus WRITE setAutoFocus NOTIFY changedAutoFocus)
70
Q_PROPERTY(bool ShowIMEOnClick READ autoVKB WRITE setAutoVKB NOTIFY changedAutoVKB)
74
KTerminalDisplay(QQuickItem *parent = 0);
77
QString scheme() { return m_scheme; }
78
/////////////////////////////////////////////////////////////////////////////////////
79
/////////////////////////////////////////////////////////////////////////////////////
80
/** Returns the terminal color palette used by the display. */
81
const ColorEntry* colorTable() const;
82
/** Sets the terminal color palette used by the display. */
83
void setColorTable(const ColorEntry table[]);
85
* Returns the seed used to generate random colors for the display
86
* (in color schemes that support them).
88
uint randomSeed() const;
90
/** Returns true if the cursor is set to blink or false otherwise. */
91
bool blinkingCursor() { return _hasBlinkingCursor; }
92
/** Specifies whether or not the cursor blinks. */
93
void setBlinkingCursor(bool blink);
95
/** Specifies whether or not text can blink. */
96
void setBlinkingTextEnabled(bool blink);
98
void setLineSpacing(uint);
99
uint lineSpacing() const;
101
void emitSelection(bool useXselection,bool appendReturn);
104
* This enum describes the available shapes for the keyboard cursor.
105
* See setKeyboardCursorShape()
107
enum KeyboardCursorShape
109
/** A rectangular block which covers the entire area of the cursor character. */
112
* A single flat line which occupies the space at the bottom of the cursor
117
* An cursor shaped like the capital letter 'I', similar to the IBeam
118
* cursor used in Qt/KDE text editors.
123
* Sets the shape of the keyboard cursor. This is the cursor drawn
124
* at the position in the terminal where keyboard input will appear.
126
* In addition the terminal display widget also has a cursor for
127
* the mouse pointer, which can be set using the QWidget::setCursor()
130
* Defaults to BlockCursor
132
void setKeyboardCursorShape(KeyboardCursorShape shape);
134
* Returns the shape of the keyboard cursor. See setKeyboardCursorShape()
136
KeyboardCursorShape keyboardCursorShape() const;
139
* Sets the color used to draw the keyboard cursor.
141
* The keyboard cursor defaults to using the foreground color of the character
144
* @param useForegroundColor If true, the cursor color will change to match
145
* the foreground color of the character underneath it as it is moved, in this
146
* case, the @p color parameter is ignored and the color of the character
147
* under the cursor is inverted to ensure that it is still readable.
148
* @param color The color to use to draw the cursor. This is only taken into
149
* account if @p useForegroundColor is false.
151
void setKeyboardCursorColor(bool useForegroundColor , const QColor& color);
154
* Returns the color of the keyboard cursor, or an invalid color if the keyboard
155
* cursor color is set to change according to the foreground color of the character
158
QColor keyboardCursorColor() const;
161
void ShowVKB(bool show);
164
* Returns the number of lines of text which can be displayed in the widget.
166
* This will depend upon the height of the widget and the current font.
169
int lines() { return _lines; }
171
* Returns the number of characters of text which can be displayed on
172
* each line in the widget.
174
* This will depend upon the width of the widget and the current font.
177
int columns() { return _columns; }
180
* Returns the height of the characters in the font used to draw the text in the display.
182
int fontHeight() { return _fontHeight; }
184
* Returns the width of the characters in the display.
185
* This assumes the use of a fixed-width font.
187
int fontWidth() { return _fontWidth; }
192
* Sets which characters, in addition to letters and numbers,
193
* are regarded as being part of a word for the purposes
194
* of selecting words in the display by double clicking on them.
196
* The word boundaries occur at the first and last characters which
197
* are either a letter, number, or a character in @p wc
199
* @param wc An array of characters which are to be considered parts
200
* of a word ( in addition to letters and numbers ).
202
void setWordCharacters(const QString& wc);
204
* Returns the characters which are considered part of a word for the
205
* purpose of selecting words in the display with the mouse.
207
* @see setWordCharacters()
209
QString wordCharacters() { return _wordCharacters; }
212
* Sets the type of effect used to alert the user when a 'bell' occurs in the
215
* The terminal session can trigger the bell effect by calling bell() with
218
void setBellMode(int mode);
220
* Returns the type of effect used to alert the user when a 'bell' occurs in
221
* the terminal session.
225
int bellMode() { return _bellMode; }
228
* This enum describes the different types of sounds and visual effects which
229
* can be used to alert the user when a 'bell' occurs in the terminal
234
/** A system beep. */
237
* KDE notification. This may play a sound, show a passive popup
238
* or perform some other action depending on the user's settings.
241
/** A silent, visual bell (eg. inverting the display's colors briefly) */
243
/** No bell effects */
247
void setSelection(const QString &t);
250
* Specified whether anti-aliasing of text in the terminal display
251
* is enabled or not. Defaults to enabled.
253
static void setAntialias( bool antialias ) { _antialiasText = antialias; }
255
* Returns true if anti-aliasing of text in the terminal is enabled.
257
static bool antialias() { return _antialiasText; }
260
* Specifies whether characters with intense colors should be rendered
261
* as bold. Defaults to true.
263
void setBoldIntense(bool value) { _boldIntense = value; }
265
* Returns true if characters with intense colors are rendered in bold.
267
bool getBoldIntense() { return _boldIntense; }
270
* Sets the status of the BiDi rendering inside the terminal display.
271
* Defaults to disabled.
273
void setBidiEnabled(bool set) { _bidiEnabled=set; }
275
* Returns the status of the BiDi rendering in this widget.
277
bool isBidiEnabled() { return _bidiEnabled; }
280
* Sets the terminal screen section which is displayed in this widget.
281
* When updateImage() is called, the display fetches the latest character image from the
282
* the associated terminal screen window.
284
* In terms of the model-view paradigm, the ScreenWindow is the model which is rendered
285
* by the TerminalDisplay.
287
void setScreenWindow( ScreenWindow* window );
288
/** Returns the terminal screen section which is displayed in this widget. See setScreenWindow() */
289
ScreenWindow* screenWindow() const;
291
static bool HAVE_TRANSPARENCY;
292
/////////////////////////////////////////////////////////////////////////////////////
293
/////////////////////////////////////////////////////////////////////////////////////
300
void setColorScheme(const QString &name);
301
QStringList availableColorSchemes();
303
void click(qreal x, qreal y);
305
bool autoFocus() { return m_focusOnClick; }
306
void setAutoFocus(bool au);
307
bool autoVKB() { return m_showVKBonClick; }
308
void setAutoVKB(bool au);
311
/** Returns the font used to draw characters in the display */
312
QFont getVTFont() { return m_font; }
315
* Sets the font used to draw the display. Has no effect if @p font
316
* is larger than the size of the display itself.
318
void setVTFont(const QFont& font);
321
* Scroll to the bottom of the terminal (reset scrolling).
325
/////////////////////////////////////////////////////////////////////////////////////
326
/////////////////////////////////////////////////////////////////////////////////////
328
* Causes the terminal display to fetch the latest character image from the associated
329
* terminal screen ( see setScreenWindow() ) and redraw the display.
333
* Causes the terminal display to fetch the latest line status flags from the
334
* associated terminal screen ( see setScreenWindow() ).
336
void updateLineProperties();
338
/** Copies the selected text to the clipboard. */
339
void copyClipboard();
341
* Pastes the content of the clipboard into the
344
void pasteClipboard();
346
* Pastes the content of the selection into the
349
void pasteSelection();
352
* Changes whether the flow control warning box should be shown when the flow control
353
* stop key (Ctrl+S) are pressed.
355
void setFlowControlWarningEnabled(bool enabled);
357
* Returns true if the flow control warning box is enabled.
358
* See outputSuspended() and setFlowControlWarningEnabled()
360
bool flowControlWarningEnabled() const
361
{ return _flowControlWarningEnabled; }
364
* Causes the widget to display or hide a message informing the user that terminal
365
* output has been suspended (by using the flow control key combination Ctrl+S)
367
* @param suspended True if terminal output has been suspended and the warning message should
368
* be shown or false to indicate that terminal output has been resumed and that
369
* the warning message should disappear.
371
//void outputSuspended(bool suspended);
374
* Shows a notification that a bell event has occurred in the terminal.
375
* TODO: More documentation here
377
void bell(const QString& message);
380
* Sets the background of the display to the specified color.
381
* @see setColorTable(), setForegroundColor()
383
void setBackgroundColor(const QColor& color);
386
* Sets the text of the display to the specified color.
387
* @see setColorTable(), setBackgroundColor()
389
void setForegroundColor(const QColor& color);
391
void selectionChanged();
392
/////////////////////////////////////////////////////////////////////////////////////
393
/////////////////////////////////////////////////////////////////////////////////////
395
void setSession(KSession * session);
396
KSession * getSession() const { return m_session; }
399
void changedScheme(QString scheme);
400
void changedAutoVKB(bool au);
401
void changedAutoFocus(bool au);
405
/////////////////////////////////////////////////////////////////////////////////////
406
/////////////////////////////////////////////////////////////////////////////////////
408
* Emitted when the user presses a key whilst the terminal widget has focus.
410
void keyPressedSignal(QKeyEvent *e);
412
void changedFontMetricSignal(int height, int width);
413
void changedContentSizeSignal(int height, int width);
416
* Emitted when the user right clicks on the display, or right-clicks with the Shift
417
* key held down if usesMouse() is true.
419
* This can be used to display a context menu.
421
void configureRequest(const QPoint& position);
424
* When a shortcut which is also a valid terminal key sequence is pressed while
425
* the terminal widget has focus, this signal is emitted to allow the host to decide
426
* whether the shortcut should be overridden.
427
* When the shortcut is overridden, the key sequence will be sent to the terminal emulation instead
428
* and the action associated with the shortcut will not be triggered.
430
* @p override is set to false by default and the shortcut will be triggered as normal.
432
void overrideShortcutCheck(QKeyEvent* keyEvent,bool& override);
433
void isBusySelecting(bool);
434
//void sendStringToEmu(const char*);
436
// qtermwidget signals
437
void copyAvailable(bool);
439
void termLostFocus();
440
/////////////////////////////////////////////////////////////////////////////////////
441
/////////////////////////////////////////////////////////////////////////////////////
443
void changedSession(KSession *session);
447
void paint (QPainter * painter);
448
void keyPressEvent(QKeyEvent *event);
449
bool event( QEvent *);
451
void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
452
QRect geometryRound(const QRectF &r) const;
454
void mousePressEvent(QMouseEvent*ev);
455
// void mouseReleaseEvent( QMouseEvent* );
456
// void mouseMoveEvent( QMouseEvent* );
458
void focusInEvent(QFocusEvent* event);
459
void focusOutEvent(QFocusEvent* event);
463
/////////////////////////////////////////////////////////////////////////////////////
464
/////////////////////////////////////////////////////////////////////////////////////
465
void fontChange(const QFont &font);
466
void extendSelection(const QPoint &pos );
468
// classifies the 'ch' into one of three categories
469
// and returns a character to indicate which category it is in
471
// - A space (returns ' ')
472
// - Part of a word (returns 'a')
473
// - Other characters (returns the input character)
474
QChar charClass(QChar ch) const;
479
// cath for UT Soft Keyboard QInputMethodEvent
480
void inputMethodEvent ( QInputMethodEvent* event );
481
void inputMethodQuery( QInputMethodQueryEvent* event );
482
QVariant inputMethodQuery( Qt::InputMethodQuery query ) const;
483
/////////////////////////////////////////////////////////////////////////////////////
484
/////////////////////////////////////////////////////////////////////////////////////
488
/////////////////////////////////////////////////////////////////////////////////////
489
/////////////////////////////////////////////////////////////////////////////////////
491
void blinkCursorEvent();
493
//Renables bell noises and visuals. Used to disable further bells for a short period of time
494
//after emitting the first in a sequence of bell events.
496
/////////////////////////////////////////////////////////////////////////////////////
497
/////////////////////////////////////////////////////////////////////////////////////
501
void swapColorTable();
504
/////////////////////////////////////////////////////////////////////////////////////
505
/////////////////////////////////////////////////////////////////////////////////////
506
// -- Drawing helpers --
508
// divides the part of the display specified by 'rect' into
509
// fragments according to their colors and styles and calls
510
// drawTextFragment() to draw the fragments
511
void drawContents(QPainter *paint, QRectF &rect);
513
// draws a section of text, all the text in this section
514
// has a common color and style
515
void drawTextFragment(QPainter *painter, const QRectF &rect,
516
const QString& text, const Character* style);
518
// draws the background for a text fragment
519
// if useOpacitySetting is true then the color's alpha value will be set to
520
// the display's transparency (set with setOpacity()), otherwise the background
521
// will be drawn fully opaque
522
void drawBackground(QPainter *painter, const QRectF& rect, const QColor& color,
523
bool useOpacitySetting);
525
// draws the cursor character
526
void drawCursor(QPainter *painter, const QRectF &rect , const QColor& foregroundColor,
527
const QColor& backgroundColor , bool& invertColors);
529
// draws the characters or line graphics in a text fragment
530
void drawCharacters(QPainter *painter, const QRectF &rect, const QString& text,
531
const Character* style, bool invertCharacterColor);
533
// draws a string of line graphics
534
void drawLineCharString(QPainter *painter, qreal x, qreal y,
535
const QString& str, const Character* attributes);
537
// draws the preedit string for input methods
538
void drawInputMethodPreeditString(QPainter *painter , const QRectF& rect);
542
// maps an area in the character image to an area on the widget
543
QRect imageToWidget(const QRect& imageArea) const;
545
// maps a point on the widget to the position ( ie. line and column )
546
// of the character at that point.
547
void getCharacterPosition(const QPointF& widgetPoint,int& line,int& column) const;
549
// the area where the preedit string for input methods will be draw
550
QRectF preeditRect() const;
552
// scrolls the image by a number of lines.
553
// 'lines' may be positive ( to scroll the image down )
554
// or negative ( to scroll the image up )
555
// 'region' is the part of the image to scroll - currently only
556
// the top, bottom and height of 'region' are taken into account,
557
// the left and right are ignored.
558
void scrollImage(int lines , const QRect& region);
561
void propagateSize();
562
void updateImageSize();
565
// returns the position of the cursor in columns and lines
566
QPoint cursorPosition() const;
568
// redraws the cursor
571
bool handleShortcutOverrideEvent(QKeyEvent* event);
572
/////////////////////////////////////////////////////////////////////////////////////
573
/////////////////////////////////////////////////////////////////////////////////////
576
* Sets the seed used to generate random colors for the display
577
* (in color schemes that support them).
579
void setRandomSeed(uint seed);
583
/////////////////////////////////////////////////////////////////////////////////////
585
/////////////////////////////////////////////////////////////////////////////////////
592
bool m_showVKBonClick;
594
QQuickItem *m_parent;
597
/////////////////////////////////////////////////////////////////////////////////////
598
/////////////////////////////////////////////////////////////////////////////////////
599
// the window onto the terminal screen which this display
600
// is currently showing.
601
QPointer<ScreenWindow> _screenWindow;
604
bool _fixedFont; // has fixed pitch
605
int _fontHeight; // height
606
qreal _fontWidth; // width
607
int _fontAscent; // ascend
608
bool _boldIntense; // Whether intense colors should be rendered with bold font
610
qreal _leftMargin; // offset
611
qreal _topMargin; // offset
613
int _lines; // the number of lines that can be displayed in the widget
614
int _columns; // the number of columns that can be displayed in the widget
616
int _usedLines; // the number of lines that are actually being used, this will be less
617
// than 'lines' if the character image provided with setImage() is smaller
618
// than the maximum image size which can be displayed
620
int _usedColumns; // the number of columns that are actually being used, this will be less
621
// than 'columns' if the character image provided with setImage() is smaller
622
// than the maximum image size which can be displayed
624
qreal _contentHeight;
627
Character* _image; // [lines][columns]
628
// only the area [usedLines][usedColumns] in the image contains valid data
631
QVector<LineProperty> _lineProperties;
633
ColorEntry _colorTable[TABLE_COLORS];
639
QPoint _iPntSel; // initial selection point
640
QPoint _pntSel; // current selection point
641
QPoint _tripleSelBegin; // help avoid flicker
642
int _actSel; // selection state
643
bool _wordSelectionMode;
644
bool _lineSelectionMode;
645
bool _preserveLineBreaks;
646
bool _columnSelectionMode;
648
QClipboard* _clipboard;
649
QString _wordCharacters;
652
bool _blinking; // hide text in paintEvent
653
bool _hasBlinker; // has characters to blink
654
bool _cursorBlinking; // hide cursor in paintEvent
655
bool _hasBlinkingCursor; // has blinking cursor enabled
656
bool _allowBlinkingText; // allow text to blink
658
bool _isFixedSize; //Columns / lines are locked.
659
QTimer* _blinkTimer; // active when hasBlinker
660
QTimer* _blinkCursorTimer; // active when hasBlinkingCursor
661
QTimer* _resizeTimer;
663
bool _flowControlWarningEnabled;
666
bool _colorsInverted; // true during visual bell
669
KeyboardCursorShape _cursorShape;
671
// custom cursor color. if this is invalid then the foreground
672
// color of the character under the cursor is used
676
struct InputMethodData
678
QString preeditString;
679
QRectF previousPreeditRect;
681
InputMethodData _inputMethodData;
683
static bool _antialiasText; // do we antialias or not
685
//the delay in milliseconds between redrawing blinking text
686
static const int TEXT_BLINK_DELAY = 500;
687
static const int DEFAULT_LEFT_MARGIN = 1;
688
static const int DEFAULT_TOP_MARGIN = 1;
689
/////////////////////////////////////////////////////////////////////////////////////
690
/////////////////////////////////////////////////////////////////////////////////////
693
static void setTransparencyEnabled(bool enable)
695
HAVE_TRANSPARENCY = enable;
699
class AutoScrollHandler : public QObject
704
AutoScrollHandler(QQuickItem* parent);
707
virtual void timerEvent(QTimerEvent* event);
708
virtual bool eventFilter(QObject* watched,QEvent* event);
711
QQuickItem* widget() const { return static_cast<QQuickItem*>(parent()); }
715
#endif // TERMINALDISPLAY_H