File indexing completed on 2024-12-29 04:54:49

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "parsingresultdialog.h"
0008 #include <PimCommon/PimUtil>
0009 #include <TextCustomEditor/PlainTextEditor>
0010 #include <TextCustomEditor/PlainTextEditorWidget>
0011 
0012 #include <KConfigGroup>
0013 #include <KLocalizedString>
0014 #include <KSharedConfig>
0015 #include <KSyntaxHighlighting/Definition>
0016 #include <KSyntaxHighlighting/SyntaxHighlighter>
0017 #include <KSyntaxHighlighting/Theme>
0018 
0019 #include <KWindowConfig>
0020 #include <QDialogButtonBox>
0021 #include <QPushButton>
0022 #include <QVBoxLayout>
0023 #include <QWindow>
0024 
0025 using namespace KSieveUi;
0026 namespace
0027 {
0028 static const char myParsingResultDialogGroupName[] = "ParsingResultDialog";
0029 }
0030 ParsingResultDialog::ParsingResultDialog(QWidget *parent)
0031     : QDialog(parent)
0032 {
0033     setWindowTitle(i18nc("@title:window", "Sieve Parsing"));
0034     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
0035 
0036     auto mainLayout = new QVBoxLayout(this);
0037     auto user1Button = new QPushButton(this);
0038     buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
0039     connect(buttonBox, &QDialogButtonBox::rejected, this, &ParsingResultDialog::reject);
0040     user1Button->setText(i18n("Save As..."));
0041 
0042     mTextEdit = new TextCustomEditor::PlainTextEditorWidget(this);
0043     auto highlighter = new KSyntaxHighlighting::SyntaxHighlighter(mTextEdit->editor()->document());
0044     highlighter->setDefinition(mSyntaxRepo.definitionForName(QStringLiteral("XML")));
0045     highlighter->setTheme((palette().color(QPalette::Base).lightness() < 128) ? mSyntaxRepo.defaultTheme(KSyntaxHighlighting::Repository::DarkTheme)
0046                                                                               : mSyntaxRepo.defaultTheme(KSyntaxHighlighting::Repository::LightTheme));
0047     mTextEdit->setReadOnly(true);
0048     mainLayout->addWidget(mTextEdit);
0049     mainLayout->addWidget(buttonBox);
0050 
0051     connect(user1Button, &QPushButton::clicked, this, &ParsingResultDialog::slotSaveAs);
0052     readConfig();
0053 }
0054 
0055 ParsingResultDialog::~ParsingResultDialog()
0056 {
0057     writeConfig();
0058 }
0059 
0060 void ParsingResultDialog::setResultParsing(const QString &result)
0061 {
0062     mTextEdit->setPlainText(result);
0063 }
0064 
0065 void ParsingResultDialog::readConfig()
0066 {
0067     create(); // ensure a window is created
0068     windowHandle()->resize(QSize(800, 600));
0069     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myParsingResultDialogGroupName));
0070     KWindowConfig::restoreWindowSize(windowHandle(), group);
0071     resize(windowHandle()->size()); // workaround for QTBUG-40584
0072 }
0073 
0074 void ParsingResultDialog::writeConfig()
0075 {
0076     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myParsingResultDialogGroupName));
0077     KWindowConfig::saveWindowSize(windowHandle(), group);
0078     group.sync();
0079 }
0080 
0081 void ParsingResultDialog::slotSaveAs()
0082 {
0083     const QString filter = i18n("XML Files (*.xml);;All Files (*)");
0084     PimCommon::Util::saveTextAs(mTextEdit->toPlainText(), filter, this);
0085 }
0086 
0087 #include "moc_parsingresultdialog.cpp"