The Red Penguin

Add a vector to store a list of files


Thursday 6 August 2020

We're going to build on the header we just added to our table by adding some actual data.

1. We're going to need somewhere to store that data. As a simple example we're going to have a vector containing strings.

Note! A vector of strings can only store a single string for each item in the playlist, but an item in the playlist has more than one piece of information associated with it. It has at least a song title and a filename. Perhaps a new class is needed? However this page deals with a vector.

In PlaylistComponent.h add at the top:
#include 
#include

2. In private: we declare the vector so we need to add:
std::vector trackTitles;

3. Go to PlaylistComponent.cpp and at the top of PlaylistComponent::PlaylistComponent() add:
    trackTitles.push_back("Track 1");
trackTitles.push_back("Track 2");

4. The data is now in the vector but how do we get the data onto the table? This is where it gets a little bit tricky. We need to inherit from TableListBoxModel. In PlaylistComponent.h add the inheritance as follows:
class PlaylistComponent  : public juce::Component, 
public juce::TableListBoxModel

5. Still in the header file, in public: add
    int getNumRows() override;

void paintRowBackground(juce::Graphics&,
int rowNumber,
int width,
int height,
bool rowIsSelected)
override;

void paintCell(juce::Graphics&,
int rowNumber,
int columnId,
int width,
int height,
bool rowIsSelected)
override;

6. We need to add the info we added in [5] above and put them in our .cpp file, so that we can implement them as functions. So go to PlaylistComponent.cpp and add:
int PlaylistComponent::getNumRows()
{
return trackTitles.size();
}

void PlaylistComponent::paintRowBackground(juce::Graphics&,
int rowNumber,
int width,
int height,
bool rowIsSelected)
{

}

void PlaylistComponent::paintCell(juce::Graphics&,
int rowNumber,
int columnId,
int width,
int height,
bool rowIsSelected)
{

}
These last two do not need to return anything so this is the minimum implementation of these functions needed so far.

7. We now need to add the data model to our component. Go back up in PC.cpp to the main constructor and under the tableComponent.getHeader we added add:
tableComponent.setModel(this);

We can now build and compile. Nothing has changed yet with the GUI but the TableListBoxModel has been implemented to the minimum amount.