libyui-qt  2.43.5
/usr/src/RPM/BUILD/libyui-qt-2.43.5/src/QY2ComboTabWidget.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:       QY2ComboTabWidget.cc
00020 
00021   Author:     Stefan Hundhammer <sh@suse.de>
00022 
00023   This is a pure Qt widget - it can be used independently of YaST2.
00024 
00025 /-*/
00026 
00027 
00028 #include <QComboBox>
00029 #include <QLabel>
00030 #include <QStackedWidget>
00031 #include <QHBoxLayout>
00032 
00033 #include <QFrame>
00034 
00035 #define YUILogComponent "qt-pkg"
00036 #include <yui/YUILog.h>
00037 
00038 #include "QY2ComboTabWidget.h"
00039 
00040 
00041 #define SPACING                 6       // between subwidgets
00042 #define MARGIN                  4       // around the widget
00043 
00044 
00045 
00046 QY2ComboTabWidget::QY2ComboTabWidget( const QString &   label,
00047                                       QWidget *         parent,
00048                                       const char *      name )
00049     : QWidget(parent)
00050 {
00051     QVBoxLayout *vbox = new QVBoxLayout(this);
00052     vbox->setMargin( 0 );
00053 
00054     QHBoxLayout *hbox = new QHBoxLayout();
00055     Q_CHECK_PTR( hbox );
00056 //     hbox->setFrameStyle( QFrame::Panel | QFrame::Raised );
00057 //     hbox->setLineWidth(2);
00058 //     hbox->setMidLineWidth(2);
00059     hbox->setSpacing( 0 );
00060     hbox->setMargin ( 0  );
00061 
00062     vbox->addLayout(hbox);
00063     //this->setSpacing( SPACING );
00064     this->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) ); // hor/vert
00065 
00066     combo_label = new QLabel(label);
00067     hbox->addWidget(combo_label);
00068     Q_CHECK_PTR( combo_label );
00069 
00070     combo_box = new QComboBox( this );
00071     Q_CHECK_PTR( combo_box );
00072     hbox->addWidget(combo_box);
00073     combo_label->setBuddy( combo_box );
00074     combo_box->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) ); // hor/vert
00075     connect( combo_box, SIGNAL( activated( int ) ),
00076              this,      SLOT  ( showPageIndex ( int ) ) );
00077 
00078     widget_stack = new QStackedWidget( this );
00079     Q_CHECK_PTR( widget_stack );
00080     vbox->addWidget(widget_stack);
00081 }
00082 
00083 
00084 
00085 QY2ComboTabWidget::~QY2ComboTabWidget()
00086 {
00087 
00088 }
00089 
00090 
00091 void
00092 QY2ComboTabWidget::addPage( const QString & page_label, QWidget * new_page )
00093 {
00094     pages.insert( combo_box->count(), new_page );
00095     combo_box->addItem( page_label );
00096     widget_stack->addWidget( new_page );
00097 
00098     if ( ! widget_stack->currentWidget() )
00099         widget_stack->setCurrentWidget( new_page );
00100 }
00101 
00102 
00103 void
00104 QY2ComboTabWidget::showPageIndex( int index )
00105 {
00106     if ( pages.contains(index) )
00107     {
00108         QWidget * page = pages[ index ];
00109         widget_stack->setCurrentWidget( page );
00110         // yuiDebug() << "Changing current page" << std::endl;
00111         emit currentChanged( page );
00112     }
00113     else
00114     {
00115         qWarning( "QY2ComboTabWidget: Page #%d not found", index );
00116         return;
00117     }
00118 }
00119 
00120 
00121 void
00122 QY2ComboTabWidget::showPage( QWidget * page )
00123 {
00124     widget_stack->setCurrentWidget( page );
00125 
00126     if ( page == pages[ combo_box->currentIndex() ] )
00127     {
00128           // Shortcut: If the requested page is the one that belongs to the item
00129           // currently selected in the combo box, don't bother searching the
00130           // correct combo box item.
00131           return;
00132     }
00133 
00134     // Search the dict for this page
00135 
00136     QHashIterator<int, QWidget *> it( pages );
00137 
00138     while ( it.hasNext() )
00139     {
00140         it.next();
00141         if ( page == it.value() )
00142         {
00143             combo_box->setCurrentIndex( it.key() );
00144             return;
00145         }
00146     }
00147 
00148     // If we come this far, that page isn't present in the dict.
00149 
00150     qWarning( "QY2ComboTabWidget: Page not found" );
00151 }
00152 
00153 
00154 
00155 #include "QY2ComboTabWidget.moc"
 All Classes Functions Variables