]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQDumbTab.cc
disabling the style for now as the theming causes terrible
[duncan/yast2-qt4.git] / src / YQDumbTab.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQDumbTab.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17 /-*/
18
19
20 #define y2log_component "qt-ui"
21 #include <ycp/y2log.h>
22 #include <qtabbar.h>
23 #include <qevent.h>
24 #include <qpainter.h>
25 #include <qdrawutil.h>
26 #include <algorithm>
27
28 #include "YQSignalBlocker.h"
29 #include "utf8.h"
30 #include "YQUI.h"
31 #include "YQDumbTab.h"
32 #include "YQAlignment.h"
33 #include "YEvent.h"
34
35 #define YQDumbTabSpacing        2
36 #define YQDumbTabFrameMargin    4
37
38
39 YQDumbTab::YQDumbTab( YWidget * parent )
40     : QWidget( (QWidget *) parent->widgetRep() )
41     , YDumbTab( parent )
42 {
43     setWidgetRep( this );
44
45     //
46     // Tab bar
47     //
48
49     _tabBar = new QTabBar( this );
50     Q_CHECK_PTR( _tabBar );
51
52     _tabBar->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ) ); // hor/vert
53     setFocusProxy( _tabBar );
54     setFocusPolicy( Qt::TabFocus );
55
56     connect( _tabBar, SIGNAL( selected    ( int ) ),
57              this,    SLOT  ( slotSelected( int ) ) );
58 }
59
60
61 YQDumbTab::~YQDumbTab()
62 {
63     // NOP
64 }
65
66
67 void
68 YQDumbTab::addItem( YItem * item )
69 {
70     YQSignalBlocker sigBlocker( _tabBar );
71     YDumbTab::addItem( item );
72
73     _tabBar->insertTab( item->index(), fromUTF8( item->label() ) );
74     y2debug( "Adding tab page [%s]", item->label().c_str() );
75 #warning YItem::setData
76     // item->setData( tab );
77
78     if ( item->selected() )
79         _tabBar->setCurrentIndex( item->index() );
80 }
81
82
83 void
84 YQDumbTab::selectItem( YItem * item, bool selected )
85 {
86     if ( selected )
87     {
88         // Don't try to suppress any signals sent here with a YQSignalBlocker,
89         // otherwise the application code that handles the event will never be executed.
90
91         _tabBar->setCurrentIndex( item->index() );
92     }
93
94     YDumbTab::selectItem( item, selected );
95 }
96
97
98 void
99 YQDumbTab::deleteAllItems()
100 {
101     for ( YItemConstIterator it = itemsBegin();
102           it != itemsEnd();
103           ++it )
104     {
105         _tabBar->removeTab( ( *it )->index() );
106     }
107
108     YDumbTab::deleteAllItems();
109 }
110
111
112 void
113 YQDumbTab::deselectAllItems()
114 {
115     YDumbTab::deselectAllItems();
116 }
117
118
119 void
120 YQDumbTab::slotSelected( int index )
121 {
122     YItem * item = itemAt( index );
123     YUI_CHECK_PTR( item );
124     y2debug( "Tab [%s] selected", item->label().c_str() );
125
126
127     YQUI::ui()->sendEvent( new YMenuEvent( item ) );
128 }
129
130
131 void
132 YQDumbTab::setEnabled( bool enabled )
133 {
134     _tabBar->setEnabled( enabled );
135     YWidget::setEnabled( enabled );
136 }
137
138
139 int
140 YQDumbTab::preferredWidth()
141 {
142     int tabBarWidth = _tabBar->sizeHint().width();
143     int childWidth  = hasChildren() ? firstChild()->preferredWidth() : 0;
144
145     return max( tabBarWidth, childWidth );
146 }
147
148
149 int
150 YQDumbTab::preferredHeight()
151 {
152     int tabBarHeight = _tabBar->sizeHint().height();
153     int childHeight  = hasChildren() ? firstChild()->preferredHeight() : 0;
154
155     return tabBarHeight + YQDumbTabSpacing + childHeight;
156 }
157
158
159 void
160 YQDumbTab::paintEvent( QPaintEvent * event )
161 {
162     QPainter painter( this );
163
164     int x_offset        = 0;
165     int y_offset        = _tabBar->height() + YQDumbTabSpacing;
166     int frameHeight     = height() - y_offset;
167     int frameWidth      = width();
168
169     qDrawWinPanel( &painter,
170                    x_offset, y_offset,
171                    frameWidth, frameHeight,
172                    palette(),
173                    false,                       // sunken
174                    (const QBrush *) 0 );        // brush - don't fill interior
175 }
176
177
178 void
179 YQDumbTab::setSize( int newWidth, int newHeight )
180 {
181     QWidget::resize( newWidth, newHeight );
182     int remainingHeight = newHeight;
183     int remainingWidth  = newWidth;
184     int x_offset        = 0;
185     int y_offset        = 0;
186
187     //
188     // _tabBar (fixed height)
189     //
190
191     int tabBarHeight = _tabBar->sizeHint().height();
192
193     if ( remainingHeight < tabBarHeight )
194         tabBarHeight = remainingHeight;
195
196     _tabBar->resize( newWidth, tabBarHeight );
197     remainingHeight -= tabBarHeight;
198
199     if ( hasChildren() )
200     {
201         //
202         // Spacing between tabBar and client area
203         //
204
205         remainingHeight -= YQDumbTabSpacing;
206         y_offset = newHeight - remainingHeight;
207
208         //
209         // 3D border
210         //
211
212         remainingHeight -= 2 * YQDumbTabFrameMargin;
213         remainingWidth  -= 2 * YQDumbTabFrameMargin;
214         x_offset += YQDumbTabFrameMargin;
215         y_offset += YQDumbTabFrameMargin;
216
217         if ( remainingHeight < 0 )
218             remainingHeight = 0;
219
220         if ( remainingWidth < 0 )
221             remainingWidth = 0;
222
223         //
224         // Client area
225         //
226
227
228         firstChild()->setSize( remainingWidth, remainingHeight );
229
230         QWidget * qChild = (QWidget *) firstChild()->widgetRep();
231         qChild->move( x_offset, y_offset );
232     }
233 }
234
235
236
237 #include "YQDumbTab.moc"