update style functions to work without selection

This commit is contained in:
Giovanni JOSSERAND 2025-06-20 23:26:30 +02:00
parent 2f6b7d44d8
commit a609d4f413

View File

@ -223,13 +223,11 @@ void MainWindow::setBold(){
if (plainTextEdit) {
QTextCursor cursor = plainTextEdit->textCursor();
if (cursor.hasSelection()) {
QTextCharFormat format;
QFont font = plainTextEdit->currentCharFormat().font();
font.setBold(!font.bold());
format.setFont(font);
cursor.mergeCharFormat(format);
}
QTextCharFormat format;
QFont font = cursor.charFormat().font();
font.setBold(!font.bold());
format.setFont(font);
plainTextEdit->mergeCurrentCharFormat(format);
}
}
@ -246,13 +244,11 @@ void MainWindow::setItalic(){
if (plainTextEdit) {
QTextCursor cursor = plainTextEdit->textCursor();
if (cursor.hasSelection()) {
QTextCharFormat format;
QFont font = plainTextEdit->currentCharFormat().font();
font.setItalic(!font.italic());
format.setFont(font);
cursor.mergeCharFormat(format);
}
QTextCharFormat format;
QFont font = cursor.charFormat().font();
font.setItalic(!font.italic());
format.setFont(font);
plainTextEdit->mergeCurrentCharFormat(format);
}
}
@ -268,13 +264,11 @@ void MainWindow::setUnderline(){
if (plainTextEdit) {
QTextCursor cursor = plainTextEdit->textCursor();
if (cursor.hasSelection()) {
QTextCharFormat format;
QFont font = plainTextEdit->currentCharFormat().font();
font.setUnderline(!font.underline());
format.setFont(font);
cursor.mergeCharFormat(format);
}
QTextCharFormat format;
QFont font = cursor.charFormat().font();
font.setUnderline(!font.underline());
format.setFont(font);
plainTextEdit->mergeCurrentCharFormat(format);
}
}
@ -292,12 +286,10 @@ void MainWindow::setColor(){
if (plainTextEdit) {
QColor color = QColorDialog::getColor(Qt::black, this, "Choisir une couleur");
QTextCursor cursor = plainTextEdit->textCursor();
if (cursor.hasSelection()) {
if (color.isValid()) {
QTextCharFormat format;
format.setForeground(color);
cursor.mergeCharFormat(format);
}
if (color.isValid()) {
QTextCharFormat format;
format.setForeground(color);
plainTextEdit->mergeCurrentCharFormat(format);
}
}
}
@ -313,13 +305,11 @@ void MainWindow::setOverline(){
if (plainTextEdit) {
QTextCursor cursor = plainTextEdit->textCursor();
if (cursor.hasSelection()) {
QTextCharFormat format;
QFont font = plainTextEdit->currentCharFormat().font();
font.setOverline(!font.overline());
format.setFont(font);
cursor.mergeCharFormat(format);
}
QTextCharFormat format;
QFont font = cursor.charFormat().font();
font.setOverline(!font.overline());
format.setFont(font);
plainTextEdit->mergeCurrentCharFormat(format);
}
}
void MainWindow::on_actionOverline_triggered()
@ -333,17 +323,15 @@ void MainWindow::setSize(){
if (plainTextEdit) {
QTextCursor cursor = plainTextEdit->textCursor();
if (cursor.hasSelection()) {
bool ok;
int size = QInputDialog::getInt(this, "Taille de la police", "Entrez la taille de la police:", cursor.charFormat().font().pointSize(), 1, 100, 1, &ok);
bool ok;
int size = QInputDialog::getInt(this, "Taille de la police", "Entrez la taille de la police:", cursor.charFormat().font().pointSize(), 1, 100, 1, &ok);
if (ok) {
QTextCharFormat format;
QFont font = cursor.charFormat().font();
font.setPointSize(size);
format.setFont(font);
cursor.mergeCharFormat(format);
}
if (ok) {
QTextCharFormat format;
QFont font = cursor.charFormat().font();
font.setPointSize(size);
format.setFont(font);
plainTextEdit->mergeCurrentCharFormat(format);
}
}
}
@ -358,14 +346,12 @@ void MainWindow::setFont(){
if (plainTextEdit) {
QTextCursor cursor = plainTextEdit->textCursor();
if (cursor.hasSelection()) {
bool ok;
QFont font = QFontDialog::getFont(&ok, cursor.charFormat().font(), this, "Choisir une police");
if (ok) {
QTextCharFormat format;
format.setFont(font);
cursor.mergeCharFormat(format);
}
bool ok;
QFont font = QFontDialog::getFont(&ok, cursor.charFormat().font(), this, "Choisir une police");
if (ok) {
QTextCharFormat format;
format.setFont(font);
plainTextEdit->mergeCurrentCharFormat(format);
}
}
}