Baloo Widgets

kcommentwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2008 Sebastian Trueg <trueg@kde.org>
3 SPDX-FileCopyrightText: 2009 Peter Penz <peter.penz@gmx.at>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "kcommentwidget_p.h"
9#include "keditcommentdialog.h"
10#include "widgetsdebug.h"
11
12#include <KLocalizedString>
13#include <KSharedConfig>
14#include <KWindowConfig>
15
16#include <QEvent>
17#include <QLabel>
18#include <QPointer>
19#include <QVBoxLayout>
20
21KCommentWidget::KCommentWidget(QWidget *parent)
22 : QWidget(parent)
23 , m_label(new QLabel(this))
24 , m_sizeHintHelper(new QLabel(this))
25{
26 m_label->setWordWrap(true);
27 m_label->setAlignment(Qt::AlignTop);
28 connect(m_label, &QLabel::linkActivated, this, &KCommentWidget::slotLinkActivated);
29
30 m_sizeHintHelper->hide();
31
32 auto layout = new QVBoxLayout(this);
33 layout->setContentsMargins(0, 0, 0, 0);
34 layout->addWidget(m_label);
35
36 setText(m_comment);
37}
38
39KCommentWidget::~KCommentWidget() = default;
40
41void KCommentWidget::setText(const QString &comment)
42{
43 QString content;
44 if (comment.isEmpty()) {
45 if (m_readOnly) {
46 content = QStringLiteral("-");
47 } else {
48 content = QStringLiteral("<a href=\"addComment\">%1</a>").arg(i18nc("@label", "Add..."));
49 }
50 } else {
51 if (m_readOnly) {
52 content = comment.toHtmlEscaped();
53 } else {
54 content = QStringLiteral("<p>%1 <a href=\"editComment\">%2</a></p>").arg(comment.toHtmlEscaped(), i18nc("@label", "Edit..."));
55 }
56 }
57
58 m_label->setText(content);
59 m_sizeHintHelper->setText(content);
60 m_comment = comment;
61}
62
63QString KCommentWidget::text() const
64{
65 return m_comment;
66}
67
68void KCommentWidget::setReadOnly(bool readOnly)
69{
70 m_readOnly = readOnly;
71 setText(m_comment);
72}
73
74bool KCommentWidget::isReadOnly() const
75{
76 return m_readOnly;
77}
78
79QSize KCommentWidget::sizeHint() const
80{
81 // Per default QLabel tries to provide a square size hint. This
82 // does not work well for complex layouts that rely on a heightForWidth()
83 // functionality with unclipped content. Use an unwrapped text label
84 // as layout helper instead, that returns the preferred size of
85 // the rich-text line.
86 return m_sizeHintHelper->sizeHint();
87}
88
89bool KCommentWidget::event(QEvent *event)
90{
91 if (event->type() == QEvent::Polish) {
92 m_label->setForegroundRole(foregroundRole());
93 }
94 return QWidget::event(event);
95}
96
97void KCommentWidget::slotLinkActivated(const QString &link)
98{
99 const QString caption = (link == QLatin1String("editComment")) ? i18nc("@title:window", "Edit Comment") : i18nc("@title:window", "Add Comment");
100
101 QPointer<KEditCommentDialog> dialog = new KEditCommentDialog(this, m_comment, caption);
102
103 KConfigGroup dialogConfig(KSharedConfig::openConfig(), QStringLiteral("Baloo KEditCommentDialog"));
104 KWindowConfig::restoreWindowSize(dialog->windowHandle(), dialogConfig);
105
106 dialog->exec();
107 if (dialog.isNull()) {
108 qCWarning(Baloo::WIDGETS) << "Comment dialog destroyed while running";
109 Q_ASSERT(!dialog.isNull());
110 return;
111 }
112
113 if (dialog->result() == QDialog::Accepted) {
114 const QString oldText = m_comment;
115 setText(dialog->getCommentText());
116
117 if (oldText != m_comment) {
118 Q_EMIT commentChanged(m_comment);
119 }
120 }
121
122 KWindowConfig::saveWindowSize(dialog->windowHandle(), dialogConfig);
123 delete dialog;
124}
125
126#include "moc_kcommentwidget_p.cpp"
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
KIOCORE_EXPORT CopyJob * link(const QList< QUrl > &src, const QUrl &destDir, JobFlags flags=DefaultFlags)
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
void linkActivated(const QString &link)
bool isNull() const const
QString arg(Args &&... args) const const
bool isEmpty() const const
QString toHtmlEscaped() const const
AlignTop
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
virtual bool event(QEvent *event) override
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:18:20 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.