Skip to content

Commit

Permalink
fixed several formating bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Odizinne committed Oct 29, 2024
1 parent 9cf73aa commit 14305e5
Showing 1 changed file with 48 additions and 46 deletions.
94 changes: 48 additions & 46 deletions src/NoteWidget/notewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,32 +518,24 @@ void NoteWidget::updateFormat() {
QTextCharFormat format;
QTextCursor cursor = ui->noteTextEdit->textCursor();

if (ui->boldButton->isChecked()) {
format.setFontWeight(QFont::Bold);
} else {
format.setFontWeight(QFont::Normal);
}
// Clear selection by moving the cursor to a single position
cursor.clearSelection();
ui->noteTextEdit->setTextCursor(cursor);

if (ui->italicButton->isChecked()) {
format.setFontItalic(true);
} else {
format.setFontItalic(false);
}
// Apply bold formatting based on the button's state
format.setFontWeight(ui->boldButton->isChecked() ? QFont::Bold : QFont::Normal);

if (ui->underlineButton->isChecked()) {
format.setFontUnderline(true);
} else {
format.setFontUnderline(false);
}
// Apply italic formatting based on the button's state
format.setFontItalic(ui->italicButton->isChecked());

if (ui->strikethroughButton->isChecked()) {
format.setFontStrikeOut(true);
} else {
format.setFontStrikeOut(false);
}
// Apply underline formatting based on the button's state
format.setFontUnderline(ui->underlineButton->isChecked());

cursor.mergeCharFormat(format);
ui->noteTextEdit->setTextCursor(cursor);
// Apply strikethrough formatting based on the button's state
format.setFontStrikeOut(ui->strikethroughButton->isChecked());

// Set the character format for future text inputs
ui->noteTextEdit->setCurrentCharFormat(format);
}

void NoteWidget::setTextEditButtons() {
Expand Down Expand Up @@ -603,49 +595,59 @@ void NoteWidget::addBulletOnNewLine() {

void NoteWidget::convertToBulletList() {
QTextCursor cursor = ui->noteTextEdit->textCursor();
int cursorPosition = cursor.position();
cursor.beginEditBlock();

QStringList lines = ui->noteTextEdit->toPlainText().split('\n');
// Move the cursor to the start of the document
cursor.movePosition(QTextCursor::Start);

QStringList modifiedLines;
bool hasChanges = false;
while (!cursor.atEnd()) {
cursor.movePosition(QTextCursor::StartOfLine);

for (const QString &line : lines) {
QString trimmedLine = line.trimmed();
if (!trimmedLine.startsWith("")) {
modifiedLines << QString("• %1").arg(trimmedLine);
hasChanges = true;
} else {
modifiedLines << trimmedLine;
// Select the current line and check its content
cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
QString lineText = cursor.selectedText().trimmed();

// Only add bullet if it does not already exist
if (!lineText.startsWith("")) {
cursor.insertText("" + lineText); // Insert bullet without altering formatting
}
}

if (hasChanges) {
ui->noteTextEdit->setPlainText(modifiedLines.join('\n'));
cursor.movePosition(QTextCursor::NextBlock); // Move to the next line
}

cursor.setPosition(qMin(cursorPosition, ui->noteTextEdit->toPlainText().length()));
cursor.endEditBlock();

// Restore cursor to its original position
ui->noteTextEdit->setTextCursor(cursor);

connect(ui->noteTextEdit, &QTextEdit::textChanged, this, &NoteWidget::addBulletOnNewLine);
}


void NoteWidget::revertBulletList() {
QTextCursor cursor = ui->noteTextEdit->textCursor();
int cursorPosition = cursor.position();
cursor.beginEditBlock();

cursor.movePosition(QTextCursor::Start);

while (!cursor.atEnd()) {
cursor.movePosition(QTextCursor::StartOfLine);

QStringList lines = ui->noteTextEdit->toPlainText().split('\n');
QStringList unbulletedLines;
// Select the current line
cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
QString lineText = cursor.selectedText();

// If the line starts with a bullet, remove it
if (lineText.trimmed().startsWith("")) {
lineText = lineText.mid(2).trimmed(); // Remove the bullet symbol
cursor.insertText(lineText); // Re-insert text without bullet
}

for (const QString &line : lines) {
QString unbulletedLine = line.startsWith("") ? line.mid(2).trimmed() : line;
unbulletedLines << unbulletedLine;
cursor.movePosition(QTextCursor::NextBlock); // Move to the next line
}

ui->noteTextEdit->setPlainText(unbulletedLines.join('\n'));
cursor.endEditBlock();

cursor.setPosition(qMin(cursorPosition, ui->noteTextEdit->toPlainText().length()));
// Restore cursor to its original position
ui->noteTextEdit->setTextCursor(cursor);

disconnect(ui->noteTextEdit, &QTextEdit::textChanged, this, &NoteWidget::addBulletOnNewLine);
Expand Down

0 comments on commit 14305e5

Please sign in to comment.