src: refactored UI elements and graphics file structure

* split QtIconButton into multiple files
* moved graph elements from view to graphics
* structured elements into sub directories
This commit is contained in:
Eberhard Graether
2019-05-29 18:31:11 +02:00
parent 1cc61a93a2
commit 841a87469b
117 changed files with 518 additions and 483 deletions
@@ -0,0 +1,38 @@
#include "QtHelpButton.h"
#include <QMessageBox>
#include "ResourcePaths.h"
QtHelpButton::QtHelpButton(const QString& helpTitle, const QString& helpText, QWidget* parent)
: QtIconButton(
ResourcePaths::getGuiPath().concatenate(L"window/help.png"),
ResourcePaths::getGuiPath().concatenate(L"window/help_hover.png"),
parent)
, m_helpTitle(helpTitle)
, m_helpText(helpText)
{
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
setAttribute(Qt::WA_LayoutUsesWidgetRect); // fixes layouting on Mac
setMouseTracking(true);
setToolTip("help");
setIconSize(QSize(16, 16));
setObjectName("helpButton");
leaveEvent(nullptr);
connect(this, &QtHelpButton::clicked, this, &QtHelpButton::handleHelpPress);
}
void QtHelpButton::handleHelpPress()
{
QMessageBox msgBox;
msgBox.setWindowTitle("Sourcetrail");
msgBox.setIcon(QMessageBox::Information);
msgBox.setText("<b>" + m_helpTitle + "</b>");
msgBox.setInformativeText(m_helpText);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
msgBox.exec();
}
@@ -0,0 +1,22 @@
#ifndef QT_HELP_BUTTON_H
#define QT_HELP_BUTTON_H
#include "QtIconButton.h"
class QtHelpButton
: public QtIconButton
{
Q_OBJECT
public:
QtHelpButton(const QString& helpTitle, const QString& helpText, QWidget* parent = nullptr);
private slots:
void handleHelpPress();
private:
QString m_helpTitle;
QString m_helpText;
};
#endif // QT_HELP_BUTTON_H
@@ -0,0 +1,24 @@
#include "QtHoverButton.h"
QtHoverButton::QtHoverButton(QWidget* parent)
: QPushButton("", parent)
{
setAttribute(Qt::WA_LayoutUsesWidgetRect); // fixes layouting on Mac
setMouseTracking(true);
}
void QtHoverButton::enterEvent(QEvent *event)
{
if (isEnabled())
{
emit hoveredIn(this);
}
}
void QtHoverButton::leaveEvent(QEvent *event)
{
if (isEnabled())
{
emit hoveredOut(this);
}
}
@@ -0,0 +1,23 @@
#ifndef QT_HOVER_BUTTON_H
#define QT_HOVER_BUTTON_H
#include <QPushButton>
class QtHoverButton
: public QPushButton
{
Q_OBJECT
public:
QtHoverButton(QWidget* parent = nullptr);
signals:
void hoveredIn(QPushButton*);
void hoveredOut(QPushButton*);
protected:
void enterEvent(QEvent *event);
void leaveEvent(QEvent *event);
};
#endif // QT_HOVER_BUTTON_H
@@ -0,0 +1,56 @@
#include "QtIconButton.h"
#include <QEvent>
#include "utilityQt.h"
QtIconButton::QtIconButton(const FilePath& iconPath, const FilePath& hoveredIconPath, QWidget* parent)
: QPushButton("", parent)
, m_iconPath(iconPath)
, m_hoveredIconPath(hoveredIconPath)
, m_color(Qt::transparent)
{
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
setAttribute(Qt::WA_LayoutUsesWidgetRect); // fixes layouting on Mac
if (!m_hoveredIconPath.empty())
{
setMouseTracking(true);
}
leaveEvent(nullptr);
}
void QtIconButton::setColor(QColor color)
{
m_color = color;
leaveEvent(nullptr);
}
void QtIconButton::enterEvent(QEvent *event)
{
if (!m_hoveredIconPath.empty() && isEnabled())
{
setIconFromPath(m_hoveredIconPath);
}
}
void QtIconButton::leaveEvent(QEvent *event)
{
if (!m_iconPath.empty())
{
setIconFromPath(m_iconPath);
}
}
void QtIconButton::setIconFromPath(const FilePath& path)
{
QPixmap pixmap = QPixmap(QString::fromStdWString(path.wstr()));
if (m_color != Qt::transparent)
{
pixmap = utility::colorizePixmap(pixmap, m_color);
}
setIcon(QIcon(pixmap));
}
@@ -0,0 +1,31 @@
#ifndef QT_ICON_BUTTON_H
#define QT_ICON_BUTTON_H
#include <QPushButton>
#include "FilePath.h"
class QtIconButton
: public QPushButton
{
Q_OBJECT
public:
QtIconButton(const FilePath& iconPath, const FilePath& hoveredIconPath, QWidget* parent = nullptr);
~QtIconButton() = default;
void setColor(QColor color);
protected:
void enterEvent(QEvent *event);
void leaveEvent(QEvent *event);
private:
void setIconFromPath(const FilePath& path);
const FilePath m_iconPath;
const FilePath m_hoveredIconPath;
QColor m_color;
};
#endif // QT_ICON_BUTTON_H
@@ -0,0 +1,107 @@
#include "QtIconStateButton.h"
#include <QEvent>
#include "utilityQt.h"
QtIconStateButton::QtIconStateButton(QWidget* parent)
: QPushButton("", parent)
{
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
setAttribute(Qt::WA_LayoutUsesWidgetRect); // fixes layouting on Mac
setMouseTracking(true);
setObjectName("iconStateButton");
leaveEvent(nullptr);
}
void QtIconStateButton::addState(ButtonState buttonState, const FilePath& iconPath, QColor color)
{
State state;
state.iconPath = iconPath;
state.color = color;
m_states.emplace(buttonState, state);
if (buttonState == STATE_DEFAULT)
{
leaveEvent(nullptr);
}
}
void QtIconStateButton::hoverIn()
{
enterEvent(nullptr);
}
void QtIconStateButton::hoverOut()
{
leaveEvent(nullptr);
}
void QtIconStateButton::changeEvent(QEvent *event)
{
if (event->type() != QEvent::EnabledChange)
{
return;
}
auto it = m_states.find(isEnabled() ? STATE_DEFAULT : STATE_DISABLED);
if (it != m_states.end())
{
setState(it->second);
}
}
void QtIconStateButton::enterEvent(QEvent *event)
{
if (!isEnabled())
{
return;
}
auto it = m_states.find(STATE_HOVERED);
if (it != m_states.end())
{
setState(it->second);
if (event)
{
emit hoveredIn(this);
}
}
}
void QtIconStateButton::leaveEvent(QEvent *event)
{
if (!isEnabled())
{
return;
}
auto it = m_states.find(STATE_DEFAULT);
if (it != m_states.end())
{
setState(it->second);
if (event)
{
emit hoveredOut(this);
}
}
}
void QtIconStateButton::setState(State state)
{
QPixmap pixmap = QPixmap(QString::fromStdWString(state.iconPath.wstr()));
if (state.color != Qt::transparent)
{
pixmap = utility::colorizePixmap(pixmap, state.color);
}
QIcon icon(pixmap);
icon.addPixmap(pixmap, QIcon::Disabled);
setIcon(icon);
}
@@ -0,0 +1,49 @@
#ifndef QT_ICON_STATE_BUTTON_H
#define QT_ICON_STATE_BUTTON_H
#include <QPushButton>
#include "FilePath.h"
class QtIconStateButton
: public QPushButton
{
Q_OBJECT
public:
enum ButtonState
{
STATE_DEFAULT,
STATE_HOVERED,
STATE_DISABLED
};
struct State
{
FilePath iconPath;
QColor color;
};
QtIconStateButton(QWidget* parent = nullptr);
void addState(ButtonState buttonState, const FilePath& iconPath, QColor color = Qt::transparent);
void hoverIn();
void hoverOut();
signals:
void hoveredIn(QPushButton*);
void hoveredOut(QPushButton*);
protected:
void changeEvent(QEvent *event);
void enterEvent(QEvent *event);
void leaveEvent(QEvent *event);
private:
void setState(State state);
std::map<ButtonState, State> m_states;
};
#endif // QT_ICON_STATE_BUTTON_H
@@ -0,0 +1,39 @@
#include "QtSelfRefreshIconButton.h"
#include "utilityQt.h"
QtSelfRefreshIconButton::QtSelfRefreshIconButton(
const QString& text, const FilePath& iconPath, const std::string& buttonKey, QWidget* parent
)
: QPushButton(text, parent)
, m_iconPath(iconPath)
, m_buttonKey(buttonKey)
{
setAttribute(Qt::WA_LayoutUsesWidgetRect); // fixes layouting on Mac
refresh();
}
void QtSelfRefreshIconButton::setIconPath(const FilePath& iconPath)
{
if (iconPath != m_iconPath)
{
m_iconPath = iconPath;
refresh();
}
}
void QtSelfRefreshIconButton::handleMessage(MessageRefreshUI* message)
{
m_onQtThread([this]()
{
refresh();
});
}
void QtSelfRefreshIconButton::refresh()
{
if (!m_iconPath.empty())
{
setIcon(utility::createButtonIcon(m_iconPath, m_buttonKey));
}
}
@@ -0,0 +1,34 @@
#ifndef QT_SELF_REFRESH_ICON_BUTTON_H
#define QT_SELF_REFRESH_ICON_BUTTON_H
#include <QPushButton>
#include "FilePath.h"
#include "MessageListener.h"
#include "MessageRefreshUI.h"
#include "QtThreadedFunctor.h"
class QtSelfRefreshIconButton
: public QPushButton
, public MessageListener<MessageRefreshUI>
{
public:
QtSelfRefreshIconButton(
const QString& text, const FilePath& iconPath, const std::string& buttonKey, QWidget* parent = nullptr);
~QtSelfRefreshIconButton() = default;
void setIconPath(const FilePath& iconPath);
protected:
void handleMessage(MessageRefreshUI* message) override;
virtual void refresh();
private:
QtThreadedLambdaFunctor m_onQtThread;
FilePath m_iconPath;
const std::string m_buttonKey;
};
#endif // QT_SELF_REFRESH_ICON_BUTTON_H