terminalgui
0.1.0
Widgets for your terminal, powered by Qt! Create textual GUI (TUI) in your console easily.
|
Base class for all widgets in a Terminal Gui application. More...
#include <tgwidget.h>
Public Slots | |
void | setPosition (const QPoint &position) |
Moves Widget to a new position. More... | |
void | setSize (const QSize &size) |
Resizes Widget to a new size. | |
void | setBackgroundColor (const Tg::Color &color) |
Changes background color. | |
void | setBackgroundCharacter (const QChar &character) |
Changes character drawn in the empty space of this Widget. | |
void | setTextColor (const Tg::Color &color) |
Changes text color. | |
void | setBorderTextColor (const Tg::Color &color) |
Changes border text color. | |
void | setBorderBackgroundColor (const Tg::Color &color) |
Changes border background color. | |
void | setVisible (const bool visible) |
Makes Widget visible (or not). More... | |
void | show () |
Makes Widget visible. More... | |
void | hide () |
Makes Widget hidden. More... | |
void | setBorderVisible (const bool visible) |
Makes Widget's border visible (or not). | |
void | setZ (const int z) |
WidgetList | childrenWidgets () const |
Returns all children of this Widget which can be cast to Widget. | |
Signals | |
void | needsRedraw (const RedrawType type, const Widget *widget) const |
Indicates that widget (usually this ) needs to be redrawn using redraw type. | |
void | positionChanged (const QPoint &position) const |
Emitted when Widget's position changes. | |
void | sizeChanged (const QSize &size) const |
Emitted when Widget's size is changed. | |
void | backgroundColorChanged (const Tg::Color &color) const |
Indicates that Widget's background color has changed. | |
void | backgroundCharacterChanged (const QChar &character) const |
Indicates that Widget's background character has changed. | |
void | textColorChanged (const Tg::Color &color) const |
Indicates that Widget's text color has changed. | |
void | borderTextColorChanged (const Tg::Color &color) const |
Indicates that Widget's border text color has changed. | |
void | borderBackgroundColorChanged (const Tg::Color &color) const |
Indicates that Widget's border background color has changed. | |
void | visibleChanged (const bool visible) const |
Indicates that Widget is shown or hidden (depening on visible). | |
void | borderVisibleChanged (const bool visible) const |
Indicates that Widget's border is now visible (or not). | |
void | acceptsFocusChanged (const bool accepts) const |
Indicates that Widget now accepts focus (or not). | |
void | hasFocusChanged (const bool hasFocus) const |
Indicates that Widget now hasFocus (or not). | |
void | moveFocusToPreviousWidget () const |
Indicates that active focus should be moved to the previous Widget ready to receive it. | |
void | moveFocusToNextWidget () const |
Indicates that active focus should be moved to the next Widget ready to receive it. | |
void | propagatesStyleChanged (const bool propagates) const |
Indicates that Widget either stopped or resumed (propagates) propagating its Style object to its children. | |
void | styleChanged () const |
Emitted when Widget's Style object is changed. | |
void | layoutOvershootChanged (const SizeOvershoot overshoot) const |
Emitted when layout overshoot has changed. | |
void | widgetOvershootChanged (const SizeOvershoot overshoot) const |
Emitted when Widget overshoot has changed. | |
void | childAdded (Widget *child) const |
Emitted when a child Widget has been added. | |
void | childRemoved () const |
Emitted when a child Widget has been removed. | |
void | zChanged (const int z) const |
Emitted when z value changes. | |
Public Member Functions | |
Widget (Widget *parent) | |
Constructs Widget as a child of parent. More... | |
Widget (Screen *screen) | |
Constructs a top-level Widget on a given screen. More... | |
~Widget () | |
Deregisters the Widget from parent Screen, then destroys the Widget object. | |
QPoint | position () const |
Returns current position of the Widget (relative). More... | |
QSize | size () const |
Returns current size of the Widget. More... | |
QRect | boundingRectangle () const |
Returns the rectangle constructed from position() and size(). More... | |
QRect | globalBoundingRectangle () const |
Returns the rectangle contructed from position() (mapped to global coordinates using mapToGlobal()) and size(). More... | |
QRect | contentsRectangle () const |
Returns the rectangle which holds Widget interior. More... | |
Tg::Color | backgroundColor () const |
Returns current background color - either taken from Style or value set using setBackgroundColor(). More... | |
QChar | backgroundCharacter () const |
Returns character drawn on empty space in this Widget. | |
Tg::Color | textColor () const |
Returns current text color - either taken from Style or value set using setTextColor(). More... | |
Tg::Color | borderTextColor () const |
Returns current border text color - either taken from Style or value set using setBorderTextColor(). More... | |
Tg::Color | borderBackgroundColor () const |
Returns current border background color - either taken from Style or value set using setBorderBackgroundColor(). More... | |
bool | visible () const |
Returns true when Widget is visible. | |
bool | borderVisible () const |
Returns true when Widget's border is visible. More... | |
bool | acceptsFocus () const |
Returns true when Widget is set to accept focus (keyboard or mouse). More... | |
bool | hasFocus () const |
Returns true when Widget actively holds focus (is receiving keyboard events). More... | |
void | setAcceptsFocus (const bool accept) |
When accept is true , this Widget will receive keyboard events from Screen instance - but only when it actively holds focus (see hasFocus()). More... | |
Screen * | screen () const |
Returns the Screen on which this Widget is being drawn. | |
Widget * | parentWidget () const |
Returns the parent Widget. More... | |
Widget * | topLevelParentWidget () const |
Returns top-level parent of current Widget. More... | |
bool | isTopLevel () const |
Returns true if this Widget is top-level. More... | |
bool | isBorder (const QPoint &pixel) const |
Returns true when pixel lies within the border of the Widget. More... | |
bool | verticalArrowsMoveFocus () const |
Returns true if this Widget ignores up and down arrows on keyboard. More... | |
QPoint | mapFromGlobal (const QPoint &position) const |
Returns position mapped from global coordinates into local one (coordinates within the Widget, where (0,0) is top-left corner). | |
QPoint | mapToGlobal (const QPoint &position) const |
Returns position mapped from local coordinates into global ones (coordinates within parent Screen). | |
QPoint | mapToChild (const WidgetPointer &child, const QPoint &position) const |
Returns position mapped into child coordinate system. | |
bool | propagatesStyle () const |
Returns true if this Widget automatically applies its Style to all children Widgets. More... | |
void | setStyle (const StylePointer &style, const bool propagate=true) |
Sets a new Style on this Widget. More... | |
Layout::Type | layoutType () const |
Returns Layout type. More... | |
void | setLayoutType (const Layout::Type type) |
Sets the way this Widget places, paints and treats children widgets to type. | |
SizeOvershoot | layoutOvershoot () const |
Returns current overshoot of layout inside this Widget. More... | |
SizeOvershoot | widgetOvershoot () const |
Returns current overshoot of contents of this Widget. More... | |
int | z () const |
Returns the z value - position of this Widget on z order stack. | |
Protected Slots | |
void | scheduleFullRedraw () const |
Instructs Screen that full redraw of entire Screen area should be performed. More... | |
void | schedulePartialRedraw () const |
Instructs Screen to redraw this Widget only, at its current position. More... | |
void | schedulePreviousPositionRedraw () const |
Instructs Screen to redraw the area previously occupied by this Widget. More... | |
Protected Member Functions | |
virtual void | init () |
Initializes Widget and it's connections. More... | |
void | setHasFocus (const bool active) |
Sets this Widget to hold active focus (or not). More... | |
virtual void | consumeKeyboardBuffer (const QString &keyboardBuffer) |
Called when Widget accepts focus and keyboardBuffer is not empty. More... | |
virtual QString | drawBorderPixel (const QPoint &pixel) const |
Returns ANSI-encoded string, used by Screen to draw the pixel. More... | |
virtual QString | drawPixel (const QPoint &pixel) const |
Returns ANSI-encoded string, used by Screen to draw the pixel. More... | |
QPoint | previousGlobalPosition () const |
Returns Widget's last position (before it was moved), in global coordinates. More... | |
QSize | previousSize () const |
Returns Widget's previous size (before it was resized). More... | |
QRect | globalPreviousBoundingRectangle () const |
Returns globalBoundingRectangle() recorded last time when widget was moved or its size has been changed. | |
int | effectiveBorderWidth () const |
If borders are visible, it returns border width. More... | |
void | setVerticalArrowsMoveFocus (const bool enable) |
If enable is true , clicking up arrow or down arrow on keyboard will be intercepted by parent Screen and used to switch focus to previous or next Widget. More... | |
void | setPropagatesStyle (const bool propagate) |
When propagate is true , this Widget will set its style() object on its children whenever it changes. More... | |
void | propagateStyleToChild (Widget *child) const |
Propagates style() to child. More... | |
StylePointer | style () const |
Returns the pointer to Style object used to draw this Widget. More... | |
void | setWidgetOvershoot (const SizeOvershoot overshoot) |
Sets current overshoot of the contents of this Widget. | |
void | setupPressTimer (QTimer *timer) const |
Sets timer object which controls how long pressed elements (buttons, scroll bar arrows, etc.) look different (pressed). More... | |
void | doLayout () |
Forces Widget to lay out its children according to layoutType(). More... | |
Properties | |
QPoint | position |
Position of Widget. More... | |
QSize | size |
Size of the Widget. More... | |
int | z |
Widget position on the z axis - this is used to control which Widgets should be displayed above and which below if they overlap. | |
Tg::Color | backgroundColor |
Color of the background of Widget's interior (contentsRectangle()). More... | |
QChar | backgroundCharacter |
This is the character which fills the empty background. More... | |
Tg::Color | textColor |
Color of the characters ("foreground color") printed inside of the Widget (see contentsRectangle()). More... | |
Tg::Color | borderTextColor |
Color of the characters ("foreground color") printed on the borders of the Widget (see boundingRectangle()). More... | |
Tg::Color | borderBackgroundColor |
Color of the background of Widget's border (see boundingRectangle()). More... | |
bool | visible |
A visible Widget is visible. More... | |
bool | borderVisible |
Controls whether border is drawn. More... | |
bool | acceptsFocus |
When true, Widget will receive keyboard signals (Screen will call consumeKeyboardBuffer()) when its turn comes. | |
bool | hasFocus |
Returns true when Widget is currently actively listening to keyboard signals. More... | |
bool | propagatesStyle |
If true , Widget will set its Style object on its children widgets. More... | |
Friends | |
class | Screen |
class | ScrollArea |
Base class for all widgets in a Terminal Gui application.
Plain Widget, when drawn on screen, will draw its border and an empty interior. If it is a child of another Widget, or when borderVisible() is false
, it only draw empty contents.
Widget subclasses reimplement its drawPixel() method to draw actual contents. It is also very important to reimplement init() method and put initialization code there, plus all signal and slot connections necessary to update the Screen when Widget changes.
Every Widget subclass should contain at least 2 constructors:
If a subclass needs keyboard interaction, it should call setAcceptsFocus() in its init() override, and implement consumeKeyboardBuffer() with logic responding to key presses. See Key namespace for helper constants which make detection of particular clicks easier.
Unlike QWidget which relies on a separate class to do layouting (QLayout), Widget can lay its children automatically. This can be controlled using setLayoutType().
Widget (and all its subclasses) can be styled in several ways. By default, Widget inherits Style from its parent (or Screen class) unless propagatesStyle() returns false
. A custom Style can be set (either for this Widget only, or also for all its children) using setStyle(). Alternatively, each individual style-related property can be overriden using various setters (for example: setBackgroundColor(), setTextColor() etc.). These setters always work only for this single Widget and have no effect on its children.
|
explicit |
Constructs Widget as a child of parent.
By default, a child Widget does not have a border.
|
explicit |
Constructs a top-level Widget on a given screen.
A top-level does have a border by default.
There can be many top-level Widgets registered under the same Screen.
bool Tg::Widget::acceptsFocus | ( | ) | const |
Returns true
when Widget is set to accept focus (keyboard or mouse).
Tg::Color Tg::Widget::backgroundColor | ( | ) | const |
Returns current background color - either taken from Style or value set using setBackgroundColor().
Tg::Color Tg::Widget::borderBackgroundColor | ( | ) | const |
Returns current border background color - either taken from Style or value set using setBorderBackgroundColor().
Tg::Color Tg::Widget::borderTextColor | ( | ) | const |
Returns current border text color - either taken from Style or value set using setBorderTextColor().
The "text" on Widget border are the lines (characters) used to draw the border. They are defined in BorderStyle.
bool Tg::Widget::borderVisible | ( | ) | const |
Returns true
when Widget's border is visible.
A top-level Widget has border visible by default. It can be overriden using setBorderVisible().
QRect Tg::Widget::boundingRectangle | ( | ) | const |
Returns the rectangle constructed from position() and size().
This rectangle includes Widget border and contents.
|
protectedvirtual |
Called when Widget accepts focus and keyboardBuffer is not empty.
Reimplemented in Tg::ScrollBar, Tg::RadioButton, Tg::Button, Tg::ListView, Tg::LineEdit, Tg::ScrollArea, and Tg::CheckBox.
QRect Tg::Widget::contentsRectangle | ( | ) | const |
Returns the rectangle which holds Widget interior.
It is similar to boundingRectangle() but with Widget border removed.
|
protected |
Forces Widget to lay out its children according to layoutType().
|
protectedvirtual |
Returns ANSI-encoded string, used by Screen to draw the pixel.
"Draws" the border at position pixel. The pixel is expected to be in local coordinate system (where (0,0) denotes the top-left corner of the border).
|
protectedvirtual |
Returns ANSI-encoded string, used by Screen to draw the pixel.
"Draws" the internal contents of the Widget. Default implementation draws the border (see drawBorderPixel()), any child Widgets (if present), in accordance with layoutType(). If there is any empty space left, it is filled with backgroundCharacter().
Reimplemented in Tg::ScrollBar, Tg::ScrollArea, and Tg::Label.
|
protected |
If borders are visible, it returns border width.
Otherwise, it returns 0.
0
(zero) or 1
.QRect Tg::Widget::globalBoundingRectangle | ( | ) | const |
Returns the rectangle contructed from position() (mapped to global coordinates using mapToGlobal()) and size().
This rectangle includes Widget border and contents.
bool Tg::Widget::hasFocus | ( | ) | const |
Returns true
when Widget actively holds focus (is receiving keyboard events).
|
slot |
Makes Widget hidden.
|
protectedvirtual |
Initializes Widget and it's connections.
All subclasses should call init() of its parent class. If a subclass has any properties or signals which affect the visual look of the Widget, it should reimplement init() and connect such signal to schedulePartialRedraw() slot.
Reimplemented in Tg::ScrollBar, Tg::RadioButton, Tg::Button, Tg::ListView, Tg::LineEdit, Tg::ScrollArea, Tg::Label, and Tg::CheckBox.
bool Tg::Widget::isBorder | ( | const QPoint & | pixel | ) | const |
Returns true
when pixel lies within the border of the Widget.
If border is not visible, it will always return false
.
bool Tg::Widget::isTopLevel | ( | ) | const |
Returns true
if this Widget is top-level.
This means that parentWidget() returns nullptr
.
Tg::SizeOvershoot Tg::Widget::layoutOvershoot | ( | ) | const |
Returns current overshoot of layout inside this Widget.
If Widgets within the Layout do not fit the Widget, SizeOvershoot will indicate whether Widget is too small horizontally or vertically.
Visually, it is represented by drawing red arrows on Widget border (by default - see BorderStyle::horizontalOvershoot, BorderStyle::verticalOvershoot, BorderStyle::overshootTextColor and BorderStyle::overshootBackgroundColor).
Tg::Layout::Type Tg::Widget::layoutType | ( | ) | const |
Tg::Widget * Tg::Widget::parentWidget | ( | ) | const |
Returns the parent Widget.
If returned pointer is a nullptr
, it means this Widget is a top level Widget, usually drawn with border (unless setBorderVisible() has overriden the default behavior).
QPoint Tg::Widget::position | ( | ) | const |
Returns current position of the Widget (relative).
|
protected |
Returns Widget's last position (before it was moved), in global coordinates.
If Widget has not been moved, it will return same location as position() mapped to global coordinate system.
|
protected |
Returns Widget's previous size (before it was resized).
If Widget has not been resized, it will return same result as size().
bool Tg::Widget::propagatesStyle | ( | ) | const |
|
protected |
|
protectedslot |
|
protectedslot |
|
protectedslot |
Instructs Screen to redraw the area previously occupied by this Widget.
This is necessary to "clean up" the are of Screen where Widget used to be before it has been moved.
void Tg::Widget::setAcceptsFocus | ( | const bool | accept | ) |
When accept is true
, this Widget will receive keyboard events from Screen instance - but only when it actively holds focus (see hasFocus()).
In order to be able to "do something" with keyboard events, reimplement consumeKeyboardBuffer().
|
protected |
|
slot |
|
protected |
void Tg::Widget::setStyle | ( | const StylePointer & | style, |
const bool | propagate = true |
||
) |
|
protected |
|
protected |
If enable is true
, clicking up arrow or down arrow on keyboard will be intercepted by parent Screen and used to switch focus to previous or next Widget.
This Widget will not receive a call to consumeKeyboardBuffer().
|
slot |
|
slot |
Makes Widget visible.
QSize Tg::Widget::size | ( | ) | const |
Returns current size of the Widget.
|
protected |
Returns the pointer to Style object used to draw this Widget.
By default, same Style object is shared by all Widgets within a Screen.
Tg::Color Tg::Widget::textColor | ( | ) | const |
Returns current text color - either taken from Style or value set using setTextColor().
Tg::Widget * Tg::Widget::topLevelParentWidget | ( | ) | const |
Returns top-level parent of current Widget.
If current Widget is already top-level, it will return a pointer to itself.
This function will never return a nullptr
.
bool Tg::Widget::verticalArrowsMoveFocus | ( | ) | const |
Tg::SizeOvershoot Tg::Widget::widgetOvershoot | ( | ) | const |
Returns current overshoot of contents of this Widget.
It is very similar to layoutOvershoot() but overshoot is reported when Widget contents (not children Widgets arranged by a layout) fo not fit. For example, when a Label has text which is too long to fit.
|
readwrite |
This is the character which fills the empty background.
By default it is space, so no character is shown at all (only backgroundColor()).
|
readwrite |
Color of the background of Widget's interior (contentsRectangle()).
|
readwrite |
Color of the background of Widget's border (see boundingRectangle()).
|
readwrite |
Color of the characters ("foreground color") printed on the borders of the Widget (see boundingRectangle()).
|
readwrite |
Controls whether border is drawn.
When border is visible, Widget contents are drawn inside of it (call contentsRectangle() to get the are inside of the borders).
When border is not visible, contents fill the whole boundingRectangle() (thus contentsRectangle() and boundingRectangle() return the same data).
|
read |
Returns true
when Widget is currently actively listening to keyboard signals.
|
readwrite |
|
read |
|
readwrite |
|
readwrite |
Color of the characters ("foreground color") printed inside of the Widget (see contentsRectangle()).
|
readwrite |
A visible Widget is visible.
Right?