KTextAddons

translatordebugdialog.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "translatordebugdialog.h"
8#include <QPlainTextEdit>
9
10#include <KConfigGroup>
11#include <KLocalizedString>
12#include <KMessageBox>
13#include <KSharedConfig>
14#include <KWindowConfig>
15#include <QDialogButtonBox>
16#include <QFileDialog>
17#include <QPointer>
18#include <QPushButton>
19#include <QTextStream>
20#include <QVBoxLayout>
21#include <QWindow>
22
23#include <cerrno>
24namespace
25{
26const char myTranslatorDebugDialogConfigGroupName[] = "TranslatorDebugDialog";
27}
28using namespace TextTranslator;
29TranslatorDebugDialog::TranslatorDebugDialog(QWidget *parent)
30 : QDialog(parent)
31 , mEdit(new QPlainTextEdit(this))
32 , mUser1Button(new QPushButton(this))
33{
34 setWindowTitle(i18nc("@title:window", "Translator Debug"));
35 auto mainLayout = new QVBoxLayout(this);
36
37 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
38 buttonBox->addButton(mUser1Button, QDialogButtonBox::ActionRole);
39 connect(buttonBox, &QDialogButtonBox::accepted, this, &TranslatorDebugDialog::accept);
40 connect(buttonBox, &QDialogButtonBox::rejected, this, &TranslatorDebugDialog::reject);
41 mUser1Button->setText(i18n("Save As…"));
42 connect(mUser1Button, &QPushButton::clicked, this, &TranslatorDebugDialog::slotSaveAs);
43
44 mEdit->setReadOnly(true);
45 mainLayout->addWidget(mEdit);
46 mainLayout->addWidget(buttonBox);
47
48 readConfig();
49 mUser1Button->setEnabled(!mEdit->toPlainText().isEmpty());
50}
51
52TranslatorDebugDialog::~TranslatorDebugDialog()
53{
54 writeConfig();
55}
56
57void TranslatorDebugDialog::setDebug(const QString &debugStr)
58{
59 mEdit->setPlainText(debugStr);
60 mUser1Button->setEnabled(!debugStr.isEmpty());
61}
62
63void TranslatorDebugDialog::readConfig()
64{
65 create(); // ensure a window is created
66 windowHandle()->resize(QSize(800, 600));
67 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myTranslatorDebugDialogConfigGroupName));
69 resize(windowHandle()->size()); // workaround for QTBUG-40584
70}
71
72void TranslatorDebugDialog::writeConfig()
73{
74 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myTranslatorDebugDialogConfigGroupName));
76 group.sync();
77}
78
79void TranslatorDebugDialog::saveTextAs(const QString &text, const QString &filter, QWidget *parent, const QUrl &url, const QString &caption)
80{
81 QPointer<QFileDialog> fdlg(new QFileDialog(parent, QString(), url.path(), filter));
82 if (!caption.isEmpty()) {
83 fdlg->setWindowTitle(caption);
84 }
85 fdlg->setAcceptMode(QFileDialog::AcceptSave);
86 if (fdlg->exec() == QDialog::Accepted) {
87 const QString fileName = fdlg->selectedFiles().at(0);
88 if (!saveToFile(fileName, text)) {
90 i18n("Could not write the file %1:\n"
91 "\"%2\" is the detailed error description.",
92 fileName,
93 QString::fromLocal8Bit(strerror(errno))),
94 i18nc("@title:window", "Save File Error"));
95 }
96 }
97 delete fdlg;
98}
99
100bool TranslatorDebugDialog::saveToFile(const QString &filename, const QString &text)
101{
102 QFile file(filename);
103 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
104 return false;
105 }
106 QTextStream out(&file);
107 out << text;
108 file.close();
109 return true;
110}
111
112void TranslatorDebugDialog::slotSaveAs()
113{
114 const QString filter = i18n("All Files (*)");
115 saveTextAs(mEdit->toPlainText(), filter, this);
116}
117
118#include "moc_translatordebugdialog.cpp"
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
void clicked(bool checked)
QObject * parent() const const
void setPlainText(const QString &text)
QString toPlainText() const const
const QChar at(qsizetype position) const const
QString fromLocal8Bit(QByteArrayView str)
bool isEmpty() const const
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString path(ComponentFormattingOptions options) const const
void create(WId window, bool initializeWindow, bool destroyOldWindow)
void setEnabled(bool)
void resize(const QSize &)
QWindow * windowHandle() const const
void resize(const QSize &newSize)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:29:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.