qBittorrent
proptabbar.cpp
Go to the documentation of this file.
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 
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  connect(m_btnGroup, &QButtonGroup::idClicked
104  , this, &PropTabBar::setCurrentIndex);
105  // Disable buttons focus
106  for (QAbstractButton *btn : asConst(m_btnGroup->buttons()))
107  btn->setFocusPolicy(Qt::NoFocus);
108 }
109 
111 {
112  return m_currentIndex;
113 }
114 
116 {
117  if (index >= m_btnGroup->buttons().size())
118  index = 0;
119  // If asked to hide or if the currently selected tab is clicked
120  if ((index < 0) || (m_currentIndex == index))
121  {
122  if (m_currentIndex >= 0)
123  {
124  m_btnGroup->button(m_currentIndex)->setDown(false);
125  m_currentIndex = -1;
126  emit visibilityToggled(false);
127  }
128  return;
129  }
130  // Unselect previous tab
131  if (m_currentIndex >= 0)
132  {
133  m_btnGroup->button(m_currentIndex)->setDown(false);
134  }
135  else
136  {
137  // Nothing was selected, show!
138  emit visibilityToggled(true);
139  }
140  // Select the new button
141  m_btnGroup->button(index)->setDown(true);
142  m_currentIndex = index;
143  // Emit the signal
144  emit tabChanged(index);
145 }
QButtonGroup * m_btnGroup
Definition: proptabbar.h:63
void setCurrentIndex(int index)
Definition: proptabbar.cpp:115
int m_currentIndex
Definition: proptabbar.h:64
void tabChanged(int index)
int currentIndex() const
Definition: proptabbar.cpp:110
void visibilityToggled(bool visible)
PropTabBar(QWidget *parent=nullptr)
Definition: proptabbar.cpp:39
static UIThemeManager * instance()
constexpr std::add_const_t< T > & asConst(T &t) noexcept
Definition: global.h:42