amuse/Editor/StatusBarWidget.hpp

92 lines
2.6 KiB
C++
Raw Normal View History

#ifndef AMUSE_STATUSBAR_WIDGET_HPP
#define AMUSE_STATUSBAR_WIDGET_HPP
#include <QStatusBar>
2018-07-18 07:39:26 +00:00
#include <QLabel>
2018-07-28 04:34:29 +00:00
#include <QPushButton>
2018-07-18 07:39:26 +00:00
class StatusBarFocus;
class StatusBarWidget : public QStatusBar
{
2018-07-18 07:39:26 +00:00
friend class StatusBarFocus;
Q_OBJECT
2018-07-28 04:34:29 +00:00
QLabel m_normalMessage;
QPushButton m_killButton;
QLabel m_voiceCount;
int m_cachedVoiceCount = -1;
2018-07-18 07:39:26 +00:00
StatusBarFocus* m_curFocus = nullptr;
2018-07-28 04:34:29 +00:00
void setKillVisible(bool vis) { m_killButton.setVisible(vis); m_voiceCount.setVisible(vis); }
public:
explicit StatusBarWidget(QWidget* parent = Q_NULLPTR) : QStatusBar(parent)
{
2018-07-28 04:34:29 +00:00
addWidget(&m_normalMessage);
m_killButton.setIcon(QIcon(QStringLiteral(":/icons/IconKill.svg")));
m_killButton.setVisible(false);
m_killButton.setToolTip(tr("Immediately kill active voices"));
m_voiceCount.setVisible(false);
addPermanentWidget(&m_voiceCount);
addPermanentWidget(&m_killButton);
}
2018-07-28 04:34:29 +00:00
void setNormalMessage(const QString& message) { m_normalMessage.setText(message); }
void setVoiceCount(int voices)
{
if (voices != m_cachedVoiceCount)
{
m_voiceCount.setText(QString::number(voices));
m_cachedVoiceCount = voices;
setKillVisible(voices != 0);
}
}
void connectKillClicked(const QObject* receiver, const char* method)
{ connect(&m_killButton, SIGNAL(clicked(bool)), receiver, method); }
};
2018-07-18 07:39:26 +00:00
class StatusBarFocus : public QObject
{
Q_OBJECT
QString m_message;
public:
explicit StatusBarFocus(StatusBarWidget* statusWidget)
: QObject(statusWidget) {}
~StatusBarFocus() { exit(); }
void setMessage(const QString& message)
{
m_message = message;
if (StatusBarWidget* widget = qobject_cast<StatusBarWidget*>(parent()))
{
if (widget->m_curFocus == this)
{
if (m_message.isEmpty())
widget->clearMessage();
else
widget->showMessage(m_message);
}
}
}
void enter()
{
if (StatusBarWidget* widget = qobject_cast<StatusBarWidget*>(parent()))
{
widget->m_curFocus = this;
if (m_message.isEmpty())
widget->clearMessage();
else
widget->showMessage(m_message);
}
}
void exit()
{
if (StatusBarWidget* widget = qobject_cast<StatusBarWidget*>(parent()))
{
if (widget->m_curFocus == this)
{
widget->clearMessage();
widget->m_curFocus = nullptr;
}
}
}
};
#endif //AMUSE_STATUSBAR_WIDGET_HPP