]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/QY2ComboTabWidget.cc
clicking packages work! so the package selector is now
[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     QVBoxLayout *vbox = new QVBoxLayout();
46     setLayout(vbox);
47
48     QHBoxLayout *hbox = new QHBoxLayout();
49     Q_CHECK_PTR( hbox );
50 //     hbox->setFrameStyle( QFrame::Panel | QFrame::Raised );
51 //     hbox->setLineWidth(2);
52 //     hbox->setMidLineWidth(2);
53     hbox->setSpacing( SPACING );
54     hbox->setMargin ( MARGIN  );
55
56     vbox->addLayout(hbox);
57     //this->setSpacing( SPACING );
58     this->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) ); // hor/vert
59     
60
61     combo_label = new QLabel(label);
62     hbox->addWidget(combo_label);
63     Q_CHECK_PTR( combo_label );
64     
65     combo_box = new QComboBox( this );
66     Q_CHECK_PTR( combo_box );
67     hbox->addWidget(combo_box);
68     combo_label->setBuddy( combo_box );
69     combo_box->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) ); // hor/vert
70     connect( combo_box, SIGNAL( activated( int ) ),
71              this,      SLOT  ( showPageIndex ( int ) ) );
72     
73     widget_stack = new QStackedWidget( this );
74     Q_CHECK_PTR( widget_stack );
75     vbox->addWidget(widget_stack);
76 }
77
78
79
80 QY2ComboTabWidget::~QY2ComboTabWidget()
81 {
82     
83 }
84
85
86 void
87 QY2ComboTabWidget::addPage( const QString & page_label, QWidget * new_page )
88 {
89     pages.insert( combo_box->count(), new_page );
90     combo_box->addItem( page_label );
91     widget_stack->addWidget( new_page );
92
93     if ( ! widget_stack->currentWidget() )
94         widget_stack->setCurrentWidget( new_page );
95 }
96
97
98 void
99 QY2ComboTabWidget::showPageIndex( int index )
100 {
101     if ( pages.contains(index) )
102     {
103         QWidget * page = pages[ index ];
104         widget_stack->setCurrentWidget( page );
105         // y2debug( "Changing current page" );
106         emit currentChanged( page );
107     }
108     else
109     {
110         qWarning( "QY2ComboTabWidget: Page #%d not found", index );
111         return;
112     }
113 }
114
115
116 void
117 QY2ComboTabWidget::showPage( QWidget * page )
118 {
119     widget_stack->setCurrentWidget( page );
120
121     if ( page == pages[ combo_box->currentIndex() ] )
122     {
123           // Shortcut: If the requested page is the one that belongs to the item
124           // currently selected in the combo box, don't bother searching the
125           // correct combo box item.
126           return;
127     }
128     
129     // Search the dict for this page
130     
131     QHashIterator<int, QWidget *> it( pages );
132
133     while ( it.hasNext() )
134     {
135         it.next();
136         if ( page == it.value() )
137         {
138             combo_box->setCurrentIndex( it.key() );
139             return;
140         }
141     }
142
143     // If we come this far, that page isn't present in the dict.
144
145     qWarning( "QY2ComboTabWidget: Page not found" );
146 }
147
148
149
150 #include "QY2ComboTabWidget.moc"