Skip to content
This repository has been archived by the owner on Feb 17, 2021. It is now read-only.

Commit

Permalink
added remove_row and clear for QListModel,
Browse files Browse the repository at this point in the history
removed a bug there inserts after clear might lead to empty entries in the model
  • Loading branch information
Eduard Stefes authored and White-Oak committed Oct 16, 2016
1 parent b5846b1 commit 700962c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@ macro_rules! Q_LISTMODEL{
self.qalm.insert_row(vec.into_iter());
}

/// Remove a row from this model
#[allow(unused_mut)]
pub fn remove_row(&mut self, index: usize) {
self.qalm.remove_row(index);
}

/// Gets an accoiated qvariant
pub fn get_qvar(&self) -> QVariant{
self.qalm.get_qvar()
Expand Down Expand Up @@ -352,6 +358,14 @@ macro_rules! Q_LISTMODEL{
)*
self.qalm.change_line(index, vec);
}

/// Remove all rows from this model
#[allow(unused_mut)]
pub fn clear(&mut self) {
self.qalm.clear();
}


}

impl<'a, 'b> From<&'a $wrapper> for QVariant {
Expand Down
33 changes: 32 additions & 1 deletion src/qabstactlistmodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,16 @@ extern "C" {
parent: DosQModelIndex,
first: i32,
last: i32);
fn dos_qabstractlistmodel_beginRemoveRows(vptr: DosQAbstractListModel,
parent: DosQModelIndex,
first: i32,
last: i32);

fn dos_qabstractlistmodel_endInsertRows(vptr: DosQAbstractListModel);

fn dos_qabstractlistmodel_beginResetModel(vptr: DosQAbstractListModel);
fn dos_qabstractlistmodel_endResetModel(vptr: DosQAbstractListModel);
fn dos_qabstractlistmodel_endRemoveRows(vptr: DosQAbstractListModel);
}

impl<'a> QListModel<'a> {
Expand Down Expand Up @@ -163,12 +169,26 @@ impl<'a> QListModel<'a> {
dos_qabstractlistmodel_beginInsertRows(self.wrapped.load(Ordering::Relaxed),
get_model_ptr(&index),
self.model.len() as i32,
(self.model.len() + 1) as i32);
(self.model.len() ) as i32);
self.model.push(qvars.collect());
dos_qabstractlistmodel_endInsertRows(self.wrapped.load(Ordering::Relaxed));
}
}

/// Remove a line from the model.
pub fn remove_row(&mut self, index: usize)
{
unsafe {
let modelindex = QModelIndex::new();
dos_qabstractlistmodel_beginRemoveRows(self.wrapped.load(Ordering::Relaxed),
get_model_ptr(&modelindex),
index as i32,
index as i32);
self.model.remove(index as usize);
dos_qabstractlistmodel_endRemoveRows(self.wrapped.load(Ordering::Relaxed));
}
}

/// Sets a data for this QAbstractListModel
pub fn set_data(&mut self, qvars: Vec<Vec<QVariant>>) {
unsafe {
Expand All @@ -187,10 +207,21 @@ impl<'a> QListModel<'a> {
}
}

/// Clear all the data from the model
pub fn clear(&mut self) {
unsafe {
dos_qabstractlistmodel_beginResetModel(self.wrapped.load(Ordering::Relaxed));
self.model.clear();
dos_qabstractlistmodel_endResetModel(self.wrapped.load(Ordering::Relaxed));
}
}

/// Gets an immutable view of the data
pub fn view_data(&self) -> &[Vec<QVariant>] {
&self.model
}


}

impl<'a, 'b> From<&'a QListModel<'b>> for QVariant {
Expand Down

0 comments on commit 700962c

Please sign in to comment.