]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQDumbTab.cc
- Don't create layouts with parent. Qt 4.x automatically reparents
[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    2
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::setSize( int newWidth, int newHeight )
161 {
162     QWidget::resize( newWidth, newHeight );
163     int remainingHeight = newHeight;
164     int remainingWidth  = newWidth;
165     int x_offset        = 0;
166     int y_offset        = 0;
167
168     //
169     // _tabBar (fixed height)
170     //
171
172     int tabBarHeight = _tabBar->sizeHint().height();
173
174     if ( remainingHeight < tabBarHeight )
175         tabBarHeight = remainingHeight;
176
177     _tabBar->resize( newWidth, tabBarHeight );
178     remainingHeight -= tabBarHeight;
179
180     if ( hasChildren() )
181     {
182         //
183         // Spacing between tabBar and client area
184         //
185
186         remainingHeight -= YQDumbTabSpacing;
187         y_offset = newHeight - remainingHeight;
188
189         //
190         // 3D border
191         //
192
193         remainingHeight -= 2 * YQDumbTabFrameMargin;
194         remainingWidth  -= 2 * YQDumbTabFrameMargin;
195         x_offset += YQDumbTabFrameMargin;
196         y_offset += YQDumbTabFrameMargin;
197
198         if ( remainingHeight < 0 )
199             remainingHeight = 0;
200
201         if ( remainingWidth < 0 )
202             remainingWidth = 0;
203
204         //
205         // Client area
206         //
207
208
209         firstChild()->setSize( remainingWidth, remainingHeight );
210
211         QWidget * qChild = (QWidget *) firstChild()->widgetRep();
212         qChild->move( x_offset, y_offset );
213     }
214 }
215
216
217
218 #include "YQDumbTab.moc"