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

KDEUI

  • sources
  • kde-4.12
  • kdelibs
  • kdeui
  • widgets
kstatusbar.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 1997 Mark Donohoe (donohoe@kde.org)
3  (C) 1997,1998, 2000 Sven Radej (radej@kde.org)
4  (C) 2007 Aron Boström (aron.bostrom@gmail.com)
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "kstatusbar.h"
23 
24 #include <QtCore/QHash>
25 #include <QtCore/QEvent>
26 #include <QtGui/QLabel>
27 
28 #include <kdebug.h>
29 #include <kglobal.h>
30 #include <ksharedconfig.h>
31 #include <kconfiggroup.h>
32 
33 #include "ksqueezedtextlabel.h"
34 
35 
36 class KStatusBarPrivate
37 {
38 public:
39  int id(QObject* object) const
40  {
41  QHash<int, QLabel*>::const_iterator it = qFind(items, object);
42  if (it != items.constEnd())
43  return it.key();
44  // Not found. This happens when a subclass uses an eventFilter too,
45  // on objects not registered here.
46  return -1;
47  }
48 
49  QHash<int, QLabel*> items;
50 };
51 
52 
53 bool KStatusBar::eventFilter(QObject* object, QEvent* event)
54 {
55  if ( event->type() == QEvent::MouseButtonPress ) {
56  const int id = d->id(object);
57  if (id > -1) {
58  emit pressed( d->id( object ) );
59  return true;
60  }
61  }
62  else if ( event->type() == QEvent::MouseButtonRelease ) {
63  const int id = d->id(object);
64  if (id > -1) {
65  emit released( d->id( object ) );
66  return true;
67  }
68  }
69  return QStatusBar::eventFilter(object, event);
70 }
71 
72 KStatusBar::KStatusBar( QWidget *parent )
73  : QStatusBar( parent ),
74  d(new KStatusBarPrivate)
75 {
76  // make the size grip stuff configurable
77  // ...but off by default (sven)
78  // ...but on by default on OSX, else windows with a KStatusBar are not resizable at all (marijn)
79  KSharedConfig::Ptr config = KGlobal::config();
80  KConfigGroup group( config, QLatin1String("StatusBar style") );
81 #ifdef Q_WS_MAC
82  bool grip_enabled = group.readEntry(QLatin1String("SizeGripEnabled"), true);
83 #else
84  bool grip_enabled = group.readEntry(QLatin1String("SizeGripEnabled"), false);
85 #endif
86  setSizeGripEnabled(grip_enabled);
87 }
88 
89 KStatusBar::~KStatusBar ()
90 {
91  delete d;
92 }
93 
94 void KStatusBar::insertItem( const QString& text, int id, int stretch)
95 {
96  if ( d->items[id] ) {
97  kDebug() << "KStatusBar::insertItem: item id " << id << " already exists.";
98  }
99 
100  KSqueezedTextLabel *l = new KSqueezedTextLabel( text, this );
101  l->installEventFilter( this );
102  l->setFixedHeight( fontMetrics().height() + 2 );
103  l->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
104  d->items.insert( id, l );
105  addPermanentWidget( l, stretch );
106  l->show();
107 }
108 
109 void KStatusBar::insertFixedItem( const QString& text, int id )
110 {
111  insertItem( text, id, 0 );
112  setItemFixed( id );
113 }
114 
115 
116 void KStatusBar::insertPermanentItem( const QString& text, int id, int stretch)
117 {
118  if (d->items[id]) {
119  kDebug() << "KStatusBar::insertPermanentItem: item id " << id << " already exists.";
120  }
121 
122  QLabel *l = new QLabel( text, this );
123  l->installEventFilter( this );
124  l->setFixedHeight( fontMetrics().height() + 2 );
125  l->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
126  d->items.insert( id, l );
127  addPermanentWidget( l, stretch );
128  l->show();
129 }
130 
131 void KStatusBar::insertPermanentFixedItem( const QString& text, int id )
132 {
133  insertPermanentItem( text, id, 0 );
134  setItemFixed( id );
135 }
136 
137 void KStatusBar::removeItem (int id)
138 {
139  if ( d->items.contains( id ) ) {
140  QLabel *label = d->items[id];
141  removeWidget( label );
142  d->items.remove( id );
143  delete label;
144  } else {
145  kDebug() << "KStatusBar::removeItem: bad item id: " << id;
146  }
147 }
148 
149 bool KStatusBar::hasItem( int id ) const
150 {
151  return d->items.contains(id);
152 }
153 
154 QString KStatusBar::itemText( int id ) const
155 {
156  if ( !hasItem( id ) ) {
157  return QString();
158  }
159 
160  return d->items[id]->text();
161 }
162 
163 void KStatusBar::changeItem( const QString& text, int id )
164 {
165  QLabel *label = d->items[id];
166  KSqueezedTextLabel *squeezed = qobject_cast<KSqueezedTextLabel*>( label );
167 
168  if ( squeezed ) {
169  squeezed->setText( text );
170  } else if ( label ) {
171  label->setText( text );
172  if ( label->minimumWidth () != label->maximumWidth () ) {
173  reformat();
174  }
175  } else {
176  kDebug() << "KStatusBar::changeItem: bad item id: " << id;
177  }
178 }
179 
180 void KStatusBar::setItemAlignment (int id, Qt::Alignment alignment)
181 {
182  QLabel *label = qobject_cast<QLabel*>( d->items[id] );
183  if ( label ) {
184  label->setAlignment( alignment );
185  } else {
186  kDebug() << "KStatusBar::setItemAlignment: bad item id: " << id;
187  }
188 }
189 
190 void KStatusBar::setItemFixed(int id, int w)
191 {
192  QLabel *label = qobject_cast<QLabel*>(d->items[id]);
193  if ( label ) {
194  if ( w == -1 ) {
195  w = fontMetrics().boundingRect(label->text()).width()+3;
196  }
197 
198  label->setFixedWidth(w);
199  } else {
200  kDebug() << "KStatusBar::setItemFixed: bad item id: " << id;
201  }
202 }
203 
204 #include "kstatusbar.moc"
205 
KStatusBar::setItemFixed
void setItemFixed(int id, int width=-1)
Sets item id to have fixed width.
Definition: kstatusbar.cpp:190
KSharedPtr< KSharedConfig >
KStatusBar::removeItem
void removeItem(int id)
Removes an item.
Definition: kstatusbar.cpp:137
kdebug.h
KStatusBar::changeItem
void changeItem(const QString &text, int id)
Changes the text in a status bar field.
Definition: kstatusbar.cpp:163
KSqueezedTextLabel::setAlignment
virtual void setAlignment(Qt::Alignment)
Overridden for internal reasons; the API remains unaffected.
Definition: ksqueezedtextlabel.cpp:126
group
KStandardShortcut::label
QString label(StandardShortcut id)
Returns a localized label for user-visible display.
Definition: kstandardshortcut.cpp:267
KSqueezedTextLabel::setText
void setText(const QString &text)
Sets the text.
Definition: ksqueezedtextlabel.cpp:89
QWidget
QString
QHash
kstatusbar.h
QObject
kDebug
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
KStatusBar::eventFilter
bool eventFilter(QObject *object, QEvent *event)
Definition: kstatusbar.cpp:53
KStatusBar::~KStatusBar
~KStatusBar()
Destructor.
Definition: kstatusbar.cpp:89
KStatusBar::insertItem
void insertItem(const QString &text, int id, int stretch=0)
Inserts a temporary text label into the status bar.
Definition: kstatusbar.cpp:94
config
KSharedConfigPtr config()
QStatusBar
kglobal.h
KStatusBar::insertPermanentFixedItem
void insertPermanentFixedItem(const QString &text, int id)
Inserts a fixed width permanent text label into status bar.
Definition: kstatusbar.cpp:131
ksharedconfig.h
KSqueezedTextLabel
A replacement for QLabel that squeezes its text.
Definition: ksqueezedtextlabel.h:47
KStatusBar::released
void released(int)
Emitted when mouse is released over item id.
ksqueezedtextlabel.h
KStatusBar::insertPermanentItem
void insertPermanentItem(const QString &text, int id, int stretch=0)
Inserts a permanent text label into the status bar.
Definition: kstatusbar.cpp:116
KConfigGroup
KStatusBar::itemText
QString itemText(int id) const
The text of an item, if it exists.
Definition: kstatusbar.cpp:154
KStatusBar::setItemAlignment
void setItemAlignment(int id, Qt::Alignment alignment)
Sets the alignment of item id.
Definition: kstatusbar.cpp:180
KStatusBar::KStatusBar
KStatusBar(QWidget *parent=0)
Constructs a status bar.
Definition: kstatusbar.cpp:72
QLabel
KStatusBar::pressed
void pressed(int)
Emitted when mouse is pressed over item id.
KConfigGroup::readEntry
T readEntry(const QString &key, const T &aDefault) const
KStatusBar::insertFixedItem
void insertFixedItem(const QString &text, int id)
Inserts a fixed width temporary text label into status bar.
Definition: kstatusbar.cpp:109
kconfiggroup.h
KStatusBar::hasItem
bool hasItem(int id) const
Returns true if an item with id exists already in KStatusBar, otherwise returns false.
Definition: kstatusbar.cpp:149
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:15 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

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