Skip to content

Commit

Permalink
Merge branch 'main' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
yousefvand authored Dec 2, 2024
2 parents ef4664b + 36e2480 commit f3ae965
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

=======
## 0.0.59

- Default Tab Fixed

## 0.0.58

- View -> Show Symbol -> Show Tabs
Expand Down
39 changes: 38 additions & 1 deletion src/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <QtConcurrent>
#include <QtConcurrent/QtConcurrent>
#include <QFileInfo>
#include "helpers.h"
#include "document.h"
#include "fileloaderworker.h"
#include "codeeditor.h"
Expand Down Expand Up @@ -320,6 +321,7 @@ void Document::saveAcopyAs() {
bool Document::closeDocument() {
qDebug() << "Checking document close: isLoading=" << m_isLoading << ", isSaving=" << m_isSaving;

// FIXME: m_isLoading sometimes is true wrongly
if (m_isLoading || m_isSaving) {
qDebug() << "Document still loading or saving, cannot close.";
return false;
Expand All @@ -335,7 +337,28 @@ bool Document::closeDocument() {
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);

if (reply == QMessageBox::Save) {
saveFile(); // Save the document
if (Helpers::isValidFilePath(m_filePath)) {
saveFile(); // Save the document
} else {
if (m_editor->document()) {
// Show the "Save As" dialog
QString filePath = QFileDialog::getSaveFileName(
this, QObject::tr("Save File As"), "",
QObject::tr("Text Files (*.txt);;All Files (*)"));

if (!filePath.isEmpty()) {
// Save the file and update the tab name
saveFileAs(filePath);
QFileInfo fileInfo(filePath);
QString title = fileInfo.completeBaseName();
setTitle(title);
}
} else {
// Show a warning if no document is available
QMessageBox::warning(this, QObject::tr("Error"), QObject::tr("No document to save."));
}
}

return true; // After saving, allow closing the tab
} else if (reply == QMessageBox::Discard) {
return true; // Discard changes and close the document
Expand All @@ -348,6 +371,20 @@ bool Document::closeDocument() {
return true;
}

void Document::setTitle(const QString &title) {
if (!title.isEmpty()) {
if (auto tabWidget = qobject_cast<QTabWidget *>(parentWidget())) {
int tabIndex = tabWidget->indexOf(this);
if (tabIndex != -1) {
tabWidget->setTabText(tabIndex, title);
qDebug() << "Tab title updated to:" << title;
}
}
} else {
qDebug() << "Empty title provided. No changes made.";
}
}

void Document::goToLineNumberInEditor() {
bool ok;
int lineNumber = QInputDialog::getInt(this, tr("Go to Line"),
Expand Down
1 change: 1 addition & 0 deletions src/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class Document : public QWidget {
int savedCursorPosition() const;
void setSavedCursorPosition(int position);
QThread* workerThread() const;
void setTitle(const QString &title);

signals:
void uiReady();
Expand Down
12 changes: 6 additions & 6 deletions src/helpers.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <QObject>
#include <QAction>
#include <QFileInfo>
#include <QTabWidget>
#include <QInputDialog>
#include <QPlainTextEdit>
Expand Down Expand Up @@ -29,12 +30,6 @@ void Helpers::RemoveMe(QTabWidget* documentsTab) {
}
}

void Helpers::AddDefaultTab(QTabWidget* documentsTab) {
Document* defaultDoc = new Document("Untitled Document", documentsTab);
documentsTab->addTab(defaultDoc, "Untitled Document");
documentsTab->setCurrentWidget(defaultDoc);
}

void Helpers::CloseTab(QTabWidget* documentsTab, int index) {
QWidget* tab = documentsTab->widget(index);
if (tab) {
Expand Down Expand Up @@ -127,6 +122,11 @@ bool Helpers::isValidRegularExpression(const QString& pattern) {
return regex.isValid();
}

bool Helpers::isValidFilePath(const QString& filePath) {
QFileInfo fileInfo(filePath);
return fileInfo.exists() && fileInfo.isFile();
}

int Helpers::countKeywordsInLine(const QString& line, const SearchOptions& options) {
QString keyword = options.keyword;
if (keyword.isEmpty()) {
Expand Down
1 change: 1 addition & 0 deletions src/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Helpers {
static void showInformationMessage(const QString& message);
static void notImplemented(QWidget* parent = nullptr);
static bool isValidRegularExpression(const QString& pattern);
static bool isValidFilePath(const QString& filePath);
static int countKeywordsInLine(const QString &line, const SearchOptions &searchOptions);
static QString highlightKeywords(const QString& line, const SearchOptions& options);

Expand Down
2 changes: 1 addition & 1 deletion src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ MainWindow::MainWindow(QWidget* parent)
formatting->setupActions(ui->actionWindows_Format, ui->action_Unix_OS_X_Format, ui->action_Old_Mac_Format);

Helpers::RemoveMe(ui->documentsTab);
Helpers::AddDefaultTab(ui->documentsTab);
fileOperations->newDocument();
Helpers::zMenu(ui->menu_Language, this);

qDebug() << "MainWindow initialized...";
Expand Down

0 comments on commit f3ae965

Please sign in to comment.