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