util.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #ifndef UTIL_HPP
  2. #define UTIL_HPP
  3. #include <QObject>
  4. #include <QMetaMethod>
  5. #include <QWidget>
  6. #include <QEvent>
  7. #include <QWebEnginePage>
  8. #include <cstdlib>
  9. #include <cmath>
  10. #include "qtbinding.h"
  11. #define BaseSize 16.0
  12. #define BaseScreen 768.0
  13. QtString WrapString(QString str);
  14. QString UnwrapString(QtString str);
  15. QString EncodeBase64(QString str);
  16. QString DecodeBase64(QString str);
  17. int Get1remPixels();
  18. QSize GetSizeFromRelative(QSize size_rem);
  19. void MoveToScreenCenter(QWidget* widget);
  20. #define CALLBACK_BLOCKED "qtbindingCallbackBlocked"
  21. typedef void (*callback_t)(size_t);
  22. Q_DECLARE_METATYPE(callback_t);
  23. class CallbackObject;
  24. struct ConnectionHandle {
  25. QMetaObject::Connection
  26. conn;
  27. CallbackObject*
  28. cb_obj;
  29. };
  30. QMetaObject::Connection QtDynamicConnect (
  31. QObject* emitter , const QString& signalName,
  32. QObject* receiver, const QString& slotName
  33. );
  34. class CallbackExecutor final: public QObject {
  35. Q_OBJECT
  36. public:
  37. CallbackExecutor(QWidget *parent = nullptr): QObject(parent) {
  38. QObject::connect (
  39. this, &CallbackExecutor::QueueCallback,
  40. this, &CallbackExecutor::__InvokeCallback,
  41. Qt::QueuedConnection
  42. );
  43. };
  44. virtual ~CallbackExecutor() {};
  45. signals:
  46. void QueueCallback(callback_t cb, size_t payload);
  47. private slots:
  48. void __InvokeCallback(callback_t cb, size_t payload) {
  49. cb(payload);
  50. };
  51. };
  52. class CallbackObject final: public QObject {
  53. Q_OBJECT
  54. public:
  55. callback_t cb;
  56. size_t payload;
  57. CallbackObject(QObject* parent, callback_t cb, size_t payload): QObject(parent) {
  58. this->cb = cb;
  59. this->payload = payload;
  60. };
  61. virtual ~CallbackObject() {};
  62. public slots:
  63. void slot() {
  64. QVariant v = parent()->property(CALLBACK_BLOCKED);
  65. if (v.isValid() && v.toBool() == true) {
  66. return;
  67. };
  68. cb(payload);
  69. };
  70. };
  71. class EventListener final: public QObject {
  72. Q_OBJECT
  73. public:
  74. QEvent::Type accept_type;
  75. bool prevent_default;
  76. callback_t cb;
  77. size_t payload;
  78. QObject* current_object;
  79. QEvent* current_event;
  80. EventListener(QEvent::Type t, bool prevent, callback_t cb, size_t payload): QObject(nullptr) {
  81. this->accept_type = t;
  82. this->prevent_default = prevent;
  83. this->cb = cb;
  84. this->payload = payload;
  85. }
  86. bool eventFilter(QObject* obj, QEvent *event) override {
  87. if (event->type() == accept_type) {
  88. current_object = obj;
  89. current_event = event;
  90. cb(payload);
  91. current_event = nullptr;
  92. current_object = nullptr;
  93. if (prevent_default) {
  94. event->ignore();
  95. return true; // stops propagation
  96. } else {
  97. return false;
  98. }
  99. } else {
  100. return false;
  101. }
  102. }
  103. };
  104. class LinkDelegatedPage: public QWebEnginePage {
  105. public:
  106. LinkDelegatedPage(QObject* parent): QWebEnginePage(parent) {}
  107. bool acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame) override {
  108. if (type == NavigationTypeLinkClicked) {
  109. parent()->setProperty("qtbindingClickedLinkUrl", url.toString());
  110. emit loadFinished(false);
  111. return false;
  112. } else {
  113. return QWebEnginePage::acceptNavigationRequest(url, type, isMainFrame);
  114. }
  115. }
  116. };
  117. #endif // UTIL_HPP