diff options
author | Michele Calgaro <[email protected]> | 2023-05-05 20:20:32 +0900 |
---|---|---|
committer | Michele Calgaro <[email protected]> | 2023-05-05 20:20:32 +0900 |
commit | 5fa736197b0b111d7d1eb3d7622499375f3f17f9 (patch) | |
tree | 48d7d5ee9c19f52ae99d38cb743a3e8143013b32 | |
parent | d53969664034f710b5eee06971d3f3e790ac1583 (diff) | |
download | universal-indent-gui-tqt-5fa736197b0b111d7d1eb3d7622499375f3f17f9.tar.gz universal-indent-gui-tqt-5fa736197b0b111d7d1eb3d7622499375f3f17f9.zip |
Added logic to save file and handling encoding
Signed-off-by: Michele Calgaro <[email protected]>
-rw-r--r-- | src/MainWindow.cpp | 247 | ||||
-rw-r--r-- | src/MainWindow.h | 6 | ||||
-rw-r--r-- | src/__TODO/IndentHandler.cpp | 2 |
3 files changed, 118 insertions, 137 deletions
diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 9c7db88..33ec315 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -89,9 +89,9 @@ MainWindow::MainWindow(TQString file2OpenOnStart, TQWidget *parent) : MainWindowBase(parent), m_aboutDialog(nullptr), m_qSciSourceCodeEditor(nullptr), m_qTranslator(nullptr), m_uiGuiTranslator(nullptr), m_textEditLineColumnInfoLabel(nullptr), - m_oldLinesNumber(0), m_encodingsList(), m_encodingActionGroup(nullptr), - m_saveEncodedActionGroup(nullptr), m_highlighterActionGroup(nullptr), - m_documentModified(false), m_previewToggled(true) + m_oldLinesNumber(0), m_openEncodingActions(), m_saveEncodingActions(), + m_encodingActionGroup(nullptr), m_saveEncodedActionGroup(nullptr), + m_highlighterActionGroup(nullptr), m_documentModified(false), m_previewToggled(true) ///- _mainWindowForm(nullptr), _settings(nullptr) ///- m_highlighter(nullptr), _aboutDialogGraphicsView(nullptr), _settingsDialog(nullptr) ///- m_textEditVScrollBar(nullptr), _toolBarWidget(nullptr), _indentHandler(nullptr) @@ -516,9 +516,7 @@ void MainWindow::openSourceFileDialog(const TQString &fileName) TQString fileExtensions = "*.h *.c *.cpp *.*"; // Remove this line when the indenter is available ///-- TQString fileExtensions = tr("Supported by indenter") + " (" + ///-- _indentHandler->getPossibleIndenterFileExtensions() + ");;" + tr("All files") + " (*.*)"; -///-- -///-- //TQString openedSourceFileContent = openFileDialog( tr("Choose source code file"), "./", -///-- // fileExtensions ); + TQString fileToOpen = fileName; if (fileToOpen.isEmpty()) { @@ -558,48 +556,48 @@ void MainWindow::openSourceFileDialog(const TQString &fileName) */ bool MainWindow::saveasSourceFileDialog(TQAction *chosenEncodingAction) { -///-- TQString encoding; -///-- TQString fileExtensions = tr("Supported by indenter") + " (" + -///-- _indentHandler->getPossibleIndenterFileExtensions() + ");;" + tr("All files") + " (*.*)"; -///-- -///-- //TQString openedSourceFileContent = openFileDialog( tr("Choose source code file"), "./", -///-- // fileExtensions ); -///-- TQString fileName = TQFileDialog::getSaveFileName(this, tr( -///-- "Save source code file"), m_currentSourceFile, fileExtensions); -///-- -///-- // Saving has been canceled if the filename is empty -///-- if (fileName.isEmpty()) -///-- { -///-- return false; -///-- } -///-- -///-- m_savedSourceContent = m_qSciSourceCodeEditor->text(); -///-- -///-- m_currentSourceFile = fileName; -///-- TQFile::remove(fileName); -///-- TQFile outSrcFile(fileName); -///-- outSrcFile.open(TQFile::ReadWrite | TQFile::Text); -///-- -///-- // Get current encoding. -///-- if (chosenEncodingAction != NULL) -///-- { -///-- encoding = chosenEncodingAction->text(); -///-- } -///-- else -///-- { -///-- encoding = m_encodingActionGroup->checkedAction()->text(); -///-- } -///-- TQTextStream outSrcStrm(&outSrcFile); -///-- outSrcStrm.setCodec(TQTextCodec::codecForName(encoding.toAscii())); -///-- outSrcStrm << m_savedSourceContent; -///-- outSrcFile.close(); -///-- -///-- TQFileInfo fileInfo(fileName); -///-- m_currentSourceFileExtension = fileInfo.suffix(); -///-- -///-- m_qSciSourceCodeEditor->setModified(false); -///-- m_documentModified = false; -///-- + TQString fileExtensions = "*.h *.c *.cpp *.*"; // Remove this line when the indenter is available +//--- TQString fileExtensions = tr("Supported by indenter") + " (" + +//--- _indentHandler->getPossibleIndenterFileExtensions() + ");;" + tr("All files") + " (*.*)"; + + TQString fileName = TQFileDialog::getSaveFileName(m_currentSourceFile, fileExtensions, this, nullptr, + tr("Save source code file")); + + // Saving has been canceled if the filename is empty + if (fileName.isEmpty()) + { + return false; + } + + m_savedSourceContent = m_qSciSourceCodeEditor->text(); + + m_currentSourceFile = fileName; + TQFile::remove(fileName); + TQFile outSrcFile(fileName); + outSrcFile.open(IO_ReadWrite | IO_Translate); + + // Get current encoding. + TQString encoding; + if (chosenEncodingAction) + { + encoding = chosenEncodingAction->text(); + } + else + { + encoding = m_currentEncoding; + } + TQTextStream outSrcStrm(&outSrcFile); + outSrcStrm.setCodec(TQTextCodec::codecForName(encoding.ascii())); + outSrcStrm << m_savedSourceContent; + outSrcFile.close(); + + TQFileInfo fileInfo(fileName); + m_currentSourceFileExtension = fileInfo.extension(false); + + m_qSciSourceCodeEditor->setModified(false); + m_documentModified = false; + + updateRecentlyOpenedList(); updateWindowTitle(); return true; } @@ -611,51 +609,30 @@ bool MainWindow::saveasSourceFileDialog(TQAction *chosenEncodingAction) */ bool MainWindow::saveSourceFile() { -///-- if (m_currentSourceFile.isEmpty()) -///-- { -///-- return saveasSourceFileDialog(); -///-- } -///-- else -///-- { -///-- TQFile::remove(m_currentSourceFile); -///-- TQFile outSrcFile(m_currentSourceFile); -///-- m_savedSourceContent = m_qSciSourceCodeEditor->text(); -///-- outSrcFile.open(TQFile::ReadWrite | TQFile::Text); -///-- -///-- // Get current encoding. -///-- TQString m_currentEncoding = m_encodingActionGroup->checkedAction()->text(); -///-- TQTextStream outSrcStrm(&outSrcFile); -///-- outSrcStrm.setCodec(TQTextCodec::codecForName(m_currentEncoding.toAscii())); -///-- outSrcStrm << m_savedSourceContent; -///-- outSrcFile.close(); -///-- -///-- m_qSciSourceCodeEditor->setModified(false); -///-- m_documentModified = false; -///-- } + if (m_currentSourceFile.isEmpty()) + { + return saveasSourceFileDialog(); + } + else + { + TQFile::remove(m_currentSourceFile); + TQFile outSrcFile(m_currentSourceFile); + m_savedSourceContent = m_qSciSourceCodeEditor->text(); + outSrcFile.open(IO_ReadWrite | IO_Translate); + + // Get current encoding. + TQTextStream outSrcStrm(&outSrcFile); + outSrcStrm.setCodec(TQTextCodec::codecForName(m_currentEncoding.ascii())); + outSrcStrm << m_savedSourceContent; + outSrcFile.close(); + + m_qSciSourceCodeEditor->setModified(false); + m_documentModified = false; + } + updateWindowTitle(); return true; } -///-- /* -///-- \brief Shows a file open dialog. -///-- -///-- Shows a file open dialog with the title \a dialogHeaderStr starting in the directory \a startPath -///-- and with a file mask defined by \a fileMaskStr. Returns the contents of the file as TQString. -///-- */ -///-- TQString MainWindow::openFileDialog(TQString dialogHeaderStr, TQString startPath, -///-- TQString fileMaskStr) -///-- { -///-- TQString fileContent = ""; -///-- -///-- TQString fileName = TQFileDialog::getOpenFileName(NULL, dialogHeaderStr, startPath, fileMaskStr); -///-- -///-- if (fileName != "") -///-- { -///-- fileContent = loadFile(fileName); -///-- } -///-- -///-- return fileContent; -///-- } - /* \brief Updates the displaying of the source code. @@ -1224,17 +1201,17 @@ void MainWindow::languageChanged(int languageIndex) */ void MainWindow::createEncodingMenu() { - m_encodingsList << "UTF-8" << "UTF-16" << "UTF-16BE" << "UTF-16LE" << - "Apple Roman" << "Big5" << "Big5-HKSCS" << "EUC-JP" << "EUC-KR" << "GB18030-0" << - "IBM 850" << "IBM 866" << "IBM 874" << "ISO 2022-JP" << "ISO 8859-1" << "ISO 8859-13" << - "Iscii-Bng" << "JIS X 0201" << "JIS X 0208" << "KOI8-R" << "KOI8-U" << "MuleLao-1" << + TQStringList encodingsList; + encodingsList << "UTF-8" << "ISO-10646-UCS-2" << "Apple Roman" << "Big5" << "Big5-HKSCS" << "CP 874" << + "EUC-JP" << "EUC-KR" << "GB18030-0" << "IBM 850" << "IBM 866" << "ISO 2022-JP" << + "ISO 8859-1" << "ISO 8859-13" << "Iscii-Bng" << "KOI8-R" << "KOI8-U" << "MuleLao-1" << "ROMAN8" << "Shift-JIS" << "TIS-620" << "TSCII" << "Windows-1250" << "WINSAMI2"; m_encodingActionGroup = new TQActionGroup(this); m_saveEncodedActionGroup = new TQActionGroup(this); // Loop for each available encoding - for(const TQString &encodingName : m_encodingsList) + for(const TQString &encodingName : encodingsList) { // Create actions for the "reopen" menu TQAction *encodingAction = new TQAction(m_encodingActionGroup); @@ -1247,6 +1224,7 @@ void MainWindow::createEncodingMenu() { encodingAction->setOn(true); } + m_openEncodingActions.append(encodingAction); // Create actions for the "save as encoded" menu encodingAction = new TQAction(m_saveEncodedActionGroup); @@ -1254,6 +1232,8 @@ void MainWindow::createEncodingMenu() encodingAction->setStatusTip(tr( "Save the currently opened source code file by using the text encoding scheme ") + encodingName); + encodingAction->setToggleAction(true); + m_saveEncodingActions.append(encodingAction); } m_encodingActionGroup->addTo(popupMenuEncoding); @@ -1273,20 +1253,20 @@ void MainWindow::createEncodingMenu() */ void MainWindow::saveAsOtherEncoding(TQAction *chosenEncodingAction) { -///-- bool fileWasSaved = saveasSourceFileDialog(chosenEncodingAction); -///-- -///-- // If the file was save with another encoding, change the selected encoding in the reopen menu. -///-- if (fileWasSaved) -///-- { -///-- foreach(TQAction * action, m_encodingActionGroup->actions()) -///-- { -///-- if (action->text() == chosenEncodingAction->text()) -///-- { -///-- action->setChecked(true); -///-- return; -///-- } -///-- } -///-- } + bool fileWasSaved = saveasSourceFileDialog(chosenEncodingAction); + + // If the file was save with another encoding, change the selected encoding in the reopen menu. + if (fileWasSaved) + { + for (TQAction *action : m_openEncodingActions) + { + if (action->text() == chosenEncodingAction->text()) + { + action->setOn(true); + return; + } + } + } } /* @@ -1294,30 +1274,31 @@ void MainWindow::saveAsOtherEncoding(TQAction *chosenEncodingAction) */ void MainWindow::encodingChanged(TQAction *encodingAction) { -///-- if (maybeSave()) -///-- { -///-- TQFile inSrcFile(m_currentSourceFile); -///-- TQString fileContent = ""; -///-- -///-- if (!inSrcFile.open(TQFile::ReadOnly | TQFile::Text)) -///-- { -///-- TQMessageBox::warning(NULL, tr("Error opening file"), tr( -///-- "Cannot read the file ") + "\"" + m_currentSourceFile + "\"."); -///-- } -///-- else -///-- { -///-- TQTextStream inSrcStrm(&inSrcFile); -///-- TQApplication::setOverrideCursor(TQt::WaitCursor); -///-- TQString encodingName = encodingAction->text(); -///-- m_currentEncoding = encodingName; -///-- inSrcStrm.setCodec(TQTextCodec::codecForName(encodingName.toAscii())); -///-- fileContent = inSrcStrm.readAll(); -///-- TQApplication::restoreOverrideCursor(); -///-- inSrcFile.close(); -///-- m_qSciSourceCodeEditor->setText(fileContent); -///-- m_qSciSourceCodeEditor->setModified(false); -///-- } -///-- } + m_currentEncoding = encodingAction ? encodingAction->text() : TQString::null; + if (maybeSave()) + { + TQFile inSrcFile(m_currentSourceFile); + TQString fileContent = ""; + + if (!inSrcFile.open(IO_ReadOnly | IO_Translate)) + { + TQMessageBox::warning(NULL, tr("Error opening file"), tr( + "Cannot read the file ") + "\"" + m_currentSourceFile + "\"."); + } + else + { + TQTextStream inSrcStrm(&inSrcFile); + TQApplication::setOverrideCursor(TQt::WaitCursor); + TQString encodingName = encodingAction->text(); + m_currentEncoding = encodingName; + inSrcStrm.setCodec(TQTextCodec::codecForName(encodingName.ascii())); + fileContent = inSrcStrm.read(); + TQApplication::restoreOverrideCursor(); + inSrcFile.close(); + m_qSciSourceCodeEditor->setText(fileContent); + m_qSciSourceCodeEditor->setModified(false); + } + } } /* diff --git a/src/MainWindow.h b/src/MainWindow.h index 4d2fe2f..022f967 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -21,7 +21,9 @@ #define MAINWINDOW_H #include "MainWindowBase.h" + #include "tqobjdefs.h" +#include "tqvaluelist.h" /// class UiGuiSettingsDialog; @@ -79,7 +81,6 @@ class MainWindow : public MainWindowBase private: TQString loadFile(const TQString &filePath); -///-- TQString openFileDialog(TQString dialogHeaderStr, TQString startPath, TQString fileMaskStr); void updateWindowTitle(); void loadLastOpenedFile(); void saveSettings(); @@ -121,6 +122,8 @@ class MainWindow : public MainWindowBase TQActionGroup *m_encodingActionGroup; TQActionGroup *m_saveEncodedActionGroup; TQActionGroup *m_highlighterActionGroup; + TQValueList<TQAction*> m_openEncodingActions; + TQValueList<TQAction*> m_saveEncodingActions; TQTranslator *m_uiGuiTranslator; TQTranslator *m_qTranslator; @@ -128,7 +131,6 @@ class MainWindow : public MainWindowBase ///-- bool _indentSettingsChanged; bool m_previewToggled; bool m_documentModified; - TQStringList m_encodingsList; TQStringList m_recentlyOpenedList; ToolBarWidget *m_toolBarWidget; diff --git a/src/__TODO/IndentHandler.cpp b/src/__TODO/IndentHandler.cpp index 7175221..83c79ed 100644 --- a/src/__TODO/IndentHandler.cpp +++ b/src/__TODO/IndentHandler.cpp @@ -1824,8 +1824,6 @@ void IndentHandler::saveasIndentCfgFileDialog() { TQString fileExtensions = tr("All files") + " (*.*)"; - //TQString openedSourceFileContent = openFileDialog( tr("Choose source code file"), "./", - // fileExtensions ); TQString fileName = TQFileDialog::getSaveFileName(this, tr( "Save indent config file"), getIndenterCfgFile(), fileExtensions); |