forked from nitramr/indigoDock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indigotabbar.cpp
208 lines (143 loc) · 4.81 KB
/
indigotabbar.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "indigotabbar.h"
#include <QDebug>
#include <QWheelEvent>
#include <QStyle>
IndigoTabbar::IndigoTabbar(QWidget *parent) :
QTabWidget(parent)
{
iconScale = 32;
this->setObjectName("indigoTab");
this->tabBar()->setFocusPolicy(Qt::NoFocus);
this->tabBar()->setIconSize(QSize(iconScale,iconScale));
// QPainter painter(this);
//style()->drawControl(QStyle::CT_TabBarTab, QStyleOptionTab, painter,this->tabBar());
QString styleSheet("QTabWidget::pane {"
"border: 0px solid transparent;"
"}"
"QTabWidget::tab-bar{"
"alignment: left;"
"}"
"QTabBar::tab {"
"text-align: center;"
"border: 0px solid transparent;"
"border-radius: 0px;"
"width: %1 px;"
"height: %1 px;"
"padding: 0px;"
"}"
"QTabBar::tab:selected {"
"background-color: rgb(153,153,153);"
"}"
);
this->setStyleSheet( styleSheet.arg(iconScale) );
// watch active tab change event
connect(this->tabBar(), SIGNAL(currentChanged(int)), this, SLOT(setActiveTab(int)));
}
void IndigoTabbar::addTab( QWidget * child, const QString & label ){
QTabWidget::addTab(child, label);
m_activeWidget = this->widget(currentIndex());
}
void IndigoTabbar::addTab ( QWidget * child, const QIcon & icon, const QString & label ){
// rotate icon if needed
QIcon ricon = rotateIcon(icon);
// use normal addTab function
QTabWidget::addTab(child, ricon, label);
m_activeWidget = this->widget(currentIndex());
}
void IndigoTabbar::addIndigoPanel(IndigoPanel *panel, int tabIndex ){
IndigoDropZone * zone = new IndigoDropZone(0);
zone->addPanel(panel);
static const QIcon &icon = panel->Icon();
// new tab
if (tabIndex == -1){
this->connect(panel, SIGNAL(mouseReleased()), zone, SLOT(dropPanel()));
this->connect(panel, SIGNAL(mouseMove()), zone, SLOT(hoverZone()));
addTab ( zone, icon, "" );
// exsisting tab
}else{
if(tabIndex >= 0 && tabIndex <= this->count() - 1){
QWidget * widget = this->widget(tabIndex);
IndigoDropZone *zone = qobject_cast<IndigoDropZone*>(widget);
if(!zone) return;
zone->addPanel(panel);
this->connect(panel, SIGNAL(mouseReleased()), zone, SLOT(dropPanel()));
this->connect(panel, SIGNAL(mouseMove()), zone, SLOT(hoverZone()));
// m_activeWidget = this->widget(currentIndex()); // not need for exsisting tabs
}
}
}
void IndigoTabbar::setActiveTab(int index){
m_activeWidget = this->widget(index);
//qDebug() << "TabIndexChange" << m_activeWidget << " index: " << index << endl;
}
void IndigoTabbar::setTabPosition(TabPosition tabPos){
// Rotate icons in right direction
for(int i=0;i<count();i++)
{
QIcon icon = this->tabBar()->tabIcon(i);
icon = rotateIcon(icon, tabPos);
this->tabBar()->setTabIcon(i, icon);
}
// set new tab position
QTabWidget::setTabPosition(tabPos);
}
/******************
*
* Helper
*
* ***************/
QIcon IndigoTabbar::rotateIcon(const QIcon &icon, TabPosition tabPos){
QSize sz;
for (int var = 0; var < icon.availableSizes().count(); ++var) {
if (icon.availableSizes().at(var).width() > sz.width())
sz = icon.availableSizes().at(var);
}
QPixmap pix = icon.pixmap(sz);
QTransform trans;
switch(this->tabPosition()){
case QTabWidget::East:{
switch(tabPos){
case QTabWidget::West:{
trans.rotate(180);
break;
}
case QTabWidget::North:{
trans.rotate(-90);
break;
}
case QTabWidget::South:{
trans.rotate(+90);
break;
}
default:
break;
}
break;
}
case QTabWidget::West:{
switch(tabPos){
case QTabWidget::East:{
trans.rotate(180);
break;
}
case QTabWidget::North:{
trans.rotate(+90);
break;
}
case QTabWidget::South:{
trans.rotate(-90);
break;
}
default:
break;
}
break;
}
default:
break;
}
pix = pix.transformed(trans);
pix = pix.scaledToWidth( iconScale );
pix = pix.scaledToHeight( iconScale );
return QIcon(pix);
}