1 /************************************************************************
2 **
3 ** Copyright (C) 2015-2020 Kevin B. Hendricks, Stratford, Ontario, Canada
4 ** Copyright (C) 2020 Doug Massay
5 ** Copyright (C) 2012 John Schember <john@nachtimwald.com>
6 ** Copyright (C) 2012 Dave Heiland
7 **
8 ** This file is part of Sigil.
9 **
10 ** Sigil is free software: you can redistribute it and/or modify
11 ** it under the terms of the GNU General Public License as published by
12 ** the Free Software Foundation, either version 3 of the License, or
13 ** (at your option) any later version.
14 **
15 ** Sigil is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ** GNU General Public License for more details.
19 **
20 ** You should have received a copy of the GNU General Public License
21 ** along with Sigil. If not, see <http://www.gnu.org/licenses/>.
22 **
23 *************************************************************************/
24
25 #include <QtGui/QContextMenuEvent>
26 #include <QtWidgets/QAction>
27 #include <QtWidgets/QMenu>
28 #include <QPointer>
29
30 #include "Misc/Utility.h"
31 #include "Tabs/TabBar.h"
32
TabBar(QWidget * parent)33 TabBar::TabBar(QWidget *parent)
34 : QTabBar(parent),
35 m_TabIndex(-1)
36 {
37 #if defined(Q_OS_MAC)
38 // work around Qt MacOSX bug missing tab close icons
39 // see: https://bugreports.qt.io/browse/QTBUG-61092
40 // still broken in document mode in Qt.5.12.2 !!!!
41 const QString FORCE_TAB_CLOSE_BUTTON =
42 "QTabBar::close-button { "
43 "background-image: url(:/qt-project.org/styles/commonstyle/images/standardbutton-closetab-16.png);"
44 "}"
45 "QTabBar::close-button:hover { "
46 "background-image: url(:/qt-project.org/styles/commonstyle/images/standardbutton-closetab-hover-16.png);"
47 "}";
48 setStyleSheet(FORCE_TAB_CLOSE_BUTTON);
49 #endif
50 }
51
mouseDoubleClickEvent(QMouseEvent * event)52 void TabBar::mouseDoubleClickEvent(QMouseEvent *event)
53 {
54 emit TabBarDoubleClicked();
55 }
56
57
mousePressEvent(QMouseEvent * event)58 void TabBar::mousePressEvent(QMouseEvent *event)
59 {
60 if (event->button() == Qt::RightButton) {
61 int tabCount = count();
62
63 if (tabCount <= 1) {
64 return;
65 }
66
67 for (int i = 0; i < tabCount; i++) {
68 if (tabRect(i).contains(event->pos())) {
69 m_TabIndex = i;
70 ShowContextMenu(event, i);
71 break;
72 }
73 }
74 } else if (event->button() == Qt::LeftButton) {
75 emit TabBarClicked();
76 }
77
78 QTabBar::mousePressEvent(event);
79 }
80
ShowContextMenu(QMouseEvent * event,int tab_index)81 void TabBar::ShowContextMenu(QMouseEvent *event, int tab_index)
82 {
83 QPointer<QMenu> menu = new QMenu();
84 QAction *closeOtherTabsAction = new QAction(tr("Close Other Tabs"), menu);
85 menu->addAction(closeOtherTabsAction);
86 connect(closeOtherTabsAction, SIGNAL(triggered()), this, SLOT(EmitCloseOtherTabs()));
87 QPoint p;
88 p = mapToGlobal(event->pos());
89 #ifdef Q_OS_WIN32
90 // Relocate the context menu slightly down and right to prevent "automatic" action
91 // highlight on Windows, which then closes all other tabs when the mouse is released.
92 p.setX(p.x() + 2);
93 p.setY(p.y() + 4);
94 #endif
95 menu->exec(p);
96 if (!menu.isNull()) {
97 delete menu.data();
98 }
99 }
100
EmitCloseOtherTabs()101 void TabBar::EmitCloseOtherTabs()
102 {
103 emit CloseOtherTabsRequest(m_TabIndex);
104 }
105