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

KDEUI

  • 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
36class KStatusBarPrivate
37{
38public:
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
53bool 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
72KStatusBar::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
89KStatusBar::~KStatusBar ()
90{
91 delete d;
92}
93
94void 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
109void KStatusBar::insertFixedItem( const QString& text, int id )
110{
111 insertItem( text, id, 0 );
112 setItemFixed( id );
113}
114
115
116void 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
131void KStatusBar::insertPermanentFixedItem( const QString& text, int id )
132{
133 insertPermanentItem( text, id, 0 );
134 setItemFixed( id );
135}
136
137void 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
149bool KStatusBar::hasItem( int id ) const
150{
151 return d->items.contains(id);
152}
153
154QString KStatusBar::itemText( int id ) const
155{
156 if ( !hasItem( id ) ) {
157 return QString();
158 }
159
160 return d->items[id]->text();
161}
162
163void 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
180void 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
190void 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
KConfigGroup
KSharedPtr< KSharedConfig >
KSqueezedTextLabel
A replacement for QLabel that squeezes its text.
Definition: ksqueezedtextlabel.h:47
KSqueezedTextLabel::setAlignment
virtual void setAlignment(Qt::Alignment)
Overridden for internal reasons; the API remains unaffected.
Definition: ksqueezedtextlabel.cpp:126
KSqueezedTextLabel::setText
void setText(const QString &text)
Sets the text.
Definition: ksqueezedtextlabel.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
KStatusBar::removeItem
void removeItem(int id)
Removes an item.
Definition: kstatusbar.cpp:137
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
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
KStatusBar::~KStatusBar
~KStatusBar()
Destructor.
Definition: kstatusbar.cpp:89
KStatusBar::insertFixedItem
void insertFixedItem(const QString &text, int id)
Inserts a fixed width temporary text label into status bar.
Definition: kstatusbar.cpp:109
KStatusBar::setItemAlignment
void setItemAlignment(int id, Qt::Alignment alignment)
Sets the alignment of item id.
Definition: kstatusbar.cpp:180
KStatusBar::released
void released(int)
Emitted when mouse is released over item id.
KStatusBar::KStatusBar
KStatusBar(QWidget *parent=0)
Constructs a status bar.
Definition: kstatusbar.cpp:72
KStatusBar::eventFilter
bool eventFilter(QObject *object, QEvent *event)
Definition: kstatusbar.cpp:53
KStatusBar::itemText
QString itemText(int id) const
The text of an item, if it exists.
Definition: kstatusbar.cpp:154
KStatusBar::setItemFixed
void setItemFixed(int id, int width=-1)
Sets item id to have fixed width.
Definition: kstatusbar.cpp:190
KStatusBar::pressed
void pressed(int)
Emitted when mouse is pressed over item id.
KStatusBar::changeItem
void changeItem(const QString &text, int id)
Changes the text in a status bar field.
Definition: kstatusbar.cpp:163
KStatusBar::insertPermanentFixedItem
void insertPermanentFixedItem(const QString &text, int id)
Inserts a fixed width permanent text label into status bar.
Definition: kstatusbar.cpp:131
QHash
QLabel
QObject
QStatusBar
QWidget
kDebug
#define kDebug
kconfiggroup.h
kdebug.h
kglobal.h
ksharedconfig.h
ksqueezedtextlabel.h
kstatusbar.h
config
KSharedConfigPtr config()
group
group
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 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-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 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
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
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