Status bar can elide messages

This commit is contained in:
Klaus Basan
2018-01-17 03:32:41 +01:00
parent 519fce5a7a
commit 95e28b0f89
2 changed files with 36 additions and 25 deletions

View File

@@ -12,6 +12,7 @@
#include "blackmisc/sequence.h" #include "blackmisc/sequence.h"
#include <QLabel> #include <QLabel>
#include <QFontMetrics>
#include <QSize> #include <QSize>
#include <QSizePolicy> #include <QSizePolicy>
#include <QStatusBar> #include <QStatusBar>
@@ -36,21 +37,21 @@ namespace BlackGui
m_statusBar->removeWidget(m_statusBarIcon); m_statusBar->removeWidget(m_statusBarIcon);
// labels will be deleted with status bar // labels will be deleted with status bar
if (m_ownStatusBar) { delete m_statusBar; } if (m_ownedStatusBar) { delete m_statusBar; }
} }
void CManagedStatusBar::initStatusBar(QStatusBar *statusBar) void CManagedStatusBar::initStatusBar(QStatusBar *statusBar)
{ {
if (m_statusBar) { return; } if (m_statusBar) { return; }
m_ownStatusBar = statusBar ? false : true; m_ownedStatusBar = statusBar ? false : true;
m_statusBar = statusBar ? statusBar : new QStatusBar(); m_statusBar = statusBar ? statusBar : new QStatusBar();
if (m_statusBar->objectName().isEmpty()) { m_statusBar->setObjectName("sb_ManagedStatusBar"); } if (m_statusBar->objectName().isEmpty()) { m_statusBar->setObjectName("sb_ManagedStatusBar"); }
if (m_ownStatusBar) { m_statusBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);} if (m_ownedStatusBar) { m_statusBar->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);}
m_statusBarIcon = new QLabel(m_statusBar); m_statusBarIcon = new QLabel(m_statusBar);
m_statusBarIcon->setObjectName(QString("lbl_StatusBarIcon").append(m_statusBar->objectName()));
m_statusBarLabel = new QLabel(m_statusBar); m_statusBarLabel = new QLabel(m_statusBar);
m_statusBarLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred); m_statusBarLabel->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
m_statusBarIcon->setObjectName(QString("lbl_StatusBarIcon").append(m_statusBar->objectName()));
m_statusBarLabel->setObjectName(QString("lbl_StatusBarLabel").append(m_statusBar->objectName())); m_statusBarLabel->setObjectName(QString("lbl_StatusBarLabel").append(m_statusBar->objectName()));
// use insert to insert from left to right // use insert to insert from left to right
@@ -59,13 +60,12 @@ namespace BlackGui
m_statusBar->insertPermanentWidget(1, m_statusBarLabel, 1); // status text m_statusBar->insertPermanentWidget(1, m_statusBarLabel, 1); // status text
// timer // timer
m_timerStatusBar = new QTimer(this); m_timerStatusBar.setObjectName(this->objectName().append(":m_timerStatusBar"));
m_timerStatusBar->setObjectName(this->objectName().append(":m_timerStatusBar")); m_timerStatusBar.setSingleShot(true);
m_timerStatusBar->setSingleShot(true); connect(&m_timerStatusBar, &QTimer::timeout, this, &CManagedStatusBar::clearStatusBar);
connect(m_timerStatusBar, &QTimer::timeout, this, &CManagedStatusBar::clearStatusBar);
// done when injected status bar // done when injected status bar
if (m_ownStatusBar) if (m_ownedStatusBar)
{ {
// self created status bar // self created status bar
QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
@@ -91,7 +91,7 @@ namespace BlackGui
m_statusBar->hide(); m_statusBar->hide();
// reset minimum width // reset minimum width
if (m_ownStatusBar) if (m_ownedStatusBar)
{ {
m_statusBar->setMinimumWidth(50); m_statusBar->setMinimumWidth(50);
} }
@@ -101,8 +101,10 @@ namespace BlackGui
{ {
Q_ASSERT_X(m_statusBarIcon, Q_FUNC_INFO, "Missing status bar icon"); Q_ASSERT_X(m_statusBarIcon, Q_FUNC_INFO, "Missing status bar icon");
Q_ASSERT_X(m_statusBar, Q_FUNC_INFO, "Missing status bar"); Q_ASSERT_X(m_statusBar, Q_FUNC_INFO, "Missing status bar");
Q_ASSERT_X(m_statusBarLabel, Q_FUNC_INFO, "Missing label");
// already displaying a message with severity higher than this one? // already displaying a message with severity higher than this one?
if (statusMessage.isEmpty()) { return; }
if (!statusMessage.isSeverityHigherOrEqual(m_currentSeverity)) { return; } if (!statusMessage.isSeverityHigherOrEqual(m_currentSeverity)) { return; }
// used with log subscriber, make sure it is not displayed twice // used with log subscriber, make sure it is not displayed twice
@@ -110,25 +112,32 @@ namespace BlackGui
statusMessage.markAsHandledBy(this); statusMessage.markAsHandledBy(this);
this->show(); this->show();
m_timerStatusBar->start(3000); // start / restart m_timerStatusBar.start(3000); // start / restart
m_statusBarIcon->setPixmap(statusMessage.toPixmap()); m_statusBarIcon->setPixmap(statusMessage.toPixmap());
m_statusBarLabel->setText(statusMessage.getMessage());
m_currentSeverity = statusMessage.getSeverity();
// restrict size for own status bars // restrict size for own status bars
if (m_ownStatusBar) const QSize size = m_statusBar->window()->size();
const int w = qRound(0.95 * size.width());
if (m_ownedStatusBar) { m_statusBar->setMaximumWidth(w); }
if (m_elideMode != Qt::ElideNone)
{ {
QSize size = m_statusBar->window()->size(); const QFontMetrics metrics(m_statusBarLabel->font());
int w = qRound(0.95 * size.width()); const QString elidedText = metrics.elidedText(statusMessage.getMessage(), m_elideMode, 0.90 * w);
m_statusBar->setMaximumWidth(w); m_statusBarLabel->setText(elidedText);
} }
else
{
m_statusBarLabel->setText(statusMessage.getMessage());
}
m_currentSeverity = statusMessage.getSeverity();
} }
void CManagedStatusBar::displayStatusMessages(const BlackMisc::CStatusMessageList &statusMessages) void CManagedStatusBar::displayStatusMessages(const CStatusMessageList &statusMessages)
{ {
foreach (CStatusMessage m, statusMessages) foreach (CStatusMessage m, statusMessages)
{ {
displayStatusMessage(m); this->displayStatusMessage(m);
} }
} }
@@ -139,5 +148,4 @@ namespace BlackGui
m_statusBarIcon->clear(); m_statusBarIcon->clear();
m_statusBarLabel->clear(); m_statusBarLabel->clear();
} }
} // namespace } // namespace

