libyui-qt  2.53.0
QY2ComboTabWidget.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: QY2ComboTabWidget.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23  This is a pure Qt widget - it can be used independently of YaST2.
24 
25 /-*/
26 
27 
28 #include <QComboBox>
29 #include <QLabel>
30 #include <QStackedWidget>
31 #include <QHBoxLayout>
32 
33 #include <QFrame>
34 
35 #define YUILogComponent "qt-pkg"
36 #include <yui/YUILog.h>
37 
38 #include "YQUI.h"
39 #include "QY2ComboTabWidget.h"
40 
41 
42 #define SPACING 6 // between subwidgets
43 #define MARGIN 4 // around the widget
44 
45 using std::string;
46 
47 
48 
49 QY2ComboTabWidget::QY2ComboTabWidget( const QString & label,
50  QWidget * parent,
51  const char * name )
52  : QWidget(parent)
53 {
54  QVBoxLayout *vbox = new QVBoxLayout(this);
55  vbox->setMargin( 0 );
56 
57  QHBoxLayout *hbox = new QHBoxLayout();
58  Q_CHECK_PTR( hbox );
59 // hbox->setFrameStyle( QFrame::Panel | QFrame::Raised );
60 // hbox->setLineWidth(2);
61 // hbox->setMidLineWidth(2);
62  hbox->setSpacing( 0 );
63  hbox->setMargin ( 0 );
64 
65  vbox->addLayout(hbox);
66  //this->setSpacing( SPACING );
67  this->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred ) ); // hor/vert
68 
69  combo_label = new QLabel(label);
70  hbox->addWidget(combo_label);
71  Q_CHECK_PTR( combo_label );
72 
73  combo_box = new QComboBox( this );
74  Q_CHECK_PTR( combo_box );
75  hbox->addWidget(combo_box);
76  combo_label->setBuddy( combo_box );
77  combo_box->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ) ); // hor/vert
78  connect( combo_box, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
79  this, &pclass(this)::showPageIndex );
80 
81  widget_stack = new QStackedWidget( this );
82  Q_CHECK_PTR( widget_stack );
83  vbox->addWidget(widget_stack);
84 }
85 
86 
87 
89 {
90 
91 }
92 
93 
94 void
95 QY2ComboTabWidget::addPage( const QString & page_label, QWidget * new_page )
96 {
97  pages.insert( combo_box->count(), new_page );
98  combo_box->addItem( page_label );
99  widget_stack->addWidget( new_page );
100 
101  if ( ! widget_stack->currentWidget() )
102  widget_stack->setCurrentWidget( new_page );
103 }
104 
105 
106 void
108 {
109  if ( pages.contains(index) )
110  {
111  QWidget * page = pages[ index ];
112  widget_stack->setCurrentWidget( page );
113  // yuiDebug() << "Changing current page" << endl;
114  emit currentChanged( page );
115  }
116  else
117  {
118  qWarning( "QY2ComboTabWidget: Page #%d not found", index );
119  return;
120  }
121 }
122 
123 
124 void
126 {
127  widget_stack->setCurrentWidget( page );
128 
129  if ( page == pages[ combo_box->currentIndex() ] )
130  {
131  // Shortcut: If the requested page is the one that belongs to the item
132  // currently selected in the combo box, don't bother searching the
133  // correct combo box item.
134  return;
135  }
136 
137  // Search the dict for this page
138 
139  QHashIterator<int, QWidget *> it( pages );
140 
141  while ( it.hasNext() )
142  {
143  it.next();
144  if ( page == it.value() )
145  {
146  combo_box->setCurrentIndex( it.key() );
147  return;
148  }
149  }
150 
151  // If we come this far, that page isn't present in the dict.
152 
153  qWarning( "QY2ComboTabWidget: Page not found" );
154 }
155 
156 
157 
158 
QY2ComboTabWidget(const QString &combo_box_label, QWidget *parent=0, const char *name=0)
Constructor.
void currentChanged(QWidget *newCurrentPage)
Emitted when the current page changes.
void showPageIndex(int index)
Show a page identified by its index.
virtual ~QY2ComboTabWidget()
Destructor.
void addPage(const QString &page_label, QWidget *page)
Add a page.
void showPage(QWidget *page)
Show a page.