-
-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
215 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// | ||
// Created by aldo on 27/01/21. | ||
// | ||
|
||
#include "QskModelObjectBinder.h" | ||
|
||
#include <QAbstractItemModel> | ||
#include <QPointer> | ||
#include <qassert.h> | ||
|
||
class QskModelObjectBinder::PrivateData | ||
{ | ||
public: | ||
PrivateData( ) | ||
{ | ||
} | ||
|
||
struct Item { | ||
Item() :valid(false), column(-1) {}; | ||
Item(QObject *obj, int column, const QByteArray& property) | ||
: valid (true), object( obj ), column( column ), property( property ) | ||
{ | ||
}; | ||
bool valid; | ||
QPointer< QObject > object; | ||
int column; | ||
QByteArray property; | ||
}; | ||
void updateObjectProperty(Item & x) | ||
{ | ||
Q_ASSERT( x.object != nullptr ); | ||
Q_ASSERT( ! x.property.isEmpty() ); | ||
x.object->setProperty(x.property, | ||
m->index(modelIndex.row(),x.column).data(Qt::EditRole)); | ||
} | ||
|
||
void updateAllObjects() | ||
{ | ||
for (auto it = items.begin(); it != items.end(); ++it) { | ||
updateObjectProperty(it.value()); | ||
} | ||
} | ||
|
||
void updateModelField(Item& x) | ||
{ | ||
Q_ASSERT( x.valid ); | ||
Q_ASSERT( x.object != nullptr ); | ||
auto prop =x.object->property( x.property ); | ||
Q_ASSERT( prop.isValid() ); | ||
|
||
m->setData(m->index(modelIndex.row(),x.column),prop); | ||
} | ||
|
||
void updateModelFields() | ||
{ | ||
for (auto it = items.begin(); it != items.end(); ++it) | ||
{ | ||
updateModelField( it.value() ); | ||
} | ||
} | ||
|
||
public: | ||
QMap<QObject*,Item> items; | ||
QAbstractItemModel* m; | ||
QPersistentModelIndex modelIndex; | ||
}; | ||
|
||
static bool modelContainsIndex(const QModelIndex & idx, const QModelIndex & topLeft, const QModelIndex & bottomRight) | ||
{ | ||
return idx.row() >= topLeft.row() && idx.row() <= bottomRight.row() | ||
&& idx.column() >= topLeft.column() && idx.column() <= bottomRight.column(); | ||
} | ||
|
||
QskModelObjectBinder::QskModelObjectBinder(QObject* parent) | ||
: QObject( parent ) | ||
{ | ||
m_data=std::make_unique<PrivateData>( ); | ||
} | ||
|
||
QskModelObjectBinder::QskModelObjectBinder(QAbstractItemModel *model, QObject* parent) | ||
: QObject( parent ) | ||
{ | ||
m_data=std::make_unique<PrivateData>(); | ||
bindModel(model); | ||
} | ||
|
||
|
||
void QskModelObjectBinder::bindObject(QObject *obj, int column, const QByteArray &propertyName) | ||
{ | ||
Q_ASSERT( obj != nullptr ); | ||
|
||
if(obj == nullptr) | ||
return; | ||
|
||
QMetaProperty prop; | ||
auto meta = obj->metaObject(); | ||
if (propertyName.isEmpty()) | ||
{ | ||
prop = meta->userProperty(); | ||
}else | ||
{ | ||
auto idx = meta->indexOfProperty( propertyName ); | ||
if(idx>=0) | ||
{ | ||
prop=meta->property(idx); | ||
} | ||
} | ||
Q_ASSERT(! prop.isValid()); | ||
|
||
PrivateData::Item item(obj,column,prop.name()); | ||
m_data->items[obj]=item; | ||
} | ||
|
||
void QskModelObjectBinder::unbindObject(QObject *obj) | ||
{ | ||
m_data->items.remove(obj); | ||
} | ||
|
||
void QskModelObjectBinder::bindModel(QAbstractItemModel *model) | ||
{ | ||
m_data->m=model; | ||
m_data->modelIndex=QModelIndex(); | ||
if(model) { | ||
connect(m_data->m,&QAbstractItemModel::dataChanged,this, | ||
&QskModelObjectBinder::modelDataChanged); | ||
} | ||
} | ||
|
||
void QskModelObjectBinder::modelDataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight, const QVector<int> &) | ||
{ | ||
const int row = m_data->modelIndex.row(); | ||
for (auto it = m_data->items.begin(); it != m_data->items.end(); ++it) { | ||
if (modelContainsIndex(m_data->m->index(row,it.value().column), topLeft, bottomRight)) { | ||
m_data->updateObjectProperty(it.value()); | ||
} | ||
} | ||
} | ||
|
||
void QskModelObjectBinder::selectRow( int row ) | ||
{ | ||
Q_ASSERT( m_data->m != nullptr ); | ||
|
||
if ( !m_data->m ) | ||
return; | ||
|
||
m_data->modelIndex = m_data->m->index( row, 0 ); | ||
m_data->updateAllObjects(); | ||
} | ||
|
||
int QskModelObjectBinder::currentRow() const | ||
{ | ||
return m_data->modelIndex.row(); | ||
} | ||
|
||
void QskModelObjectBinder::updateModel() | ||
{ | ||
m_data->updateModelFields(); | ||
} | ||
|
||
#include "moc_QskModelObjectBinder.cpp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef QSKDATACONTROLMAPPER_H | ||
#define QSKDATACONTROLMAPPER_H | ||
|
||
#include <QskControl.h> | ||
#include <QAbstractItemModel> | ||
|
||
class QskModelObjectBinder : public QObject { | ||
Q_OBJECT | ||
|
||
public: | ||
QskModelObjectBinder(QObject *parent=nullptr); | ||
QskModelObjectBinder(QAbstractItemModel* model, QObject *parent=nullptr); | ||
|
||
void bindObject(QObject*, int column, const QByteArray &propertyName=""); | ||
void unbindObject(QObject*o); | ||
|
||
void bindModel(QAbstractItemModel* model); | ||
|
||
void selectRow(int row); | ||
[[nodiscard]] int currentRow() const; | ||
|
||
void updateModel(); | ||
|
||
private Q_SLOTS: | ||
void modelDataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight, const QVector<int> &); | ||
|
||
private: | ||
class PrivateData; | ||
std::unique_ptr< PrivateData > m_data; | ||
}; | ||
|
||
|
||
|
||
#endif //QSKDATACONTROLMAPPER_H |
Oops, something went wrong.