Fabcoin Core  0.16.2
P2P Digital Currency
tabbarinfo.cpp
Go to the documentation of this file.
1 #include <tabbarinfo.h>
2 #include <QToolButton>
3 #include <QSize>
4 
5 TabBarInfo::TabBarInfo(QStackedWidget *parent) :
6  QObject(parent),
7  m_current(0),
8  m_stack(parent),
9  m_tabBar(0),
10  m_attached(false),
11  m_iconCloseTab(0)
12 {}
13 
14 bool TabBarInfo::addTab(int index, const QString &name)
15 {
16  if(m_stack->count() <= index)
17  {
18  return false;
19  }
20  m_mapName[index] = name;
21  m_mapVisible[index] = true;
22  update();
23  return true;
24 }
25 
26 void TabBarInfo::removeTab(int index)
27 {
28  int count = m_stack->count();
29  if(count <= index)
30  {
31  return;
32  }
33 
34  QMap<int, QString> mapName;
35  QMap<int, bool> mapVisible;
36  for(int i = 0; i < count; i++)
37  {
38  if(i < index)
39  {
40  mapName[i] = m_mapName[i];
41  mapVisible[i] = m_mapVisible[i];
42  }
43  else if(i > index)
44  {
45  mapName[i-1] = m_mapName[i];
46  mapVisible[i-1] = m_mapVisible[i];
47  }
48  }
49  m_mapName = mapName;
50  m_mapVisible = mapVisible;
51 
52  QWidget* widget = m_stack->widget(index);
53  m_stack->removeWidget(widget);
54  widget->deleteLater();
55 
56  update();
57 }
58 
59 void TabBarInfo::setTabVisible(int index, bool visible)
60 {
61  if(m_stack->count() > index)
62  {
63  m_mapVisible[index] = visible;
64  }
65  update();
66 }
67 
68 void TabBarInfo::attach(QTabBar *tabBar, QIcon* iconCloseTab)
69 {
70  m_tabBar = tabBar;
71  m_iconCloseTab = iconCloseTab;
72  if(m_tabBar)
73  {
74  connect(m_tabBar, SIGNAL(currentChanged(int)), SLOT(on_currentChanged(int)));
75  }
76  update();
77  m_attached = true;
78 }
79 
81 {
82  m_attached = false;
83  if(m_tabBar)
84  {
85  disconnect(m_tabBar, 0, 0, 0);
86  int count = m_tabBar->count();
87  for(int i = count - 1; i >= 0; i--)
88  {
89  m_tabBar->removeTab(i);
90  }
91  m_tabBar = 0;
92  }
93 }
94 
95 void TabBarInfo::setCurrent(int index)
96 {
97  m_current = index;
98  update();
99 }
100 
102 {
103  for(int i = m_stack->count() - 1; i > 0; i--)
104  {
105  QWidget* widget = m_stack->widget(i);
106  m_stack->removeWidget(widget);
107  widget->deleteLater();
108  }
109 
110  QMap<int, QString> mapName;
111  QMap<int, bool> mapVisible;
112  mapName[0] = m_mapName[0];
113  mapVisible[0] = m_mapVisible[0];
114  m_mapName = mapName;
115  m_mapVisible = mapVisible;
116  m_current = 0;
117 
118  update();
119 }
120 
122 {
123  if(m_attached && index < m_mapTabInfo.keys().size())
124  {
125  int tab = m_mapTabInfo[index];
126  if(tab < m_stack->count())
127  {
128  m_stack->setCurrentIndex(tab);
129  }
130  m_current = tab;
131  }
132 }
133 
135 {
136  if(m_attached && m_tabBar)
137  {
138  QObject* obj = sender();
139  if(obj)
140  {
141  for(int index = 0; index < m_tabBar->count(); index++)
142  {
143  if(obj == m_tabBar->tabButton(index, QTabBar::RightSide))
144  {
145  if(index < m_mapTabInfo.keys().size())
146  {
147  int tab = m_mapTabInfo[index];
148  removeTab(tab);
149  }
150  break;
151  }
152  }
153  }
154  }
155 }
156 
158 {
159  if(m_tabBar)
160  {
161  // Populate the tab bar
162  QMap<int, int> mapTabInfo;
163  int currentTab = 0;
164  int numberTabs = m_mapName.keys().size();
165  for(int i = 0; i < numberTabs; i++)
166  {
167  bool visible = m_mapVisible[i];
168  if(visible)
169  {
170  if(m_tabBar->count() > currentTab)
171  {
172  m_tabBar->setTabText(currentTab, m_mapName[i]);
173  }
174  else
175  {
176  m_tabBar->addTab(m_mapName[i]);
177  int count = m_tabBar->count();
178  m_tabBar->setTabButton(count - 1, QTabBar::LeftSide, 0);
179  if(count == 1)
180  {
181  m_tabBar->setTabButton(0, QTabBar::RightSide, 0);
182  }
183  else
184  {
185  if(m_iconCloseTab)
186  {
187  QToolButton* tool = new QToolButton(m_tabBar);
188  tool->setIcon(*m_iconCloseTab);
189  tool->setObjectName("tabBarTool");
190  tool->setFixedSize(16, 16);
191  m_tabBar->setTabButton(count - 1, QTabBar::RightSide, tool);
192  connect(tool, SIGNAL(clicked(bool)), SLOT(on_closeButtonClick()));
193  }
194  }
195  }
196  mapTabInfo[currentTab] = i;
197  currentTab++;
198  }
199  }
200  int count = m_tabBar->count();
201  if(currentTab < count)
202  {
203  for(int i = count - 1; i >= currentTab; i--)
204  {
205  m_tabBar->removeTab(i);
206  }
207  }
208  m_mapTabInfo = mapTabInfo;
209 
210  // Set the current tab
211  int tabCurrent = m_mapTabInfo[m_tabBar->currentIndex()];
212  if(tabCurrent != m_current)
213  {
214  for(int i = 0; i < m_tabBar->count(); i++)
215  {
216  tabCurrent = m_mapTabInfo[i];
217  if(tabCurrent == m_current)
218  {
219  m_tabBar->setCurrentIndex(tabCurrent);
220  }
221  }
222  }
223  }
224 }
void on_closeButtonClick()
on_closeButtonClick Slot for close button click
Definition: tabbarinfo.cpp:134
void setTabVisible(int index, bool visible)
setTabVisible Set tab visible
Definition: tabbarinfo.cpp:59
void removeTab(int index)
removeTab Removing tab
Definition: tabbarinfo.cpp:26
TabBarInfo(QStackedWidget *parent)
TabBarInfo Constructor.
Definition: tabbarinfo.cpp:5
size_t count
Definition: ExecStats.cpp:37
void attach(QTabBar *tabBar, QIcon *iconCloseTab)
attach Attack the tab bar
Definition: tabbarinfo.cpp:68
int m_current
Definition: tabbarinfo.h:86
bool addTab(int index, const QString &name)
addTab Add new tab
Definition: tabbarinfo.cpp:14
QIcon * m_iconCloseTab
Definition: tabbarinfo.h:93
void setCurrent(int index)
setCurrent Set current index
Definition: tabbarinfo.cpp:95
QMap< int, int > m_mapTabInfo
Definition: tabbarinfo.h:89
void update()
update Update the GUI
Definition: tabbarinfo.cpp:157
void detach()
detach Detach the tab bar
Definition: tabbarinfo.cpp:80
const char * name
Definition: rest.cpp:36
void clear()
clear Remove all tabs except the first one which is not closable
Definition: tabbarinfo.cpp:101
void on_currentChanged(int index)
on_currentChanged Slot for changing the tab bar index
Definition: tabbarinfo.cpp:121
bool m_attached
Definition: tabbarinfo.h:92
QMap< int, QString > m_mapName
Definition: tabbarinfo.h:87
QMap< int, bool > m_mapVisible
Definition: tabbarinfo.h:88
QStackedWidget * m_stack
Definition: tabbarinfo.h:90
QTabBar * m_tabBar
Definition: tabbarinfo.h:91