terminalgui  0.1.0
Widgets for your terminal, powered by Qt! Create textual GUI (TUI) in your console easily.
tgscreen.h
1 #pragma once
2 
3 #include <QObject>
4 #include <QPointer>
5 #include <QList>
6 #include <QSize>
7 #include <QRect>
8 #include <QTimer>
9 
10 #include "utils/tghelpers.h"
11 
17 namespace Tg {
18 class Terminal;
19 
30 class Screen : public QObject
31 {
32  Q_OBJECT
33 
44  Q_PROPERTY(QSize size READ size NOTIFY sizeChanged)
45 
46 
51  Q_PROPERTY(bool canDragWidgets READ canDragWidgets WRITE setCanDragWidgets NOTIFY canDragWidgetsChanged)
52 
53  friend class Widget;
54 
55 public:
64  Screen(QObject *parent = nullptr, const StylePointer &style = nullptr);
65 
71  QSize size() const;
72 
76  StylePointer style() const;
77 
78  bool canDragWidgets() const;
79 
80 public slots:
91  void scheduleRedraw(const RedrawType type, const Widget *widget);
92 
100 
107  void moveFocusToNextWidget();
108 
109  void setCanDragWidgets(const bool canDragWidgets);
110 
111 signals:
115  void sizeChanged(const QSize &size) const;
116 
117  void canDragWidgetsChanged(const bool canDragWidgets) const;
118 
119 private slots:
120  void draw();
121  void checkKeyboard();
122  void setSize(const QSize &size);
123 
124 private:
125  enum class DragType {
126  Unknown,
127  Move,
128  Resize
129  };
130 
138  void registerWidget(Widget *widget);
139  void deregisterWidget(Widget *widget);
140 
141  void updateRedrawRegions(const RedrawType type, const Widget *widget);
142  void updateRedrawRegion(const QRect &region);
143 
149  void compressRedraws();
150  void setActiveFocusWidget(const WidgetPointer &widget);
151  void clearActiveFocusWidget();
152 
153  void handleDrag(const QPoint &point, const bool isPressActive);
154 
155  QTimer _keyboardTimer;
156  QTimer _redrawTimer;
157  QVector<QRect> _redrawRegions;
158  QSize _size;
159 
160  WidgetList _widgets;
161  WidgetPointer _activeFocusWidget;
162  StylePointer _style;
163  QPointer<Terminal> _terminal;
164 
165  bool _canDragWidgets = true;
166  DragType _dragType = DragType::Unknown;
167  WidgetPointer _dragWidget;
168  QPoint _dragRelativePosition;
169 };
170 }
Tg::Screen::style
StylePointer style() const
Returns the default style, shared with all Widget instances.
Definition: tgscreen.cpp:69
Tg::Screen::moveFocusToNextWidget
void moveFocusToNextWidget()
Finds previous Widget ready to accept keyboard focus and moves the focus to it.
Definition: tgscreen.cpp:117
Tg::Screen
Screen is the "canvas" on which widgets (subclasses of Widget) are drawn.
Definition: tgscreen.h:31
Tg::Screen::size
QSize size
Size of the Screen.
Definition: tgscreen.h:44
Tg::Terminal
Cross-platform representation of terminal window.
Definition: tgterminal.h:20
Tg::Screen::sizeChanged
void sizeChanged(const QSize &size) const
Emitted when Screen's size is modified.
Tg
All Terminal GUI classes (both core and widgets) are defined within the Tg namespace.
Definition: tgcolor.h:6
Tg::Widget
Base class for all widgets in a Terminal Gui application.
Definition: tgwidget.h:64
Tg::Screen::moveFocusToPreviousWidget
void moveFocusToPreviousWidget()
Finds next Widget ready to accept keyboard focus and moves the focus to it.
Definition: tgscreen.cpp:85
Tg::StylePointer
QSharedPointer< Style > StylePointer
Convenient alias of QSharedPointer<Style>.
Definition: tghelpers.h:51
Tg::WidgetList
QList< WidgetPointer > WidgetList
Convenient alias for QList<WidgetPointer>.
Definition: tghelpers.h:46
Tg::Screen::Screen
Screen(QObject *parent=nullptr, const StylePointer &style=nullptr)
Constructs a Screen.
Definition: tgscreen.cpp:13
Tg::Screen::scheduleRedraw
void scheduleRedraw(const RedrawType type, const Widget *widget)
Schedules a redraw of widget using type.
Definition: tgscreen.cpp:79
Tg::Screen::canDragWidgets
bool canDragWidgets
When true, widgets can be dragged around on the Screen using a mouse.
Definition: tgscreen.h:51
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