Incidenceeditor

incidencedescription.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Bertjan Broeksema <broeksema@kde.org>
3 SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "incidencedescription.h"
9using namespace Qt::Literals::StringLiterals;
10
11#include "ui_dialogdesktop.h"
12#include <KPIMTextEdit/RichTextComposer>
13
14#include "incidenceeditor_debug.h"
15#include <KActionCollection>
16#include <KLocalizedString>
17#include <KToolBar>
18
19using namespace IncidenceEditorNG;
20
21namespace IncidenceEditorNG
22{
23class IncidenceDescriptionPrivate
24{
25public:
26 IncidenceDescriptionPrivate() = default;
27
28 QString mRealOriginalDescriptionEditContents;
29 bool mRichTextEnabled = false;
30};
31}
32
33IncidenceDescription::IncidenceDescription(Ui::EventOrTodoDesktop *ui)
34 : IncidenceEditor(nullptr)
35 , mUi(ui)
36 , d(new IncidenceDescriptionPrivate())
37{
38 setObjectName("IncidenceDescription"_L1);
39 mUi->mRichTextLabel->setContextMenuPolicy(Qt::NoContextMenu);
40 setupToolBar();
41 connect(mUi->mRichTextLabel, &QLabel::linkActivated, this, &IncidenceDescription::toggleRichTextDescription);
42 connect(mUi->mDescriptionEdit->richTextComposer(), &KPIMTextEdit::RichTextComposer::textChanged, this, &IncidenceDescription::checkDirtyStatus);
43}
44
45IncidenceDescription::~IncidenceDescription() = default;
46
48{
49 mLoadedIncidence = incidence;
50
51 d->mRealOriginalDescriptionEditContents.clear();
52
53 if (incidence) {
54 enableRichTextDescription(incidence->descriptionIsRich());
55 if (incidence->descriptionIsRich()) {
56 mUi->mDescriptionEdit->richTextComposer()->setHtml(incidence->richDescription());
57 d->mRealOriginalDescriptionEditContents = mUi->mDescriptionEdit->richTextComposer()->toHtml();
58 } else {
59 mUi->mDescriptionEdit->richTextComposer()->setPlainText(incidence->description());
60 d->mRealOriginalDescriptionEditContents = mUi->mDescriptionEdit->richTextComposer()->toPlainText();
61 }
62 } else {
63 enableRichTextDescription(false);
64 mUi->mDescriptionEdit->richTextComposer()->clear();
65 }
66
67 mWasDirty = false;
68}
69
71{
72 if (d->mRichTextEnabled) {
73 incidence->setDescription(mUi->mDescriptionEdit->richTextComposer()->toHtml(), true);
74 } else {
75 incidence->setDescription(mUi->mDescriptionEdit->richTextComposer()->toPlainText(), false);
76 }
77}
78
80{
81 /* Sometimes, what you put in a KRichTextWidget isn't the same as what you get out.
82 Line terminators (cr,lf) for example can be converted.
83
84 So, to see if the user changed something, we can't compare the original incidence
85 with the new editor content.
86
87 Instead we compare the new editor content, with the original editor content, this way
88 any transformation regarding non-printable chars will be irrelevant.
89 */
90 if (d->mRichTextEnabled) {
91 return !mLoadedIncidence->descriptionIsRich() || d->mRealOriginalDescriptionEditContents != mUi->mDescriptionEdit->richTextComposer()->toHtml();
92 } else {
93 return mLoadedIncidence->descriptionIsRich() || d->mRealOriginalDescriptionEditContents != mUi->mDescriptionEdit->richTextComposer()->toPlainText();
94 }
95}
96
97void IncidenceDescription::enableRichTextDescription(bool enable)
98{
99 d->mRichTextEnabled = enable;
100
101 QString rt(i18nc("@action Enable or disable rich text editing", "Enable rich text"));
102 QString placeholder(QStringLiteral("<a href=\"show\">%1 &gt;&gt;</a>"));
103
104 if (enable) {
105 rt = i18nc("@action Enable or disable rich text editing", "Disable rich text");
106 placeholder = QStringLiteral("<a href=\"show\">&lt;&lt; %1</a>");
107 mUi->mDescriptionEdit->richTextComposer()->activateRichText();
108 d->mRealOriginalDescriptionEditContents = mUi->mDescriptionEdit->richTextComposer()->toHtml();
109 } else {
110 mUi->mDescriptionEdit->richTextComposer()->switchToPlainText();
111 d->mRealOriginalDescriptionEditContents = mUi->mDescriptionEdit->richTextComposer()->toPlainText();
112 }
113
114 placeholder = placeholder.arg(rt);
115 mUi->mRichTextLabel->setText(placeholder);
116 mUi->mDescriptionEdit->richTextComposer()->setEnableActions(enable);
117 mUi->mEditToolBarPlaceHolder->setVisible(enable);
119}
120
121void IncidenceDescription::toggleRichTextDescription()
122{
123 enableRichTextDescription(!d->mRichTextEnabled);
124}
125
126void IncidenceDescription::setupToolBar()
127{
128#ifndef QT_NO_TOOLBAR
129 auto collection = new KActionCollection(this);
130 mUi->mDescriptionEdit->richTextComposer()->createActions(collection);
131
132 auto mEditToolBar = new KToolBar(mUi->mEditToolBarPlaceHolder);
133 mEditToolBar->setToolButtonStyle(Qt::ToolButtonIconOnly);
134 mEditToolBar->addAction(collection->action(QStringLiteral("format_text_bold")));
135 mEditToolBar->addAction(collection->action(QStringLiteral("format_text_italic")));
136 mEditToolBar->addAction(collection->action(QStringLiteral("format_text_underline")));
137 mEditToolBar->addAction(collection->action(QStringLiteral("format_text_strikeout")));
138 mEditToolBar->addSeparator();
139
140 mEditToolBar->addAction(collection->action(QStringLiteral("format_font_family")));
141 mEditToolBar->addAction(collection->action(QStringLiteral("format_font_size")));
142 mEditToolBar->addSeparator();
143
144 mEditToolBar->addAction(collection->action(QStringLiteral("format_text_foreground_color")));
145 mEditToolBar->addAction(collection->action(QStringLiteral("format_text_background_color")));
146 mEditToolBar->addSeparator();
147
148 mEditToolBar->addAction(collection->action(QStringLiteral("format_list_style")));
149 mEditToolBar->addSeparator();
150
151 mEditToolBar->addAction(collection->action(QStringLiteral("format_align_left")));
152 mEditToolBar->addAction(collection->action(QStringLiteral("format_align_center")));
153 mEditToolBar->addAction(collection->action(QStringLiteral("format_align_right")));
154 mEditToolBar->addAction(collection->action(QStringLiteral("format_align_justify")));
155 mEditToolBar->addSeparator();
156
157 mEditToolBar->addAction(collection->action(QStringLiteral("format_painter")));
158 mEditToolBar->addSeparator();
159 mEditToolBar->addAction(collection->action(QStringLiteral("manage_link")));
160 mUi->mDescriptionEdit->richTextComposer()->setEnableActions(false);
161
162 auto layout = new QGridLayout(mUi->mEditToolBarPlaceHolder);
163 layout->addWidget(mEditToolBar);
164#endif
165
166 // By default we don't show the rich text toolbar.
167 mUi->mEditToolBarPlaceHolder->setVisible(false);
168 d->mRichTextEnabled = false;
169}
170
172{
173 // We're going to crash
174 qCDebug(INCIDENCEEDITOR_LOG) << "RichText enabled " << d->mRichTextEnabled;
175
176 if (mLoadedIncidence) {
177 qCDebug(INCIDENCEEDITOR_LOG) << "Incidence description is rich " << mLoadedIncidence->descriptionIsRich();
178
179 if (mLoadedIncidence->descriptionIsRich()) {
180 qCDebug(INCIDENCEEDITOR_LOG) << "desc is rich, and it is <desc>" << mLoadedIncidence->richDescription() << "</desc>; "
181 << "widget has <desc>" << mUi->mDescriptionEdit->richTextComposer()->toHtml() << "</desc>; "
182 << "expr mLoadedIncidence->richDescription() != mUi->mDescriptionEdit->toHtml() is "
183 << (mLoadedIncidence->richDescription() != mUi->mDescriptionEdit->richTextComposer()->toHtml());
184 } else {
185 qCDebug(INCIDENCEEDITOR_LOG) << "desc is not rich, and it is <desc>" << mLoadedIncidence->description() << "</desc>; "
186 << "widget has <desc>" << mUi->mDescriptionEdit->richTextComposer()->toPlainText() << "</desc>; "
187 << "expr mLoadedIncidence->description() != mUi->mDescriptionEdit->toPlainText() is "
188 << (mLoadedIncidence->description() != mUi->mDescriptionEdit->richTextComposer()->toPlainText());
189 }
190 } else {
191 qCDebug(INCIDENCEEDITOR_LOG) << "Incidence is invalid";
192 }
193}
194
195#include "moc_incidencedescription.cpp"
bool isDirty() const override
Returns whether or not the current values in the editor differ from the initial values.
void save(const KCalendarCore::Incidence::Ptr &incidence) override
Store the current values of the editor into.
void load(const KCalendarCore::Incidence::Ptr &incidence) override
Load the values of.
void printDebugInfo() const override
Re-implement this and print important member values and widget enabled/disabled states that could hav...
KCal Incidences are complicated objects.
void checkDirtyStatus()
Checks if the dirty status has changed until last check and emits the dirtyStatusChanged signal if ne...
QSharedPointer< IncidenceT > incidence() const
Convenience method to get a pointer for a specific const Incidence Type.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void linkActivated(const QString &link)
NoContextMenu
ToolButtonIconOnly
void textChanged()
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:58:15 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.