rootform.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "rootform.h"
  2. #include "ui_rootform.h"
  3. #include "treemodel/treemodel.h"
  4. #include <QFile>
  5. #include <QStyleFactory>
  6. RootForm::RootForm(const QString& iniFilePath, QWidget *parent) :
  7. QWidget(parent),
  8. ui(new Ui::RootForm)
  9. {
  10. ui->setupUi(this);
  11. ui->view->setStyleSheet("QTreeView::item:hover{background-color:rgb(0,0,255,50)}"
  12. "QTreeView::item:selected{background-color:rgb(0,0,255,100)}"
  13. "QTreeView::item{border-right: 1px solid black;border-bottom: 1px solid black;}"
  14. );
  15. TreeModel *model = new TreeModel(iniFilePath);
  16. ui->view->setModel(model);
  17. ui->view->setStyle(QStyleFactory::create("windows"));
  18. for (int column = 0; column < model->columnCount(); ++column)
  19. ui->view->resizeColumnToContents(column);
  20. connect(ui->delSectionBtn, SIGNAL(clicked()), this, SLOT(removeRow()));
  21. connect(ui->delKeyBtn, SIGNAL(clicked()), this, SLOT(removeRow()));
  22. connect(ui->addSectionBtn, SIGNAL(clicked()), this, SLOT(insertRow()));
  23. connect(ui->addKeyBtn, SIGNAL(clicked()), this, SLOT(insertChild()));
  24. connect(ui->view->selectionModel(), &QItemSelectionModel::selectionChanged, this, &RootForm::updateActions);
  25. updateActions();
  26. }
  27. RootForm::RootForm(QWidget *parent) :
  28. QWidget(parent),
  29. ui(new Ui::RootForm)
  30. {
  31. ui->setupUi(this);
  32. //const QStringList headers({tr("Name"), tr("Description")});
  33. //QFile file(":/res/default.txt");
  34. //file.open(QIODevice::ReadOnly);
  35. //TreeModel *model = new TreeModel(headers, file.readAll());
  36. //file.close();
  37. ui->view->setStyleSheet("QTreeView::item:hover{background-color:rgb(0,0,255,50)}"
  38. "QTreeView::item:selected{background-color:rgb(0,0,255,100)}"
  39. "QTreeView::item{border-right: 1px solid black;border-bottom: 1px solid black;}"
  40. );
  41. TreeModel *model = new TreeModel("D:\\Run\\hardwarecfg\\root.ini");
  42. ui->view->setModel(model);
  43. ui->view->setStyle(QStyleFactory::create("windows"));
  44. for (int column = 0; column < model->columnCount(); ++column)
  45. ui->view->resizeColumnToContents(column);
  46. connect(ui->delSectionBtn, SIGNAL(clicked()), this, SLOT(removeRow()));
  47. connect(ui->delKeyBtn, SIGNAL(clicked()), this, SLOT(removeRow()));
  48. connect(ui->addSectionBtn, SIGNAL(clicked()), this, SLOT(insertRow()));
  49. connect(ui->addKeyBtn, SIGNAL(clicked()), this, SLOT(insertChild()));
  50. connect(ui->view->selectionModel(), &QItemSelectionModel::selectionChanged, this, &RootForm::updateActions);
  51. updateActions();
  52. }
  53. RootForm::~RootForm()
  54. {
  55. delete ui;
  56. }
  57. void RootForm::insertChild()
  58. {
  59. const QModelIndex index = ui->view->selectionModel()->currentIndex();
  60. QAbstractItemModel *model = ui->view->model();
  61. TreeModel* treeModel = dynamic_cast<TreeModel*>(model);
  62. Q_ASSERT(treeModel != NULL);
  63. const TreeItemType itemType = treeModel->getItemType(index.row(), index.parent());
  64. if (model->columnCount(index) == 0) {
  65. if (!model->insertColumn(0, index))
  66. return;
  67. }
  68. if (!model->insertRow(0, index))
  69. return;
  70. for (int column = 0; column < model->columnCount(index); ++column) {
  71. const QModelIndex child = model->index(0, column, index);
  72. model->setData(child, QVariant(itemType == Section ? DEFAULT_KEY_VALUE : DEFAULT_UNKNOWN_VALUE), Qt::EditRole);
  73. if (!model->headerData(column, Qt::Horizontal).isValid())
  74. model->setHeaderData(column, Qt::Horizontal, QVariant(tr("[No header]")), Qt::EditRole);
  75. }
  76. ui->view->selectionModel()->setCurrentIndex(model->index(0, 0, index),
  77. QItemSelectionModel::ClearAndSelect);
  78. updateActions();
  79. }
  80. bool RootForm::insertColumn()
  81. {
  82. QAbstractItemModel *model = ui->view->model();
  83. int column = ui->view->selectionModel()->currentIndex().column();
  84. // Insert a column in the parent item.
  85. bool changed = model->insertColumn(column + 1);
  86. if (changed)
  87. model->setHeaderData(column + 1, Qt::Horizontal, QVariant("[No header]"), Qt::EditRole);
  88. updateActions();
  89. return changed;
  90. }
  91. void RootForm::insertRow()
  92. {
  93. const QModelIndex index = ui->view->selectionModel()->currentIndex();
  94. QAbstractItemModel *model = ui->view->model();
  95. TreeModel* treeModel = dynamic_cast<TreeModel*>(model);
  96. Q_ASSERT(treeModel != NULL);
  97. const TreeItemType itemType = treeModel->getItemType(index.row(), index.parent());
  98. if (!model->insertRow(index.row() + 1, index.parent()))
  99. return;
  100. updateActions();
  101. for (int column = 0; column < model->columnCount(index.parent()); ++column) {
  102. const QModelIndex child = model->index(index.row() + 1, column, index.parent());
  103. QVariant value = (itemType == Key ? DEFAULT_KEY_VALUE : DEFAULT_SECTION_VALUE);
  104. if(column == DEFAULT_VALUE_COLUMN && itemType == Section) value = "\t";
  105. model->setData(child, value, Qt::EditRole);
  106. }
  107. }
  108. bool RootForm::removeColumn()
  109. {
  110. QAbstractItemModel *model = ui->view->model();
  111. const int column = ui->view->selectionModel()->currentIndex().column();
  112. // Insert columns in each child of the parent item.
  113. const bool changed = model->removeColumn(column);
  114. if (changed)
  115. updateActions();
  116. return changed;
  117. }
  118. void RootForm::removeRow()
  119. {
  120. const QModelIndex index = ui->view->selectionModel()->currentIndex();
  121. QAbstractItemModel *model = ui->view->model();
  122. if (model->removeRow(index.row(), index.parent()))
  123. updateActions();
  124. }
  125. void RootForm::updateActions()
  126. {
  127. const QModelIndex index = ui->view->selectionModel()->currentIndex();
  128. QAbstractItemModel *model = ui->view->model();
  129. TreeModel* treeModel = dynamic_cast<TreeModel*>(model);
  130. Q_ASSERT(treeModel != NULL);
  131. const TreeItemType itemType = treeModel->getItemType(index.row(), index.parent());
  132. const bool hasSelection = !ui->view->selectionModel()->selection().isEmpty();
  133. ui->delKeyBtn->setEnabled(hasSelection && itemType == Key);
  134. ui->delSectionBtn->setEnabled(hasSelection && itemType == Section);
  135. const bool hasCurrent = ui->view->selectionModel()->currentIndex().isValid();
  136. ui->addKeyBtn->setEnabled(hasCurrent && itemType == Section);
  137. ui->addSectionBtn->setEnabled(hasCurrent && itemType == Section);
  138. if (hasCurrent) {
  139. ui->view->closePersistentEditor(ui->view->selectionModel()->currentIndex());
  140. }
  141. }