From 718965d78df04cc40ac7047f661905b442363676 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 5 Nov 2014 16:58:11 +0100 Subject: [PATCH] refs #335, managed status bar * auto init if required * auto clear (timer based) if required * to be used in floating widgets --- src/blackgui/managedstatusbar.cpp | 123 ++++++++++++++++++++++++++++++ src/blackgui/managedstatusbar.h | 72 +++++++++++++++++ 2 files changed, 195 insertions(+) create mode 100644 src/blackgui/managedstatusbar.cpp create mode 100644 src/blackgui/managedstatusbar.h diff --git a/src/blackgui/managedstatusbar.cpp b/src/blackgui/managedstatusbar.cpp new file mode 100644 index 000000000..dfb994741 --- /dev/null +++ b/src/blackgui/managedstatusbar.cpp @@ -0,0 +1,123 @@ +/* Copyright (C) 2014 + * swift project Community / Contributors + * + * This file is part of swift Project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "managedstatusbar.h" +#include + +using namespace BlackMisc; + +namespace BlackGui +{ + + CManagedStatusBar::CManagedStatusBar(QObject *parent) : QObject(parent) + { + this->setObjectName("qo_ManagedStatusBar"); + } + + CManagedStatusBar::~CManagedStatusBar() + { + // we are not necessarily the owner of the status bar + this->m_statusBar->removeWidget(this->m_statusBarLabel); + this->m_statusBar->removeWidget(this->m_statusBarIcon); + + // labels will be deleted with status bar + if (this->m_ownStatusBar) { delete m_statusBar; } + } + + void CManagedStatusBar::initStatusBar(QStatusBar *statusBar) + { + if (this->m_statusBar) { return; } + this->m_ownStatusBar = statusBar ? false : true; + this->m_statusBar = statusBar ? statusBar : new QStatusBar(); + if (this->m_statusBar->objectName().isEmpty()) { this->m_statusBar->setObjectName("sb_ManagedStatusBar"); } + + this->m_statusBarIcon = new QLabel(this->m_statusBar); + this->m_statusBarLabel = new QLabel(this->m_statusBar); + this->m_statusBarLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); + this->m_statusBarIcon->setObjectName(QString("lbl_StatusBarIcon").append(this->m_statusBar->objectName())); + this->m_statusBarLabel->setObjectName(QString("lbl_StatusBarLabel").append(this->m_statusBar->objectName())); + + // use insert to insert from left to right + // this keeps any grip on the right size + this->m_statusBar->insertPermanentWidget(0, this->m_statusBarIcon, 0); + this->m_statusBar->insertPermanentWidget(1, this->m_statusBarLabel, 1); + + // timer + this->m_timerStatusBar = new QTimer(this); + this->m_timerStatusBar->setSingleShot(true); + connect(this->m_timerStatusBar, &QTimer::timeout, this, &CManagedStatusBar::ps_clearStatusBar); + + // done when injected status bar + if (this->m_ownStatusBar) + { + // self created status bar + QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); + sizePolicy.setHorizontalStretch(0); + sizePolicy.setVerticalStretch(0); + sizePolicy.setHeightForWidth(this->m_statusBar->sizePolicy().hasHeightForWidth()); + + this->m_statusBar->setSizePolicy(sizePolicy); + this->m_statusBar->setSizeGripEnabled(false); + } + } + + void CManagedStatusBar::show() + { + if (!this->m_statusBar) { return; } + this->m_statusBar->show(); + } + + void CManagedStatusBar::hide() + { + if (!this->m_statusBar) { return; } + this->m_statusBar->hide(); + + // reset minimum width + if (this->m_ownStatusBar) + { + this->m_statusBar->setMinimumWidth(50); + } + } + + void CManagedStatusBar::displayStatusMessage(const CStatusMessage &statusMessage) + { + if (statusMessage.isRedundant()) { return; } + if (statusMessage.wasHandledBy(this)) { return; } + statusMessage.markAsHandledBy(this); + + this->show(); + this->m_timerStatusBar->start(3000); // start / restart + this->m_statusBarIcon->setPixmap(statusMessage.toPixmap()); + this->m_statusBarLabel->setText(statusMessage.getMessage()); + + // restrict size for own status bars + if (this->m_ownStatusBar) + { + QSize size = this->m_statusBar->window()->size(); + int w = qRound(0.95 * size.width()); + this->m_statusBar->setMaximumWidth(w); + } + } + + void CManagedStatusBar::displayStatusMessages(const BlackMisc::CStatusMessageList &statusMessages) + { + foreach(CStatusMessage m, statusMessages) + { + displayStatusMessage(m); + } + } + + void CManagedStatusBar::ps_clearStatusBar() + { + if (!this->m_statusBar) { return; } + this->m_statusBarIcon->clear(); + this->m_statusBarLabel->clear(); + } + +} // namespace diff --git a/src/blackgui/managedstatusbar.h b/src/blackgui/managedstatusbar.h new file mode 100644 index 000000000..de2926b86 --- /dev/null +++ b/src/blackgui/managedstatusbar.h @@ -0,0 +1,72 @@ +/* Copyright (C) 2014 + * swift project Community / Contributors + * + * This file is part of swift Project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKGUI_MANAGEDSTATUSBAR_H +#define BLACKGUI_MANAGEDSTATUSBAR_H + +#include "blackmisc/statusmessagelist.h" +#include "blackmisc/statusmessage.h" + +#include +#include +#include +#include + +namespace BlackGui +{ + + //! Managed status bar + class CManagedStatusBar : public QObject + { + Q_OBJECT + + public: + + //! Constructor + explicit CManagedStatusBar(QObject *parent = nullptr); + + //! Destructor + ~CManagedStatusBar(); + + //! Get the status bar + QStatusBar *getStatusBar() const { return m_statusBar; } + + //! Init + void initStatusBar(QStatusBar *statusBar = nullptr); + + //! Show + void show(); + + //! Hide + void hide(); + + public slots: + //! Display status message + void displayStatusMessage(const BlackMisc::CStatusMessage &statusMessage); + + //! Display status messages + void displayStatusMessages(const BlackMisc::CStatusMessageList &statusMessages); + + private slots: + //! Clear status bar + void ps_clearStatusBar(); + + private: + QStatusBar *m_statusBar = nullptr; //!< the status bar itself + QLabel *m_statusBarIcon = nullptr; //!< status bar icon + QLabel *m_statusBarLabel = nullptr; //!< status bar label + QTimer *m_timerStatusBar = nullptr; //!< cleaning up status bar (own cleaning as I need to clean window / icon) + bool m_ownStatusBar = false; + }; + +} // namespace + +#endif // guard