From 9877bef7c56b07e715d1f7a6dddc8529387b689b Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Fri, 26 Jan 2007 19:38:32 +0000 Subject: [PATCH] Further optimizations for the status bar: hide the space information if the status bar text does not fit into the remaining width. svn path=/trunk/playground/utils/dolphin/; revision=627486 --- src/dolphinstatusbar.cpp | 44 +++++++++++++++++++++++++---------- src/dolphinstatusbar.h | 15 ++++++++++-- src/statusbarmessagelabel.cpp | 23 ++++++++++++------ src/statusbarmessagelabel.h | 23 ++++++++++++++---- src/statusbarspaceinfo.cpp | 8 ++----- src/statusbarspaceinfo.h | 2 +- 6 files changed, 83 insertions(+), 32 deletions(-) diff --git a/src/dolphinstatusbar.cpp b/src/dolphinstatusbar.cpp index a112a1f64..c81a01c70 100644 --- a/src/dolphinstatusbar.cpp +++ b/src/dolphinstatusbar.cpp @@ -57,7 +57,7 @@ DolphinStatusBar::DolphinStatusBar(DolphinView* parent) : m_messageLabel->setMinimumTextHeight(size.height()); connect(parent, SIGNAL(urlChanged(const KUrl&)), - this, SLOT(updateSpaceInfo(const KUrl&))); + this, SLOT(updateSpaceInfoContent(const KUrl&))); } @@ -71,16 +71,12 @@ void DolphinStatusBar::setMessage(const QString& msg, m_messageLabel->setText(msg); m_messageLabel->setType(type); - if (type == Error) { - // assure that enough space is available for the error message and - // hide the space information and progress information - m_spaceInfo->hide(); + const int widthGap = m_messageLabel->widthGap(); + if (widthGap > 0) { m_progressBar->hide(); m_progressText->hide(); } - else if (!m_progressBar->isVisible()) { - m_spaceInfo->show(); - } + showSpaceInfo(); } DolphinStatusBar::Type DolphinStatusBar::type() const @@ -145,6 +141,12 @@ void DolphinStatusBar::setDefaultText(const QString& text) m_defaultText = text; } +void DolphinStatusBar::resizeEvent(QResizeEvent* event) +{ + QWidget::resizeEvent(event); + QTimer::singleShot(0, this, SLOT(showSpaceInfo())); +} + void DolphinStatusBar::updateProgressInfo() { const bool isErrorShown = (m_messageLabel->type() == Error); @@ -160,15 +162,33 @@ void DolphinStatusBar::updateProgressInfo() // hide the progress information and show the space information m_progressText->hide(); m_progressBar->hide(); - if (m_messageLabel->type() != Error) { - m_spaceInfo->show(); - } + showSpaceInfo(); } } -void DolphinStatusBar::updateSpaceInfo(const KUrl& url) +void DolphinStatusBar::updateSpaceInfoContent(const KUrl& url) { m_spaceInfo->setUrl(url); + showSpaceInfo(); +} + +void DolphinStatusBar::showSpaceInfo() +{ + const int widthGap = m_messageLabel->widthGap(); + const bool isProgressBarVisible = m_progressBar->isVisible(); + + if (m_spaceInfo->isVisible()) { + // The space information is shown currently. Hide it + // if the progress bar is visible or if the status bar + // text does not fit into the available width. + const QSize size(m_progressBar->sizeHint()); + if (isProgressBarVisible || (widthGap > 0)) { + m_spaceInfo->hide(); + } + } + else if (widthGap + m_spaceInfo->width() <= 0) { + m_spaceInfo->show(); + } } #include "dolphinstatusbar.moc" diff --git a/src/dolphinstatusbar.h b/src/dolphinstatusbar.h index 445419c3f..e98e9e512 100644 --- a/src/dolphinstatusbar.h +++ b/src/dolphinstatusbar.h @@ -110,15 +110,26 @@ public: void setDefaultText(const QString& text); const QString& defaultText() const { return m_defaultText; } +protected: + /** @see QWidget::resizeEvent() */ + virtual void resizeEvent(QResizeEvent* event); + private slots: void updateProgressInfo(); /** * Is invoked, when the URL of the DolphinView, where the * statusbar belongs too, has been changed. The space information - * is updated. + * content is updated. + */ + void updateSpaceInfoContent(const KUrl& url); + + /** + * Shows the space information if there is enough room to show it + * without the need to clip the status bar text. If the progress + * bar is shown, the space information won't be shown. */ - void updateSpaceInfo(const KUrl& url); + void showSpaceInfo(); private: StatusBarMessageLabel* m_messageLabel; diff --git a/src/statusbarmessagelabel.cpp b/src/statusbarmessagelabel.cpp index 682aa123b..de66bebba 100644 --- a/src/statusbarmessagelabel.cpp +++ b/src/statusbarmessagelabel.cpp @@ -15,7 +15,7 @@ * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * ***************************************************************************/ #include "statusbarmessagelabel.h" @@ -106,6 +106,13 @@ void StatusBarMessageLabel::setMinimumTextHeight(int min) } } +int StatusBarMessageLabel::widthGap() const +{ + QFontMetrics fontMetrics(font()); + const int defaultGap = 10; + return fontMetrics.width(m_text) - availableTextWidth() + defaultGap; +} + void StatusBarMessageLabel::paintEvent(QPaintEvent* /* event */) { QPainter painter(this); @@ -189,13 +196,10 @@ void StatusBarMessageLabel::assureVisibleText() return; } - - int availableWidth = width() - m_pixmap.width() - pixmapGap() * 2; - QFontMetrics fontMetrics(font()); - QRect bounds(fontMetrics.boundingRect(0, 0, availableWidth, height(), - Qt::AlignVCenter | Qt::TextWordWrap, - m_text)); + const QRect bounds(fontMetrics.boundingRect(0, 0, availableTextWidth(), height(), + Qt::AlignVCenter | Qt::TextWordWrap, + m_text)); int requiredHeight = bounds.height(); if (requiredHeight < m_minTextHeight) { requiredHeight = m_minTextHeight; @@ -204,6 +208,11 @@ void StatusBarMessageLabel::assureVisibleText() updateGeometry(); } +int StatusBarMessageLabel::availableTextWidth() const +{ + return width() - m_pixmap.width() - pixmapGap() * 2; +} + QColor StatusBarMessageLabel::mixColors(const QColor& c1, const QColor& c2, int percent) const diff --git a/src/statusbarmessagelabel.h b/src/statusbarmessagelabel.h index 9aa8ac47d..922465584 100644 --- a/src/statusbarmessagelabel.h +++ b/src/statusbarmessagelabel.h @@ -37,8 +37,6 @@ class QTimer; * is shown in front of the text. For message texts having the type * DolphinStatusBar::Error a dynamic color blending is done to get the * attention from the user. - * - * @author Peter Penz */ class StatusBarMessageLabel : public QWidget { @@ -58,17 +56,34 @@ public: void setMinimumTextHeight(int min); int minimumTextHeight() const { return m_minTextHeight; } + /** + * Returns the gap of the width of the current set text to the + * width of the message label. A gap <= 0 means that the text + * fits into the available width. + */ + int widthGap() const; + protected: - /** @see QWidget::paintEvent */ + /** @see QWidget::paintEvent() */ virtual void paintEvent(QPaintEvent* event); - /** @see QWidget::resizeEvent */ + /** @see QWidget::resizeEvent() */ virtual void resizeEvent(QResizeEvent* event); private slots: void timerDone(); + + /** + * Increases the height of the message label so that + * the given text fits into given area. + */ void assureVisibleText(); + /** + * Returns the available width in pixels for the text. + */ + int availableTextWidth() const; + private: enum State { Default, diff --git a/src/statusbarspaceinfo.cpp b/src/statusbarspaceinfo.cpp index bccda6e68..fca7bdd0b 100644 --- a/src/statusbarspaceinfo.cpp +++ b/src/statusbarspaceinfo.cpp @@ -132,13 +132,9 @@ void StatusBarSpaceInfo::slotFoundMountPoint(const unsigned long& kBSize, update(); } -void StatusBarSpaceInfo::slotDone() +void StatusBarSpaceInfo::showResult() { m_gettingSize = false; - if ((m_kBSize > 0) && (m_kBAvailable > 0)) { - show(); - } - update(); } @@ -160,7 +156,7 @@ void StatusBarSpaceInfo::refresh() const unsigned long&, const QString& ))); connect(job, SIGNAL(done()), - this, SLOT(slotDone())); + this, SLOT(showResult())); job->readDF(mountPoint); } diff --git a/src/statusbarspaceinfo.h b/src/statusbarspaceinfo.h index 6db2cc878..488465705 100644 --- a/src/statusbarspaceinfo.h +++ b/src/statusbarspaceinfo.h @@ -58,7 +58,7 @@ private slots: const unsigned long& kBUsed, const unsigned long& kBAvailable, const QString& mountPoint); - void slotDone(); + void showResult(); /** Refreshs the space information for the current set Url. */ void refresh(); -- 2.47.3