summaryrefslogtreecommitdiff
path: root/noncore/settings/sysinfo/otabwidget.cpp
blob: 9fe6c4b62a969e55fe8c6703237feccbe0b1b5aa (plain)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**********************************************************************
** OTabWidget
**
** Modified tab widget control
**
** Copyright (C) 2002, Dan Williams
**                    williamsdr@acm.org
**                    http://draknor.net
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
**********************************************************************/

#include "otabwidget.h"

#include <qpe/config.h>
#include <qpe/resource.h>

#include <qcombobox.h>
#include <qtabbar.h>
#include <qwidgetstack.h>

OTabWidget::OTabWidget( QWidget *parent, const char *name = 0x0,
                        TabStyle s = Global, TabPosition p = Top )
    : QWidget( parent, name )
{
    if ( s == Global )
    {
        Config config( "qpe" );
        config.setGroup( "Appearance" );
        tabBarStyle = ( TabStyle ) config.readNumEntry( "TabStyle", (int) IconTab );
        if ( tabBarStyle <= Global || tabBarStyle > IconList)
        {
            tabBarStyle = IconTab;
        }
        QString pos = config.readEntry( "TabPosition", "Top");
        if ( pos == "Top" )
        {
            tabBarPosition = Top;
        }
        else
        {
            tabBarPosition = Bottom;
        }
    }
    else
    {
        tabBarStyle = s;
        tabBarPosition = p;
    }

    widgetStack = new QWidgetStack( this, "widgetstack" );
    widgetStack->setFrameStyle( QFrame::StyledPanel | QFrame::Raised );
    widgetStack->setLineWidth( style().defaultFrameWidth() );

    tabBarStack = new QWidgetStack( this, "tabbarstack" );

    tabBar = new QTabBar( tabBarStack, "tabbar" );
    tabBarStack->addWidget( tabBar, 0 );
    connect( tabBar, SIGNAL( selected( int ) ), this, SLOT( slotTabBarSelected( int ) ) );

    tabList = new QComboBox( false, tabBarStack, "tablist" );
    tabBarStack->addWidget( tabList, 1 );
    connect( tabList, SIGNAL( activated( int ) ), this, SLOT( slotTabListSelected( int ) ) );

    if ( tabBarStyle == TextTab || tabBarStyle == IconTab )
    {
        tabBarStack->raiseWidget( tabBar );
    }
    else if ( tabBarStyle == TextList || tabBarStyle == IconList )
    {
        tabBarStack->raiseWidget( tabList );
    }

    if ( tabBarPosition == Bottom )
    {
        tabBar->setShape( QTabBar::RoundedBelow );
    }
	
    currentTab= 0x0;
}

OTabWidget::~OTabWidget()
{
}

void OTabWidget::addTab( QWidget *child, const QString &icon, const QString &label )
{
    QPixmap iconset = loadSmooth( icon );

    // Add to tabBar
    QTab * tab = new QTab();
    if ( tabBarStyle == IconTab )
    {
        tab->label = QString::null;
    }
    else
    {
        tab->label = label;
    }
    if ( tabBarStyle == IconTab || tabBarStyle == IconList)
    {
        tab->iconset = new QIconSet( iconset );
    }
    int tabid = tabBar->addTab( tab );

    // Add to tabList
    if ( tabBarStyle == IconTab || tabBarStyle == IconList )
    {
        tabList->insertItem( iconset, label, -1 );
    }
    else
    {
        tabList->insertItem( label );
    }

    // Add child to widget list
    widgetStack->addWidget( child, tabid );
    widgetStack->raiseWidget( child );

    // Save tab information
    TabInfo *tabinfo = new TabInfo( tabid, child, icon, label );
    tabs.append( tabinfo );
    selectTab( tabinfo );

//    setUpLayout();
}

void OTabWidget::setCurrentTab( QWidget *childwidget )
{
    TabInfo *newtab = tabs.first();
    while ( newtab && newtab->control() != childwidget )
    {
        newtab = tabs.next();
    }
    if ( newtab && newtab->control() == childwidget )
    {
        selectTab( newtab );
    }
}

void OTabWidget::setCurrentTab( QString tabname )
{
    TabInfo *newtab = tabs.first();
    while ( newtab && newtab->label() != tabname )
    {
        newtab = tabs.next();
    }
    if ( newtab && newtab->label() == tabname )
    {
        selectTab( newtab );
    }
}

OTabWidget::TabStyle OTabWidget::tabStyle() const
{
    return tabBarStyle;
}

void OTabWidget::setTabStyle( TabStyle s )
{
    tabBarStyle = s;
}

OTabWidget::TabPosition OTabWidget::tabPosition() const
{
    return tabBarPosition;
}

void OTabWidget::setTabPosition( TabPosition p )
{
    tabBarPosition = p;
}

void OTabWidget::slotTabBarSelected( int id )
{
    TabInfo *newtab = tabs.first();
    while ( newtab && newtab->id() != id )
    {
        newtab = tabs.next();
    }
    if ( newtab && newtab->id() == id )
    {
        selectTab( newtab );
    }
}

void OTabWidget::slotTabListSelected( int index )
{
    TabInfo *newtab = tabs.at( index );
    if ( newtab )
    {
        selectTab( newtab );
    }
}

QPixmap OTabWidget::loadSmooth( const QString &name )
{
    QImage image = Resource::loadImage( name );
    QPixmap pixmap;
    pixmap.convertFromImage( image.smoothScale( 16, 16 ) );
    return pixmap;
}

void OTabWidget::selectTab( TabInfo *tab )
{
    if ( tabBarStyle == IconTab )
    {
        if ( currentTab )
        {
            tabBar->tab( currentTab->id() )->setText( QString::null );
            setUpLayout();
        }
        tabBar->tab( tab->id() )->setText( tab->label() );
        currentTab = tab;
    }
    tabBar->setCurrentTab( tab->id() );
    setUpLayout();
    tabBar->update();

    widgetStack->raiseWidget( tab->control() );
}

void OTabWidget::setUpLayout()
{
    tabBar->layoutTabs();
    QSize t( tabBarStack->sizeHint() );
    if ( t.width() > width() )
        t.setWidth( width() );
    int lw = widgetStack->lineWidth();
    if ( tabBarPosition == Bottom )
    {
        tabBarStack->setGeometry( QMAX(0, lw-2), height() - t.height() - lw, t.width(), t.height() );
        widgetStack->setGeometry( 0, 0, width(), height()-t.height()+QMAX(0, lw-2) );
    }
    else
    { // Top
        tabBarStack->setGeometry( QMAX(0, lw-2), 0, t.width(), t.height() );
        widgetStack->setGeometry( 0, t.height()-lw, width(), height()-t.height()+QMAX(0, lw-2));
    }

//    if ( !onlyCheck )
//        update();
    if ( autoMask() )
        updateMask();
}

QSize OTabWidget::sizeHint() const
{
    QSize s( widgetStack->sizeHint() );
    QSize t( tabBarStack->sizeHint() );
    return QSize( QMAX( s.width(), t.width()), s.height() + t.height() );
}

void OTabWidget::resizeEvent( QResizeEvent * )
{
    setUpLayout();
}