• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • libs
  • kasten
  • gui
  • shell
statusbarlayout.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Kasten Framework, made within the KDE community.
3 
4  Copyright 2009 Friedrich W. H. Kossebau <kossebau@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) version 3, or any
10  later version accepted by the membership of KDE e.V. (or its
11  successor approved by the membership of KDE e.V.), which shall
12  act as a proxy defined in Section 6 of version 3 of the license.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "statusbarlayout.h"
24 
25 // KDE
26 #include <KDebug>
27 // Qt
28 #include <QtGui/QWidgetItem>
29 #include <QtGui/QWidget>
30 #include <QtGui/QStyle>
31 
32 
33 namespace Kasten2
34 {
35 
36 StatusBarLayout::StatusBarLayout( QWidget* parent )
37  : QLayout( parent ),
38  mIsDirty( true ),
39  mIsEmpty( true )
40 {
41  setMargin( 0 );
42 }
43 
44 
45 int StatusBarLayout::count() const { return mWidgetList.count(); }
46 
47 bool StatusBarLayout::isEmpty() const
48 {
49  if( mIsDirty )
50  updateLayoutStructs();
51 
52  return mIsEmpty;
53 }
54 
55 QLayoutItem* StatusBarLayout::itemAt( int index ) const
56 {
57  if( index < 0 || mWidgetList.count() <= index )
58  return 0;
59 
60  return mWidgetList.at( index );
61 }
62 
63 int StatusBarLayout::indexOf( QWidget* widget ) const
64 {
65  int result = -1;
66 
67  for( int i=0; i<mWidgetList.count(); ++i )
68  if( mWidgetList.at(i)->widget() == widget )
69  {
70  result = i;
71  break;
72  }
73 
74  return result;
75 }
76 
77 QSize StatusBarLayout::sizeHint() const
78 {
79  updateLayoutStructs();
80 
81  return mSizeHint;
82 }
83 
84 QSize StatusBarLayout::minimumSize() const
85 {
86  updateLayoutStructs();
87 
88  return QSize( 0, mSizeHint.height() );
89 }
90 
91 
92 void StatusBarLayout::addItem( QLayoutItem* item )
93 {
94 Q_UNUSED( item )
95  kWarning() << "not implemented! Please use addWidget() instead";
96  return;
97 }
98 
99 QLayoutItem* StatusBarLayout::takeAt( int index )
100 {
101  if( index < 0 || mWidgetList.count() <= index )
102  return 0;
103 
104  QWidgetItem* item = mWidgetList.takeAt( index );
105 
106  // TODO: any need to delete or reparent the widget?
107 
108  invalidate();
109  return item;
110 }
111 
112 void StatusBarLayout::invalidate()
113 {
114  mIsDirty = true;
115  QLayout::invalidate();
116 }
117 
118 Qt::Orientations StatusBarLayout::expandingDirections() const { return Qt::Orientations(0); }
119 
120 void StatusBarLayout::addWidget( QWidget* widget )
121 {
122  if( widget )
123  {
124  mWidgetList.append( new QWidgetItem(widget) );
125  invalidate();
126  }
127 }
128 
129 
130 #if 0
131 void StatusBarLayout::updateMarginAndSpacing()
132 {
133  Statusbar* statusBar = qobject_cast<Statusbar*>( parentWidget() );
134  if( ! statusBar )
135  return;
136 
137  QStyle* style = statusBar->style();
138  QStyleOptionToolBar opt;
139  statusBar->initStyleOption( &opt );
140  setMargin( style->pixelMetric(QStyle::PM_ToolBarItemMargin, &opt, statusBar)
141  + style->pixelMetric(QStyle::PM_ToolBarFrameWidth, &opt, statusBar) );
142  setSpacing( style->pixelMetric(QStyle::PM_ToolBarItemSpacing, &opt, statusBar) );
143 }
144 #endif
145 
146 void StatusBarLayout::setGeometry( const QRect& _rect )
147 {
148  QLayout::setGeometry( _rect );
149 
150  if( mIsDirty )
151  updateLayoutStructs();
152 
153  QRect rect( 0, 0, _rect.width(), _rect.height() );
154 
155  const int margin = 0;//this->margin();
156  const int spacing = this->spacing();
157 
158  int availableWidth = rect.size().width() - 2*margin;
159  const int availableHeight = rect.size().height() - 2*margin;
160 
161  int usedWidth = 0;
162  int visibleCount = 0;
163  int i;
164  for( i = 0; i<mWidgetList.count(); ++i )
165  {
166  QWidgetItem* item = mWidgetList.at( i );
167  QWidget* widget = item->widget();
168 
169  // TODO: is there really no way to get to the geometry data if a widget is hidden?
170  if( widget->isHidden() )
171  widget->show();
172 
173  const int itemWidth = item->sizeHint().width();
174  const int itemSpacing = ( visibleCount == 0 ) ? 0 : spacing;
175  const int newUsedWidth = usedWidth + itemSpacing + itemWidth;
176 // kDebug()<<widget<<<<availableWidth<<usedWidth<<itemWidth<<itemSpacing<<newUsedWidth;
177 
178  const bool isTooWide = ( newUsedWidth > availableWidth );
179 
180  if( isTooWide )
181  break;
182 
183  const QPoint pos( margin + usedWidth + itemSpacing, margin );
184  const QSize size( itemWidth, availableHeight );
185  QRect r( pos, size );
186 
187  r = QStyle::visualRect( parentWidget()->layoutDirection(), rect, r );
188 
189  item->setGeometry( r );
190 
191  usedWidth = newUsedWidth;
192 
193  ++visibleCount;
194  }
195  // hide the rest if needed
196  for( ; i<mWidgetList.count(); ++i )
197  {
198  QWidgetItem* item = mWidgetList.at( i );
199  QWidget* widget = item->widget();
200 
201  if( ! widget->isHidden() )
202  widget->hide();
203  }
204 }
205 
206 
207 void StatusBarLayout::updateLayoutStructs() const
208 {
209  StatusBarLayout* that = const_cast<StatusBarLayout*>( this );
210 
211 // const int margin = this->margin();
212  const int spacing = this->spacing();
213 
214  QSize sizeHint( 0, 0 );
215 
216  int visibleCount = 0;
217  for( int i = 0; i < mWidgetList.count(); ++i )
218  {
219  QWidgetItem* item = mWidgetList.at( i );
220 
221  if( ! item->isEmpty() )
222  {
223  const QSize itemSizeHint = item->sizeHint();
224 
225  sizeHint.rwidth() += (visibleCount == 0 ? 0 : spacing) + itemSizeHint.width();
226  sizeHint.rheight() = qMax( sizeHint.height(), itemSizeHint.height() );
227  ++visibleCount;
228  }
229  }
230 // sizeHint += QSize( 2*margin, 2*margin );
231 
232  that->mIsEmpty = ( visibleCount == 0 );
233  that->mSizeHint = sizeHint;
234  that->mIsDirty = false;
235 }
236 
237 
238 StatusBarLayout::~StatusBarLayout()
239 {
240 // while( !mWidgetList.isEmpty() )
241 // {
242 // QWidgetItem *item = mWidgetList.takeFirst();
243 // delete item;
244 // }
245 }
246 
247 }
Kasten2::StatusBarLayout::expandingDirections
virtual Qt::Orientations expandingDirections() const
Definition: statusbarlayout.cpp:118
Kasten2::StatusBarLayout
Definition: statusbarlayout.h:36
statusbarlayout.h
Kasten2::StatusBarLayout::~StatusBarLayout
virtual ~StatusBarLayout()
Definition: statusbarlayout.cpp:238
Kasten2::StatusBarLayout::count
virtual int count() const
Definition: statusbarlayout.cpp:45
Kasten2::StatusBarLayout::updateLayoutStructs
void updateLayoutStructs() const
Definition: statusbarlayout.cpp:207
QWidget
Kasten2::StatusBarLayout::addItem
virtual void addItem(QLayoutItem *item)
Definition: statusbarlayout.cpp:92
Kasten2::StatusBarLayout::isEmpty
virtual bool isEmpty() const
Definition: statusbarlayout.cpp:47
Kasten2::StatusBarLayout::indexOf
virtual int indexOf(QWidget *widget) const
Definition: statusbarlayout.cpp:63
QLayout
Kasten2::StatusBarLayout::addWidget
void addWidget(QWidget *widget)
Definition: statusbarlayout.cpp:120
Kasten2::StatusBarLayout::mSizeHint
QSize mSizeHint
Definition: statusbarlayout.h:71
Kasten2::StatusBarLayout::mIsEmpty
bool mIsEmpty
Definition: statusbarlayout.h:70
Kasten2::StatusBarLayout::invalidate
virtual void invalidate()
Definition: statusbarlayout.cpp:112
Kasten2::StatusBarLayout::itemAt
virtual QLayoutItem * itemAt(int index) const
Definition: statusbarlayout.cpp:55
Kasten2::StatusBarLayout::setGeometry
virtual void setGeometry(const QRect &rect)
Definition: statusbarlayout.cpp:146
Kasten2::StatusBarLayout::sizeHint
virtual QSize sizeHint() const
Definition: statusbarlayout.cpp:77
Kasten2::StatusBarLayout::mIsDirty
bool mIsDirty
Definition: statusbarlayout.h:69
Kasten2::StatusBarLayout::mWidgetList
QList< QWidgetItem * > mWidgetList
Definition: statusbarlayout.h:67
Kasten2::StatusBarLayout::takeAt
virtual QLayoutItem * takeAt(int index)
Definition: statusbarlayout.cpp:99
Kasten2::StatusBarLayout::StatusBarLayout
StatusBarLayout(QWidget *parent=0)
Definition: statusbarlayout.cpp:36
Kasten2::StatusBarLayout::minimumSize
virtual QSize minimumSize() const
Definition: statusbarlayout.cpp:84
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:09 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal