-
Notifications
You must be signed in to change notification settings - Fork 6
New file feature #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New file feature #32
Changes from 2 commits
d9a795e
1a85e60
d74913e
0a6c0bf
30adc7e
e145cf9
6eaa797
1a30bde
e231f0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ FileManager::~FileManager() {} | |
|
|
||
| void FileManager::initialize(CodeEditor *editor, MainWindow *mainWindow) | ||
| { | ||
| m_editor = editor; | ||
| m_editor = editor; | ||
| m_mainWindow = mainWindow; | ||
| } | ||
|
|
||
|
|
@@ -37,7 +37,68 @@ void FileManager::setCurrentFileName(const QString fileName) | |
|
|
||
| void FileManager::newFile() | ||
| { | ||
| // Logic to create a new file | ||
| QString m_currentFileName = getCurrentFileName(); | ||
| bool isFileSaved = !m_currentFileName.isEmpty(); | ||
| bool isTextEditorEmpty = this->m_editor->toPlainText().isEmpty(); | ||
chrisdedman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // File has not been saved and the text editor is not empty | ||
| if (!isFileSaved && !isTextEditorEmpty) | ||
| { | ||
| // Create box to prompt user to save changes to file | ||
| QMessageBox promptBox; | ||
| promptBox.setWindowTitle("Save Current File"); | ||
| promptBox.setText("Would you like to save the file?"); | ||
| promptBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel); | ||
| promptBox.setDefaultButton(QMessageBox::Save); | ||
|
|
||
| int option = promptBox.exec(); | ||
| // return if the user hit Cancel button | ||
| if (option == QMessageBox::Cancel) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| saveFile(); | ||
| } | ||
| // File has been previously saved | ||
| else if (isFileSaved) | ||
| { | ||
| // Read from saved file and compare to current file | ||
| QFile file(m_currentFileName); | ||
chrisdedman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) | ||
| return; | ||
| QTextStream in(&file); | ||
|
|
||
| QString savedFileContents = in.readAll(); | ||
chrisdedman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (savedFileContents != this->m_editor->toPlainText().trimmed()) | ||
| { | ||
| // Create box to prompt user to save changes to file | ||
| QMessageBox promptBox; | ||
| promptBox.setWindowTitle("Changes Detected"); | ||
| promptBox.setText("Would you like to save the current changes to the file?"); | ||
| promptBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel); | ||
| promptBox.setDefaultButton(QMessageBox::Save); | ||
| int option = promptBox.exec(); | ||
| // return if the user hit Cancel button | ||
| if (option == QMessageBox::Cancel) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| saveFile(); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| MainWindow *newWindow = new MainWindow(); | ||
| newWindow->setWindowTitle("Code Astra ~ untitled"); | ||
| newWindow->show(); | ||
| } | ||
|
|
||
| // // New window will be created with the untitled name | ||
|
|
||
| // newWindow->~MainWindow(); | ||
|
||
| } | ||
|
|
||
| void FileManager::saveFile() | ||
|
|
@@ -75,7 +136,7 @@ void FileManager::saveFile() | |
| void FileManager::saveFileAs() | ||
| { | ||
| QString fileExtension = getFileExtension(); | ||
| QString filter = "All Files (*);;C++ Files (*.cpp *.h);;Text Files (*.txt)"; | ||
| QString filter = "All Files (*);;C++ Files (*.cpp *.h);;Text Files (*.txt)"; | ||
| if (!fileExtension.isEmpty()) | ||
| { | ||
| filter = QString("%1 Files (*.%2);;%3").arg(fileExtension.toUpper(), fileExtension, filter); | ||
|
|
@@ -235,14 +296,14 @@ OperationResult FileManager::deletePath(const QFileInfo &pathInfo) | |
| } | ||
|
|
||
| std::filesystem::path pathToDelete = pathInfo.absoluteFilePath().toStdString(); | ||
|
|
||
| QString qPathToDelete = QString::fromStdString(pathToDelete.string()); | ||
| // Validate the input path | ||
| if (!isValidPath(pathToDelete)) | ||
| { | ||
| return {false, "ERROR: invalid file path." + pathToDelete.filename().string()}; | ||
| } | ||
|
|
||
| if (!QFile::moveToTrash(pathToDelete)) | ||
| if (!QFile::moveToTrash(qPathToDelete)) | ||
chrisdedman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| return {false, "ERROR: failed to delete: " + pathToDelete.string()}; | ||
| } | ||
|
|
@@ -324,11 +385,11 @@ OperationResult FileManager::duplicatePath(const QFileInfo &pathInfo) | |
| // Validate the input path | ||
| if (!isValidPath(filePath)) | ||
| { | ||
| return {false , "Invalid path."}; | ||
| return {false, "Invalid path."}; | ||
| } | ||
|
|
||
| std::string fileName = filePath.stem().string(); | ||
| std::filesystem::path dupPath = filePath.parent_path() / (fileName + "_copy" + filePath.extension().c_str()); | ||
| std::string fileName = filePath.stem().string(); | ||
| std::filesystem::path dupPath = filePath.parent_path() / (fileName + "_copy" + filePath.extension().c_str()); | ||
chrisdedman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| int counter = 1; | ||
| while (QFileInfo(dupPath).exists()) | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the reason for changing The change will introduce a serious issue. From my research, QCOMPARE_H is not a valid test assertion macro and has no effect in testing. The original implementation with QCOMPARE_EQ is correct as it checks the equality of the menu bar’s action count and provides meaningful test output in case of failure.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment left on the previous conversation on this file. Thanks |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question as the other test file. I am not sure to understand your logic in changing
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for all the feed back Chris! I would like to note that I am working on WSL. When I was compiling the code I kept getting issue with the QCOMPARE_EQ function. Instead, it advised me to use QCOMPARE_H. No real logic behind using it besides the fact it makes my code work on my machine. I would also like to not that QCOMPARE() also works. Not sure if it is a machine independent issue.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hi @franciscomartinez45 . Ok, so I did some research, and your Qt6 version on WSL is probably not updated. Could you check your Qt version by adding a debugging line (maybe at the start of the program in the main function) and show me what you get? qDebug() << QT_VERSION_STR;If your version is greater than 6.6.0, then it would be best to update with |

Uh oh!
There was an error while loading. Please reload this page.