]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQDumbTab.cc
unhide some virtual overloads - fixing Huha's only complaint :)
[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     Q_CHECK_PTR( _tabBar );
53
54     _tabBar->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ) ); // hor/vert
55     setFocusProxy( _tabBar );
56     setFocusPolicy( Qt::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     _tabBar->insertTab( item->index(), fromUTF8( item->label() ) );
76     y2debug( "Adding tab page [%s]", item->label().c_str() );
77 #warning YItem::setData
78     // item->setData( tab );
79
80     if ( item->selected() )
81         _tabBar->setCurrentIndex( item->index() );
82 }
83
84
85 void
86 YQDumbTab::selectItem( YItem * item, bool selected )
87 {
88     if ( selected )
89     {
90         // Don't try to suppress any signals sent here with a YQSignalBlocker,
91         // otherwise the application code that handles the event will never be executed.
92
93         _tabBar->setCurrentIndex( item->index() );
94     }
95
96     YDumbTab::selectItem( item, selected );
97 }
98
99
100 void
101 YQDumbTab::deleteAllItems()
102 {
103     for ( YItemConstIterator it = itemsBegin();
104           it != itemsEnd();
105           ++it )
106     {
107         _tabBar->removeTab( ( *it )->index() );
108     }
109
110     YDumbTab::deleteAllItems();
111 }
112
113
114 void
115 YQDumbTab::deselectAllItems()
116 {
117     YDumbTab::deselectAllItems();
118 }
119
120
121 void
122 YQDumbTab::slotSelected( int index )
123 {
124     YItem * item = itemAt( index );
125     YUI_CHECK_PTR( item );
126     y2debug( "Tab [%s] selected", item->label().c_str() );
127
128
129     YQUI::ui()->sendEvent( new YMenuEvent( item ) );
130 }
131
132
133 void
134 YQDumbTab::setEnabled( bool enabled )
135 {
136     _tabBar->setEnabled( enabled );
137     YWidget::setEnabled( enabled );
138 }
139
140
141 int
142 YQDumbTab::preferredWidth()
143 {
144     int tabBarWidth = _tabBar->sizeHint().width();
145     int childWidth  = hasChildren() ? firstChild()->preferredWidth() : 0;
146
147     return max( tabBarWidth, childWidth );
148 }
149
150
151 int
152 YQDumbTab::preferredHeight()
153 {
154     int tabBarHeight = _tabBar->sizeHint().height();
155     int childHeight  = hasChildren() ? firstChild()->preferredHeight() : 0;
156
157     return tabBarHeight + YQDumbTabSpacing + childHeight;
158 }
159
160
161 void
162 YQDumbTab::paintEvent( QPaintEvent * event )
163 {
164     QPainter painter( this );
165
166     int x_offset        = 0;
167     int y_offset        = _tabBar->height() + YQDumbTabSpacing;
168     int frameHeight     = height() - y_offset;
169     int frameWidth      = width();
170
171     qDrawWinPanel( &painter,
172                    x_offset, y_offset,
173                    frameWidth, frameHeight,
174                    colorGroup(),
175                    false,                       // sunken
176                    (const QBrush *) 0 );        // brush - don't fill interior
177 }
178
179
180 void
181 YQDumbTab::setSize( int newWidth, int newHeight )
182 {
183     QWidget::resize( newWidth, newHeight );
184     int remainingHeight = newHeight;
185     int remainingWidth  = newWidth;
186     int x_offset        = 0;
187     int y_offset        = 0;
188
189     //
190     // _tabBar (fixed height)
191     //
192
193     int tabBarHeight = _tabBar->sizeHint().height();
194
195     if ( remainingHeight < tabBarHeight )
196         tabBarHeight = remainingHeight;
197
198     _tabBar->resize( newWidth, tabBarHeight );
199     remainingHeight -= tabBarHeight;
200
201     if ( hasChildren() )
202     {
203         //
204         // Spacing between tabBar and client area
205         //
206
207         remainingHeight -= YQDumbTabSpacing;
208         y_offset = newHeight - remainingHeight;
209
210         //
211         // 3D border
212         //
213
214         remainingHeight -= 2 * YQDumbTabFrameMargin;
215         remainingWidth  -= 2 * YQDumbTabFrameMargin;
216         x_offset += YQDumbTabFrameMargin;
217         y_offset += YQDumbTabFrameMargin;
218
219         if ( remainingHeight < 0 )
220             remainingHeight = 0;
221
222         if ( remainingWidth < 0 )
223             remainingWidth = 0;
224
225         //
226         // Client area
227         //
228
229
230         firstChild()->setSize( remainingWidth, remainingHeight );
231
232         QWidget * qChild = (QWidget *) firstChild()->widgetRep();
233         qChild->move( x_offset, y_offset );
234     }
235 }
236
237
238
239 #include "YQDumbTab.moc"