treeapp.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "treeapp.h"
  2. #include "ui_treeapp.h"
  3. #include <QString>
  4. #include <QFileDialog>
  5. #include <QMessageBox>
  6. #include <QTextStream>
  7. #include <QStringList>
  8. #include <QFile>
  9. #include <QByteArray>
  10. #include <QDebug>
  11. #include <fstream>
  12. #include "node.h"
  13. #include "treemodel.h"
  14. #include <QRegExp>
  15. #include <memory>
  16. #include <QColor>
  17. TreeApp::TreeApp(QWidget *parent)
  18. : QMainWindow(parent)
  19. , ui(new Ui::TreeApp)
  20. {
  21. ui->setupUi(this);
  22. model = new TreeModel(this);
  23. QStringList cols;
  24. cols<<"objectName"<<"id";
  25. model->setColumns(cols);
  26. ui->treeView->setModel(model);
  27. //ui->treeView->hideColumn(1);
  28. }
  29. TreeApp::~TreeApp()
  30. {
  31. delete ui;
  32. }
  33. void TreeApp::on_actionSelect_a_file_triggered()
  34. {
  35. QString filter = "*.txt";
  36. QString filename = QFileDialog::getOpenFileName(0, "OpenDialog",QDir::homePath(),filter);
  37. QStringList strs;//for strings from a file
  38. if(!filename.isNull())
  39. {
  40. QFile file(filename);
  41. if(!file.open(QFile::ReadOnly | QFile::Text))
  42. {
  43. QMessageBox::warning(this,"Error","File not open");
  44. return;
  45. }
  46. QTextStream in(&file);
  47. while(!in.atEnd ())
  48. {
  49. QString str = in.readLine();
  50. if(!str.isEmpty())
  51. strs.push_back(str);
  52. }
  53. file.close();
  54. }
  55. for (QString& data : strs) //обработка каждой строки считанной из файла
  56. //(TODO - реализовать проверку на соответствие критериям и действия при несоответствии)
  57. {
  58. QRegExp space(":");
  59. QStringList forNode = data.split(space);
  60. QObject * newItem = new Node();
  61. Node* node = static_cast<Node*>(newItem);
  62. //id
  63. unsigned int id = forNode.at(0).toInt();//промежуточная переменная нужна?
  64. newItem->setProperty("id",id);//убрать
  65. node->setId(id);
  66. //pld
  67. QString pldStr = forNode.at(1);//убрать?
  68. node->setPld(pldStr.toInt());
  69. //caption
  70. newItem->setObjectName(forNode.at(2));
  71. //status
  72. bool status = forNode.at(3).toInt();
  73. node->setStatus(status);
  74. //message
  75. node->setMessage(forNode.at(4));
  76. if(node->pld() < 0)
  77. {
  78. model->AddItem(newItem, QModelIndex());
  79. newItem->setParent(model->rootItem());
  80. }
  81. else
  82. {
  83. //QModelIndex ind = model->match(model->index(0,1),Qt::DisplayRole,QVariant(pldStr), -1).first();//переделать
  84. QModelIndex ind = model->IndexById(pldStr);
  85. if(model->objByIndex(ind)->parent() == model->rootItem())
  86. {
  87. model->AddItem(newItem,ind);
  88. }
  89. else
  90. {
  91. //TODO
  92. QString mess = "You tried to make more than 2 levels of nesting. This feature is under development. You can still see the first 2 levels";
  93. QMessageBox::information(this,"Info",mess);
  94. }
  95. }
  96. }
  97. }
  98. void TreeApp::doubleClicked(QModelIndex ind)
  99. {
  100. Node* Nnode = static_cast<Node*>(model->objByIndex(ind));
  101. if(Nnode->status())
  102. QMessageBox::information(this,"Info",Nnode->message());
  103. }