| 1 | #include "DockableWidgetLayout.h" |
| 2 | #include "DockableWidget.h" |
| 3 | #include <QLayoutItem> |
| 4 | #include <QtGlobal> |
| 5 | #include <QSet> |
| 6 | #include <iterator> |
| 7 | |
| 8 | static const int SNAP_DISTANCE = 16; |
| 9 | |
| 10 | DockableWidgetLayout::DockableWidgetLayout(QWidget* parent, int margin, int spacing) |
| 11 | : QLayout(parent) |
| 12 | { |
| 13 | setMargin(margin); |
| 14 | setSpacing(spacing); |
| 15 | minWidth = minHeight = 0; |
| 16 | maxWidth = maxHeight = 0; |
| 17 | } |
| 18 | |
| 19 | DockableWidgetLayout::DockableWidgetLayout(int spacing) |
| 20 | { |
| 21 | setSpacing(spacing); |
| 22 | minWidth = minHeight = 0; |
| 23 | maxWidth = maxHeight = 0; |
| 24 | } |
| 25 | |
| 26 | DockableWidgetLayout::~DockableWidgetLayout() |
| 27 | { |
| 28 | while (!dockedWidgets.empty()) { |
| 29 | DockInfo* info = dockedWidgets.takeFirst(); |
| 30 | delete info->item; |
| 31 | delete info->widget; |
| 32 | delete info; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | void DockableWidgetLayout::addItem(QLayoutItem* item) |
| 37 | { |
| 38 | addItem(item, -1); |
| 39 | } |
| 40 | |
| 41 | void DockableWidgetLayout::addItem( |
| 42 | QLayoutItem* item, int index, DockSide side, int dist, int w, int h) |
| 43 | { |
| 44 | auto* info = new DockInfo(); |
| 45 | info->item = item; |
| 46 | info->widget = qobject_cast<DockableWidget*>(item->widget()); |
| 47 | info->dockSide = side; |
| 48 | info->dockDistance = dist; |
| 49 | info->left = 0; |
| 50 | info->top = 0; |
| 51 | info->width = -1; |
| 52 | info->height = -1; |
| 53 | info->useHintWidth = true; |
| 54 | info->useHintHeight = true; |
| 55 | |
| 56 | if (info->widget->sizePolicy().horizontalPolicy() != QSizePolicy::Fixed && |
| 57 | w > 0) { |
| 58 | info->width = w; |
| 59 | info->useHintWidth = false; |
| 60 | } |
| 61 | if (info->widget->sizePolicy().verticalPolicy() != QSizePolicy::Fixed && |
| 62 | h > 0) { |
| 63 | info->height = h; |
| 64 | info->useHintHeight = false; |
| 65 | } |
| 66 | |
| 67 | // first widget is the resize widget, set initial size |
| 68 | if (dockedWidgets.empty()) { |
| 69 | if (info->width == -1) { |
| 70 | info->width = item->sizeHint().width(); |
| 71 | } |
| 72 | if (info->height == -1) { |
| 73 | info->height = item->sizeHint().height(); |
| 74 | } |
| 75 | info->useHintWidth = false; |
| 76 | info->useHintHeight = false; |
| 77 | } |
| 78 | |
| 79 | for (const auto* w : dockedWidgets) { |
| 80 | Q_ASSERT(w->widget != item->widget()); |
| 81 | } |
| 82 | |
| 83 | if (index > -1 && index < dockedWidgets.size()) { |
| 84 | dockedWidgets.insert(index, info); |
| 85 | } else { |
| 86 | dockedWidgets.append(info); |
| 87 | } |
| 88 | |
| 89 | // recalculate size limits |
| 90 | calcSizeLimits(); |
| 91 | } |
| 92 | |
| 93 | void DockableWidgetLayout::addWidget(DockableWidget* widget, const QRect& rect) |
| 94 | { |
| 95 | int index; |
| 96 | DockSide side; |
| 97 | QRect r(rect); |
| 98 | if (insertLocation(r, index, side, widget->sizePolicy())) { |
| 99 | DockInfo* d = dockedWidgets.first(); |
| 100 | int dist; |
| 101 | switch (side) { |
| 102 | case TOP: |
| 103 | case BOTTOM: |
| 104 | dist = r.left() - d->left; |
| 105 | break; |
| 106 | case LEFT: |
| 107 | case RIGHT: |
| 108 | dist = r.top() - d->top; |
| 109 | break; |
| 110 | } |
| 111 | widget->show(); |
| 112 | addItem(new QWidgetItem(widget), index, side, dist, |
| 113 | r.width(), r.height()); |
| 114 | update(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void DockableWidgetLayout::addWidget( |
| 119 | DockableWidget* widget, DockSide side, int dist, int w, int h) |
| 120 | { |
| 121 | // append item at the back |
| 122 | addItem(new QWidgetItem(widget), -1, side, dist, w, h); |
| 123 | } |
| 124 | |
| 125 | void DockableWidgetLayout::changed() |
| 126 | { |
| 127 | calcSizeLimits(); |
| 128 | update(); |
| 129 | } |
| 130 | |
| 131 | Qt::Orientations DockableWidgetLayout::expandingDirections() const |
| 132 | { |
| 133 | return Qt::Horizontal | Qt::Vertical; |
| 134 | } |
| 135 | |
| 136 | bool DockableWidgetLayout::hasHeightForWidth() const |
| 137 | { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | int DockableWidgetLayout::count() const |
| 142 | { |
| 143 | return dockedWidgets.size(); |
| 144 | } |
| 145 | |
| 146 | QLayoutItem* DockableWidgetLayout::takeAt(int index) |
| 147 | { |
| 148 | if (index < 0 || index >= dockedWidgets.size()) return nullptr; |
| 149 | |
| 150 | DockInfo* info = dockedWidgets.takeAt(index); |
| 151 | QLayoutItem* item = info->item; |
| 152 | delete info; |
| 153 | |
| 154 | calcSizeLimits(); |
| 155 | return item; |
| 156 | } |
| 157 | |
| 158 | QLayoutItem* DockableWidgetLayout::itemAt(int index) const |
| 159 | { |
| 160 | if (index < 0 || index >= dockedWidgets.size()) return nullptr; |
| 161 | |
| 162 | return dockedWidgets.at(index)->item; |
| 163 | } |
| 164 | |
| 165 | QSize DockableWidgetLayout::minimumSize() const |
| 166 | { |
| 167 | return {minWidth, minHeight}; |
| 168 | } |
| 169 | |
| 170 | QSize DockableWidgetLayout::maximumSize() const |
| 171 | { |
| 172 | return {maxWidth, maxHeight}; |
| 173 | } |
| 174 | |
| 175 | QSize DockableWidgetLayout::sizeHint() const |
| 176 | { |
| 177 | return {layoutWidth, layoutHeight}; |
| 178 | } |
| 179 | |
| 180 | void DockableWidgetLayout::setGeometry(const QRect& rect) |
| 181 | { |
| 182 | QLayout::setGeometry(rect); |
| 183 | |
| 184 | // Qt sometimes sets the geometry outside the minimumSize/maximumSize range. :/ |
| 185 | int W = std::clamp(rect.width(), minWidth, maxWidth ); |
| 186 | int H = std::clamp(rect.height(), minHeight, maxHeight); |
| 187 | |
| 188 | // set main widget size |
| 189 | int dx = W - layoutWidth; |
| 190 | int dy = H - layoutHeight; |
| 191 | |
| 192 | if (dx != 0 || dy != 0) { |
| 193 | sizeMove(dx, dy); |
| 194 | calcSizeLimits(); |
| 195 | } |
| 196 | |
| 197 | // resize the widgets |
| 198 | for (auto* d : dockedWidgets) { |
| 199 | if (!d->widget->isHidden()) { |
| 200 | d->item->setGeometry(d->bounds()); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | void DockableWidgetLayout::calcSizeLimits() |
| 206 | { |
| 207 | if (dockedWidgets.empty()) return; |
| 208 | |
| 209 | // layout with current sizes |
| 210 | doLayout(); |
| 211 | DockInfo* d = dockedWidgets.first(); |
| 212 | |
| 213 | // store current size |
| 214 | int curWidth = d->width; |
| 215 | int curHeight = d->height; |
| 216 | QVector<int> distStore; |
| 217 | for (auto* d : dockedWidgets) { |
| 218 | distStore.push_back(d->dockDistance); |
| 219 | } |
| 220 | |
| 221 | // first check minimum width (blunt method) |
| 222 | for (int i = d->widget->minimumWidth(); i <= curWidth; ++i) { |
| 223 | // trial layout |
| 224 | sizeMove(i - d->width, 0); |
| 225 | doLayout(true); |
| 226 | // restore |
| 227 | d->width = curWidth; |
| 228 | for (int j = 1; j < dockedWidgets.size(); ++j) { |
| 229 | dockedWidgets.at(j)->dockDistance = distStore.at(j); |
| 230 | } |
| 231 | // check result |
| 232 | if (layoutHeight == checkHeight && |
| 233 | layoutWidth - checkWidth == d->width - i) { |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | minWidth = checkWidth; |
| 238 | |
| 239 | // first check maximum width (blunt method) |
| 240 | for (int i = d->widget->maximumWidth(); i >= curWidth; --i) { |
| 241 | // trial layout |
| 242 | sizeMove(i - d->width, 0); |
| 243 | doLayout(true); |
| 244 | // restore |
| 245 | d->width = curWidth; |
| 246 | for (int j = 1; j < dockedWidgets.size(); ++j) { |
| 247 | dockedWidgets.at(j)->dockDistance = distStore.at(j); |
| 248 | } |
| 249 | // check result |
| 250 | if (layoutHeight == checkHeight && |
| 251 | layoutWidth - checkWidth == d->width - i) { |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | maxWidth = checkWidth; |
| 256 | |
| 257 | // first check minimum height (blunt method) |
| 258 | for (int i = d->widget->minimumHeight(); i <= curHeight; ++i) { |
| 259 | // trial layout |
| 260 | sizeMove(0, i - d->height); |
| 261 | doLayout(true); |
| 262 | // restore |
| 263 | d->height = curHeight; |
| 264 | for (int j = 1; j < dockedWidgets.size(); ++j) { |
| 265 | dockedWidgets.at(j)->dockDistance = distStore.at(j); |
| 266 | } |
| 267 | // check result |
| 268 | if (layoutWidth == checkWidth && |
| 269 | layoutHeight - checkHeight == d->height - i) { |
| 270 | break; |
| 271 | } |
| 272 | } |
| 273 | minHeight = checkHeight; |
| 274 | |
| 275 | // first check maximum width (blunt method) |
| 276 | for (int i = d->widget->maximumHeight(); i >= curHeight; --i) { |
| 277 | // trial layout |
| 278 | sizeMove(0, i - d->height); |
| 279 | doLayout(true); |
| 280 | // restore |
| 281 | d->height = curHeight; |
| 282 | for (int j = 1; j < dockedWidgets.size(); ++j) { |
| 283 | dockedWidgets.at(j)->dockDistance = distStore.at(j); |
| 284 | } |
| 285 | // check result |
| 286 | if (layoutWidth == checkWidth && |
| 287 | layoutHeight - checkHeight == d->height - i) { |
| 288 | break; |
| 289 | } |
| 290 | } |
| 291 | maxHeight = checkHeight; |
| 292 | |
| 293 | // restore layout |
| 294 | doLayout(); |
| 295 | } |
| 296 | |
| 297 | void DockableWidgetLayout::sizeMove(int dx, int dy) |
| 298 | { |
| 299 | DockInfo* d0 = dockedWidgets.first(); |
| 300 | for (int i = 1; i < dockedWidgets.size(); ++i) { |
| 301 | DockInfo* d = dockedWidgets.at(i); |
| 302 | if (d->dockSide == TOP || d->dockSide == BOTTOM) { |
| 303 | if (d->dockDistance >= d0->width) { |
| 304 | d->dockDistance += dx; |
| 305 | } |
| 306 | } |
| 307 | if (d->dockSide == LEFT || d->dockSide == RIGHT) { |
| 308 | if (d->dockDistance >= d0->height) { |
| 309 | d->dockDistance += dy; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | d0->width += dx; |
| 314 | d0->height += dy; |
| 315 | } |
| 316 | |
| 317 | void DockableWidgetLayout::doLayout(bool check) |
| 318 | { |
| 319 | if (dockedWidgets.empty()) return; |
| 320 | |
| 321 | DockInfo* d = dockedWidgets.first(); |
| 322 | d->left = 0; |
| 323 | d->top = 0; |
| 324 | int W = d->width; |
| 325 | int H = d->height; |
| 326 | |
| 327 | int dx = 0, dy = 0; |
| 328 | for (int i = 1; i < dockedWidgets.size(); ++i) { |
| 329 | d = dockedWidgets[i]; |
| 330 | // only process visible widgets |
| 331 | if (d->widget->isHidden()) { |
| 332 | d->left = -10000; |
| 333 | d->top = -10000; |
| 334 | continue; |
| 335 | } |
| 336 | // determine size |
| 337 | if (d->useHintWidth) { |
| 338 | d->width = d->item->sizeHint().width(); |
| 339 | } |
| 340 | if (d->useHintHeight) { |
| 341 | d->height = d->item->sizeHint().height(); |
| 342 | } |
| 343 | // determine location |
| 344 | switch (d->dockSide) { |
| 345 | case TOP: |
| 346 | d->left = d->dockDistance; |
| 347 | if (d->dockDistance >= W || d->dockDistance+d->width <= 0) { |
| 348 | d->top = H - d->height; |
| 349 | } else { |
| 350 | d->top = -d->height; |
| 351 | } |
| 352 | // adjust position until it doesn't overlap other widgets |
| 353 | for (int j = 1; j < i; ++j) { |
| 354 | DockInfo* d2 = dockedWidgets[j]; |
| 355 | QRect r(d->left, d->top - QWIDGETSIZE_MAX, |
| 356 | d->width, d->height + QWIDGETSIZE_MAX); |
| 357 | if (r.intersects(d2->bounds())) { |
| 358 | d->top = d2->top - d->height; |
| 359 | } |
| 360 | } |
| 361 | break; |
| 362 | case LEFT: |
| 363 | d->top = d->dockDistance; |
| 364 | if (d->dockDistance >= H || d->dockDistance+d->height <= 0) { |
| 365 | d->left = W - d->width; |
| 366 | } else { |
| 367 | d->left = -d->width; |
| 368 | } |
| 369 | // adjust position until it doesn't overlap other widgets |
| 370 | for (int j = 1; j < i; ++j) { |
| 371 | DockInfo* d2 = dockedWidgets[j]; |
| 372 | QRect r(d->left - QWIDGETSIZE_MAX, d->top, |
| 373 | d->width + QWIDGETSIZE_MAX, d->height); |
| 374 | if (r.intersects(d2->bounds())) { |
| 375 | d->left = d2->left - d->width; |
| 376 | } |
| 377 | } |
| 378 | break; |
| 379 | case RIGHT: |
| 380 | d->top = d->dockDistance; |
| 381 | if (d->dockDistance >= H || d->dockDistance+d->height <= 0) { |
| 382 | d->left = 0; |
| 383 | } else { |
| 384 | d->left = W; |
| 385 | } |
| 386 | // adjust position until it doesn't overlap other widgets |
| 387 | for (int j = 1; j < i; ++j) { |
| 388 | DockInfo* d2 = dockedWidgets[j]; |
| 389 | QRect r(d->left, d->top, |
| 390 | d->width + QWIDGETSIZE_MAX, d->height); |
| 391 | if (r.intersects(d2->bounds())) { |
| 392 | d->left = d2->left + d2->width; |
| 393 | } |
| 394 | } |
| 395 | break; |
| 396 | case BOTTOM: |
| 397 | d->left = d->dockDistance; |
| 398 | if (d->dockDistance >= W || d->dockDistance+d->width <= 0) { |
| 399 | d->top = 0; |
| 400 | } else { |
| 401 | d->top = H; |
| 402 | } |
| 403 | // adjust position until it doesn't overlap other widgets |
| 404 | for (int j = 1; j < i; ++j) { |
| 405 | DockInfo* d2 = dockedWidgets[j]; |
| 406 | QRect r(d->left, d->top, |
| 407 | d->width, d->height + QWIDGETSIZE_MAX); |
| 408 | if (r.intersects(d2->bounds())) { |
| 409 | d->top = d2->top + d2->height; |
| 410 | } |
| 411 | } |
| 412 | break; |
| 413 | } |
| 414 | // check negative coordinates |
| 415 | if (d->left < dx) dx = d->left; |
| 416 | if (d->top < dy) dy = d->top; |
| 417 | } |
| 418 | |
| 419 | // translate widgets and calculate size |
| 420 | int& w = check ? checkWidth : layoutWidth; |
| 421 | int& h = check ? checkHeight : layoutHeight; |
| 422 | w = h = 0; |
| 423 | for (auto* d : dockedWidgets) { |
| 424 | if (!d->widget->isHidden()) { |
| 425 | d->left -= dx; |
| 426 | d->top -= dy; |
| 427 | w = std::max(w, d->right()); |
| 428 | h = std::max(h, d->bottom()); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | bool DockableWidgetLayout::overlaysWithFirstNWidgets(const QRect& r, int n) const |
| 434 | { |
| 435 | for (int i = 0; i < n; ++i) { |
| 436 | if (r.intersects(dockedWidgets[i]->bounds())) { |
| 437 | return true; |
| 438 | } |
| 439 | } |
| 440 | return false; |
| 441 | } |
| 442 | |
| 443 | static bool isClose(int a, int b) |
| 444 | { |
| 445 | return abs(a - b) < SNAP_DISTANCE; |
| 446 | } |
| 447 | |
| 448 | bool DockableWidgetLayout::insertLocation( |
| 449 | QRect& rect, int& index, DockSide& side, const QSizePolicy& sizePol) |
| 450 | { |
| 451 | // best insertion data |
| 452 | // Distance is a number that represents the how far |
| 453 | // the insertion rectangle is from the final location. |
| 454 | unsigned bestDistance = 0xFFFFFFFF; |
| 455 | int bestIndex = 0; |
| 456 | DockSide bestSide; |
| 457 | QRect bestRect; |
| 458 | |
| 459 | // loop over all widgets and find appropriate matching sides |
| 460 | for (int i = 0; i < dockedWidgets.size(); ++i) { |
| 461 | DockInfo* d = dockedWidgets[i]; |
| 462 | /***************************************************** |
| 463 | * Check for placement against the top of the widget * |
| 464 | *****************************************************/ |
| 465 | if (i == 0 || d->dockSide != BOTTOM) { |
| 466 | if (!(rect.left() > d->right() - SNAP_DISTANCE || |
| 467 | rect.right() < d->left + SNAP_DISTANCE) && |
| 468 | isClose(rect.bottom(), d->top)) { |
| 469 | // rectangle is close to the edge |
| 470 | unsigned dist = 8 * abs(rect.bottom() - d->top); |
| 471 | // now find all points on this side |
| 472 | // (use set as a sorted unique list) |
| 473 | QSet<int> sidePoints; |
| 474 | for (int j = 0; j <= i; ++j) { |
| 475 | DockInfo* d2 = dockedWidgets[j]; |
| 476 | if (d->top == d2->top) { |
| 477 | sidePoints.insert(d2->left); |
| 478 | sidePoints.insert(d2->right()); |
| 479 | // check if any other widget rest against this side |
| 480 | for (int k = i + 1; k < dockedWidgets.size(); ++k) { |
| 481 | DockInfo* d3 = dockedWidgets[k]; |
| 482 | if (d3->bottom() == d2->top) { |
| 483 | sidePoints.insert(d3->left); |
| 484 | sidePoints.insert(d3->right()); |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | // widget placement can occur at all points, find the closest |
| 490 | auto it = sidePoints.begin(); |
| 491 | for (int j = 0; j < sidePoints.size() - 1; ++j) { |
| 492 | // check after point |
| 493 | unsigned newDist1 = dist + abs(*it - rect.left()); |
| 494 | if (newDist1 < bestDistance && isClose(*it, rect.left())) { |
| 495 | QRect r(QPoint(*it, d->top - rect.height()), rect.size()); |
| 496 | if (!overlaysWithFirstNWidgets(r, i)) { |
| 497 | bestDistance = newDist1; |
| 498 | bestIndex = i + 1; |
| 499 | bestSide = TOP; |
| 500 | bestRect = r; |
| 501 | } |
| 502 | } |
| 503 | ++it; |
| 504 | // check before point |
| 505 | unsigned newDist2 = dist + abs(*it - rect.right()); |
| 506 | if (newDist2 < bestDistance && isClose(*it, rect.right())) { |
| 507 | QRect r(QPoint(*it - rect.width(), d->top - rect.height()), |
| 508 | rect.size()); |
| 509 | if (!overlaysWithFirstNWidgets(r, i)) { |
| 510 | bestDistance = newDist2; |
| 511 | bestIndex = i + 1; |
| 512 | bestSide = TOP; |
| 513 | bestRect = r; |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | // check for resized placement options |
| 518 | if (sizePol.horizontalPolicy() != QSizePolicy::Fixed) { |
| 519 | int mid = rect.left() + rect.width() / 2; |
| 520 | for (auto ita = std::next(sidePoints.begin()); ita != sidePoints.end(); ++ita) { |
| 521 | for (auto itb = sidePoints.begin(); ita != itb; ++itb) { |
| 522 | int sp_mid = (*ita + *itb) / 2; |
| 523 | int sp_diff = *ita - *itb; |
| 524 | if (isClose(sp_mid, mid)) { |
| 525 | QRect r(*itb, d->top - rect.height(), |
| 526 | sp_diff, rect.height()); |
| 527 | if (!overlaysWithFirstNWidgets(r, i)) { |
| 528 | bestDistance = dist + abs(sp_mid - mid); |
| 529 | bestIndex = i + 1; |
| 530 | bestSide = TOP; |
| 531 | bestRect = r; |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | /******************************************************** |
| 541 | * Check for placement against the bottom of the widget * |
| 542 | ********************************************************/ |
| 543 | if (i == 0 || d->dockSide != TOP) { |
| 544 | if (!(rect.left() > d->right() - SNAP_DISTANCE || |
| 545 | rect.right() < d->left + SNAP_DISTANCE) && |
| 546 | isClose(rect.top(), d->bottom())) { |
| 547 | // rectangle is close to the edge |
| 548 | unsigned dist = 8 * abs(rect.top() - d->bottom()); |
| 549 | // now find all points on this side |
| 550 | // (use set as a sorted unique list) |
| 551 | QSet<int> sidePoints; |
| 552 | for (int j = 0; j <= i; ++j) { |
| 553 | DockInfo* d2 = dockedWidgets[j]; |
| 554 | if (d->bottom() == d2->bottom()) { |
| 555 | sidePoints.insert(d2->left); |
| 556 | sidePoints.insert(d2->right()); |
| 557 | // check if any other widget rest against this side |
| 558 | for (int k = i + 1; k < dockedWidgets.size(); ++k) { |
| 559 | DockInfo* d3 = dockedWidgets[k]; |
| 560 | if (d3->top == d2->bottom()) { |
| 561 | sidePoints.insert(d3->left); |
| 562 | sidePoints.insert(d3->right()); |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | // widget placement can occur at all points, find the closest |
| 568 | auto it = sidePoints.begin(); |
| 569 | for (int j = 0; j < sidePoints.size() - 1; ++j) { |
| 570 | // check after point |
| 571 | unsigned newDist1 = dist + abs(*it - rect.left()); |
| 572 | if (newDist1 < bestDistance && isClose(*it, rect.left())) { |
| 573 | QRect r(QPoint(*it, d->bottom()), rect.size()); |
| 574 | if (!overlaysWithFirstNWidgets(r, i)) { |
| 575 | bestDistance = newDist1; |
| 576 | bestIndex = i + 1; |
| 577 | bestSide = BOTTOM; |
| 578 | bestRect = r; |
| 579 | } |
| 580 | } |
| 581 | ++it; |
| 582 | // check before point |
| 583 | unsigned newDist2 = dist + abs(*it - rect.right()); |
| 584 | if (newDist2 < bestDistance && isClose(*it, rect.right())) { |
| 585 | QRect r(QPoint(*it - rect.width(), d->bottom()), rect.size()); |
| 586 | if (!overlaysWithFirstNWidgets(r, i)) { |
| 587 | bestDistance = newDist2; |
| 588 | bestIndex = i + 1; |
| 589 | bestSide = BOTTOM; |
| 590 | bestRect = r; |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | // check for resized placement options |
| 595 | if (sizePol.horizontalPolicy() != QSizePolicy::Fixed) { |
| 596 | int mid = rect.left() + rect.width() / 2; |
| 597 | for (auto ita = std::next(sidePoints.begin()); ita != sidePoints.end(); ++ita) { |
| 598 | for (auto itb = sidePoints.begin(); ita != itb; ++itb) { |
| 599 | int sp_mid = (*ita + *itb) / 2; |
| 600 | int sp_diff = *ita - *itb; |
| 601 | if (isClose(sp_mid, mid)) { |
| 602 | QRect r(*itb, d->bottom(), |
| 603 | sp_diff, rect.height()); |
| 604 | if (!overlaysWithFirstNWidgets(r, i)) { |
| 605 | bestDistance = dist + abs(sp_mid - mid); |
| 606 | bestIndex = i + 1; |
| 607 | bestSide = BOTTOM; |
| 608 | bestRect = r; |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | /****************************************************** |
| 618 | * Check for placement against the left of the widget * |
| 619 | ******************************************************/ |
| 620 | if (i == 0 || d->dockSide != RIGHT) { |
| 621 | if (!(rect.top() > d->bottom() - SNAP_DISTANCE || |
| 622 | rect.bottom() < d->top + SNAP_DISTANCE) && |
| 623 | isClose(rect.right(), d->left)) { |
| 624 | // rectangle is close to the edge |
| 625 | unsigned dist = 8 * abs(rect.right() - d->left); |
| 626 | // now find all points on this side |
| 627 | // (use set as a sorted unique list) |
| 628 | QSet<int> sidePoints; |
| 629 | for (int j = 0; j <= i; ++j) { |
| 630 | DockInfo* d2 = dockedWidgets[j]; |
| 631 | if (d->left == d2->left) { |
| 632 | sidePoints.insert(d2->top); |
| 633 | sidePoints.insert(d2->bottom()); |
| 634 | // check if any other widget rest against this side |
| 635 | for (int k = i + 1; k < dockedWidgets.size(); ++k) { |
| 636 | DockInfo* d3 = dockedWidgets[k]; |
| 637 | if (d3->right() == d2->left) { |
| 638 | sidePoints.insert(d3->top); |
| 639 | sidePoints.insert(d3->bottom()); |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | } |
| 644 | // widget placement can occur at all points, find the closest |
| 645 | auto it = sidePoints.begin(); |
| 646 | for (int j = 0; j < sidePoints.size() - 1; ++j) { |
| 647 | // check after point |
| 648 | unsigned newDist1 = dist + abs(*it - rect.top()); |
| 649 | if (newDist1 < bestDistance && isClose(*it, rect.top())) { |
| 650 | QRect r(QPoint(d->left - rect.width(), *it), rect.size()); |
| 651 | if (!overlaysWithFirstNWidgets(r, i)) { |
| 652 | bestDistance = newDist1; |
| 653 | bestIndex = i + 1; |
| 654 | bestSide = LEFT; |
| 655 | bestRect = r; |
| 656 | } |
| 657 | } |
| 658 | ++it; |
| 659 | // check before point |
| 660 | unsigned newDist2 = dist + abs(*it - rect.bottom()); |
| 661 | if (newDist2 < bestDistance && isClose(*it, rect.bottom())) { |
| 662 | QRect r(QPoint(d->left - rect.width(), *it - rect.height()), |
| 663 | rect.size()); |
| 664 | if (!overlaysWithFirstNWidgets(r, i)) { |
| 665 | bestDistance = newDist2; |
| 666 | bestIndex = i + 1; |
| 667 | bestSide = LEFT; |
| 668 | bestRect = r; |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | // check for resized placement options |
| 673 | if (sizePol.verticalPolicy() != QSizePolicy::Fixed) { |
| 674 | int mid = rect.top() + rect.height() / 2; |
| 675 | for (auto ita = std::next(sidePoints.begin()); ita != sidePoints.end(); ++ita) { |
| 676 | for (auto itb = sidePoints.begin(); ita != itb; ++itb) { |
| 677 | int sp_mid = (*ita + *itb) / 2; |
| 678 | int sp_diff = *ita - *itb; |
| 679 | if (isClose(sp_mid, mid)) { |
| 680 | QRect r(d->left - rect.width(), *itb, |
| 681 | rect.width(), sp_diff); |
| 682 | if (!overlaysWithFirstNWidgets(r, i)) { |
| 683 | bestDistance = dist + abs(sp_mid - mid); |
| 684 | bestIndex = i + 1; |
| 685 | bestSide = LEFT; |
| 686 | bestRect = r; |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | /******************************************************* |
| 695 | * Check for placement against the right of the widget * |
| 696 | *******************************************************/ |
| 697 | if (i == 0 || d->dockSide != LEFT) { |
| 698 | if (!(rect.top() > d->bottom() - SNAP_DISTANCE || |
| 699 | rect.bottom() < d->top + SNAP_DISTANCE) && |
| 700 | isClose(rect.left(), d->right())) { |
| 701 | // rectangle is close to the edge |
| 702 | unsigned dist = 8 * abs(rect.left() - d->right()); |
| 703 | // now find all points on this side |
| 704 | // (use set as a sorted unique list) |
| 705 | QSet<int> sidePoints; |
| 706 | for (int j = 0; j <= i; ++j) { |
| 707 | DockInfo* d2 = dockedWidgets[j]; |
| 708 | if (d->right() == d2->right()) { |
| 709 | sidePoints.insert(d2->top); |
| 710 | sidePoints.insert(d2->bottom()); |
| 711 | // check if any other widget rest against this side |
| 712 | for (int k = i + 1; k < dockedWidgets.size(); ++k) { |
| 713 | DockInfo* d3 = dockedWidgets[k]; |
| 714 | if (d3->left == d2->right()) { |
| 715 | sidePoints.insert(d3->top); |
| 716 | sidePoints.insert(d3->bottom()); |
| 717 | } |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | // widget placement can occur at all points, find the closest |
| 722 | auto it = sidePoints.begin(); |
| 723 | for (int j = 0; j < sidePoints.size() - 1; ++j) { |
| 724 | // check after point |
| 725 | unsigned newDist1 = dist + abs(*it - rect.top()); |
| 726 | if (newDist1 < bestDistance && isClose(*it, rect.top())) { |
| 727 | QRect r(QPoint(d->left + d->width, *it), rect.size()); |
| 728 | if (!overlaysWithFirstNWidgets(r, i)) { |
| 729 | bestDistance = newDist1; |
| 730 | bestIndex = i + 1; |
| 731 | bestSide = RIGHT; |
| 732 | bestRect = r; |
| 733 | } |
| 734 | } |
| 735 | ++it; |
| 736 | // check before point |
| 737 | unsigned newDist2 = dist + abs(*it - rect.bottom()); |
| 738 | if (newDist2 < bestDistance && isClose(*it, rect.bottom())) { |
| 739 | QRect r(QPoint(d->right(), *it - rect.height()), rect.size()); |
| 740 | if (!overlaysWithFirstNWidgets(r, i)) { |
| 741 | bestDistance = newDist2; |
| 742 | bestIndex = i + 1; |
| 743 | bestSide = RIGHT; |
| 744 | bestRect = r; |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | // check for resized placement options |
| 749 | if (sizePol.verticalPolicy() != QSizePolicy::Fixed) { |
| 750 | int mid = rect.top() + rect.height() / 2; |
| 751 | for (auto ita = std::next(sidePoints.begin()); ita != sidePoints.end(); ++ita) { |
| 752 | for (auto itb = sidePoints.begin(); ita != itb; ++itb) { |
| 753 | int sp_mid = (*ita + *itb) / 2; |
| 754 | int sp_diff = *ita - *itb; |
| 755 | if (isClose(sp_mid, mid)) { |
| 756 | QRect r(d->right(), *itb, rect.width(), sp_diff); |
| 757 | if (!overlaysWithFirstNWidgets(r, i)) { |
| 758 | bestDistance = dist + abs(sp_mid - mid); |
| 759 | bestIndex = i + 1; |
| 760 | bestSide = RIGHT; |
| 761 | bestRect = r; |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | if (bestIndex) { |
| 772 | rect = bestRect; |
| 773 | index = bestIndex; |
| 774 | side = bestSide; |
| 775 | return true; |
| 776 | } else { |
| 777 | return false; |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | bool DockableWidgetLayout::insertLocation(QRect& rect, const QSizePolicy& sizePol) |
| 782 | { |
| 783 | int index; |
| 784 | DockSide side; |
| 785 | return insertLocation(rect, index, side, sizePol); |
| 786 | } |
| 787 | |
| 788 | void DockableWidgetLayout::getConfig(QStringList& list) |
| 789 | { |
| 790 | for (auto* d : dockedWidgets) { |
| 791 | // string format D [Hidden/Visible] [Side] [Distance] [Width] [Height] |
| 792 | QString s("%1 D %2 %3 %4 %5 %6"); |
| 793 | s = s.arg(d->widget->id()); |
| 794 | |
| 795 | if (d->widget->isHidden()) { |
| 796 | s = s.arg("H"); |
| 797 | } else { |
| 798 | s = s.arg("V"); |
| 799 | } |
| 800 | |
| 801 | switch (d->dockSide) { |
| 802 | case TOP: |
| 803 | s = s.arg("T"); |
| 804 | break; |
| 805 | case LEFT: |
| 806 | s = s.arg("L"); |
| 807 | break; |
| 808 | case RIGHT: |
| 809 | s = s.arg("R"); |
| 810 | break; |
| 811 | case BOTTOM: |
| 812 | s = s.arg("B"); |
| 813 | break; |
| 814 | } |
| 815 | |
| 816 | s = s.arg(d->dockDistance); |
| 817 | |
| 818 | if (d->useHintWidth) { |
| 819 | s = s.arg(-1); |
| 820 | } else { |
| 821 | s = s.arg(d->width); |
| 822 | } |
| 823 | |
| 824 | if (d->useHintHeight) { |
| 825 | s = s.arg(-1); |
| 826 | } else { |
| 827 | s = s.arg(d->height); |
| 828 | } |
| 829 | list.append(s); |
| 830 | } |
| 831 | } |