libyui-qt  2.43.5
/usr/src/RPM/BUILD/libyui-qt-2.43.5/src/YQDumbTab.cc
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019   File:       YQDumbTab.cc
00020 
00021   Author:     Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 
00026 #define YUILogComponent "qt-ui"
00027 #include <yui/YUILog.h>
00028 #include <qtabbar.h>
00029 #include <qevent.h>
00030 #include <qpainter.h>
00031 #include <qdrawutil.h>
00032 #include <algorithm>
00033 
00034 #include "YQSignalBlocker.h"
00035 #include "utf8.h"
00036 #include "YQUI.h"
00037 #include "YQDumbTab.h"
00038 #include "YQAlignment.h"
00039 #include <yui/YEvent.h>
00040 
00041 #define YQDumbTabSpacing        2
00042 #define YQDumbTabFrameMargin    2
00043 
00044 
00045 YQDumbTab::YQDumbTab( YWidget * parent )
00046     : QWidget( (QWidget *) parent->widgetRep() )
00047     , YDumbTab( parent )
00048 {
00049     setWidgetRep( this );
00050 
00051     //
00052     // Tab bar
00053     //
00054 
00055     _tabBar = new QTabBar( this );
00056     Q_CHECK_PTR( _tabBar );
00057 
00058     _tabBar->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ) ); // hor/vert
00059     setFocusProxy( _tabBar );
00060     setFocusPolicy( Qt::TabFocus );
00061 
00062     connect( _tabBar, SIGNAL( selected    ( int ) ),
00063              this,    SLOT  ( slotSelected( int ) ) );
00064 }
00065 
00066 
00067 YQDumbTab::~YQDumbTab()
00068 {
00069     // NOP
00070 }
00071 
00072 
00073 void
00074 YQDumbTab::addItem( YItem * item )
00075 {
00076     YQSignalBlocker sigBlocker( _tabBar );
00077     YDumbTab::addItem( item );
00078 
00079     _tabBar->insertTab( item->index(), fromUTF8( item->label() ) );
00080     yuiDebug() << "Adding tab page [" << item->label() << "]" << std::endl;
00081 
00082     if ( item->selected() )
00083         _tabBar->setCurrentIndex( item->index() );
00084 }
00085 
00086 
00087 void
00088 YQDumbTab::selectItem( YItem * item, bool selected )
00089 {
00090     if ( selected )
00091     {
00092         // Don't try to suppress any signals sent here with a YQSignalBlocker,
00093         // otherwise the application code that handles the event will never be executed.
00094 
00095         _tabBar->setCurrentIndex( item->index() );
00096     }
00097 
00098     YDumbTab::selectItem( item, selected );
00099 }
00100 
00101 
00102 void
00103 YQDumbTab::deleteAllItems()
00104 {
00105     for ( YItemConstIterator it = itemsBegin();
00106           it != itemsEnd();
00107           ++it )
00108     {
00109         _tabBar->removeTab( ( *it )->index() );
00110     }
00111 
00112     YDumbTab::deleteAllItems();
00113 }
00114 
00115 
00116 void
00117 YQDumbTab::deselectAllItems()
00118 {
00119     YDumbTab::deselectAllItems();
00120 }
00121 
00122 
00123 void
00124 YQDumbTab::slotSelected( int index )
00125 {
00126     YItem * item = itemAt( index );
00127     YUI_CHECK_PTR( item );
00128     yuiDebug() << "Tab [" << item->label() << "] selected" << std::endl;
00129     YDumbTab::selectItem( item );
00130 
00131     YQUI::ui()->sendEvent( new YMenuEvent( item ) );
00132 }
00133 
00134 
00135 void
00136 YQDumbTab::shortcutChanged()
00137 {
00138     // Any of the items might have its keyboard shortcut changed, but we don't
00139     // know which one. So let's simply set all tab labels again.
00140 
00141     for ( YItemConstIterator it = itemsBegin();
00142           it != itemsEnd();
00143           ++it )
00144     {
00145         YItem * item = *it;
00146         _tabBar->setTabText( item->index(), fromUTF8( item->label() ) );
00147     }
00148 }
00149 
00150 
00151 void
00152 YQDumbTab::setEnabled( bool enabled )
00153 {
00154     _tabBar->setEnabled( enabled );
00155     YWidget::setEnabled( enabled );
00156 }
00157 
00158 
00159 int
00160 YQDumbTab::preferredWidth()
00161 {
00162     int tabBarWidth = _tabBar->sizeHint().width();
00163     int childWidth  = hasChildren() ? firstChild()->preferredWidth() : 0;
00164 
00165     return std::max( tabBarWidth, childWidth );
00166 }
00167 
00168 
00169 int
00170 YQDumbTab::preferredHeight()
00171 {
00172     int tabBarHeight = _tabBar->sizeHint().height();
00173     int childHeight  = hasChildren() ? firstChild()->preferredHeight() : 0;
00174 
00175     return tabBarHeight + YQDumbTabSpacing + childHeight;
00176 }
00177 
00178 
00179 void
00180 YQDumbTab::setSize( int newWidth, int newHeight )
00181 {
00182     QWidget::resize( newWidth, newHeight );
00183     int remainingHeight = newHeight;
00184     int remainingWidth  = newWidth;
00185     int x_offset        = 0;
00186     int y_offset        = 0;
00187 
00188     //
00189     // _tabBar (fixed height)
00190     //
00191 
00192     int tabBarHeight = _tabBar->sizeHint().height();
00193 
00194     if ( remainingHeight < tabBarHeight )
00195         tabBarHeight = remainingHeight;
00196 
00197     _tabBar->resize( newWidth, tabBarHeight );
00198     remainingHeight -= tabBarHeight;
00199 
00200     if ( hasChildren() )
00201     {
00202         //
00203         // Spacing between tabBar and client area
00204         //
00205 
00206         remainingHeight -= YQDumbTabSpacing;
00207         y_offset = newHeight - remainingHeight;
00208 
00209         //
00210         // 3D border
00211         //
00212 
00213         remainingHeight -= 2 * YQDumbTabFrameMargin;
00214         remainingWidth  -= 2 * YQDumbTabFrameMargin;
00215         x_offset += YQDumbTabFrameMargin;
00216         y_offset += YQDumbTabFrameMargin;
00217 
00218         if ( remainingHeight < 0 )
00219             remainingHeight = 0;
00220 
00221         if ( remainingWidth < 0 )
00222             remainingWidth = 0;
00223 
00224         //
00225         // Client area
00226         //
00227 
00228 
00229         firstChild()->setSize( remainingWidth, remainingHeight );
00230 
00231         QWidget * qChild = (QWidget *) firstChild()->widgetRep();
00232         qChild->move( x_offset, y_offset );
00233     }
00234 }
00235 
00236 
00237 
00238 #include "YQDumbTab.moc"
 All Classes Functions Variables