1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2010  Christophe Dumez <chris@qbittorrent.org>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  *
19  * In addition, as a special exception, the copyright holders give permission to
20  * link this program with the OpenSSL project's "OpenSSL" library (or with
21  * modified versions of it that use the same license as the "OpenSSL" library),
22  * and distribute the linked executables. You must obey the GNU General Public
23  * License in all respects for all of the code used other than "OpenSSL".  If you
24  * modify file(s), you may extend this exception to your version of the file(s),
25  * but you are not obligated to do so. If you do not wish to do so, delete this
26  * exception statement from your version.
27  */
28 
29 #include "proptabbar.h"
30 
31 #include <QButtonGroup>
32 #include <QKeySequence>
33 #include <QPushButton>
34 #include <QSpacerItem>
35 
36 #include "base/global.h"
37 #include "gui/uithememanager.h"
38 
PropTabBar(QWidget * parent)39 PropTabBar::PropTabBar(QWidget *parent)
40     : QHBoxLayout(parent)
41     , m_currentIndex(-1)
42 {
43     setAlignment(Qt::AlignLeft | Qt::AlignCenter);
44     setSpacing(3);
45     m_btnGroup = new QButtonGroup(this);
46     // General tab
47     QPushButton *mainInfosButton = new QPushButton(
48 #ifndef Q_OS_MACOS
49             UIThemeManager::instance()->getIcon("document-properties"),
50 #endif
51             tr("General"), parent);
52     mainInfosButton->setShortcut(Qt::ALT + Qt::Key_G);
53     addWidget(mainInfosButton);
54     m_btnGroup->addButton(mainInfosButton, MainTab);
55     // Trackers tab
56     QPushButton *trackersButton = new QPushButton(
57 #ifndef Q_OS_MACOS
58             UIThemeManager::instance()->getIcon("network-server"),
59 #endif
60             tr("Trackers"), parent);
61     trackersButton->setShortcut(Qt::ALT + Qt::Key_C);
62     addWidget(trackersButton);
63     m_btnGroup->addButton(trackersButton, TrackersTab);
64     // Peers tab
65     QPushButton *peersButton = new QPushButton(
66 #ifndef Q_OS_MACOS
67             UIThemeManager::instance()->getIcon("edit-find-user"),
68 #endif
69             tr("Peers"), parent);
70     peersButton->setShortcut(Qt::ALT + Qt::Key_R);
71     addWidget(peersButton);
72     m_btnGroup->addButton(peersButton, PeersTab);
73     // URL seeds tab
74     QPushButton *URLSeedsButton = new QPushButton(
75 #ifndef Q_OS_MACOS
76             UIThemeManager::instance()->getIcon("network-server"),
77 #endif
78             tr("HTTP Sources"), parent);
79     URLSeedsButton->setShortcut(Qt::ALT + Qt::Key_B);
80     addWidget(URLSeedsButton);
81     m_btnGroup->addButton(URLSeedsButton, URLSeedsTab);
82     // Files tab
83     QPushButton *filesButton = new QPushButton(
84 #ifndef Q_OS_MACOS
85             UIThemeManager::instance()->getIcon("inode-directory"),
86 #endif
87             tr("Content"), parent);
88     filesButton->setShortcut(Qt::ALT + Qt::Key_Z);
89     addWidget(filesButton);
90     m_btnGroup->addButton(filesButton, FilesTab);
91     // Spacer
92     addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Fixed));
93     // Speed tab
94     QPushButton *speedButton = new QPushButton(
95 #ifndef Q_OS_MACOS
96             UIThemeManager::instance()->getIcon("office-chart-line"),
97 #endif
98             tr("Speed"), parent);
99     speedButton->setShortcut(Qt::ALT + Qt::Key_D);
100     addWidget(speedButton);
101     m_btnGroup->addButton(speedButton, SpeedTab);
102     // SIGNAL/SLOT
103 #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
104     connect(m_btnGroup, &QButtonGroup::idClicked
105             , this, &PropTabBar::setCurrentIndex);
106 #else
107     connect(m_btnGroup, qOverload<int>(&QButtonGroup::buttonClicked)
108             , this, &PropTabBar::setCurrentIndex);
109 #endif
110     // Disable buttons focus
111     for (QAbstractButton *btn : asConst(m_btnGroup->buttons()))
112         btn->setFocusPolicy(Qt::NoFocus);
113 }
114 
currentIndex() const115 int PropTabBar::currentIndex() const
116 {
117     return m_currentIndex;
118 }
119 
setCurrentIndex(int index)120 void PropTabBar::setCurrentIndex(int index)
121 {
122     if (index >= m_btnGroup->buttons().size())
123         index = 0;
124     // If asked to hide or if the currently selected tab is clicked
125     if ((index < 0) || (m_currentIndex == index))
126     {
127         if (m_currentIndex >= 0)
128         {
129           m_btnGroup->button(m_currentIndex)->setDown(false);
130           m_currentIndex = -1;
131           emit visibilityToggled(false);
132         }
133         return;
134     }
135     // Unselect previous tab
136     if (m_currentIndex >= 0)
137     {
138         m_btnGroup->button(m_currentIndex)->setDown(false);
139     }
140     else
141     {
142         // Nothing was selected, show!
143         emit visibilityToggled(true);
144     }
145     // Select the new button
146     m_btnGroup->button(index)->setDown(true);
147     m_currentIndex = index;
148     // Emit the signal
149     emit tabChanged(index);
150 }
151