summaryrefslogtreecommitdiff
path: root/libopie2/opieui/otabwidget.cpp
blob: 7103884d5fb737c9dd256b938096cf0774aa080a (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
                             This file is part of the Opie Project

                             Copyright (C) 2002, 2005 Dan Williams <drw@handhelds.org>
              =.
            .=l.
           .>+-=
 _;:,     .>    :=|.         This program is free software; you can
.> <`_,   >  .   <=          redistribute it and/or  modify it under
:`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
.="- .-=="i,     .._         License as published by the Free Software
 - .   .-<_>     .<>         Foundation; either version 2 of the License,
     ._= =}       :          or (at your option) any later version.
    .%`+i>       _;_.
    .i_,=:_.      -<s.       This program is distributed in the hope that
     +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
    : ..    .:,     . . .    without even the implied warranty of
    =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
  _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
..}^=.=       =       ;      Library General Public License for more
++=   -.     .`     .:       details.
    :     =  ...= . :.=-
 -.   .:....=;==+<;          You should have received a copy of the GNU
  -_. . .   )=.  =           Library General Public License along with
    --        :-=`           this library; see the file COPYING.LIB.
                             If not, write to the Free Software Foundation,
                             Inc., 59 Temple Place - Suite 330,
                             Boston, MA 02111-1307, USA.
*/

#include <opie2/otabwidget.h>

/* OPIE */
#include <opie2/oresource.h>
#include <opie2/otabbar.h>

#include <qpe/config.h>

/* QT */
#include <qcombobox.h>
#include <qwidgetstack.h>

using namespace Opie::Ui;

OTabWidget::OTabWidget( QWidget *parent, const char *name, TabStyle s, TabPosition p )
        : QWidget( parent, name )
        , m_currTab( 0l )
        , m_tabBarStyle( Global )
        , m_tabBarPosition( Top )
        , m_usingTabs( true )
        , m_tabBar( 0l )
        , m_tabList( 0l )
{
    if ( s == Global )
    {
        // Read Opie global settings for style and position
        Config config( "qpe" );
        config.setGroup( "Appearance" );

        // Style
        s = ( TabStyle ) config.readNumEntry( "TabStyle", (int) IconTab );
        if ( s <= Global || s > IconList)
            s = IconTab;

        // Position
        ( config.readEntry( "TabPosition", "Top" ) == "Bottom" ) ? p = Bottom
                                                                 : p = Top;
    }

    // Initialize widget stack for tab widgets
    m_widgetStack = new QWidgetStack( this );
    m_widgetStack->setFrameStyle( QFrame::NoFrame );
    m_widgetStack->setLineWidth( style().defaultFrameWidth() );

    // Set initial selector control style and position
    setTabStyle( s );
    setTabPosition( p );
}

OTabWidget::~OTabWidget()
{
    m_tabs.setAutoDelete( true );
    m_tabs.clear();
}

void OTabWidget::addTab( QWidget *child, const QString &icon, const QString &label )
{
    int tabid = -1;

    if ( m_usingTabs )
    {
        // Create new tab in tab bar
        QTab *tab = new QTab();

        // Set label (and icon if necessary)
        if ( m_tabBarStyle == IconTab )
        {
            tab->label = QString::null;
            tab->iconset = new QIconSet( Opie::Core::OResource::loadPixmap( icon, Opie::Core::OResource::SmallIcon ) );
        }
        else
            tab->label = label;

        tabid = m_tabBar->addTab( tab );
    }
    else
    {
        // Insert entry (with icon if necessary) into drop down list
        if ( m_tabBarStyle == IconList )
            m_tabList->insertItem( Opie::Core::OResource::loadPixmap( icon, Opie::Core::OResource::SmallIcon ), label, -1 );
        else
            m_tabList->insertItem( label );
    }

    // Add widget to stack
    m_widgetStack->addWidget( child, tabid );
    m_widgetStack->raiseWidget( child );
    m_widgetStack->setFrameStyle( QFrame::StyledPanel | QFrame::Raised );

    // Keep track of tab information
    OTabInfo *tabinfo = new OTabInfo( tabid, child, icon, label );
    m_tabs.append( tabinfo );

    // Force resizing of child controls
    resizeEvent( 0x0 );

    // Make newly added tab the current one displayed
    selectTab( tabinfo );
}

void OTabWidget::removePage( QWidget *childwidget )
{
    if ( childwidget )
    {
        // Find tab information for desired widget
        OTabInfo *tab = m_tabs.first();
        while ( tab && tab->control() != childwidget )
            tab = m_tabs.next();

        if ( tab && tab->control() == childwidget )
        {
            if ( m_usingTabs )
            {
                // Remove tab from tab bar
                m_tabBar->setTabEnabled( tab->id(), false );
                m_tabBar->removeTab( m_tabBar->tab( tab->id() ) );
            }
            else
            {
                // Remove entry from drop down list
                int i = 0;
                while ( i < m_tabList->count() && m_tabList->text( i ) != tab->label() )
                    i++;
                if ( m_tabList->text( i ) == tab->label() )
                    m_tabList->removeItem( i );
            }

            // Remove widget from stack
            m_widgetStack->removeWidget( childwidget );

            // Get rid of tab information
            m_tabs.remove( tab );
            delete tab;

            // Reset current tab
            m_currTab = m_tabs.current();
            if ( !m_currTab )
                m_widgetStack->setFrameStyle( QFrame::NoFrame );

            // Redraw widget
            setUpLayout();
        }

        // Force resizing of child controls
        resizeEvent( 0x0 );
    }
}

void OTabWidget::changeTab( QWidget *widget, const QString &iconset, const QString &label)
{
    // Find tab information for desired widget
    OTabInfo *currtab = m_tabs.first();
    while ( currtab && currtab->control() != widget )
        currtab = m_tabs.next();

    if ( currtab && currtab->control() == widget )
    {
        QPixmap icon( Opie::Core::OResource::loadPixmap( iconset, Opie::Core::OResource::SmallIcon ) );

        if ( m_usingTabs )
        {
            // Update tab label and icon (if necessary)
            QTab *tab = m_tabBar->tab( currtab->id() );
            tab->setText( label );
            if ( m_tabBarStyle == IconTab )
                tab->setIconSet( icon );
        }
        else
        {
            // Update entry label and icon (if necessary)
            int i = 0;
            while ( i < m_tabList->count() && m_tabList->text( i ) != currtab->label() )
                i++;
            if ( i < m_tabList->count() && m_tabList->text( i ) == currtab->label() )
            {
                if ( m_tabBarStyle == IconList )
                    m_tabList->changeItem( icon, label, i );
                else
                    m_tabList->changeItem( label, i );
            }
        }

        // Update tab information
        currtab->setLabel( label );
        currtab->setIcon( iconset );

        // Force resizing of child controls
        resizeEvent( 0x0 );

        // Redraw widget
        setUpLayout();
    }
}

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

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

void OTabWidget::setCurrentTab(int tabindex)
{
    OTabInfo *newtab = m_tabs.first();
    while ( newtab && newtab->id() != tabindex )
    {
        newtab = m_tabs.next();
    }
    if ( newtab && newtab->id() == tabindex )
    {
        selectTab( newtab );
    }
}


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

void OTabWidget::setTabStyle( TabStyle s )
{
    // Get out if new and current styles are the same
    if ( s == m_tabBarStyle )
        return;

    // Delete current selector control
    if ( m_usingTabs )
    {
        delete m_tabBar;
        m_tabBar = 0l;
    }
    else
    {
        delete m_tabList;
        m_tabList = 0l;
    }

    // Set new style information
    m_tabBarStyle = s;
    m_usingTabs = ( m_tabBarStyle == TextTab || m_tabBarStyle == IconTab );

    // Create new selector control and populate with tab information
    if ( m_usingTabs )
    {
        // Create new tab bar selector
        m_tabBar = new OTabBar( this );
        connect( m_tabBar, SIGNAL(selected(int)), this, SLOT(slotTabBarSelected(int)) );

        // Add all current tabs to tab bar
        for (  OTabInfo *tabinfo = m_tabs.first(); tabinfo; tabinfo = m_tabs.next() )
        {
            // Create new tab in tab bar
            QTab *tab = new QTab();

            // Set label (and icon if necessary)
            if ( m_tabBarStyle == IconTab )
            {
                tab->label = QString::null;
                tab->iconset = new QIconSet( Opie::Core::OResource::loadPixmap( tabinfo->icon(), Opie::Core::OResource::SmallIcon ) );
            }
            else
                tab->label = tabinfo->label();

            // Add tab and save its Id
            int tabid = m_tabBar->addTab( tab );
            tabinfo->setId( tabid );
        }
    }
    else
    {
        // Create new drop down list selector
        m_tabList = new QComboBox( false, this );
        connect( m_tabList, SIGNAL(activated(int)), this, SLOT(slotTabListSelected(int)) );

        // Add all current tabs to drop down list
        for (  OTabInfo *tabinfo = m_tabs.first(); tabinfo; tabinfo = m_tabs.next() )
        {
            if ( m_tabBarStyle == IconList )
                m_tabList->insertItem( Opie::Core::OResource::loadPixmap( tabinfo->icon(), Opie::Core::OResource::SmallIcon ),
                                       tabinfo->label() );
            else
                m_tabList->insertItem( tabinfo->label() );
        }
    }

    // Redraw widget
    setUpLayout();
}

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

void OTabWidget::setTabPosition( TabPosition p )
{
    m_tabBarPosition = p;

    // If using the tab bar selector, set its shape
    if ( m_usingTabs )
    {
        ( m_tabBarPosition == Top ) ? m_tabBar->setShape( QTabBar::RoundedAbove )
                                    : m_tabBar->setShape( QTabBar::RoundedBelow );
    }

    // Redraw widget
    setUpLayout();
}

void OTabWidget::slotTabBarSelected( int id )
{
    OTabInfo *newtab = m_tabs.first();
    while ( newtab && newtab->id() != id )
        newtab = m_tabs.next();

    if ( newtab && newtab->id() == id )
        selectTab( newtab );
}

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

void OTabWidget::selectTab( OTabInfo *tab )
{
    if ( m_tabBarStyle == IconTab )
    {
        // Remove text label from currently selected tab
        if ( m_currTab )
        {
            m_tabBar->tab( m_currTab->id() )->setText( QString::null );
            //setUpLayout();
        }

        // Set text label for newly selected tab
        m_tabBar->tab( tab->id() )->setText( tab->label() );
        m_tabBar->setCurrentTab( tab->id() );

        setUpLayout();

        QSize t;

        t = m_tabBar->sizeHint();
        if ( t.width() > width() )
            t.setWidth( width() );
        int lw = m_widgetStack->lineWidth();
        if ( m_tabBarPosition == Bottom )
            m_tabBar->setGeometry( QMAX(0, lw-2), height() - t.height() - lw, t.width(), t.height() );
        else
            m_tabBar->setGeometry( QMAX(0, lw-2), 0, t.width(), t.height() );
    }
    else if ( m_tabBarStyle == TextTab )
    {
        m_tabBar->setCurrentTab( tab->id() );
    }

    m_widgetStack->raiseWidget( tab->control() );

    emit currentChanged( tab->control() );

    m_currTab = tab;
}

void OTabWidget::setUpLayout()
{
    if ( m_usingTabs )
    {
        m_tabBar->update();
        m_tabBar->layoutTabs();
    }
}

void OTabWidget::resizeEvent( QResizeEvent * )
{
    QSize t;

    if ( m_usingTabs )
    {
        m_tabBar->layoutTabs();
        t = m_tabBar->sizeHint();
        if ( t.width() > width() )
            t.setWidth( width() );
    }
    else
    {
        t = m_tabList->sizeHint();
        t.setWidth( width() );
    }

    int lw = m_widgetStack->lineWidth();
    if ( m_tabBarPosition == Bottom )
    {
        if ( m_usingTabs )
            m_tabBar->setGeometry( QMAX(0, lw-2), height() - t.height() - lw, t.width(), t.height() );
        else
            m_tabList->setGeometry( QMAX(0, lw-2), height() - t.height() - lw, t.width(), t.height() );

        m_widgetStack->setGeometry( 0, 0, width(), height()-t.height()+QMAX(0, lw-2) );
    }
    else
    {
        if ( m_usingTabs )
            m_tabBar->setGeometry( QMAX(0, lw-2), 0, t.width(), t.height() );
        else
            m_tabList->setGeometry( QMAX(0, lw-2), 0, t.width(), t.height() );

        m_widgetStack->setGeometry( 0, t.height()-lw, width(), height()-t.height()+QMAX( 0, lw-2 ) );
    }

    if ( autoMask() )
        updateMask();
}

int OTabWidget::currentTab()
{
    if ( m_currTab )
    {
        return m_currTab->id();
    }
    return -1;
}

QWidget* OTabWidget::currentWidget()const
{
    if ( m_currTab )
    {
        return m_currTab->control();
    }

    return 0;
}