Use std::vector in PicturePreviewSelection

The code for handling covers isn't shared between the CLI and Qt GUI yet
but this might make it easier.
This commit is contained in:
Martchus 2021-04-28 00:47:55 +02:00
parent d31fd79e87
commit 8258a14089
2 changed files with 42 additions and 43 deletions

View File

@ -129,16 +129,16 @@ template <class TagType> bool fieldPredicate(int i, const std::pair<typename Tag
* \param previousValueHandling Specifies the "previous value handling policy". * \param previousValueHandling Specifies the "previous value handling policy".
*/ */
template <class TagType> template <class TagType>
int fetchId3v2CoverValues( int fetchId3v2CoverValues(const TagType *tag, KnownField field, std::vector<TagParser::TagValue> &values, const std::size_t valueCount,
const TagType *tag, KnownField field, QList<TagParser::TagValue> &values, const int valueCount, const PreviousValueHandling previousValueHandling) const PreviousValueHandling previousValueHandling)
{ {
values.reserve(valueCount); values.reserve(valueCount);
int first = -1; int first = -1;
const auto &fields = tag->fields(); const auto &fields = tag->fields();
auto range = fields.equal_range(tag->fieldId(field)); auto range = fields.equal_range(tag->fieldId(field));
for (int i = 0; i < valueCount; ++i) { for (std::size_t i = 0; i < valueCount; ++i) {
if (i >= values.size()) { if (i >= values.size()) {
values << TagValue(); values.emplace_back();
} }
auto pair = find_if(range.first, range.second, std::bind(fieldPredicate<TagType>, i, placeholders::_1)); auto pair = find_if(range.first, range.second, std::bind(fieldPredicate<TagType>, i, placeholders::_1));
if (pair != range.second) { if (pair != range.second) {
@ -151,10 +151,10 @@ int fetchId3v2CoverValues(
values[i].clearDataAndMetadata(); values[i].clearDataAndMetadata();
} }
if (first < 0 && !values[i].isEmpty()) { if (first < 0 && !values[i].isEmpty()) {
first = i; first = static_cast<int>(i);
} }
} }
values.erase(values.begin() + valueCount, values.end()); values.erase(values.begin() + static_cast<typename std::remove_reference_t<decltype(values)>::difference_type>(valueCount), values.end());
return first; return first;
} }
@ -184,13 +184,13 @@ bool PicturePreviewSelection::setup(PreviousValueHandling previousValueHandling)
int first; int first;
switch (m_tag->type()) { switch (m_tag->type()) {
case TagType::Id3v2Tag: case TagType::Id3v2Tag:
first first = fetchId3v2CoverValues(static_cast<Id3v2Tag *>(m_tag), m_field, m_values,
= fetchId3v2CoverValues(static_cast<Id3v2Tag *>(m_tag), m_field, m_values, m_ui->switchTypeComboBox->count(), previousValueHandling); static_cast<std::size_t>(m_ui->switchTypeComboBox->count()), previousValueHandling);
break; break;
case TagType::VorbisComment: case TagType::VorbisComment:
case TagType::OggVorbisComment: case TagType::OggVorbisComment:
first = fetchId3v2CoverValues( first = fetchId3v2CoverValues(static_cast<VorbisComment *>(m_tag), m_field, m_values,
static_cast<VorbisComment *>(m_tag), m_field, m_values, m_ui->switchTypeComboBox->count(), previousValueHandling); static_cast<std::size_t>(m_ui->switchTypeComboBox->count()), previousValueHandling);
break; break;
default: default:
first = 0; first = 0;
@ -198,7 +198,7 @@ bool PicturePreviewSelection::setup(PreviousValueHandling previousValueHandling)
if (first >= 0) { if (first >= 0) {
m_ui->switchTypeComboBox->setCurrentIndex(first); m_ui->switchTypeComboBox->setCurrentIndex(first);
} }
m_currentTypeIndex = m_ui->switchTypeComboBox->currentIndex(); m_currentTypeIndex = static_cast<std::size_t>(m_ui->switchTypeComboBox->currentIndex());
connect(m_ui->switchTypeComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, connect(m_ui->switchTypeComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this,
&PicturePreviewSelection::typeSwitched); &PicturePreviewSelection::typeSwitched);
} else { } else {
@ -212,13 +212,13 @@ bool PicturePreviewSelection::setup(PreviousValueHandling previousValueHandling)
m_values[0] = value; m_values[0] = value;
} }
} else { } else {
m_values << value; m_values.emplace_back(value);
} }
} }
if (m_values.size()) { if (m_values.size()) {
m_values.erase(m_values.begin() + 1, m_values.end()); m_values.erase(m_values.begin() + 1, m_values.end());
} else { } else {
m_values << TagValue(); m_values.emplace_back();
} }
} }
const auto hideDesc = !m_tag->supportsDescription(m_field); const auto hideDesc = !m_tag->supportsDescription(m_field);
@ -324,14 +324,14 @@ void PicturePreviewSelection::assignImageToTagValue(const QImage &image, TagValu
* \param field Specifies the field. * \param field Specifies the field.
* \param values Specifies the values to be pushed. * \param values Specifies the values to be pushed.
*/ */
template <class TagType> void pushId3v2CoverValues(TagType *tag, KnownField field, const QList<TagParser::TagValue> &values) template <class TagType> void pushId3v2CoverValues(TagType *tag, KnownField field, const std::vector<TagParser::TagValue> &values)
{ {
auto &fields = tag->fields(); auto &fields = tag->fields();
const auto id = tag->fieldId(field); const auto id = tag->fieldId(field);
const auto range = fields.equal_range(id); const auto range = fields.equal_range(id);
const auto first = range.first; const auto first = range.first;
// iterate through all tag values
for (int index = 0, valueCount = values.size(); index < valueCount; ++index) { for (std::size_t index = 0, valueCount = values.size(); index < valueCount; ++index) {
// check whether there is already a tag value with the current index/type // check whether there is already a tag value with the current index/type
auto pair = find_if(first, range.second, std::bind(fieldPredicate<TagType>, index, placeholders::_1)); auto pair = find_if(first, range.second, std::bind(fieldPredicate<TagType>, index, placeholders::_1));
if (pair != range.second) { if (pair != range.second) {
@ -343,7 +343,7 @@ template <class TagType> void pushId3v2CoverValues(TagType *tag, KnownField fiel
// -> remove these values as we only support one value of a type in the same tag // -> remove these values as we only support one value of a type in the same tag
pair->second.setValue(TagValue()); pair->second.setValue(TagValue());
} }
} else if (!values[index].isEmpty()) { } else if (!values[static_cast<std::size_t>(index)].isEmpty()) {
using FieldType = typename TagType::FieldType; using FieldType = typename TagType::FieldType;
using TypeInfoType = typename FieldType::TypeInfoType; using TypeInfoType = typename FieldType::TypeInfoType;
using IndexCompareType = typename Traits::Conditional<std::is_unsigned<TypeInfoType>, make_unsigned<decltype(index)>::type, TypeInfoType>; using IndexCompareType = typename Traits::Conditional<std::is_unsigned<TypeInfoType>, make_unsigned<decltype(index)>::type, TypeInfoType>;
@ -351,7 +351,7 @@ template <class TagType> void pushId3v2CoverValues(TagType *tag, KnownField fiel
if (static_cast<IndexCompareType>(index) < numeric_limits<TypeInfoType>::max()) { if (static_cast<IndexCompareType>(index) < numeric_limits<TypeInfoType>::max()) {
newField.setTypeInfo(static_cast<TypeInfoType>(index)); newField.setTypeInfo(static_cast<TypeInfoType>(index));
} }
fields.insert(std::make_pair(id, newField)); fields.insert(std::pair(id, std::move(newField)));
} }
} }
} }
@ -375,8 +375,8 @@ void PicturePreviewSelection::apply()
default:; default:;
} }
} else { } else {
if (m_values.size()) { if (!m_values.empty()) {
m_tag->setValue(m_field, m_values.first()); m_tag->setValue(m_field, m_values.front());
} else { } else {
m_tag->setValue(m_field, TagValue()); m_tag->setValue(m_field, TagValue());
} }
@ -388,8 +388,8 @@ void PicturePreviewSelection::apply()
*/ */
void PicturePreviewSelection::clear() void PicturePreviewSelection::clear()
{ {
for (int i = 0, count = m_values.count(); i < count; ++i) { for (auto &value : m_values) {
m_values[i].clearDataAndMetadata(); value.clearDataAndMetadata();
} }
updatePreview(m_currentTypeIndex); updatePreview(m_currentTypeIndex);
updateDescription(m_currentTypeIndex); updateDescription(m_currentTypeIndex);
@ -487,17 +487,16 @@ void PicturePreviewSelection::pasteOfSelectedType(const char *format)
*/ */
void PicturePreviewSelection::removeSelected() void PicturePreviewSelection::removeSelected()
{ {
if (m_currentTypeIndex < m_values.size()) { if (m_currentTypeIndex >= m_values.size()) {
if (m_values[m_currentTypeIndex].isEmpty()) {
QMessageBox::information(this, QCoreApplication::applicationName(), tr("There is no cover to remove."));
} else {
m_values[m_currentTypeIndex].clearData();
updatePreview(m_currentTypeIndex);
emit pictureChanged();
}
} else {
throw logic_error("Invalid type selected (no corresponding value assigned)."); throw logic_error("Invalid type selected (no corresponding value assigned).");
} }
if (m_values[m_currentTypeIndex].isEmpty()) {
QMessageBox::information(this, QCoreApplication::applicationName(), tr("There is no cover to remove."));
} else {
m_values[m_currentTypeIndex].clearData();
updatePreview(m_currentTypeIndex);
emit pictureChanged();
}
} }
/*! /*!
@ -707,11 +706,11 @@ void PicturePreviewSelection::dropEvent(QDropEvent *event)
/*! /*!
* \brief Handles selected type being switched. * \brief Handles selected type being switched.
*/ */
void PicturePreviewSelection::typeSwitched(int index) void PicturePreviewSelection::typeSwitched(std::size_t index)
{ {
assert(m_currentTypeIndex < m_values.size()); assert(m_currentTypeIndex < m_values.size());
int lastIndex = m_currentTypeIndex; const auto lastIndex = m_currentTypeIndex;
if (index < 0 || index >= m_values.size()) { if (index >= m_values.size()) {
throw logic_error("current type index is invalid"); throw logic_error("current type index is invalid");
} else { } else {
m_currentTypeIndex = index; m_currentTypeIndex = index;
@ -723,7 +722,7 @@ void PicturePreviewSelection::typeSwitched(int index)
/*! /*!
* \brief Shows the description of type with the specified \a newIndex. * \brief Shows the description of type with the specified \a newIndex.
*/ */
void PicturePreviewSelection::updateDescription(int newIndex) void PicturePreviewSelection::updateDescription(std::size_t newIndex)
{ {
// show description of selected type // show description of selected type
m_ui->descriptionLineEdit->setText(Utility::stringToQString(m_values[newIndex].description(), m_values[newIndex].descriptionEncoding())); m_ui->descriptionLineEdit->setText(Utility::stringToQString(m_values[newIndex].description(), m_values[newIndex].descriptionEncoding()));
@ -732,7 +731,7 @@ void PicturePreviewSelection::updateDescription(int newIndex)
/*! /*!
* \brief Shows the description of type with the specified \a newIndex and saves the description of the type with the specified \a lastIndex before. * \brief Shows the description of type with the specified \a newIndex and saves the description of the type with the specified \a lastIndex before.
*/ */
void PicturePreviewSelection::updateDescription(int lastIndex, int newIndex) void PicturePreviewSelection::updateDescription(std::size_t lastIndex, std::size_t newIndex)
{ {
TagTextEncoding enc; TagTextEncoding enc;
if (m_tag) { if (m_tag) {
@ -750,7 +749,7 @@ void PicturePreviewSelection::updateDescription(int lastIndex, int newIndex)
/*! /*!
* \brief Updates the preview to show the type with the specified \a index. * \brief Updates the preview to show the type with the specified \a index.
*/ */
void PicturePreviewSelection::updatePreview(int index) void PicturePreviewSelection::updatePreview(std::size_t index)
{ {
if (!m_scene) { if (!m_scene) {
m_scene = new QGraphicsScene(m_ui->previewGraphicsView); m_scene = new QGraphicsScene(m_ui->previewGraphicsView);

View File

@ -74,10 +74,10 @@ protected:
void dropEvent(QDropEvent *event) override; void dropEvent(QDropEvent *event) override;
private Q_SLOTS: private Q_SLOTS:
void typeSwitched(int index); void typeSwitched(std::size_t index);
void updateDescription(int newIndex); void updateDescription(std::size_t newIndex);
void updateDescription(int lastIndex, int newIndex); void updateDescription(std::size_t lastIndex, std::size_t newIndex);
void updatePreview(int index); void updatePreview(std::size_t index);
void showContextMenu(const QPoint &position); void showContextMenu(const QPoint &position);
private: private:
@ -96,11 +96,11 @@ private:
QGraphicsRectItem *m_rectItem; QGraphicsRectItem *m_rectItem;
TagParser::Tag *m_tag; TagParser::Tag *m_tag;
TagParser::KnownField m_field; TagParser::KnownField m_field;
QList<TagParser::TagValue> m_values; std::vector<TagParser::TagValue> m_values;
std::size_t m_currentFileSize; std::size_t m_currentFileSize;
QSize m_currentResolution; QSize m_currentResolution;
QString m_currentMimeType; QString m_currentMimeType;
int m_currentTypeIndex; std::size_t m_currentTypeIndex;
}; };
/*! /*!