Big code refactor
The design was defective and I didn't expect the popularity of the project. After these changes the code will be more mantainable and understandable. Among the changes we can see: - A better code structure - Decoupled button widget from its logic - More code reuse - Easier way to add buttons - Specialized classes
This commit is contained in:
@@ -17,10 +17,12 @@
|
||||
|
||||
#include "capturebutton.h"
|
||||
#include "src/capture/capturewidget.h"
|
||||
#include "src/utils/confighandler.h"
|
||||
#include "src/capture/tools/capturetool.h"
|
||||
#include "src/capture/tools/toolfactory.h"
|
||||
#include <QIcon>
|
||||
#include <QPropertyAnimation>
|
||||
#include <QToolTip>
|
||||
#include <QSettings>
|
||||
#include <QMouseEvent>
|
||||
|
||||
// Button represents a single button of the capture widget, it can enable
|
||||
@@ -30,39 +32,28 @@ namespace {
|
||||
const int BUTTON_SIZE = 30;
|
||||
}
|
||||
|
||||
CaptureButton::CaptureButton(const Type t, QWidget *parent) : QPushButton(parent),
|
||||
CaptureButton::CaptureButton(const ButtonType t, QWidget *parent) : QPushButton(parent),
|
||||
m_buttonType(t), m_pressed(false)
|
||||
{
|
||||
initButton();
|
||||
|
||||
if (t == CaptureButton::Type::selectionIndicator) {
|
||||
if (t == TYPE_SELECTIONINDICATOR) {
|
||||
QFont f = this->font();
|
||||
setFont(QFont(f.family(), 7, QFont::Bold));
|
||||
} else {
|
||||
setIcon(getIcon(t));
|
||||
}
|
||||
}
|
||||
|
||||
CaptureButton::CaptureButton(const CaptureButton::Type t, const bool isWhite, QWidget *parent)
|
||||
: QPushButton(parent), m_buttonType(t), m_pressed(false)
|
||||
{
|
||||
initButton();
|
||||
|
||||
if (t == CaptureButton::Type::selectionIndicator) {
|
||||
QFont f = this->font();
|
||||
setFont(QFont(f.family(), 7, QFont::Bold));
|
||||
} else {
|
||||
setIcon(getIcon(t, isWhite));
|
||||
setIcon(getIcon());
|
||||
}
|
||||
}
|
||||
|
||||
void CaptureButton::initButton() {
|
||||
m_tool = ToolFactory().CreateTool(m_buttonType, this);
|
||||
connect(this, &CaptureButton::pressed, m_tool, &CaptureTool::onPressed);
|
||||
|
||||
setFocusPolicy(Qt::NoFocus);
|
||||
resize(BUTTON_SIZE, BUTTON_SIZE);
|
||||
setMouseTracking(true);
|
||||
setMask(QRegion(QRect(-1,-1,BUTTON_SIZE+2, BUTTON_SIZE+2), QRegion::Ellipse));
|
||||
|
||||
setToolTip(getTypeTooltip(m_buttonType));
|
||||
setToolTip(m_tool->getDescription());
|
||||
|
||||
emergeAnimation = new QPropertyAnimation(this, "size", this);
|
||||
emergeAnimation->setEasingCurve(QEasingCurve::InOutQuad);
|
||||
@@ -71,91 +62,55 @@ void CaptureButton::initButton() {
|
||||
emergeAnimation->setEndValue(QSize(BUTTON_SIZE, BUTTON_SIZE));
|
||||
}
|
||||
|
||||
// getIcon returns the icon for the type of button, this method lets
|
||||
// you choose between black or white icons (needed for the config menu)
|
||||
QIcon CaptureButton::getIcon(const Type t, bool isWhite) {
|
||||
QString iconColor = "Black";
|
||||
if (isWhite) {
|
||||
iconColor = "White";
|
||||
}
|
||||
QString path = ":/img/buttonIcons" + iconColor + "/";
|
||||
|
||||
switch (t) {
|
||||
case Type::arrow:
|
||||
path += "arrow-bottom-left.png";
|
||||
break;
|
||||
case Type::circle:
|
||||
path += "circle-outline.png";
|
||||
break;
|
||||
case Type::copy:
|
||||
path += "content-copy.png";
|
||||
break;
|
||||
case Type::exit:
|
||||
path += "close.png";
|
||||
break;
|
||||
case Type::imageUploader:
|
||||
path += "cloud-upload.png";
|
||||
break;
|
||||
case Type::line:
|
||||
path += "line.png";
|
||||
break;
|
||||
case Type::marker:
|
||||
path += "marker.png";
|
||||
break;
|
||||
case Type::pencil:
|
||||
path += "pencil.png";
|
||||
break;
|
||||
case Type::selection:
|
||||
path += "square-outline.png";
|
||||
break;
|
||||
case Type::save:
|
||||
path += "content-save.png";
|
||||
break;
|
||||
case Type::undo:
|
||||
path += "undo-variant.png";
|
||||
break;
|
||||
case Type::move:
|
||||
path += "cursor-move.png";
|
||||
break;
|
||||
case Type::rectangle:
|
||||
path += "square.png";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return QIcon(path);
|
||||
QVector<CaptureButton::ButtonType> CaptureButton::getIterableButtonTypes() {
|
||||
return iterableButtonTypes;
|
||||
}
|
||||
|
||||
QString CaptureButton::getStyle() {
|
||||
QSettings settings;
|
||||
m_mainColor = settings.value("uiColor").value<QColor>();
|
||||
return getStyle(m_mainColor);
|
||||
}
|
||||
|
||||
QString CaptureButton::getStyle(const QColor &mainColor) {
|
||||
m_mainColor = mainColor;
|
||||
QString baseSheet = "Button { border-radius: %3;"
|
||||
QString CaptureButton::getGlobalStyleSheet() {
|
||||
QColor mainColor = ConfigHandler().getUIMainColor();
|
||||
QString baseSheet = "CaptureButton { border-radius: %3;"
|
||||
"background-color: %1; color: %4 }"
|
||||
"Button:hover { background-color: %2; }"
|
||||
"Button:pressed:!hover { "
|
||||
"CaptureButton:hover { background-color: %2; }"
|
||||
"CaptureButton:pressed:!hover { "
|
||||
"background-color: %1; }";
|
||||
|
||||
// define color when mouse is hovering
|
||||
QColor contrast(mainColor.darker(120));
|
||||
if (mainColor.value() < m_colorValueLimit ||
|
||||
mainColor.saturation() > m_colorSaturationLimit) {
|
||||
contrast = mainColor.lighter(140);
|
||||
}
|
||||
|
||||
// foreground color
|
||||
QString color = iconIsWhite(mainColor) ? "white" : "black";
|
||||
QString color = iconIsWhiteByColor(mainColor) ? "white" : "black";
|
||||
|
||||
return baseSheet.arg(mainColor.name()).arg(contrast.name())
|
||||
.arg(BUTTON_SIZE/2).arg(color);
|
||||
}
|
||||
|
||||
QString CaptureButton::getStyleSheet() const {
|
||||
QString baseSheet = "CaptureButton { border-radius: %3;"
|
||||
"background-color: %1; color: %4 }"
|
||||
"CaptureButton:hover { background-color: %2; }"
|
||||
"CaptureButton:pressed:!hover { "
|
||||
"background-color: %1; }";
|
||||
// define color when mouse is hovering
|
||||
QColor contrast(m_mainColor.darker(120));
|
||||
if (m_mainColor.value() < m_colorValueLimit ||
|
||||
m_mainColor.saturation() > m_colorSaturationLimit) {
|
||||
contrast = m_mainColor.lighter(140);
|
||||
}
|
||||
// foreground color
|
||||
QString color = iconIsWhiteByColor(m_mainColor) ? "white" : "black";
|
||||
|
||||
return baseSheet.arg(m_mainColor.name()).arg(contrast.name())
|
||||
.arg(BUTTON_SIZE/2).arg(color);
|
||||
}
|
||||
|
||||
// get icon returns the icon for the type of button
|
||||
QIcon CaptureButton::getIcon(const Type t) {
|
||||
return getIcon(t, iconIsWhite(m_mainColor));
|
||||
QIcon CaptureButton::getIcon() const {
|
||||
QString color(iconIsWhiteByColor(m_mainColor) ? "White" : "Black");
|
||||
QString iconPath = QString(":/img/buttonIcons%1/%2")
|
||||
.arg(color).arg(m_tool->getIconName());
|
||||
return QIcon(iconPath);
|
||||
}
|
||||
|
||||
void CaptureButton::enterEvent(QEvent *e) {
|
||||
@@ -180,6 +135,7 @@ void CaptureButton::mouseReleaseEvent(QMouseEvent *e) {
|
||||
|
||||
void CaptureButton::mousePressEvent(QMouseEvent *) {
|
||||
m_pressed = true;
|
||||
Q_EMIT pressed();
|
||||
}
|
||||
|
||||
void CaptureButton::animatedShow() {
|
||||
@@ -187,20 +143,26 @@ void CaptureButton::animatedShow() {
|
||||
emergeAnimation->start();
|
||||
}
|
||||
|
||||
CaptureButton::Type CaptureButton::getButtonType() const {
|
||||
CaptureButton::ButtonType CaptureButton::getButtonType() const {
|
||||
return m_buttonType;
|
||||
}
|
||||
|
||||
void CaptureButton::updateIconColor(const QColor &c) {
|
||||
setIcon(getIcon(m_buttonType, iconIsWhite(c)));
|
||||
CaptureTool *CaptureButton::getTool() const {
|
||||
return m_tool;
|
||||
}
|
||||
|
||||
void CaptureButton::updateIconColor() {
|
||||
setIcon(getIcon(m_buttonType, iconIsWhite()));
|
||||
void CaptureButton::setColor(const QColor &c) {
|
||||
m_mainColor = c;
|
||||
setStyleSheet(getStyleSheet());
|
||||
setIcon(getIcon());
|
||||
}
|
||||
// iconIsWhite returns true if the passed color would contain a white icon
|
||||
// if applied to a button, and false otherwise
|
||||
bool CaptureButton::iconIsWhite(const QColor &c) {
|
||||
|
||||
// getButtonBaseSize returns the base size of the buttons
|
||||
size_t CaptureButton::getButtonBaseSize() {
|
||||
return BUTTON_SIZE;
|
||||
}
|
||||
|
||||
bool CaptureButton::iconIsWhiteByColor(const QColor &c) {
|
||||
bool isWhite = false;
|
||||
if (c.value() < m_colorValueLimit ||
|
||||
c.saturation() > m_colorSaturationLimit) {
|
||||
@@ -209,63 +171,21 @@ bool CaptureButton::iconIsWhite(const QColor &c) {
|
||||
return isWhite;
|
||||
}
|
||||
|
||||
bool CaptureButton::iconIsWhite() const {
|
||||
return iconIsWhite(m_mainColor);
|
||||
}
|
||||
// getButtonBaseSize returns the base size of the buttons
|
||||
size_t CaptureButton::getButtonBaseSize() {
|
||||
return BUTTON_SIZE;
|
||||
}
|
||||
// getTypeByName receives a name and return the corresponding button type.
|
||||
// returns Button::Type::last when the corresponding button is not found.
|
||||
CaptureButton::Type CaptureButton::getTypeByName(const QString s) {
|
||||
CaptureButton::Type res = Type::last;
|
||||
for (auto i: typeName.toStdMap())
|
||||
if (tr(i.second) == s)
|
||||
res = i.first;
|
||||
return res;
|
||||
}
|
||||
QColor CaptureButton::m_mainColor = ConfigHandler().getUIMainColor();
|
||||
|
||||
QString CaptureButton::getTypeName(const CaptureButton::Type t) {
|
||||
return tr(typeName[t]);
|
||||
}
|
||||
|
||||
QString CaptureButton::getTypeTooltip(const CaptureButton::Type t) {
|
||||
return tr(typeTooltip[t]);
|
||||
}
|
||||
|
||||
CaptureButton::typeData CaptureButton::typeTooltip = {
|
||||
{CaptureButton::Type::selectionIndicator, QT_TR_NOOP("Shows the dimensions of the selection (X Y)")},
|
||||
{CaptureButton::Type::exit, QT_TR_NOOP("Leaves the capture screen")},
|
||||
{CaptureButton::Type::copy, QT_TR_NOOP("Copies the selecion into the clipboard")},
|
||||
{CaptureButton::Type::save, QT_TR_NOOP("Opens the save image window")},
|
||||
{CaptureButton::Type::pencil, QT_TR_NOOP("Sets the Pencil as the paint tool")},
|
||||
{CaptureButton::Type::line, QT_TR_NOOP("Sets the Line as the paint tool")},
|
||||
{CaptureButton::Type::arrow, QT_TR_NOOP("Sets the Arrow as the paint tool")},
|
||||
{CaptureButton::Type::rectangle, QT_TR_NOOP("Sets the Rectangle as the paint tool")},
|
||||
{CaptureButton::Type::circle, QT_TR_NOOP("Sets the Circle as the paint tool")},
|
||||
{CaptureButton::Type::marker, QT_TR_NOOP("Sets the Marker as the paint tool")},
|
||||
{CaptureButton::Type::undo, QT_TR_NOOP("Undo the last modification")},
|
||||
{CaptureButton::Type::imageUploader, QT_TR_NOOP("Uploads the selection to Imgur")},
|
||||
{CaptureButton::Type::selection, QT_TR_NOOP("Sets the Selection as the paint tool")},
|
||||
{CaptureButton::Type::move, QT_TR_NOOP("Move the selection area")}
|
||||
QVector<CaptureButton::ButtonType> CaptureButton::iterableButtonTypes = {
|
||||
CaptureButton::TYPE_PENCIL,
|
||||
CaptureButton::TYPE_LINE,
|
||||
CaptureButton::TYPE_ARROW,
|
||||
CaptureButton::TYPE_SELECTION,
|
||||
CaptureButton::TYPE_RECTANGLE,
|
||||
CaptureButton::TYPE_CIRCLE,
|
||||
CaptureButton::TYPE_MARKER,
|
||||
CaptureButton::TYPE_SELECTIONINDICATOR,
|
||||
CaptureButton::TYPE_MOVESELECTION,
|
||||
CaptureButton::TYPE_UNDO,
|
||||
CaptureButton::TYPE_COPY,
|
||||
CaptureButton::TYPE_SAVE,
|
||||
CaptureButton::TYPE_EXIT,
|
||||
CaptureButton::TYPE_IMAGEUPLOADER,
|
||||
};
|
||||
|
||||
CaptureButton::typeData CaptureButton::typeName = {
|
||||
{CaptureButton::Type::selectionIndicator, QT_TR_NOOP("Selection Size Indicator")},
|
||||
{CaptureButton::Type::exit, QT_TR_NOOP("Exit")},
|
||||
{CaptureButton::Type::copy, QT_TR_NOOP("Copy")},
|
||||
{CaptureButton::Type::save, QT_TR_NOOP("Save")},
|
||||
{CaptureButton::Type::pencil, QT_TR_NOOP("Pencil")},
|
||||
{CaptureButton::Type::line, QT_TR_NOOP("Line")},
|
||||
{CaptureButton::Type::arrow, QT_TR_NOOP("Arrow")},
|
||||
{CaptureButton::Type::rectangle, QT_TR_NOOP("Rectangle")},
|
||||
{CaptureButton::Type::circle, QT_TR_NOOP("Circle")},
|
||||
{CaptureButton::Type::marker, QT_TR_NOOP("Marker")},
|
||||
{CaptureButton::Type::undo, QT_TR_NOOP("Undo")},
|
||||
{CaptureButton::Type::imageUploader, QT_TR_NOOP("Image Uploader")},
|
||||
{CaptureButton::Type::selection, QT_TR_NOOP("Rectangular Selection")},
|
||||
{CaptureButton::Type::move, QT_TR_NOOP("Move")}
|
||||
};
|
||||
|
||||
QColor CaptureButton::m_mainColor = QSettings().value("uiColor").value<QColor>();
|
||||
|
||||
Reference in New Issue
Block a user