qt.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. //$(moc) -i -o qt.moc qt.hpp
  2. /*
  3. Qt requires moc in order to bind callbacks, which causes many complications.
  4. First, moc is not C++11-aware. Thus, all of the "public slots:" functions must
  5. be declared using C++98 syntax.
  6. Second, multiple inheritance with QObject (eg pWindow : QObject, pObject)
  7. seems to cause heap corruption. As such, we need to have separate classes for
  8. inheriting from QObject, which are defined in this file.
  9. Third, moc preprocessor output is required for every Q_OBJECT class. So to
  10. avoid needing to generate several .moc files, all QObject classes are placed
  11. inside this one file instead.
  12. */
  13. #if !defined(HIRO_QT)
  14. #include "../components.hpp"
  15. #endif
  16. namespace hiro {
  17. #if defined(Hiro_Timer)
  18. struct QtTimer : public QTimer {
  19. Q_OBJECT
  20. public:
  21. QtTimer(pTimer& p) : p(p) {}
  22. pTimer& p;
  23. public slots:
  24. void onActivate();
  25. };
  26. #endif
  27. #if defined(Hiro_Window)
  28. struct QtWindow : public QWidget {
  29. QtWindow(pWindow& p) : p(p) {}
  30. auto closeEvent(QCloseEvent*) -> void;
  31. auto dragEnterEvent(QDragEnterEvent*) -> void;
  32. auto dropEvent(QDropEvent*) -> void;
  33. auto keyPressEvent(QKeyEvent*) -> void;
  34. auto keyReleaseEvent(QKeyEvent*) -> void;
  35. auto moveEvent(QMoveEvent*) -> void;
  36. auto resizeEvent(QResizeEvent*) -> void;
  37. auto sizeHint() const -> QSize;
  38. pWindow& p;
  39. };
  40. #endif
  41. #if defined(Hiro_MenuItem)
  42. struct QtMenuItem : public QAction {
  43. Q_OBJECT
  44. public:
  45. QtMenuItem(pMenuItem& p) : QAction(nullptr), p(p) {}
  46. pMenuItem& p;
  47. public slots:
  48. void onActivate();
  49. };
  50. #endif
  51. #if defined(Hiro_MenuCheckItem)
  52. struct QtMenuCheckItem : public QAction {
  53. Q_OBJECT
  54. public:
  55. QtMenuCheckItem(pMenuCheckItem& p) : QAction(nullptr), p(p) {}
  56. pMenuCheckItem& p;
  57. public slots:
  58. void onToggle();
  59. };
  60. #endif
  61. #if defined(Hiro_MenuRadioItem)
  62. struct QtMenuRadioItem : public QAction {
  63. Q_OBJECT
  64. public:
  65. QtMenuRadioItem(pMenuRadioItem& p) : QAction(nullptr), p(p) {}
  66. pMenuRadioItem& p;
  67. public slots:
  68. void onActivate();
  69. };
  70. #endif
  71. #if defined(Hiro_Button)
  72. struct QtButton : public QToolButton {
  73. Q_OBJECT
  74. public:
  75. QtButton(pButton& p) : p(p) {}
  76. pButton& p;
  77. public slots:
  78. void onActivate();
  79. };
  80. #endif
  81. #if defined(Hiro_Canvas)
  82. struct QtCanvas : public QWidget {
  83. Q_OBJECT
  84. public:
  85. QtCanvas(pCanvas& p) : p(p) {}
  86. auto dragEnterEvent(QDragEnterEvent*) -> void;
  87. auto dropEvent(QDropEvent*) -> void;
  88. auto leaveEvent(QEvent*) -> void;
  89. auto mouseMoveEvent(QMouseEvent*) -> void;
  90. auto mousePressEvent(QMouseEvent*) -> void;
  91. auto mouseReleaseEvent(QMouseEvent*) -> void;
  92. auto paintEvent(QPaintEvent*) -> void;
  93. pCanvas& p;
  94. };
  95. #endif
  96. #if defined(Hiro_CheckButton)
  97. struct QtCheckButton : public QToolButton {
  98. Q_OBJECT
  99. public:
  100. QtCheckButton(pCheckButton& p) : p(p) {}
  101. pCheckButton& p;
  102. public slots:
  103. void onToggle(bool checked);
  104. };
  105. #endif
  106. #if defined(Hiro_CheckLabel)
  107. struct QtCheckLabel : public QCheckBox {
  108. Q_OBJECT
  109. public:
  110. QtCheckLabel(pCheckLabel& p) : p(p) {}
  111. pCheckLabel& p;
  112. public slots:
  113. void onToggle();
  114. };
  115. #endif
  116. #if defined(Hiro_ComboButton)
  117. struct QtComboButton : public QComboBox {
  118. Q_OBJECT
  119. public:
  120. QtComboButton(pComboButton& p) : p(p) {}
  121. pComboButton& p;
  122. public slots:
  123. void onChange(int offset);
  124. };
  125. #endif
  126. #if defined(Hiro_HexEdit)
  127. struct QtHexEdit : public QTextEdit {
  128. Q_OBJECT
  129. public:
  130. QtHexEdit(pHexEdit& p) : p(p) {}
  131. auto keyPressEvent(QKeyEvent*) -> void;
  132. auto keyPressEventAcknowledge(QKeyEvent*) -> void;
  133. auto wheelEvent(QWheelEvent*) -> void;
  134. pHexEdit& p;
  135. };
  136. struct QtHexEditScrollBar : public QScrollBar {
  137. Q_OBJECT
  138. public:
  139. QtHexEditScrollBar(pHexEdit& p) : p(p) {}
  140. auto event(QEvent*) -> bool;
  141. pHexEdit& p;
  142. public slots:
  143. void onScroll();
  144. };
  145. #endif
  146. #if defined(Hiro_HorizontalScrollBar)
  147. struct QtHorizontalScrollBar : public QScrollBar {
  148. Q_OBJECT
  149. public:
  150. QtHorizontalScrollBar(pHorizontalScrollBar& p) : QScrollBar(Qt::Horizontal), p(p) {}
  151. pHorizontalScrollBar& p;
  152. public slots:
  153. void onChange();
  154. };
  155. #endif
  156. #if defined(Hiro_HorizontalSlider)
  157. struct QtHorizontalSlider : public QSlider {
  158. Q_OBJECT
  159. public:
  160. QtHorizontalSlider(pHorizontalSlider& p) : QSlider(Qt::Horizontal), p(p) {}
  161. pHorizontalSlider& p;
  162. public slots:
  163. void onChange();
  164. };
  165. #endif
  166. #if defined(Hiro_Label)
  167. struct QtLabel : public QWidget {
  168. Q_OBJECT
  169. public:
  170. QtLabel(pLabel& p) : p(p) {}
  171. auto mousePressEvent(QMouseEvent*) -> void;
  172. auto mouseReleaseEvent(QMouseEvent*) -> void;
  173. auto paintEvent(QPaintEvent*) -> void;
  174. pLabel& p;
  175. };
  176. #endif
  177. #if defined(Hiro_LineEdit)
  178. struct QtLineEdit : public QLineEdit {
  179. Q_OBJECT
  180. public:
  181. QtLineEdit(pLineEdit& p) : p(p) {}
  182. pLineEdit& p;
  183. public slots:
  184. void onActivate();
  185. void onChange();
  186. };
  187. #endif
  188. #if defined(Hiro_RadioLabel)
  189. struct QtRadioLabel : public QRadioButton {
  190. Q_OBJECT
  191. public:
  192. QtRadioLabel(pRadioLabel& p) : p(p) {}
  193. pRadioLabel& p;
  194. public slots:
  195. void onActivate();
  196. };
  197. #endif
  198. #if defined(Hiro_RadioButton)
  199. struct QtRadioButton : public QToolButton {
  200. Q_OBJECT
  201. public:
  202. QtRadioButton(pRadioButton& p) : p(p) {}
  203. pRadioButton& p;
  204. public slots:
  205. void onActivate();
  206. };
  207. #endif
  208. #if defined(Hiro_TabFrame)
  209. struct QtTabFrame : public QTabWidget {
  210. Q_OBJECT
  211. public:
  212. QtTabFrame(pTabFrame& p) : p(p) {}
  213. pTabFrame& p;
  214. auto showEvent(QShowEvent*) -> void override;
  215. public slots:
  216. void onChange(int selection);
  217. };
  218. #endif
  219. #if defined(Hiro_TableView)
  220. struct QtTableView : public QTreeWidget {
  221. Q_OBJECT
  222. public:
  223. QtTableView(pTableView& p) : p(p) {}
  224. auto mousePressEvent(QMouseEvent*) -> void override;
  225. auto resizeEvent(QResizeEvent*) -> void override;
  226. auto showEvent(QShowEvent*) -> void override;
  227. pTableView& p;
  228. public slots:
  229. void onActivate(QTreeWidgetItem* item, int column);
  230. void onChange();
  231. void onContext();
  232. void onSort(int column);
  233. void onToggle(QTreeWidgetItem* item, int column);
  234. };
  235. struct QtTableViewDelegate : public QStyledItemDelegate {
  236. QtTableViewDelegate(pTableView& p);
  237. auto paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const -> void;
  238. pTableView& p;
  239. };
  240. #endif
  241. #if defined(Hiro_TextEdit)
  242. struct QtTextEdit : public QTextEdit {
  243. Q_OBJECT
  244. public:
  245. QtTextEdit(pTextEdit& p) : p(p) {}
  246. pTextEdit& p;
  247. public slots:
  248. void onChange();
  249. };
  250. #endif
  251. #if defined(Hiro_VerticalScrollBar)
  252. struct QtVerticalScrollBar : public QScrollBar {
  253. Q_OBJECT
  254. public:
  255. QtVerticalScrollBar(pVerticalScrollBar& p) : QScrollBar(Qt::Vertical), p(p) {}
  256. pVerticalScrollBar& p;
  257. public slots:
  258. void onChange();
  259. };
  260. #endif
  261. #if defined(Hiro_VerticalSlider)
  262. struct QtVerticalSlider : public QSlider {
  263. Q_OBJECT
  264. public:
  265. QtVerticalSlider(pVerticalSlider& p) : QSlider(Qt::Vertical), p(p) {}
  266. pVerticalSlider& p;
  267. public slots:
  268. void onChange();
  269. };
  270. #endif
  271. #if defined(Hiro_Viewport)
  272. struct QtViewport : public QWidget {
  273. Q_OBJECT
  274. public:
  275. QtViewport(pViewport& p) : p(p) {}
  276. auto dragEnterEvent(QDragEnterEvent*) -> void;
  277. auto dropEvent(QDropEvent*) -> void;
  278. auto leaveEvent(QEvent*) -> void;
  279. auto mouseMoveEvent(QMouseEvent*) -> void;
  280. auto mousePressEvent(QMouseEvent*) -> void;
  281. auto mouseReleaseEvent(QMouseEvent*) -> void;
  282. pViewport& p;
  283. };
  284. #endif
  285. }