Use C++11 enum class

This commit is contained in:
Jonas Kvinge
2023-02-18 14:09:27 +01:00
parent e6c5f76872
commit dd72fb4ca5
237 changed files with 2915 additions and 2840 deletions

View File

@@ -60,15 +60,15 @@ void MoodbarController::CurrentSongChanged(const Song &song) {
const MoodbarLoader::Result result = app_->moodbar_loader()->Load(song.url(), song.has_cue(), &data, &pipeline);
switch (result) {
case MoodbarLoader::CannotLoad:
case MoodbarLoader::Result::CannotLoad:
emit CurrentMoodbarDataChanged(QByteArray());
break;
case MoodbarLoader::Loaded:
case MoodbarLoader::Result::Loaded:
emit CurrentMoodbarDataChanged(data);
break;
case MoodbarLoader::WillLoadAsync:
case MoodbarLoader::Result::WillLoadAsync:
// Emit an empty array for now so the GUI reverts to a normal progress
// bar. Our slot will be called when the data is actually loaded.
emit CurrentMoodbarDataChanged(QByteArray());
@@ -94,9 +94,9 @@ void MoodbarController::AsyncLoadComplete(MoodbarPipeline *pipeline, const QUrl
}
// Did we stop the song?
switch (app_->player()->GetState()) {
case Engine::Error:
case Engine::Empty:
case Engine::Idle:
case Engine::State::Error:
case Engine::State::Empty:
case Engine::State::Idle:
return;
default:

View File

@@ -43,14 +43,14 @@
#include "settings/moodbarsettingspage.h"
MoodbarItemDelegate::Data::Data() : state_(State_None) {}
MoodbarItemDelegate::Data::Data() : state_(State::None) {}
MoodbarItemDelegate::MoodbarItemDelegate(Application *app, PlaylistView *view, QObject *parent)
: QItemDelegate(parent),
app_(app),
view_(view),
enabled_(false),
style_(MoodbarRenderer::Style_Normal) {
style_(MoodbarRenderer::MoodbarStyle::Normal) {
QObject::connect(app_, &Application::SettingsChanged, this, &MoodbarItemDelegate::ReloadSettings);
ReloadSettings();
@@ -62,7 +62,7 @@ void MoodbarItemDelegate::ReloadSettings() {
QSettings s;
s.beginGroup(MoodbarSettingsPage::kSettingsGroup);
enabled_ = s.value("enabled", false).toBool();
MoodbarRenderer::MoodbarStyle new_style = static_cast<MoodbarRenderer::MoodbarStyle>(s.value("style", MoodbarRenderer::Style_Normal).toInt());
const MoodbarRenderer::MoodbarStyle new_style = static_cast<MoodbarRenderer::MoodbarStyle>(s.value("style", static_cast<int>(MoodbarRenderer::MoodbarStyle::Normal)).toInt());
s.endGroup();
if (!enabled_) {
@@ -113,13 +113,13 @@ QPixmap MoodbarItemDelegate::PixmapForIndex(const QModelIndex &idx, const QSize
data->desired_size_ = size;
switch (data->state_) {
case Data::State_CannotLoad:
case Data::State_LoadingData:
case Data::State_LoadingColors:
case Data::State_LoadingImage:
case Data::State::CannotLoad:
case Data::State::LoadingData:
case Data::State::LoadingColors:
case Data::State::LoadingImage:
return data->pixmap_;
case Data::State_Loaded:
case Data::State::Loaded:
// Is the pixmap the right size?
if (data->pixmap_.size() != size) {
StartLoadingImage(url, data);
@@ -127,7 +127,7 @@ QPixmap MoodbarItemDelegate::PixmapForIndex(const QModelIndex &idx, const QSize
return data->pixmap_;
case Data::State_None:
case Data::State::None:
break;
}
@@ -140,22 +140,22 @@ QPixmap MoodbarItemDelegate::PixmapForIndex(const QModelIndex &idx, const QSize
void MoodbarItemDelegate::StartLoadingData(const QUrl &url, const bool has_cue, Data *data) {
data->state_ = Data::State_LoadingData;
data->state_ = Data::State::LoadingData;
// Load a mood file for this song and generate some colors from it
QByteArray bytes;
MoodbarPipeline *pipeline = nullptr;
switch (app_->moodbar_loader()->Load(url, has_cue, &bytes, &pipeline)) {
case MoodbarLoader::CannotLoad:
data->state_ = Data::State_CannotLoad;
case MoodbarLoader::Result::CannotLoad:
data->state_ = Data::State::CannotLoad;
break;
case MoodbarLoader::Loaded:
case MoodbarLoader::Result::Loaded:
// We got the data immediately.
StartLoadingColors(url, bytes, data);
break;
case MoodbarLoader::WillLoadAsync:
case MoodbarLoader::Result::WillLoadAsync:
// Maybe in a little while.
QObject::connect(pipeline, &MoodbarPipeline::Finished, this, [this, url, pipeline]() { DataLoaded(url, pipeline); });
break;
@@ -179,7 +179,7 @@ void MoodbarItemDelegate::ReloadAllColors() {
for (const QUrl &url : data_.keys()) {
Data *data = data_[url];
if (data->state_ == Data::State_Loaded) {
if (data->state_ == Data::State::Loaded) {
StartLoadingData(url, false, data);
}
}
@@ -197,7 +197,7 @@ void MoodbarItemDelegate::DataLoaded(const QUrl &url, MoodbarPipeline *pipeline)
}
if (!pipeline->success()) {
data->state_ = Data::State_CannotLoad;
data->state_ = Data::State::CannotLoad;
return;
}
@@ -208,7 +208,7 @@ void MoodbarItemDelegate::DataLoaded(const QUrl &url, MoodbarPipeline *pipeline)
void MoodbarItemDelegate::StartLoadingColors(const QUrl &url, const QByteArray &bytes, Data *data) {
data->state_ = Data::State_LoadingColors;
data->state_ = Data::State::LoadingColors;
QFuture<ColorVector> future = QtConcurrent::run(MoodbarRenderer::Colors, bytes, style_, qApp->palette());
QFutureWatcher<ColorVector> *watcher = new QFutureWatcher<ColorVector>();
@@ -239,7 +239,7 @@ void MoodbarItemDelegate::ColorsLoaded(const QUrl &url, const ColorVector &color
void MoodbarItemDelegate::StartLoadingImage(const QUrl &url, Data *data) {
data->state_ = Data::State_LoadingImage;
data->state_ = Data::State::LoadingImage;
QFuture<QImage> future = QtConcurrent::run(MoodbarRenderer::RenderToImage, data->colors_, data->desired_size_);
QFutureWatcher<QImage> *watcher = new QFutureWatcher<QImage>();
@@ -269,7 +269,7 @@ void MoodbarItemDelegate::ImageLoaded(const QUrl &url, const QImage &image) {
}
data->pixmap_ = QPixmap::fromImage(image);
data->state_ = Data::State_Loaded;
data->state_ = Data::State::Loaded;
Playlist *playlist = view_->playlist();
const PlaylistFilter *filter = playlist->filter();

View File

@@ -58,13 +58,13 @@ class MoodbarItemDelegate : public QItemDelegate {
struct Data {
Data();
enum State {
State_None,
State_CannotLoad,
State_LoadingData,
State_LoadingColors,
State_LoadingImage,
State_Loaded
enum class State {
None,
CannotLoad,
LoadingData,
LoadingColors,
LoadingImage,
Loaded
};
QSet<QPersistentModelIndex> indexes_;

View File

@@ -94,13 +94,13 @@ QStringList MoodbarLoader::MoodFilenames(const QString &song_filename) {
MoodbarLoader::Result MoodbarLoader::Load(const QUrl &url, const bool has_cue, QByteArray *data, MoodbarPipeline **async_pipeline) {
if (!url.isLocalFile() || has_cue) {
return CannotLoad;
return Result::CannotLoad;
}
// Are we in the middle of loading this moodbar already?
if (requests_.contains(url)) {
*async_pipeline = requests_[url];
return WillLoadAsync;
return Result::WillLoadAsync;
}
// Check if a mood file exists for this file already
@@ -113,7 +113,7 @@ MoodbarLoader::Result MoodbarLoader::Load(const QUrl &url, const bool has_cue, Q
qLog(Info) << "Loading moodbar data from" << possible_mood_file;
*data = f.readAll();
f.close();
return Loaded;
return Result::Loaded;
}
else {
qLog(Error) << "Failed to load moodbar data from" << possible_mood_file << f.errorString();
@@ -127,7 +127,7 @@ MoodbarLoader::Result MoodbarLoader::Load(const QUrl &url, const bool has_cue, Q
qLog(Info) << "Loading cached moodbar data for" << filename;
*data = cache_device->readAll();
if (!data->isEmpty()) {
return Loaded;
return Result::Loaded;
}
}
@@ -144,7 +144,7 @@ MoodbarLoader::Result MoodbarLoader::Load(const QUrl &url, const bool has_cue, Q
MaybeTakeNextRequest();
*async_pipeline = pipeline;
return WillLoadAsync;
return Result::WillLoadAsync;
}

View File

@@ -39,7 +39,7 @@ class MoodbarLoader : public QObject {
explicit MoodbarLoader(Application *app, QObject *parent = nullptr);
~MoodbarLoader() override;
enum Result {
enum class Result {
// The URL isn't a local file or the moodbar plugin was not available -
// moodbar data can never be loaded.
CannotLoad,

View File

@@ -78,7 +78,7 @@ void MoodbarPipeline::Start() {
Q_ASSERT(QThread::currentThread() != qApp->thread());
Utilities::SetThreadIOPriority(Utilities::IOPRIO_CLASS_IDLE);
Utilities::SetThreadIOPriority(Utilities::IoPriority::IOPRIO_CLASS_IDLE);
if (pipeline_) {
return;

View File

@@ -52,8 +52,8 @@ MoodbarProxyStyle::MoodbarProxyStyle(Application *app, QSlider *slider, QObject*
app_(app),
slider_(slider),
enabled_(true),
moodbar_style_(MoodbarRenderer::Style_Normal),
state_(MoodbarOff),
moodbar_style_(MoodbarRenderer::MoodbarStyle::Normal),
state_(State::MoodbarOff),
fade_timeline_(new QTimeLine(1000, this)),
moodbar_colors_dirty_(true),
moodbar_pixmap_dirty_(true),
@@ -82,7 +82,7 @@ void MoodbarProxyStyle::ReloadSettings() {
NextState();
// Get the style, and redraw if there's a change.
MoodbarRenderer::MoodbarStyle new_style = static_cast<MoodbarRenderer::MoodbarStyle>(s.value("style", MoodbarRenderer::Style_Normal).toInt());
const MoodbarRenderer::MoodbarStyle new_style = static_cast<MoodbarRenderer::MoodbarStyle>(s.value("style", static_cast<int>(MoodbarRenderer::MoodbarStyle::Normal)).toInt());
s.endGroup();
@@ -129,13 +129,13 @@ void MoodbarProxyStyle::NextState() {
show_moodbar_action_->setChecked(enabled_);
}
if ((visible && (state_ == MoodbarOn || state_ == FadingToOn)) || (!visible && (state_ == MoodbarOff || state_ == FadingToOff))) {
if ((visible && (state_ == State::MoodbarOn || state_ == State::FadingToOn)) || (!visible && (state_ == State::MoodbarOff || state_ == State::FadingToOff))) {
return;
}
const QTimeLine::Direction direction = visible ? QTimeLine::Forward : QTimeLine::Backward;
if (state_ == MoodbarOn || state_ == MoodbarOff) {
if (state_ == State::MoodbarOn || state_ == State::MoodbarOff) {
// Start the fade from the beginning.
fade_timeline_->setDirection(direction);
fade_timeline_->start();
@@ -151,7 +151,7 @@ void MoodbarProxyStyle::NextState() {
fade_timeline_->resume();
}
state_ = visible ? FadingToOn : FadingToOff;
state_ = visible ? State::FadingToOn : State::FadingToOff;
}
@@ -198,16 +198,16 @@ void MoodbarProxyStyle::Render(ComplexControl control, const QStyleOptionSlider
const qreal fade_value = fade_timeline_->currentValue();
// Have we finished fading?
if (state_ == FadingToOn && fade_value == 1.0) {
state_ = MoodbarOn;
if (state_ == State::FadingToOn && fade_value == 1.0) {
state_ = State::MoodbarOn;
}
else if (state_ == FadingToOff && fade_value == 0.0) {
state_ = MoodbarOff;
else if (state_ == State::FadingToOff && fade_value == 0.0) {
state_ = State::MoodbarOff;
}
switch (state_) {
case FadingToOn:
case FadingToOff:
case State::FadingToOn:
case State::FadingToOff:
// Update the cached pixmaps if necessary
if (fade_source_.isNull()) {
// Draw the normal slider into the fade source pixmap.
@@ -224,7 +224,7 @@ void MoodbarProxyStyle::Render(ComplexControl control, const QStyleOptionSlider
}
if (fade_target_.isNull()) {
if (state_ == FadingToOn) {
if (state_ == State::FadingToOn) {
EnsureMoodbarRendered(option);
}
fade_target_ = moodbar_pixmap_;
@@ -240,12 +240,12 @@ void MoodbarProxyStyle::Render(ComplexControl control, const QStyleOptionSlider
painter->setOpacity(1.0);
break;
case MoodbarOff:
case State::MoodbarOff:
// It's a normal slider widget.
QProxyStyle::drawComplexControl(control, option, painter, widget);
break;
case MoodbarOn:
case State::MoodbarOn:
EnsureMoodbarRendered(option);
painter->drawPixmap(option->rect, moodbar_pixmap_);
DrawArrow(option, painter);
@@ -276,12 +276,12 @@ QRect MoodbarProxyStyle::subControlRect(ComplexControl cc, const QStyleOptionCom
}
switch (state_) {
case MoodbarOff:
case FadingToOff:
case State::MoodbarOff:
case State::FadingToOff:
break;
case MoodbarOn:
case FadingToOn:
case State::MoodbarOn:
case State::FadingToOn:
switch (sc) {
case SC_SliderGroove:
return opt->rect.adjusted(kMarginSize, kMarginSize, -kMarginSize, -kMarginSize);
@@ -376,7 +376,7 @@ void MoodbarProxyStyle::ShowContextMenu(const QPoint pos) {
QMenu *styles_menu = context_menu_->addMenu(tr("Moodbar style"));
style_action_group_ = new QActionGroup(styles_menu);
for (int i = 0; i < MoodbarRenderer::StyleCount; ++i) {
for (int i = 0; i < static_cast<int>(MoodbarRenderer::MoodbarStyle::StyleCount); ++i) {
const MoodbarRenderer::MoodbarStyle style = static_cast<MoodbarRenderer::MoodbarStyle>(i);
QAction *action = style_action_group_->addAction(MoodbarRenderer::StyleName(style));

View File

@@ -70,7 +70,12 @@ class MoodbarProxyStyle : public QProxyStyle {
static const int kArrowWidth;
static const int kArrowHeight;
enum State { MoodbarOn, MoodbarOff, FadingToOn, FadingToOff };
enum class State {
MoodbarOn,
MoodbarOff,
FadingToOn,
FadingToOff
};
private:
void NextState();

View File

@@ -37,19 +37,19 @@ ColorVector MoodbarRenderer::Colors(const QByteArray &data, const MoodbarStyle s
// Set some parameters based on the moodbar style
StyleProperties properties;
switch (style) {
case Style_Angry:
case MoodbarStyle::Angry:
properties = StyleProperties(samples / 360 * 9, 45, -45, 200, 100);
break;
case Style_Frozen:
case MoodbarStyle::Frozen:
properties = StyleProperties(samples / 360 * 1, 140, 160, 50, 100);
break;
case Style_Happy:
case MoodbarStyle::Happy:
properties = StyleProperties(samples / 360 * 2, 0, 359, 150, 250);
break;
case Style_Normal:
case MoodbarStyle::Normal:
properties = StyleProperties(samples / 360 * 3, 0, 359, 100, 100);
break;
case Style_SystemPalette:
case MoodbarStyle::SystemPalette:
default: {
const QColor highlight_color(palette.color(QPalette::Active, QPalette::Highlight));
@@ -163,15 +163,15 @@ QImage MoodbarRenderer::RenderToImage(const ColorVector &colors, const QSize siz
QString MoodbarRenderer::StyleName(const MoodbarStyle style) {
switch (style) {
case Style_Normal:
case MoodbarStyle::Normal:
return QObject::tr("Normal");
case Style_Angry:
case MoodbarStyle::Angry:
return QObject::tr("Angry");
case Style_Frozen:
case MoodbarStyle::Frozen:
return QObject::tr("Frozen");
case Style_Happy:
case MoodbarStyle::Happy:
return QObject::tr("Happy");
case Style_SystemPalette:
case MoodbarStyle::SystemPalette:
return QObject::tr("System colors");
default:

View File

@@ -36,12 +36,12 @@ using ColorVector = QVector<QColor>;
class MoodbarRenderer {
public:
// These values are persisted. Remember to change moodbarsettingspage.ui when changing them.
enum MoodbarStyle {
Style_Normal = 0,
Style_Angry,
Style_Frozen,
Style_Happy,
Style_SystemPalette,
enum class MoodbarStyle {
Normal = 0,
Angry,
Frozen,
Happy,
SystemPalette,
StyleCount
};