From 5e830c5adb984283ced45c8269f5eb67e6f0a873 Mon Sep 17 00:00:00 2001 From: Remisa Yousefvand Date: Sun, 15 Dec 2024 19:04:15 +0330 Subject: [PATCH] v0.0.72 --- CHANGELOG.md | 9 ++ CMakeLists.txt | 13 +++ .../interpretcurrentdocumentasutf16be.cpp | 60 +++++++++++++ .../interpretcurrentdocumentasutf16be.h | 25 ++++++ .../interpretcurrentdocumentasutf16le.cpp | 36 ++++++++ .../interpretcurrentdocumentasutf16le.h | 19 +++++ .../interpretcurrentdocumentasutf8.cpp | 30 +++++++ src/encoding/interpretcurrentdocumentasutf8.h | 23 +++++ ...terpretcurrentdocumentasutf8withoutbom.cpp | 33 ++++++++ ...interpretcurrentdocumentasutf8withoutbom.h | 24 ++++++ src/encoding/interpreteasdialog.cpp | 36 ++++++++ src/encoding/interpreteasdialog.h | 16 ++++ src/encoding/interpreteasdialog.ui | 55 ++++++++++++ src/encoding/interpreteasutf8.cpp | 33 ++++++++ src/encoding/interpreteasutf8.h | 16 ++++ src/mainwindow.cpp | 84 ++++++++++++++++++- src/mainwindow.h | 10 +++ src/mainwindow.ui | 4 +- 18 files changed, 522 insertions(+), 4 deletions(-) create mode 100644 src/encoding/interpretcurrentdocumentasutf16be.cpp create mode 100644 src/encoding/interpretcurrentdocumentasutf16be.h create mode 100644 src/encoding/interpretcurrentdocumentasutf16le.cpp create mode 100644 src/encoding/interpretcurrentdocumentasutf16le.h create mode 100644 src/encoding/interpretcurrentdocumentasutf8.cpp create mode 100644 src/encoding/interpretcurrentdocumentasutf8.h create mode 100644 src/encoding/interpretcurrentdocumentasutf8withoutbom.cpp create mode 100644 src/encoding/interpretcurrentdocumentasutf8withoutbom.h create mode 100644 src/encoding/interpreteasdialog.cpp create mode 100644 src/encoding/interpreteasdialog.h create mode 100644 src/encoding/interpreteasdialog.ui create mode 100644 src/encoding/interpreteasutf8.cpp create mode 100644 src/encoding/interpreteasutf8.h diff --git a/CHANGELOG.md b/CHANGELOG.md index ec28662..381324a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ ============ +## 0.0.72 + +- Implemented: +- Encoding Menu -> Interpret as UTF-8 +- Encoding Menu -> Interpret as UTF-8 without BOM +- Encoding Menu -> Interpret as UTF-16BE (UCS2 Big Endian) +- Encoding Menu -> Interpret as UTF-16LE (UCS2 Little Endian) +- Encoding Menu -> Interpret as ... + ## 0.0.71 - Implemented: diff --git a/CMakeLists.txt b/CMakeLists.txt index 25d1632..a69f3de 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,7 @@ set(PROJECT_UI src/systemreplace/systemreplacedialog.ui src/systemsearchresultdialog.ui src/aboutdialog.ui + src/encoding/interpreteasdialog.ui ) set(CMAKE_AUTOUIC_SEARCH_PATHS src) @@ -117,6 +118,18 @@ set(PROJECT_SOURCES src/view/wordwrap.h src/view/toggletoformertab.cpp src/view/toggletoformertab.h + src/encoding/interpretcurrentdocumentasutf8.cpp + src/encoding/interpretcurrentdocumentasutf8.h + src/encoding/interpretcurrentdocumentasutf8withoutbom.cpp + src/encoding/interpretcurrentdocumentasutf8withoutbom.h + src/encoding/interpretcurrentdocumentasutf16be.cpp + src/encoding/interpretcurrentdocumentasutf16be.h + src/encoding/interpretcurrentdocumentasutf16le.cpp + src/encoding/interpretcurrentdocumentasutf16le.h + src/encoding/interpreteasdialog.cpp + src/encoding/interpreteasdialog.h + src/encoding/interpreteasutf8.cpp + src/encoding/interpreteasutf8.h ${PROJECT_UI} ) diff --git a/src/encoding/interpretcurrentdocumentasutf16be.cpp b/src/encoding/interpretcurrentdocumentasutf16be.cpp new file mode 100644 index 0000000..bfe2fd9 --- /dev/null +++ b/src/encoding/interpretcurrentdocumentasutf16be.cpp @@ -0,0 +1,60 @@ +#include "interpretcurrentdocumentasutf16be.h" +#include "../codeeditor.h" +#include + +InterpretCurrentDocumentAsUTF16BE& InterpretCurrentDocumentAsUTF16BE::instance() +{ + static InterpretCurrentDocumentAsUTF16BE instance; + return instance; +} + +void InterpretCurrentDocumentAsUTF16BE::execute(CodeEditor* editor) +{ + if (!editor) { + qDebug() << "Error: No CodeEditor instance provided."; + return; + } + + // Get the current text from the editor + QString text = editor->toPlainText(); + + // Log the original text + qDebug() << "Original text:" << text; + + // Convert the text to UTF-16BE + QByteArray utf16beData; + const char16_t* utf16Data = reinterpret_cast(text.utf16()); + qsizetype utf16Length = text.size(); + + for (qsizetype i = 0; i < utf16Length; ++i) { + char16_t codeUnit = utf16Data[i]; + + // Convert to big-endian format + utf16beData.append(static_cast((codeUnit >> 8) & 0xFF)); // High byte + utf16beData.append(static_cast(codeUnit & 0xFF)); // Low byte + } + + // Log the UTF-16BE data + qDebug() << "UTF-16BE data:" << utf16beData.toHex(); + + // Correctly interpret UTF-16BE as QString + QByteArray tempData; + for (qsizetype i = 0; i < utf16beData.size(); i += 2) { + // Convert back to native-endian format for QString::fromUtf16 + tempData.append(utf16beData[i + 1]); // Low byte + tempData.append(utf16beData[i]); // High byte + } + + // Interpret the text using native-endian conversion + QString interpretedText = QString::fromUtf16( + reinterpret_cast(tempData.constData()), + tempData.size() / 2); + + // Log the interpreted text + qDebug() << "Interpreted text:" << interpretedText; + + // Update the editor content + editor->setPlainText(interpretedText); + + qDebug() << "Document successfully reinterpreted as UTF-16BE."; +} diff --git a/src/encoding/interpretcurrentdocumentasutf16be.h b/src/encoding/interpretcurrentdocumentasutf16be.h new file mode 100644 index 0000000..cfe8710 --- /dev/null +++ b/src/encoding/interpretcurrentdocumentasutf16be.h @@ -0,0 +1,25 @@ +#pragma once + +#include + +class CodeEditor; // Forward declaration + +class InterpretCurrentDocumentAsUTF16BE +{ +public: + // Singleton instance access + static InterpretCurrentDocumentAsUTF16BE& instance(); + + // Method to interpret the current document as UTF-16BE + void execute(CodeEditor* editor); + +private: + // Private constructor for singleton + InterpretCurrentDocumentAsUTF16BE() = default; + ~InterpretCurrentDocumentAsUTF16BE() = default; + + // Delete copy constructor and assignment operator + InterpretCurrentDocumentAsUTF16BE(const InterpretCurrentDocumentAsUTF16BE&) = delete; + InterpretCurrentDocumentAsUTF16BE& operator=(const InterpretCurrentDocumentAsUTF16BE&) = delete; +}; + diff --git a/src/encoding/interpretcurrentdocumentasutf16le.cpp b/src/encoding/interpretcurrentdocumentasutf16le.cpp new file mode 100644 index 0000000..f64dc49 --- /dev/null +++ b/src/encoding/interpretcurrentdocumentasutf16le.cpp @@ -0,0 +1,36 @@ +#include "interpretcurrentdocumentasutf16le.h" +#include "../codeeditor.h" +#include + +InterpretCurrentDocumentAsUTF16LE& InterpretCurrentDocumentAsUTF16LE::instance() +{ + static InterpretCurrentDocumentAsUTF16LE instance; + return instance; +} + +InterpretCurrentDocumentAsUTF16LE::InterpretCurrentDocumentAsUTF16LE() = default; + +InterpretCurrentDocumentAsUTF16LE::~InterpretCurrentDocumentAsUTF16LE() = default; + +void InterpretCurrentDocumentAsUTF16LE::execute(CodeEditor* editor) +{ + if (!editor) { + qDebug() << "Error: No CodeEditor instance provided."; + return; + } + + // Get the raw content of the document + QByteArray rawData = editor->toPlainText().toUtf8(); // Ensure raw data + qDebug() << "Original Raw Data (Hex):" << rawData.toHex(); + + // Reinterpret as UTF-16LE + QString interpretedText = QString::fromUtf16( + reinterpret_cast(rawData.constData()), rawData.size() / 2); + + qDebug() << "Interpreted UTF-16LE Text:" << interpretedText; + + // Update the editor content + editor->setPlainText(interpretedText); + + qDebug() << "Document reinterpreted as UTF-16LE."; +} diff --git a/src/encoding/interpretcurrentdocumentasutf16le.h b/src/encoding/interpretcurrentdocumentasutf16le.h new file mode 100644 index 0000000..fb08c1f --- /dev/null +++ b/src/encoding/interpretcurrentdocumentasutf16le.h @@ -0,0 +1,19 @@ +#pragma once + +#include + +class CodeEditor; + +class InterpretCurrentDocumentAsUTF16LE +{ +public: + static InterpretCurrentDocumentAsUTF16LE& instance(); + void execute(CodeEditor* editor); + +private: + InterpretCurrentDocumentAsUTF16LE(); // Private constructor + ~InterpretCurrentDocumentAsUTF16LE(); + InterpretCurrentDocumentAsUTF16LE(const InterpretCurrentDocumentAsUTF16LE&) = delete; + InterpretCurrentDocumentAsUTF16LE& operator=(const InterpretCurrentDocumentAsUTF16LE&) = delete; +}; + diff --git a/src/encoding/interpretcurrentdocumentasutf8.cpp b/src/encoding/interpretcurrentdocumentasutf8.cpp new file mode 100644 index 0000000..64c13ae --- /dev/null +++ b/src/encoding/interpretcurrentdocumentasutf8.cpp @@ -0,0 +1,30 @@ +#include "interpretcurrentdocumentasutf8.h" +#include "../codeeditor.h" +#include + +// Singleton instance +InterpretCurrentDocumentAsUTF8& InterpretCurrentDocumentAsUTF8::instance() +{ + static InterpretCurrentDocumentAsUTF8 instance; + return instance; +} + +// Execute the reinterpretation of the current document as UTF-8 +void InterpretCurrentDocumentAsUTF8::execute(CodeEditor* editor) +{ + if (!editor) { + qDebug() << "Error: No CodeEditor instance provided."; + return; + } + + // Retrieve the raw text from the CodeEditor + QByteArray rawData = editor->toPlainText().toUtf8(); + + // Reinterpret the content as UTF-8 + QString utf8Text = QString::fromUtf8(rawData); + + // Update the editor content with the reinterpreted UTF-8 text + editor->setPlainText(utf8Text); + + qDebug() << "Document successfully reinterpreted as UTF-8."; +} diff --git a/src/encoding/interpretcurrentdocumentasutf8.h b/src/encoding/interpretcurrentdocumentasutf8.h new file mode 100644 index 0000000..a7be3b7 --- /dev/null +++ b/src/encoding/interpretcurrentdocumentasutf8.h @@ -0,0 +1,23 @@ +#pragma once + +#include + +class CodeEditor; // Forward declaration + +class InterpretCurrentDocumentAsUTF8 +{ +public: + // Singleton access + static InterpretCurrentDocumentAsUTF8& instance(); + + // Method to reinterpret the content of a CodeEditor as UTF-8 + void execute(CodeEditor* editor); + +private: + InterpretCurrentDocumentAsUTF8() = default; // Private constructor for singleton + ~InterpretCurrentDocumentAsUTF8() = default; // Destructor + InterpretCurrentDocumentAsUTF8(const InterpretCurrentDocumentAsUTF8&) = delete; // No copy + InterpretCurrentDocumentAsUTF8& operator=(const InterpretCurrentDocumentAsUTF8&) = delete; // No assignment +}; + + diff --git a/src/encoding/interpretcurrentdocumentasutf8withoutbom.cpp b/src/encoding/interpretcurrentdocumentasutf8withoutbom.cpp new file mode 100644 index 0000000..89422e6 --- /dev/null +++ b/src/encoding/interpretcurrentdocumentasutf8withoutbom.cpp @@ -0,0 +1,33 @@ +#include "interpretcurrentdocumentasutf8withoutbom.h" +#include "../codeeditor.h" +#include + +InterpretCurrentDocumentAsUTF8WithoutBOM& InterpretCurrentDocumentAsUTF8WithoutBOM::instance() +{ + static InterpretCurrentDocumentAsUTF8WithoutBOM instance; + return instance; +} + +void InterpretCurrentDocumentAsUTF8WithoutBOM::execute(CodeEditor* editor) +{ + if (!editor) { + qDebug() << "Error: No CodeEditor instance provided."; + return; + } + + // Get the current text as raw bytes + QByteArray rawData = editor->toPlainText().toUtf8(); + + // Remove BOM if present + if (rawData.startsWith("\xEF\xBB\xBF")) { + rawData.remove(0, 3); // Remove the first three bytes of BOM + } + + // Convert back to QString + QString utf8Text = QString::fromUtf8(rawData); + + // Update the editor content with the UTF-8 without BOM + editor->setPlainText(utf8Text); + + qDebug() << "Document successfully reinterpreted as UTF-8 without BOM."; +} diff --git a/src/encoding/interpretcurrentdocumentasutf8withoutbom.h b/src/encoding/interpretcurrentdocumentasutf8withoutbom.h new file mode 100644 index 0000000..81706b5 --- /dev/null +++ b/src/encoding/interpretcurrentdocumentasutf8withoutbom.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +class CodeEditor; // Forward declaration + +class InterpretCurrentDocumentAsUTF8WithoutBOM +{ +public: + // Singleton access method + static InterpretCurrentDocumentAsUTF8WithoutBOM& instance(); + + // Method to interpret the current document as UTF-8 without BOM + void execute(CodeEditor* editor); + +private: + // Private constructor for singleton pattern + InterpretCurrentDocumentAsUTF8WithoutBOM() = default; + ~InterpretCurrentDocumentAsUTF8WithoutBOM() = default; + + // Delete copy constructor and assignment operator + InterpretCurrentDocumentAsUTF8WithoutBOM(const InterpretCurrentDocumentAsUTF8WithoutBOM&) = delete; + InterpretCurrentDocumentAsUTF8WithoutBOM& operator=(const InterpretCurrentDocumentAsUTF8WithoutBOM&) = delete; +}; diff --git a/src/encoding/interpreteasdialog.cpp b/src/encoding/interpreteasdialog.cpp new file mode 100644 index 0000000..e4a2f3e --- /dev/null +++ b/src/encoding/interpreteasdialog.cpp @@ -0,0 +1,36 @@ +#include "interpreteasdialog.h" +#include +#include +#include + +InterpreteAsDialog::InterpreteAsDialog(QWidget* parent) + : QDialog(parent) +{ + setWindowTitle("Interpret As"); + + // Create and populate the combobox + comboBox = new QComboBox(this); + comboBox->addItems({ + "UTF-8", + "UTF-7" + }); + + // Create OK and Cancel buttons using QDialogButtonBox + QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); + + // Set layout for the dialog + QVBoxLayout* layout = new QVBoxLayout(this); + layout->addWidget(comboBox); + layout->addWidget(buttonBox); + + // Connect button signals to accept/reject the dialog + connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); + connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); +} + +QString InterpreteAsDialog::getSelectedItem() const +{ + return comboBox->currentText(); +} + + diff --git a/src/encoding/interpreteasdialog.h b/src/encoding/interpreteasdialog.h new file mode 100644 index 0000000..419615b --- /dev/null +++ b/src/encoding/interpreteasdialog.h @@ -0,0 +1,16 @@ +#pragma once + +#include +#include + +class QComboBox; + +class InterpreteAsDialog : public QDialog +{ +public: + explicit InterpreteAsDialog(QWidget* parent = nullptr); + QString getSelectedItem() const; + +private: + QComboBox* comboBox; // Pointer to the combobox +}; diff --git a/src/encoding/interpreteasdialog.ui b/src/encoding/interpreteasdialog.ui new file mode 100644 index 0000000..0303b2d --- /dev/null +++ b/src/encoding/interpreteasdialog.ui @@ -0,0 +1,55 @@ + + + InterpreteAsDialog + + + + 0 + 0 + 476 + 120 + + + + Encoding + + + + + 100 + 24 + 361 + 25 + + + + + + + 290 + 74 + 166 + 25 + + + + QDialogButtonBox::StandardButton::Cancel|QDialogButtonBox::StandardButton::Ok + + + + + + 20 + 20 + 91 + 31 + + + + Interpret As: + + + + + + diff --git a/src/encoding/interpreteasutf8.cpp b/src/encoding/interpreteasutf8.cpp new file mode 100644 index 0000000..3c58740 --- /dev/null +++ b/src/encoding/interpreteasutf8.cpp @@ -0,0 +1,33 @@ +#include "interpreteasutf8.h" +#include "../codeeditor.h" +#include + +InterpreteAsUtf8& InterpreteAsUtf8::instance() +{ + static InterpreteAsUtf8 instance; + return instance; +} + +void InterpreteAsUtf8::execute(CodeEditor* editor) +{ + if (!editor) { + qDebug() << "Error: No CodeEditor instance provided."; + return; + } + + // Retrieve the current document content as raw bytes + QByteArray rawData = editor->toPlainText().toUtf8(); + qDebug() << "Original Raw Data (Hex):" << rawData.toHex(); + + // Interpret the raw data as UTF-8 + QString interpretedText = QString::fromUtf8(rawData); + + if (interpretedText.isEmpty() && !rawData.isEmpty()) { + qDebug() << "Error: Failed to interpret document as UTF-8."; + return; + } + + // Set the interpreted text back into the editor + editor->setPlainText(interpretedText); + qDebug() << "Document interpreted as UTF-8."; +} diff --git a/src/encoding/interpreteasutf8.h b/src/encoding/interpreteasutf8.h new file mode 100644 index 0000000..bedd9b5 --- /dev/null +++ b/src/encoding/interpreteasutf8.h @@ -0,0 +1,16 @@ +#pragma once + +#include + +class CodeEditor; + +class InterpreteAsUtf8 +{ +public: + static InterpreteAsUtf8& instance(); + void execute(CodeEditor* editor); + +private: + InterpreteAsUtf8() = default; // Singleton: private constructor + ~InterpreteAsUtf8() = default; +}; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 66518d0..95b6ea3 100755 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -15,7 +15,6 @@ #include "mainwindow/session.h" #include "src/ui_mainwindow.h" #include "helpers.h" -#include "indentation/indentationdialog.h" #include "find/finddialog.h" #include "replace/replacedialog.h" #include "systemfind/systemfinddialog.h" @@ -25,7 +24,12 @@ #include "view/openinnewwindow.h" #include "view/wordwrap.h" #include "aboutdialog.h" -#include "view/toggletoformertab.h" +#include "encoding/interpretcurrentdocumentasutf8.h" +#include "encoding/interpretcurrentdocumentasutf8withoutbom.h" +#include "encoding/interpretcurrentdocumentasutf16be.h" +#include "encoding/interpretcurrentdocumentasutf16le.h" +#include "encoding/interpreteasdialog.h" +#include "encoding/interpreteasutf8.h" MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), @@ -751,6 +755,82 @@ void MainWindow::on_action_Full_Screen_toggled(bool enabled) ui->action_Full_Screen->setChecked(isFullScreen()); } +/* Encoding */ + +void MainWindow::on_action_Interpret_as_UTF_8_triggered() +{ + CodeEditor* editor = dynamic_cast(ui->documentsTab->currentWidget()); + InterpretCurrentDocumentAsUTF8::instance().execute(editor); +} + +void MainWindow::on_actionInterpret_as_utf_8_without_BOM_triggered() +{ + CodeEditor* editor = dynamic_cast(ui->documentsTab->currentWidget()); + InterpretCurrentDocumentAsUTF8WithoutBOM::instance().execute(editor); +} + +void MainWindow::on_actionInterpret_as_16_BE_triggered() +{ + Document* doc = qobject_cast(ui->documentsTab->currentWidget()); + CodeEditor* editor = doc->editor(); + InterpretCurrentDocumentAsUTF16BE::instance().execute(editor); +} + +void MainWindow::on_actionInterpret_as_16_LE_triggered() +{ + Document* doc = qobject_cast(ui->documentsTab->currentWidget()); + CodeEditor* editor = doc->editor(); + InterpretCurrentDocumentAsUTF16LE::instance().execute(editor); +} + +void MainWindow::on_actionInterpret_As_triggered() +{ + InterpreteAsDialog dialog(this); // Create the CCC with MainWindow as the parent + + if (dialog.exec() == QDialog::Accepted) { + // OK clicked + QString selectedItem = dialog.getSelectedItem(); + qDebug() << "Selected item:" << selectedItem; + Document* doc = qobject_cast(ui->documentsTab->currentWidget()); + CodeEditor* editor = doc->editor(); + if (!editor) { + qFatal("No active editor"); + return; + } + if (selectedItem == "UTF-8") { + InterpreteAsUtf8::instance().execute(editor); + } // TODO: Implement UTF-7 and ... + + } else { + // Cancel clicked + qDebug() << "Dialog canceled."; + } +} + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/mainwindow.h b/src/mainwindow.h index a7ff576..c41e1f5 100755 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -188,6 +188,16 @@ private slots: void on_action_Full_Screen_toggled(bool enabled); + void on_action_Interpret_as_UTF_8_triggered(); + + void on_actionInterpret_as_16_BE_triggered(); + + void on_actionInterpret_as_utf_8_without_BOM_triggered(); + + void on_actionInterpret_as_16_LE_triggered(); + + void on_actionInterpret_As_triggered(); + private: Ui::MainWindow* ui; FileOperations* fileOperations; diff --git a/src/mainwindow.ui b/src/mainwindow.ui index d7c4bc8..092edb5 100755 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -222,7 +222,7 @@ - + @@ -893,7 +893,7 @@ Interpret as UTF-16BE (UCS-2 &Big Endian) - + Interpret as UTF-16LE (UCS-2 &Little Endian)