Skip to content

Add iter_mut and IndexMut to SimpleListItem #266

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions qmetaobject/src/listmodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

use std::collections::HashMap;
use std::iter::FromIterator;
use std::ops::Index;
use std::ops::{ Index, IndexMut };

use cpp::cpp;

Expand Down Expand Up @@ -274,18 +274,25 @@ impl<T: SimpleListItem> SimpleListModel<T> {
}
pub fn change_line(&mut self, index: usize, value: T) {
self.values[index] = value;
let idx = (self as &mut dyn QAbstractListModel).row_index(index as i32);
(self as &mut dyn QAbstractListModel).data_changed(idx, idx);
self.data_changed(index);
}
pub fn reset_data(&mut self, data: Vec<T>) {
(self as &mut dyn QAbstractListModel).begin_reset_model();
self.values = data;
(self as &mut dyn QAbstractListModel).end_reset_model();
}
pub fn data_changed(&mut self, index: usize) {
let idx = (self as &mut dyn QAbstractListModel).row_index(index as i32);
(self as &mut dyn QAbstractListModel).data_changed(idx, idx);
}
/// Returns an iterator over the items in the model
pub fn iter(&self) -> impl Iterator<Item = &T> {
self.values.iter()
}
/// Returns mutable iterator over the items in the model
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> {
self.values.iter_mut()
}
}

impl<T> FromIterator<T> for SimpleListModel<T>
Expand Down Expand Up @@ -319,3 +326,12 @@ where
&self.values[index]
}
}

impl<T> IndexMut<usize> for SimpleListModel<T>
where
T: SimpleListItem,
{
fn index_mut(&mut self, index: usize) -> &mut T {
&mut self.values[index]
}
}