Use strongly typed enums

remotes/origin/HEAD
Konstantinos Sideris 8 years ago
parent 1f90c58076
commit e44cc374e1
  1. 2
      Makefile
  2. 2
      include/MatrixClient.h
  3. 2
      include/SlidingStackWidget.h
  4. 2
      include/events/Event.h
  5. 2
      include/events/HistoryVisibilityEventContent.h
  6. 2
      include/events/JoinRulesEventContent.h
  7. 2
      include/events/MemberEventContent.h
  8. 2
      include/events/MessageEventContent.h
  9. 18
      include/events/PowerLevelsEventContent.h
  10. 10
      include/ui/FlatButton.h
  11. 18
      include/ui/Theme.h
  12. 24
      src/MatrixClient.cc
  13. 2
      src/TimelineView.cc
  14. 2
      src/ui/CircularProgress.cc
  15. 38
      src/ui/FlatButton.cc
  16. 4
      src/ui/Theme.cc
  17. 24
      tests/events.cc

@ -15,7 +15,7 @@ lint:
@clang-format -i $(SRC) @clang-format -i $(SRC)
test: test:
@cmake -DBUILD_TESTS=ON -H. -GNinja -Bbuild -DCMAKE_BUILD_TYPE=Release @cmake -DBUILD_TESTS=ON -H. -GNinja -Bbuild -DCMAKE_BUILD_TYPE=RelWithDebInfo
@cmake --build build @cmake --build build
@cd build && GTEST_COLOR=1 ctest --verbose @cd build && GTEST_COLOR=1 ctest --verbose

