Skip to content

Commit

Permalink
abstract note
Browse files Browse the repository at this point in the history
  • Loading branch information
bramtayl committed Nov 9, 2024
1 parent 41ca837 commit b6f69f0
Show file tree
Hide file tree
Showing 42 changed files with 596 additions and 549 deletions.
20 changes: 7 additions & 13 deletions src/abstract_rational/AbstractRational.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <nlohmann/json.hpp>
#include <utility>

#include "other/other.hpp"

AbstractRational::AbstractRational(int numerator_input, int denominator_input)
: numerator(numerator_input), denominator(denominator_input){};

Expand All @@ -32,17 +30,6 @@ void AbstractRational::to_json(nlohmann::json &json_rational) const {
add_int_to_json(json_rational, "denominator", denominator, 1);
}

auto get_numerator_schema() -> nlohmann::json {
return get_number_schema("integer", "numerator", 1,
MAX_RATIONAL_NUMERATOR);
}

auto get_denominator_schema() -> nlohmann::json {
return get_number_schema("integer", "denominator", 1,
MAX_RATIONAL_DENOMINATOR);

}

void add_abstract_rational_to_json(nlohmann::json &json_row,
const AbstractRational &rational,
const char *column_name) {
Expand All @@ -52,3 +39,10 @@ void add_abstract_rational_to_json(nlohmann::json &json_row,
json_row[column_name] = std::move(json_rational);
}
}

void add_int_to_json(nlohmann::json &json_object, const char *field_name,
int value, int default_value) {
if (value != default_value) {
json_object[field_name] = value;
}
}
7 changes: 3 additions & 4 deletions src/abstract_rational/AbstractRational.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ struct AbstractRational {
virtual void to_json(nlohmann::json &json_rational) const;
};

[[nodiscard]] auto get_numerator_schema() -> nlohmann::json;

[[nodiscard]] auto get_denominator_schema() -> nlohmann::json;

void add_abstract_rational_to_json(nlohmann::json &json_row,
const AbstractRational &rational,
const char *column_name);
Expand All @@ -39,3 +35,6 @@ auto json_field_to_abstract_rational(const nlohmann::json &json_row,
}
return {};
}

void add_int_to_json(nlohmann::json &json_object, const char *field_name,
int value, int default_value);
11 changes: 0 additions & 11 deletions src/abstract_rational/interval/Interval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#include <cmath>

#include "other/other.hpp"

static const auto OCTAVE_RATIO = 2.0;

Interval::Interval(const nlohmann::json &json_rational)
Expand All @@ -26,15 +24,6 @@ auto Interval::to_double() const -> double {
return AbstractRational::to_double() * pow(OCTAVE_RATIO, octave);
}

auto get_interval_schema() -> nlohmann::json {
return get_object_schema(
"an interval",
nlohmann::json({{"numerator", get_numerator_schema()},
{"denominator", get_denominator_schema()},
{"octave", get_number_schema("integer", "octave",
-MAX_OCTAVE, MAX_OCTAVE)}}));
}

