simplify build requires
[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(this);
46     setLayout(vbox);
47
48     QHBoxLayout *hbox = new QHBoxLayout(this);
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  ( showPage ( 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::showPage( int index )
100 {
101     QWidget * page = pages[ index ];
102
103     if ( page )
104     {
105         widget_stack->setCurrentWidget( page );
106         // y2debug( "Changing current page" );
107         emit currentChanged( page );
108     }
109     else
110     {
111         qWarning( "QY2ComboTabWidget: Page #%d not found", index );
112         return;
113     }
114 }
115
116
117 void
118 QY2ComboTabWidget::showPage( QWidget * page )
119 {
120     widget_stack->setCurrentWidget( page );
121
122     if ( page == pages[ combo_box->currentIndex() ] )
123     {
124         // Shortcut: If the requested page is the one that belongs to the item
125         // currently selected in the combo box, don't bother searching the
126         // correct combo box item.
127         return;
128     }
129
130     
131     // Search the dict for this page
132     
133     QHashIterator<int, QWidget *> it( pages );
134
135     while ( it.hasNext() )
136     {
137         if ( page == it.value() )
138         {
139             combo_box->setCurrentIndex( it.key() );
140             return;
141         }
142
143         it.next();
144     }
145
146     // If we come this far, that page isn't present in the dict.
147
148     qWarning( "QY2ComboTabWidget: Page not found" );
149 }
150
151
152
153 #include "QY2ComboTabWidget.moc"