Commit 942c8917 authored by Екатерина Спорова's avatar Екатерина Спорова
Browse files

Add new file

parent 6607defe
No related merge requests found
Pipeline #1100 canceled with stages
Showing with 194 additions and 0 deletions
+194 -0
mainwindow.cpp 0 → 100644
#include <QErrorMessage>
#include <QPushButton>
#include <QFont>
#include <QtXmlPatterns>
#include <QContextMenuEvent>
#include <parser.h>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow), current_file(nullptr), current_pos(nullptr)
{
ui->setupUi(this);
tree_widget = new QTreeWidget(this);
tree_widget->setHeaderLabel("Файлы");
tree_widget->setContextMenuPolicy(Qt::CustomContextMenu);
connect(tree_widget, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(slot_custom_menu(const QPoint&)));
menu_custom = new QMenu(this);
menu_custom->addAction("&Сделать действующим", this, SLOT(make_active()));
menu_custom->addAction("&Закрыть", this, SLOT(close_file()));
text_edit = new QTextEdit(this);
QMenu *menu = new QMenu("&Меню", this);
QPushButton *putton_download = new QPushButton("Загрузить Xquery запрос",this);
putton_download->setFont(*(new QFont("Times New Roman", 14, QFont::Bold)));
connect(putton_download, SIGNAL(clicked()), SLOT(open_query()));
QPushButton *putton = new QPushButton("Выполнить XQuery запрос",this);
putton->setFont(*(new QFont("Times New Roman", 14, QFont::Bold)));
connect(putton, SIGNAL(clicked()), SLOT(make_query()));
menu->addAction("&Открыть файл", this, SLOT(open_file()), Qt::CTRL + Qt::Key_O);
menu->addAction("&Закрыть все файлы", this, SLOT(clean_tree()));
menu->addAction("&Загрузить XQuery запрос", this, SLOT(open_query()), Qt::CTRL + Qt::Key_E);
menu->addSeparator();
menu->addAction("&Выход", this, SLOT(close_window()), Qt::CTRL + Qt::Key_Q);
ui->menubar->addMenu(menu);
dock = new QDockWidget("Результат выполнения запроса", this);
this->addDockWidget(Qt::DockWidgetArea::LeftDockWidgetArea, dock);
text_edit_dock = new QTextEdit(this);
text_edit_dock->setReadOnly(true);
dock->setWidget(text_edit_dock);
main_layout = new QGridLayout(this);
main_layout->addWidget(tree_widget, 0, 0, 5, 4);
main_layout->addWidget(text_edit, 5, 0, 5, 4);
main_layout->addWidget(putton, 16, 0, 1, 2);
main_layout->addWidget(putton_download, 16, 2, 1, 2);
main_layout->addWidget(dock, 10, 0, 5, 4);
main_widget = new QWidget(this);
main_widget->setLayout(main_layout);
setCentralWidget(main_widget);
setWindowTitle("XML reader");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::close_window()
{
QApplication::quit();
}
void MainWindow::clean_tree()
{
if(current_file)
{
delete current_file;
current_file = nullptr;
current_pos = nullptr;
}
tree_widget->clear();
}
void MainWindow::make_query()
{
if(current_file->open(QIODevice::ReadOnly))
{
QString query = text_edit->toPlainText();
QXmlQuery xml_query(QXmlQuery::XQuery10);
if(!xml_query.setFocus(current_file))
(new QErrorMessage(this))->showMessage("Выберите таблицу данных!");
xml_query.setQuery(query);
if (!xml_query.isValid())
{
current_file->close();
(new QErrorMessage(this))->showMessage("Ошибочный запрос!");
}
else
{
QString answer;
if (!xml_query.evaluateTo(&answer))
{
qDebug() << answer;
current_file->close();
(new QErrorMessage(this))->showMessage("Ошибка выполнения запроса!");
}
else
{
current_file->close();
text_edit_dock->setText(answer);
dock->show();
}
}
}
}
void MainWindow::open_file()
{
QString filepath = QFileDialog::getOpenFileName(this, "Choose file", "File", "*.xml");
if(!filepath.isEmpty())
{
QFile file(filepath);
QXmlSimpleReader reader;
QString filename = filepath.section("/",-1,-1);
parser handler(tree_widget, filename);
QXmlInputSource source(&file);
reader.setContentHandler(&handler);
if (!reader.parse(source))
(new QErrorMessage(this))->showMessage("Не удаётся открыть файл!");
if(!current_file)
{
current_file = new QFile(filepath);
current_pos = tree_widget->itemAt(0, 0);
current_pos->setFont(0, QFont("Times New Roman", 14, QFont::Bold));
}
}
}
void MainWindow::open_query()
{
QString filepath = QFileDialog::getOpenFileName(this, "Choose query-file", "File", "*.xq");
if (!filepath.isEmpty())
{
QFile file(filepath);
if (file.open(QIODevice::ReadOnly))
{
QString query = file.readAll();
file.close();
text_edit->setText(query);
make_query();
}
else
{
(new QErrorMessage(this))->showMessage("Не удаётся открыть файл!");
}
}
}
void MainWindow::slot_custom_menu(const QPoint& pos)
{
if(tree_widget->currentItem() && !tree_widget->currentItem()->parent())
menu_custom->exec(QCursor::pos());
}
void MainWindow::make_active()
{
QString current_filename = current_file->fileName();
current_filename.resize(current_file->fileName().lastIndexOf('/') + 1);
current_filename += tree_widget->currentItem()->data(0,0).toString();
current_file->setFileName(current_filename);
current_pos->setFont(0, QFont("Times New Roman", 14));
tree_widget->currentItem()->setFont(0, QFont("Times New Roman", 14, QFont::Bold));
current_pos = tree_widget->currentItem();
}
void MainWindow::close_file()
{
QString new_filename = current_file->fileName();
new_filename.resize(current_file->fileName().lastIndexOf('/') + 1);
QModelIndex point;
if(tree_widget->currentIndex().row())
point = tree_widget->model()->index(tree_widget->currentIndex().row() - 1, tree_widget->currentIndex().column());
else
point = tree_widget->model()->index(tree_widget->currentIndex().row() + 1, tree_widget->currentIndex().column());
QString currnetActiveFile(tree_widget->currentItem()->text(0));
delete tree_widget->currentItem();
if(!point.isValid())
{
delete current_file;
current_file = nullptr;
}
else
{
tree_widget->setCurrentIndex(point);
new_filename += tree_widget->currentItem()->data(0,0).toString();
qDebug() << new_filename;
if(current_file->fileName().contains(currnetActiveFile))
{
current_file->setFileName(new_filename);
tree_widget->currentItem()->setFont(0, QFont("Times New Roman", 14, QFont::Bold));
}
current_pos = tree_widget->currentItem();
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment