Files
qt6-migration-pipeline/Lts/Common/LtsWidgets/lts_text_edit_dialog.cpp
T

138 lines
3.3 KiB
C++
Executable File

#include "lts_text_edit_dialog.h"
#include "lts_text_editor.h"
#include <QHBoxLayout>
#if 1
#define USE_TEXT_EDITOR_2
#else
#define USE_TEXT_EDITOR
#endif
namespace LtsWidgets
{
LtsTextEditDialog::LtsTextEditDialog(QWidget *parent)
: LtsDialog(parent, ButtonCombination::OkApplyCancel)
#ifdef USE_TEXT_EDITOR
, m_textEditor(new LtsTextEditor(this))
#else
, m_textEditor2(new LtsTextEditor2(this))
#endif
{
QHBoxLayout *hbox = new QHBoxLayout(this);
#ifdef USE_TEXT_EDITOR
hbox->addWidget(m_textEditor);
connect(m_textEditor->textEdit(), &QTextEdit::textChanged, this, [this]() {
emit signalTextChanged(this->getHtmlText());
});
connect(m_textEditor, &LtsTextEditor::backgroundColorChanged,
this, &LtsTextEditDialog::onBackgroundColorChanged);
#else
hbox->addWidget(m_textEditor2);
connect(m_textEditor2->textEdit(), &QTextEdit::textChanged, this, [this]() {
emit signalTextChanged(this->getHtmlText());
});
connect(m_textEditor2, &LtsTextEditor2::backgroundColorChanged,
this, &LtsTextEditDialog::onBackgroundColorChanged);
#endif
enableBackgroundColor(false);
}
LtsTextEditDialog::~LtsTextEditDialog()
{}
void LtsTextEditDialog::enableBackgroundColor(bool onOff)
{
#ifdef USE_TEXT_EDITOR
m_textEditor->enableBackgroundColor(onOff);
#else
m_textEditor2->enableBackgroundColor(onOff);
#endif
}
void LtsTextEditDialog::retranslate()
{
QString name = tr("Edit Text");
translate(name);
}
bool LtsTextEditDialog::apply()
{
#ifdef USE_TEXT_EDITOR
QString textHtml = m_textEditor->textEdit()->document()->toHtml();
const QString backGroundColor = QLatin1String("background-color:'%1'; ").arg(m_textEditor->backgroundColor().name());
#else
QString textHtml = m_textEditor2->textEdit()->document()->toHtml();
const QString backGroundColor = QLatin1String("background-color:'%1'; ").arg(m_textEditor2->backgroundColor().name());
#endif
emit signalTextApplied(textHtml);
const int insertBackGroundColorIndex = textHtml.indexOf(QLatin1String("font-family"));
textHtml.insert(insertBackGroundColorIndex, backGroundColor);
emit signalTextAppliedWithBackGroundColor(textHtml);
return true;
}
void LtsTextEditDialog::setHtml(const QString &htmlText)
{
#ifdef USE_TEXT_EDITOR
m_textEditor->textEdit()->document()->setHtml(htmlText);
#else
m_textEditor2->textEdit()->document()->setHtml(htmlText);
#endif
}
QString LtsTextEditDialog::getHtmlText() const
{
#ifdef USE_TEXT_EDITOR
if(m_textEditor && m_textEditor->textEdit() && m_textEditor->textEdit()->document())
return m_textEditor->textEdit()->document()->toHtml();
#else
if(m_textEditor2 && m_textEditor2->textEdit() && m_textEditor2->textEdit()->document())
return m_textEditor2->textEdit()->document()->toHtml();
#endif
return {};
}
void LtsTextEditDialog::setInitialBackgroundColor(const QColor &bgColor)
{
#ifdef USE_TEXT_EDITOR
if (m_textEditor)
m_textEditor->setInitialBackgroundColor(bgColor);
#else
if (m_textEditor2)
m_textEditor2->setInitialBackgroundColor(bgColor);
#endif
}
void LtsTextEditDialog::dialogCanceled()
{
emit signalDialogCanceled();
BaseClass::dialogCanceled();
}
void LtsTextEditDialog::onBackgroundColorChanged(const QColor &backgroundColor)
{
slotChangesMade();
emit signalBackgroundColorChanged(backgroundColor);
}
}