| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- //EasyTableModel.cpp
- #include "EasyTableModel.h"
- #include <m_loger.h>
- EasyTableModel::EasyTableModel(QObject *parent)
- : QAbstractTableModel(parent)
- {
- }
- QStringList EasyTableModel::getHorHeader() const
- {
- return _horHeaderList;
- }
- void EasyTableModel::setHorHeader(const QStringList &header)
- {
- _horHeaderList=header;
- emit horHeaderChanged();
- }
- QString EasyTableModel::getTable() const
- {
- return _table_name;
- }
- void EasyTableModel::setTable(const QString &strA)
- {
- _table_name = strA;
- emit initTableChanged();
- }
- QJsonArray EasyTableModel::getInitData() const
- {
- return _initData;
- }
- void EasyTableModel::setInitData(const QJsonArray &jsonArr)
- {
- _initData=jsonArr;
- //设置最大值
- if(_completed){
- loadData(_initData,_table_name);
- }
- emit initDataChanged();
- }
- void EasyTableModel::classBegin()
- {
- DEBUG("EasyTableModel::classBegin()");
- }
- void EasyTableModel::componentComplete()
- {
- DEBUG("EasyTableModel::componentComplete()");
- _completed=true;
- if(!_initData.isEmpty()){
- loadData(_initData,_table_name);
- }
- }
- QHash<int, QByteArray> EasyTableModel::roleNames() const
- {
- //value表示取值,edit表示编辑
- return QHash<int,QByteArray>{
- { Qt::DisplayRole,"value" },
- { Qt::EditRole,"edit" }
- };
- }
- QVariant EasyTableModel::headerData(int section, Qt::Orientation orientation, int role) const
- {
- //返回表头数据,无效的返回None
- if(role==Qt::DisplayRole){
- if(orientation==Qt::Horizontal){
- return _horHeaderList.value(section,QString::number(section));
- }else if(orientation==Qt::Vertical){
- return QString::number(section);
- }
- }
- return QVariant();
- }
- bool EasyTableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
- {
- if (value != headerData(section, orientation, role)) {
- if(orientation==Qt::Horizontal&&role==Qt::EditRole){
- _horHeaderList[section]=value.toString();
- emit headerDataChanged(orientation, section, section);
- return true;
- }
- }
- return false;
- }
- int EasyTableModel::rowCount(const QModelIndex &parent) const
- {
- if (parent.isValid())
- return 0;
- return _modelData.count();
- }
- int EasyTableModel::columnCount(const QModelIndex &parent) const
- {
- if (parent.isValid())
- return 0;
- return _horHeaderList.count();
- }
- QVariant EasyTableModel::data(const QModelIndex &index, int role) const
- {
- if (!index.isValid())
- return QVariant();
- switch (role) {
- case Qt::DisplayRole:
- case Qt::EditRole:
- return _modelData.at(index.row()).at(index.column());
- default:
- break;
- }
- return QVariant();
- }
- bool EasyTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
- {
- if (value.isValid()&&index.isValid()&&(data(index, role) != value)) {
- if(Qt::EditRole==role){
- _modelData[index.row()][index.column()]=value;
- DEBUG("_modelData: row={} col={}", index.row(), index.column());
- //emit dataChanged(index, index, QVector<int>() << role); //
- emit dataChanged();
- return true;
- }
- }
- return false;
- }
- Qt::ItemFlags EasyTableModel::flags(const QModelIndex &index) const
- {
- if (!index.isValid())
- return Qt::NoItemFlags;
- return Qt::ItemIsEnabled|Qt::ItemIsSelectable|Qt::ItemIsEditable;
- }
- void EasyTableModel::loadData(const QJsonArray &data,QString &table_name)
- {
- DEBUG("tbname: {}", table_name.toStdString());
- //如果要区分类型的话,可以用role,
- //这样ui中就能使用model.role来获取对应index的参数
- QVector<QVector<QVariant>> newData;
- QJsonArray::const_iterator iter;
- for(iter=data.begin();iter!=data.end();++iter){
- QVector<QVariant> newRow;
- const QJsonObject itemRow=(*iter).toObject();
- if(table_name != QString::fromLocal8Bit("camera"))
- newRow.append(itemRow.value("id"));
- if(table_name == QString::fromLocal8Bit("mqtt") ||table_name == QString::fromLocal8Bit("mqtt_other"))
- {
- newRow.append(itemRow.value("dtype"));
- newRow.append(itemRow.value("ip"));
- newRow.append(itemRow.value("port"));
- newRow.append(itemRow.value("imei"));
- newRow.append(itemRow.value("uid"));
- newRow.append(itemRow.value("pwd"));
- newRow.append(itemRow.value("clientid"));
- newRow.append(itemRow.value("qos"));
- newRow.append(itemRow.value("keepalive"));
- newRow.append(itemRow.value("sub"));
- newRow.append(itemRow.value("pub"));
- newRow.append(itemRow.value("offline"));
- }
- else if(table_name == QString::fromLocal8Bit("ftp") ||table_name == QString::fromLocal8Bit("ftp_other"))
- {
- newRow.append(itemRow.value("ip"));
- newRow.append(itemRow.value("port"));
- newRow.append(itemRow.value("dir"));
- newRow.append(itemRow.value("uid"));
- newRow.append(itemRow.value("pwd"));
- newRow.append(itemRow.value("mode"));
- }
- else if(table_name == QString::fromLocal8Bit("location"))
- {
- newRow.append(itemRow.value("lbs_lat"));
- newRow.append(itemRow.value("lbs_lng"));
- newRow.append(itemRow.value("gps_lat"));
- newRow.append(itemRow.value("gps_lng"));
- newRow.append(itemRow.value("gps_elevation"));
- }else if(table_name == QString::fromLocal8Bit("camera")){
- newRow.append(itemRow.value("camera_type"));
- newRow.append(itemRow.value("mode"));
- newRow.append(itemRow.value("stitch_photonum"));
- newRow.append(itemRow.value("photo_width"));
- newRow.append(itemRow.value("photo_height"));
- newRow.append(itemRow.value("auto_focus"));
- newRow.append(itemRow.value("movz_f"));
- newRow.append(itemRow.value("movz_b"));
- newRow.append(itemRow.value("movx_f"));
- newRow.append(itemRow.value("movx_b"));
- }
- else if(table_name == QString::fromLocal8Bit("data"))
- {
- newRow.append(itemRow.value("datetime"));
- newRow.append(itemRow.value("Json_data"));
- newRow.append(itemRow.value("Json_warn"));
- }
- //ftp
- newData.append(newRow);
- }
- emit beginResetModel();
- _modelData=newData;
- emit endResetModel();
- }
|