Replace one-use methods with lambdas

This commit is contained in:
Gres 2020-04-08 20:51:32 +03:00
parent 1ba4f6be4d
commit eff7552d07
2 changed files with 41 additions and 42 deletions

View File

@ -521,25 +521,27 @@ UserActions Model::userActions() const
if (!root_)
return {};
UserActions result;
fillUserActions(result, *root_);
return result;
}
UserActions actions;
void Model::fillUserActions(UserActions &actions, Component &component) const
{
if (!component.files.empty()) {
if (component.action == Action::NoAction)
const auto visitor = [&actions](const Component &component, auto v) -> void {
if (!component.files.empty()) {
if (component.action == Action::NoAction)
return;
for (auto &file : component.files)
actions.emplace(component.action, file);
return;
}
for (auto &file : component.files) actions.emplace(component.action, file);
return;
}
if (!component.children.empty()) {
for (auto &child : component.children) v(*child, v);
return;
}
};
if (!component.children.empty()) {
for (auto &child : component.children) fillUserActions(actions, *child);
return;
}
visitor(*root_, visitor);
return actions;
}
void Model::updateStates()
@ -548,14 +550,36 @@ void Model::updateStates()
return;
updateState(*root_);
emitColumnsChanged(QModelIndex());
const auto visitor = [this](const QModelIndex &parent, auto v) -> void {
const auto count = rowCount(parent);
if (count == 0)
return;
emit dataChanged(index(0, int(Column::State), parent),
index(count - 1, int(Column::Action), parent),
{Qt::DisplayRole, Qt::EditRole});
for (auto i = 0; i < count; ++i) v(index(0, 0, parent), v);
};
visitor(QModelIndex(), visitor);
}
bool Model::hasUpdates() const
{
if (!root_)
return false;
return hasUpdates(*root_);
const auto visitor = [](const Component &component, auto v) -> bool {
for (const auto &i : component.children) {
if (i->state == State::UpdateAvailable || v(*i, v))
return true;
}
return false;
};
return visitor(*root_, visitor);
}
void Model::updateProgress(const QUrl &url, int progress)
@ -614,15 +638,6 @@ void Model::resetActions()
visitor(*root_, visitor);
}
bool Model::hasUpdates(const Model::Component &component) const
{
for (const auto &i : component.children) {
if (i->state == State::UpdateAvailable || hasUpdates(*i))
return true;
}
return false;
}
void Model::updateState(Model::Component &component)
{
if (!component.files.empty()) {
@ -687,19 +702,6 @@ QString Model::expanded(const QString &source) const
return result;
}
void Model::emitColumnsChanged(const QModelIndex &parent)
{
const auto count = rowCount(parent);
if (count == 0)
return;
emit dataChanged(index(0, int(Column::State), parent),
index(count - 1, int(Column::Action), parent),
{Qt::DisplayRole, Qt::EditRole});
for (auto i = 0; i < count; ++i) emitColumnsChanged(index(0, 0, parent));
}
Model::Component *Model::toComponent(const QModelIndex &index) const
{
return static_cast<Component *>(index.internalPointer());

View File

@ -93,11 +93,8 @@ private:
std::unique_ptr<Component> parse(const QJsonObject& json) const;
void updateProgress(Component& component, const QUrl& url, int progress);
void updateState(Component& component);
bool hasUpdates(const Component& component) const;
void fillUserActions(UserActions& actions, Component& component) const;
State currentState(const File& file) const;
QString expanded(const QString& source) const;
void emitColumnsChanged(const QModelIndex& parent);
Component* toComponent(const QModelIndex& index) const;
QModelIndex toIndex(const Component& component, int column) const;