Skip to content
Snippets Groups Projects
Commit 1f3eca55 authored by Andreas Traczyk's avatar Andreas Traczyk
Browse files

overlay button: remove duplicate files, code cleanup

Change-Id: Ic4c20a1a5329400a52a5a3258f07ce42f9aa0e32
parent 5383bc2a
No related branches found
No related tags found
No related merge requests found
/**************************************************************************
* Copyright (C) 2019 by Savoir-faire Linux *
* Author: Yang Wang <yang.wang@savoirfairelinux.com> *
* *
* 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 *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
**************************************************************************/
#include "overlaybutton.h"
#include "utils.h"
#include <QEvent>
OverlayButton::OverlayButton(QWidget *parent)
: QPushButton(parent)
{
btnCallbacks_.push_back(
[this](QEvent* event) {
if (event->type() == QEvent::HoverEnter) isHovered_ = true;
return true;
});
btnCallbacks_.push_back(
[this](QEvent* event) {
if (event->type() == QEvent::HoverLeave) isHovered_ = false;
return true;
});
connect(this, SIGNAL(toggled(bool)), this, SLOT(slotOnToggle(bool)));
}
OverlayButton::~OverlayButton()
{
}
void
OverlayButton::setOriginPix(QPixmap originPixPath)
{
pathOriginal_ = originPixPath;
}
QPixmap
OverlayButton::getOriginPix() const
{
return pathOriginal_;
}
void
OverlayButton::setCheckedPix(QPixmap checkedPixPath)
{
pathChecked_ = checkedPixPath;
}
QPixmap
OverlayButton::getCheckedPix() const
{
return pathChecked_;
}
void
OverlayButton::setTintColor(QColor tint_color)
{
tintColor_ = tint_color;
}
QColor
OverlayButton::getTintColor() const
{
return tintColor_;
}
void
OverlayButton::updateIcon(QEvent* event)
{
if (!event)
return;
if (event->type() != QEvent::HoverEnter &&
event->type() != QEvent::HoverLeave) {
return;
}
setButtonIcon();
}
void
OverlayButton::setButtonIcon()
{
if (isSelected_) {
if (isHovered_) {
setIcon(tintCheckedIc_);
} else {
setIcon(checkedIc_);
}
} else {
if (isHovered_) {
setIcon(tintOriginIc_);
} else {
setIcon(originIc_);
}
}
}
bool
OverlayButton::event(QEvent* event)
{
QPushButton::event(event);
if (isFirstTime_) {
originIc_ = QPixmap(pathOriginal_);
checkedIc_ = QPixmap(pathChecked_);
tintOriginIc_ = Utils::generateTintedPixmap(originIc_, tintColor_);
tintCheckedIc_ = Utils::generateTintedPixmap(checkedIc_, tintColor_);
setIcon(originIc_);
isFirstTime_ = false;
}
bool isHandled = false;
// iterate the handlers of this class
for (auto cb : btnCallbacks_) {
isHandled = cb(event);
}
// emit the signal so that other callback can be defined outside of this class
updateIcon(event);
emit signalBtnEvent(event);
return isHandled;
}
void
OverlayButton::slotOnToggle(bool checked)
{
isSelected_ = checked;
setButtonIcon();
}
void
OverlayButton::resetToOriginal()
{
setIcon(originIc_);
}
void
OverlayButton::setOverlayButtonChecked(bool checked)
{
Utils::whileBlocking(this)->setChecked(checked);
isSelected_ = checked;
setButtonIcon();
}
/**************************************************************************
* Copyright (C) 2019 by Savoir-faire Linux *
* Author: Yang Wang <yang.wang@savoirfairelinux.com> *
* *
* 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 *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
**************************************************************************/
#pragma once
#include "utils.h"
#include <QColor>
#include <QImage>
#include <QPushButton>
#include <functional>
class OverlayButton : public QPushButton {
Q_OBJECT;
Q_PROPERTY(QPixmap originPix READ getOriginPix WRITE setOriginPix DESIGNABLE true NOTIFY originPixChanged);
Q_PROPERTY(QPixmap checkedPix READ getCheckedPix WRITE setCheckedPix DESIGNABLE true NOTIFY checkedPixChanged);
Q_PROPERTY(QColor tintColor READ getTintColor WRITE setTintColor DESIGNABLE true NOTIFY tintColorChanged);
public:
using EventCallback = std::function<bool(QEvent*)>;
OverlayButton(QWidget*);
virtual ~OverlayButton();
void resetToOriginal();
void setOverlayButtonChecked(bool);
public:
void setOriginPix(QPixmap);
QPixmap getOriginPix() const;
void setCheckedPix(QPixmap);
QPixmap getCheckedPix() const;
void setTintColor(QColor);
QColor getTintColor() const;
signals:
void signalBtnEvent(QEvent* event);
void originPixChanged(QString);
void checkedPixChanged(QString);
void tintColorChanged(QColor);
protected:
virtual bool event(QEvent* event);
private:
void updateIcon(QEvent* event);
void setButtonIcon();
private slots:
void slotOnToggle(bool);
private:
bool isHovered_ = false;
bool isSelected_ = false;
bool isFirstTime_ = true;
QPixmap pathOriginal_;
QPixmap pathChecked_;
QPixmap originIc_;
QPixmap tintOriginIc_;
QPixmap checkedIc_;
QPixmap tintCheckedIc_;
QColor tintColor_;
std::vector<EventCallback> btnCallbacks_;
};
...@@ -24,52 +24,44 @@ ...@@ -24,52 +24,44 @@
OverlayButton::OverlayButton(QWidget *parent) OverlayButton::OverlayButton(QWidget *parent)
: QPushButton(parent) : QPushButton(parent)
{ {
btnCallbacks_.push_back( connect(this, SIGNAL(toggled(bool)),
[this](QEvent* event) { this, SLOT(slotToggled(bool)));
if (event->type() == QEvent::HoverEnter) isHovered_ = true;
return true;
});
btnCallbacks_.push_back(
[this](QEvent* event) {
if (event->type() == QEvent::HoverLeave) isHovered_ = false;
return true;
});
connect(this, SIGNAL(toggled(bool)), this, SLOT(slotOnToggle(bool)));
} }
OverlayButton::~OverlayButton() OverlayButton::~OverlayButton()
{ {}
}
void void
OverlayButton::setOriginPix(QPixmap originPixPath) OverlayButton::setOriginPix(QPixmap originPixPath)
{ {
pathOriginal_ = originPixPath; normalPixmap_ = originPixPath;
setIcon(normalPixmap_);
} }
QPixmap QPixmap
OverlayButton::getOriginPix() const OverlayButton::getOriginPix() const
{ {
return pathOriginal_; return normalPixmap_;
} }
void void
OverlayButton::setCheckedPix(QPixmap checkedPixPath) OverlayButton::setCheckedPix(QPixmap checkedPixPath)
{ {
pathChecked_ = checkedPixPath; checkedPixmap_ = checkedPixPath;
} }
QPixmap QPixmap
OverlayButton::getCheckedPix() const OverlayButton::getCheckedPix() const
{ {
return pathChecked_; return checkedPixmap_;
} }
void void
OverlayButton::setTintColor(QColor tint_color) OverlayButton::setTintColor(QColor tint_color)
{ {
tintColor_ = tint_color; tintColor_ = tint_color;
tintNormalPixmap_ = Utils::generateTintedPixmap(normalPixmap_, tintColor_);
tintCheckedPixmap_ = Utils::generateTintedPixmap(checkedPixmap_, tintColor_);
} }
QColor QColor
...@@ -79,72 +71,42 @@ OverlayButton::getTintColor() const ...@@ -79,72 +71,42 @@ OverlayButton::getTintColor() const
} }
void void
OverlayButton::updateIcon(QEvent* event) OverlayButton::updateIcon()
{
if (!event)
return;
if (event->type() != QEvent::HoverEnter &&
event->type() != QEvent::HoverLeave) {
return;
}
setButtonIcon();
}
void
OverlayButton::setButtonIcon()
{ {
if (isSelected_) { if (isSelected_) {
if (isHovered_) { setIcon(isHovered_ ? tintCheckedPixmap_ : checkedPixmap_);
setIcon(tintCheckedIc_);
} else {
setIcon(checkedIc_);
}
} else {
if (isHovered_) {
setIcon(tintOriginIc_);
} else { } else {
setIcon(originIc_); setIcon(isHovered_ ? tintNormalPixmap_ : normalPixmap_);
}
} }
} }
bool void
OverlayButton::event(QEvent* event) OverlayButton::enterEvent(QEvent* event)
{ {
QPushButton::event(event); Q_UNUSED(event);
isHovered_ = true;
if (isFirstTime_) { updateIcon();
originIc_ = QPixmap(pathOriginal_);
checkedIc_ = QPixmap(pathChecked_);
tintOriginIc_ = Utils::generateTintedPixmap(originIc_, tintColor_);
tintCheckedIc_ = Utils::generateTintedPixmap(checkedIc_, tintColor_);
setIcon(originIc_);
isFirstTime_ = false;
} }
bool isHandled = false; void
// iterate the handlers of this class OverlayButton::leaveEvent(QEvent* event)
for (auto cb : btnCallbacks_) { {
isHandled = cb(event); Q_UNUSED(event);
} isHovered_ = false;
// emit the signal so that other callback can be defined outside of this class updateIcon();
updateIcon(event);
emit signalBtnEvent(event);
return isHandled;
} }
void void
OverlayButton::slotOnToggle(bool checked) OverlayButton::slotToggled(bool checked)
{ {
isSelected_ = checked; isSelected_ = checked;
setButtonIcon(); updateIcon();
} }
void void
OverlayButton::resetToOriginal() OverlayButton::resetToOriginal()
{ {
setIcon(originIc_); setIcon(normalPixmap_);
} }
void void
...@@ -152,5 +114,5 @@ OverlayButton::setOverlayButtonChecked(bool checked) ...@@ -152,5 +114,5 @@ OverlayButton::setOverlayButtonChecked(bool checked)
{ {
Utils::whileBlocking(this)->setChecked(checked); Utils::whileBlocking(this)->setChecked(checked);
isSelected_ = checked; isSelected_ = checked;
setButtonIcon(); updateIcon();
} }
...@@ -17,26 +17,21 @@ ...@@ -17,26 +17,21 @@
**************************************************************************/ **************************************************************************/
#pragma once #pragma once
#include "utils.h"
#include <QColor> #include <QColor>
#include <QImage> #include <QImage>
#include <QPushButton> #include <QPushButton>
#include <functional>
class OverlayButton : public QPushButton { class OverlayButton : public QPushButton {
Q_OBJECT; Q_OBJECT;
Q_PROPERTY(QPixmap originPix READ getOriginPix WRITE setOriginPix DESIGNABLE true NOTIFY originPixChanged); Q_PROPERTY(QPixmap originPix READ getOriginPix WRITE setOriginPix DESIGNABLE true);
Q_PROPERTY(QPixmap checkedPix READ getCheckedPix WRITE setCheckedPix DESIGNABLE true NOTIFY checkedPixChanged); Q_PROPERTY(QPixmap checkedPix READ getCheckedPix WRITE setCheckedPix DESIGNABLE true);
Q_PROPERTY(QColor tintColor READ getTintColor WRITE setTintColor DESIGNABLE true NOTIFY tintColorChanged); Q_PROPERTY(QColor tintColor READ getTintColor WRITE setTintColor DESIGNABLE true);
public: public:
using EventCallback = std::function<bool(QEvent*)>; explicit OverlayButton(QWidget* parent = nullptr);
~OverlayButton();
OverlayButton(QWidget*);
virtual ~OverlayButton();
void resetToOriginal(); void resetToOriginal();
void setOverlayButtonChecked(bool); void setOverlayButtonChecked(bool);
...@@ -48,32 +43,24 @@ public: ...@@ -48,32 +43,24 @@ public:
void setTintColor(QColor); void setTintColor(QColor);
QColor getTintColor() const; QColor getTintColor() const;
signals:
void signalBtnEvent(QEvent* event);
void originPixChanged(QString);
void checkedPixChanged(QString);
void tintColorChanged(QColor);
protected: protected:
virtual bool event(QEvent* event); void enterEvent(QEvent *event) override;
void leaveEvent(QEvent* event) override;
private: private:
void updateIcon(QEvent* event); void updateIcon();
void setButtonIcon();
private slots: private slots:
void slotOnToggle(bool); void slotToggled(bool);
private: private:
bool isHovered_ = false; bool isHovered_ = false;
bool isSelected_ = false; bool isSelected_ = false;
bool isFirstTime_ = true;
QPixmap pathOriginal_; QPixmap normalPixmap_;
QPixmap pathChecked_; QPixmap checkedPixmap_;
QPixmap originIc_; QPixmap tintNormalPixmap_;
QPixmap tintOriginIc_; QPixmap tintCheckedPixmap_;
QPixmap checkedIc_;
QPixmap tintCheckedIc_;
QColor tintColor_; QColor tintColor_;
std::vector<EventCallback> btnCallbacks_;
}; };
...@@ -109,11 +109,11 @@ VideoOverlay::updateCall(const conversation::Info& convInfo) ...@@ -109,11 +109,11 @@ VideoOverlay::updateCall(const conversation::Info& convInfo)
ui->noVideoButton->setVisible(!isAudioOnly); ui->noVideoButton->setVisible(!isAudioOnly);
// Block the signals of buttons // Block the signals of buttons
Utils::whileBlocking(ui->noMicButton)->setOverlayButtonChecked(isAudioMuted); ui->noMicButton->setOverlayButtonChecked(isAudioMuted);
Utils::whileBlocking(ui->noVideoButton)->setOverlayButtonChecked(isVideoMuted); ui->noVideoButton->setOverlayButtonChecked(isVideoMuted);
Utils::whileBlocking(ui->recButton)->setOverlayButtonChecked(isRecording); ui->recButton->setOverlayButtonChecked(isRecording);
Utils::whileBlocking(ui->holdButton)->setOverlayButtonChecked(isPaused); ui->holdButton->setOverlayButtonChecked(isPaused);
Utils::whileBlocking(ui->onHoldLabel)->setVisible(isPaused); ui->onHoldLabel->setVisible(isPaused);
bool isSIP = accInfo.profileInfo.type == lrc::api::profile::Type::SIP; bool isSIP = accInfo.profileInfo.type == lrc::api::profile::Type::SIP;
ui->addToConferenceButton->setVisible(!isSIP); ui->addToConferenceButton->setVisible(!isSIP);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment