summaryrefslogtreecommitdiff
path: root/libopie/otabwidget.cpp
Unidiff
Diffstat (limited to 'libopie/otabwidget.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie/otabwidget.cpp267
1 files changed, 267 insertions, 0 deletions
diff --git a/libopie/otabwidget.cpp b/libopie/otabwidget.cpp
new file mode 100644
index 0000000..6e10be9
--- a/dev/null
+++ b/libopie/otabwidget.cpp
@@ -0,0 +1,267 @@
1/*
2                This file is part of the Opie Project
3
4              Copyright (c) 2002 Dan Williams <williamsdr@acm.org>
5 =.
6 .=l.
7           .>+-=
8 _;:,     .>    :=|. This program is free software; you can
9.> <`_,   >  .   <= redistribute it and/or modify it under
10:`=1 )Y*s>-.--   : the terms of the GNU General Public
11.="- .-=="i,     .._ License as published by the Free Software
12 - .   .-<_>     .<> Foundation; either version 2 of the License,
13     ._= =}       : or (at your option) any later version.
14    .%`+i>       _;_.
15    .i_,=:_.      -<s. This program is distributed in the hope that
16     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY;
17    : ..    .:,     . . . without even the implied warranty of
18    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A
19  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU
20..}^=.=       =       ; Library General Public License for more
21++=   -.     .`     .: details.
22 :     =  ...= . :.=-
23 -.   .:....=;==+<; You should have received a copy of the GNU
24  -_. . .   )=.  = Library General Public License along with
25    --        :-=` this library; see the file COPYING.LIB.
26 If not, write to the Free Software Foundation,
27 Inc., 59 Temple Place - Suite 330,
28 Boston, MA 02111-1307, USA.
29
30*/
31
32#include "otabwidget.h"
33
34#include <qpe/config.h>
35#include <qpe/resource.h>
36
37#include <qcombobox.h>
38#include <qtabbar.h>
39#include <qwidgetstack.h>
40
41OTabWidget::OTabWidget( QWidget *parent, const char *name, TabStyle s, TabPosition p )
42 : QWidget( parent, name )
43{
44 if ( s == Global )
45 {
46 Config config( "qpe" );
47 config.setGroup( "Appearance" );
48 tabBarStyle = ( TabStyle ) config.readNumEntry( "TabStyle", (int) IconTab );
49 if ( tabBarStyle <= Global || tabBarStyle > IconList)
50 {
51 tabBarStyle = IconTab;
52 }
53 QString pos = config.readEntry( "TabPosition", "Top");
54 if ( pos == "Bottom" )
55 {
56 tabBarPosition = Bottom;
57 }
58 else
59 {
60 tabBarPosition = Top;
61 }
62 }
63 else
64 {
65 tabBarStyle = s;
66 tabBarPosition = p;
67 }
68
69 widgetStack = new QWidgetStack( this, "widgetstack" );
70 widgetStack->setFrameStyle( QFrame::StyledPanel | QFrame::Raised );
71 widgetStack->setLineWidth( style().defaultFrameWidth() );
72
73 tabBarStack = new QWidgetStack( this, "tabbarstack" );
74
75 tabBar = new QTabBar( tabBarStack, "tabbar" );
76 tabBarStack->addWidget( tabBar, 0 );
77 connect( tabBar, SIGNAL( selected( int ) ), this, SLOT( slotTabBarSelected( int ) ) );
78
79 tabList = new QComboBox( false, tabBarStack, "tablist" );
80 tabBarStack->addWidget( tabList, 1 );
81 connect( tabList, SIGNAL( activated( int ) ), this, SLOT( slotTabListSelected( int ) ) );
82
83 if ( tabBarStyle == TextTab || tabBarStyle == IconTab )
84 {
85 tabBarStack->raiseWidget( tabBar );
86 }
87 else if ( tabBarStyle == TextList || tabBarStyle == IconList )
88 {
89 tabBarStack->raiseWidget( tabList );
90 }
91
92 if ( tabBarPosition == Bottom )
93 {
94 tabBar->setShape( QTabBar::RoundedBelow );
95 }
96
97 currentTab= 0x0;
98}
99
100OTabWidget::~OTabWidget()
101{
102}
103
104void OTabWidget::addTab( QWidget *child, const QString &icon, const QString &label )
105{
106 QPixmap iconset = loadSmooth( icon );
107
108 QTab * tab = new QTab();
109 if ( tabBarStyle == IconTab )
110 {
111 tab->label = QString::null;
112 }
113 else
114 {
115 tab->label = label;
116 }
117 if ( tabBarStyle == IconTab || tabBarStyle == IconList)
118 {
119 tab->iconset = new QIconSet( iconset );
120 }
121 int tabid = tabBar->addTab( tab );
122
123 if ( tabBarStyle == IconTab || tabBarStyle == IconList )
124 {
125 tabList->insertItem( iconset, label, -1 );
126 }
127 else
128 {
129 tabList->insertItem( label );
130 }
131
132 widgetStack->addWidget( child, tabid );
133 widgetStack->raiseWidget( child );
134
135 OTabInfo *tabinfo = new OTabInfo( tabid, child, icon, label );
136 tabs.append( tabinfo );
137 selectTab( tabinfo );
138}
139
140void OTabWidget::setCurrentTab( QWidget *childwidget )
141{
142 OTabInfo *newtab = tabs.first();
143 while ( newtab && newtab->control() != childwidget )
144 {
145 newtab = tabs.next();
146 }
147 if ( newtab && newtab->control() == childwidget )
148 {
149 selectTab( newtab );
150 }
151}
152
153void OTabWidget::setCurrentTab( const QString &tabname )
154{
155 OTabInfo *newtab = tabs.first();
156 while ( newtab && newtab->label() != tabname )
157 {
158 newtab = tabs.next();
159 }
160 if ( newtab && newtab->label() == tabname )
161 {
162 selectTab( newtab );
163 }
164}
165
166OTabWidget::TabStyle OTabWidget::tabStyle() const
167{
168 return tabBarStyle;
169}
170
171void OTabWidget::setTabStyle( TabStyle s )
172{
173 tabBarStyle = s;
174}
175
176OTabWidget::TabPosition OTabWidget::tabPosition() const
177{
178 return tabBarPosition;
179}
180
181void OTabWidget::setTabPosition( TabPosition p )
182{
183 tabBarPosition = p;
184}
185
186void OTabWidget::slotTabBarSelected( int id )
187{
188 OTabInfo *newtab = tabs.first();
189 while ( newtab && newtab->id() != id )
190 {
191 newtab = tabs.next();
192 }
193 if ( newtab && newtab->id() == id )
194 {
195 selectTab( newtab );
196 }
197}
198
199void OTabWidget::slotTabListSelected( int index )
200{
201 OTabInfo *newtab = tabs.at( index );
202 if ( newtab )
203 {
204 selectTab( newtab );
205 }
206}
207
208QPixmap OTabWidget::loadSmooth( const QString &name )
209{
210 QImage image = Resource::loadImage( name );
211 QPixmap pixmap;
212 pixmap.convertFromImage( image.smoothScale( 16, 16 ) );
213 return pixmap;
214}
215
216void OTabWidget::selectTab( OTabInfo *tab )
217{
218 if ( tabBarStyle == IconTab )
219 {
220 if ( currentTab )
221 {
222 tabBar->tab( currentTab->id() )->setText( QString::null );
223 setUpLayout();
224 }
225 tabBar->tab( tab->id() )->setText( tab->label() );
226 currentTab = tab;
227 }
228 tabBar->setCurrentTab( tab->id() );
229 setUpLayout();
230 tabBar->update();
231
232 widgetStack->raiseWidget( tab->control() );
233}
234
235void OTabWidget::setUpLayout()
236{
237 tabBar->layoutTabs();
238 QSize t( tabBarStack->sizeHint() );
239 if ( t.width() > width() )
240 t.setWidth( width() );
241 int lw = widgetStack->lineWidth();
242 if ( tabBarPosition == Bottom )
243 {
244 tabBarStack->setGeometry( QMAX(0, lw-2), height() - t.height() - lw, t.width(), t.height() );
245 widgetStack->setGeometry( 0, 0, width(), height()-t.height()+QMAX(0, lw-2) );
246 }
247 else
248 {
249 tabBarStack->setGeometry( QMAX(0, lw-2), 0, t.width(), t.height() );
250 widgetStack->setGeometry( 0, t.height()-lw, width(), height()-t.height()+QMAX(0, lw-2));
251 }
252
253 if ( autoMask() )
254 updateMask();
255}
256
257QSize OTabWidget::sizeHint() const
258{
259 QSize s( widgetStack->sizeHint() );
260 QSize t( tabBarStack->sizeHint() );
261 return QSize( QMAX( s.width(), t.width()), s.height() + t.height() );
262}
263
264void OTabWidget::resizeEvent( QResizeEvent * )
265{
266 setUpLayout();
267}