diff options
| author | Peter Penz <[email protected]> | 2012-04-12 23:57:51 +0200 |
|---|---|---|
| committer | Peter Penz <[email protected]> | 2012-04-13 00:11:36 +0200 |
| commit | 31ee4085c2b2c374158fb956ac376399ff375b5a (patch) | |
| tree | 1e48eb6c8b01a044abab1a186621c47e1f7e8215 /src/statusbar | |
| parent | 2c4e59a16f0a30fb0b6d8ef6cb44efff27166416 (diff) | |
Use KMessageWidget for error- and information-messages
See http://agateau.com/2011/04/21/kde-ux-2011/ for more details.
This simplifies the statusbar in Dolphin a lot and allows to
do a proper eliding in case if status-messages are too long: In
this case a tooltip will show the whole status-message (thanks
to Wolfgang Mader for the initial patch!).
There is still missing some finetuning but the general approach
seems to work quite nice.
BUG: 249638
BUG: 245618
BUG: 146533
FIXED-IN: 4.9.0
Diffstat (limited to 'src/statusbar')
| -rw-r--r-- | src/statusbar/dolphinstatusbar.cpp | 245 | ||||
| -rw-r--r-- | src/statusbar/dolphinstatusbar.h | 97 | ||||
| -rw-r--r-- | src/statusbar/statusbarspaceinfo.cpp | 18 | ||||
| -rw-r--r-- | src/statusbar/statusbarspaceinfo.h | 6 |
4 files changed, 138 insertions, 228 deletions
diff --git a/src/statusbar/dolphinstatusbar.cpp b/src/statusbar/dolphinstatusbar.cpp index 71e86dd60..61ca84e44 100644 --- a/src/statusbar/dolphinstatusbar.cpp +++ b/src/statusbar/dolphinstatusbar.cpp @@ -1,6 +1,5 @@ /*************************************************************************** - * Copyright (C) 2006 by Peter Penz * - * [email protected] * + * Copyright (C) 2006-2012 by Peter Penz <[email protected]> * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -42,86 +41,77 @@ #include <views/dolphinview.h> #include <views/zoomlevelinfo.h> -DolphinStatusBar::DolphinStatusBar(QWidget* parent, DolphinView* view) : +DolphinStatusBar::DolphinStatusBar(QWidget* parent) : QWidget(parent), - m_view(view), - m_messageLabel(0), + m_text(), + m_defaultText(), + m_label(0), m_spaceInfo(0), m_zoomSlider(0), m_progressBar(0), m_stopButton(0), m_progress(100), - m_showProgressBarTimer(0), - m_messageTimeStamp() + m_showProgressBarTimer(0) { - connect(m_view, SIGNAL(urlChanged(KUrl)), - this, SLOT(updateSpaceInfoContent(KUrl))); - - // Initialize message label - m_messageLabel = new KonqStatusBarMessageLabel(this); + // Initialize text label + m_label = new QLabel(this); + m_label->setWordWrap(true); + m_label->installEventFilter(this); // Initialize zoom widget m_zoomSlider = new QSlider(Qt::Horizontal, this); m_zoomSlider->setAccessibleName(i18n("Zoom slider")); m_zoomSlider->setPageStep(1); + m_zoomSlider->setRange(ZoomLevelInfo::minimumLevel(), ZoomLevelInfo::maximumLevel()); - const int min = ZoomLevelInfo::minimumLevel(); - const int max = ZoomLevelInfo::maximumLevel(); - m_zoomSlider->setRange(min, max); - m_zoomSlider->setValue(view->zoomLevel()); - updateZoomSliderToolTip(view->zoomLevel()); - - connect(m_zoomSlider, SIGNAL(valueChanged(int)), this, SLOT(setZoomLevel(int))); + connect(m_zoomSlider, SIGNAL(valueChanged(int)), this, SIGNAL(zoomLevelChanged(int))); connect(m_zoomSlider, SIGNAL(sliderMoved(int)), this, SLOT(showZoomSliderToolTip(int))); - connect(m_view, SIGNAL(zoomLevelChanged(int,int)), this, SLOT(slotZoomLevelChanged(int,int))); - connect(m_view, SIGNAL(previewsShownChanged(bool)), this, SLOT(slotPreviewsShownChanged(bool))); // Initialize space information m_spaceInfo = new StatusBarSpaceInfo(this); - m_spaceInfo->setUrl(m_view->url()); // Initialize progress information m_stopButton = new QToolButton(this); m_stopButton->setIcon(KIcon("process-stop")); m_stopButton->setAccessibleName(i18n("Stop")); - // TODO: Add tooltip for KDE SC 4.7.0, if new strings are allowed again m_stopButton->setAutoRaise(true); + m_stopButton->setToolTip(i18nc("@tooltip", "Stop loading")); m_stopButton->hide(); connect(m_stopButton, SIGNAL(clicked()), this, SIGNAL(stopPressed())); - m_progressText = new QLabel(this); - m_progressText->hide(); + m_progressTextLabel = new QLabel(this); + m_progressTextLabel->hide(); m_progressBar = new QProgressBar(this); m_progressBar->hide(); m_showProgressBarTimer = new QTimer(this); - m_showProgressBarTimer->setInterval(200); + m_showProgressBarTimer->setInterval(500); m_showProgressBarTimer->setSingleShot(true); connect(m_showProgressBarTimer, SIGNAL(timeout()), this, SLOT(updateProgressInfo())); // Initialize top layout and size policies - const int fontHeight = QFontMetrics(m_messageLabel->font()).height(); + const int fontHeight = QFontMetrics(m_label->font()).height(); const int zoomSliderHeight = m_zoomSlider->minimumSizeHint().height(); const int contentHeight = qMax(fontHeight, zoomSliderHeight); - m_messageLabel->setMinimumTextHeight(contentHeight); + m_label->setMinimumHeight(contentHeight); + m_label->setMaximumHeight(contentHeight); + m_label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); - m_spaceInfo->setMaximumSize(200, contentHeight - 5); - m_spaceInfo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); - - m_progressBar->setMaximumSize(200, contentHeight); - m_zoomSlider->setMaximumSize(150, contentHeight); - m_zoomSlider->setMinimumWidth(30); + const QSize size(150, contentHeight); + applyFixedWidgetSize(m_spaceInfo, size); + applyFixedWidgetSize(m_progressBar, size); + applyFixedWidgetSize(m_zoomSlider, size); QHBoxLayout* topLayout = new QHBoxLayout(this); topLayout->setMargin(0); topLayout->setSpacing(4); - topLayout->addWidget(m_messageLabel); + topLayout->addWidget(m_label); topLayout->addWidget(m_zoomSlider); topLayout->addWidget(m_spaceInfo); topLayout->addWidget(m_stopButton); - topLayout->addWidget(m_progressText); + topLayout->addWidget(m_progressTextLabel); topLayout->addWidget(m_progressBar); setExtensionsVisible(true); @@ -131,70 +121,27 @@ DolphinStatusBar::~DolphinStatusBar() { } -void DolphinStatusBar::setMessage(const QString& msg, - Type type) +void DolphinStatusBar::setText(const QString& text) { - int timeout = 1000; // Timeout in milliseconds until default - // messages may overwrite other messages. - - QString message = msg; - if (message.isEmpty()) { - // Show the default text as fallback. An empty text indicates - // a clearing of the information message. - if (m_messageLabel->defaultText().isEmpty()) { - return; - } - message = m_messageLabel->defaultText(); - type = Default; - timeout = 0; - } - - KonqStatusBarMessageLabel::Type konqType = static_cast<KonqStatusBarMessageLabel::Type>(type); - if ((message == m_messageLabel->text()) && (konqType == m_messageLabel->type())) { - // the message is already shown - return; - } - - const QTime currentTime = QTime::currentTime(); - const bool skipMessage = (type == Default) && - m_messageTimeStamp.isValid() && - (m_messageTimeStamp.msecsTo(currentTime) < timeout); - if (skipMessage) { - // A non-default message is shown just for a very short time. Don't hide - // the message by a default message, so that the user gets the chance to - // read the information. - return; + if (m_text != text) { + m_text = text; + updateLabelText(); } - - m_messageLabel->setMessage(message, konqType); - if (type != Default) { - m_messageTimeStamp = currentTime; - } -} - -DolphinStatusBar::Type DolphinStatusBar::type() const -{ - return static_cast<Type>(m_messageLabel->type()); } -QString DolphinStatusBar::message() const +QString DolphinStatusBar::text() const { - return m_messageLabel->text(); + return m_text; } void DolphinStatusBar::setProgressText(const QString& text) { - m_progressText->setText(text); -} - -int DolphinStatusBar::progress() const -{ - return m_progress; + m_progressTextLabel->setText(text); } QString DolphinStatusBar::progressText() const { - return m_progressText->text(); + return m_progressTextLabel->text(); } void DolphinStatusBar::setProgress(int percent) @@ -202,20 +149,9 @@ void DolphinStatusBar::setProgress(int percent) // Show a busy indicator if a value < 0 is provided: m_progressBar->setMaximum((percent < 0) ? 0 : 100); - if (percent < 0) { - percent = 0; - } else if (percent > 100) { - percent = 100; - } - + percent = qBound(0, percent, 100); const bool progressRestarted = (percent < 100) && (percent < m_progress); m_progress = percent; - if (m_messageLabel->type() == KonqStatusBarMessageLabel::Error) { - // Don't update any widget or status bar text if an - // error message is shown - return; - } - if (progressRestarted && !m_progressBar->isVisible()) { // Show the progress bar delayed: In the case if 100 % are reached within // a short time, no progress bar will be shown at all. @@ -229,29 +165,51 @@ void DolphinStatusBar::setProgress(int percent) m_showProgressBarTimer->stop(); updateProgressInfo(); } +} - const QString defaultText = m_messageLabel->defaultText(); - const QString msg(m_messageLabel->text()); - if ((percent == 0) && !msg.isEmpty()) { - setMessage(QString(), Default); - } else if ((percent == 100) && (msg != defaultText)) { - setMessage(defaultText, Default); - } +int DolphinStatusBar::progress() const +{ + return m_progress; } -void DolphinStatusBar::clear() +void DolphinStatusBar::resetToDefaultText() { - setMessage(m_messageLabel->defaultText(), Default); + m_text.clear(); + updateLabelText(); } void DolphinStatusBar::setDefaultText(const QString& text) { - m_messageLabel->setDefaultText(text); + m_defaultText = text; + updateLabelText(); } QString DolphinStatusBar::defaultText() const { - return m_messageLabel->defaultText(); + return m_defaultText; +} + +void DolphinStatusBar::setUrl(const KUrl& url) +{ + m_spaceInfo->setUrl(url); +} + +KUrl DolphinStatusBar::url() const +{ + return m_spaceInfo->url(); +} + +void DolphinStatusBar::setZoomLevel(int zoomLevel) +{ + if (zoomLevel != m_zoomSlider->value()) { + m_zoomSlider->setValue(zoomLevel); + updateZoomSliderToolTip(zoomLevel); + } +} + +int DolphinStatusBar::zoomLevel() const +{ + return m_zoomSlider->value(); } void DolphinStatusBar::readSettings() @@ -265,19 +223,7 @@ void DolphinStatusBar::contextMenuEvent(QContextMenuEvent* event) KMenu menu(this); - QAction* copyAction = 0; - switch (type()) { - case Default: - case OperationCompleted: - case Information: - copyAction = menu.addAction(i18nc("@action:inmenu", "Copy Information Message")); - break; - case Error: - copyAction = menu.addAction(i18nc("@action:inmenu", "Copy Error Message")); - break; - default: break; - } - + QAction* copyAction = menu.addAction(i18nc("@action:inmenu", "Copy Text")); QAction* showZoomSliderAction = menu.addAction(i18nc("@action:inmenu", "Show Zoom Slider")); showZoomSliderAction->setCheckable(true); showZoomSliderAction->setChecked(GeneralSettings::showZoomSlider()); @@ -289,7 +235,7 @@ void DolphinStatusBar::contextMenuEvent(QContextMenuEvent* event) const QAction* action = menu.exec(QCursor::pos()); if (action == copyAction) { QMimeData* mimeData = new QMimeData(); - mimeData->setText(message()); + mimeData->setText(text()); QApplication::clipboard()->setMimeData(mimeData); } else if (action == showZoomSliderAction) { const bool visible = showZoomSliderAction->isChecked(); @@ -302,15 +248,12 @@ void DolphinStatusBar::contextMenuEvent(QContextMenuEvent* event) } } -void DolphinStatusBar::updateSpaceInfoContent(const KUrl& url) -{ - m_spaceInfo->setUrl(url); -} - -void DolphinStatusBar::setZoomLevel(int zoomLevel) +bool DolphinStatusBar::eventFilter(QObject* obj, QEvent* event) { - m_view->setZoomLevel(zoomLevel); - updateZoomSliderToolTip(zoomLevel); + if (obj == m_label && event->type() == QEvent::Resize) { + updateLabelText(); + } + return QWidget::eventFilter(obj, event); } void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel) @@ -323,39 +266,30 @@ void DolphinStatusBar::showZoomSliderToolTip(int zoomLevel) QApplication::sendEvent(m_zoomSlider, &toolTipEvent); } -void DolphinStatusBar::slotZoomLevelChanged(int current, int previous) -{ - Q_UNUSED(previous); - m_zoomSlider->setValue(current); -} - -void DolphinStatusBar::slotPreviewsShownChanged(bool shown) -{ - Q_UNUSED(shown); - // The zoom level might be different with/without previews -> update the zoom slider. - m_zoomSlider->setValue(m_view->zoomLevel()); -} - void DolphinStatusBar::updateProgressInfo() { - const bool isErrorShown = (m_messageLabel->type() == KonqStatusBarMessageLabel::Error); if (m_progress < 100) { // Show the progress information and hide the extensions setExtensionsVisible(false); - if (!isErrorShown) { - m_stopButton->show(); - m_progressText->show(); - m_progressBar->show(); - } } else { // Hide the progress information and show the extensions m_stopButton->hide(); - m_progressText->hide(); + m_progressTextLabel->hide(); m_progressBar->hide(); setExtensionsVisible(true); } } +void DolphinStatusBar::updateLabelText() +{ + const QString text = m_text.isEmpty() ? m_defaultText : m_text; + + QFontMetrics fontMetrics(m_label->font()); + const QString elidedText = fontMetrics.elidedText(text, Qt::ElideRight, m_label->width()); + m_label->setText(elidedText); + m_label->setToolTip(text == elidedText ? QString() : text); +} + void DolphinStatusBar::setExtensionsVisible(bool visible) { bool showSpaceInfo = visible; @@ -374,4 +308,11 @@ void DolphinStatusBar::updateZoomSliderToolTip(int zoomLevel) m_zoomSlider->setToolTip(i18ncp("@info:tooltip", "Size: 1 pixel", "Size: %1 pixels", size)); } +void DolphinStatusBar::applyFixedWidgetSize(QWidget* widget, const QSize& size) +{ + widget->setMinimumSize(size); + widget->setMaximumSize(size); + widget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); +} + #include "dolphinstatusbar.moc" diff --git a/src/statusbar/dolphinstatusbar.h b/src/statusbar/dolphinstatusbar.h index 789656b8e..1a81968a1 100644 --- a/src/statusbar/dolphinstatusbar.h +++ b/src/statusbar/dolphinstatusbar.h @@ -1,6 +1,5 @@ /*************************************************************************** - * Copyright (C) 2006 by Peter Penz * - * [email protected] * + * Copyright (C) 2006-2012 by Peter Penz <[email protected]> * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -21,11 +20,8 @@ #ifndef DOLPHINSTATUSBAR_H #define DOLPHINSTATUSBAR_H -#include "konq_statusbarmessagelabel.h" -#include <QTime> #include <QWidget> -class DolphinView; class KUrl; class StatusBarSpaceInfo; class QLabel; @@ -37,45 +33,18 @@ class QTimer; /** * @brief Represents the statusbar of a Dolphin view. * - * The statusbar allows to show messages and progress - * information. + * The statusbar allows to show messages, progress + * information and space-information of a disk. */ class DolphinStatusBar : public QWidget { Q_OBJECT public: - /** - * Describes the type of the message text. Dependent - * from the type a corresponding icon and color is - * used for the message text. - */ - enum Type { - Default = KonqStatusBarMessageLabel::Default, - OperationCompleted = KonqStatusBarMessageLabel::OperationCompleted, - Information = KonqStatusBarMessageLabel::Information, - Error = KonqStatusBarMessageLabel::Error - }; - - DolphinStatusBar(QWidget* parent, DolphinView* view); - + DolphinStatusBar(QWidget* parent); virtual ~DolphinStatusBar(); - /** - * Sets the message text to \a msg. Dependant - * from the given type \a type an icon is shown and - * the color of the text is adjusted. The height of - * the statusbar is automatically adjusted in a way, - * that the full text fits into the available width. - * - * If a progress is ongoing and a message - * with the type Type::Error is set, the progress - * is cleared automatically. - */ - void setMessage(const QString& msg, Type type); - QString message() const; - - Type type() const; + QString text() const; /** * Sets the text for the progress information. @@ -95,53 +64,53 @@ public: int progress() const; /** - * Clears the message text of the status bar by replacing - * the message with the default text, which can be set - * by DolphinStatusBar::setDefaultText(). The progress - * information is not cleared. + * Replaces the text set by setText() by the text that + * has been set by setDefaultText(). DolphinStatusBar::text() + * will return an empty string afterwards. */ - void clear(); + void resetToDefaultText(); /** * Sets the default text, which is shown if the status bar - * is cleared by DolphinStatusBar::clear(). + * is rest by DolphinStatusBar::resetToDefaultText(). */ void setDefaultText(const QString& text); QString defaultText() const; + KUrl url() const; + int zoomLevel() const; + /** * Refreshes the status bar to get synchronized with the (updated) Dolphin settings. */ void readSettings(); +public slots: + void setText(const QString& text); + void setUrl(const KUrl& url); + void setZoomLevel(int zoomLevel); + signals: /** * Is emitted if the stop-button has been pressed during showing a progress. */ void stopPressed(); + void zoomLevelChanged(int zoomLevel); + protected: - /** @see QWidget::contextMenuEvent() */ virtual void contextMenuEvent(QContextMenuEvent* event); + virtual bool eventFilter(QObject* obj, QEvent* event); private slots: - /** - * Is invoked, when the URL of the DolphinView, where the - * statusbar belongs too, has been changed. The space information - * content is updated. - */ - void updateSpaceInfoContent(const KUrl& url); + void showZoomSliderToolTip(int zoomLevel); + void updateProgressInfo(); /** - * Sets the zoom level of the item view to \a zoomLevel. + * Updates the text for m_label and does an eliding in + * case if the text does not fit into the available width. */ - void setZoomLevel(int zoomLevel); - - void showZoomSliderToolTip(int zoomLevel); - void slotZoomLevelChanged(int current, int previous); - void slotPreviewsShownChanged(bool shown); - - void updateProgressInfo(); + void updateLabelText(); private: /** @@ -158,23 +127,21 @@ private: */ void updateZoomSliderToolTip(int zoomLevel); + void applyFixedWidgetSize(QWidget* widget, const QSize& size); + private: - DolphinView* m_view; - KonqStatusBarMessageLabel* m_messageLabel; + QString m_text; + QString m_defaultText; + QLabel* m_label; StatusBarSpaceInfo* m_spaceInfo; QSlider* m_zoomSlider; - QLabel* m_progressText; + QLabel* m_progressTextLabel; QProgressBar* m_progressBar; QToolButton* m_stopButton; int m_progress; QTimer* m_showProgressBarTimer; - - // Timestamp when the last message has been set that has not the type - // 'Default'. The timestamp is used to prevent that default messages - // hide more important messages after a very short delay. - QTime m_messageTimeStamp; }; #endif diff --git a/src/statusbar/statusbarspaceinfo.cpp b/src/statusbar/statusbarspaceinfo.cpp index 43e6b456d..61b28334a 100644 --- a/src/statusbar/statusbarspaceinfo.cpp +++ b/src/statusbar/statusbarspaceinfo.cpp @@ -35,7 +35,7 @@ StatusBarSpaceInfo::StatusBarSpaceInfo(QWidget* parent) : // Use a timer to update the space information. Polling is useful // here, as files can be deleted/added outside the scope of Dolphin. m_timer = new QTimer(this); - connect(m_timer, SIGNAL(timeout()), this, SLOT(refresh())); + connect(m_timer, SIGNAL(timeout()), this, SLOT(calculateSpaceInfo())); } StatusBarSpaceInfo::~StatusBarSpaceInfo() @@ -44,8 +44,12 @@ StatusBarSpaceInfo::~StatusBarSpaceInfo() void StatusBarSpaceInfo::setUrl(const KUrl& url) { - m_url = url; - refresh(); + if (m_url != url) { + m_url = url; + if (isVisible()) { + calculateSpaceInfo(); + } + } } KUrl StatusBarSpaceInfo::url() const @@ -57,7 +61,7 @@ void StatusBarSpaceInfo::showEvent(QShowEvent* event) { KCapacityBar::showEvent(event); if (!event->spontaneous()) { - refresh(); + calculateSpaceInfo(); m_timer->start(10000); } } @@ -68,12 +72,8 @@ void StatusBarSpaceInfo::hideEvent(QHideEvent* event) KCapacityBar::hideEvent(event); } -void StatusBarSpaceInfo::refresh() +void StatusBarSpaceInfo::calculateSpaceInfo() { - if (!isVisible()) { - return; - } - // KDiskFreeSpace is for local paths only if (!m_url.isLocalFile()) { setText(i18nc("@info:status", "Unknown size")); diff --git a/src/statusbar/statusbarspaceinfo.h b/src/statusbar/statusbarspaceinfo.h index 0a563ad13..1849462a9 100644 --- a/src/statusbar/statusbarspaceinfo.h +++ b/src/statusbar/statusbarspaceinfo.h @@ -51,8 +51,10 @@ protected: void hideEvent(QHideEvent* event); private slots: - /** Refreshes the space information for the current set URL. */ - void refresh(); + /** + * Calculates the space information for the current set URL. + */ + void calculateSpaceInfo(); private: quint64 m_kBSize; |
