diff --git a/homework2/mainwindow.cpp b/homework2/mainwindow.cpp index 3a8a7694c8e5f857c715ee33e98f77c017ca914e..0d57235a3544f3147e68e8f116a1f8489ac14052 100644 --- a/homework2/mainwindow.cpp +++ b/homework2/mainwindow.cpp @@ -14,7 +14,7 @@ MainWindow::MainWindow(QWidget *parent) connect(openAction, SIGNAL(triggered()), this, SLOT(openSlot())); menu->addAction(closeAction); - connect(closeAction, SIGNAL(triggered()), this, SLOT(closeSlot())); + connect(closeAction, SIGNAL(triggered()), this, SLOT(closeAllSlot())); menu->addAction(quitAction); connect(quitAction, SIGNAL(triggered()), this, SLOT(quitSlot())); @@ -42,7 +42,7 @@ void MainWindow::openSlot() myTreeView->reset(); } -void MainWindow::closeSlot() +void MainWindow::closeAllSlot() { delete myXmlModel; myXmlModel=new XmlModel(this); @@ -50,6 +50,26 @@ void MainWindow::closeSlot() myTreeView->reset(); } +void MainWindow::closeSlot() +{ + QObject* newRoot = new QObject(this); + bool isActive = false; + + for(QObject* child: myXmlModel->getRoot()->children()) { + if (child == myXmlModel->objectByIndex(myXmlModel->getIndex())) { + child->setParent(newRoot); + } + else if (child->property("font") == true) { + isActive = true; + } + } + if (!isActive) { + myXmlModel->getRoot()->children().at(0)->setProperty("font", true); + } + + myTreeView->reset(); +} + void MainWindow::quitSlot() { close(); @@ -57,11 +77,15 @@ void MainWindow::quitSlot() void MainWindow::contextMenuCall(QPoint pos) { - if (myTreeView->currentIndex().parent() == myTreeView->rootIndex()) { + if (myTreeView->currentIndex().parent() == myTreeView->rootIndex() && myTreeView->indexAt(pos).isValid()) { QMenu *menu=new QMenu(this); QAction *actionActive = new QAction(tr("Сделать активным"), this); - connect(actionActive, &QAction::triggered, myXmlModel, &XmlModel::makeActive); + QAction *actionClose = new QAction(tr("Закрыть"), this); + connect(actionActive, &QAction::triggered, myXmlModel, &XmlModel::makeActive); + connect(actionClose, &QAction::triggered, this, &MainWindow::closeSlot); + myXmlModel->setIndex(myTreeView->indexAt(pos)); menu->addAction(actionActive); - menu->popup(myTreeView->viewport()->mapToGlobal(pos)); + menu->addAction(actionClose); + menu->exec(QCursor::pos()); } } diff --git a/homework2/mainwindow.h b/homework2/mainwindow.h index 9ad77570b653dd8d568e0c6ead44fe3debc3aac2..980eca932baaaef93ccbfad8286cfbd6a149cca2 100644 --- a/homework2/mainwindow.h +++ b/homework2/mainwindow.h @@ -29,6 +29,7 @@ private: private slots: void contextMenuCall(QPoint pos); void openSlot(); + void closeAllSlot(); void closeSlot(); void quitSlot(); }; diff --git a/homework2/xmlmodel.cpp b/homework2/xmlmodel.cpp index cfb7a384f459447ca176e382f587fb3846774756..b1664e5e1dde77854242281eb0b98f23f7973487 100644 --- a/homework2/xmlmodel.cpp +++ b/homework2/xmlmodel.cpp @@ -10,6 +10,16 @@ XmlModel::~XmlModel() delete rootItem; } +QObject* XmlModel::getRoot() const +{ + return rootItem; +} + +QModelIndex XmlModel::getIndex() const +{ + return indexOfObject; +} + QObject* XmlModel::objectByIndex(const QModelIndex &index) const{ if(!index.isValid()) return rootItem; @@ -84,7 +94,7 @@ int XmlModel::columnCount(const QModelIndex &parent) const return 1; } -void XmlModel::getIndex(QModelIndex index) +void XmlModel::setIndex(QModelIndex index) { indexOfObject=index; } diff --git a/homework2/xmlmodel.h b/homework2/xmlmodel.h index 8473b341f83231d7e47fd3139d62af7e73d188e1..733e8d7d5b0a4a0d1778178e57e1c597238b2357 100644 --- a/homework2/xmlmodel.h +++ b/homework2/xmlmodel.h @@ -26,10 +26,12 @@ public: int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; - void getIndex(QModelIndex index); + void setIndex(QModelIndex index); QObject* objectByIndex(const QModelIndex &index) const; friend void addElement(const QDomNode& node,QObject*parent); void addFiles(QStringList filesName); + QObject* getRoot() const; + QModelIndex getIndex() const; private: QObject* rootItem;