Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default Model Values #123

Merged
merged 12 commits into from
Jun 1, 2020
Merged

Default Model Values #123

merged 12 commits into from
Jun 1, 2020

Conversation

RobertBColton
Copy link
Contributor

@RobertBColton RobertBColton commented May 6, 2020

This is how Josh would like to resolve #122 by making it explicit when you are ok accepting the default value from the model. I had to also allow column 0 as a special sentinel for RepeatedMessageModel to return a pointer to the requested model so that I have an actual way to call dataOrDefault on repeated messages. The hack avoids the complication of having to add dataOrDefault on all the higher level models and interfaces, such as the sort filter proxy model.

Models/MessageModel.cpp Outdated Show resolved Hide resolved
@@ -131,7 +132,7 @@ QVariant MessageModel::data(const QModelIndex &index, int role) const {
}

// If the field has't been initialized return an invalid QVariant. (see QVariant.isValid())
if (!refl->HasField(*_protobuf, field)) return QVariant();
if (HasField && !refl->HasField(*_protobuf, field)) return QVariant();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rusky says we should treat this as an error/with an assert.

Copy link
Member

@JoshDreamland JoshDreamland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM; you can enforce the optimization of HasField by making a separate if constexpr for it, but the optimizer should have no trouble with it as-is.

@@ -41,5 +41,7 @@ bool RepeatedMessageModel::setData(const QModelIndex &index, const QVariant &val
}

QVariant RepeatedMessageModel::data(const QModelIndex &index, int role) const {
// protobuf field number with 0 is impossible, use as sentinel to get model itself
if (index.column() == 0) return QVariant::fromValue(_subModels[index.row()]);
Copy link
Contributor Author

@RobertBColton RobertBColton May 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's any implications of this? This is otherwise just an error condition. Unless later we add field number offsets to MessageModel so that we can use field number 0 for whatever reason fundies wanted to. We could possibly just add a custom role for this instead too.

@RobertBColton RobertBColton merged commit 70e9113 into master Jun 1, 2020
@RobertBColton RobertBColton deleted the data-default-fix branch June 1, 2020 17:31
@RobertBColton RobertBColton mentioned this pull request Jun 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Default Values
2 participants