From 727f7ac57a307f80f9ba990e2d66bb59b77fff4b Mon Sep 17 00:00:00 2001 From: Ekaterina Chernova Date: Tue, 18 Jan 2022 07:13:24 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B7=D0=B0=D0=BC=D0=B5=D1=87=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D0=B9=20+=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BD=D0=B0=20=D0=B7=D0=B0=D1=89=D0=B8=D1=82=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- homework2/mainwindow.cpp | 34 +++++++++++++++++++++++++++++----- homework2/mainwindow.h | 1 + homework2/xmlmodel.cpp | 12 +++++++++++- homework2/xmlmodel.h | 4 +++- 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/homework2/mainwindow.cpp b/homework2/mainwindow.cpp index 3a8a769..0d57235 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 9ad7757..980eca9 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 cfb7a38..b1664e5 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 8473b34..733e8d7 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; -- GitLab