MatrixTableTechMeasuresWidget.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "MatrixTableTechMeasuresWidget.h"
  2. #include "MatrixTableItemDelegate.h"
  3. #include <QApplication>
  4. #include <QDesktopWidget>
  5. #include <QHeaderView>
  6. #include <QDebug>
  7. #include <qcombobox.h>
  8. #include <SpinBoxDelegate.h>
  9. MatrixTableTechMeasuresWidget::MatrixTableTechMeasuresWidget(QWidget *parent) : QTableView(parent)
  10. {
  11. model = new QStandardItemModel();
  12. this->setModel(model);
  13. paintDone = false;
  14. delegate = new SpinBoxDelegate(this);
  15. this->setItemDelegate(delegate);
  16. connect(delegate, &SpinBoxDelegate::dataChanged, this, &MatrixTableTechMeasuresWidget::itemClicked);
  17. }
  18. MatrixTableTechMeasuresWidget::~MatrixTableTechMeasuresWidget() { }
  19. void MatrixTableTechMeasuresWidget::addRowNode(QString node, QString name, QString remark)
  20. {
  21. matrixRows << MatrixTableTechMeasuresWidget::MatrixNode(node, name, remark);
  22. }
  23. void MatrixTableTechMeasuresWidget::addColNode(QString node, QString name, QString remark)
  24. {
  25. matrixCols << MatrixTableTechMeasuresWidget::MatrixNode(node, name, remark);
  26. }
  27. QList<MatrixDataSource> MatrixTableTechMeasuresWidget::getSource() const
  28. {
  29. return dataSource;
  30. }
  31. void MatrixTableTechMeasuresWidget::setTableName(QString name)
  32. {
  33. m_msg_name = name;
  34. }
  35. void MatrixTableTechMeasuresWidget::paintMatrixTable(QList<NodeMatrixInfo *> nodeValueInfoList)
  36. {
  37. int row = matrixRows.count();
  38. int col = matrixCols.count();
  39. this->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
  40. this->setSelectionMode(QAbstractItemView::SingleSelection);
  41. this->setSelectionBehavior(QAbstractItemView::SelectItems);
  42. model->setHorizontalHeaderItem(0, new QStandardItem(""));
  43. for (int c = 0; c < col; ++c) {
  44. model->setHorizontalHeaderItem(c + 1, new QStandardItem(matrixCols[c].name));
  45. model->horizontalHeaderItem(c + 1)->setToolTip(matrixCols[c].remark);
  46. }
  47. for (int r = 0; r < row; ++r) {
  48. model->setItem(r, 0, new QStandardItem(matrixRows[r].name));
  49. model->item(r, 0)->setToolTip(matrixRows[r].remark);
  50. model->item(r, 0)->setTextAlignment(Qt::AlignCenter);
  51. model->item(r, 0)->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
  52. for (int c = 1; c < col + 1; ++c) {
  53. MatrixDataSource data;
  54. data.row = r;
  55. data.col = c;
  56. data.changed = true;
  57. // data.nodeValue = "";
  58. // qDebug() << matrixRows[r].name << "---" << matrixCols[c - 1].name << "--" << nodeValueInfoList.size();
  59. for (NodeMatrixInfo *nodeMatrixInfo : nodeValueInfoList) {
  60. // qDebug() << nodeMatrixInfo->abscissa << "---" << nodeMatrixInfo->ordinate << "---"
  61. // << nodeMatrixInfo->nodeValue;
  62. if (nodeMatrixInfo->abscissa == matrixRows[r].name
  63. && nodeMatrixInfo->ordinate == matrixCols[c - 1].name) {
  64. data.nodeValue = nodeMatrixInfo->nodeValue;
  65. data.changed = true;
  66. }
  67. }
  68. QModelIndex index = model->index(r, c, QModelIndex());
  69. QVariant initData = 0;
  70. if (data.nodeValue != nullptr && data.nodeValue != "") {
  71. initData = data.nodeValue;
  72. }
  73. model->setData(index, initData);
  74. // model->setItem(r, c, new QStandardItem(data.nodeValue));
  75. model->item(r, c)->setTextAlignment(Qt::AlignCenter);
  76. data.node = matrixCols[c - 1].node;
  77. data.abscissa = matrixRows[r].name;
  78. data.ordinate = matrixCols[c - 1].name;
  79. dataSource << data;
  80. }
  81. }
  82. emit returnModel(model);
  83. emit returnModelName(m_msg_name);
  84. paintDone = true;
  85. }
  86. void MatrixTableTechMeasuresWidget::itemClicked(const QModelIndex &index)
  87. {
  88. // qDebug() << "itemClicked" << index << model->itemFromIndex(index)->text() << endl;
  89. // QStringList l = { "1/9", "1/7", "1/5", "1/3", "1", "3", "5", "7", "9" };
  90. // SchemeBar *scheme =
  91. // new SchemeBar(model->item(index.row(), 0)->text(),
  92. // model->horizontalHeaderItem(index.column())->text(), l);
  93. // scheme->setModal(true);
  94. // scheme->setAttribute(Qt::WA_DeleteOnClose);
  95. // connect(scheme, &SchemeBar::setValue, [=](QString val) { editItemData(index, val); });
  96. // scheme->show();
  97. // QPoint p = QCursor::pos();
  98. // if (p.x() + scheme->width() + 10 >= QApplication::desktop()->width()) {
  99. // p.setX(QApplication::desktop()->width() - 10 - scheme->width());
  100. // }
  101. // scheme->move(p);
  102. editItemData(index, model->itemFromIndex(index)->text());
  103. }
  104. void MatrixTableTechMeasuresWidget::editItemData(const QModelIndex &index, const QString &val)
  105. {
  106. qDebug() << "-----" << index << val;
  107. model->itemFromIndex(index)->setText(val);
  108. QStandardItem *item = model->itemFromIndex(index);
  109. // qDebug() << "dataSource.count()" << dataSource.count();
  110. for (int i = 0; i < dataSource.count(); ++i) {
  111. // qDebug() << item->text();
  112. if (dataSource[i].row == item->row() && dataSource[i].col == item->column()) {
  113. dataSource[i].nodeValue = item->text().trimmed();
  114. if (!(item == NULL) && !item->text().trimmed().isEmpty()) {
  115. dataSource[i].changed = true;
  116. } else {
  117. dataSource[i].changed = false;
  118. }
  119. }
  120. }
  121. // emit dataReady(isDataReady());
  122. }
  123. bool MatrixTableTechMeasuresWidget::isDataReady() const
  124. {
  125. if (dataSource.count() == 0) {
  126. return false;
  127. }
  128. for (MatrixDataSource data : dataSource) {
  129. if (data.changed == false) {
  130. return false;
  131. }
  132. }
  133. return true;
  134. }