author | mickeyl <mickeyl> | 2004-01-15 15:04:23 (UTC) |
---|---|---|
committer | mickeyl <mickeyl> | 2004-01-15 15:04:23 (UTC) |
commit | ac1e2b945965ee8caabd658e90f9e234fc622619 (patch) (unidiff) | |
tree | d787cb386c6b1b5f69cb00809636299e66c56833 /libopie2/opieui/otabwidget.cpp | |
parent | dde789ef19fa3a3913805e452ac1e3400688e8a0 (diff) | |
download | opie-ac1e2b945965ee8caabd658e90f9e234fc622619.zip opie-ac1e2b945965ee8caabd658e90f9e234fc622619.tar.gz opie-ac1e2b945965ee8caabd658e90f9e234fc622619.tar.bz2 |
libopie1 --> libopie2 + namespace cleanups, code layout, etc.
Diffstat (limited to 'libopie2/opieui/otabwidget.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libopie2/opieui/otabwidget.cpp | 423 |
1 files changed, 423 insertions, 0 deletions
diff --git a/libopie2/opieui/otabwidget.cpp b/libopie2/opieui/otabwidget.cpp new file mode 100644 index 0000000..ec6af9d --- a/dev/null +++ b/libopie2/opieui/otabwidget.cpp | |||
@@ -0,0 +1,423 @@ | |||
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 Library 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 <opie2/otabwidget.h> | ||
33 | |||
34 | /* OPIE */ | ||
35 | #include <qpe/applnk.h> | ||
36 | #include <qpe/config.h> | ||
37 | #include <qpe/resource.h> | ||
38 | #include <opie2/otabbar.h> | ||
39 | |||
40 | /* QT */ | ||
41 | #include <qcombobox.h> | ||
42 | #include <qwidgetstack.h> | ||
43 | |||
44 | using namespace Opie; | ||
45 | |||
46 | OTabWidget::OTabWidget( QWidget *parent, const char *name, TabStyle s, TabPosition p ) | ||
47 | : QWidget( parent, name ) | ||
48 | { | ||
49 | if ( s == Global ) | ||
50 | { | ||
51 | Config config( "qpe" ); | ||
52 | config.setGroup( "Appearance" ); | ||
53 | s = ( TabStyle ) config.readNumEntry( "TabStyle", (int) IconTab ); | ||
54 | if ( s <= Global || s > IconList) | ||
55 | { | ||
56 | s = IconTab; | ||
57 | } | ||
58 | QString pos = config.readEntry( "TabPosition", "Top"); | ||
59 | if ( pos == "Bottom" ) | ||
60 | { | ||
61 | p = Bottom; | ||
62 | } | ||
63 | else | ||
64 | { | ||
65 | p = Top; | ||
66 | } | ||
67 | } | ||
68 | |||
69 | widgetStack = new QWidgetStack( this, "widgetstack" ); | ||
70 | widgetStack->setFrameStyle( QFrame::NoFrame ); | ||
71 | widgetStack->setLineWidth( style().defaultFrameWidth() ); | ||
72 | |||
73 | tabBarStack = new QWidgetStack( this, "tabbarstack" ); | ||
74 | |||
75 | tabBar = new OTabBar( 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 | tabBarPosition = p; | ||
84 | setTabStyle( s ); | ||
85 | setTabPosition( p ); | ||
86 | |||
87 | currTab= 0x0; | ||
88 | } | ||
89 | |||
90 | OTabWidget::~OTabWidget() | ||
91 | {} | ||
92 | |||
93 | void OTabWidget::addTab( QWidget *child, const QString &icon, const QString &label ) | ||
94 | { | ||
95 | QPixmap iconset = loadSmooth( icon ); | ||
96 | |||
97 | QTab *tab = new QTab(); | ||
98 | if ( tabBarStyle == IconTab ) | ||
99 | { | ||
100 | tab->label = QString::null; | ||
101 | } | ||
102 | else | ||
103 | { | ||
104 | tab->label = label; | ||
105 | } | ||
106 | if ( tabBarStyle == IconTab || tabBarStyle == IconList ) | ||
107 | { | ||
108 | tab->iconset = new QIconSet( iconset ); | ||
109 | } | ||
110 | int tabid = tabBar->addTab( tab ); | ||
111 | |||
112 | if ( tabBarStyle == IconTab || tabBarStyle == IconList ) | ||
113 | { | ||
114 | tabList->insertItem( iconset, label, -1 ); | ||
115 | } | ||
116 | else | ||
117 | { | ||
118 | tabList->insertItem( label ); | ||
119 | } | ||
120 | |||
121 | widgetStack->addWidget( child, tabid ); | ||
122 | widgetStack->raiseWidget( child ); | ||
123 | widgetStack->setFrameStyle( QFrame::StyledPanel | QFrame::Raised ); | ||
124 | |||
125 | OTabInfo *tabinfo = new OTabInfo( tabid, child, icon, label ); | ||
126 | tabs.append( tabinfo ); | ||
127 | selectTab( tabinfo ); | ||
128 | } | ||
129 | |||
130 | void OTabWidget::removePage( QWidget *childwidget ) | ||
131 | { | ||
132 | if ( childwidget ) | ||
133 | { | ||
134 | OTabInfo *tab = tabs.first(); | ||
135 | while ( tab && tab->control() != childwidget ) | ||
136 | { | ||
137 | tab = tabs.next(); | ||
138 | } | ||
139 | if ( tab && tab->control() == childwidget ) | ||
140 | { | ||
141 | tabBar->setTabEnabled( tab->id(), FALSE ); | ||
142 | tabBar->removeTab( tabBar->tab( tab->id() ) ); | ||
143 | int i = 0; | ||
144 | while ( i < tabList->count() && tabList->text( i ) != tab->label() ) | ||
145 | { | ||
146 | i++; | ||
147 | } | ||
148 | if ( tabList->text( i ) == tab->label() ) | ||
149 | { | ||
150 | tabList->removeItem( i ); | ||
151 | } | ||
152 | widgetStack->removeWidget( childwidget ); | ||
153 | tabs.remove( tab ); | ||
154 | delete tab; | ||
155 | currTab = tabs.current(); | ||
156 | if ( !currTab ) | ||
157 | { | ||
158 | widgetStack->setFrameStyle( QFrame::NoFrame ); | ||
159 | } | ||
160 | |||
161 | setUpLayout(); | ||
162 | } | ||
163 | } | ||
164 | } | ||
165 | |||
166 | void OTabWidget::changeTab( QWidget *widget, const QString &iconset, const QString &label) | ||
167 | { | ||
168 | OTabInfo *currtab = tabs.first(); | ||
169 | while ( currtab && currtab->control() != widget ) | ||
170 | { | ||
171 | currtab = tabs.next(); | ||
172 | } | ||
173 | if ( currtab && currtab->control() == widget ) | ||
174 | { | ||
175 | QTab *tab = tabBar->tab( currtab->id() ); | ||
176 | QPixmap icon( loadSmooth( iconset ) ); | ||
177 | tab->setText( label ); | ||
178 | if ( tabBarStyle == IconTab ) | ||
179 | tab->setIconSet( icon ); | ||
180 | int i = 0; | ||
181 | while ( i < tabList->count() && tabList->text( i ) != currtab->label() ) | ||
182 | { | ||
183 | i++; | ||
184 | } | ||
185 | if ( i < tabList->count() && tabList->text( i ) == currtab->label() ) | ||
186 | { | ||
187 | if ( tabBarStyle == IconTab || tabBarStyle == IconList ) | ||
188 | { | ||
189 | tabList->changeItem( icon, label, i ); | ||
190 | } | ||
191 | else | ||
192 | { | ||
193 | tabList->changeItem( label, i ); | ||
194 | } | ||
195 | } | ||
196 | currtab->setLabel( label ); | ||
197 | currtab->setIcon( iconset ); | ||
198 | } | ||
199 | setUpLayout(); | ||
200 | } | ||
201 | |||
202 | void OTabWidget::setCurrentTab( QWidget *childwidget ) | ||
203 | { | ||
204 | OTabInfo *currtab = tabs.first(); | ||
205 | while ( currtab && currtab->control() != childwidget ) | ||
206 | { | ||
207 | currtab = tabs.next(); | ||
208 | } | ||
209 | if ( currtab && currtab->control() == childwidget ) | ||
210 | { | ||
211 | selectTab( currtab ); | ||
212 | } | ||
213 | } | ||
214 | |||
215 | void OTabWidget::setCurrentTab( const QString &tabname ) | ||
216 | { | ||
217 | OTabInfo *newtab = tabs.first(); | ||
218 | while ( newtab && newtab->label() != tabname ) | ||
219 | { | ||
220 | newtab = tabs.next(); | ||
221 | } | ||
222 | if ( newtab && newtab->label() == tabname ) | ||
223 | { | ||
224 | selectTab( newtab ); | ||
225 | } | ||
226 | } | ||
227 | |||
228 | void OTabWidget::setCurrentTab(int tabindex) | ||
229 | { | ||
230 | OTabInfo *newtab = tabs.first(); | ||
231 | while ( newtab && newtab->id() != tabindex ) | ||
232 | { | ||
233 | newtab = tabs.next(); | ||
234 | } | ||
235 | if ( newtab && newtab->id() == tabindex ) | ||
236 | { | ||
237 | selectTab( newtab ); | ||
238 | } | ||
239 | } | ||
240 | |||
241 | |||
242 | OTabWidget::TabStyle OTabWidget::tabStyle() const | ||
243 | { | ||
244 | return tabBarStyle; | ||
245 | } | ||
246 | |||
247 | void OTabWidget::setTabStyle( TabStyle s ) | ||
248 | { | ||
249 | tabBarStyle = s; | ||
250 | if ( tabBarStyle == TextTab || tabBarStyle == IconTab ) | ||
251 | { | ||
252 | QTab *currtab; | ||
253 | for ( OTabInfo *tabinfo = tabs.first(); tabinfo; tabinfo = tabs.next() ) | ||
254 | { | ||
255 | currtab = tabBar->tab( tabinfo->id() ); | ||
256 | if ( tabBarStyle == IconTab ) | ||
257 | { | ||
258 | currtab->iconset = new QIconSet( loadSmooth( tabinfo->icon() ) ); | ||
259 | if ( tabinfo == currTab ) | ||
260 | currtab->setText( tabinfo->label() ); | ||
261 | else | ||
262 | currtab->setText( QString::null ); | ||
263 | } | ||
264 | else | ||
265 | { | ||
266 | currtab->iconset = 0x0; | ||
267 | currtab->setText( tabinfo->label() ); | ||
268 | } | ||
269 | } | ||
270 | tabBarStack->raiseWidget( tabBar ); | ||
271 | } | ||
272 | else if ( tabBarStyle == TextList || tabBarStyle == IconList ) | ||
273 | { | ||
274 | tabList->clear(); | ||
275 | for ( OTabInfo *tabinfo = tabs.first(); tabinfo; tabinfo = tabs.next() ) | ||
276 | { | ||
277 | if ( tabBarStyle == IconList ) | ||
278 | { | ||
279 | tabList->insertItem( loadSmooth( tabinfo->icon() ), tabinfo->label() ); | ||
280 | } | ||
281 | else | ||
282 | { | ||
283 | tabList->insertItem( tabinfo->label() ); | ||
284 | } | ||
285 | } | ||
286 | tabBarStack->raiseWidget( tabList ); | ||
287 | } | ||
288 | setUpLayout(); | ||
289 | } | ||
290 | |||
291 | OTabWidget::TabPosition OTabWidget::tabPosition() const | ||
292 | { | ||
293 | return tabBarPosition; | ||
294 | } | ||
295 | |||
296 | void OTabWidget::setTabPosition( TabPosition p ) | ||
297 | { | ||
298 | tabBarPosition = p; | ||
299 | if ( tabBarPosition == Top ) | ||
300 | { | ||
301 | tabBar->setShape( QTabBar::RoundedAbove ); | ||
302 | } | ||
303 | else | ||
304 | { | ||
305 | tabBar->setShape( QTabBar::RoundedBelow ); | ||
306 | } | ||
307 | setUpLayout(); | ||
308 | } | ||
309 | |||
310 | void OTabWidget::slotTabBarSelected( int id ) | ||
311 | { | ||
312 | OTabInfo *newtab = tabs.first(); | ||
313 | while ( newtab && newtab->id() != id ) | ||
314 | { | ||
315 | newtab = tabs.next(); | ||
316 | } | ||
317 | if ( newtab && newtab->id() == id ) | ||
318 | { | ||
319 | selectTab( newtab ); | ||
320 | } | ||
321 | } | ||
322 | |||
323 | void OTabWidget::slotTabListSelected( int index ) | ||
324 | { | ||
325 | OTabInfo *newtab = tabs.at( index ); | ||
326 | if ( newtab ) | ||
327 | { | ||
328 | selectTab( newtab ); | ||
329 | } | ||
330 | } | ||
331 | |||
332 | QPixmap OTabWidget::loadSmooth( const QString &name ) | ||
333 | { | ||
334 | QPixmap p; | ||
335 | p.convertFromImage( Resource::loadImage( name ).smoothScale( AppLnk::smallIconSize(), AppLnk::smallIconSize() ) ); | ||
336 | return p; | ||
337 | } | ||
338 | |||
339 | void OTabWidget::selectTab( OTabInfo *tab ) | ||
340 | { | ||
341 | if ( tabBarStyle == IconTab ) | ||
342 | { | ||
343 | if ( currTab ) | ||
344 | { | ||
345 | tabBar->tab( currTab->id() )->setText( QString::null ); | ||
346 | setUpLayout(); | ||
347 | } | ||
348 | tabBar->tab( tab->id() )->setText( tab->label() ); | ||
349 | tabBar->setCurrentTab( tab->id() ); | ||
350 | setUpLayout(); | ||
351 | tabBar->update(); | ||
352 | } | ||
353 | else | ||
354 | { | ||
355 | tabBar->setCurrentTab( tab->id() ); | ||
356 | } | ||
357 | |||
358 | widgetStack->raiseWidget( tab->control() ); | ||
359 | |||
360 | emit currentChanged( tab->control() ); | ||
361 | |||
362 | currTab = tab; | ||
363 | } | ||
364 | |||
365 | void OTabWidget::setUpLayout() | ||
366 | { | ||
367 | tabBar->layoutTabs(); | ||
368 | QSize t( tabBarStack->sizeHint() ); | ||
369 | if ( tabBarStyle == IconTab ) | ||
370 | { | ||
371 | if ( t.width() > width() ) | ||
372 | t.setWidth( width() ); | ||
373 | } | ||
374 | else | ||
375 | { | ||
376 | t.setWidth( width() ); | ||
377 | } | ||
378 | int lw = widgetStack->lineWidth(); | ||
379 | if ( tabBarPosition == Bottom ) | ||
380 | { | ||
381 | tabBarStack->setGeometry( QMAX(0, lw-2), height() - t.height() - lw, t.width(), t.height() ); | ||
382 | widgetStack->setGeometry( 0, 0, width(), height()-t.height()+QMAX(0, lw-2) ); | ||
383 | } | ||
384 | else | ||
385 | { | ||
386 | tabBarStack->setGeometry( QMAX(0, lw-2), 0, t.width(), t.height() ); | ||
387 | widgetStack->setGeometry( 0, t.height()-lw, width(), height()-t.height()+QMAX( 0, lw-2 ) ); | ||
388 | } | ||
389 | |||
390 | if ( autoMask() ) | ||
391 | updateMask(); | ||
392 | } | ||
393 | |||
394 | QSize OTabWidget::sizeHint() const | ||
395 | { | ||
396 | QSize s( widgetStack->sizeHint() ); | ||
397 | QSize t( tabBarStack->sizeHint() ); | ||
398 | return QSize( QMAX( s.width(), t.width() ), s.height() + t.height() ); | ||
399 | } | ||
400 | |||
401 | void OTabWidget::resizeEvent( QResizeEvent * ) | ||
402 | { | ||
403 | setUpLayout(); | ||
404 | } | ||
405 | |||
406 | int OTabWidget::currentTab() | ||
407 | { | ||
408 | if ( currTab ) | ||
409 | { | ||
410 | return currTab->id(); | ||
411 | } | ||
412 | return -1; | ||
413 | } | ||
414 | |||
415 | QWidget* OTabWidget::currentWidget()const | ||
416 | { | ||
417 | if ( currTab ) | ||
418 | { | ||
419 | return currTab->control(); | ||
420 | } | ||
421 | |||
422 | return 0; | ||
423 | } | ||