File indexing completed on 2024-04-21 14:55:58

0001 /* This file is part of the KDE libraries
0002     Copyright (C) 1997 Mark Donohoe (donohoe@kde.org)
0003               (C) 1997,1998, 2000 Sven Radej (radej@kde.org)
0004               (C) 2007 Aron Boström (aron.bostrom@gmail.com)
0005 
0006     This library is free software; you can redistribute it and/or
0007     modify it under the terms of the GNU Library General Public
0008     License as published by the Free Software Foundation; either
0009     version 2 of the License, or (at your option) any later version.
0010 
0011     This library is distributed in the hope that it will be useful,
0012     but WITHOUT ANY WARRANTY; without even the implied warranty of
0013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014     Library General Public License for more details.
0015 
0016     You should have received a copy of the GNU Library General Public License
0017     along with this library; see the file COPYING.LIB.  If not, write to
0018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019     Boston, MA 02110-1301, USA.
0020 */
0021 
0022 #include "kstatusbar.h"
0023 
0024 #include <QHash>
0025 #include <QEvent>
0026 #include <QLabel>
0027 
0028 #include <kdebug.h>
0029 
0030 #include "ksqueezedtextlabel.h"
0031 
0032 class KStatusBarPrivate
0033 {
0034 public:
0035     int id(QObject *object) const
0036     {
0037         QHash<int, QLabel *>::const_iterator it = qFind(items, object);
0038         if (it != items.constEnd()) {
0039             return it.key();
0040         }
0041         // Not found. This happens when a subclass uses an eventFilter too,
0042         // on objects not registered here.
0043         return -1;
0044     }
0045 
0046     QHash<int, QLabel *> items;
0047 };
0048 
0049 bool KStatusBar::eventFilter(QObject *object, QEvent *event)
0050 {
0051     if (event->type() == QEvent::MouseButtonPress) {
0052         const int id = d->id(object);
0053         if (id > -1) {
0054             emit pressed(d->id(object));
0055             return true;
0056         }
0057     } else if (event->type() == QEvent::MouseButtonRelease) {
0058         const int id = d->id(object);
0059         if (id > -1) {
0060             emit released(d->id(object));
0061             return true;
0062         }
0063     }
0064     return QStatusBar::eventFilter(object, event);
0065 }
0066 
0067 KStatusBar::KStatusBar(QWidget *parent)
0068     : QStatusBar(parent),
0069       d(new KStatusBarPrivate)
0070 {
0071 }
0072 
0073 KStatusBar::~KStatusBar()
0074 {
0075     delete d;
0076 }
0077 
0078 void KStatusBar::insertItem(const QString &text, int id, int stretch)
0079 {
0080     if (d->items[id]) {
0081         kDebug() << "KStatusBar::insertItem: item id " << id << " already exists.";
0082     }
0083 
0084     KSqueezedTextLabel *l = new KSqueezedTextLabel(text, this);
0085     l->installEventFilter(this);
0086     l->setFixedHeight(fontMetrics().height() + 2);
0087     l->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
0088     d->items.insert(id, l);
0089     addPermanentWidget(l, stretch);
0090     l->show();
0091 }
0092 
0093 void KStatusBar::insertFixedItem(const QString &text, int id)
0094 {
0095     insertItem(text, id, 0);
0096     setItemFixed(id);
0097 }
0098 
0099 void KStatusBar::insertPermanentItem(const QString &text, int id, int stretch)
0100 {
0101     if (d->items[id]) {
0102         kDebug() << "KStatusBar::insertPermanentItem: item id " << id << " already exists.";
0103     }
0104 
0105     QLabel *l = new QLabel(text, this);
0106     l->installEventFilter(this);
0107     l->setFixedHeight(fontMetrics().height() + 2);
0108     l->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
0109     d->items.insert(id, l);
0110     addPermanentWidget(l, stretch);
0111     l->show();
0112 }
0113 
0114 void KStatusBar::insertPermanentFixedItem(const QString &text, int id)
0115 {
0116     insertPermanentItem(text, id, 0);
0117     setItemFixed(id);
0118 }
0119 
0120 void KStatusBar::removeItem(int id)
0121 {
0122     if (d->items.contains(id)) {
0123         QLabel *label = d->items[id];
0124         removeWidget(label);
0125         d->items.remove(id);
0126         delete label;
0127     } else {
0128         kDebug() << "KStatusBar::removeItem: bad item id: " << id;
0129     }
0130 }
0131 
0132 bool KStatusBar::hasItem(int id) const
0133 {
0134     return d->items.contains(id);
0135 }
0136 
0137 QString KStatusBar::itemText(int id) const
0138 {
0139     if (!hasItem(id)) {
0140         return QString();
0141     }
0142 
0143     return d->items[id]->text();
0144 }
0145 
0146 void KStatusBar::changeItem(const QString &text, int id)
0147 {
0148     QLabel *label = d->items[id];
0149     KSqueezedTextLabel *squeezed = qobject_cast<KSqueezedTextLabel *>(label);
0150 
0151     if (squeezed) {
0152         squeezed->setText(text);
0153     } else if (label) {
0154         label->setText(text);
0155         if (label->minimumWidth() != label->maximumWidth()) {
0156             reformat();
0157         }
0158     } else {
0159         kDebug() << "KStatusBar::changeItem: bad item id: " << id;
0160     }
0161 }
0162 
0163 void KStatusBar::setItemAlignment(int id, Qt::Alignment alignment)
0164 {
0165     QLabel *label = qobject_cast<QLabel *>(d->items[id]);
0166     if (label) {
0167         label->setAlignment(alignment);
0168     } else {
0169         kDebug() << "KStatusBar::setItemAlignment: bad item id: " << id;
0170     }
0171 }
0172 
0173 void KStatusBar::setItemFixed(int id, int w)
0174 {
0175     QLabel *label = qobject_cast<QLabel *>(d->items[id]);
0176     if (label) {
0177         if (w == -1) {
0178             w = fontMetrics().boundingRect(label->text()).width() + 3;
0179         }
0180 
0181         label->setFixedWidth(w);
0182     } else {
0183         kDebug() << "KStatusBar::setItemFixed: bad item id: " << id;
0184     }
0185 }
0186 
0187 #include "moc_kstatusbar.cpp"