void Interval::to_json(nlohmann::json &json_interval) const {
AbstractRational::to_json(json_interval);
add_int_to_json(json_interval, "octave", octave, 0);
Expand Down
1 change: 0 additions & 1 deletion src/abstract_rational/interval/Interval.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ struct Interval : public AbstractRational {

Q_DECLARE_METATYPE(Interval);

[[nodiscard]] auto get_interval_schema() -> nlohmann::json;
9 changes: 0 additions & 9 deletions src/abstract_rational/rational/Rational.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

#include <nlohmann/json.hpp>

#include "other/other.hpp"

Rational::Rational(const nlohmann::json &json_rational)
: AbstractRational(json_rational) {}

Expand All @@ -15,10 +13,3 @@ auto Rational::operator==(const Rational &other_rational) const -> bool {
denominator == other_rational.denominator;
}

auto get_rational_schema(const char *description) -> nlohmann::json {
return get_object_schema(
description,
nlohmann::json(
{{"numerator", get_numerator_schema()},
{"denominator", get_denominator_schema()}}));
}
2 changes: 0 additions & 2 deletions src/abstract_rational/rational/Rational.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@ struct Rational : public AbstractRational {

Q_DECLARE_METATYPE(Rational);

[[nodiscard]] auto
get_rational_schema(const char *description) -> nlohmann::json;
14 changes: 0 additions & 14 deletions src/named/Named.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,3 @@
Named::Named(const char* name_input) : name(name_input) {

}

auto get_name_or_empty(const Named *named_pointer) -> QString {
if (named_pointer == nullptr) {
return "";
}
return named_pointer->name;
}

void add_named_to_json(nlohmann::json &json_row, const Named *named_pointer,
const char *column_name) {
if (named_pointer != nullptr) {
json_row[column_name] = named_pointer->name.toStdString();
}
}
55 changes: 1 addition & 54 deletions src/named/Named.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
#pragma once

#include <QMessageBox>
#include <QObject>
#include <QString>
#include <QtGlobal>
#include <concepts>
#include <iterator>
#include <nlohmann/json.hpp>
#include <string>
#include <utility>
#include <vector>

class QWidget;

// a subnamed should have the following method:
// static auto SubNamed::get_all_nameds() -> const QList<SubNamed>&;
Expand All @@ -20,10 +11,8 @@ struct Named {
explicit Named(const char *name_input);
};

[[nodiscard]] auto get_name_or_empty(const Named *named_pointer) -> QString;

template <std::derived_from<Named> SubNamed>
[[nodiscard]] static auto get_by_name(const QString &name) -> const SubNamed & {
[[nodiscard]] static auto get_by_name(const QString& name) -> const SubNamed & {
const auto &all_nameds = SubNamed::get_all_nameds();
const auto named_pointer =
std::find_if(all_nameds.cbegin(), all_nameds.cend(),
Expand All @@ -32,45 +21,3 @@ template <std::derived_from<Named> SubNamed>
return *named_pointer;
}

template <std::derived_from<Named> SubNamed>
auto substitute_named_for(QWidget &parent, const SubNamed *sub_named_pointer,
const SubNamed *current_sub_named_pointer,
const char *default_one, const char *error_type,
const QString &message) -> const SubNamed & {
if (sub_named_pointer == nullptr) {
sub_named_pointer = current_sub_named_pointer;
};
if (sub_named_pointer == nullptr) {
QMessageBox::warning(&parent, QObject::tr(error_type), message);
sub_named_pointer = &get_by_name<SubNamed>(default_one);
}
return *sub_named_pointer;
}

template <std::derived_from<Named> SubNamed>
[[nodiscard]] auto
json_field_to_named_pointer(const nlohmann::json &json_row,
const char *field_name) -> const SubNamed * {
if (json_row.contains(field_name)) {
const auto &json_named = json_row[field_name];
Q_ASSERT(json_named.is_string());
return &get_by_name<SubNamed>(
QString::fromStdString(json_named.get<std::string>()));
};
return nullptr;
}

void add_named_to_json(nlohmann::json &json_row, const Named *named_pointer,
const char *column_name);

template <std::derived_from<Named> SubNamed>
auto get_named_schema(const char *description) -> nlohmann::json {
std::vector<std::string> names;
const auto &all_nameds = SubNamed::get_all_nameds();
std::transform(all_nameds.cbegin(), all_nameds.cend(),
std::back_inserter(names),
[](const SubNamed &item) { return item.name.toStdString(); });
return nlohmann::json({{"type", "string"},
{"description", description},
{"enum", std::move(names)}});
};
13 changes: 13 additions & 0 deletions src/named/percussion_instrument/PercussionInstrument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,16 @@ auto PercussionInstrument::get_all_nameds()
});
return all_percussions;
}

auto PercussionInstrument::get_field_name() -> const char* {
return "percussion_instrument";
};
auto PercussionInstrument::get_type_name() -> const char* {
return "percussion instrument";
};
auto PercussionInstrument::get_missing_error() -> const char* {
return "Percussion instrument error";
};
auto PercussionInstrument::get_default() -> const char* {
return "Tambourine";
};
5 changes: 5 additions & 0 deletions src/named/percussion_instrument/PercussionInstrument.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ struct PercussionInstrument : public Named {
PercussionInstrument(const char* name, short midi_number);
[[nodiscard]] static auto
get_all_nameds() -> const QList<PercussionInstrument> &;

[[nodiscard]] static auto get_field_name() -> const char*;
[[nodiscard]] static auto get_type_name() -> const char*;
[[nodiscard]] static auto get_missing_error() -> const char*;
[[nodiscard]] static auto get_default() -> const char*;
};

Q_DECLARE_METATYPE(const PercussionInstrument *);
13 changes: 13 additions & 0 deletions src/named/program/instrument/Instrument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,16 @@ auto Instrument::get_all_nameds() -> const QList<Instrument> & {
static const auto all_instruments = get_programs<Instrument>(false);
return all_instruments;
}

auto Instrument::get_field_name() -> const char* {
return "instrument";
};
auto Instrument::get_type_name() -> const char* {
return "instrument";
};
auto Instrument::get_missing_error() -> const char* {
return "Instrument error";
};
auto Instrument::get_default() -> const char* {
return "Marimba";
};
5 changes: 5 additions & 0 deletions src/named/program/instrument/Instrument.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ template <typename T> class QList;
struct Instrument : public Program {
Instrument(const char* name, short bank_number, short preset_number);
[[nodiscard]] static auto get_all_nameds() -> const QList<Instrument> &;

[[nodiscard]] static auto get_field_name() -> const char*;
[[nodiscard]] static auto get_type_name() -> const char*;
[[nodiscard]] static auto get_missing_error() -> const char*;
[[nodiscard]] static auto get_default() -> const char*;
};

Q_DECLARE_METATYPE(const Instrument *);
13 changes: 13 additions & 0 deletions src/named/program/percussion_set/PercussionSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,16 @@ auto PercussionSet::get_all_nameds() -> const QList<PercussionSet> & {
static const auto all_percussion_sets = get_programs<PercussionSet>(true);
return all_percussion_sets;
}

auto PercussionSet::get_field_name() -> const char* {
return "percussion_set";
};
auto PercussionSet::get_type_name() -> const char* {
return "percussion set";
};
auto PercussionSet::get_missing_error() -> const char* {
return "Percussion set error";
};
auto PercussionSet::get_default() -> const char* {
return "Standard";
};
5 changes: 5 additions & 0 deletions src/named/program/percussion_set/PercussionSet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ template <typename T> class QList;
struct PercussionSet : public Program {
PercussionSet(const char* name, short bank_number, short preset_number);
[[nodiscard]] static auto get_all_nameds() -> const QList<PercussionSet> &;

[[nodiscard]] static auto get_field_name() -> const char*;
[[nodiscard]] static auto get_type_name() -> const char*;
[[nodiscard]] static auto get_missing_error() -> const char*;
[[nodiscard]] static auto get_default() -> const char*;
};

Q_DECLARE_METATYPE(const PercussionSet *);
9 changes: 8 additions & 1 deletion src/other/justly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,18 @@
#include "other/other.hpp"
#include "row/RowsModel.hpp"
#include "row/chord/ChordsModel.hpp"
#include "row/pitched_note/PitchedNotesModel.hpp"
#include "row/note/pitched_note/PitchedNotesModel.hpp"
#include "song/Player.hpp"
#include "song/Song.hpp"
#include "song/SongEditor.hpp"

static auto get_name_or_empty(const Named *named_pointer) -> QString {
if (named_pointer == nullptr) {
return "";
}
return named_pointer->name;
}

void set_up() {
QApplication::setApplicationDisplayName("Justly");

Expand Down
49 changes: 0 additions & 49 deletions src/other/other.cpp
Original file line number Diff line number Diff line change
@@ -1,55 +1,6 @@
#include "other/other.hpp"

#include <QObject>
#include <QTextStream>
#include <QWidget>
#include <nlohmann/json.hpp>

auto get_words_schema() -> nlohmann::json {
return nlohmann::json({{"type", "string"}, {"description", "the words"}});
}

auto get_number_schema(const char *type, const char *description, int minimum,
int maximum) -> nlohmann::json {
return nlohmann::json({{"type", type},
{"description", description},
{"minimum", minimum},
{"maximum", maximum}});
}

auto get_array_schema(const char *description,
const nlohmann::json &item_json) -> nlohmann::json {
return nlohmann::json(
{{"type", "array"}, {"description", description}, {"items", item_json}});
};

auto get_object_schema(const char *description,
const nlohmann::json &properties_json)
-> nlohmann::json {
return nlohmann::json({{"type", "object"},
{"description", description},
{"properties", properties_json}});
};

void add_int_to_json(nlohmann::json &json_object, const char *field_name,
int value, int default_value) {
if (value != default_value) {
json_object[field_name] = value;
}
}

void add_words_to_json(nlohmann::json &json_row, const QString &words) {
if (!words.isEmpty()) {
json_row["words"] = words.toStdString().c_str();
}
}


void add_note_location(QTextStream &stream, int chord_number, int note_number,
const char *note_type) {
stream << QObject::tr(" for chord ") << chord_number + 1 << QObject::tr(", ")
<< QObject::tr(note_type) << QObject::tr(" note ") << note_number + 1;
}

void prevent_compression(QWidget &widget) {
widget.setMinimumSize(widget.minimumSizeHint());
Expand Down
Loading

0 comments on commit b6f69f0

Please sign in to comment.