libyui-qt  2.46.13
YQDumbTab.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: YQDumbTab.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #define YUILogComponent "qt-ui"
27 #include <yui/YUILog.h>
28 #include <qtabbar.h>
29 #include <qevent.h>
30 #include <qpainter.h>
31 #include <qdrawutil.h>
32 #include <algorithm>
33 
34 #include "YQSignalBlocker.h"
35 #include "utf8.h"
36 #include "YQUI.h"
37 #include "YQDumbTab.h"
38 #include "YQAlignment.h"
39 #include <yui/YEvent.h>
40 
41 #define YQDumbTabSpacing 2
42 #define YQDumbTabFrameMargin 2
43 
44 
45 YQDumbTab::YQDumbTab( YWidget * parent )
46  : QWidget( (QWidget *) parent->widgetRep() )
47  , YDumbTab( parent )
48 {
49  setWidgetRep( this );
50 
51  //
52  // Tab bar
53  //
54 
55  _tabBar = new QTabBar( this );
56  Q_CHECK_PTR( _tabBar );
57 
58  _tabBar->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ) ); // hor/vert
59  setFocusProxy( _tabBar );
60  setFocusPolicy( Qt::TabFocus );
61 
62  connect( _tabBar, &pclass(_tabBar)::currentChanged,
63  this, &pclass(this)::slotSelected );
64 }
65 
66 
68 {
69  // NOP
70 }
71 
72 
73 void
74 YQDumbTab::addItem( YItem * item )
75 {
76  YQSignalBlocker sigBlocker( _tabBar );
77  YDumbTab::addItem( item );
78 
79  _tabBar->insertTab( item->index(), fromUTF8( item->label() ) );
80  yuiDebug() << "Adding tab page [" << item->label() << "]" << std::endl;
81 
82  if ( item->selected() )
83  _tabBar->setCurrentIndex( item->index() );
84 }
85 
86 
87 void
88 YQDumbTab::selectItem( YItem * item, bool selected )
89 {
90  if ( selected )
91  {
92  // Don't try to suppress any signals sent here with a YQSignalBlocker,
93  // otherwise the application code that handles the event will never be executed.
94 
95  _tabBar->setCurrentIndex( item->index() );
96  }
97 
98  YDumbTab::selectItem( item, selected );
99 }
100 
101 
102 void
104 {
105  for ( YItemConstIterator it = itemsBegin();
106  it != itemsEnd();
107  ++it )
108  {
109  _tabBar->removeTab( ( *it )->index() );
110  }
111 
112  YDumbTab::deleteAllItems();
113 }
114 
115 
116 void
118 {
119  YDumbTab::deselectAllItems();
120 }
121 
122 
123 void
125 {
126  YItem * item = itemAt( index );
127  YUI_CHECK_PTR( item );
128  yuiDebug() << "Tab [" << item->label() << "] selected" << std::endl;
129  YDumbTab::selectItem( item );
130 
131  YQUI::ui()->sendEvent( new YMenuEvent( item ) );
132 }
133 
134 
135 void
137 {
138  // Any of the items might have its keyboard shortcut changed, but we don't
139  // know which one. So let's simply set all tab labels again.
140 
141  for ( YItemConstIterator it = itemsBegin();
142  it != itemsEnd();
143  ++it )
144  {
145  YItem * item = *it;
146  _tabBar->setTabText( item->index(), fromUTF8( item->label() ) );
147  }
148 }
149 
150 
151 void
152 YQDumbTab::setEnabled( bool enabled )
153 {
154  _tabBar->setEnabled( enabled );
155  YWidget::setEnabled( enabled );
156 }
157 
158 
159 int
161 {
162  int tabBarWidth = _tabBar->sizeHint().width();
163  int childWidth = hasChildren() ? firstChild()->preferredWidth() : 0;
164 
165  return std::max( tabBarWidth, childWidth );
166 }
167 
168 
169 int
171 {
172  int tabBarHeight = _tabBar->sizeHint().height();
173  int childHeight = hasChildren() ? firstChild()->preferredHeight() : 0;
174 
175  return tabBarHeight + YQDumbTabSpacing + childHeight;
176 }
177 
178 
179 void
180 YQDumbTab::setSize( int newWidth, int newHeight )
181 {
182  QWidget::resize( newWidth, newHeight );
183  int remainingHeight = newHeight;
184  int remainingWidth = newWidth;
185  int x_offset = 0;
186  int y_offset = 0;
187 
188  //
189  // _tabBar (fixed height)
190  //
191 
192  int tabBarHeight = _tabBar->sizeHint().height();
193 
194  if ( remainingHeight < tabBarHeight )
195  tabBarHeight = remainingHeight;
196 
197  _tabBar->resize( newWidth, tabBarHeight );
198  remainingHeight -= tabBarHeight;
199 
200  if ( hasChildren() )
201  {
202  //
203  // Spacing between tabBar and client area
204  //
205 
206  remainingHeight -= YQDumbTabSpacing;
207  y_offset = newHeight - remainingHeight;
208 
209  //
210  // 3D border
211  //
212 
213  remainingHeight -= 2 * YQDumbTabFrameMargin;
214  remainingWidth -= 2 * YQDumbTabFrameMargin;
215  x_offset += YQDumbTabFrameMargin;
216  y_offset += YQDumbTabFrameMargin;
217 
218  if ( remainingHeight < 0 )
219  remainingHeight = 0;
220 
221  if ( remainingWidth < 0 )
222  remainingWidth = 0;
223 
224  //
225  // Client area
226  //
227 
228 
229  firstChild()->setSize( remainingWidth, remainingHeight );
230 
231  QWidget * qChild = (QWidget *) firstChild()->widgetRep();
232  qChild->move( x_offset, y_offset );
233  }
234 }
235 
236 
237 
238 #include "YQDumbTab.moc"
virtual void addItem(YItem *item)
Add an item (a tab page).
Definition: YQDumbTab.cc:74
Helper class to block Qt signals for QWidgets or QObjects as long as this object exists.
virtual void deleteAllItems()
Delete all items (all tab pages).
Definition: YQDumbTab.cc:103
YQDumbTab(YWidget *parent)
Constructor.
Definition: YQDumbTab.cc:45
void slotSelected(int index)
Send an event that the tab with the specified index is selected.
Definition: YQDumbTab.cc:124
virtual int preferredHeight()
Preferred height of the widget.
Definition: YQDumbTab.cc:170
virtual ~YQDumbTab()
Destructor.
Definition: YQDumbTab.cc:67
virtual void shortcutChanged()
Notification that some shortcut was changed.
Definition: YQDumbTab.cc:136
void sendEvent(YEvent *event)
Widget event handlers (slots) call this when an event occured that should be the answer to a UserInpu...
Definition: YQUI.cc:486
virtual void setEnabled(bool enabled)
Set enabled/disabled state.
Definition: YQDumbTab.cc:152
virtual int preferredWidth()
Preferred width of the widget.
Definition: YQDumbTab.cc:160
virtual void selectItem(YItem *item, bool selected=true)
Select or deselect an item.
Definition: YQDumbTab.cc:88
static YQUI * ui()
Access the global Qt-UI.
Definition: YQUI.h:81
virtual void deselectAllItems()
Deselect all items.
Definition: YQDumbTab.cc:117
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
Definition: YQDumbTab.cc:180