CustomGrip.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #include "CustomGrip.h"
  2. #include <QMouseEvent>
  3. #include <QSizeGrip>
  4. #include <QDebug>
  5. CustomGrip::CustomGrip(QWidget *parent, Qt::Edge positon, bool disableColor) : QWidget(parent)
  6. {
  7. wi = new Widgets(this);
  8. switch (positon) {
  9. // SHOW TOP GRIP
  10. case Qt::TopEdge: {
  11. this->wi->top(this);
  12. setGeometry(0, 0, qobject_cast<QWidget *>(this->parent())->width(), 10);
  13. setMaximumHeight(10);
  14. // GRIPS
  15. QSizeGrip *topLeft = new QSizeGrip(this->wi->m_topLeft);
  16. QSizeGrip *topRight = new QSizeGrip(this->wi->m_topRight);
  17. if (disableColor) {
  18. this->wi->m_topLeft->setStyleSheet("background: transparent");
  19. this->wi->m_topRight->setStyleSheet("background: transparent");
  20. this->wi->m_top->setStyleSheet("background: transparent");
  21. }
  22. wi->m_top->installEventFilter(this);
  23. } break;
  24. // SHOW BOTTOM GRIP
  25. case Qt::BottomEdge: {
  26. this->wi->bottom(this);
  27. setGeometry(0, qobject_cast<QWidget *>(this->parent())->height() - 10,
  28. qobject_cast<QWidget *>(this->parent())->width(), 10);
  29. setMaximumHeight(10);
  30. // GRIPS
  31. QSizeGrip *bottom_left = new QSizeGrip(this->wi->m_bottomLeft);
  32. QSizeGrip *bottom_right = new QSizeGrip(this->wi->m_bottomRight);
  33. if (disableColor) {
  34. this->wi->m_bottomLeft->setStyleSheet("background: transparent");
  35. this->wi->m_bottomRight->setStyleSheet("background: transparent");
  36. this->wi->m_bottom->setStyleSheet("background: transparent");
  37. }
  38. wi->m_bottom->installEventFilter(this);
  39. } break;
  40. // SHOW LEFT GRIP
  41. case Qt::LeftEdge: {
  42. this->wi->left(this);
  43. setGeometry(0, 10, 10, qobject_cast<QWidget *>(this->parent())->height());
  44. setMaximumWidth(10);
  45. if (disableColor) {
  46. this->wi->m_leftgrip->setStyleSheet("background: transparent");
  47. }
  48. wi->m_leftgrip->installEventFilter(this);
  49. } break;
  50. case Qt::RightEdge: {
  51. this->wi->right(this);
  52. setGeometry(qobject_cast<QWidget *>(this->parent())->width() - 10, 10, 10,
  53. qobject_cast<QWidget *>(this->parent())->height());
  54. setMaximumWidth(10);
  55. if (disableColor) {
  56. this->wi->m_rightgrip->setStyleSheet("background: transparent");
  57. }
  58. wi->m_rightgrip->installEventFilter(this);
  59. } break;
  60. default:
  61. break;
  62. }
  63. }
  64. void CustomGrip::mouseReleaseEvent(QMouseEvent *event)
  65. {
  66. Q_UNUSED(event)
  67. mousePos = QPoint();
  68. }
  69. void CustomGrip::resizeEvent(QResizeEvent *event)
  70. {
  71. Q_UNUSED(event)
  72. if (this->wi->property("container_top").isValid()) {
  73. this->wi->m_containerTop->setGeometry(0, 0, this->width(), 10);
  74. } else if (this->wi->property("container_bottom").isValid()) {
  75. this->wi->m_containerBottom->setGeometry(0, 0, this->width(), 10);
  76. } else if (this->wi->property("leftgrip").isValid()) {
  77. this->wi->m_leftgrip->setGeometry(0, 0, 10, this->height() - 20);
  78. } else if (this->wi->property("rightgrip").isValid()) {
  79. this->wi->m_rightgrip->setGeometry(0, 0, 10, this->height() - 20);
  80. }
  81. }
  82. bool CustomGrip::eventFilter(QObject *watched, QEvent *event)
  83. {
  84. if (watched != nullptr && event->type() == QEvent::MouseMove) {
  85. QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
  86. if (watched == this->wi->m_top) { // RESIZE TOP
  87. QPoint delta = mouseEvent->pos();
  88. QWidget *parent = qobject_cast<QWidget *>(this->parent());
  89. int height = std::max(parent->minimumHeight(), parent->height() - delta.y());
  90. QRect geo = parent->geometry();
  91. geo.setTop(geo.bottom() - height);
  92. parent->setGeometry(geo);
  93. event->accept();
  94. } else if (watched == this->wi->m_bottom) { // RESIZE BOTTOM
  95. QPoint delta = mouseEvent->pos();
  96. QWidget *parent = qobject_cast<QWidget *>(this->parent());
  97. int height = std::max(parent->minimumHeight(), parent->height() + delta.y());
  98. parent->resize(parent->width(), height);
  99. event->accept();
  100. } else if (watched == this->wi->m_leftgrip) { // RESIZE LEFT
  101. QPoint delta = mouseEvent->pos();
  102. QWidget *parent = qobject_cast<QWidget *>(this->parent());
  103. int width = std::max(parent->minimumWidth(), parent->width() - delta.x());
  104. QRect geo = parent->geometry();
  105. geo.setLeft(geo.right() - width);
  106. parent->setGeometry(geo);
  107. event->accept();
  108. } else if (watched == this->wi->m_rightgrip) { // RESIZE RIGHT
  109. QPoint delta = mouseEvent->pos();
  110. QWidget *parent = qobject_cast<QWidget *>(this->parent());
  111. int width = std::max(parent->minimumWidth(), parent->width() + delta.x());
  112. parent->resize(width, parent->height());
  113. event->accept();
  114. }
  115. }
  116. return QWidget::eventFilter(watched, event);
  117. }
  118. ////////////////////////////////////////////////////////////////////////////////////////////////
  119. Widgets::Widgets(QObject *parent) : QObject(parent)
  120. {
  121. m_containerTop = nullptr;
  122. m_topLayout = nullptr;
  123. m_topLeft = nullptr;
  124. m_top = nullptr;
  125. m_topRight = nullptr;
  126. m_containerBottom = nullptr;
  127. m_bottomLayout = nullptr;
  128. m_bottomLeft = nullptr;
  129. m_bottom = nullptr;
  130. m_bottomRight = nullptr;
  131. m_leftgrip = nullptr;
  132. m_rightgrip = nullptr;
  133. }
  134. void Widgets::top(QWidget *form)
  135. {
  136. if (form->objectName().isEmpty()) {
  137. form->setObjectName("Form");
  138. }
  139. this->setProperty("container_top", "container_top");
  140. this->m_containerTop = new QFrame(form);
  141. this->m_containerTop->setObjectName("container-top");
  142. this->m_containerTop->setGeometry(QRect(0, 0, 500, 10));
  143. this->m_containerTop->setMinimumSize(QSize(0, 10));
  144. this->m_containerTop->setMaximumSize(QSize(16777215, 10));
  145. this->m_containerTop->setFrameShape(QFrame::NoFrame);
  146. this->m_containerTop->setFrameShadow(QFrame::Raised);
  147. this->m_topLayout = new QHBoxLayout(this->m_containerTop);
  148. this->m_topLayout->setSpacing(0);
  149. this->m_topLayout->setObjectName("top-layout");
  150. this->m_topLayout->setContentsMargins(0, 0, 0, 0);
  151. this->m_topLeft = new QFrame(this->m_containerTop);
  152. this->m_topLeft->setObjectName("top-left");
  153. this->m_topLeft->setMinimumSize(QSize(10, 10));
  154. this->m_topLeft->setMaximumSize(QSize(10, 10));
  155. this->m_topLeft->setCursor(QCursor(Qt::SizeFDiagCursor));
  156. this->m_topLeft->setStyleSheet("background-color: rgb(33, 37, 43);");
  157. this->m_topLeft->setFrameShape(QFrame::NoFrame);
  158. this->m_topLeft->setFrameShadow(QFrame::Raised);
  159. this->m_topLayout->addWidget(this->m_topLeft);
  160. this->m_top = new QFrame(this->m_containerTop);
  161. this->m_top->setObjectName("top");
  162. this->m_top->setCursor(QCursor(Qt::SizeVerCursor));
  163. this->m_top->setStyleSheet("background-color: rgb(85, 255, 255);");
  164. this->m_top->setFrameShape(QFrame::NoFrame);
  165. this->m_top->setFrameShadow(QFrame::Raised);
  166. this->m_topLayout->addWidget(this->m_top);
  167. this->m_topRight = new QFrame(this->m_containerTop);
  168. this->m_topRight->setObjectName("top_right");
  169. this->m_topRight->setMinimumSize(QSize(10, 10));
  170. this->m_topRight->setMaximumSize(QSize(10, 10));
  171. this->m_topRight->setCursor(QCursor(Qt::SizeBDiagCursor));
  172. this->m_topRight->setStyleSheet("background-color: rgb(33, 37, 43);");
  173. this->m_topRight->setFrameShape(QFrame::NoFrame);
  174. this->m_topRight->setFrameShadow(QFrame::Raised);
  175. this->m_topLayout->addWidget(this->m_topRight);
  176. }
  177. void Widgets::bottom(QWidget *form)
  178. {
  179. if (form->objectName().isEmpty()) {
  180. form->setObjectName("Form");
  181. }
  182. this->setProperty("container_bottom", "container_bottom");
  183. this->m_containerBottom = new QFrame(form);
  184. this->m_containerBottom->setObjectName("container_bottom");
  185. this->m_containerBottom->setGeometry(QRect(0, 0, 500, 10));
  186. this->m_containerBottom->setMinimumSize(QSize(0, 10));
  187. this->m_containerBottom->setMaximumSize(QSize(16777215, 10));
  188. this->m_containerBottom->setFrameShape(QFrame::NoFrame);
  189. this->m_containerBottom->setFrameShadow(QFrame::Raised);
  190. this->m_bottomLayout = new QHBoxLayout(this->m_containerBottom);
  191. this->m_bottomLayout->setSpacing(0);
  192. this->m_bottomLayout->setObjectName("bottom_layout");
  193. this->m_bottomLayout->setContentsMargins(0, 0, 0, 0);
  194. this->m_bottomLeft = new QFrame(this->m_containerBottom);
  195. this->m_bottomLeft->setObjectName("bottom_left");
  196. this->m_bottomLeft->setMinimumSize(QSize(10, 10));
  197. this->m_bottomLeft->setMaximumSize(QSize(10, 10));
  198. this->m_bottomLeft->setCursor(QCursor(Qt::SizeBDiagCursor));
  199. this->m_bottomLeft->setStyleSheet("background-color: rgb(33, 37, 43);");
  200. this->m_bottomLeft->setFrameShape(QFrame::NoFrame);
  201. this->m_bottomLeft->setFrameShadow(QFrame::Raised);
  202. this->m_bottomLayout->addWidget(this->m_bottomLeft);
  203. this->m_bottom = new QFrame(this->m_containerBottom);
  204. this->m_bottom->setObjectName("bottom");
  205. this->m_bottom->setCursor(QCursor(Qt::SizeVerCursor));
  206. this->m_bottom->setStyleSheet("background-color: rgb(85, 170, 0);");
  207. this->m_bottom->setFrameShape(QFrame::NoFrame);
  208. this->m_bottom->setFrameShadow(QFrame::Raised);
  209. this->m_bottomLayout->addWidget(this->m_bottom);
  210. this->m_bottomRight = new QFrame(this->m_containerBottom);
  211. this->m_bottomRight->setObjectName("bottom_right");
  212. this->m_bottomRight->setMinimumSize(QSize(10, 10));
  213. this->m_bottomRight->setMaximumSize(QSize(10, 10));
  214. this->m_bottomRight->setCursor(QCursor(Qt::SizeFDiagCursor));
  215. this->m_bottomRight->setStyleSheet("background-color: rgb(33, 37, 43);");
  216. this->m_bottomRight->setFrameShape(QFrame::NoFrame);
  217. this->m_bottomRight->setFrameShadow(QFrame::Raised);
  218. this->m_bottomLayout->addWidget(this->m_bottomRight);
  219. }
  220. void Widgets::left(QWidget *form)
  221. {
  222. if (form->objectName().isEmpty()) {
  223. form->setObjectName("Form");
  224. }
  225. this->setProperty("leftgrip", "leftgrip");
  226. this->m_leftgrip = new QFrame(form);
  227. this->m_leftgrip->setObjectName("left");
  228. this->m_leftgrip->setGeometry(QRect(0, 10, 10, 480));
  229. this->m_leftgrip->setMinimumSize(QSize(10, 0));
  230. this->m_leftgrip->setCursor(QCursor(Qt::SizeHorCursor));
  231. this->m_leftgrip->setStyleSheet("background-color: rgb(255, 121, 198);");
  232. this->m_leftgrip->setFrameShape(QFrame::NoFrame);
  233. this->m_leftgrip->setFrameShadow(QFrame::Raised);
  234. }
  235. void Widgets::right(QWidget *form)
  236. {
  237. if (form->objectName().isEmpty()) {
  238. form->setObjectName("Form");
  239. }
  240. this->setProperty("rightgrip", "rightgrip");
  241. form->resize(500, 500);
  242. this->m_rightgrip = new QFrame(form);
  243. this->m_rightgrip->setObjectName("right");
  244. this->m_rightgrip->setGeometry(QRect(0, 0, 10, 500));
  245. this->m_rightgrip->setMinimumSize(QSize(10, 0));
  246. this->m_rightgrip->setCursor(QCursor(Qt::SizeHorCursor));
  247. this->m_rightgrip->setStyleSheet("background-color: rgb(255, 0, 127);");
  248. this->m_rightgrip->setFrameShape(QFrame::NoFrame);
  249. this->m_rightgrip->setFrameShadow(QFrame::Raised);
  250. }