21 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
29 : QWidget(parent), themeManager_(themeManager ? themeManager : &
ThemeManager::instance())
32 setContextMenuPolicy(Qt::DefaultContextMenu);
34 layout_ =
new QHBoxLayout(
this);
35 layout_->setContentsMargins(4, 2, 4, 2);
36 layout_->setSpacing(2);
39 setStyleSheet(QString(
"DraggableToolbar { background-color: %1; border-bottom: 1px solid %2; }")
49 orderedSections_.append(
section);
56 auto it = sections_.find(
id);
57 return (it != sections_.end()) ? it->second :
nullptr;
63 for (
auto* s : orderedSections_)
66 if (spacersBefore_.find(s->id()) != spacersBefore_.end())
78 for (
auto& [
id, spacer] : spacersBefore_)
80 layout_->removeWidget(spacer);
83 spacersBefore_.clear();
86 for (
auto* s : orderedSections_)
88 layout_->removeWidget(s);
94 layout_->removeWidget(endSpacer_);
98 QList<DraggableToolbarSection*> newOrder;
99 bool nextNeedsSpacer =
false;
101 for (
const QString&
id : order)
105 nextNeedsSpacer =
true;
114 spacersBefore_[id] = spacer;
115 nextNeedsSpacer =
false;
122 for (
auto* s : orderedSections_)
124 if (!newOrder.contains(s))
130 orderedSections_ = newOrder;
141 endSpacer_ =
new QWidget(
this);
142 endSpacer_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
144 layout_->addWidget(endSpacer_);
149 if (spacersBefore_.find(sectionId) != spacersBefore_.end())
153 spacersBefore_[sectionId] = spacer;
160 auto it = spacersBefore_.find(sectionId);
161 if (it == spacersBefore_.end())
164 layout_->removeWidget(it->second);
166 spacersBefore_.erase(it);
173 return spacersBefore_.find(sectionId) != spacersBefore_.end();
178 if (locked_ == locked)
184 for (
auto* s : orderedSections_)
188 s->setCursor(locked_ ? Qt::ArrowCursor : Qt::OpenHandCursor);
195void DraggableToolbar::rebuildLayout()
198 while (layout_->count() > 0)
204 for (
auto* s : orderedSections_)
207 auto it = spacersBefore_.find(s->id());
208 if (it != spacersBefore_.end())
210 layout_->addWidget(it->second);
212 layout_->addWidget(s);
217 if (endSpacer_ && spacersBefore_.empty())
219 layout_->addWidget(endSpacer_);
225 for (
auto* s : orderedSections_)
227 if (s->geometry().contains(pos) && s->isVisible())
237 auto* clickedSection = sectionAtPos(event->pos());
241 QString(
"QMenu { background-color: %1; color: %2; border: 1px solid %3; padding: 4px; }"
242 "QMenu::item { padding: 6px 16px; border-radius: 3px; }"
243 "QMenu::item:selected { background-color: %4; color: %5; }"
244 "QMenu::item:disabled { color: %6; }"
245 "QMenu::separator { height: 1px; background: %3; margin: 4px 8px; }")
254 themeManager_->styleMenu(&menu);
256 if (clickedSection && !clickedSection->isFixed())
258 menu.addAction(QString(
"\u25A3 %1").arg(clickedSection->displayName()))->setEnabled(
false);
262 QAction* pushRightAction = menu.addAction(
"\u2192 Push to Right");
263 pushRightAction->setCheckable(
true);
265 pushRightAction->setEnabled(!locked_);
266 connect(pushRightAction, &QAction::triggered,
this,
267 [
this, clickedSection](
bool checked)
278 qsizetype idx = orderedSections_.indexOf(clickedSection);
279 QAction* moveLeftAction = menu.addAction(
"◀ Move Left");
280 moveLeftAction->setEnabled(!locked_ && idx > 0);
281 connect(moveLeftAction, &QAction::triggered,
this,
282 [
this, clickedSection, idx]()
286 moveSection(clickedSection,
static_cast<int>(idx - 1));
291 QAction* moveRightAction = menu.addAction(
"▶ Move Right");
292 moveRightAction->setEnabled(!locked_ && idx < orderedSections_.size() - 1);
293 connect(moveRightAction, &QAction::triggered,
this,
294 [
this, clickedSection, idx]()
296 if (idx < orderedSections_.size() - 1)
298 moveSection(clickedSection,
static_cast<int>(idx + 2));
303 QAction* moveFirstAction = menu.addAction(
"\u25C0\u25C0 Move to Start");
304 moveFirstAction->setEnabled(!locked_ && idx > 0);
305 connect(moveFirstAction, &QAction::triggered,
this,
306 [
this, clickedSection]()
309 int firstMovable = 0;
310 for (
int i = 0; i < orderedSections_.size(); ++i)
312 if (!orderedSections_[i]->isFixed())
318 moveSection(clickedSection, firstMovable);
322 QAction* moveLastAction = menu.addAction(
"\u25B6\u25B6 Move to End");
323 moveLastAction->setEnabled(!locked_ && idx < orderedSections_.size() - 1);
324 connect(moveLastAction, &QAction::triggered,
this,
325 [
this, clickedSection]()
327 moveSection(clickedSection,
static_cast<int>(orderedSections_.size()));
333 QAction* lockAction =
334 menu.addAction(locked_ ?
"\u25CB Unlock Toolbar" :
"\u25CF Lock Toolbar");
335 connect(lockAction, &QAction::triggered,
this, [
this]() {
setLocked(!locked_); });
340 menu.addAction(
"\u2699 Toolbar Options")->setEnabled(
false);
344 QAction* lockAction =
345 menu.addAction(locked_ ?
"\u25CB Unlock Toolbar" :
"\u25CF Lock Toolbar");
346 connect(lockAction, &QAction::triggered,
this, [
this]() {
setLocked(!locked_); });
348 QAction* resetAction = menu.addAction(
"\u21A9 Reset Layout");
349 resetAction->setEnabled(!locked_);
350 connect(resetAction, &QAction::triggered,
this,
354 for (
auto& [
id, spacer] : spacersBefore_)
356 layout_->removeWidget(spacer);
359 spacersBefore_.clear();
365 menu.exec(event->globalPos());
370 if (event->mimeData()->hasFormat(
"application/x-toolbar-section"))
372 event->acceptProposedAction();
373 showDropIndicator_ =
true;
374 updateDropIndicator(event->position().toPoint());
380 if (event->mimeData()->hasFormat(
"application/x-toolbar-section"))
382 event->acceptProposedAction();
383 updateDropIndicator(event->position().toPoint());
390 showDropIndicator_ =
false;
393 if (!event->mimeData()->hasFormat(
"application/x-toolbar-section"))
396 QString sectionId = QString::fromUtf8(event->mimeData()->data(
"application/x-toolbar-section"));
397 auto* droppedSection =
section(sectionId);
401 int newIndex = findInsertIndex(event->position().toPoint());
402 moveSection(droppedSection, newIndex);
404 event->acceptProposedAction();
410 QWidget::paintEvent(event);
412 if (showDropIndicator_ && dropIndicatorIndex_ >= 0)
414 QPainter painter(
this);
419 if (dropIndicatorIndex_ < orderedSections_.size())
421 x = orderedSections_[dropIndicatorIndex_]->geometry().left() - 2;
423 else if (!orderedSections_.isEmpty())
425 x = orderedSections_.last()->geometry().right() + 2;
429 painter.setPen(QPen(c.
accent, 3));
430 painter.drawLine(x, 4, x, height() - 4);
433 painter.setBrush(c.
accent);
434 painter.setPen(Qt::NoPen);
436 QPolygon topTriangle;
437 topTriangle << QPoint(x - 5, 2) << QPoint(x + 5, 2) << QPoint(x, 8);
438 painter.drawPolygon(topTriangle);
440 QPolygon bottomTriangle;
441 bottomTriangle << QPoint(x - 5, height() - 2) << QPoint(x + 5, height() - 2)
442 << QPoint(x, height() - 8);
443 painter.drawPolygon(bottomTriangle);
447void DraggableToolbar::updateDropIndicator(
const QPoint& pos)
449 dropIndicatorIndex_ = findInsertIndex(pos);
452int DraggableToolbar::findInsertIndex(
const QPoint& pos)
const
454 for (qsizetype i = 0; i < orderedSections_.size(); ++i)
456 QRect rect = orderedSections_[i]->geometry();
457 if (pos.x() < rect.center().x())
459 return static_cast<int>(i);
462 return static_cast<int>(orderedSections_.size());
467 qsizetype currentIndex = orderedSections_.indexOf(
section);
468 if (currentIndex < 0 || currentIndex == newIndex)
472 if (newIndex > currentIndex)
475 orderedSections_.removeAt(currentIndex);
476 orderedSections_.insert(newIndex,
section);
487 setStyleSheet(QString(
"DraggableToolbar { background-color: %1; border-bottom: 1px solid %2; }")
492 for (
auto*
section : orderedSections_)
505 : QFrame(parent), id_(
id), displayName_(
displayName), toolbar_(toolbar),
506 themeManager_(themeManager ? themeManager : &
ThemeManager::instance())
508 setMouseTracking(
true);
509 setFrameShape(QFrame::NoFrame);
511 contentLayout_ =
new QHBoxLayout(
this);
512 contentLayout_->setContentsMargins(6, 2, 6, 2);
513 contentLayout_->setSpacing(4);
520 resizable_ = resizable;
521 minWidth_ = minWidth;
522 maxWidth_ = maxWidth;
525void DraggableToolbarSection::updateStyle()
529 QString bgColor =
"transparent";
530 QString borderColor =
"transparent";
534 bgColor = c.
accent.name();
535 borderColor = c.
accent.name();
537 else if (hovered_ && !fixed_)
540 borderColor = c.
border.name();
543 setStyleSheet(QString(
"DraggableToolbarSection { background-color: %1; border: 1px solid %2; "
544 "border-radius: 4px; }")
556 if (event->button() == Qt::LeftButton)
558 dragStartPos_ =
event->pos();
560 if (resizable_ && isOnResizeHandle(event->pos()))
563 resizeStartX_ =
event->globalPosition().toPoint().x();
564 originalWidth_ = width();
565 setCursor(Qt::SplitHCursor);
568 QFrame::mousePressEvent(event);
573 bool toolbarLocked = toolbar_ && toolbar_->isLocked();
577 int delta =
event->globalPosition().toPoint().x() - resizeStartX_;
578 int newWidth = qBound(minWidth_, originalWidth_ + delta, maxWidth_);
579 setFixedWidth(newWidth);
582 emit toolbar_->sectionResized(id_, newWidth);
584 else if (!fixed_ && !toolbarLocked && (event->buttons() & Qt::LeftButton))
586 if ((event->pos() - dragStartPos_).manhattanLength() >= DRAG_START_DISTANCE)
594 if (resizable_ && isOnResizeHandle(event->pos()))
595 setCursor(Qt::SplitHCursor);
596 else if (!fixed_ && !toolbarLocked)
597 setCursor(Qt::OpenHandCursor);
599 setCursor(Qt::ArrowCursor);
602 QFrame::mouseMoveEvent(event);
610 QFrame::mouseReleaseEvent(event);
617 bool toolbarLocked = toolbar_ && toolbar_->isLocked();
618 if (!fixed_ && !toolbarLocked)
619 setCursor(Qt::OpenHandCursor);
620 QFrame::enterEvent(event);
627 setCursor(Qt::ArrowCursor);
628 QFrame::leaveEvent(event);
633 QFrame::paintEvent(event);
636 if (resizable_ && hovered_)
638 QPainter painter(
this);
642 painter.setPen(Qt::NoPen);
645 int centerY = height() / 2;
647 painter.drawEllipse(QPoint(x, centerY - 4), 1, 1);
648 painter.drawEllipse(QPoint(x, centerY), 1, 1);
649 painter.drawEllipse(QPoint(x, centerY + 4), 1, 1);
653void DraggableToolbarSection::startDrag()
658 QDrag* drag =
new QDrag(
this);
659 QMimeData* mimeData =
new QMimeData();
660 mimeData->setData(
"application/x-toolbar-section", id_.toUtf8());
661 drag->setMimeData(mimeData);
664 QPixmap pixmap(size());
665 pixmap.fill(Qt::transparent);
669 QPixmap transparentPixmap(pixmap.size());
670 transparentPixmap.fill(Qt::transparent);
671 QPainter painter(&transparentPixmap);
672 painter.setOpacity(0.7);
673 painter.drawPixmap(0, 0, pixmap);
676 drag->setPixmap(transparentPixmap);
677 drag->setHotSpot(dragStartPos_);
679 setCursor(Qt::ClosedHandCursor);
680 drag->exec(Qt::MoveAction);
684 setCursor(Qt::OpenHandCursor);
687bool DraggableToolbarSection::isOnResizeHandle(
const QPoint& pos)
const
689 return pos.x() >= width() - RESIZE_HANDLE_WIDTH;
const ThemeColors & colors() const
Get effective colors (with accent customization applied).