123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- #include "rootform.h"
- #include "ui_rootform.h"
- #include "treemodel/treemodel.h"
- #include <QFile>
- #include <QStyleFactory>
- RootForm::RootForm(const QString& iniFilePath, QWidget *parent) :
- QWidget(parent),
- ui(new Ui::RootForm)
- {
- ui->setupUi(this);
- ui->view->setStyleSheet("QTreeView::item:hover{background-color:rgb(0,0,255,50)}"
- "QTreeView::item:selected{background-color:rgb(0,0,255,100)}"
- "QTreeView::item{border-right: 1px solid black;border-bottom: 1px solid black;}"
- );
- TreeModel *model = new TreeModel(iniFilePath);
- ui->view->setModel(model);
- ui->view->setStyle(QStyleFactory::create("windows"));
- for (int column = 0; column < model->columnCount(); ++column)
- ui->view->resizeColumnToContents(column);
- connect(ui->delSectionBtn, SIGNAL(clicked()), this, SLOT(removeRow()));
- connect(ui->delKeyBtn, SIGNAL(clicked()), this, SLOT(removeRow()));
- connect(ui->addSectionBtn, SIGNAL(clicked()), this, SLOT(insertRow()));
- connect(ui->addKeyBtn, SIGNAL(clicked()), this, SLOT(insertChild()));
- connect(ui->view->selectionModel(), &QItemSelectionModel::selectionChanged, this, &RootForm::updateActions);
- updateActions();
- }
- RootForm::RootForm(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::RootForm)
- {
- ui->setupUi(this);
- //const QStringList headers({tr("Name"), tr("Description")});
- //QFile file(":/res/default.txt");
- //file.open(QIODevice::ReadOnly);
- //TreeModel *model = new TreeModel(headers, file.readAll());
- //file.close();
- ui->view->setStyleSheet("QTreeView::item:hover{background-color:rgb(0,0,255,50)}"
- "QTreeView::item:selected{background-color:rgb(0,0,255,100)}"
- "QTreeView::item{border-right: 1px solid black;border-bottom: 1px solid black;}"
- );
- TreeModel *model = new TreeModel("D:\\Run\\hardwarecfg\\root.ini");
- ui->view->setModel(model);
- ui->view->setStyle(QStyleFactory::create("windows"));
- for (int column = 0; column < model->columnCount(); ++column)
- ui->view->resizeColumnToContents(column);
- connect(ui->delSectionBtn, SIGNAL(clicked()), this, SLOT(removeRow()));
- connect(ui->delKeyBtn, SIGNAL(clicked()), this, SLOT(removeRow()));
- connect(ui->addSectionBtn, SIGNAL(clicked()), this, SLOT(insertRow()));
- connect(ui->addKeyBtn, SIGNAL(clicked()), this, SLOT(insertChild()));
- connect(ui->view->selectionModel(), &QItemSelectionModel::selectionChanged, this, &RootForm::updateActions);
- updateActions();
- }
- RootForm::~RootForm()
- {
- delete ui;
- }
- void RootForm::insertChild()
- {
- const QModelIndex index = ui->view->selectionModel()->currentIndex();
- QAbstractItemModel *model = ui->view->model();
- TreeModel* treeModel = dynamic_cast<TreeModel*>(model);
- Q_ASSERT(treeModel != NULL);
- const TreeItemType itemType = treeModel->getItemType(index.row(), index.parent());
- if (model->columnCount(index) == 0) {
- if (!model->insertColumn(0, index))
- return;
- }
- if (!model->insertRow(0, index))
- return;
- for (int column = 0; column < model->columnCount(index); ++column) {
- const QModelIndex child = model->index(0, column, index);
- model->setData(child, QVariant(itemType == Section ? DEFAULT_KEY_VALUE : DEFAULT_UNKNOWN_VALUE), Qt::EditRole);
- if (!model->headerData(column, Qt::Horizontal).isValid())
- model->setHeaderData(column, Qt::Horizontal, QVariant(tr("[No header]")), Qt::EditRole);
- }
- ui->view->selectionModel()->setCurrentIndex(model->index(0, 0, index),
- QItemSelectionModel::ClearAndSelect);
- updateActions();
- }
- bool RootForm::insertColumn()
- {
- QAbstractItemModel *model = ui->view->model();
- int column = ui->view->selectionModel()->currentIndex().column();
- // Insert a column in the parent item.
- bool changed = model->insertColumn(column + 1);
- if (changed)
- model->setHeaderData(column + 1, Qt::Horizontal, QVariant("[No header]"), Qt::EditRole);
- updateActions();
- return changed;
- }
- void RootForm::insertRow()
- {
- const QModelIndex index = ui->view->selectionModel()->currentIndex();
- QAbstractItemModel *model = ui->view->model();
- TreeModel* treeModel = dynamic_cast<TreeModel*>(model);
- Q_ASSERT(treeModel != NULL);
- const TreeItemType itemType = treeModel->getItemType(index.row(), index.parent());
- if (!model->insertRow(index.row() + 1, index.parent()))
- return;
- updateActions();
- for (int column = 0; column < model->columnCount(index.parent()); ++column) {
- const QModelIndex child = model->index(index.row() + 1, column, index.parent());
- QVariant value = (itemType == Key ? DEFAULT_KEY_VALUE : DEFAULT_SECTION_VALUE);
- if(column == DEFAULT_VALUE_COLUMN && itemType == Section) value = "\t";
- model->setData(child, value, Qt::EditRole);
- }
- }
- bool RootForm::removeColumn()
- {
- QAbstractItemModel *model = ui->view->model();
- const int column = ui->view->selectionModel()->currentIndex().column();
- // Insert columns in each child of the parent item.
- const bool changed = model->removeColumn(column);
- if (changed)
- updateActions();
- return changed;
- }
- void RootForm::removeRow()
- {
- const QModelIndex index = ui->view->selectionModel()->currentIndex();
- QAbstractItemModel *model = ui->view->model();
- if (model->removeRow(index.row(), index.parent()))
- updateActions();
- }
- void RootForm::updateActions()
- {
- const QModelIndex index = ui->view->selectionModel()->currentIndex();
- QAbstractItemModel *model = ui->view->model();
- TreeModel* treeModel = dynamic_cast<TreeModel*>(model);
- Q_ASSERT(treeModel != NULL);
- const TreeItemType itemType = treeModel->getItemType(index.row(), index.parent());
- const bool hasSelection = !ui->view->selectionModel()->selection().isEmpty();
- ui->delKeyBtn->setEnabled(hasSelection && itemType == Key);
- ui->delSectionBtn->setEnabled(hasSelection && itemType == Section);
- const bool hasCurrent = ui->view->selectionModel()->currentIndex().isValid();
- ui->addKeyBtn->setEnabled(hasCurrent && itemType == Section);
- ui->addSectionBtn->setEnabled(hasCurrent && itemType == Section);
- if (hasCurrent) {
- ui->view->closePersistentEditor(ui->view->selectionModel()->currentIndex());
- }
- }
|