ctkSizeGrip.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // CTK includes
  15. #include "ctkSizeGrip.h"
  16. #include <QApplication>
  17. #include <QDebug>
  18. #include <QEvent>
  19. #include <QLayout>
  20. #include <QMouseEvent>
  21. #include <QPaintEvent>
  22. #include <QStyle>
  23. #include <QStyleOption>
  24. #include <QPainter>
  25. //------------------------------------------------------------------------------
  26. static inline bool hasHeightForWidth(QWidget *widget)
  27. {
  28. if (!widget)
  29. {
  30. return false;
  31. }
  32. if (QLayout *layout = widget->layout())
  33. {
  34. return layout->hasHeightForWidth();
  35. }
  36. return widget->sizePolicy().hasHeightForWidth();
  37. }
  38. //------------------------------------------------------------------------------
  39. class ctkSizeGripPrivate
  40. {
  41. Q_DECLARE_PUBLIC(ctkSizeGrip)
  42. protected:
  43. ctkSizeGrip* const q_ptr;
  44. public:
  45. ctkSizeGripPrivate(ctkSizeGrip& object);
  46. void init();
  47. Qt::Orientations Orientations;
  48. QWidget* WidgetToResize;
  49. QRect WidgetGeom;
  50. QSize WidgetSizeHint;
  51. QSize WidgetMaxSize;
  52. QPoint StartPos;
  53. bool Hover;
  54. bool Pressed;
  55. bool Resize;
  56. };
  57. //------------------------------------------------------------------------------
  58. ctkSizeGripPrivate::ctkSizeGripPrivate(ctkSizeGrip& object)
  59. : q_ptr(&object)
  60. , Orientations(Qt::Horizontal | Qt::Vertical)
  61. , WidgetToResize(0)
  62. , Hover(false)
  63. , Pressed(false)
  64. , Resize(false)
  65. {
  66. }
  67. //------------------------------------------------------------------------------
  68. void ctkSizeGripPrivate::init()
  69. {
  70. Q_Q(ctkSizeGrip);
  71. q->setOrientations(Qt::Horizontal | Qt::Vertical);
  72. }
  73. //------------------------------------------------------------------------------
  74. ctkSizeGrip::ctkSizeGrip(QWidget* parent)
  75. : QWidget(parent)
  76. , d_ptr(new ctkSizeGripPrivate(*this))
  77. {
  78. Q_D(ctkSizeGrip);
  79. d->WidgetToResize = parent;
  80. d->init();
  81. }
  82. //------------------------------------------------------------------------------
  83. ctkSizeGrip::ctkSizeGrip(QWidget* target, QWidget* parent)
  84. : QWidget(parent)
  85. , d_ptr(new ctkSizeGripPrivate(*this))
  86. {
  87. Q_D(ctkSizeGrip);
  88. d->WidgetToResize = target;
  89. d->init();
  90. }
  91. //------------------------------------------------------------------------------
  92. ctkSizeGrip::~ctkSizeGrip()
  93. {
  94. }
  95. //------------------------------------------------------------------------------
  96. void ctkSizeGrip::setOrientations(Qt::Orientations newOrientations)
  97. {
  98. Q_D(ctkSizeGrip);
  99. d->Orientations = newOrientations;
  100. QCursor newCursor;
  101. QSizePolicy newSizePolicy;
  102. switch(d->Orientations)
  103. {
  104. case Qt::Horizontal:
  105. newCursor = Qt::SizeHorCursor;
  106. newSizePolicy = QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
  107. break;
  108. case Qt::Vertical:
  109. newCursor = Qt::SizeVerCursor;
  110. newSizePolicy = QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
  111. break;
  112. default:
  113. newCursor = this->isLeftToRight() ?
  114. Qt::SizeFDiagCursor : Qt::SizeBDiagCursor;
  115. newSizePolicy = QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  116. break;
  117. };
  118. this->setCursor(newCursor);
  119. this->setSizePolicy(newSizePolicy);
  120. this->updateGeometry(); // sizeHint might change
  121. }
  122. //------------------------------------------------------------------------------
  123. Qt::Orientations ctkSizeGrip::orientations()const
  124. {
  125. Q_D(const ctkSizeGrip);
  126. return d->Orientations;
  127. }
  128. //------------------------------------------------------------------------------
  129. void ctkSizeGrip::setWidgetToResize(QWidget* target)
  130. {
  131. Q_D(ctkSizeGrip);
  132. d->WidgetToResize = target;
  133. d->WidgetSizeHint = QSize();
  134. }
  135. //------------------------------------------------------------------------------
  136. QWidget* ctkSizeGrip::widgetToResize()const
  137. {
  138. Q_D(const ctkSizeGrip);
  139. return d->WidgetToResize;
  140. }
  141. //------------------------------------------------------------------------------
  142. void ctkSizeGrip::setResizeWidget(bool resize)
  143. {
  144. Q_D(ctkSizeGrip);
  145. d->Resize = resize;
  146. this->setWidgetSizeHint(d->WidgetSizeHint);
  147. }
  148. //------------------------------------------------------------------------------
  149. bool ctkSizeGrip::resizeWidget()const
  150. {
  151. Q_D(const ctkSizeGrip);
  152. return d->Resize;
  153. }
  154. //------------------------------------------------------------------------------
  155. void ctkSizeGrip::setWidgetSizeHint(QSize sizeHint)
  156. {
  157. Q_D(ctkSizeGrip);
  158. if (d->Resize && d->WidgetToResize)
  159. {
  160. QSize newSize = d->WidgetToResize->size();
  161. if (sizeHint.width())
  162. {
  163. newSize.setWidth(sizeHint.width());
  164. }
  165. if (sizeHint.height())
  166. {
  167. newSize.setHeight(sizeHint.height());
  168. }
  169. d->WidgetToResize->resize(newSize);
  170. }
  171. if (d->WidgetSizeHint != sizeHint)
  172. {
  173. d->WidgetSizeHint = sizeHint;
  174. emit widgetSizeHintChanged(d->WidgetSizeHint);
  175. }
  176. }
  177. //------------------------------------------------------------------------------
  178. QSize ctkSizeGrip::widgetSizeHint()const
  179. {
  180. Q_D(const ctkSizeGrip);
  181. return d->WidgetSizeHint;
  182. }
  183. //------------------------------------------------------------------------------
  184. QSize ctkSizeGrip::sizeHint() const
  185. {
  186. Q_D(const ctkSizeGrip);
  187. QSize minSize;
  188. QStyle::ContentsType contents;
  189. switch (d->Orientations)
  190. {
  191. case Qt::Horizontal:
  192. case Qt::Vertical:
  193. {
  194. contents = QStyle::CT_Splitter;
  195. int splitterWidth = this->style()->pixelMetric(QStyle::PM_SplitterWidth, 0, this);
  196. minSize = QSize(splitterWidth, splitterWidth);
  197. break;
  198. }
  199. default:
  200. contents = QStyle::CT_SizeGrip;
  201. minSize = QSize(13, 13);
  202. break;
  203. };
  204. QStyleOption opt(0);
  205. opt.init(this);
  206. return (this->style()->sizeFromContents(contents, &opt, minSize, this).
  207. expandedTo(QApplication::globalStrut()));
  208. }
  209. //------------------------------------------------------------------------------
  210. void ctkSizeGrip::paintEvent(QPaintEvent *event)
  211. {
  212. Q_UNUSED(event);
  213. Q_D(ctkSizeGrip);
  214. QPainter painter(this);
  215. switch (d->Orientations)
  216. {
  217. case Qt::Horizontal:
  218. case Qt::Vertical:
  219. {
  220. QStyleOption opt(0);
  221. opt.rect = rect();
  222. opt.palette = palette();
  223. opt.state = (d->Orientations == Qt::Horizontal) ?
  224. QStyle::State_Horizontal : QStyle::State_None;
  225. if (d->Hover)
  226. {
  227. opt.state |= QStyle::State_MouseOver;
  228. }
  229. if (d->Pressed)
  230. {
  231. opt.state |= QStyle::State_Sunken;
  232. }
  233. if (isEnabled())
  234. {
  235. opt.state |= QStyle::State_Enabled;
  236. }
  237. this->style()->drawControl(QStyle::CE_Splitter, &opt, &painter, this);
  238. break;
  239. }
  240. default:
  241. {
  242. QStyleOptionSizeGrip opt;
  243. opt.init(this);
  244. opt.corner = this->isLeftToRight() ? Qt::BottomRightCorner : Qt::BottomLeftCorner;
  245. style()->drawControl(QStyle::CE_SizeGrip, &opt, &painter, this);
  246. break;
  247. }
  248. };
  249. }
  250. //------------------------------------------------------------------------------
  251. bool ctkSizeGrip::event(QEvent *event)
  252. {
  253. Q_D(ctkSizeGrip);
  254. switch(event->type())
  255. {
  256. case QEvent::HoverEnter:
  257. d->Hover = true;
  258. update();
  259. break;
  260. case QEvent::HoverLeave:
  261. d->Hover = false;
  262. update();
  263. break;
  264. default:
  265. break;
  266. }
  267. return this->Superclass::event(event);
  268. }
  269. //------------------------------------------------------------------------------
  270. void ctkSizeGrip::mousePressEvent(QMouseEvent * e)
  271. {
  272. if (e->button() != Qt::LeftButton)
  273. {
  274. QWidget::mousePressEvent(e);
  275. return;
  276. }
  277. Q_D(ctkSizeGrip);
  278. d->StartPos = e->globalPos();
  279. d->Pressed = true;
  280. d->WidgetGeom = d->WidgetToResize->geometry();
  281. d->WidgetSizeHint = QSize();
  282. d->WidgetMaxSize = d->WidgetToResize->maximumSize();
  283. }
  284. //------------------------------------------------------------------------------
  285. void ctkSizeGrip::mouseMoveEvent(QMouseEvent * e)
  286. {
  287. if (e->buttons() != Qt::LeftButton)
  288. {
  289. this->Superclass::mouseMoveEvent(e);
  290. return;
  291. }
  292. Q_D(ctkSizeGrip);
  293. if (!d->Pressed || d->WidgetToResize->testAttribute(Qt::WA_WState_ConfigPending))
  294. {
  295. return;
  296. }
  297. QPoint newPos(e->globalPos());
  298. QSize offset(newPos.x() - d->StartPos.x(), newPos.y() - d->StartPos.y());
  299. QSize widgetSizeHint = d->WidgetGeom.size();
  300. if (d->Orientations & Qt::Vertical)
  301. {
  302. widgetSizeHint.rheight() = d->WidgetGeom.height() + offset.height();
  303. }
  304. if (d->Orientations & Qt::Horizontal)
  305. {
  306. widgetSizeHint.rwidth() = d->WidgetGeom.width() + offset.width() * (this->isLeftToRight() ? 1 : -1);
  307. }
  308. widgetSizeHint = widgetSizeHint.boundedTo(d->WidgetMaxSize);
  309. widgetSizeHint = QLayout::closestAcceptableSize(d->WidgetToResize, widgetSizeHint)
  310. .expandedTo(QApplication::globalStrut());
  311. this->setWidgetSizeHint(
  312. QSize(d->Orientations & Qt::Horizontal ? widgetSizeHint.width() : 0,
  313. d->Orientations & Qt::Vertical ? widgetSizeHint.height() : 0));
  314. }
  315. //------------------------------------------------------------------------------
  316. void ctkSizeGrip::mouseReleaseEvent(QMouseEvent *mouseEvent)
  317. {
  318. if (mouseEvent->button() != Qt::LeftButton)
  319. {
  320. this->Superclass::mouseReleaseEvent(mouseEvent);
  321. return;
  322. }
  323. Q_D(ctkSizeGrip);
  324. d->Pressed = false;
  325. d->StartPos = QPoint();
  326. }