392 lines
12 KiB
C++
Executable File
392 lines
12 KiB
C++
Executable File
#include "lts_text_editor.h"
|
|
|
|
#include "ui_lts_text_editor.h"
|
|
|
|
#include <QColorDialog>
|
|
#include <QTextCursor>
|
|
#include <QApplication>
|
|
#include <QClipboard>
|
|
#include <QTextCharFormat>
|
|
#include <QMimeData>
|
|
#include <QTextList>
|
|
#include <QLineEdit>
|
|
|
|
#include <QDebug>
|
|
|
|
namespace LtsWidgets
|
|
{
|
|
|
|
LtsTextEditor::LtsTextEditor(QWidget *parent)
|
|
: QWidget(parent)
|
|
, m_ui(new Ui::LtsTextEditor)
|
|
{
|
|
m_ui->setupUi(this);
|
|
|
|
connect(m_ui->textEdit, &QTextEdit::currentCharFormatChanged,
|
|
this, &LtsTextEditor::slotCurrentCharFormatChanged);
|
|
connect(m_ui->textEdit, &QTextEdit::cursorPositionChanged,
|
|
this, &LtsTextEditor::slotAlignmentChanged);
|
|
|
|
connect(m_ui->textEdit->document(), &QTextDocument::undoAvailable,
|
|
m_ui->pbUndo, &QToolButton::setEnabled);
|
|
connect(m_ui->textEdit->document(), &QTextDocument::redoAvailable,
|
|
m_ui->pbRedo, &QToolButton::setEnabled);
|
|
|
|
m_ui->pbUndo->setEnabled(m_ui->textEdit->document()->isUndoAvailable());
|
|
m_ui->pbRedo->setEnabled(m_ui->textEdit->document()->isRedoAvailable());
|
|
|
|
connect(QApplication::clipboard(), &QClipboard::dataChanged,
|
|
this, &LtsTextEditor::slotClipboardDataChanged);
|
|
|
|
|
|
connect(m_ui->pbCut, &QToolButton::clicked,
|
|
m_ui->textEdit, &QTextEdit::cut);
|
|
connect(m_ui->pbCopy, &QToolButton::clicked,
|
|
m_ui->textEdit, &QTextEdit::copy);
|
|
connect(m_ui->pbPaste, &QToolButton::clicked,
|
|
m_ui->textEdit, &QTextEdit::paste);
|
|
|
|
connect(m_ui->pbUndo, &QToolButton::clicked,
|
|
m_ui->textEdit, &QTextEdit::undo);
|
|
connect(m_ui->pbRedo, &QToolButton::clicked,
|
|
m_ui->textEdit, &QTextEdit::redo);
|
|
|
|
connect(m_ui->pbBold, &QToolButton::clicked,
|
|
this, &LtsTextEditor::slotTextBold);
|
|
connect(m_ui->pbItalic, &QToolButton::clicked,
|
|
this, &LtsTextEditor::slotTextItalic);
|
|
connect(m_ui->pbUnderline, &QToolButton::clicked,
|
|
this, &LtsTextEditor::slotTextUnderline);
|
|
|
|
connect(m_ui->pbTextColor, &QToolButton::clicked,
|
|
this, &LtsTextEditor::slotTextColor);
|
|
connect(m_ui->pbBackgroundColor, &QToolButton::clicked,
|
|
this, &LtsTextEditor::slotBackgroundColor);
|
|
|
|
connect(m_ui->pbAlignLeft, &QToolButton::toggled,
|
|
this, &LtsTextEditor::slotTextAlign);
|
|
connect(m_ui->pbAlignCenter, &QToolButton::toggled,
|
|
this, &LtsTextEditor::slotTextAlign);
|
|
connect(m_ui->pbAlignRight, &QToolButton::toggled,
|
|
this, &LtsTextEditor::slotTextAlign);
|
|
connect(m_ui->pbAlignJustify, &QToolButton::toggled,
|
|
this, &LtsTextEditor::slotTextAlign);
|
|
|
|
|
|
QFont textFont(QStringLiteral("Sans Serif"));
|
|
textFont.setStyleHint(QFont::SansSerif);
|
|
m_ui->textEdit->setFont(textFont);
|
|
slotFontChanged(m_ui->textEdit->font());
|
|
slotTextColorChanged(m_ui->textEdit->textColor());
|
|
slotAlignmentChanged();
|
|
|
|
slotBackgroundColorChanged(m_backgroundColor);
|
|
|
|
|
|
connect(m_ui->cbFonts, QOverload<const QString &>::of(&QComboBox::activated),
|
|
this, &LtsTextEditor::slotTextFamily);
|
|
|
|
const QList<int> standardSizes = QFontDatabase::standardSizes();
|
|
for (int size : standardSizes)
|
|
m_ui->cbFontSize->addItem(QString::number(size));
|
|
m_ui->cbFontSize->setCurrentIndex(standardSizes.indexOf(QApplication::font().pointSize()));
|
|
|
|
connect(m_ui->cbFontSize, QOverload<const QString &>::of(&QComboBox::activated),
|
|
this, &LtsTextEditor::slotTextSize);
|
|
|
|
connect(m_ui->cbFontSize->lineEdit(), &QLineEdit::editingFinished,
|
|
this, &LtsTextEditor::slotTextSizeEditingFinished);
|
|
|
|
m_ui->textEdit->setFocus();
|
|
}
|
|
|
|
|
|
LtsTextEditor::~LtsTextEditor()
|
|
{
|
|
delete m_ui;
|
|
}
|
|
|
|
|
|
QTextEdit *LtsTextEditor::textEdit()
|
|
{
|
|
return m_ui->textEdit;
|
|
}
|
|
|
|
QColor LtsTextEditor::backgroundColor() const
|
|
{
|
|
return m_backgroundColor;
|
|
}
|
|
|
|
void LtsTextEditor::enableBackgroundColor(bool onOff)
|
|
{
|
|
m_ui->pbBackgroundColor->setVisible(onOff);
|
|
}
|
|
|
|
void LtsTextEditor::setInitialBackgroundColor(const QColor &bgColor)
|
|
{
|
|
if (bgColor.isValid())
|
|
m_backgroundColor = bgColor;
|
|
slotBackgroundColorChanged(m_backgroundColor);
|
|
}
|
|
|
|
|
|
void LtsTextEditor::slotTextBold()
|
|
{
|
|
QTextCharFormat fmt;
|
|
fmt.setFontItalic(m_ui->pbItalic->isChecked());
|
|
fmt.setFontUnderline(m_ui->pbUnderline->isChecked());
|
|
fmt.setFontWeight(m_ui->pbBold->isChecked() ? QFont::Bold : QFont::Normal);
|
|
if (m_ui->textEdit->document()->isEmpty())
|
|
{
|
|
const QString &f = m_ui->cbFonts->currentText();
|
|
fmt.setFontFamily(f);
|
|
fmt.setFontFamilies({f});
|
|
fmt.setForeground(m_foregroundColor);
|
|
const qreal pointSize = (m_ui->cbFontSize->currentText().toFloat() > 0 ? m_ui->cbFontSize->currentText().toFloat() : 11);
|
|
fmt.setFontPointSize(pointSize);
|
|
}
|
|
slotMergeFormatOnWordOrSelection(fmt);
|
|
}
|
|
|
|
|
|
void LtsTextEditor::slotTextUnderline()
|
|
{
|
|
QTextCharFormat fmt;
|
|
fmt.setFontItalic(m_ui->pbItalic->isChecked());
|
|
fmt.setFontUnderline(m_ui->pbUnderline->isChecked());
|
|
fmt.setFontWeight(m_ui->pbBold->isChecked() ? QFont::Bold : QFont::Normal);
|
|
if (m_ui->textEdit->document()->isEmpty())
|
|
{
|
|
const QString &f = m_ui->cbFonts->currentText();
|
|
fmt.setFontFamily(f);
|
|
fmt.setFontFamilies({f});
|
|
fmt.setForeground(m_foregroundColor);
|
|
const qreal pointSize = (m_ui->cbFontSize->currentText().toFloat() > 0 ? m_ui->cbFontSize->currentText().toFloat() : 11);
|
|
fmt.setFontPointSize(pointSize);
|
|
}
|
|
slotMergeFormatOnWordOrSelection(fmt);
|
|
}
|
|
|
|
|
|
void LtsTextEditor::slotTextItalic()
|
|
{
|
|
QTextCharFormat fmt;
|
|
fmt.setFontItalic(m_ui->pbItalic->isChecked());
|
|
fmt.setFontUnderline(m_ui->pbUnderline->isChecked());
|
|
fmt.setFontWeight(m_ui->pbBold->isChecked() ? QFont::Bold : QFont::Normal);
|
|
if (m_ui->textEdit->document()->isEmpty())
|
|
{
|
|
const QString &f = m_ui->cbFonts->currentText();
|
|
fmt.setFontFamily(f);
|
|
fmt.setFontFamilies({f});
|
|
fmt.setForeground(m_foregroundColor);
|
|
const qreal pointSize = (m_ui->cbFontSize->currentText().toFloat() > 0 ? m_ui->cbFontSize->currentText().toFloat() : 11);
|
|
fmt.setFontPointSize(pointSize);
|
|
}
|
|
slotMergeFormatOnWordOrSelection(fmt);
|
|
}
|
|
|
|
|
|
void LtsTextEditor::slotTextFamily(const QString &f)
|
|
{
|
|
QTextCharFormat fmt;
|
|
if (m_ui->textEdit->document()->isEmpty())
|
|
{
|
|
fmt.setFontItalic(m_ui->pbItalic->isChecked());
|
|
fmt.setFontUnderline(m_ui->pbUnderline->isChecked());
|
|
fmt.setFontWeight(m_ui->pbBold->isChecked() ? QFont::Bold : QFont::Normal);
|
|
|
|
const qreal pointSize = (m_ui->cbFontSize->currentText().toFloat() > 0 ? m_ui->cbFontSize->currentText().toFloat() : 11);
|
|
fmt.setFontPointSize(pointSize);
|
|
fmt.setForeground(m_foregroundColor);
|
|
}
|
|
fmt.setFontFamily(f);
|
|
fmt.setFontFamilies({f});
|
|
slotMergeFormatOnWordOrSelection(fmt);
|
|
}
|
|
|
|
|
|
void LtsTextEditor::slotTextSize(const QString &p)
|
|
{
|
|
qreal pointSize = p.toFloat();
|
|
|
|
if (p.toFloat() > 0)
|
|
{
|
|
QSignalBlocker signalBlocker(m_ui->cbFontSize->lineEdit());
|
|
m_ui->cbFontSize->clearFocus();
|
|
|
|
QTextCharFormat fmt;
|
|
if (m_ui->textEdit->document()->isEmpty())
|
|
{
|
|
const QString &f = m_ui->cbFonts->currentText();
|
|
fmt.setFontFamily(f);
|
|
fmt.setFontFamilies({f});
|
|
fmt.setForeground(m_foregroundColor);
|
|
|
|
fmt.setFontItalic(m_ui->pbItalic->isChecked());
|
|
fmt.setFontUnderline(m_ui->pbUnderline->isChecked());
|
|
fmt.setFontWeight(m_ui->pbBold->isChecked() ? QFont::Bold : QFont::Normal);
|
|
|
|
}
|
|
fmt.setFontPointSize(pointSize);
|
|
slotMergeFormatOnWordOrSelection(fmt);
|
|
}
|
|
}
|
|
|
|
void LtsTextEditor::slotTextSizeEditingFinished()
|
|
{
|
|
qreal pointSize = m_ui->cbFontSize->currentText().toFloat();
|
|
|
|
if (pointSize > 0)
|
|
{
|
|
QTextCharFormat textCharFormat;
|
|
|
|
textCharFormat.setFontPointSize(pointSize);
|
|
|
|
QTextCursor cursor = m_ui->textEdit->textCursor();
|
|
|
|
if (cursor.charFormat().font().pointSizeF() == pointSize)
|
|
return;
|
|
|
|
if (!cursor.hasSelection())
|
|
cursor.select(QTextCursor::WordUnderCursor);
|
|
|
|
cursor.mergeCharFormat(textCharFormat);
|
|
}
|
|
}
|
|
|
|
void LtsTextEditor::slotTextColor()
|
|
{
|
|
QColor col = QColorDialog::getColor(m_ui->textEdit->textColor(), this);
|
|
if (!col.isValid())
|
|
return;
|
|
|
|
QTextCharFormat fmt;
|
|
if (m_ui->textEdit->document()->isEmpty())
|
|
{
|
|
fmt.setFontItalic(m_ui->pbItalic->isChecked());
|
|
fmt.setFontUnderline(m_ui->pbUnderline->isChecked());
|
|
fmt.setFontWeight(m_ui->pbBold->isChecked() ? QFont::Bold : QFont::Normal);
|
|
|
|
const QString &f = m_ui->cbFonts->currentText();
|
|
fmt.setFontFamily(f);
|
|
fmt.setFontFamilies({f});
|
|
const qreal pointSize = (m_ui->cbFontSize->currentText().toFloat() > 0 ? m_ui->cbFontSize->currentText().toFloat() : 11);
|
|
fmt.setFontPointSize(pointSize);
|
|
}
|
|
fmt.setForeground(col);
|
|
slotMergeFormatOnWordOrSelection(fmt);
|
|
slotTextColorChanged(col);
|
|
}
|
|
|
|
void LtsTextEditor::slotBackgroundColor()
|
|
{
|
|
QColor col = QColorDialog::getColor(m_backgroundColor.isValid() ? m_backgroundColor : Qt::white, this);
|
|
if (!col.isValid())
|
|
return;
|
|
|
|
m_backgroundColor = col;
|
|
slotBackgroundColorChanged(m_backgroundColor);
|
|
emit backgroundColorChanged(m_backgroundColor);
|
|
}
|
|
|
|
|
|
void LtsTextEditor::slotCurrentCharFormatChanged(const QTextCharFormat &format)
|
|
{
|
|
slotFontChanged(format.font());
|
|
slotTextColorChanged(format.foreground().color());
|
|
}
|
|
|
|
|
|
void LtsTextEditor::slotFontChanged(const QFont &f)
|
|
{
|
|
m_ui->cbFonts->setCurrentIndex(m_ui->cbFonts->findText(f.family()));
|
|
|
|
QSignalBlocker comboBoxsignalBlocker(m_ui->cbFontSize);
|
|
QSignalBlocker lineEditSignalBlocker(m_ui->cbFontSize->lineEdit());
|
|
QString fontSizeString = QString::number(f.pointSize());
|
|
int32_t fontSizeIndex = m_ui->cbFontSize->findText(fontSizeString);
|
|
|
|
if (fontSizeIndex >= 0)
|
|
m_ui->cbFontSize->setCurrentIndex(fontSizeIndex);
|
|
else if (m_ui->cbFontSize->currentText() != fontSizeString)
|
|
m_ui->cbFontSize->setCurrentText(fontSizeString);
|
|
|
|
m_ui->pbBold->setChecked(f.bold());
|
|
m_ui->pbItalic->setChecked(f.italic());
|
|
m_ui->pbUnderline->setChecked(f.underline());
|
|
}
|
|
|
|
|
|
void LtsTextEditor::slotMergeFormatOnWordOrSelection(const QTextCharFormat &format)
|
|
{
|
|
QTextCursor cursor = m_ui->textEdit->textCursor();
|
|
if (!cursor.hasSelection())
|
|
cursor.select(QTextCursor::WordUnderCursor);
|
|
|
|
cursor.mergeCharFormat(format);
|
|
//[GUEZE-3151] Voreinstellung vom Text werden wenn text editor leer ist nicht greifen
|
|
// Mögliche Grund: je nach position des cursors auf dem Text wird die tools dynamisch eingestellt.
|
|
if (m_ui->textEdit->document()->isEmpty())
|
|
m_ui->textEdit->setTextCursor(cursor);
|
|
}
|
|
|
|
|
|
void LtsTextEditor::slotTextColorChanged(const QColor &c)
|
|
{
|
|
QPixmap pix(32, 32);
|
|
pix.fill(c);
|
|
m_ui->pbTextColor->setIcon(pix);
|
|
m_foregroundColor = c;
|
|
}
|
|
|
|
|
|
void LtsTextEditor::slotBackgroundColorChanged(const QColor &bgColor)
|
|
{
|
|
QPixmap pix(32, 32);
|
|
pix.fill(bgColor);
|
|
m_ui->pbBackgroundColor->setIcon(pix);
|
|
}
|
|
|
|
|
|
void LtsTextEditor::slotTextAlign()
|
|
{
|
|
QToolButton *b = qobject_cast<QToolButton *>(sender());
|
|
|
|
if (b == nullptr)
|
|
return;
|
|
|
|
if (b == m_ui->pbAlignLeft)
|
|
m_ui->textEdit->setAlignment(Qt::AlignLeft | Qt::AlignAbsolute);
|
|
else if (b == m_ui->pbAlignCenter)
|
|
m_ui->textEdit->setAlignment(Qt::AlignHCenter);
|
|
else if (b == m_ui->pbAlignRight)
|
|
m_ui->textEdit->setAlignment(Qt::AlignRight | Qt::AlignAbsolute);
|
|
else if (b == m_ui->pbAlignJustify)
|
|
m_ui->textEdit->setAlignment(Qt::AlignJustify);
|
|
}
|
|
|
|
|
|
void LtsTextEditor::slotAlignmentChanged()
|
|
{
|
|
auto a = m_ui->textEdit->alignment();
|
|
|
|
if (a & Qt::AlignLeft)
|
|
m_ui->pbAlignLeft->setChecked(true);
|
|
if (a & Qt::AlignHCenter)
|
|
m_ui->pbAlignCenter->setChecked(true);
|
|
if (a & Qt::AlignRight)
|
|
m_ui->pbAlignRight->setChecked(true);
|
|
if (a & Qt::AlignJustify)
|
|
m_ui->pbAlignJustify->setChecked(true);
|
|
}
|
|
|
|
|
|
void LtsTextEditor::slotClipboardDataChanged()
|
|
{
|
|
if (const QMimeData *md = QApplication::clipboard()->mimeData())
|
|
m_ui->pbPaste->setEnabled(md->hasText());
|
|
}
|
|
|
|
} // namespace LtsWidgets
|