Override QAbstractItemModel::insertRows so UI code can add rows

master
Jos van den Oever 2017-10-03 00:34:44 +02:00
parent 117eddfa9e
commit 7ea7dd1d5e
10 changed files with 71 additions and 0 deletions

View File

@ -432,6 +432,7 @@ pub trait FibonacciListTrait {
fn new(emit: FibonacciListEmitter, model: FibonacciListList) -> Self;
fn emit(&self) -> &FibonacciListEmitter;
fn row_count(&self) -> usize;
fn insert_rows(&mut self, row: usize, count: usize) -> bool { false }
fn can_fetch_more(&self) -> bool {
false
}
@ -481,6 +482,10 @@ pub unsafe extern "C" fn fibonacci_list_row_count(ptr: *const FibonacciList) ->
(&*ptr).row_count() as c_int
}
#[no_mangle]
pub unsafe extern "C" fn fibonacci_list_insert_rows(ptr: *mut FibonacciList, row: c_int, count: c_int) -> bool {
(&mut *ptr).insert_rows(row as usize, count as usize)
}
#[no_mangle]
pub unsafe extern "C" fn fibonacci_list_can_fetch_more(ptr: *const FibonacciList) -> bool {
(&*ptr).can_fetch_more()
}
@ -1082,6 +1087,7 @@ pub trait TimeSeriesTrait {
fn new(emit: TimeSeriesEmitter, model: TimeSeriesList) -> Self;
fn emit(&self) -> &TimeSeriesEmitter;
fn row_count(&self) -> usize;
fn insert_rows(&mut self, row: usize, count: usize) -> bool { false }
fn can_fetch_more(&self) -> bool {
false
}
@ -1135,6 +1141,10 @@ pub unsafe extern "C" fn time_series_row_count(ptr: *const TimeSeries) -> c_int
(&*ptr).row_count() as c_int
}
#[no_mangle]
pub unsafe extern "C" fn time_series_insert_rows(ptr: *mut TimeSeries, row: c_int, count: c_int) -> bool {
(&mut *ptr).insert_rows(row as usize, count as usize)
}
#[no_mangle]
pub unsafe extern "C" fn time_series_can_fetch_more(ptr: *const TimeSeries) -> bool {
(&*ptr).can_fetch_more()
}

View File

