/
Zero_Fly
/
JSON_viewer
Обзор
Документация
Войти
/
Zero_Fly
/
JSON_viewer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
result
src/json-viewer/win.cpp
81 строка
2 KB
akulyanin
close to finish JSON_tree
22 янв 2025, 15:04
22 янв 2025, 15:04
04f5434
Код
Авторство
О чём код?
#include "win.h" #include "ui_win.h" #include "JSON_tree.h" #include <QFileDialog> #include<QDebug> #include<QFile> Win::Win(QWidget *parent) :QMainWindow(parent) ,ui(new Ui::Win){ ui->setupUi(this); this->highlighter = new JsonSyntaxHighlighter(ui->jsonEdit->document()); connect(ui->openButton, &QPushButton::clicked, this, &Win::openFile); connect(ui->refreshButton, &QPushButton::clicked, this, &Win::refreshTree); connect(ui->expendButton, &QPushButton::clicked, this, &Win::expandAll); } Win::~Win() { delete highlighter; delete ui; } void Win::openFile(){ // Create a file dialog QString filePath = QFileDialog::getOpenFileName(this, "Open File", QDir::homePath(), "Text Files (*.txt);;All Files (*)"); // Check if a file was selected if (!filePath.isEmpty()) { qDebug() << "Selected file:" << filePath; this->readFromFile(filePath); } else { qDebug() << "File doesn't opened or exist."; } } void Win::readFromFile(QString filepath){ QFile file(filepath); if(!file.open(QIODevice::ReadOnly)) { qDebug() << "File can't be read"; return; } QString text = file.readAll(); if(text.isEmpty()){ qDebug() << "File is empty"; return; } qDebug() << text; ui->jsonEdit->setText(text); return refreshTree(); } bool Win::areAllItemsExpanded(QTreeView *treeView) { QAbstractItemModel *model = treeView->model(); if (!model) return false; int rowCount = model->rowCount(QModelIndex()); for (int row = 0; row < rowCount; ++row) { QModelIndex index = model->index(row, 0, QModelIndex()); if (!treeView->isExpanded(index)) { return false; } } return true; } void Win::expandAll(){ if (this->areAllItemsExpanded(ui->jsonView)) ui->jsonView->collapseAll(); else ui->jsonView->expandAll(); } void Win::refreshTree(){ this->jsonTree = new JsonTreeModel(ui->jsonEdit->toPlainText()); ui->jsonView->setModel(this->jsonTree); ui->jsonView->update(); return; }