diff --git a/XML-Files-Parser.pro b/XML-Files-Parser.pro index 13e2010..5bcdf18 100644 --- a/XML-Files-Parser.pro +++ b/XML-Files-Parser.pro @@ -5,6 +5,7 @@ INCLUDEPATH += $$quote(E:/FEASU/Fall semester 2023 - on/DS/XML-Files-Parser/inc) # Input HEADERS += inc/tree.h \ inc/Node.h \ + inc/resultwindow.h \ inc/tree.h \ inc/XML_Compressor.h \ inc/XML_Decompressor.h \ @@ -14,6 +15,7 @@ HEADERS += inc/tree.h \ inc/TreeNode.h \ inc/XML_Parser.h SOURCES += src/main.cpp \ + src/resultwindow.cpp \ src/tree.cpp \ src/TreeNode.cpp \ src/XML_Parser.cpp \ diff --git a/XML-Files-Parser.pro.user b/XML-Files-Parser.pro.user index b680397..fb08cf1 100644 --- a/XML-Files-Parser.pro.user +++ b/XML-Files-Parser.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -84,7 +84,7 @@ true - true + false true diff --git a/inc/mainwindow.h b/inc/mainwindow.h index a623fc4..310d7aa 100644 --- a/inc/mainwindow.h +++ b/inc/mainwindow.h @@ -27,6 +27,14 @@ private slots: void on_fix_data_button_clicked(); + void on_locate_button_clicked(); + + void on_json_button_clicked(); + + void on_compress_button_clicked(); + + void on_show_errors_button_clicked(); + private: Ui::MainWindow *ui; diff --git a/inc/resultwindow.h b/inc/resultwindow.h new file mode 100644 index 0000000..8c4844a --- /dev/null +++ b/inc/resultwindow.h @@ -0,0 +1,22 @@ +#ifndef RESULTWINDOW_H +#define RESULTWINDOW_H + +#include +#include +#include +#include + +class ResultWindow : public QDialog +{ + Q_OBJECT + +public: + ResultWindow(QWidget *parent = nullptr); + void setLabelText(const QString &text); + +private: + QLabel *resultLabel; + QScrollArea *scrollArea; +}; + +#endif // RESULTWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index 08930a6..df950df 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -6,150 +6,156 @@ 0 0 - 1091 - 543 + 798 + 515 MainWindow - - - - 200 - 40 - 231 - 181 - - - - Locate - - - - - - 30 - 270 - 991 - 20 - - - - Your file location will be put here. - - - Qt::AlignCenter - - - - - false - - - - 800 - 130 - 181 - 101 - - - - Save to json - - - - - false - - - - 800 - 40 - 181 - 61 - - - - Compress - - - false - - - - - - 40 - 300 - 991 - 20 - - - - - - - Qt::AlignCenter - - - - - true - - - - 600 - 40 - 181 - 61 - - - - Decompress - - - false - - - - - false - - - - 600 - 130 - 181 - 101 - - - - Save fixed - - - false - - + + + + + true + + + + + 0 + 0 + 774 + 491 + + + + + + 50 + 30 + 191 + 171 + + + + Locate + + + + + false + + + + 630 + 20 + 121 + 131 + + + + To json + + + + + false + + + + 630 + 160 + 121 + 29 + + + + Compress + + + + + false + + + + 630 + 200 + 121 + 29 + + + + Decompress + + + + + false + + + + 630 + 240 + 121 + 29 + + + + Save fixed + + + + + + 270 + 40 + 321 + 41 + + + + Your file address will be put here + + + true + + + + + + 270 + 110 + 291 + 20 + + + + + + + + + false + + + + 630 + 280 + 121 + 29 + + + + Show errors + + + + + + - - - - 0 - 0 - 1091 - 25 - - - - - Parser - - - - - diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 6e84dc2..6f831e9 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -4,11 +4,40 @@ #include #include "XML_Parser.h" #include "XML_to_json.h" -#include "XML_Compressor.h" -#include "XML_Decompressor.h" +#include "Huffman_Coding.h" +#include "resultwindow.h" using namespace std; +HuffmanCoding huffman; +string compressedData; + +void saveToCompressedFile(QString extension, string data) { + QString filter = "Binary File (*." + extension + ")"; // create filter from extension + QString filename = QFileDialog::getSaveFileName(0, "Save file", QDir::currentPath(), filter); + + if (!filename.isEmpty()) { + QFile file(filename); + if (file.open(QIODevice::WriteOnly)) { + QDataStream stream(&file); + QByteArray byteArray; + + // Convert string of '0's and '1's to binary + for (int i = 0; i < data.size(); i += 8) { + string byteString = data.substr(i, 8); + char byte = static_cast(bitset<8>(byteString).to_ulong()); + byteArray.append(byte); + } + + stream.writeRawData(byteArray.data(), byteArray.size()); + file.close(); + } else { + // handle error + } + } else { + // handle error + } +} void saveToFile(QString extension, string data) { @@ -36,11 +65,19 @@ string readFromFile(QString filePath) { return ""; } - QTextStream in(&file); - QString data = in.readAll(); + QDataStream in(&file); + QByteArray byteArray = file.readAll(); + string data; + + // Convert binary data to string of '0's and '1's + for (int i = 0; i < byteArray.size(); ++i) { + bitset<8> bits(byteArray[i]); + data += bits.to_string(); + } + file.close(); - return data.toStdString(); + return data; } XML_Parser parseData(QString fileName){ @@ -63,69 +100,93 @@ MainWindow::~MainWindow() delete ui; } -void MainWindow::on_pushButton_clicked() + + +void MainWindow::on_decompress_button_clicked() { + string data =huffman.decompress(compressedData); + QString fileName = ui->address_label->text(); + XML_Parser parser= parseData(fileName); + string formattedData =parser.get_formatted_xml_data(); + ResultWindow *resultWindow = new ResultWindow(this); + resultWindow->setLabelText(QString::fromStdString(formattedData)); + resultWindow->exec(); - QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"), "/home", tr("Files (*.*)")); - XML_Parser parser = parseData(fileName); - if(parser.has_errors()){ - ui->label_2->setText("Has errors"); - ui->fix_data_button->setEnabled(true); - } - else{ - ui->label_2->setText("No errors"); - } - ui->label->setText(fileName); - ui->pushButton_3->setEnabled(true); - ui->pushButton_2->setEnabled(true); +} +void MainWindow::on_fix_data_button_clicked() +{ + QString fileName = ui->address_label->text(); + XML_Parser parser = parseData(fileName); + string data=parser.fix_xml_data(); + saveToFile("xml",data); +} +void MainWindow::on_locate_button_clicked() +{ + ui->fix_data_button->setEnabled(false); + ui->show_errors_button->setEnabled(false); + ui->decompress_button->setEnabled(false); + QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"), "/home", tr("Files (*.xml)")); + XML_Parser parser = parseData(fileName); + if(parser.has_errors()){ + ui->error_label->setText("Has errors"); + ui->fix_data_button->setEnabled(true); + ui->show_errors_button->setEnabled(true); + } + else{ + ui->error_label->setText("No errors"); + } + ui->address_label->setText(fileName); + ui->json_button->setEnabled(true); + ui->compress_button->setEnabled(true); } -void MainWindow::on_pushButton_2_clicked() + + +void MainWindow::on_json_button_clicked() { - // Get the file name from the label - QString fileName = ui->label->text(); + QString fileName = ui->address_label->text(); XML_Parser parser = parseData(fileName); XML_to_json jsonParser(parser); string data = jsonParser.json_string(); saveToFile("json",data); - - // Call your function } -void MainWindow::on_pushButton_3_clicked() + +void MainWindow::on_compress_button_clicked() { - QString fileName = ui->label->text(); + QString fileName = ui->address_label->text(); XML_Parser parser = parseData(fileName); string data = parser.get_raw_xml_data(); - Compressor compressor; - string compressedData = compressor.compress(data); - saveToFile("txt",compressedData); -} + compressedData = huffman.compress(data); + ResultWindow *resultWindow = new ResultWindow(this); + resultWindow->setLabelText(QString::fromStdString(compressedData)); + resultWindow->exec(); -void MainWindow::on_decompress_button_clicked() -{ - QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"), "/home", tr("Files (*.*)")); - string compressedData = readFromFile(fileName); - cout<decompress_button->setEnabled(true); } -void MainWindow::on_fix_data_button_clicked() +void MainWindow::on_show_errors_button_clicked() { - QString fileName = ui->label->text(); + + + QString fileName = ui->address_label->text(); XML_Parser parser = parseData(fileName); - string data=parser.fix_xml_data(); - saveToFile("xml",data); + + //Ysta Htgyb El data bta3tk String w + //To7otha mkan el parser.get_fixed_xml_data() + + + ResultWindow *resultWindow = new ResultWindow(this); + resultWindow->setLabelText(QString::fromStdString( parser.get_fixed_xml_data() )); + resultWindow->exec(); + } diff --git a/src/resultwindow.cpp b/src/resultwindow.cpp new file mode 100644 index 0000000..3c0868a --- /dev/null +++ b/src/resultwindow.cpp @@ -0,0 +1,23 @@ +#include "resultwindow.h" + +ResultWindow::ResultWindow(QWidget *parent) + : QDialog(parent) +{ + QVBoxLayout *layout = new QVBoxLayout(this); + + // Create a scroll area + scrollArea = new QScrollArea(this); + resultLabel = new QLabel(this); + + // Set the label as the widget for the scroll area + scrollArea->setWidget(resultLabel); + scrollArea->setWidgetResizable(true); + + layout->addWidget(scrollArea); + setLayout(layout); +} + +void ResultWindow::setLabelText(const QString &text) +{ + resultLabel->setText(text); +}