@ -152,6 +152,7 @@ extern "C" {
void fibonacci_list_sort(FibonacciList::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder);
int fibonacci_list_row_count(const FibonacciList::Private*);
bool fibonacci_list_insert_rows(FibonacciList::Private*, int, int);
bool fibonacci_list_can_fetch_more(const FibonacciList::Private*);
void fibonacci_list_fetch_more(FibonacciList::Private*);
}
@ -170,6 +171,11 @@ int FibonacciList::rowCount(const QModelIndex &parent) const
return (parent.isValid()) ? 0 : fibonacci_list_row_count(m_d);
}
bool FibonacciList::insertRows(int row, int count, const QModelIndex &parent)
{
return fibonacci_list_insert_rows(m_d, row, count);
}
QModelIndex FibonacciList::index(int row, int column, const QModelIndex &parent) const
{
if (!parent.isValid() && row >= 0 && row < rowCount(parent) && column >= 0 && column < 2) {
@ -311,6 +317,11 @@ int FileSystemTree::rowCount(const QModelIndex &parent) const
return file_system_tree_row_count(m_d, parent.internalId(), parent.isValid());
}
bool FileSystemTree::insertRows(int, int, const QModelIndex &)
{
return false; // not supported yet
}
QModelIndex FileSystemTree::index(int row, int column, const QModelIndex &parent) const
{
if (row < 0 || column < 0 || column >= 5) {
@ -533,6 +544,11 @@ int Processes::rowCount(const QModelIndex &parent) const
return processes_row_count(m_d, parent.internalId(), parent.isValid());
}
bool Processes::insertRows(int, int, const QModelIndex &)
{
return false; // not supported yet
}
QModelIndex Processes::index(int row, int column, const QModelIndex &parent) const
{
if (row < 0 || column < 0 || column >= 3) {
@ -725,6 +741,7 @@ extern "C" {
void time_series_sort(TimeSeries::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder);
int time_series_row_count(const TimeSeries::Private*);
bool time_series_insert_rows(TimeSeries::Private*, int, int);
bool time_series_can_fetch_more(const TimeSeries::Private*);
void time_series_fetch_more(TimeSeries::Private*);
}
@ -743,6 +760,11 @@ int TimeSeries::rowCount(const QModelIndex &parent) const
return (parent.isValid()) ? 0 : time_series_row_count(m_d);
}
bool TimeSeries::insertRows(int row, int count, const QModelIndex &parent)
{
return time_series_insert_rows(m_d, row, count);
}
QModelIndex TimeSeries::index(int row, int column, const QModelIndex &parent) const
{
if (!parent.isValid() && row >= 0 && row < rowCount(parent) && column >= 0 && column < 3) {

View File

@ -102,6 +102,7 @@ public:
QHash<int, QByteArray> roleNames() const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override;
Q_INVOKABLE bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
Q_INVOKABLE QVariant fibonacciNumber(int row) const;
Q_INVOKABLE QVariant row(int row) const;
@ -144,6 +145,7 @@ public:
QHash<int, QByteArray> roleNames() const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override;
Q_INVOKABLE bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
Q_INVOKABLE QVariant fileIcon(const QModelIndex& index) const;
Q_INVOKABLE QVariant fileName(const QModelIndex& index) const;
Q_INVOKABLE QVariant filePath(const QModelIndex& index) const;
@ -191,6 +193,7 @@ public:
QHash<int, QByteArray> roleNames() const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override;
Q_INVOKABLE bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
Q_INVOKABLE QVariant cmd(const QModelIndex& index) const;
Q_INVOKABLE QVariant cpuPercentage(const QModelIndex& index) const;
Q_INVOKABLE QVariant cpuUsage(const QModelIndex& index) const;
@ -236,6 +239,7 @@ public:
QHash<int, QByteArray> roleNames() const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override;
Q_INVOKABLE bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
Q_INVOKABLE QVariant cos(int row) const;
Q_INVOKABLE bool setCos(int row, const QVariant& value);

View File

@ -78,6 +78,7 @@ void writeHeaderItemModel(QTextStream& h, const Object& o) {
QHash<int, QByteArray> roleNames() const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override;
Q_INVOKABLE bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
)");
if (modelIsWritable(o)) {
h << " bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;\n";
@ -224,6 +225,7 @@ void writeCppModel(QTextStream& cpp, const Object& o) {
if (o.type == ObjectType::List) {
cpp << QString(R"(
int %2_row_count(const %1::Private*);
bool %2_insert_rows(%1::Private*, int, int);
bool %2_can_fetch_more(const %1::Private*);
void %2_fetch_more(%1::Private*);
}
@ -242,6 +244,11 @@ int %1::rowCount(const QModelIndex &parent) const
return (parent.isValid()) ? 0 : %2_row_count(m_d);
}
bool %1::insertRows(int row, int count, const QModelIndex &parent)
{
return %2_insert_rows(m_d, row, count);
}
QModelIndex %1::index(int row, int column, const QModelIndex &parent) const
{
if (!parent.isValid() && row >= 0 && row < rowCount(parent) && column >= 0 && column < %3) {
@ -294,6 +301,11 @@ int %1::rowCount(const QModelIndex &parent) const
return %2_row_count(m_d, parent.internalId(), parent.isValid());
}
bool %1::insertRows(int, int, const QModelIndex &)
{
return false; // not supported yet
}
QModelIndex %1::index(int row, int column, const QModelIndex &parent) const
{
if (row < 0 || column < 0 || column >= %3) {

View File

@ -284,6 +284,7 @@ pub trait %1Trait {
}
if (o.type == ObjectType::List) {
r << R"( fn row_count(&self) -> usize;
fn insert_rows(&mut self, row: usize, count: usize) -> bool { false }
fn can_fetch_more(&self) -> bool {
false
}
@ -414,6 +415,10 @@ pub unsafe extern "C" fn %2_row_count(ptr: *const %1) -> c_int {
(&*ptr).row_count() as c_int
}
#[no_mangle]
pub unsafe extern "C" fn %2_insert_rows(ptr: *mut %1, row: c_int, count: c_int) -> bool {
(&mut *ptr).insert_rows(row as usize, count as usize)
}
#[no_mangle]
pub unsafe extern "C" fn %2_can_fetch_more(ptr: *const %1) -> bool {
(&*ptr).can_fetch_more()
}

View File

@ -147,6 +147,7 @@ pub trait PersonsTrait {
fn new(emit: PersonsEmitter, model: PersonsList) -> Self;
fn emit(&self) -> &PersonsEmitter;
fn row_count(&self) -> usize;
fn insert_rows(&mut self, row: usize, count: usize) -> bool { false }
fn can_fetch_more(&self) -> bool {
false
}
@ -196,6 +197,10 @@ pub unsafe extern "C" fn persons_row_count(ptr: *const Persons) -> c_int {
(&*ptr).row_count() as c_int
}
#[no_mangle]
pub unsafe extern "C" fn persons_insert_rows(ptr: *mut Persons, row: c_int, count: c_int) -> bool {
(&mut *ptr).insert_rows(row as usize, count as usize)
}
#[no_mangle]
pub unsafe extern "C" fn persons_can_fetch_more(ptr: *const Persons) -> bool {
(&*ptr).can_fetch_more()
}

View File

@ -44,6 +44,7 @@ extern "C" {
void persons_sort(Persons::Private*, unsigned char column, Qt::SortOrder order = Qt::AscendingOrder);
int persons_row_count(const Persons::Private*);
bool persons_insert_rows(Persons::Private*, int, int);
bool persons_can_fetch_more(const Persons::Private*);
void persons_fetch_more(Persons::Private*);
}
@ -62,6 +63,11 @@ int Persons::rowCount(const QModelIndex &parent) const
return (parent.isValid()) ? 0 : persons_row_count(m_d);
}
bool Persons::insertRows(int row, int count, const QModelIndex &parent)
{
return persons_insert_rows(m_d, row, count);
}
QModelIndex Persons::index(int row, int column, const QModelIndex &parent) const
{
if (!parent.isValid() && row >= 0 && row < rowCount(parent) && column >= 0 && column < 1) {

View File

@ -33,6 +33,7 @@ public:
QHash<int, QByteArray> roleNames() const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override;
Q_INVOKABLE bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
Q_INVOKABLE QVariant userName(int row) const;
Q_INVOKABLE bool setUserName(int row, const QVariant& value);

View File

@ -68,6 +68,11 @@ int Persons::rowCount(const QModelIndex &parent) const
return persons_row_count(m_d, parent.internalId(), parent.isValid());
}
bool Persons::insertRows(int, int, const QModelIndex &)
{
return false; // not supported yet
}
QModelIndex Persons::index(int row, int column, const QModelIndex &parent) const
{
if (row < 0 || column < 0 || column >= 1) {

View File

@ -33,6 +33,7 @@ public:
QHash<int, QByteArray> roleNames() const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role = Qt::EditRole) override;
Q_INVOKABLE bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
Q_INVOKABLE QVariant userName(const QModelIndex& index) const;
Q_INVOKABLE bool setUserName(const QModelIndex& index, const QVariant& value);