terminalgui  0.1.0
Widgets for your terminal, powered by Qt! Create textual GUI (TUI) in your console easily.
tgwidget.h
1 #pragma once
2 
3 #include <QObject>
4 #include <QPoint>
5 #include <QSize>
6 #include <QPointer>
7 
8 #include <QMetaEnum>
9 
10 #include <tgcolor.h>
11 
12 #include "utils/tghelpers.h"
13 #include "layouts/tglayout.h"
14 
15 class QTimer;
16 
17 namespace Tg {
18 class Screen;
19 class Layout;
20 
63 class Widget : public QObject
64 {
65  Q_OBJECT
66 
72  Q_PROPERTY(QPoint position READ position WRITE setPosition NOTIFY positionChanged)
73 
74 
80  Q_PROPERTY(QSize size READ size WRITE setSize NOTIFY sizeChanged)
81 
82 
86  Q_PROPERTY(int z READ z WRITE setZ NOTIFY zChanged)
87 
88 
99 
100 
109 
110 
121  Q_PROPERTY(Tg::Color textColor READ textColor WRITE setTextColor NOTIFY textColorChanged)
122 
123 
135 
136 
147 
148 
151  Q_PROPERTY(bool visible READ visible WRITE setVisible NOTIFY visibleChanged)
152 
153 
166  Q_PROPERTY(bool borderVisible READ borderVisible WRITE setBorderVisible NOTIFY borderVisibleChanged)
167 
168 
172  Q_PROPERTY(bool acceptsFocus READ acceptsFocus NOTIFY acceptsFocusChanged)
173 
174 
180  Q_PROPERTY(bool hasFocus READ hasFocus NOTIFY hasFocusChanged)
181 
182 
187  Q_PROPERTY(bool propagatesStyle READ propagatesStyle NOTIFY propagatesStyleChanged)
188 
189  friend class Screen;
190  friend class ScrollArea;
191 
192 public:
199  explicit Widget(Widget *parent);
200 
209  explicit Widget(Screen *screen);
210 
215  ~Widget();
216 
222  QPoint position() const;
223 
229  QSize size() const;
230 
236  QRect boundingRectangle() const;
237 
244  QRect globalBoundingRectangle() const;
245 
250  QRect contentsRectangle() const;
251 
258  Tg::Color backgroundColor() const;
259 
263  QChar backgroundCharacter() const;
264 
271  Tg::Color textColor() const;
272 
282  Tg::Color borderTextColor() const;
283 
291 
295  bool visible() const;
296 
303  bool borderVisible() const;
304 
310  bool acceptsFocus() const;
311 
318  bool hasFocus() const;
319 
329  void setAcceptsFocus(const bool accept);
330 
334  Screen *screen() const;
335 
343  Widget *parentWidget() const;
344 
353  Widget *topLevelParentWidget() const;
354 
359  bool isTopLevel() const;
360 
368  bool isBorder(const QPoint &pixel) const;
369 
374  bool verticalArrowsMoveFocus() const;
375 
380  QPoint mapFromGlobal(const QPoint &position) const;
381 
386  QPoint mapToGlobal(const QPoint &position) const;
387 
391  QPoint mapToChild(const WidgetPointer &child, const QPoint &position) const;
392 
397  bool propagatesStyle() const;
398 
403  void setStyle(const StylePointer &style, const bool propagate = true);
404 
409  Layout::Type layoutType() const;
410 
415  void setLayoutType(const Layout::Type type);
416 
428  SizeOvershoot layoutOvershoot() const;
429 
439  SizeOvershoot widgetOvershoot() const;
440 
444  int z() const;
445 
446 signals:
451  void needsRedraw(const RedrawType type, const Widget *widget) const;
452 
456  void positionChanged(const QPoint &position) const;
457 
461  void sizeChanged(const QSize &size) const;
462 
466  void backgroundColorChanged(const Tg::Color &color) const;
467 
471  void backgroundCharacterChanged(const QChar &character) const;
472 
476  void textColorChanged(const Tg::Color &color) const;
477 
481  void borderTextColorChanged(const Tg::Color &color) const;
482 
486  void borderBackgroundColorChanged(const Tg::Color &color) const;
487 
491  void visibleChanged(const bool visible) const;
492 
496  void borderVisibleChanged(const bool visible) const;
497 
501  void acceptsFocusChanged(const bool accepts) const;
502 
506  void hasFocusChanged(const bool hasFocus) const;
507 
513 
518  void moveFocusToNextWidget() const;
519 
524  void propagatesStyleChanged(const bool propagates) const;
525 
529  void styleChanged() const;
530 
534  void layoutOvershootChanged(const SizeOvershoot overshoot) const;
535 
539  void widgetOvershootChanged(const SizeOvershoot overshoot) const;
540 
544  void childAdded(Widget *child) const;
545 
549  void childRemoved() const;
550 
554  void zChanged(const int z) const;
555 
556 public slots:
561  void setPosition(const QPoint &position);
562 
566  void setSize(const QSize &size);
567 
571  void setBackgroundColor(const Tg::Color &color);
572 
576  void setBackgroundCharacter(const QChar &character);
577 
581  void setTextColor(const Tg::Color &color);
582 
586  void setBorderTextColor(const Tg::Color &color);
587 
591  void setBorderBackgroundColor(const Tg::Color &color);
592 
598  void setVisible(const bool visible);
599 
605  void show();
606 
612  void hide();
613 
617  void setBorderVisible(const bool visible);
618 
619  void setZ(const int z);
620 
624  WidgetList childrenWidgets() const;
625 
626 protected:
635  virtual void init();
636 
642  void setHasFocus(const bool active);
643 
649  virtual void consumeKeyboardBuffer(const QString &keyboardBuffer);
650 
660  virtual QString drawBorderPixel(const QPoint &pixel) const;
661 
673  virtual QString drawPixel(const QPoint &pixel) const;
674 
682  QPoint previousGlobalPosition() const;
683 
690  QSize previousSize() const;
691 
696  QRect globalPreviousBoundingRectangle() const;
697 
706  int effectiveBorderWidth() const;
707 
716  void setVerticalArrowsMoveFocus(const bool enable);
717 
729  void setPropagatesStyle(const bool propagate);
730 
735  void propagateStyleToChild(Widget *child) const;
736 
743  StylePointer style() const;
744 
748  void setWidgetOvershoot(const SizeOvershoot overshoot);
749 
758  void setupPressTimer(QTimer *timer) const;
759 
765  void doLayout();
766 
767 protected slots:
774  void scheduleFullRedraw() const;
775 
781  void schedulePartialRedraw() const;
782 
790  void schedulePreviousPositionRedraw() const;
791 
792 private:
793  bool canRedraw() const;
794  void updatePreviousBoundingRect();
795 
796  const int _borderWidth = 1;
797  bool _resizableByMouse = true;
798 
799  QPointer<Screen> _screen;
800  QPointer<Widget> _parentWidget;
801  StylePointer _style;
802  // TODO: smart pointer
803  Layout *_layout = nullptr;
804  SizeOvershoot _layoutOvershoot = Overshoot::None;
805  SizeOvershoot _widgetOvershoot = Overshoot::None;
806 
807  QPoint _position = { 0, 0 };
808  QPoint _previousGlobalPosition = { 0, 0 };
809  QSize _size = { 1, 1 };
810  QSize _previousSize = { 1, 1 };
811  int _z = 0;
812 
813  Tg::Color _backgroundColor;
814  Tg::Color _textColor;
815  Tg::Color _borderTextColor;
816  Tg::Color _borderBackgroundColor;
817 
818  bool _visible = false;
819  bool _borderVisible = true;
820  bool _acceptsFocus = false;
821  bool _hasFocus = false;
822  bool _verticalArrowsMoveFocus = false;
823  bool _propagatesStyle = true;
824  QChar _backgroundCharacter;
825 };
826 }
Tg::Widget::childrenWidgets
WidgetList childrenWidgets() const
Returns all children of this Widget which can be cast to Widget.
Definition: tgwidget.cpp:417
Tg::Widget::sizeChanged
void sizeChanged(const QSize &size) const
Emitted when Widget's size is changed.
Tg::Widget::setPropagatesStyle
void setPropagatesStyle(const bool propagate)
When propagate is true, this Widget will set its style() object on its children whenever it changes.
Definition: tgwidget.cpp:639
Tg::Widget::setBackgroundCharacter
void setBackgroundCharacter(const QChar &character)
Changes character drawn in the empty space of this Widget.
Definition: tgwidget.cpp:485
Tg::Widget::verticalArrowsMoveFocus
bool verticalArrowsMoveFocus() const
Returns true if this Widget ignores up and down arrows on keyboard.
Definition: tgwidget.cpp:301
Tg::Widget::setSize
void setSize(const QSize &size)
Resizes Widget to a new size.
Definition: tgwidget.cpp:464
Tg::Widget::setBorderVisible
void setBorderVisible(const bool visible)
Makes Widget's border visible (or not).
Definition: tgwidget.cpp:549
Tg::Widget::borderBackgroundColor
Tg::Color borderBackgroundColor
Color of the background of Widget's border (see boundingRectangle()).
Definition: tgwidget.h:146
Tg::Widget::hide
void hide()
Makes Widget hidden.
Definition: tgwidget.cpp:544
Tg::Screen
Screen is the "canvas" on which widgets (subclasses of Widget) are drawn.
Definition: tgscreen.h:31
Tg::Widget::borderBackgroundColorChanged
void borderBackgroundColorChanged(const Tg::Color &color) const
Indicates that Widget's border background color has changed.
Tg::Widget::propagateStyleToChild
void propagateStyleToChild(Widget *child) const
Propagates style() to child.
Definition: tgwidget.cpp:647
Tg::Widget::visibleChanged
void visibleChanged(const bool visible) const
Indicates that Widget is shown or hidden (depening on visible).
Tg::Widget::previousGlobalPosition
QPoint previousGlobalPosition() const
Returns Widget's last position (before it was moved), in global coordinates.
Definition: tgwidget.cpp:40
Tg::Widget::previousSize
QSize previousSize() const
Returns Widget's previous size (before it was resized).
Definition: tgwidget.cpp:50
Tg::Widget::needsRedraw
void needsRedraw(const RedrawType type, const Widget *widget) const
Indicates that widget (usually this) needs to be redrawn using redraw type.
Tg::Widget::topLevelParentWidget
Widget * topLevelParentWidget() const
Returns top-level parent of current Widget.
Definition: tgwidget.cpp:184
Tg::Overshoot::None
@ None
Tg::Widget::borderTextColorChanged
void borderTextColorChanged(const Tg::Color &color) const
Indicates that Widget's border text color has changed.
Tg::Widget::parentWidget
Widget * parentWidget() const
Returns the parent Widget.
Definition: tgwidget.cpp:179
Tg::Widget::hasFocus
bool hasFocus
Returns true when Widget is currently actively listening to keyboard signals.
Definition: tgwidget.h:180
Tg::Widget::borderVisibleChanged
void borderVisibleChanged(const bool visible) const
Indicates that Widget's border is now visible (or not).
Tg::Widget::moveFocusToNextWidget
void moveFocusToNextWidget() const
Indicates that active focus should be moved to the next Widget ready to receive it.
Tg::Widget::mapToChild
QPoint mapToChild(const WidgetPointer &child, const QPoint &position) const
Returns position mapped into child coordinate system.
Definition: tgwidget.cpp:330
Tg::Widget::layoutOvershootChanged
void layoutOvershootChanged(const SizeOvershoot overshoot) const
Emitted when layout overshoot has changed.
Tg::Widget::schedulePreviousPositionRedraw
void schedulePreviousPositionRedraw() const
Instructs Screen to redraw the area previously occupied by this Widget.
Definition: tgwidget.cpp:683
Tg::Layout
Helper class for Widget, manages positions and sizes of Widget's children.
Definition: tglayout.h:17
Tg::Layout::Type
Type
Type of a Layout.
Definition: tglayout.h:24
Tg::Widget::borderVisible
bool borderVisible
Controls whether border is drawn.
Definition: tgwidget.h:166
Tg::Widget::boundingRectangle
QRect boundingRectangle() const
Returns the rectangle constructed from position() and size().
Definition: tgwidget.cpp:55
Tg::Widget::setBorderTextColor
void setBorderTextColor(const Tg::Color &color)
Changes border text color.
Definition: tgwidget.cpp:503
Tg::Widget::setupPressTimer
void setupPressTimer(QTimer *timer) const
Sets timer object which controls how long pressed elements (buttons, scroll bar arrows,...
Definition: tgwidget.cpp:667
Tg
All Terminal GUI classes (both core and widgets) are defined within the Tg namespace.
Definition: tgcolor.h:6
Tg::Widget::setVerticalArrowsMoveFocus
void setVerticalArrowsMoveFocus(const bool enable)
If enable is true, clicking up arrow or down arrow on keyboard will be intercepted by parent Screen a...
Definition: tgwidget.cpp:634
Tg::Widget::setStyle
void setStyle(const StylePointer &style, const bool propagate=true)
Sets a new Style on this Widget.
Definition: tgwidget.cpp:344
Tg::Widget::setPosition
void setPosition(const QPoint &position)
Moves Widget to a new position.
Definition: tgwidget.cpp:444
Tg::Widget::backgroundColor
Tg::Color backgroundColor
Color of the background of Widget's interior (contentsRectangle()).
Definition: tgwidget.h:98
Tg::Widget::show
void show()
Makes Widget visible.
Definition: tgwidget.cpp:539
Tg::Widget
Base class for all widgets in a Terminal Gui application.
Definition: tgwidget.h:64
Tg::Widget::style
StylePointer style() const
Returns the pointer to Style object used to draw this Widget.
Definition: tgwidget.cpp:654
Tg::Widget::borderTextColor
Tg::Color borderTextColor
Color of the characters ("foreground color") printed on the borders of the Widget (see boundingRectan...
Definition: tgwidget.h:134
Tg::Widget::Widget
Widget(Widget *parent)
Constructs Widget as a child of parent.
Definition: tgwidget.cpp:12
Tg::Widget::drawBorderPixel
virtual QString drawBorderPixel(const QPoint &pixel) const
Returns ANSI-encoded string, used by Screen to draw the pixel.
Definition: tgwidget.cpp:199
Tg::Widget::size
QSize size
Size of the Widget.
Definition: tgwidget.h:80
Tg::Widget::propagatesStyle
bool propagatesStyle
If true, Widget will set its Style object on its children widgets.
Definition: tgwidget.h:187
Tg::Widget::backgroundCharacterChanged
void backgroundCharacterChanged(const QChar &character) const
Indicates that Widget's background character has changed.
Tg::Widget::contentsRectangle
QRect contentsRectangle() const
Returns the rectangle which holds Widget interior.
Definition: tgwidget.cpp:81
Tg::Color
Represents colors in a terminal.
Definition: tgcolor.h:20
Tg::Widget::setAcceptsFocus
void setAcceptsFocus(const bool accept)
When accept is true, this Widget will receive keyboard events from Screen instance - but only when it...
Definition: tgwidget.cpp:158
Tg::Widget::widgetOvershoot
SizeOvershoot widgetOvershoot() const
Returns current overshoot of contents of this Widget.
Definition: tgwidget.cpp:434
Tg::StylePointer
QSharedPointer< Style > StylePointer
Convenient alias of QSharedPointer<Style>.
Definition: tghelpers.h:51
Tg::Widget::moveFocusToPreviousWidget
void moveFocusToPreviousWidget() const
Indicates that active focus should be moved to the previous Widget ready to receive it.
Tg::Widget::textColorChanged
void textColorChanged(const Tg::Color &color) const
Indicates that Widget's text color has changed.
Tg::Widget::layoutType
Layout::Type layoutType() const
Returns Layout type.
Definition: tgwidget.cpp:365
Tg::Widget::effectiveBorderWidth
int effectiveBorderWidth() const
If borders are visible, it returns border width.
Definition: tgwidget.cpp:567
Tg::Widget::screen
Screen * screen() const
Returns the Screen on which this Widget is being drawn.
Definition: tgwidget.cpp:174
Tg::Widget::doLayout
void doLayout()
Forces Widget to lay out its children according to layoutType().
Definition: tgwidget.cpp:402
Tg::Widget::layoutOvershoot
SizeOvershoot layoutOvershoot() const
Returns current overshoot of layout inside this Widget.
Definition: tgwidget.cpp:429
Tg::WidgetList
QList< WidgetPointer > WidgetList
Convenient alias for QList<WidgetPointer>.
Definition: tghelpers.h:46
Tg::Widget::consumeKeyboardBuffer
virtual void consumeKeyboardBuffer(const QString &keyboardBuffer)
Called when Widget accepts focus and keyboardBuffer is not empty.
Definition: tgwidget.cpp:629
Tg::Widget::scheduleFullRedraw
void scheduleFullRedraw() const
Instructs Screen that full redraw of entire Screen area should be performed.
Definition: tgwidget.cpp:673
Tg::Widget::z
int z
Widget position on the z axis - this is used to control which Widgets should be displayed above and w...
Definition: tgwidget.h:86
Tg::Widget::setTextColor
void setTextColor(const Tg::Color &color)
Changes text color.
Definition: tgwidget.cpp:494
Tg::Widget::visible
bool visible
A visible Widget is visible.
Definition: tgwidget.h:151
Tg::Widget::acceptsFocusChanged
void acceptsFocusChanged(const bool accepts) const
Indicates that Widget now accepts focus (or not).
Tg::Widget::zChanged
void zChanged(const int z) const
Emitted when z value changes.
Tg::Widget::propagatesStyleChanged
void propagatesStyleChanged(const bool propagates) const
Indicates that Widget either stopped or resumed (propagates) propagating its Style object to its chil...
Tg::Widget::setBorderBackgroundColor
void setBorderBackgroundColor(const Tg::Color &color)
Changes border background color.
Definition: tgwidget.cpp:512
Tg::Widget::childRemoved
void childRemoved() const
Emitted when a child Widget has been removed.
Tg::Widget::setHasFocus
void setHasFocus(const bool active)
Sets this Widget to hold active focus (or not).
Definition: tgwidget.cpp:166
Tg::Widget::init
virtual void init()
Initializes Widget and it's connections.
Definition: tgwidget.cpp:576
Tg::Widget::position
QPoint position
Position of Widget.
Definition: tgwidget.h:72
Tg::ScrollArea
Definition: tgscrollarea.h:11
Tg::Widget::setLayoutType
void setLayoutType(const Layout::Type type)
Sets the way this Widget places, paints and treats children widgets to type.
Definition: tgwidget.cpp:370
Tg::Widget::mapFromGlobal
QPoint mapFromGlobal(const QPoint &position) const
Returns position mapped from global coordinates into local one (coordinates within the Widget,...
Definition: tgwidget.cpp:306
Tg::Widget::~Widget
~Widget()
Deregisters the Widget from parent Screen, then destroys the Widget object.
Definition: tgwidget.cpp:28
Tg::Widget::mapToGlobal
QPoint mapToGlobal(const QPoint &position) const
Returns position mapped from local coordinates into global ones (coordinates within parent Screen).
Definition: tgwidget.cpp:319
Tg::Widget::drawPixel
virtual QString drawPixel(const QPoint &pixel) const
Returns ANSI-encoded string, used by Screen to draw the pixel.
Definition: tgwidget.cpp:257
Tg::Widget::hasFocusChanged
void hasFocusChanged(const bool hasFocus) const
Indicates that Widget now hasFocus (or not).
Tg::Widget::globalPreviousBoundingRectangle
QRect globalPreviousBoundingRectangle() const
Returns globalBoundingRectangle() recorded last time when widget was moved or its size has been chang...
Definition: tgwidget.cpp:70
Tg::Widget::backgroundCharacter
QChar backgroundCharacter
This is the character which fills the empty background.
Definition: tgwidget.h:108
Tg::Widget::schedulePartialRedraw
void schedulePartialRedraw() const
Instructs Screen to redraw this Widget only, at its current position.
Definition: tgwidget.cpp:678
Tg::Widget::childAdded
void childAdded(Widget *child) const
Emitted when a child Widget has been added.
Tg::Widget::setVisible
void setVisible(const bool visible)
Makes Widget visible (or not).
Definition: tgwidget.cpp:521
Tg::Widget::styleChanged
void styleChanged() const
Emitted when Widget's Style object is changed.
Tg::Widget::acceptsFocus
bool acceptsFocus
When true, Widget will receive keyboard signals (Screen will call consumeKeyboardBuffer()) when its t...
Definition: tgwidget.h:172
Tg::Widget::isBorder
bool isBorder(const QPoint &pixel) const
Returns true when pixel lies within the border of the Widget.
Definition: tgwidget.cpp:283
Tg::Widget::positionChanged
void positionChanged(const QPoint &position) const
Emitted when Widget's position changes.
Tg::Widget::globalBoundingRectangle
QRect globalBoundingRectangle() const
Returns the rectangle contructed from position() (mapped to global coordinates using mapToGlobal()) a...
Definition: tgwidget.cpp:60
Tg::Widget::widgetOvershootChanged
void widgetOvershootChanged(const SizeOvershoot overshoot) const
Emitted when Widget overshoot has changed.
Tg::Widget::backgroundColorChanged
void backgroundColorChanged(const Tg::Color &color) const
Indicates that Widget's background color has changed.
Tg::Widget::setBackgroundColor
void setBackgroundColor(const Tg::Color &color)
Changes background color.
Definition: tgwidget.cpp:476
Tg::WidgetPointer
QPointer< Widget > WidgetPointer
Convenient alias of QPointer<Widget>.
Definition: tghelpers.h:41
Tg::RedrawType
RedrawType
Specifies how much of the Tg::Screen needs to be redrawn.
Definition: tghelpers.h:72
Tg::Widget::setWidgetOvershoot
void setWidgetOvershoot(const SizeOvershoot overshoot)
Sets current overshoot of the contents of this Widget.
Definition: tgwidget.cpp:659
Tg::Widget::isTopLevel
bool isTopLevel() const
Returns true if this Widget is top-level.
Definition: tgwidget.cpp:194
Tg::Widget::textColor
Tg::Color textColor
Color of the characters ("foreground color") printed inside of the Widget (see contentsRectangle()).
Definition: tgwidget.h:121