1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor: Store buttons state and calculate style and tooltip

This commit is contained in:
Diadlo 2017-10-28 00:04:37 +03:00
parent 8aa372854f
commit 0a58c2fa5b
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
2 changed files with 120 additions and 62 deletions

View File

@ -38,14 +38,56 @@ static const QSize VOL_MIC_BUTTONS_SIZE{22, 18};
static const short HEAD_LAYOUT_SPACING = 5; static const short HEAD_LAYOUT_SPACING = 5;
static const short MIC_BUTTONS_LAYOUT_SPACING = 4; static const short MIC_BUTTONS_LAYOUT_SPACING = 4;
static const short BUTTONS_LAYOUT_HOR_SPACING = 4; static const short BUTTONS_LAYOUT_HOR_SPACING = 4;
static const QString Green = QStringLiteral("green");
#define STYLE_SHEET(x) Style::getStylesheet(":/ui/" #x "/" #x ".css") #define STYLE_SHEET(x) Style::getStylesheet(":/ui/" #x "/" #x ".css")
#define SET_STYLESHEET(x) (x)->setStyleSheet(STYLE_SHEET(x)) #define SET_STYLESHEET(x) (x)->setStyleSheet(STYLE_SHEET(x))
namespace {
const QString ObjectName[] = {
QString{},
QStringLiteral("green"),
QStringLiteral("red"),
QStringLiteral("yellow"),
QStringLiteral("yellow"),
};
const QString CallToolTip[] = {
ChatFormHeader::tr("Can't start audio call"),
ChatFormHeader::tr("Start audio call"),
ChatFormHeader::tr("End audio call"),
ChatFormHeader::tr("Cancel audio call"),
ChatFormHeader::tr("Accept audio call"),
};
const QString VideoToolTip[] = {
ChatFormHeader::tr("Can't start video call"),
ChatFormHeader::tr("Start video call"),
ChatFormHeader::tr("End video call"),
ChatFormHeader::tr("Cancel video call"),
ChatFormHeader::tr("Accept video call"),
};
const QString VolToolTip[] = {
ChatFormHeader::tr("Sound can be disabled only during a call"),
ChatFormHeader::tr("Unmute call"),
ChatFormHeader::tr("Mute call"),
};
const QString MicToolTip[] = {
ChatFormHeader::tr("Microphone can be muted only during a call"),
ChatFormHeader::tr("Unmute microphone"),
ChatFormHeader::tr("Mute microphone"),
};
}
ChatFormHeader::ChatFormHeader(QWidget* parent) ChatFormHeader::ChatFormHeader(QWidget* parent)
: QWidget(parent) : QWidget(parent)
, mode{Mode::AV} , mode{Mode::AV}
, callState{CallButtonState::Disabled}
, videoState{CallButtonState::Disabled}
, volState{ToolButtonState::Disabled}
, micState{ToolButtonState::Disabled}
{ {
QHBoxLayout* headLayout = new QHBoxLayout(); QHBoxLayout* headLayout = new QHBoxLayout();
avatar = new MaskablePixmapWidget(this, AVATAR_SIZE, ":/img/avatar_mask.svg"); avatar = new MaskablePixmapWidget(this, AVATAR_SIZE, ":/img/avatar_mask.svg");
@ -105,11 +147,7 @@ ChatFormHeader::ChatFormHeader(QWidget* parent)
setLayout(headLayout); setLayout(headLayout);
updateCallButtons(false, false, false); updateButtonsView();
updateMuteMicButton(false, false);
updateMuteVolButton(false, false);
retranslateUi();
Translator::registerHandler(std::bind(&ChatFormHeader::retranslateUi, this), this); Translator::registerHandler(std::bind(&ChatFormHeader::retranslateUi, this), this);
} }
@ -131,38 +169,44 @@ void ChatFormHeader::setMode(ChatFormHeader::Mode mode)
} }
} }
template<class State>
void setStateToolTip(QAbstractButton* btn, State state, const QString toolTip[])
{
const int index = static_cast<int>(state);
btn->setToolTip(toolTip[index]);
}
void ChatFormHeader::retranslateUi() void ChatFormHeader::retranslateUi()
{ {
const QString callObjectName = callButton->objectName(); setStateToolTip(callButton, callState, CallToolTip);
const QString videoObjectName = videoButton->objectName(); setStateToolTip(videoButton, videoState, VideoToolTip);
setStateToolTip(micButton, micState, MicToolTip);
setStateToolTip(volButton, volState, VolToolTip);
}
if (callObjectName == QStringLiteral("green")) { template<class State>
callButton->setToolTip(tr("Start audio call")); void setStateName(QAbstractButton* btn, State state)
} else if (callObjectName == QStringLiteral("yellow")) { {
callButton->setToolTip(tr("Accept audio call")); const int index = static_cast<int>(state);
} else if (callObjectName == QStringLiteral("red")) { btn->setObjectName(ObjectName[index]);
callButton->setToolTip(tr("End audio call")); btn->setEnabled(index != 0);
} else if (callObjectName.isEmpty()) { }
callButton->setToolTip(QString{});
}
if (videoObjectName == QStringLiteral("green")) { void ChatFormHeader::updateButtonsView()
videoButton->setToolTip(tr("Start video call")); {
} else if (videoObjectName == QStringLiteral("yellow")) { setStateName(callButton, callState);
videoButton->setToolTip(tr("Accept video call")); setStateName(videoButton, videoState);
} else if (videoObjectName == QStringLiteral("red")) { setStateName(micButton, micState);
videoButton->setToolTip(tr("End video call")); setStateName(volButton, volState);
} else if (videoObjectName.isEmpty()) { retranslateUi();
videoButton->setToolTip(QString{}); Style::repolish(this);
}
} }
void ChatFormHeader::showOutgoingCall(bool video) void ChatFormHeader::showOutgoingCall(bool video)
{ {
QPushButton* btn = video ? videoButton : callButton; CallButtonState& state = video ? videoState : callState;
btn->setObjectName("yellow"); state = CallButtonState::Outgoing;
Style::repolish(btn); updateButtonsView();
btn->setToolTip(video ? tr("Cancel video call") : tr("Cancel audio call"));
} }
void ChatFormHeader::showCallConfirm(bool video) void ChatFormHeader::showCallConfirm(bool video)
@ -181,56 +225,53 @@ void ChatFormHeader::removeCallConfirm()
void ChatFormHeader::updateCallButtons(bool online, bool audio, bool video) void ChatFormHeader::updateCallButtons(bool online, bool audio, bool video)
{ {
callButton->setEnabled(audio && !video); const bool audioAvaliable = online && (mode & Mode::Audio);
videoButton->setEnabled(video); const bool videoAvaliable = online && (mode & Mode::Video);
if (audio) { if (!audioAvaliable) {
callButton->setObjectName(!video ? "red" : ""); callState = CallButtonState::Disabled;
callButton->setToolTip(!video ? tr("End audio call") : tr("Can't start audio call")); } else if (video) {
callState = CallButtonState::Disabled;
videoButton->setObjectName(video ? "red" : ""); } else if (audio) {
videoButton->setToolTip(video ? tr("End video call") : tr("Can't start video call")); callState = CallButtonState::InCall;
} else { } else {
const bool audioAvaliable = online && (mode & Mode::Audio); callState = CallButtonState::Avaliable;
callButton->setEnabled(audioAvaliable);
callButton->setObjectName(audioAvaliable ? "green" : "");
callButton->setToolTip(audioAvaliable ? tr("Start audio call") : tr("Can't start audio call"));
const bool videoAvaliable = online && (mode & Mode::Video);
videoButton->setEnabled(videoAvaliable);
videoButton->setObjectName(videoAvaliable ? "green" : "");
videoButton->setToolTip(videoAvaliable ? tr("Start video call") : tr("Can't start video call"));
} }
Style::repolish(callButton); if (!videoAvaliable) {
Style::repolish(videoButton); videoState = CallButtonState::Disabled;
} else if (video) {
videoState = CallButtonState::InCall;
} else if (audio) {
videoState = CallButtonState::Disabled;
} else {
videoState = CallButtonState::Avaliable;
}
updateButtonsView();
} }
void ChatFormHeader::updateMuteMicButton(bool active, bool inputMuted) void ChatFormHeader::updateMuteMicButton(bool active, bool inputMuted)
{ {
micButton->setEnabled(active); micButton->setEnabled(active);
if (micButton->isEnabled()) { if (active) {
micButton->setObjectName(inputMuted ? "red" : "green"); micState = inputMuted ? ToolButtonState::On : ToolButtonState::Off;
micButton->setToolTip(inputMuted ? tr("Unmute microphone") : tr("Mute microphone"));
} else { } else {
micButton->setObjectName(""); micState = ToolButtonState::Disabled;
micButton->setToolTip(tr("Microphone can be muted only during a call"));
} }
Style::repolish(micButton); updateButtonsView();
} }
void ChatFormHeader::updateMuteVolButton(bool active, bool outputMuted) void ChatFormHeader::updateMuteVolButton(bool active, bool outputMuted)
{ {
volButton->setEnabled(active); volButton->setEnabled(active);
if (volButton->isEnabled()) { if (active) {
volButton->setObjectName(outputMuted ? "red" : "green"); volState = outputMuted ? ToolButtonState::On : ToolButtonState::Off;
volButton->setToolTip(outputMuted ? tr("Unmute call") : tr("Mute call"));
} else { } else {
volButton->setObjectName(""); volState = ToolButtonState::Disabled;
volButton->setToolTip(tr("Sound can be disabled only during a call"));
} }
Style::repolish(volButton); updateButtonsView();
} }
void ChatFormHeader::setAvatar(const QPixmap &img) void ChatFormHeader::setAvatar(const QPixmap &img)

View File

@ -35,6 +35,18 @@ class ChatFormHeader : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
enum class CallButtonState {
Disabled = 0, // Grey
Avaliable = 1, // Green
InCall = 2, // Red
Outgoing = 3, // Yellow
Incoming = 4, // Yellow
};
enum class ToolButtonState {
Disabled = 0, // Grey
Off = 1, // Green
On = 2, // Red
};
enum Mode { enum Mode {
None = 0, None = 0,
Audio = 1, Audio = 1,
@ -77,6 +89,7 @@ signals:
private slots: private slots:
void onNameChanged(const QString& name); void onNameChanged(const QString& name);
void retranslateUi(); void retranslateUi();
void updateButtonsView();
private: private:
Mode mode; Mode mode;
@ -86,10 +99,14 @@ private:
QPushButton* callButton; QPushButton* callButton;
QPushButton* videoButton; QPushButton* videoButton;
QToolButton* volButton; QToolButton* volButton;
QToolButton* micButton; QToolButton* micButton;
CallButtonState callState;
CallButtonState videoState;
ToolButtonState volState;
ToolButtonState micState;
std::unique_ptr<CallConfirmWidget> callConfirm; std::unique_ptr<CallConfirmWidget> callConfirm;
}; };