Skip to content

Commit

Permalink
Switch all enums in the ColorMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianFeldmann committed Jul 21, 2024
1 parent 88e850d commit 2b6e62b
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 36 deletions.
2 changes: 1 addition & 1 deletion YUViewLib/src/common/EnumMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ template <typename T> class EnumMapper
using EntryVector = std::vector<Entry>;

EnumMapper() = default;
EnumMapper(const EntryVector &entryVector) : entryVector(entryVector){};
EnumMapper(const EntryVector &entryVector) : entryVector(entryVector) {};

std::optional<T> getValue(std::string name, StringType stringType = StringType::Name) const
{
Expand Down
12 changes: 8 additions & 4 deletions YUViewLib/src/common/Functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

#include <algorithm>
#include <charconv>
#include <string_view>

#include <QThread>

Expand Down Expand Up @@ -162,11 +163,14 @@ QStringList toQStringList(const std::vector<std::string> &stringVec)
return list;
}

std::string toLower(std::string str)
std::string toLower(const std::string_view str)
{
std::transform(
str.begin(), str.end(), str.begin(), [](unsigned char c) { return std::tolower(c); });
return str;
std::string lowercaseStr(str);
std::transform(lowercaseStr.begin(),
lowercaseStr.end(),
lowercaseStr.begin(),
[](unsigned char c) { return std::tolower(c); });
return lowercaseStr;
}

ByteVector readData(std::istream &istream, const size_t nrBytes)
Expand Down
2 changes: 1 addition & 1 deletion YUViewLib/src/common/Functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ QStringList getThemeColors(QString themeName);
QString formatDataSize(double size, bool isBits = false);

QStringList toQStringList(const std::vector<std::string> &stringVec);
std::string toLower(std::string str);
std::string toLower(const std::string_view str);
ByteVector readData(std::istream &istream, const size_t nrBytes);

template <typename T> unsigned clipToUnsigned(T val)
Expand Down
25 changes: 24 additions & 1 deletion YUViewLib/src/common/NewEnumMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ template <class T, size_t N> struct NewEnumMapper
return this->items.at(index);
}

constexpr std::optional<T> getValueCaseInsensitive(const std::string_view name) const
{
const auto compareToNameLowercase = [&name](const std::string_view str)
{
if (name.length() != str.length())
return false;
for (std::size_t i = 0; i < name.length(); ++i)
{
if (std::tolower(name.at(i)) != std::tolower(str.at(i)))
return false;
}
return true;
};

const auto it = std::find_if(this->names.begin(), this->names.end(), compareToNameLowercase);
if (it == this->names.end())
return {};

const auto index = std::distance(this->names.begin(), it);
return this->items.at(index);
}

std::optional<T> getValueFromNameOrIndex(const std::string_view nameOrIndex) const
{
if (auto index = functions::toUnsigned(nameOrIndex))
Expand Down Expand Up @@ -99,7 +121,8 @@ template <class T, size_t N> struct NewEnumMapper
return this->items.at(index);
}

constexpr const std::array<T, N> &getItems() const { return this->items; }
constexpr const std::array<T, N> &getItems() const { return this->items; }
constexpr const std::array<std::string_view, N> &getNames() const { return this->names; }

private:
constexpr void addElementsRecursively(const std::size_t) {};
Expand Down
10 changes: 6 additions & 4 deletions YUViewLib/src/common/YUViewDomElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,16 @@ void YUViewDomElement::appendProperiteChild(const QString &type,
appendChild(newChild);
}

void YUViewDomElement::setAttribute(const std::string &name, const std::string &value)
void YUViewDomElement::setAttribute(const std::string_view name, const std::string_view value)
{
QDomElement::setAttribute(QString::fromStdString(name), QString::fromStdString(value));
QDomElement::setAttribute(QString::fromStdString(std::string(name)),
QString::fromStdString(std::string(value)));
}

void YUViewDomElement::appendProperiteChild(const std::string &type, const std::string_view name)
void YUViewDomElement::appendProperiteChild(const std::string_view type,
const std::string_view name)
{
auto newChild = ownerDocument().createElement(QString::fromStdString(type));
auto newChild = ownerDocument().createElement(QString::fromStdString(std::string(type)));
newChild.appendChild(ownerDocument().createTextNode(QString::fromStdString(std::string(name))));
appendChild(newChild);
}
4 changes: 2 additions & 2 deletions YUViewLib/src/common/YUViewDomElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ class YUViewDomElement : public QDomElement
double findChildValueDouble(const QString &tagName, double defaultValue) const;

using QDomElement::setAttribute;
void setAttribute(const std::string &name, const std::string &value);
void setAttribute(const std::string_view name, const std::string_view value);

void appendProperiteChild(const QString &type,
const QString &name,
const QStringPairList &attributes = QStringPairList());
void appendProperiteChild(const std::string &type, const std::string_view name);
void appendProperiteChild(const std::string_view type, const std::string_view name);
};
43 changes: 27 additions & 16 deletions YUViewLib/src/statistics/ColorMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#pragma once

#include <common/Color.h>
#include <common/EnumMapper.h>
#include <common/NewEnumMapper.h>
#include <common/Typedef.h>
#include <common/YUViewDomElement.h>

Expand Down Expand Up @@ -78,18 +78,28 @@ enum class PredefinedType
Col3_bwg
};