@ -82,7 +82,7 @@ private slots:
void onResponse(QNetworkReply *reply); void onResponse(QNetworkReply *reply);
private: private:
enum Endpoint { enum class Endpoint {
GetOwnProfile, GetOwnProfile,
GetOwnAvatar, GetOwnAvatar,
GetProfile, GetProfile,

@ -37,7 +37,7 @@ class SlidingStackWidget : public QStackedWidget
public: public:
// Defines the animation direction. // Defines the animation direction.
enum AnimationDirection { enum class AnimationDirection {
LEFT_TO_RIGHT, LEFT_TO_RIGHT,
RIGHT_TO_LEFT, RIGHT_TO_LEFT,
AUTOMATIC AUTOMATIC

@ -26,7 +26,7 @@ namespace matrix
{ {
namespace events namespace events
{ {
enum EventType { enum class EventType {
/// m.room.aliases /// m.room.aliases
RoomAliases, RoomAliases,
/// m.room.avatar /// m.room.avatar

@ -26,7 +26,7 @@ namespace matrix
{ {
namespace events namespace events
{ {
enum HistoryVisibility { enum class HistoryVisibility {
Invited, Invited,
Joined, Joined,
Shared, Shared,

@ -26,7 +26,7 @@ namespace matrix
{ {
namespace events namespace events
{ {
enum JoinRule { enum class JoinRule {
// A user who wishes to join the room must first receive // A user who wishes to join the room must first receive
// an invite to the room from someone already inside of the room. // an invite to the room from someone already inside of the room.
Invite, Invite,

@ -27,7 +27,7 @@ namespace matrix
{ {
namespace events namespace events
{ {
enum Membership { enum class Membership {
// The user is banned. // The user is banned.
BanState, BanState,

@ -26,7 +26,7 @@ namespace matrix
{ {
namespace events namespace events
{ {
enum MessageEventType { enum class MessageEventType {
// m.audio // m.audio
Audio, Audio,

@ -27,7 +27,7 @@ namespace matrix
{ {
namespace events namespace events
{ {
enum PowerLevels { enum class PowerLevels {
User = 0, User = 0,
Moderator = 50, Moderator = 50,
Admin = 100, Admin = 100,
@ -55,14 +55,14 @@ public:
int userLevel(QString user_id) const; int userLevel(QString user_id) const;
private: private:
int ban_ = PowerLevels::Moderator; int ban_ = static_cast<int>(PowerLevels::Moderator);
int invite_ = PowerLevels::Moderator; int invite_ = static_cast<int>(PowerLevels::Moderator);
int kick_ = PowerLevels::Moderator; int kick_ = static_cast<int>(PowerLevels::Moderator);
int redact_ = PowerLevels::Moderator; int redact_ = static_cast<int>(PowerLevels::Moderator);
int events_default_ = PowerLevels::User; int events_default_ = static_cast<int>(PowerLevels::User);
int state_default_ = PowerLevels::Moderator; int state_default_ = static_cast<int>(PowerLevels::Moderator);
int users_default_ = PowerLevels::User; int users_default_ = static_cast<int>(PowerLevels::User);
QMap<QString, int> events_; QMap<QString, int> events_;
QMap<QString, int> users_; QMap<QString, int> users_;

@ -86,9 +86,9 @@ class FlatButton : public QPushButton
Q_PROPERTY(qreal fontSize WRITE setFontSize READ fontSize) Q_PROPERTY(qreal fontSize WRITE setFontSize READ fontSize)
public: public:
explicit FlatButton(QWidget *parent = 0, ui::ButtonPreset preset = ui::FlatPreset); explicit FlatButton(QWidget *parent = 0, ui::ButtonPreset preset = ui::ButtonPreset::FlatPreset);
explicit FlatButton(const QString &text, QWidget *parent = 0, ui::ButtonPreset preset = ui::FlatPreset); explicit FlatButton(const QString &text, QWidget *parent = 0, ui::ButtonPreset preset = ui::ButtonPreset::FlatPreset);
FlatButton(const QString &text, ui::Role role, QWidget *parent = 0, ui::ButtonPreset preset = ui::FlatPreset); FlatButton(const QString &text, ui::Role role, QWidget *parent = 0, ui::ButtonPreset preset = ui::ButtonPreset::FlatPreset);
~FlatButton(); ~FlatButton();
void applyPreset(ui::ButtonPreset preset); void applyPreset(ui::ButtonPreset preset);
@ -132,9 +132,7 @@ public:
QSize sizeHint() const override; QSize sizeHint() const override;
protected: protected:
enum { int IconPadding = 0;
IconPadding = 0
};
void checkStateSet() override; void checkStateSet() override;
void mousePressEvent(QMouseEvent *event) override; void mousePressEvent(QMouseEvent *event) override;

@ -7,7 +7,7 @@
namespace ui namespace ui
{ {
enum AvatarType { enum class AvatarType {
Icon, Icon,
Image, Image,
Letter Letter
@ -19,40 +19,40 @@ const int FontSize = 16;
// Default avatar size. Width and height. // Default avatar size. Width and height.
const int AvatarSize = 40; const int AvatarSize = 40;
enum ButtonPreset { enum class ButtonPreset {
FlatPreset, FlatPreset,
CheckablePreset CheckablePreset
}; };
enum RippleStyle { enum class RippleStyle {
CenteredRipple, CenteredRipple,
PositionedRipple, PositionedRipple,
NoRipple NoRipple
}; };
enum OverlayStyle { enum class OverlayStyle {
NoOverlay, NoOverlay,
TintedOverlay, TintedOverlay,
GrayOverlay GrayOverlay
}; };
enum Role { enum class Role {
Default, Default,
Primary, Primary,
Secondary Secondary
}; };
enum ButtonIconPlacement { enum class ButtonIconPlacement {
LeftIcon, LeftIcon,
RightIcon RightIcon
}; };
enum ProgressType { enum class ProgressType {
DeterminateProgress, DeterminateProgress,
IndeterminateProgress IndeterminateProgress
}; };
enum Color { enum class Color {
Black, Black,
BrightWhite, BrightWhite,
FadedWhite, FadedWhite,
@ -78,7 +78,7 @@ public:
QColor getColor(const QString &key) const; QColor getColor(const QString &key) const;
void setColor(const QString &key, const QColor &color); void setColor(const QString &key, const QColor &color);
void setColor(const QString &key, ui::Color &color); void setColor(const QString &key, ui::Color color);
private: private:
QColor rgba(int r, int g, int b, qreal a) const; QColor rgba(int r, int g, int b, qreal a) const;

@ -337,7 +337,7 @@ void MatrixClient::onImageResponse(QNetworkReply *reply)
void MatrixClient::onResponse(QNetworkReply *reply) void MatrixClient::onResponse(QNetworkReply *reply)
{ {
switch (reply->property("endpoint").toInt()) { switch (static_cast<Endpoint>(reply->property("endpoint").toInt())) {
case Endpoint::Versions: case Endpoint::Versions:
onVersionsResponse(reply); onVersionsResponse(reply);
break; break;
@ -387,7 +387,7 @@ void MatrixClient::login(const QString &username, const QString &password) noexc
LoginRequest body(username, password); LoginRequest body(username, password);
QNetworkReply *reply = post(request, body.serialize()); QNetworkReply *reply = post(request, body.serialize());
reply->setProperty("endpoint", Endpoint::Login); reply->setProperty("endpoint", static_cast<int>(Endpoint::Login));
} }
void MatrixClient::logout() noexcept void MatrixClient::logout() noexcept
@ -404,7 +404,7 @@ void MatrixClient::logout() noexcept
QJsonObject body{}; QJsonObject body{};
QNetworkReply *reply = post(request, QJsonDocument(body).toJson(QJsonDocument::Compact)); QNetworkReply *reply = post(request, QJsonDocument(body).toJson(QJsonDocument::Compact));
reply->setProperty("endpoint", Endpoint::Logout); reply->setProperty("endpoint", static_cast<int>(Endpoint::Logout));
} }
void MatrixClient::registerUser(const QString &user, const QString &pass, const QString &server) noexcept void MatrixClient::registerUser(const QString &user, const QString &pass, const QString &server) noexcept
@ -424,7 +424,7 @@ void MatrixClient::registerUser(const QString &user, const QString &pass, const
RegisterRequest body(user, pass); RegisterRequest body(user, pass);
QNetworkReply *reply = post(request, body.serialize()); QNetworkReply *reply = post(request, body.serialize());
reply->setProperty("endpoint", Endpoint::Register); reply->setProperty("endpoint", static_cast<int>(Endpoint::Register));
} }
void MatrixClient::sync() noexcept void MatrixClient::sync() noexcept
@ -452,7 +452,7 @@ void MatrixClient::sync() noexcept
QNetworkRequest request(QString(endpoint.toEncoded())); QNetworkRequest request(QString(endpoint.toEncoded()));
QNetworkReply *reply = get(request); QNetworkReply *reply = get(request);
reply->setProperty("endpoint", Endpoint::Sync); reply->setProperty("endpoint", static_cast<int>(Endpoint::Sync));
} }
void MatrixClient::sendTextMessage(const QString &roomid, const QString &msg) noexcept void MatrixClient::sendTextMessage(const QString &roomid, const QString &msg) noexcept
@ -473,7 +473,7 @@ void MatrixClient::sendTextMessage(const QString &roomid, const QString &msg) no
QNetworkReply *reply = put(request, QJsonDocument(body).toJson(QJsonDocument::Compact)); QNetworkReply *reply = put(request, QJsonDocument(body).toJson(QJsonDocument::Compact));
reply->setProperty("endpoint", Endpoint::SendTextMessage); reply->setProperty("endpoint", static_cast<int>(Endpoint::SendTextMessage));
reply->setProperty("txn_id", txn_id_); reply->setProperty("txn_id", txn_id_);
reply->setProperty("roomid", roomid); reply->setProperty("roomid", roomid);
@ -505,7 +505,7 @@ void MatrixClient::initialSync() noexcept
QNetworkRequest request(QString(endpoint.toEncoded())); QNetworkRequest request(QString(endpoint.toEncoded()));
QNetworkReply *reply = get(request); QNetworkReply *reply = get(request);
reply->setProperty("endpoint", Endpoint::InitialSync); reply->setProperty("endpoint", static_cast<int>(Endpoint::InitialSync));
} }
void MatrixClient::versions() noexcept void MatrixClient::versions() noexcept
@ -516,7 +516,7 @@ void MatrixClient::versions() noexcept
QNetworkRequest request(endpoint); QNetworkRequest request(endpoint);
QNetworkReply *reply = get(request); QNetworkReply *reply = get(request);
reply->setProperty("endpoint", Endpoint::Versions); reply->setProperty("endpoint", static_cast<int>(Endpoint::Versions));
} }
void MatrixClient::getOwnProfile() noexcept void MatrixClient::getOwnProfile() noexcept
@ -535,7 +535,7 @@ void MatrixClient::getOwnProfile() noexcept
QNetworkRequest request(QString(endpoint.toEncoded())); QNetworkRequest request(QString(endpoint.toEncoded()));
QNetworkReply *reply = get(request); QNetworkReply *reply = get(request);
reply->setProperty("endpoint", Endpoint::GetOwnProfile); reply->setProperty("endpoint", static_cast<int>(Endpoint::GetOwnProfile));
} }
void MatrixClient::fetchRoomAvatar(const QString &roomid, const QUrl &avatar_url) void MatrixClient::fetchRoomAvatar(const QString &roomid, const QUrl &avatar_url)
@ -554,7 +554,7 @@ void MatrixClient::fetchRoomAvatar(const QString &roomid, const QUrl &avatar_url
QNetworkReply *reply = get(avatar_request); QNetworkReply *reply = get(avatar_request);
reply->setProperty("roomid", roomid); reply->setProperty("roomid", roomid);
reply->setProperty("endpoint", Endpoint::RoomAvatar); reply->setProperty("endpoint", static_cast<int>(Endpoint::RoomAvatar));
} }
void MatrixClient::downloadImage(const QString &event_id, const QUrl &url) void MatrixClient::downloadImage(const QString &event_id, const QUrl &url)
@ -563,7 +563,7 @@ void MatrixClient::downloadImage(const QString &event_id, const QUrl &url)
QNetworkReply *reply = get(image_request); QNetworkReply *reply = get(image_request);
reply->setProperty("event_id", event_id); reply->setProperty("event_id", event_id);
reply->setProperty("endpoint", Endpoint::Image); reply->setProperty("endpoint", static_cast<int>(Endpoint::Image));
} }
void MatrixClient::fetchOwnAvatar(const QUrl &avatar_url) void MatrixClient::fetchOwnAvatar(const QUrl &avatar_url)
@ -581,5 +581,5 @@ void MatrixClient::fetchOwnAvatar(const QUrl &avatar_url)
QNetworkRequest avatar_request(media_url); QNetworkRequest avatar_request(media_url);
QNetworkReply *reply = get(avatar_request); QNetworkReply *reply = get(avatar_request);
reply->setProperty("endpoint", Endpoint::GetOwnAvatar); reply->setProperty("endpoint", static_cast<int>(Endpoint::GetOwnAvatar));
} }

@ -72,7 +72,7 @@ int TimelineView::addEvents(const QJsonArray &events)
for (const auto &event : events) { for (const auto &event : events) {
ty = events::extractEventType(event.toObject()); ty = events::extractEventType(event.toObject());
if (ty == events::RoomMessage) { if (ty == events::EventType::RoomMessage) {
events::MessageEventType msg_type = events::extractMessageEventType(event.toObject()); events::MessageEventType msg_type = events::extractMessageEventType(event.toObject());
if (msg_type == events::MessageEventType::Text) { if (msg_type == events::MessageEventType::Text) {

@ -145,7 +145,7 @@ void CircularProgress::paintEvent(QPaintEvent *event)
pen.setWidthF(width_); pen.setWidthF(width_);
pen.setColor(color()); pen.setColor(color());
if (ui::IndeterminateProgress == progress_type_) { if (ui::ProgressType::IndeterminateProgress == progress_type_) {
QVector<qreal> pattern; QVector<qreal> pattern;
pattern << delegate_->dashLength() * size_ / 50 << 30 * size_ / 50; pattern << delegate_->dashLength() * size_ / 50 << 30 * size_ / 50;

@ -15,10 +15,10 @@ void FlatButton::init()
{ {
ripple_overlay_ = new RippleOverlay(this); ripple_overlay_ = new RippleOverlay(this);
state_machine_ = new FlatButtonStateMachine(this); state_machine_ = new FlatButtonStateMachine(this);
role_ = ui::Default; role_ = ui::Role::Default;
ripple_style_ = ui::PositionedRipple; ripple_style_ = ui::RippleStyle::PositionedRipple;
icon_placement_ = ui::LeftIcon; icon_placement_ = ui::ButtonIconPlacement::LeftIcon;
overlay_style_ = ui::GrayOverlay; overlay_style_ = ui::OverlayStyle::GrayOverlay;
bg_mode_ = Qt::TransparentMode; bg_mode_ = Qt::TransparentMode;
fixed_ripple_radius_ = 64; fixed_ripple_radius_ = 64;
corner_radius_ = 3; corner_radius_ = 3;
@ -69,11 +69,11 @@ FlatButton::~FlatButton()
void FlatButton::applyPreset(ui::ButtonPreset preset) void FlatButton::applyPreset(ui::ButtonPreset preset)
{ {
switch (preset) { switch (preset) {
case ui::FlatPreset: case ui::ButtonPreset::FlatPreset:
setOverlayStyle(ui::NoOverlay); setOverlayStyle(ui::OverlayStyle::NoOverlay);
break; break;
case ui::CheckablePreset: case ui::ButtonPreset::CheckablePreset:
setOverlayStyle(ui::NoOverlay); setOverlayStyle(ui::OverlayStyle::NoOverlay);
setCheckable(true); setCheckable(true);
break; break;
default: default:
@ -105,11 +105,11 @@ QColor FlatButton::foregroundColor() const
} }
switch (role_) { switch (role_) {
case ui::Primary: case ui::Role::Primary:
return ThemeManager::instance().themeColor("Blue"); return ThemeManager::instance().themeColor("Blue");
case ui::Secondary: case ui::Role::Secondary:
return ThemeManager::instance().themeColor("Gray"); return ThemeManager::instance().themeColor("Gray");
case ui::Default: case ui::Role::Default:
default: default:
return ThemeManager::instance().themeColor("Black"); return ThemeManager::instance().themeColor("Black");
} }
@ -127,11 +127,11 @@ QColor FlatButton::backgroundColor() const
{ {
if (!background_color_.isValid()) { if (!background_color_.isValid()) {
switch (role_) { switch (role_) {
case ui::Primary: case ui::Role::Primary:
return ThemeManager::instance().themeColor("Blue"); return ThemeManager::instance().themeColor("Blue");
case ui::Secondary: case ui::Role::Secondary:
return ThemeManager::instance().themeColor("Gray"); return ThemeManager::instance().themeColor("Gray");
case ui::Default: case ui::Role::Default:
default: default:
return ThemeManager::instance().themeColor("Black"); return ThemeManager::instance().themeColor("Black");
} }
@ -143,7 +143,7 @@ QColor FlatButton::backgroundColor() const
void FlatButton::setOverlayColor(const QColor &color) void FlatButton::setOverlayColor(const QColor &color)
{ {
overlay_color_ = color; overlay_color_ = color;
setOverlayStyle(ui::TintedOverlay); setOverlayStyle(ui::OverlayStyle::TintedOverlay);
} }
QColor FlatButton::overlayColor() const QColor FlatButton::overlayColor() const
@ -314,11 +314,11 @@ void FlatButton::checkStateSet()
void FlatButton::mousePressEvent(QMouseEvent *event) void FlatButton::mousePressEvent(QMouseEvent *event)
{ {
if (ui::NoRipple != ripple_style_) { if (ui::RippleStyle::NoRipple != ripple_style_) {
QPoint pos; QPoint pos;
qreal radiusEndValue; qreal radiusEndValue;
if (ui::CenteredRipple == ripple_style_) { if (ui::RippleStyle::CenteredRipple == ripple_style_) {
pos = rect().center(); pos = rect().center();
} else { } else {
pos = event->pos(); pos = event->pos();
@ -410,8 +410,8 @@ void FlatButton::paintBackground(QPainter *painter)
return; return;
} }
if ((ui::NoOverlay != overlay_style_) && (overlayOpacity > 0)) { if ((ui::OverlayStyle::NoOverlay != overlay_style_) && (overlayOpacity > 0)) {
if (ui::TintedOverlay == overlay_style_) { if (ui::OverlayStyle::TintedOverlay == overlay_style_) {
brush.setColor(overlayColor()); brush.setColor(overlayColor());
} else { } else {
brush.setColor(Qt::gray); brush.setColor(Qt::gray);

@ -49,7 +49,7 @@ void Theme::setColor(const QString &key, const QColor &color)
colors_.insert(key, color); colors_.insert(key, color);
} }
void Theme::setColor(const QString &key, ui::Color &color) void Theme::setColor(const QString &key, ui::Color color)
{ {
static const QColor palette[] = { static const QColor palette[] = {
QColor("#171919"), QColor("#171919"),
@ -69,5 +69,5 @@ void Theme::setColor(const QString &key, ui::Color &color)
rgba(0, 0, 0, 0), rgba(0, 0, 0, 0),
}; };
colors_.insert(key, palette[color]); colors_.insert(key, palette[static_cast<int>(color)]);
} }

@ -550,18 +550,18 @@ TEST(PowerLevelsEventContent, DefaultValues)
{ {
PowerLevelsEventContent power_levels; PowerLevelsEventContent power_levels;
EXPECT_EQ(power_levels.banLevel(), PowerLevels::Moderator); EXPECT_EQ(power_levels.banLevel(), static_cast<int>(PowerLevels::Moderator));
EXPECT_EQ(power_levels.inviteLevel(), PowerLevels::Moderator); EXPECT_EQ(power_levels.inviteLevel(), static_cast<int>(PowerLevels::Moderator));
EXPECT_EQ(power_levels.kickLevel(), PowerLevels::Moderator); EXPECT_EQ(power_levels.kickLevel(), static_cast<int>(PowerLevels::Moderator));
EXPECT_EQ(power_levels.redactLevel(), PowerLevels::Moderator); EXPECT_EQ(power_levels.redactLevel(), static_cast<int>(PowerLevels::Moderator));
EXPECT_EQ(power_levels.eventsDefaultLevel(), PowerLevels::User); EXPECT_EQ(power_levels.eventsDefaultLevel(), static_cast<int>(PowerLevels::User));
EXPECT_EQ(power_levels.usersDefaultLevel(), PowerLevels::User); EXPECT_EQ(power_levels.usersDefaultLevel(), static_cast<int>(PowerLevels::User));
EXPECT_EQ(power_levels.stateDefaultLevel(), PowerLevels::Moderator); EXPECT_EQ(power_levels.stateDefaultLevel(), static_cast<int>(PowerLevels::Moderator));
// Default levels. // Default levels.
EXPECT_EQ(power_levels.userLevel("@joe:matrix.org"), PowerLevels::User); EXPECT_EQ(power_levels.userLevel("@joe:matrix.org"), static_cast<int>(PowerLevels::User));
EXPECT_EQ(power_levels.eventLevel("m.room.message"), PowerLevels::User); EXPECT_EQ(power_levels.eventLevel("m.room.message"), static_cast<int>(PowerLevels::User));
} }
TEST(PowerLevelsEventContent, FullDeserialization) TEST(PowerLevelsEventContent, FullDeserialization)
@ -618,11 +618,11 @@ TEST(PowerLevelsEventContent, PartialDeserialization)
EXPECT_EQ(power_levels.banLevel(), 1); EXPECT_EQ(power_levels.banLevel(), 1);
EXPECT_EQ(power_levels.inviteLevel(), 2); EXPECT_EQ(power_levels.inviteLevel(), 2);
EXPECT_EQ(power_levels.kickLevel(), PowerLevels::Moderator); EXPECT_EQ(power_levels.kickLevel(), static_cast<int>(PowerLevels::Moderator));
EXPECT_EQ(power_levels.redactLevel(), PowerLevels::Moderator); EXPECT_EQ(power_levels.redactLevel(), static_cast<int>(PowerLevels::Moderator));
EXPECT_EQ(power_levels.eventsDefaultLevel(), 5); EXPECT_EQ(power_levels.eventsDefaultLevel(), 5);
EXPECT_EQ(power_levels.stateDefaultLevel(), PowerLevels::Moderator); EXPECT_EQ(power_levels.stateDefaultLevel(), static_cast<int>(PowerLevels::Moderator));
EXPECT_EQ(power_levels.usersDefaultLevel(), 7); EXPECT_EQ(power_levels.usersDefaultLevel(), 7);
EXPECT_EQ(power_levels.userLevel("@alice:matrix.org"), 10); EXPECT_EQ(power_levels.userLevel("@alice:matrix.org"), 10);

Loading…
Cancel
Save