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