EasyTableModel.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //EasyTableModel.cpp
  2. #include "EasyTableModel.h"
  3. #include <m_loger.h>
  4. EasyTableModel::EasyTableModel(QObject *parent)
  5. : QAbstractTableModel(parent)
  6. {
  7. }
  8. QStringList EasyTableModel::getHorHeader() const
  9. {
  10. return _horHeaderList;
  11. }
  12. void EasyTableModel::setHorHeader(const QStringList &header)
  13. {
  14. _horHeaderList=header;
  15. emit horHeaderChanged();
  16. }
  17. QString EasyTableModel::getTable() const
  18. {
  19. return _table_name;
  20. }
  21. void EasyTableModel::setTable(const QString &strA)
  22. {
  23. _table_name = strA;
  24. emit initTableChanged();
  25. }
  26. QJsonArray EasyTableModel::getInitData() const
  27. {
  28. return _initData;
  29. }
  30. void EasyTableModel::setInitData(const QJsonArray &jsonArr)
  31. {
  32. _initData=jsonArr;
  33. //设置最大值
  34. if(_completed){
  35. loadData(_initData,_table_name);
  36. }
  37. emit initDataChanged();
  38. }
  39. void EasyTableModel::classBegin()
  40. {
  41. DEBUG("EasyTableModel::classBegin()");
  42. }
  43. void EasyTableModel::componentComplete()
  44. {
  45. DEBUG("EasyTableModel::componentComplete()");
  46. _completed=true;
  47. if(!_initData.isEmpty()){
  48. loadData(_initData,_table_name);
  49. }
  50. }
  51. QHash<int, QByteArray> EasyTableModel::roleNames() const
  52. {
  53. //value表示取值,edit表示编辑
  54. return QHash<int,QByteArray>{
  55. { Qt::DisplayRole,"value" },
  56. { Qt::EditRole,"edit" }
  57. };
  58. }
  59. QVariant EasyTableModel::headerData(int section, Qt::Orientation orientation, int role) const
  60. {
  61. //返回表头数据,无效的返回None
  62. if(role==Qt::DisplayRole){
  63. if(orientation==Qt::Horizontal){
  64. return _horHeaderList.value(section,QString::number(section));
  65. }else if(orientation==Qt::Vertical){
  66. return QString::number(section);
  67. }
  68. }
  69. return QVariant();
  70. }
  71. bool EasyTableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role)
  72. {
  73. if (value != headerData(section, orientation, role)) {
  74. if(orientation==Qt::Horizontal&&role==Qt::EditRole){
  75. _horHeaderList[section]=value.toString();
  76. emit headerDataChanged(orientation, section, section);
  77. return true;
  78. }
  79. }
  80. return false;
  81. }
  82. int EasyTableModel::rowCount(const QModelIndex &parent) const
  83. {
  84. if (parent.isValid())
  85. return 0;
  86. return _modelData.count();
  87. }
  88. int EasyTableModel::columnCount(const QModelIndex &parent) const
  89. {
  90. if (parent.isValid())
  91. return 0;
  92. return _horHeaderList.count();
  93. }
  94. QVariant EasyTableModel::data(const QModelIndex &index, int role) const
  95. {
  96. if (!index.isValid())
  97. return QVariant();
  98. switch (role) {
  99. case Qt::DisplayRole:
  100. case Qt::EditRole:
  101. return _modelData.at(index.row()).at(index.column());
  102. default:
  103. break;
  104. }
  105. return QVariant();
  106. }
  107. bool EasyTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
  108. {
  109. if (value.isValid()&&index.isValid()&&(data(index, role) != value)) {
  110. if(Qt::EditRole==role){
  111. _modelData[index.row()][index.column()]=value;
  112. DEBUG("_modelData: row={} col={}", index.row(), index.column());
  113. //emit dataChanged(index, index, QVector<int>() << role); //
  114. emit dataChanged();
  115. return true;
  116. }
  117. }
  118. return false;
  119. }
  120. Qt::ItemFlags EasyTableModel::flags(const QModelIndex &index) const
  121. {
  122. if (!index.isValid())
  123. return Qt::NoItemFlags;
  124. return Qt::ItemIsEnabled|Qt::ItemIsSelectable|Qt::ItemIsEditable;
  125. }
  126. void EasyTableModel::loadData(const QJsonArray &data,QString &table_name)
  127. {
  128. DEBUG("tbname: {}", table_name.toStdString());
  129. //如果要区分类型的话,可以用role,
  130. //这样ui中就能使用model.role来获取对应index的参数
  131. QVector<QVector<QVariant>> newData;
  132. QJsonArray::const_iterator iter;
  133. for(iter=data.begin();iter!=data.end();++iter){
  134. QVector<QVariant> newRow;
  135. const QJsonObject itemRow=(*iter).toObject();
  136. if(table_name != QString::fromLocal8Bit("camera"))
  137. newRow.append(itemRow.value("id"));
  138. if(table_name == QString::fromLocal8Bit("mqtt") ||table_name == QString::fromLocal8Bit("mqtt_other"))
  139. {
  140. newRow.append(itemRow.value("dtype"));
  141. newRow.append(itemRow.value("ip"));
  142. newRow.append(itemRow.value("port"));
  143. newRow.append(itemRow.value("imei"));
  144. newRow.append(itemRow.value("uid"));
  145. newRow.append(itemRow.value("pwd"));
  146. newRow.append(itemRow.value("clientid"));
  147. newRow.append(itemRow.value("qos"));
  148. newRow.append(itemRow.value("keepalive"));
  149. newRow.append(itemRow.value("sub"));
  150. newRow.append(itemRow.value("pub"));
  151. newRow.append(itemRow.value("offline"));
  152. }
  153. else if(table_name == QString::fromLocal8Bit("ftp") ||table_name == QString::fromLocal8Bit("ftp_other"))
  154. {
  155. newRow.append(itemRow.value("ip"));
  156. newRow.append(itemRow.value("port"));
  157. newRow.append(itemRow.value("dir"));
  158. newRow.append(itemRow.value("uid"));
  159. newRow.append(itemRow.value("pwd"));
  160. newRow.append(itemRow.value("mode"));
  161. }
  162. else if(table_name == QString::fromLocal8Bit("location"))
  163. {
  164. newRow.append(itemRow.value("lbs_lat"));
  165. newRow.append(itemRow.value("lbs_lng"));
  166. newRow.append(itemRow.value("gps_lat"));
  167. newRow.append(itemRow.value("gps_lng"));
  168. newRow.append(itemRow.value("gps_elevation"));
  169. }else if(table_name == QString::fromLocal8Bit("camera")){
  170. newRow.append(itemRow.value("camera_type"));
  171. newRow.append(itemRow.value("mode"));
  172. newRow.append(itemRow.value("stitch_photonum"));
  173. newRow.append(itemRow.value("photo_width"));
  174. newRow.append(itemRow.value("photo_height"));
  175. newRow.append(itemRow.value("auto_focus"));
  176. newRow.append(itemRow.value("movz_f"));
  177. newRow.append(itemRow.value("movz_b"));
  178. newRow.append(itemRow.value("movx_f"));
  179. newRow.append(itemRow.value("movx_b"));
  180. }
  181. else if(table_name == QString::fromLocal8Bit("data"))
  182. {
  183. newRow.append(itemRow.value("datetime"));
  184. newRow.append(itemRow.value("Json_data"));
  185. newRow.append(itemRow.value("Json_warn"));
  186. }
  187. //ftp
  188. newData.append(newRow);
  189. }
  190. emit beginResetModel();
  191. _modelData=newData;
  192. emit endResetModel();
  193. }