From cdd20da5c070014f044a5b1878e6712e1c2a1803 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Wed, 26 Nov 2014 08:55:06 -0500 Subject: [PATCH] Track: Convert length_str into a function I don't think this value needs to be stored anywhere in the Track class since it's fairly easy to calculate. Let's convert it into a function for now and reevaluate later! Signed-off-by: Anna Schumaker --- core/tags.cpp | 16 ---------------- core/tags/track.cpp | 15 +++++++++++++++ gui/gui.cpp | 2 +- include/core/tags/track.h | 8 ++++---- lib/model.cpp | 2 +- tests/core/tags/track.cpp | 20 +++++++++++++++++++- 6 files changed, 40 insertions(+), 23 deletions(-) diff --git a/core/tags.cpp b/core/tags.cpp index 036fc7b1..2e25cf4e 100644 --- a/core/tags.cpp +++ b/core/tags.cpp @@ -8,7 +8,6 @@ #include #include -#include #include #include @@ -54,7 +53,6 @@ void Track :: read(File &f) filter :: add(_artist->name(), index()); filter :: add(_album->name(), index()); _library->inc_size(); - set_length_str(); } void Track :: write(File &f) @@ -67,19 +65,6 @@ void Track :: write(File &f) f << std::endl << filepath << std::endl; } -void Track :: set_length_str() -{ - std::stringstream ss; - unsigned int minutes = _length / 60; - unsigned int seconds = _length % 60; - - ss << minutes << ":"; - if (seconds < 10) - ss << "0"; - ss << seconds; - length_str = ss.str(); -} - static inline const std::string format_tag(const TagLib::String &str) { return str.stripWhiteSpace().to8Bit(true); @@ -117,7 +102,6 @@ bool Track :: tag() //_title = format_tag(tag->title()); //title_lower = filter :: add(title, index()); - set_length_str(); filter :: add(_artist->name(), index()); filter :: add(_album->name(), index()); diff --git a/core/tags/track.cpp b/core/tags/track.cpp index 23013caf..08ad3d0f 100644 --- a/core/tags/track.cpp +++ b/core/tags/track.cpp @@ -4,6 +4,8 @@ */ #include +#include + Track :: Track() : GenericTag(), @@ -34,3 +36,16 @@ Library *Track :: library() { return _library; } unsigned int Track :: count() { return _count; } unsigned int Track :: length() { return _length; } unsigned int Track :: track() { return _track; } + +const std::string Track :: length_str() const +{ + std::stringstream ss; + unsigned int minutes = _length / 60; + unsigned int seconds = _length % 60; + + ss << minutes << ":"; + if (seconds < 10) + ss << 0; + ss << seconds; + return ss.str(); +} diff --git a/gui/gui.cpp b/gui/gui.cpp index 06745bc8..2f6177cd 100644 --- a/gui/gui.cpp +++ b/gui/gui.cpp @@ -49,7 +49,7 @@ static void on_track_loaded(Track *track) set_label_text(title, "xx-large", track->name()); set_label_text(artist, "x-large", "By: " + track->artist()->name()); set_label_text(album, "x-large", "From: " + track->album()->name()); - duration->set_text(track->length_str); + duration->set_text(track->length_str()); bool banned = playlist :: has(track, "Banned"); bool favorite = playlist :: has(track, "Favorites"); diff --git a/include/core/tags/track.h b/include/core/tags/track.h index 8a2cc81c..e95221a6 100644 --- a/include/core/tags/track.h +++ b/include/core/tags/track.h @@ -48,8 +48,6 @@ private: unsigned int _length; /**< Length of this track (in seconds). */ unsigned int _track; /**< Track number of this track. */ - void set_length_str(); - public: /** The year this track was last played */ unsigned int last_year; @@ -60,8 +58,6 @@ public: /** The filepath of this track, relative to the library root */ std :: string filepath; - /** The length of this track in a human readable string */ - std :: string length_str; /** Track constructor */ Track(); @@ -100,6 +96,10 @@ public: unsigned int length(); /**< @return Track::_length. */ unsigned int track(); /**< @return Track::_track. */ + /** @return Track::_length in mm:ss format. */ + const std::string length_str() const; + + /** * Called to access a track's primary key * @return The full path of the track diff --git a/lib/model.cpp b/lib/model.cpp index e642be0f..2c484b59 100644 --- a/lib/model.cpp +++ b/lib/model.cpp @@ -143,7 +143,7 @@ void QueueModel::get_value_str(Track *track, int column, specific.set(track->name()); break; case 2: - specific.set(track->length_str); + specific.set(track->length_str()); break; case 3: specific.set(track->artist()->name()); diff --git a/tests/core/tags/track.cpp b/tests/core/tags/track.cpp index 3300d140..b787f043 100644 --- a/tests/core/tags/track.cpp +++ b/tests/core/tags/track.cpp @@ -18,7 +18,7 @@ static void test_track_tag_default() test_equal(track.lowercase(), (std::string)""); test_equal(track.primary_key(), (std::string)""); test_equal(track.filepath, (std::string)""); - test_equal(track.length_str, (std::string)""); + test_equal(track.length_str(), (std::string)"0:00"); test_equal(track.track(), (unsigned)0); test_equal(track.length(), (unsigned)0); @@ -44,6 +44,7 @@ static void test_track_tag_constructor() test_equal(track.name(), (std::string)"Legend of Zelda Medley"); test_equal(track.lowercase(), (std::string)"legend of zelda medley"); + test_equal(track.length_str(), (std::string)"4:48"); test_equal(track.track(), (unsigned)13); test_equal(track.length(), (unsigned)288); @@ -56,10 +57,27 @@ static void test_track_tag_destructor() test_equal(library->size(), (unsigned)0); } +static void test_track_tag_functional() +{ + Album *album = tags :: get_album("Hyrule Symphony", 1998); + Artist *artist = tags :: get_artist("Koji Kondo"); + Genre *genre = tags :: get_genre("Video Game Music"); + Library *library = tags :: get_library("/home/Zelda/Music"); + Track track; + + track = Track(album, artist, genre, library, "Kakariko Village", 186, 6); + test_equal(track.length_str(), (std::string)"3:06"); + + /* Not an actual track on the album, I just needed something < 1 min. */ + track = Track(album, artist, genre, library, "Intro", 56, 0); + test_equal(track.length_str(), (std::string)"0:56"); +} + int main(int argc, char **argv) { run_test("Track Tag Default Constructor Test", test_track_tag_default); run_test("Track Tag Constructor Test", test_track_tag_constructor); run_test("Track Tag Destructor Test", test_track_tag_destructor); + run_test("Track Tag Functional Test", test_track_tag_functional); return 0; }