From 31ee4085c2b2c374158fb956ac376399ff375b5a Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Thu, 12 Apr 2012 23:57:51 +0200 Subject: [PATCH] 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 --- src/dolphinmainwindow.cpp | 34 ++-- src/dolphinnewfilemenu.cpp | 4 +- src/dolphinviewcontainer.cpp | 130 ++++++++------ src/dolphinviewcontainer.h | 38 +++-- src/statusbar/dolphinstatusbar.cpp | 245 ++++++++++----------------- src/statusbar/dolphinstatusbar.h | 97 ++++------- src/statusbar/statusbarspaceinfo.cpp | 18 +- src/statusbar/statusbarspaceinfo.h | 6 +- 8 files changed, 261 insertions(+), 311 deletions(-) diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp index aca4d15b6..c62d685f6 100644 --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -331,29 +331,23 @@ void DolphinMainWindow::showCommand(CommandType command) DolphinStatusBar* statusBar = m_activeViewContainer->statusBar(); switch (command) { case KIO::FileUndoManager::Copy: - statusBar->setMessage(i18nc("@info:status", "Successfully copied."), - DolphinStatusBar::OperationCompleted); + statusBar->setText(i18nc("@info:status", "Successfully copied.")); break; case KIO::FileUndoManager::Move: - statusBar->setMessage(i18nc("@info:status", "Successfully moved."), - DolphinStatusBar::OperationCompleted); + statusBar->setText(i18nc("@info:status", "Successfully moved.")); break; case KIO::FileUndoManager::Link: - statusBar->setMessage(i18nc("@info:status", "Successfully linked."), - DolphinStatusBar::OperationCompleted); + statusBar->setText(i18nc("@info:status", "Successfully linked.")); break; case KIO::FileUndoManager::Trash: - statusBar->setMessage(i18nc("@info:status", "Successfully moved to trash."), - DolphinStatusBar::OperationCompleted); + statusBar->setText(i18nc("@info:status", "Successfully moved to trash.")); break; case KIO::FileUndoManager::Rename: - statusBar->setMessage(i18nc("@info:status", "Successfully renamed."), - DolphinStatusBar::OperationCompleted); + statusBar->setText(i18nc("@info:status", "Successfully renamed.")); break; case KIO::FileUndoManager::Mkdir: - statusBar->setMessage(i18nc("@info:status", "Created folder."), - DolphinStatusBar::OperationCompleted); + statusBar->setText(i18nc("@info:status", "Created folder.")); break; default: @@ -729,10 +723,7 @@ void DolphinMainWindow::quit() void DolphinMainWindow::showErrorMessage(const QString& message) { - if (!message.isEmpty()) { - DolphinStatusBar* statusBar = m_activeViewContainer->statusBar(); - statusBar->setMessage(message, DolphinStatusBar::Error); - } + m_activeViewContainer->showMessage(message, DolphinViewContainer::Error); } void DolphinMainWindow::slotUndoAvailable(bool available) @@ -1320,7 +1311,10 @@ void DolphinMainWindow::tabDropEvent(int tab, QDropEvent* event) const ViewTab& viewTab = m_viewTab[tab]; const DolphinView* view = viewTab.isPrimaryViewActive ? viewTab.primaryView->view() : viewTab.secondaryView->view(); - DragAndDropHelper::dropUrls(view->rootItem(), view->url(), event); + const QString error = DragAndDropHelper::dropUrls(view->rootItem(), view->url(), event); + if (!error.isEmpty()) { + activeViewContainer()->showMessage(error, DolphinViewContainer::Error); + } } } @@ -2061,7 +2055,7 @@ void DolphinMainWindow::refreshViews() void DolphinMainWindow::clearStatusBar() { - m_activeViewContainer->statusBar()->clear(); + m_activeViewContainer->statusBar()->resetToDefaultText(); } void DolphinMainWindow::connectViewSignals(DolphinViewContainer* container) @@ -2233,8 +2227,8 @@ void DolphinMainWindow::UndoUiInterface::jobError(KIO::Job* job) { DolphinMainWindow* mainWin= qobject_cast(parentWidget()); if (mainWin) { - DolphinStatusBar* statusBar = mainWin->activeViewContainer()->statusBar(); - statusBar->setMessage(job->errorString(), DolphinStatusBar::Error); + DolphinViewContainer* container = mainWin->activeViewContainer(); + container->showMessage(job->errorString(), DolphinViewContainer::Error); } else { KIO::FileUndoManager::UiInterface::jobError(job); } diff --git a/src/dolphinnewfilemenu.cpp b/src/dolphinnewfilemenu.cpp index dcbc57b40..30d79c670 100644 --- a/src/dolphinnewfilemenu.cpp +++ b/src/dolphinnewfilemenu.cpp @@ -44,8 +44,8 @@ DolphinNewFileMenu::~DolphinNewFileMenu() void DolphinNewFileMenu::slotResult(KJob* job) { if (job->error()) { - DolphinStatusBar* statusBar = m_mainWin->activeViewContainer()->statusBar(); - statusBar->setMessage(job->errorString(), DolphinStatusBar::Error); + DolphinViewContainer* container = m_mainWin->activeViewContainer(); + container->showMessage(job->errorString(), DolphinViewContainer::Error); } else { KNewFileMenu::slotResult(job); } diff --git a/src/dolphinviewcontainer.cpp b/src/dolphinviewcontainer.cpp index e10072d2a..9d75ad816 100644 --- a/src/dolphinviewcontainer.cpp +++ b/src/dolphinviewcontainer.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -58,6 +59,7 @@ DolphinViewContainer::DolphinViewContainer(const KUrl& url, QWidget* parent) : m_topLayout(0), m_urlNavigator(0), m_searchBox(0), + m_messageWidget(0), m_view(0), m_filterBar(0), m_statusBar(0), @@ -91,12 +93,14 @@ DolphinViewContainer::DolphinViewContainer(const KUrl& url, QWidget* parent) : connect(m_searchBox, SIGNAL(search(QString)), this, SLOT(startSearching(QString))); connect(m_searchBox, SIGNAL(returnPressed(QString)), this, SLOT(requestFocus())); + m_messageWidget = new KMessageWidget(this); + m_messageWidget->setCloseButtonVisible(true); + m_messageWidget->hide(); + m_view = new DolphinView(url, this); connect(m_view, SIGNAL(urlChanged(KUrl)), m_urlNavigator, SLOT(setUrl(KUrl))); connect(m_view, SIGNAL(writeStateChanged(bool)), this, SIGNAL(writeStateChanged(bool))); connect(m_view, SIGNAL(requestItemInfo(KFileItem)), this, SLOT(showItemInfo(KFileItem))); - connect(m_view, SIGNAL(errorMessage(QString)), this, SLOT(showErrorMessage(QString))); - connect(m_view, SIGNAL(infoMessage(QString)), this, SLOT(showInfoMessage(QString))); connect(m_view, SIGNAL(itemActivated(KFileItem)), this, SLOT(slotItemActivated(KFileItem))); connect(m_view, SIGNAL(redirection(KUrl,KUrl)), this, SLOT(redirect(KUrl,KUrl))); connect(m_view, SIGNAL(directoryLoadingStarted()), this, SLOT(slotDirectoryLoadingStarted())); @@ -104,11 +108,11 @@ DolphinViewContainer::DolphinViewContainer(const KUrl& url, QWidget* parent) : connect(m_view, SIGNAL(itemCountChanged()), this, SLOT(delayedStatusBarUpdate())); connect(m_view, SIGNAL(directoryLoadingProgress(int)), this, SLOT(updateDirectoryLoadingProgress(int))); connect(m_view, SIGNAL(directorySortingProgress(int)), this, SLOT(updateDirectorySortingProgress(int))); - connect(m_view, SIGNAL(infoMessage(QString)), this, SLOT(showInfoMessage(QString))); - connect(m_view, SIGNAL(errorMessage(QString)), this, SLOT(showErrorMessage(QString))); connect(m_view, SIGNAL(selectionChanged(KFileItemList)), this, SLOT(delayedStatusBarUpdate())); - connect(m_view, SIGNAL(operationCompletedMessage(QString)), this, SLOT(showOperationCompletedMessage(QString))); connect(m_view, SIGNAL(urlAboutToBeChanged(KUrl)), this, SLOT(slotViewUrlAboutToBeChanged(KUrl))); + connect(m_view, SIGNAL(errorMessage(QString)), this, SLOT(showErrorMessage(QString))); + connect(m_view, SIGNAL(infoMessage(QString)), this, SLOT(showInfoMessage(QString))); + connect(m_view, SIGNAL(operationCompletedMessage(QString)), m_statusBar, SLOT(setText(QString))); connect(m_urlNavigator, SIGNAL(urlAboutToBeChanged(KUrl)), this, SLOT(slotUrlNavigatorLocationAboutToBeChanged(KUrl))); @@ -117,21 +121,25 @@ DolphinViewContainer::DolphinViewContainer(const KUrl& url, QWidget* parent) : connect(m_urlNavigator, SIGNAL(historyChanged()), this, SLOT(slotHistoryChanged())); - // initialize status bar - m_statusBar = new DolphinStatusBar(this, m_view); - connect(m_statusBar, SIGNAL(stopPressed()), this, SLOT(stopLoading())); + // Initialize status bar + m_statusBar = new DolphinStatusBar(this); + m_statusBar->setUrl(m_view->url()); + m_statusBar->setZoomLevel(m_view->zoomLevel()); + connect(m_view, SIGNAL(urlChanged(KUrl)), m_statusBar, SLOT(setUrl(KUrl))); + connect(m_view, SIGNAL(zoomLevelChanged(int,int)), m_statusBar, SLOT(setZoomLevel(int))); + connect(m_statusBar, SIGNAL(stopPressed()), this, SLOT(stopDirectoryLoading())); + connect(m_statusBar, SIGNAL(zoomLevelChanged(int)), this, SLOT(slotStatusBarZoomLevelChanged(int))); m_statusBarTimer = new QTimer(this); m_statusBarTimer->setSingleShot(true); m_statusBarTimer->setInterval(300); - connect(m_statusBarTimer, SIGNAL(timeout()), - this, SLOT(updateStatusBar())); + connect(m_statusBarTimer, SIGNAL(timeout()), this, SLOT(updateStatusBar())); KIO::FileUndoManager* undoManager = KIO::FileUndoManager::self(); connect(undoManager, SIGNAL(jobRecordingFinished(CommandType)), this, SLOT(delayedStatusBarUpdate())); - // initialize filter bar + // Initialize filter bar m_filterBar = new FilterBar(this); m_filterBar->setVisible(settings->filterBar()); connect(m_filterBar, SIGNAL(filterChanged(QString)), @@ -143,6 +151,7 @@ DolphinViewContainer::DolphinViewContainer(const KUrl& url, QWidget* parent) : m_topLayout->addWidget(m_urlNavigator); m_topLayout->addWidget(m_searchBox); + m_topLayout->addWidget(m_messageWidget); m_topLayout->addWidget(m_view); m_topLayout->addWidget(m_filterBar); m_topLayout->addWidget(m_statusBar); @@ -211,6 +220,30 @@ DolphinSearchBox* DolphinViewContainer::searchBox() return m_searchBox; } +void DolphinViewContainer::showMessage(const QString& msg, MessageType type) +{ + if (msg.isEmpty()) { + return; + } + + m_messageWidget->setText(msg); + + switch (type) { + case Information: m_messageWidget->setMessageType(KMessageWidget::Information); break; + case Warning: m_messageWidget->setMessageType(KMessageWidget::Warning); break; + case Error: m_messageWidget->setMessageType(KMessageWidget::Error); break; + default: + Q_ASSERT(false); + break; + } + + m_messageWidget->setWordWrap(false); + const int unwrappedWidth = m_messageWidget->sizeHint().width(); + m_messageWidget->setWordWrap(unwrappedWidth > size().width()); + + m_messageWidget->animatedShow(); +} + void DolphinViewContainer::readSettings() { if (GeneralSettings::modifiedStartupSettings()) { @@ -322,14 +355,9 @@ void DolphinViewContainer::updateStatusBar() { m_statusBarTimestamp.start(); - const QString newMessage = m_view->statusBarText(); - m_statusBar->setDefaultText(newMessage); - - // We don't want to override errors. Other messages are only protected by - // the Statusbar itself depending on timings (see DolphinStatusBar::setMessage). - if (m_statusBar->type() != DolphinStatusBar::Error) { - m_statusBar->setMessage(newMessage, DolphinStatusBar::Default); - } + const QString text = m_view->statusBarText(); + m_statusBar->setDefaultText(text); + m_statusBar->resetToDefaultText(); } void DolphinViewContainer::updateDirectoryLoadingProgress(int percent) @@ -374,7 +402,7 @@ void DolphinViewContainer::slotDirectoryLoadingCompleted() if (isSearchUrl(url()) && m_view->itemsCount() == 0) { // The dir lister has been completed on a Nepomuk-URI and no items have been found. Instead // of showing the default status bar information ("0 items") a more helpful information is given: - m_statusBar->setMessage(i18nc("@info:status", "No items found."), DolphinStatusBar::Information); + m_statusBar->setText(i18nc("@info:status", "No items found.")); } else { updateStatusBar(); } @@ -424,34 +452,13 @@ void DolphinViewContainer::slotItemActivated(const KFileItem& item) void DolphinViewContainer::showItemInfo(const KFileItem& item) { if (item.isNull()) { - // Only clear the status bar if unimportant messages are shown. - // This prevents that information- or error-messages get hidden - // by moving the mouse above the viewport or when closing the - // context menu. - if (m_statusBar->type() == DolphinStatusBar::Default) { - m_statusBar->clear(); - } + m_statusBar->resetToDefaultText(); } else { - const QString message = item.isDir() ? item.text() : item.getStatusBarInfo(); - m_statusBar->setMessage(message, DolphinStatusBar::Default); + const QString text = item.isDir() ? item.text() : item.getStatusBarInfo(); + m_statusBar->setText(text); } } -void DolphinViewContainer::showInfoMessage(const QString& msg) -{ - m_statusBar->setMessage(msg, DolphinStatusBar::Information); -} - -void DolphinViewContainer::showErrorMessage(const QString& msg) -{ - m_statusBar->setMessage(msg, DolphinStatusBar::Error); -} - -void DolphinViewContainer::showOperationCompletedMessage(const QString& msg) -{ - m_statusBar->setMessage(msg, DolphinStatusBar::OperationCompleted); -} - void DolphinViewContainer::closeFilterBar() { m_filterBar->hide(); @@ -510,8 +517,10 @@ void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl& url) } else if (KProtocolManager::isSourceProtocol(url)) { QString app = "konqueror"; if (url.protocol().startsWith(QLatin1String("http"))) { - showErrorMessage(i18nc("@info:status", - "Dolphin does not support web pages, the web browser has been launched")); + showMessage(i18nc("@info:status", + "Dolphin does not support web pages, the web browser has been launched"), + Information); + const KConfigGroup config(KSharedConfig::openConfig("kdeglobals"), "General"); const QString browser = config.readEntry("BrowserApplication"); if (!browser.isEmpty()) { @@ -522,21 +531,25 @@ void DolphinViewContainer::slotUrlNavigatorLocationChanged(const KUrl& url) } } } else { - showErrorMessage(i18nc("@info:status", - "Protocol not supported by Dolphin, Konqueror has been launched")); + showMessage(i18nc("@info:status", + "Protocol not supported by Dolphin, Konqueror has been launched"), + Information); } const QString secureUrl = KShell::quoteArg(url.pathOrUrl()); const QString command = app + ' ' + secureUrl; KRun::runCommand(command, app, app, this); } else { - showErrorMessage(i18nc("@info:status", "Invalid protocol")); + showMessage(i18nc("@info:status", "Invalid protocol"), Error); } } void DolphinViewContainer::dropUrls(const KUrl& destination, QDropEvent* event) { - DragAndDropHelper::dropUrls(KFileItem(), destination, event); + const QString error = DragAndDropHelper::dropUrls(KFileItem(), destination, event); + if (!error.isEmpty()) { + showMessage(error, Error); + } } void DolphinViewContainer::redirect(const KUrl& oldUrl, const KUrl& newUrl) @@ -588,12 +601,27 @@ void DolphinViewContainer::closeSearchBox() setSearchModeEnabled(false); } -void DolphinViewContainer::stopLoading() +void DolphinViewContainer::stopDirectoryLoading() { m_view->stopLoading(); m_statusBar->setProgress(100); } +void DolphinViewContainer::slotStatusBarZoomLevelChanged(int zoomLevel) +{ + m_view->setZoomLevel(zoomLevel); +} + +void DolphinViewContainer::showErrorMessage(const QString& msg) +{ + showMessage(msg, Error); +} + +void DolphinViewContainer::showInfoMessage(const QString& msg) +{ + showMessage(msg, Information); +} + bool DolphinViewContainer::isSearchUrl(const KUrl& url) const { const QString protocol = url.protocol(); diff --git a/src/dolphinviewcontainer.h b/src/dolphinviewcontainer.h index b3c48ccf9..5e98f5e94 100644 --- a/src/dolphinviewcontainer.h +++ b/src/dolphinviewcontainer.h @@ -33,6 +33,7 @@ #include class FilterBar; +class KMessageWidget; class KUrl; class KUrlNavigator; class DolphinSearchBox; @@ -55,6 +56,13 @@ class DolphinViewContainer : public QWidget Q_OBJECT public: + enum MessageType + { + Information, + Warning, + Error + }; + DolphinViewContainer(const KUrl& url, QWidget* parent); virtual ~DolphinViewContainer(); @@ -83,6 +91,12 @@ public: const DolphinSearchBox* searchBox() const; DolphinSearchBox* searchBox(); + /** + * Shows the message \msg with the given type non-modal above + * the view-content. + */ + void showMessage(const QString& msg, MessageType type); + /** * Refreshes the view container to get synchronized with the (updated) Dolphin settings. */ @@ -183,15 +197,6 @@ private slots: */ void showItemInfo(const KFileItem& item); - /** Shows the information \a msg inside the statusbar. */ - void showInfoMessage(const QString& msg); - - /** Shows the error message \a msg inside the statusbar. */ - void showErrorMessage(const QString& msg); - - /** Shows the "operation completed" message \a msg inside the statusbar. */ - void showOperationCompletedMessage(const QString& msg); - void closeFilterBar(); /** @@ -259,7 +264,19 @@ private slots: * Stops the loading of a directory. Is connected with the "stopPressed" signal * from the statusbar. */ - void stopLoading(); + void stopDirectoryLoading(); + + void slotStatusBarZoomLevelChanged(int zoomLevel); + + /** + * Slot that calls showMessage(msg, Error). + */ + void showErrorMessage(const QString& msg); + + /** + * Slot that calls showMessage(msg, Information). + */ + void showInfoMessage(const QString& msg); private: /** @@ -277,6 +294,7 @@ private: QVBoxLayout* m_topLayout; KUrlNavigator* m_urlNavigator; DolphinSearchBox* m_searchBox; + KMessageWidget* m_messageWidget; DolphinView* m_view; 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 * - * peter.penz@gmx.at * + * Copyright (C) 2006-2012 by Peter Penz * * * * 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 #include -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(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; - } - - m_messageLabel->setMessage(message, konqType); - if (type != Default) { - m_messageTimeStamp = currentTime; + if (m_text != text) { + m_text = text; + updateLabelText(); } } -DolphinStatusBar::Type DolphinStatusBar::type() const -{ - return static_cast(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 * - * peter.penz@gmx.at * + * Copyright (C) 2006-2012 by Peter Penz * * * * 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 #include -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; -- 2.47.3