const auto PredefinedTypeMapper = EnumMapper<PredefinedType>(
{{PredefinedType::Jet, "Jet"}, {PredefinedType::Heat, "Heat"},
{PredefinedType::Hsv, "Hsv"}, {PredefinedType::Shuffle, "Shuffle"},
{PredefinedType::Hot, "Hot"}, {PredefinedType::Cool, "Cool"},
{PredefinedType::Spring, "Spring"}, {PredefinedType::Summer, "Summer"},
{PredefinedType::Autumn, "Autumn"}, {PredefinedType::Winter, "Winter"},
{PredefinedType::Gray, "Gray"}, {PredefinedType::Bone, "Bone"},
{PredefinedType::Copper, "Copper"}, {PredefinedType::Pink, "Pink"},
{PredefinedType::Lines, "Lines"}, {PredefinedType::Col3_gblr, "Col3_gblr"},
{PredefinedType::Col3_gwr, "Col3_gwr"}, {PredefinedType::Col3_bblr, "Col3_bblr"},
{PredefinedType::Col3_bwr, "Col3_bwr"}, {PredefinedType::Col3_bblg, "Col3_bblg"},
{PredefinedType::Col3_bwg, "Col3_bwg"}});
constexpr NewEnumMapper<PredefinedType, 21>
PredefinedTypeMapper(std::make_pair(PredefinedType::Jet, "Jet"sv),
std::make_pair(PredefinedType::Heat, "Heat"sv),
std::make_pair(PredefinedType::Hsv, "Hsv"sv),
std::make_pair(PredefinedType::Shuffle, "Shuffle"sv),
std::make_pair(PredefinedType::Hot, "Hot"sv),
std::make_pair(PredefinedType::Cool, "Cool"sv),
std::make_pair(PredefinedType::Spring, "Spring"sv),
std::make_pair(PredefinedType::Summer, "Summer"sv),
std::make_pair(PredefinedType::Autumn, "Autumn"sv),
std::make_pair(PredefinedType::Winter, "Winter"sv),
std::make_pair(PredefinedType::Gray, "Gray"sv),
std::make_pair(PredefinedType::Bone, "Bone"sv),
std::make_pair(PredefinedType::Copper, "Copper"sv),
std::make_pair(PredefinedType::Pink, "Pink"sv),
std::make_pair(PredefinedType::Lines, "Lines"sv),
std::make_pair(PredefinedType::Col3_gblr, "Col3_gblr"sv),
std::make_pair(PredefinedType::Col3_gwr, "Col3_gwr"sv),
std::make_pair(PredefinedType::Col3_bblr, "Col3_bblr"sv),
std::make_pair(PredefinedType::Col3_bwr, "Col3_bwr"sv),
std::make_pair(PredefinedType::Col3_bblg, "Col3_bblg"sv),
std::make_pair(PredefinedType::Col3_bwg, "Col3_bwg"sv));

enum class MappingType
{
Expand All @@ -98,9 +108,10 @@ enum class MappingType
Predefined
};

const auto MappingTypeMapper = EnumMapper<MappingType>({{MappingType::Gradient, "Gradient"},
{MappingType::Map, "Map"},
{MappingType::Predefined, "Predefined"}});
constexpr NewEnumMapper<MappingType, 3>
MappingTypeMapper(std::make_pair(MappingType::Gradient, "Gradient"sv),
std::make_pair(MappingType::Map, "Map"sv),
std::make_pair(MappingType::Predefined, "Predefined"sv));

class ColorMapper
{
Expand Down
1 change: 1 addition & 0 deletions YUViewLib/src/ui/PlaybackController.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include <chrono>

#include <common/EnumMapper.h>
#include <common/Typedef.h>
#include <ui/views/SplitViewWidget.h>
#include <ui/widgets/PlaylistTreeWidget.h>
Expand Down
17 changes: 10 additions & 7 deletions YUViewLib/src/ui/Statisticsstylecontrol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ StatisticsStyleControl::StatisticsStyleControl(QWidget *parent)

QSignalBlocker blockerPredefined(this->ui.comboBoxPredefined);
for (auto typeName : stats::color::PredefinedTypeMapper.getNames())
this->ui.comboBoxPredefined->addItem(QString::fromStdString(typeName));
this->ui.comboBoxPredefined->addItem(QString::fromStdString(std::string(typeName)));
this->refreshComboBoxCustomMapFromStorage();
}

Expand Down Expand Up @@ -320,12 +320,15 @@ void StatisticsStyleControl::on_pushButtonEditMap_clicked()

StatisticsStyleControl_ColorMapEditor colorMapEditor(originalColorMap, originalOtherColor, this);

connect(&colorMapEditor, &StatisticsStyleControl_ColorMapEditor::mapChanged, [&]() {
this->currentItem->colorMapper.colorMap = colorMapEditor.getColorMap();
this->currentItem->colorMapper.colorMapOther = colorMapEditor.getOtherColor();
this->ui.frameDataColor->setColorMapper(this->currentItem->colorMapper);
emit StyleChanged();
});
connect(&colorMapEditor,
&StatisticsStyleControl_ColorMapEditor::mapChanged,
[&]()
{
this->currentItem->colorMapper.colorMap = colorMapEditor.getColorMap();
this->currentItem->colorMapper.colorMapOther = colorMapEditor.getOtherColor();
this->ui.frameDataColor->setColorMapper(this->currentItem->colorMapper);
emit StyleChanged();
});

if (colorMapEditor.exec() == QDialog::Accepted)
{
Expand Down

0 comments on commit 2b6e62b

Please sign in to comment.