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