dockoverlay.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2020 Uwe Kindler
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of Qt Creator.
  7. **
  8. ** Commercial License Usage
  9. ** Licensees holding valid commercial Qt licenses may use this file in
  10. ** accordance with the commercial license agreement provided with the
  11. ** Software or, alternatively, in accordance with the terms contained in
  12. ** a written agreement between you and The Qt Company. For licensing terms
  13. ** and conditions see https://www.qt.io/terms-conditions. For further
  14. ** information use the contact form at https://www.qt.io/contact-us.
  15. **
  16. ** GNU Lesser General Public License Usage
  17. ** Alternatively, this file may be used under the terms of the GNU Lesser
  18. ** General Public License version 2.1 or (at your option) any later version.
  19. ** The licenses are as published by the Free Software Foundation
  20. ** and appearing in the file LICENSE.LGPLv21 included in the packaging
  21. ** of this file. Please review the following information to ensure
  22. ** the GNU Lesser General Public License version 2.1 requirements
  23. ** will be met: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 3 or (at your option) any later version
  28. ** approved by the KDE Free Qt Foundation. The licenses are as published by
  29. ** the Free Software Foundation and appearing in the file LICENSE.GPL3
  30. ** included in the packaging of this file. Please review the following
  31. ** information to ensure the GNU General Public License requirements will
  32. ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
  33. **
  34. ****************************************************************************/
  35. #include "dockoverlay.h"
  36. #include "dockareawidget.h"
  37. #include <utils/hostosinfo.h>
  38. #include <QCursor>
  39. #include <QGridLayout>
  40. #include <QIcon>
  41. #include <QLabel>
  42. #include <QMap>
  43. #include <QMoveEvent>
  44. #include <QPaintEvent>
  45. #include <QPainter>
  46. #include <QPointer>
  47. #include <QResizeEvent>
  48. #include <QWindow>
  49. #include <QtGlobal>
  50. #include <iostream>
  51. namespace ADS {
  52. /**
  53. * Private data class of DockOverlay
  54. */
  55. struct DockOverlayPrivate
  56. {
  57. DockOverlay *q;
  58. DockWidgetAreas m_allowedAreas = InvalidDockWidgetArea;
  59. DockOverlayCross *m_cross;
  60. QPointer<QWidget> m_targetWidget;
  61. DockWidgetArea m_lastLocation = InvalidDockWidgetArea;
  62. bool m_dropPreviewEnabled = true;
  63. DockOverlay::eMode m_mode = DockOverlay::ModeDockAreaOverlay;
  64. QRect m_dropAreaRect;
  65. /**
  66. * Private data constructor
  67. */
  68. DockOverlayPrivate(DockOverlay *parent)
  69. : q(parent)
  70. {}
  71. };
  72. /**
  73. * Private data of DockOverlayCross class
  74. */
  75. struct DockOverlayCrossPrivate
  76. {
  77. DockOverlayCross *q;
  78. DockOverlay::eMode m_mode = DockOverlay::ModeDockAreaOverlay;
  79. DockOverlay *m_dockOverlay;
  80. QHash<DockWidgetArea, QWidget *> m_dropIndicatorWidgets;
  81. QGridLayout *m_gridLayout;
  82. QColor m_iconColors[5];
  83. bool m_updateRequired = false;
  84. double m_lastDevicePixelRatio = 0.1;
  85. /**
  86. * Private data constructor
  87. */
  88. DockOverlayCrossPrivate(DockOverlayCross *parent)
  89. : q(parent)
  90. {}
  91. /**
  92. * @param area
  93. * @return
  94. */
  95. QPoint areaGridPosition(const DockWidgetArea area);
  96. /**
  97. * Palette based default icon colors
  98. */
  99. QColor defaultIconColor(DockOverlayCross::eIconColor colorIndex)
  100. {
  101. QPalette palette = q->palette();
  102. switch (colorIndex) {
  103. case DockOverlayCross::FrameColor:
  104. return palette.color(QPalette::Active, QPalette::Highlight);
  105. case DockOverlayCross::WindowBackgroundColor:
  106. return palette.color(QPalette::Active, QPalette::Base);
  107. case DockOverlayCross::OverlayColor: {
  108. QColor color = palette.color(QPalette::Active, QPalette::Highlight);
  109. color.setAlpha(64);
  110. return color;
  111. }
  112. case DockOverlayCross::ArrowColor:
  113. return palette.color(QPalette::Active, QPalette::Base);
  114. case DockOverlayCross::ShadowColor:
  115. return QColor(0, 0, 0, 64);
  116. }
  117. return QColor();
  118. }
  119. /**
  120. * Stylehseet based icon colors
  121. */
  122. QColor iconColor(DockOverlayCross::eIconColor colorIndex)
  123. {
  124. QColor color = m_iconColors[colorIndex];
  125. if (!color.isValid()) {
  126. color = defaultIconColor(colorIndex);
  127. m_iconColors[colorIndex] = color;
  128. }
  129. return color;
  130. }
  131. /**
  132. * Helper function that returns the drop indicator width depending on the
  133. * operating system
  134. */
  135. qreal dropIndicatiorWidth(QLabel *label) const
  136. {
  137. #ifdef Q_OS_LINUX
  138. Q_UNUSED(label)
  139. return 40;
  140. #else
  141. return static_cast<qreal>(label->fontMetrics().height()) * 3.f;
  142. #endif
  143. }
  144. QWidget *createDropIndicatorWidget(DockWidgetArea dockWidgetArea, DockOverlay::eMode mode)
  145. {
  146. QLabel *label = new QLabel();
  147. label->setObjectName("DockWidgetAreaLabel");
  148. const qreal metric = dropIndicatiorWidth(label);
  149. const QSizeF size(metric, metric);
  150. label->setPixmap(createHighDpiDropIndicatorPixmap(size, dockWidgetArea, mode));
  151. label->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
  152. label->setAttribute(Qt::WA_TranslucentBackground);
  153. label->setProperty("dockWidgetArea", dockWidgetArea);
  154. return label;
  155. }
  156. void updateDropIndicatorIcon(QWidget *dropIndicatorWidget)
  157. {
  158. QLabel *label = qobject_cast<QLabel *>(dropIndicatorWidget);
  159. const qreal metric = dropIndicatiorWidth(label);
  160. const QSizeF size(metric, metric);
  161. int area = label->property("dockWidgetArea").toInt();
  162. label->setPixmap(createHighDpiDropIndicatorPixmap(size,
  163. static_cast<DockWidgetArea>(area),
  164. m_mode)); // TODO
  165. }
  166. QPixmap createHighDpiDropIndicatorPixmap(const QSizeF &size,
  167. DockWidgetArea dockWidgetArea,
  168. DockOverlay::eMode mode)
  169. {
  170. QColor borderColor = iconColor(DockOverlayCross::FrameColor);
  171. QColor backgroundColor = iconColor(DockOverlayCross::WindowBackgroundColor);
  172. double devicePixelRatio = q->window()->devicePixelRatioF();
  173. QSizeF pixmapSize = size * devicePixelRatio;
  174. QPixmap pixmap(pixmapSize.toSize());
  175. pixmap.fill(QColor(0, 0, 0, 0));
  176. QPainter painter(&pixmap);
  177. QPen pen = painter.pen();
  178. QRectF shadowRect(pixmap.rect());
  179. QRectF baseRect;
  180. baseRect.setSize(shadowRect.size() * 0.7);
  181. baseRect.moveCenter(shadowRect.center());
  182. // Fill
  183. QColor shadowColor = iconColor(DockOverlayCross::ShadowColor);
  184. if (shadowColor.alpha() == 255) {
  185. shadowColor.setAlpha(64);
  186. }
  187. painter.fillRect(shadowRect, shadowColor);
  188. // Drop area rect.
  189. painter.save();
  190. QRectF areaRect;
  191. QLineF areaLine;
  192. QRectF nonAreaRect;
  193. switch (dockWidgetArea) {
  194. case TopDockWidgetArea:
  195. areaRect = QRectF(baseRect.x(), baseRect.y(), baseRect.width(), baseRect.height() * 0.5);
  196. nonAreaRect = QRectF(baseRect.x(),
  197. shadowRect.height() * 0.5,
  198. baseRect.width(),
  199. baseRect.height() * 0.5);
  200. areaLine = QLineF(areaRect.bottomLeft(), areaRect.bottomRight());
  201. break;
  202. case RightDockWidgetArea:
  203. areaRect = QRectF(shadowRect.width() * 0.5,
  204. baseRect.y(),
  205. baseRect.width() * 0.5,
  206. baseRect.height());
  207. nonAreaRect = QRectF(baseRect.x(),
  208. baseRect.y(),
  209. baseRect.width() * 0.5,
  210. baseRect.height());
  211. areaLine = QLineF(areaRect.topLeft(), areaRect.bottomLeft());
  212. break;
  213. case BottomDockWidgetArea:
  214. areaRect = QRectF(baseRect.x(),
  215. shadowRect.height() * 0.5,
  216. baseRect.width(),
  217. baseRect.height() * 0.5);
  218. nonAreaRect = QRectF(baseRect.x(),
  219. baseRect.y(),
  220. baseRect.width(),
  221. baseRect.height() * 0.5);
  222. areaLine = QLineF(areaRect.topLeft(), areaRect.topRight());
  223. break;
  224. case LeftDockWidgetArea:
  225. areaRect = QRectF(baseRect.x(), baseRect.y(), baseRect.width() * 0.5, baseRect.height());
  226. nonAreaRect = QRectF(shadowRect.width() * 0.5,
  227. baseRect.y(),
  228. baseRect.width() * 0.5,
  229. baseRect.height());
  230. areaLine = QLineF(areaRect.topRight(), areaRect.bottomRight());
  231. break;
  232. default:
  233. break;
  234. }
  235. QSizeF baseSize = baseRect.size();
  236. if (DockOverlay::ModeContainerOverlay == mode && dockWidgetArea != CenterDockWidgetArea) {
  237. baseRect = areaRect;
  238. }
  239. painter.fillRect(baseRect, backgroundColor);
  240. if (areaRect.isValid()) {
  241. pen = painter.pen();
  242. pen.setColor(borderColor);
  243. QColor color = iconColor(DockOverlayCross::OverlayColor);
  244. if (color.alpha() == 255) {
  245. color.setAlpha(64);
  246. }
  247. painter.setBrush(color);
  248. painter.setPen(Qt::NoPen);
  249. painter.drawRect(areaRect);
  250. pen = painter.pen();
  251. pen.setWidth(1);
  252. pen.setColor(borderColor);
  253. pen.setStyle(Qt::DashLine);
  254. painter.setPen(pen);
  255. painter.drawLine(areaLine);
  256. }
  257. painter.restore();
  258. painter.save();
  259. // Draw outer border
  260. pen = painter.pen();
  261. pen.setColor(borderColor);
  262. pen.setWidth(1);
  263. painter.setBrush(Qt::NoBrush);
  264. painter.setPen(pen);
  265. painter.drawRect(baseRect);
  266. // draw window title bar
  267. painter.setBrush(borderColor);
  268. QRectF frameRect(baseRect.topLeft(), QSizeF(baseRect.width(), baseSize.height() / 10));
  269. painter.drawRect(frameRect);
  270. painter.restore();
  271. // Draw arrow for outer container drop indicators
  272. if (DockOverlay::ModeContainerOverlay == mode && dockWidgetArea != CenterDockWidgetArea) {
  273. QRectF arrowRect;
  274. arrowRect.setSize(baseSize);
  275. arrowRect.setWidth(arrowRect.width() / 4.6);
  276. arrowRect.setHeight(arrowRect.height() / 2);
  277. arrowRect.moveCenter(QPointF(0, 0));
  278. QPolygonF arrow;
  279. arrow << arrowRect.topLeft() << QPointF(arrowRect.right(), arrowRect.center().y())
  280. << arrowRect.bottomLeft();
  281. painter.setPen(Qt::NoPen);
  282. painter.setBrush(iconColor(DockOverlayCross::ArrowColor));
  283. painter.setRenderHint(QPainter::Antialiasing, true);
  284. painter.translate(nonAreaRect.center().x(), nonAreaRect.center().y());
  285. switch (dockWidgetArea) {
  286. case TopDockWidgetArea:
  287. painter.rotate(-90);
  288. break;
  289. case RightDockWidgetArea:
  290. break;
  291. case BottomDockWidgetArea:
  292. painter.rotate(90);
  293. break;
  294. case LeftDockWidgetArea:
  295. painter.rotate(180);
  296. break;
  297. default:
  298. break;
  299. }
  300. painter.drawPolygon(arrow);
  301. }
  302. pixmap.setDevicePixelRatio(devicePixelRatio);
  303. return pixmap;
  304. }
  305. };
  306. DockOverlay::DockOverlay(QWidget *parent, eMode mode)
  307. : QFrame(parent)
  308. , d(new DockOverlayPrivate(this))
  309. {
  310. d->m_mode = mode;
  311. d->m_cross = new DockOverlayCross(this);
  312. if (Utils::HostOsInfo::isLinuxHost())
  313. setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint
  314. | Qt::X11BypassWindowManagerHint);
  315. else
  316. setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
  317. setWindowOpacity(1);
  318. setWindowTitle("DockOverlay");
  319. setAttribute(Qt::WA_NoSystemBackground);
  320. setAttribute(Qt::WA_TranslucentBackground);
  321. d->m_cross->setVisible(false);
  322. setVisible(false);
  323. }
  324. DockOverlay::~DockOverlay()
  325. {
  326. delete d;
  327. }
  328. void DockOverlay::setAllowedAreas(DockWidgetAreas areas)
  329. {
  330. if (areas == d->m_allowedAreas)
  331. return;
  332. d->m_allowedAreas = areas;
  333. d->m_cross->reset();
  334. }
  335. DockWidgetAreas DockOverlay::allowedAreas() const
  336. {
  337. return d->m_allowedAreas;
  338. }
  339. DockWidgetArea DockOverlay::dropAreaUnderCursor() const
  340. {
  341. DockWidgetArea result = d->m_cross->cursorLocation();
  342. if (result != InvalidDockWidgetArea) {
  343. return result;
  344. }
  345. DockAreaWidget *dockArea = qobject_cast<DockAreaWidget *>(d->m_targetWidget.data());
  346. if (!dockArea) {
  347. return result;
  348. }
  349. if (dockArea->allowedAreas().testFlag(CenterDockWidgetArea)
  350. && dockArea->titleBarGeometry().contains(dockArea->mapFromGlobal(QCursor::pos()))) {
  351. return CenterDockWidgetArea;
  352. }
  353. return result;
  354. }
  355. DockWidgetArea DockOverlay::showOverlay(QWidget *target)
  356. {
  357. if (d->m_targetWidget == target) {
  358. // Hint: We could update geometry of overlay here.
  359. DockWidgetArea dockWidgetArea = dropAreaUnderCursor();
  360. if (dockWidgetArea != d->m_lastLocation) {
  361. repaint();
  362. d->m_lastLocation = dockWidgetArea;
  363. }
  364. return dockWidgetArea;
  365. }
  366. d->m_targetWidget = target;
  367. d->m_lastLocation = InvalidDockWidgetArea;
  368. // Move it over the target.
  369. resize(target->size());
  370. QPoint topLeft = target->mapToGlobal(target->rect().topLeft());
  371. move(topLeft);
  372. show();
  373. d->m_cross->updatePosition();
  374. d->m_cross->updateOverlayIcons();
  375. return dropAreaUnderCursor();
  376. }
  377. void DockOverlay::hideOverlay()
  378. {
  379. hide();
  380. d->m_targetWidget.clear();
  381. d->m_lastLocation = InvalidDockWidgetArea;
  382. d->m_dropAreaRect = QRect();
  383. }
  384. void DockOverlay::enableDropPreview(bool enable)
  385. {
  386. d->m_dropPreviewEnabled = enable;
  387. update();
  388. }
  389. bool DockOverlay::dropPreviewEnabled() const
  390. {
  391. return d->m_dropPreviewEnabled;
  392. }
  393. void DockOverlay::paintEvent(QPaintEvent *event)
  394. {
  395. Q_UNUSED(event)
  396. // Draw rect based on location
  397. if (!d->m_dropPreviewEnabled) {
  398. d->m_dropAreaRect = QRect();
  399. return;
  400. }
  401. QRect rectangle = rect();
  402. const DockWidgetArea dockWidgetArea = dropAreaUnderCursor();
  403. double factor = (DockOverlay::ModeContainerOverlay == d->m_mode) ? 3 : 2;
  404. switch (dockWidgetArea) {
  405. case TopDockWidgetArea:
  406. rectangle.setHeight(static_cast<int>(rectangle.height() / factor));
  407. break;
  408. case RightDockWidgetArea:
  409. rectangle.setX(static_cast<int>(rectangle.width() * (1 - 1 / factor)));
  410. break;
  411. case BottomDockWidgetArea:
  412. rectangle.setY(static_cast<int>(rectangle.height() * (1 - 1 / factor)));
  413. break;
  414. case LeftDockWidgetArea:
  415. rectangle.setWidth(static_cast<int>(rectangle.width() / factor));
  416. break;
  417. case CenterDockWidgetArea:
  418. rectangle = rect();
  419. break;
  420. default:
  421. return;
  422. }
  423. QPainter painter(this);
  424. QColor color = palette().color(QPalette::Active, QPalette::Highlight);
  425. QPen pen = painter.pen();
  426. pen.setColor(color.darker(120));
  427. pen.setStyle(Qt::SolidLine);
  428. pen.setWidth(1);
  429. pen.setCosmetic(true);
  430. painter.setPen(pen);
  431. color = color.lighter(130);
  432. color.setAlpha(64);
  433. painter.setBrush(color);
  434. painter.drawRect(rectangle.adjusted(0, 0, -1, -1));
  435. d->m_dropAreaRect = rectangle;
  436. }
  437. QRect DockOverlay::dropOverlayRect() const
  438. {
  439. return d->m_dropAreaRect;
  440. }
  441. void DockOverlay::showEvent(QShowEvent *event)
  442. {
  443. d->m_cross->show();
  444. QFrame::showEvent(event);
  445. }
  446. void DockOverlay::hideEvent(QHideEvent *event)
  447. {
  448. d->m_cross->hide();
  449. QFrame::hideEvent(event);
  450. }
  451. bool DockOverlay::event(QEvent *event)
  452. {
  453. bool result = Super::event(event);
  454. if (event->type() == QEvent::Polish) {
  455. d->m_cross->setupOverlayCross(d->m_mode);
  456. }
  457. return result;
  458. }
  459. static int areaAlignment(const DockWidgetArea area)
  460. {
  461. switch (area) {
  462. case TopDockWidgetArea:
  463. return Qt::AlignHCenter | Qt::AlignBottom;
  464. case RightDockWidgetArea:
  465. return Qt::AlignLeft | Qt::AlignVCenter;
  466. case BottomDockWidgetArea:
  467. return Qt::AlignHCenter | Qt::AlignTop;
  468. case LeftDockWidgetArea:
  469. return Qt::AlignRight | Qt::AlignVCenter;
  470. case CenterDockWidgetArea:
  471. return Qt::AlignCenter;
  472. default:
  473. return Qt::AlignCenter;
  474. }
  475. }
  476. // DockOverlayCrossPrivate
  477. QPoint DockOverlayCrossPrivate::areaGridPosition(const DockWidgetArea area)
  478. {
  479. if (DockOverlay::ModeDockAreaOverlay == m_mode) {
  480. switch (area) {
  481. case TopDockWidgetArea:
  482. return QPoint(1, 2);
  483. case RightDockWidgetArea:
  484. return QPoint(2, 3);
  485. case BottomDockWidgetArea:
  486. return QPoint(3, 2);
  487. case LeftDockWidgetArea:
  488. return QPoint(2, 1);
  489. case CenterDockWidgetArea:
  490. return QPoint(2, 2);
  491. default:
  492. return QPoint();
  493. }
  494. } else {
  495. switch (area) {
  496. case TopDockWidgetArea:
  497. return QPoint(0, 2);
  498. case RightDockWidgetArea:
  499. return QPoint(2, 4);
  500. case BottomDockWidgetArea:
  501. return QPoint(4, 2);
  502. case LeftDockWidgetArea:
  503. return QPoint(2, 0);
  504. case CenterDockWidgetArea:
  505. return QPoint(2, 2);
  506. default:
  507. return QPoint();
  508. }
  509. }
  510. }
  511. DockOverlayCross::DockOverlayCross(DockOverlay *overlay)
  512. : QWidget(overlay->parentWidget())
  513. , d(new DockOverlayCrossPrivate(this))
  514. {
  515. d->m_dockOverlay = overlay;
  516. if (Utils::HostOsInfo::isLinuxHost())
  517. setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint
  518. | Qt::X11BypassWindowManagerHint);
  519. else
  520. setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);
  521. setWindowTitle("DockOverlayCross");
  522. setAttribute(Qt::WA_TranslucentBackground);
  523. d->m_gridLayout = new QGridLayout();
  524. d->m_gridLayout->setSpacing(0);
  525. setLayout(d->m_gridLayout);
  526. }
  527. DockOverlayCross::~DockOverlayCross()
  528. {
  529. delete d;
  530. }
  531. void DockOverlayCross::setupOverlayCross(DockOverlay::eMode mode)
  532. {
  533. d->m_mode = mode;
  534. QHash<DockWidgetArea, QWidget *> areaWidgets;
  535. areaWidgets.insert(TopDockWidgetArea, d->createDropIndicatorWidget(TopDockWidgetArea, mode));
  536. areaWidgets.insert(RightDockWidgetArea, d->createDropIndicatorWidget(RightDockWidgetArea, mode));
  537. areaWidgets.insert(BottomDockWidgetArea,
  538. d->createDropIndicatorWidget(BottomDockWidgetArea, mode));
  539. areaWidgets.insert(LeftDockWidgetArea, d->createDropIndicatorWidget(LeftDockWidgetArea, mode));
  540. areaWidgets.insert(CenterDockWidgetArea,
  541. d->createDropIndicatorWidget(CenterDockWidgetArea, mode));
  542. d->m_lastDevicePixelRatio = devicePixelRatioF();
  543. setAreaWidgets(areaWidgets);
  544. d->m_updateRequired = false;
  545. }
  546. void DockOverlayCross::updateOverlayIcons()
  547. {
  548. if (windowHandle()->devicePixelRatio() == d->m_lastDevicePixelRatio) { // TODO
  549. return;
  550. }
  551. for (auto Widget : d->m_dropIndicatorWidgets) {
  552. d->updateDropIndicatorIcon(Widget);
  553. }
  554. d->m_lastDevicePixelRatio = devicePixelRatioF();
  555. }
  556. void DockOverlayCross::setIconColor(eIconColor colorIndex, const QColor &color)
  557. {
  558. d->m_iconColors[colorIndex] = color;
  559. d->m_updateRequired = true;
  560. }
  561. QColor DockOverlayCross::iconColor(eIconColor colorIndex) const
  562. {
  563. return d->m_iconColors[colorIndex];
  564. }
  565. void DockOverlayCross::setAreaWidgets(const QHash<DockWidgetArea, QWidget *> &widgets)
  566. {
  567. // Delete old widgets.
  568. const auto values = d->m_dropIndicatorWidgets.values();
  569. for (auto widget : values) {
  570. d->m_gridLayout->removeWidget(widget);
  571. delete widget;
  572. }
  573. d->m_dropIndicatorWidgets.clear();
  574. // Insert new widgets into grid.
  575. d->m_dropIndicatorWidgets = widgets;
  576. const QHash<DockWidgetArea, QWidget *> areas = d->m_dropIndicatorWidgets;
  577. QHash<DockWidgetArea, QWidget *>::const_iterator constIt;
  578. for (constIt = areas.begin(); constIt != areas.end(); ++constIt) {
  579. const DockWidgetArea area = constIt.key();
  580. QWidget *widget = constIt.value();
  581. QPoint position = d->areaGridPosition(area);
  582. d->m_gridLayout->addWidget(widget,
  583. position.x(),
  584. position.y(),
  585. static_cast<Qt::Alignment>(areaAlignment(area)));
  586. }
  587. if (DockOverlay::ModeDockAreaOverlay == d->m_mode) {
  588. d->m_gridLayout->setContentsMargins(0, 0, 0, 0);
  589. d->m_gridLayout->setRowStretch(0, 1);
  590. d->m_gridLayout->setRowStretch(1, 0);
  591. d->m_gridLayout->setRowStretch(2, 0);
  592. d->m_gridLayout->setRowStretch(3, 0);
  593. d->m_gridLayout->setRowStretch(4, 1);
  594. d->m_gridLayout->setColumnStretch(0, 1);
  595. d->m_gridLayout->setColumnStretch(1, 0);
  596. d->m_gridLayout->setColumnStretch(2, 0);
  597. d->m_gridLayout->setColumnStretch(3, 0);
  598. d->m_gridLayout->setColumnStretch(4, 1);
  599. } else {
  600. d->m_gridLayout->setContentsMargins(4, 4, 4, 4);
  601. d->m_gridLayout->setRowStretch(0, 0);
  602. d->m_gridLayout->setRowStretch(1, 1);
  603. d->m_gridLayout->setRowStretch(2, 1);
  604. d->m_gridLayout->setRowStretch(3, 1);
  605. d->m_gridLayout->setRowStretch(4, 0);
  606. d->m_gridLayout->setColumnStretch(0, 0);
  607. d->m_gridLayout->setColumnStretch(1, 1);
  608. d->m_gridLayout->setColumnStretch(2, 1);
  609. d->m_gridLayout->setColumnStretch(3, 1);
  610. d->m_gridLayout->setColumnStretch(4, 0);
  611. }
  612. reset();
  613. }
  614. DockWidgetArea DockOverlayCross::cursorLocation() const
  615. {
  616. const QPoint position = mapFromGlobal(QCursor::pos());
  617. const QHash<DockWidgetArea, QWidget *> areas = d->m_dropIndicatorWidgets;
  618. QHash<DockWidgetArea, QWidget *>::const_iterator constIt;
  619. for (constIt = areas.begin(); constIt != areas.end(); ++constIt)
  620. {
  621. if (d->m_dockOverlay->allowedAreas().testFlag(constIt.key()) && constIt.value()
  622. && constIt.value()->isVisible() && constIt.value()->geometry().contains(position)) {
  623. return constIt.key();
  624. }
  625. }
  626. return InvalidDockWidgetArea;
  627. }
  628. void DockOverlayCross::showEvent(QShowEvent *)
  629. {
  630. if (d->m_updateRequired) {
  631. setupOverlayCross(d->m_mode);
  632. }
  633. this->updatePosition();
  634. }
  635. void DockOverlayCross::updatePosition()
  636. {
  637. resize(d->m_dockOverlay->size());
  638. QPoint topLeft = d->m_dockOverlay->pos();
  639. QPoint offest((this->width() - d->m_dockOverlay->width()) / 2,
  640. (this->height() - d->m_dockOverlay->height()) / 2);
  641. QPoint crossTopLeft = topLeft - offest;
  642. move(crossTopLeft);
  643. }
  644. void DockOverlayCross::reset()
  645. {
  646. const QList<DockWidgetArea> allAreas{TopDockWidgetArea,
  647. RightDockWidgetArea,
  648. BottomDockWidgetArea,
  649. LeftDockWidgetArea,
  650. CenterDockWidgetArea};
  651. const DockWidgetAreas allowedAreas = d->m_dockOverlay->allowedAreas();
  652. // Update visibility of area widgets based on allowedAreas.
  653. for (auto area : allAreas) {
  654. QPoint position = d->areaGridPosition(area);
  655. QLayoutItem *item = d->m_gridLayout->itemAtPosition(position.x(), position.y());
  656. QWidget *widget = nullptr;
  657. if (item && (widget = item->widget()) != nullptr) {
  658. widget->setVisible(allowedAreas.testFlag(area));
  659. }
  660. }
  661. }
  662. void DockOverlayCross::setIconColors(const QString &colors)
  663. {
  664. static const QMap<QString, int>
  665. colorCompenentStringMap{{"Frame", DockOverlayCross::FrameColor},
  666. {"Background", DockOverlayCross::WindowBackgroundColor},
  667. {"Overlay", DockOverlayCross::OverlayColor},
  668. {"Arrow", DockOverlayCross::ArrowColor},
  669. {"Shadow", DockOverlayCross::ShadowColor}};
  670. auto colorList = colors.split(' ', QString::SkipEmptyParts);
  671. for (const auto &colorListEntry : colorList) {
  672. auto componentColor = colorListEntry.split('=', QString::SkipEmptyParts);
  673. int component = colorCompenentStringMap.value(componentColor[0], -1);
  674. if (component < 0) {
  675. continue;
  676. }
  677. d->m_iconColors[component] = QColor(componentColor[1]);
  678. }
  679. d->m_updateRequired = true;
  680. }
  681. QString DockOverlayCross::iconColors() const
  682. {
  683. return QString();
  684. }
  685. } // namespace ADS