View File

@@ -16,10 +16,10 @@
#include "blackmisc/statusmessage.h" #include "blackmisc/statusmessage.h"
#include "blackmisc/statusmessagelist.h" #include "blackmisc/statusmessagelist.h"
#include <QObject> #include <QObject>
#include <QTimer>
class QLabel; class QLabel;
class QStatusBar; class QStatusBar;
class QTimer;
namespace BlackGui namespace BlackGui
{ {
@@ -47,7 +47,9 @@ namespace BlackGui
//! Hide //! Hide
void hide(); void hide();
public slots: //! Set the label elide mode
void setElideMode(Qt::TextElideMode mode) { m_elideMode = mode; }
//! Display status message //! Display status message
void displayStatusMessage(const BlackMisc::CStatusMessage &statusMessage); void displayStatusMessage(const BlackMisc::CStatusMessage &statusMessage);
@@ -61,9 +63,10 @@ namespace BlackGui
QStatusBar *m_statusBar = nullptr; //!< the status bar itself QStatusBar *m_statusBar = nullptr; //!< the status bar itself
QLabel *m_statusBarIcon = nullptr; //!< status bar icon QLabel *m_statusBarIcon = nullptr; //!< status bar icon
QLabel *m_statusBarLabel = nullptr; //!< status bar label QLabel *m_statusBarLabel = nullptr; //!< status bar label
QTimer *m_timerStatusBar = nullptr; //!< cleaning up status bar (own cleaning as I need to clean window / icon) QTimer m_timerStatusBar { this }; //!< cleaning up status bar (own cleaning as I need to clean window / icon)
bool m_ownStatusBar = false; bool m_ownedStatusBar = false; //!< own status bar or "injected"
BlackMisc::StatusSeverity m_currentSeverity = BlackMisc::StatusSeverity::SeverityDebug; // severity currently displayed Qt::TextElideMode m_elideMode = Qt::ElideMiddle; //!< label text elide
BlackMisc::StatusSeverity m_currentSeverity = BlackMisc::StatusSeverity::SeverityDebug; //!< severity currently displayed
}; };
} // namespace } // namespace