1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
|
/***************************************************************************
* Copyright 2006-2009 Alexander Dymo <adymo@kdevelop.org> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License as *
* published by the Free Software Foundation; either version 2 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include "container.h"
#include <QApplication>
#include <QBoxLayout>
#include <QClipboard>
#include <QDir>
#include <QEvent>
#include <QSpacerItem>
#include <QLabel>
#include <QMenu>
#include <QMouseEvent>
#include <QPointer>
#include <QStackedWidget>
#include <QStyleFactory>
#include <QStyleOptionTabBarBase>
#include <QStylePainter>
#include <QTabBar>
#include <QToolButton>
#include <QWindow>
#include <KAcceleratorManager>
#include <KConfigGroup>
#include <KLocalizedString>
#include <KSharedConfig>
#include "view.h"
#include "urldocument.h"
#include <KSqueezedTextLabel>
namespace Sublime {
// class ContainerTabBar
class ContainerTabBar : public QTabBar
{
Q_OBJECT
public:
explicit ContainerTabBar(Container* container)
: QTabBar(container), m_container(container)
{
if (QApplication::style()->objectName() == QLatin1String("macintosh")) {
static QPointer<QStyle> qTabBarStyle = QStyleFactory::create(QStringLiteral("fusion"));
if (qTabBarStyle) {
setStyle(qTabBarStyle);
}
}
// configure the QTabBar style so it behaves as appropriately as possible,
// even if we end up using the native Macintosh style because the user's
// Qt doesn't have the Fusion style installed.
setDocumentMode(true);
setUsesScrollButtons(true);
setElideMode(Qt::ElideNone);
installEventFilter(this);
}
bool event(QEvent* ev) override {
if(ev->type() == QEvent::ToolTip)
{
ev->accept();
auto* helpEvent = static_cast<QHelpEvent*>(ev);
int tab = tabAt(helpEvent->pos());
if(tab != -1)
{
m_container->showTooltipForTab(tab);
}
return true;
}
return QTabBar::event(ev);
}
void mousePressEvent(QMouseEvent* event) override {
if (event->button() == Qt::MiddleButton) {
// just close on midbutton, drag can still be done with left mouse button
int tab = tabAt(event->pos());
if (tab != -1) {
emit tabCloseRequested(tab);
}
return;
}
QTabBar::mousePressEvent(event);
}
bool eventFilter(QObject* obj, QEvent* event) override
{
if (obj != this) {
return QObject::eventFilter(obj, event);
}
// TODO Qt6: Move to mouseDoubleClickEvent when fixme in qttabbar.cpp is resolved
// see "fixme Qt 6: move to mouseDoubleClickEvent(), here for BC reasons." in qtabbar.cpp
if (event->type() == QEvent::MouseButtonDblClick) {
// block tabBarDoubleClicked signals with RMB, see https://bugs.kde.org/show_bug.cgi?id=356016
auto mouseEvent = static_cast<const QMouseEvent*>(event);
if (mouseEvent->button() == Qt::MiddleButton) {
return true;
}
}
return QObject::eventFilter(obj, event);
}
Q_SIGNALS:
void newTabRequested();
private:
Container* const m_container;
};
bool sortViews(const View* const lhs, const View* const rhs)
{
return lhs->document()->title().compare(rhs->document()->title(), Qt::CaseInsensitive) < 0;
}
#ifdef Q_OS_MACOS
// only one of these per process:
static QMenu* currentDockMenu = nullptr;
#endif
class ContainerPrivate
{
public:
QBoxLayout* layout;
QHash<QWidget*, View*> viewForWidget;
ContainerTabBar *tabBar;
QStackedWidget *stack;
KSqueezedTextLabel *fileNameCorner;
QSpacerItem* shortCutHelpLeftSpacerItem;
QSpacerItem* shortCutHelpRightSpacerItem;
QLabel *shortcutHelpLabel;
QLabel *fileStatus;
KSqueezedTextLabel *statusCorner;
QPointer<QWidget> leftCornerWidget;
QToolButton* documentListButton;
QMenu* documentListMenu;
QHash<View*, QAction*> documentListActionForView;
/**
* Updates the context menu which is shown when
* the document list button in the tab bar is clicked.
*
* It shall build a popup menu which contains all currently
* enabled views using the title their document provides.
*/
void updateDocumentListPopupMenu()
{
qDeleteAll(documentListActionForView);
documentListActionForView.clear();
documentListMenu->clear();
// create a lexicographically sorted list
QVector<View*> views;
views.reserve(viewForWidget.size());
for (View* view : qAsConst(viewForWidget)) {
views << view;
}
std::sort(views.begin(), views.end(), sortViews);
for (int i = 0; i < views.size(); ++i) {
View *view = views.at(i);
QString visibleEntryTitle;
// if filename is not unique, prepend containing directory
if ((i < views.size() - 1 && view->document()->title() == views.at(i + 1)->document()->title())
|| (i > 0 && view->document()->title() == views.at(i - 1)->document()->title())
) {
auto urlDoc = qobject_cast<Sublime::UrlDocument*>(view->document());
if (!urlDoc) {
visibleEntryTitle = view->document()->title();
}
else {
auto url = urlDoc->url().toString();
int secondOffset = url.lastIndexOf(QLatin1Char('/'));
secondOffset = url.lastIndexOf(QLatin1Char('/'), secondOffset - 1);
visibleEntryTitle = url.right(url.length() - url.lastIndexOf(QLatin1Char('/'), secondOffset) - 1);
}
} else {
visibleEntryTitle = view->document()->title();
}
QAction* action = documentListMenu->addAction(visibleEntryTitle);
action->setData(QVariant::fromValue(view));
documentListActionForView[view] = action;
action->setIcon(view->document()->icon());
///FIXME: push this code somehow into shell, such that we can access the project model for
/// icons and also get a neat, short path like the document switcher.
}
setAsDockMenu();
}
void setAsDockMenu()
{
#ifdef Q_OS_MACOS
if (documentListMenu != currentDockMenu) {
documentListMenu->setAsDockMenu();
currentDockMenu = documentListMenu;
}
#endif
}
~ContainerPrivate()
{
#ifdef Q_OS_MACOS
if (documentListMenu == currentDockMenu) {
QMenu().setAsDockMenu();
currentDockMenu = nullptr;
}
#endif
}
};
class UnderlinedLabel: public KSqueezedTextLabel {
Q_OBJECT
public:
explicit UnderlinedLabel(QTabBar *tabBar, QWidget* parent = nullptr)
:KSqueezedTextLabel(parent), m_tabBar(tabBar)
{
}
protected:
void paintEvent(QPaintEvent *ev) override
{
#ifndef Q_OS_OSX
// getting the underlining right on OS X is tricky; omitting
// the underlining attracts the eye less than not getting it
// exactly right.
if (m_tabBar->isVisible() && m_tabBar->count() > 0)
{
QStylePainter p(this);
QStyleOptionTabBarBase optTabBase;
optTabBase.init(m_tabBar);
optTabBase.shape = m_tabBar->shape();
optTabBase.tabBarRect = m_tabBar->rect();
optTabBase.tabBarRect.moveRight(0);
QStyleOptionTab tabOverlap;
tabOverlap.shape = m_tabBar->shape();
int overlap = style()->pixelMetric(QStyle::PM_TabBarBaseOverlap, &tabOverlap, m_tabBar);
if( overlap > 0 )
{
QRect rect;
rect.setRect(0, height()-overlap, width(), overlap);
optTabBase.rect = rect;
}
if( m_tabBar->drawBase() )
{
p.drawPrimitive(QStyle::PE_FrameTabBarBase, optTabBase);
}
}
#endif
KSqueezedTextLabel::paintEvent(ev);
}
QTabBar *m_tabBar;
};
class StatusLabel: public UnderlinedLabel {
Q_OBJECT
public:
explicit StatusLabel(QTabBar *tabBar, QWidget* parent = nullptr):
UnderlinedLabel(tabBar, parent)
{
setAlignment(Qt::AlignRight | Qt::AlignVCenter);
setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
}
QSize minimumSizeHint() const override
{
QRect rect = style()->itemTextRect(fontMetrics(), QRect(), Qt::AlignRight, true, i18n("Line: 00000 Col: 000"));
rect.setHeight(m_tabBar->height());
return rect.size();
}
};
// class Container
Container::Container(QWidget *parent)
: QWidget(parent)
, d_ptr(new ContainerPrivate())
{
Q_D(Container);
KAcceleratorManager::setNoAccel(this);
auto *l = new QBoxLayout(QBoxLayout::TopToBottom, this);
l->setContentsMargins(0, 0, 0, 0);
l->setSpacing(0);
d->layout = new QBoxLayout(QBoxLayout::LeftToRight);
d->layout->setContentsMargins(0, 0, 0, 0);
d->layout->setSpacing(0);
d->documentListMenu = new QMenu(this);
d->documentListButton = new QToolButton(this);
d->documentListButton->setIcon(QIcon::fromTheme(QStringLiteral("format-list-unordered")));
d->documentListButton->setMenu(d->documentListMenu);
#ifdef Q_OS_MACOS
// for maintaining the Dock menu:
setFocusPolicy(Qt::StrongFocus);
#endif
d->documentListButton->setPopupMode(QToolButton::InstantPopup);
d->documentListButton->setAutoRaise(true);
d->documentListButton->setToolTip(i18nc("@info:tooltip", "Show sorted list of opened documents"));
d->documentListButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
d->layout->addWidget(d->documentListButton);
d->tabBar = new ContainerTabBar(this);
d->tabBar->setContextMenuPolicy(Qt::CustomContextMenu);
d->layout->addWidget(d->tabBar);
d->fileStatus = new QLabel( this );
d->fileStatus->setFixedSize( QSize( 16, 16 ) );
d->layout->addWidget(d->fileStatus);
d->fileNameCorner = new UnderlinedLabel(d->tabBar, this);
d->fileNameCorner->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
d->layout->addWidget(d->fileNameCorner);
d->shortcutHelpLabel = new QLabel(i18nc("@info", "(Press Ctrl+Tab to switch)"), this);
auto font = d->shortcutHelpLabel->font();
font.setPointSize(font.pointSize() - 2);
font.setItalic(true);
d->shortcutHelpLabel->setFont(font);
d->shortCutHelpLeftSpacerItem = new QSpacerItem(0, 0); // fully set in setTabBarHidden()
d->layout->addSpacerItem(d->shortCutHelpLeftSpacerItem);
d->shortcutHelpLabel->setAlignment(Qt::AlignCenter);
d->layout->addWidget(d->shortcutHelpLabel);
d->shortCutHelpRightSpacerItem = new QSpacerItem(0, 0); // fully set in setTabBarHidden()
d->layout->addSpacerItem(d->shortCutHelpRightSpacerItem);
d->statusCorner = new StatusLabel(d->tabBar, this);
d->layout->addWidget(d->statusCorner);
l->addLayout(d->layout);
d->stack = new QStackedWidget(this);
l->addWidget(d->stack);
connect(d->tabBar, &ContainerTabBar::currentChanged, this, &Container::widgetActivated);
connect(d->tabBar, &ContainerTabBar::tabCloseRequested, this, QOverload<int>::of(&Container::requestClose));
connect(d->tabBar, &ContainerTabBar::newTabRequested, this, &Container::newTabRequested);
connect(d->tabBar, &ContainerTabBar::tabMoved, this, &Container::tabMoved);
connect(d->tabBar, &ContainerTabBar::customContextMenuRequested, this, &Container::contextMenu);
connect(d->tabBar, &ContainerTabBar::tabBarDoubleClicked, this, &Container::doubleClickTriggered);
connect(d->documentListMenu, &QMenu::triggered, this, &Container::documentListActionTriggered);
setTabBarHidden(!configTabBarVisible());
d->tabBar->setTabsClosable(configCloseButtonsOnTabs());
d->tabBar->setMovable(true);
d->tabBar->setExpanding(false);
d->tabBar->setSelectionBehaviorOnRemove(QTabBar::SelectPreviousTab);
}
Container::~Container() = default;
bool Container::configTabBarVisible()
{
KConfigGroup group = KSharedConfig::openConfig()->group("UiSettings");
return group.readEntry("TabBarVisibility", 1);
}
bool Container::configCloseButtonsOnTabs()
{
KConfigGroup group = KSharedConfig::openConfig()->group("UiSettings");
return group.readEntry("CloseButtonsOnTabs", 1);
}
void Container::setLeftCornerWidget(QWidget* widget)
{
Q_D(Container);
if(d->leftCornerWidget.data() == widget) {
if(d->leftCornerWidget)
d->leftCornerWidget.data()->setParent(nullptr);
}else{
delete d->leftCornerWidget.data();
d->leftCornerWidget.clear();
}
d->leftCornerWidget = widget;
if(!widget)
return;
widget->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
d->layout->insertWidget(0, widget);
widget->show();
}
QList<View*> Container::views() const
{
Q_D(const Container);
return d->viewForWidget.values();
}
void Container::requestClose(int idx)
{
emit requestClose(widget(idx));
}
void Container::widgetActivated(int idx)
{
Q_D(Container);
if (idx < 0)
return;
if (QWidget* w = d->stack->widget(idx)) {
Sublime::View* view = d->viewForWidget.value(w);
if(view)
emit activateView(view);
}
}
void Container::addWidget(View *view, int position)
{
Q_D(Container);
QWidget *w = view->widget(this);
int idx = 0;
if (position != -1)
{
idx = d->stack->insertWidget(position, w);
}
else
idx = d->stack->addWidget(w);
d->tabBar->insertTab(idx, view->document()->statusIcon(), view->document()->title());
Q_ASSERT(view);
d->viewForWidget[w] = view;
// Update document list context menu. This has to be called before
// setCurrentWidget, because we call the status icon and title update slots
// already, which in turn need the document list menu to be setup.
d->updateDocumentListPopupMenu();
setCurrentWidget(d->stack->currentWidget());
// This fixes a strange layouting bug, that could be reproduced like this: Open a few files in KDevelop, activate the rightmost tab.
// Then temporarily switch to another area, and then switch back. After that, the tab-bar was gone.
// The problem could only be fixed by closing/opening another view.
d->tabBar->setMinimumHeight(d->tabBar->sizeHint().height());
connect(view, &View::statusChanged, this, &Container::statusChanged);
connect(view->document(), &Document::statusIconChanged, this, &Container::statusIconChanged);
connect(view->document(), &Document::titleChanged, this, &Container::documentTitleChanged);
}
void Container::statusChanged(Sublime::View* view)
{
Q_D(Container);
const auto statusText = view->viewStatus();
d->statusCorner->setText(statusText);
d->statusCorner->setVisible(!statusText.isEmpty());
}
void Container::statusIconChanged(Document* doc)
{
Q_D(Container);
QHashIterator<QWidget*, View*> it = d->viewForWidget;
while (it.hasNext()) {
if (it.next().value()->document() == doc) {
d->fileStatus->setPixmap( doc->statusIcon().pixmap( QSize( 16,16 ) ) );
int tabIndex = d->stack->indexOf(it.key());
if (tabIndex != -1) {
d->tabBar->setTabIcon(tabIndex, doc->statusIcon());
}
// Update the document title's menu associated action
// using the View* index map
Q_ASSERT(d->documentListActionForView.contains(it.value()));
d->documentListActionForView[it.value()]->setIcon(doc->icon());
break;
}
}
}
void Container::documentTitleChanged(Sublime::Document* doc)
{
Q_D(Container);
QHashIterator<QWidget*, View*> it = d->viewForWidget;
while (it.hasNext()) {
Sublime::View* view = it.next().value();
if (view->document() == doc) {
if (currentView() == view) {
d->fileNameCorner->setText( doc->title(Document::Extended) );
// TODO KF6: remove this as soon as it is included upstream and we reqire
// that version
// see https://phabricator.kde.org/D7010
d->fileNameCorner->updateGeometry();
}
int tabIndex = d->stack->indexOf(it.key());
if (tabIndex != -1) {
d->tabBar->setTabText(tabIndex, doc->title());
}
break;
}
}
// Update document list popup title
d->updateDocumentListPopupMenu();
}
int Container::count() const
{
Q_D(const Container);
return d->stack->count();
}
QWidget* Container::currentWidget() const
{
Q_D(const Container);
return d->stack->currentWidget();
}
void Container::setCurrentWidget(QWidget* w)
{
Q_D(Container);
if (d->stack->currentWidget() == w) {
return;
}
d->stack->setCurrentWidget(w);
d->tabBar->setCurrentIndex(d->stack->indexOf(w));
if (View* view = viewForWidget(w))
{
statusChanged(view);
if (!d->tabBar->isVisible())
{
// repaint icon and document title only in tabbar-less mode
// tabbar will do repainting for us
statusIconChanged( view->document() );
documentTitleChanged( view->document() );
}
}
}
QWidget* Container::widget(int i) const
{
Q_D(const Container);
return d->stack->widget(i);
}
int Container::indexOf(QWidget* w) const
{
Q_D(const Container);
return d->stack->indexOf(w);
}
void Container::removeWidget(QWidget *w)
{
Q_D(Container);
if (w) {
int widgetIdx = d->stack->indexOf(w);
d->stack->removeWidget(w);
d->tabBar->removeTab(widgetIdx);
if (d->tabBar->currentIndex() != -1 && !d->tabBar->isVisible()) {
// repaint icon and document title only in tabbar-less mode
// tabbar will do repainting for us
View* view = currentView();
if( view ) {
statusIconChanged( view->document() );
documentTitleChanged( view->document() );
}
}
View* view = d->viewForWidget.take(w);
if (view)
{
disconnect(view->document(), &Document::titleChanged, this, &Container::documentTitleChanged);
disconnect(view->document(), &Document::statusIconChanged, this, &Container::statusIconChanged);
disconnect(view, &View::statusChanged, this, &Container::statusChanged);
// Update document list context menu
Q_ASSERT(d->documentListActionForView.contains(view));
delete d->documentListActionForView.take(view);
}
}
}
bool Container::hasWidget(QWidget* w) const
{
Q_D(const Container);
return d->stack->indexOf(w) != -1;
}
View *Container::viewForWidget(QWidget *w) const
{
Q_D(const Container);
return d->viewForWidget.value(w);
}
void Container::setTabBarHidden(bool hide)
{
Q_D(Container);
if (hide)
{
d->tabBar->hide();
d->fileStatus->show();
d->shortCutHelpLeftSpacerItem->changeSize(style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing), 0,
QSizePolicy::Fixed, QSizePolicy::Fixed);
d->shortcutHelpLabel->show();
d->shortCutHelpRightSpacerItem->changeSize(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum);
d->fileNameCorner->show();
}
else
{
d->fileNameCorner->hide();
d->fileStatus->hide();
d->tabBar->show();
d->shortCutHelpLeftSpacerItem->changeSize(0, 0, QSizePolicy::Fixed, QSizePolicy::Fixed);
d->shortcutHelpLabel->hide();
d->shortCutHelpRightSpacerItem->changeSize(0, 0, QSizePolicy::Fixed, QSizePolicy::Fixed);
}
// have spacer item changes taken into account
d->layout->invalidate();
View* v = currentView();
if (v) {
documentTitleChanged(v->document());
}
}
void Container::setCloseButtonsOnTabs(bool show)
{
Q_D(Container);
d->tabBar->setTabsClosable(show);
}
void Container::resetTabColors(const QColor& color)
{
Q_D(Container);
for (int i = 0; i < count(); i++){
d->tabBar->setTabTextColor(i, color);
}
}
void Container::setTabColor(const View* view, const QColor& color)
{
Q_D(Container);
for (int i = 0; i < count(); i++){
if (view == viewForWidget(widget(i))) {
d->tabBar->setTabTextColor(i, color);
}
}
}
void Container::setTabColors(const QHash<const View*, QColor>& colors)
{
Q_D(Container);
for (int i = 0; i < count(); i++) {
auto view = viewForWidget(widget(i));
auto color = colors[view];
if (color.isValid()) {
d->tabBar->setTabTextColor(i, color);
}
}
}
void Container::tabMoved(int from, int to)
{
Q_D(Container);
QWidget *w = d->stack->widget(from);
d->stack->removeWidget(w);
d->stack->insertWidget(to, w);
d->viewForWidget[w]->notifyPositionChanged(to);
}
void Container::contextMenu( const QPoint& pos )
{
Q_D(Container);
QWidget* senderWidget = qobject_cast<QWidget*>(sender());
Q_ASSERT(senderWidget);
int currentTab = d->tabBar->tabAt(pos);
QMenu menu;
// Polish before creating a native window below. The style could want change the surface format
// of the window which will have no effect when the native window has already been created.
menu.ensurePolished();
// At least for positioning on Wayland the window the menu belongs to
// needs to be set. We cannot set senderWidget as parent because some actions (e.g. split view)
// result in sync destruction of the senderWidget, which then would also prematurely
// destruct the menu object
// Workaround (best known currently, check again API of Qt >5.9):
menu.winId(); // trigger being a native widget already, to ensure windowHandle created
auto parentWindowHandle = senderWidget->windowHandle();
if (!parentWindowHandle) {
parentWindowHandle = senderWidget->nativeParentWidget()->windowHandle();
}
menu.windowHandle()->setTransientParent(parentWindowHandle);
Sublime::View* view = viewForWidget(widget(currentTab));
emit tabContextMenuRequested(view, &menu);
menu.addSeparator();
QAction* copyPathAction = nullptr;
QAction* closeTabAction = nullptr;
QAction* closeOtherTabsAction = nullptr;
if (view) {
copyPathAction = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy")),
i18nc("@action:inmenu", "Copy Filename"));
menu.addSeparator();
closeTabAction = menu.addAction(QIcon::fromTheme(QStringLiteral("document-close")),
i18nc("@action:inmenu", "Close"));
closeOtherTabsAction = menu.addAction(QIcon::fromTheme(QStringLiteral("document-close")),
i18nc("@action:inmenu", "Close All Other"));
}
QAction* closeAllTabsAction = menu.addAction(QIcon::fromTheme(QStringLiteral("document-close")), i18nc("@action:inmenu", "Close All"));
QAction* triggered = menu.exec(senderWidget->mapToGlobal(pos));
if (triggered) {
if ( triggered == closeTabAction ) {
requestClose(currentTab);
} else if ( triggered == closeOtherTabsAction ) {
// activate the remaining tab
widgetActivated(currentTab);
// first get the widgets to be closed since otherwise the indices will be wrong
QList<QWidget*> otherTabs;
for ( int i = 0; i < count(); ++i ) {
if ( i != currentTab ) {
otherTabs << widget(i);
}
}
// finally close other tabs
for (QWidget* tab : qAsConst(otherTabs)) {
emit requestClose(tab);
}
} else if ( triggered == closeAllTabsAction ) {
// activate last tab
widgetActivated(count() - 1);
// close all
for ( int i = 0; i < count(); ++i ) {
emit requestClose(widget(i));
}
} else if( triggered == copyPathAction ) {
auto view = viewForWidget( widget( currentTab ) );
auto urlDocument = qobject_cast<UrlDocument*>( view->document() );
if( urlDocument ) {
QString toCopy = urlDocument->url().toDisplayString(QUrl::PreferLocalFile);
if (urlDocument->url().isLocalFile()) {
toCopy = QDir::toNativeSeparators(toCopy);
}
QApplication::clipboard()->setText(toCopy);
}
} // else the action was handled by someone else
}
}
void Container::showTooltipForTab(int tab)
{
emit tabToolTipRequested(viewForWidget(widget(tab)), this, tab);
}
bool Container::isCurrentTab(int tab) const
{
Q_D(const Container);
return d->tabBar->currentIndex() == tab;
}
QRect Container::tabRect(int tab) const
{
Q_D(const Container);
return d->tabBar->tabRect(tab).translated(d->tabBar->mapToGlobal(QPoint(0, 0)));
}
void Container::doubleClickTriggered(int tab)
{
if (tab == -1) {
emit newTabRequested();
} else {
emit tabDoubleClicked(viewForWidget(widget(tab)));
}
}
void Container::documentListActionTriggered(QAction* action)
{
Q_D(Container);
auto* view = action->data().value< Sublime::View* >();
Q_ASSERT(view);
QWidget* widget = d->viewForWidget.key(view);
Q_ASSERT(widget);
setCurrentWidget(widget);
}
Sublime::View* Container::currentView() const
{
Q_D(const Container);
return d->viewForWidget.value(widget( d->tabBar->currentIndex() ));
}
void Container::focusInEvent(QFocusEvent* event)
{
Q_D(Container);
d->setAsDockMenu();
QWidget::focusInEvent(event);
}
}
#include "container.moc"
|