]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/QY2ComboTabWidget.cc
merge in changes from wizard-rework. Still work in progress,
[duncan/yast2-qt4.git] / src / QY2ComboTabWidget.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                          contributed Qt widgets                      |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       QY2ComboTabWidget.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17   This is a pure Qt widget - it can be used independently of YaST2.
18
19 /-*/
20
21
22 #include <QComboBox>
23 #include <QLabel>
24 #include <QStackedWidget>
25 #include <QHBoxLayout>
26
27 #include <QFrame>
28
29 #define y2log_component "qt-pkg"
30 #include <ycp/y2log.h>
31
32 #include "QY2ComboTabWidget.h"
33
34
35 #define SPACING                 6       // between subwidgets
36 #define MARGIN                  4       // around the widget
37
38
39
40 QY2ComboTabWidget::QY2ComboTabWidget( const QString &   label,
41                                       QWidget *         parent,
42                                       const char *      name )
43     : QWidget(parent)
44 {
45     QHBoxLayout *hbox = new QHBoxLayout(this);
46 //     hbox->setFrameStyle( QFrame::Panel | QFrame::Raised );
47 //     hbox->setLineWidth(2);
48 //     hbox->setMidLineWidth(2);
49     hbox->setSpacing( SPACING );
50     hbox->setMargin ( MARGIN  );
51
52     Q_CHECK_PTR( hbox );
53     //this->setSpacing( SPACING );
54     this->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) ); // hor/vert
55     
56
57     combo_label = new QLabel(label);
58     hbox->addWidget(combo_label);
59     Q_CHECK_PTR( combo_label );
60     
61     combo_box = new QComboBox( this );
62     Q_CHECK_PTR( combo_box );
63     hbox->addWidget(combo_box);
64     combo_label->setBuddy( combo_box );
65     combo_box->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) ); // hor/vert
66     connect( combo_box, SIGNAL( activated( int ) ),
67              this,      SLOT  ( showPage ( int ) ) );
68     
69     widget_stack = new QStackedWidget( this );
70     Q_CHECK_PTR( widget_stack );
71 }
72
73
74
75 QY2ComboTabWidget::~QY2ComboTabWidget()
76 {
77     
78 }
79
80
81 void
82 QY2ComboTabWidget::addPage( const QString & page_label, QWidget * new_page )
83 {
84     pages.insert( combo_box->count(), new_page );
85     combo_box->addItem( page_label );
86     widget_stack->addWidget( new_page );
87
88     if ( ! widget_stack->currentWidget() )
89         widget_stack->setCurrentWidget( new_page );
90 }
91
92
93 void
94 QY2ComboTabWidget::showPage( int index )
95 {
96     QWidget * page = pages[ index ];
97
98     if ( page )
99     {
100         widget_stack->setCurrentWidget( page );
101         // y2debug( "Changing current page" );
102         emit currentChanged( page );
103     }
104     else
105     {
106         qWarning( "QY2ComboTabWidget: Page #%d not found", index );
107         return;
108     }
109 }
110
111
112 void
113 QY2ComboTabWidget::showPage( QWidget * page )
114 {
115     widget_stack->setCurrentWidget( page );
116
117     if ( page == pages[ combo_box->currentIndex() ] )
118     {
119         // Shortcut: If the requested page is the one that belongs to the item
120         // currently selected in the combo box, don't bother searching the
121         // correct combo box item.
122         return;
123     }
124
125     
126     // Search the dict for this page
127     
128     QHashIterator<int, QWidget *> it( pages );
129
130     while ( it.hasNext() )
131     {
132         if ( page == it.value() )
133         {
134             combo_box->setCurrentIndex( it.key() );
135             return;
136         }
137
138         it.next();
139     }
140
141     // If we come this far, that page isn't present in the dict.
142
143     qWarning( "QY2ComboTabWidget: Page not found" );
144 }
145
146
147
148 #include "QY2ComboTabWidget.moc"