1 /* ============================================================
2 * QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader
3 * Copyright (C) 2011-2020 QuiteRSS Team <quiterssteam@gmail.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 * ============================================================ */
18 #include "tabbar.h"
19 
20 #include "mainapplication.h"
21 
TabBar(QWidget * parent)22 TabBar::TabBar(QWidget *parent)
23   : QTabBar(parent)
24   , closingTabState_(CloseTabIdle)
25   , indexClickedTab_(-1)
26   , tabFixed_(false)
27 {
28   setObjectName("tabBar_");
29   setFocusPolicy(Qt::NoFocus);
30   setDocumentMode(true);
31   setMouseTracking(true);
32   setExpanding(false);
33   setMovable(true);
34   setElideMode(Qt::ElideNone);
35   setIconSize(QSize(0, 0));
36   setContextMenuPolicy(Qt::CustomContextMenu);
37 
38   setStyleSheet(QString("#tabBar_ QToolButton {border: 1px solid %1; border-radius: 2px; background: %2;}").
39                 arg(qApp->palette().color(QPalette::Dark).name()).
40                 arg(palette().background().color().name()));
41 
42   addTab("");
43 
44   connect(this, SIGNAL(customContextMenuRequested(QPoint)),
45           this, SLOT(showContextMenuTabBar(const QPoint &)));
46 
47   installEventFilter(this);
48 }
49 
eventFilter(QObject * obj,QEvent * event)50 bool TabBar::eventFilter(QObject *obj, QEvent *event)
51 {
52   if (event->type() == QEvent::MouseButtonPress) {
53     QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
54     if (tabAt(mouseEvent->pos()) < 0)
55       return false;
56     if (mouseEvent->button() & Qt::MiddleButton) {
57       emit closeTab(tabAt(mouseEvent->pos()));
58     } else if (mouseEvent->button() & Qt::LeftButton) {
59       if (tabAt(QPoint(mouseEvent->pos().x(), 0)) == 0)
60         tabFixed_ = true;
61       else
62         tabFixed_ = false;
63     }
64   } else if (event->type() == QEvent::MouseMove) {
65     QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
66     if (mouseEvent->buttons() & Qt::LeftButton) {
67       if ((tabAt(QPoint(mouseEvent->pos().x()-78, 0)) <= 0) || tabFixed_)
68         return true;
69     }
70   }
71 
72   return QTabBar::eventFilter(obj, event);
73 }
74 
showContextMenuTabBar(const QPoint & pos)75 void TabBar::showContextMenuTabBar(const QPoint &pos)
76 {
77   indexClickedTab_ = tabAt(pos);
78 
79   if (indexClickedTab_ == -1) return;
80 
81   QMenu menu;
82   menu.addAction(mainApp->mainWindow()->closeTabAct_);
83   menu.addAction(mainApp->mainWindow()->closeOtherTabsAct_);
84   menu.addAction(mainApp->mainWindow()->closeAllTabsAct_);
85 
86   menu.exec(mapToGlobal(pos));
87 
88   indexClickedTab_ = -1;
89 }
90 
slotCloseTab()91 void TabBar::slotCloseTab()
92 {
93   int index = indexClickedTab_;
94   if (index == -1) index = currentIndex();
95 
96   emit closeTab(index);
97 }
98 
slotCloseOtherTabs()99 void TabBar::slotCloseOtherTabs()
100 {
101   int index = indexClickedTab_;
102   int curIndex = -1;
103   if (index == -1) index = currentIndex();
104   else {
105     if (indexClickedTab_ > currentIndex()) {
106       curIndex = currentIndex();
107     } else if (indexClickedTab_ < currentIndex()){
108       curIndex = indexClickedTab_ + 1;
109     }
110   }
111 
112   closingTabState_ = CloseTabOtherIndex;
113   for (int i = count()-1; i > 0; i--) {
114     if (i == index) continue;
115     if (i == curIndex) closingTabState_ = CloseTabCurrentIndex;
116     else closingTabState_ = CloseTabOtherIndex;
117 
118     emit closeTab(i);
119   }
120   closingTabState_ = CloseTabIdle;
121 }
122 
slotCloseAllTab()123 void TabBar::slotCloseAllTab()
124 {
125   closingTabState_ = CloseTabOtherIndex;
126   for (int i = count()-1; i > 0; i--) {
127     if (i == 1) closingTabState_ = CloseTabCurrentIndex;
128 
129     emit closeTab(i);
130   }
131   closingTabState_ = CloseTabIdle;
132 }
133 
slotNextTab()134 void TabBar::slotNextTab()
135 {
136   setCurrentIndex(currentIndex()+1);
137 }
138 
slotPrevTab()139 void TabBar::slotPrevTab()
140 {
141   setCurrentIndex(currentIndex()-1);
142 }
143