Baloo Widgets

kcommentwidget.cpp
1 /*
2  SPDX-FileCopyrightText: 2008 Sebastian Trueg <[email protected]>
3  SPDX-FileCopyrightText: 2009 Peter Penz <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "kcommentwidget_p.h"
9 #include "keditcommentdialog.h"
10 
11 #include <KLocalizedString>
12 #include <KSharedConfig>
13 #include <KWindowConfig>
14 
15 #include <QEvent>
16 #include <QLabel>
17 #include <QPointer>
18 #include <QTextEdit>
19 #include <QVBoxLayout>
20 
21 KCommentWidget::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 
39 KCommentWidget::~KCommentWidget() = default;
40 
41 void KCommentWidget::setText(const QString &comment)
42 {
43  QString text;
44  if (comment.isEmpty()) {
45  if (m_readOnly) {
46  text = QStringLiteral("-");
47  } else {
48  text = QStringLiteral("<a href=\"addComment\">") + i18nc("@label", "Add...") + QStringLiteral("</a>");
49  }
50  } else {
51  if (m_readOnly) {
52  text = comment.toHtmlEscaped();
53  } else {
54  text = QStringLiteral("<p>") + comment.toHtmlEscaped() + QStringLiteral(" <a href=\"editComment\">") + i18nc("@label", "Edit...")
55  + QStringLiteral("</a></p>");
56  }
57  }
58 
59  m_label->setText(text);
60  m_sizeHintHelper->setText(text);
61  m_comment = comment;
62 }
63 
64 QString KCommentWidget::text() const
65 {
66  return m_comment;
67 }
68 
69 void KCommentWidget::setReadOnly(bool readOnly)
70 {
71  m_readOnly = readOnly;
72  setText(m_comment);
73 }
74 
75 bool KCommentWidget::isReadOnly() const
76 {
77  return m_readOnly;
78 }
79 
80 QSize KCommentWidget::sizeHint() const
81 {
82  // Per default QLabel tries to provide a square size hint. This
83  // does not work well for complex layouts that rely on a heightForWidth()
84  // functionality with unclipped content. Use an unwrapped text label
85  // as layout helper instead, that returns the preferred size of
86  // the rich-text line.
87  return m_sizeHintHelper->sizeHint();
88 }
89 
90 bool KCommentWidget::event(QEvent *event)
91 {
92  if (event->type() == QEvent::Polish) {
93  m_label->setForegroundRole(foregroundRole());
94  }
95  return QWidget::event(event);
96 }
97 
98 void KCommentWidget::slotLinkActivated(const QString &link)
99 {
100  const QString caption = (link == QLatin1String("editComment")) ? i18nc("@title:window", "Edit Comment") : i18nc("@title:window", "Add Comment");
101 
102  QPointer<KEditCommentDialog> dialog = new KEditCommentDialog(this, m_comment, caption);
103 
104  KConfigGroup dialogConfig(KSharedConfig::openConfig(), "Baloo KEditCommentDialog");
105  KWindowConfig::restoreWindowSize(dialog->windowHandle(), dialogConfig);
106 
107  if (dialog->exec() == QDialog::Accepted) {
108  const QString oldText = m_comment;
109  if (dialog != nullptr) {
110  setText(dialog->getCommentText());
111  }
112  if (oldText != m_comment) {
113  Q_EMIT commentChanged(m_comment);
114  }
115  }
116 
117  if (dialog != nullptr) {
118  KWindowConfig::saveWindowSize(dialog->windowHandle(), dialogConfig);
119  delete dialog;
120  dialog = nullptr;
121  }
122 }
AlignTop
virtual QLayout * layout() override
QString toHtmlEscaped() const const
virtual bool event(QEvent *event) override
QString caption()
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
void linkActivated(const QString &link)
KIOCORE_EXPORT CopyJob * link(const QList< QUrl > &src, const QUrl &destDir, JobFlags flags=DefaultFlags)
bool isEmpty() const const
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
void addWidget(QWidget *w)
void setContentsMargins(int left, int top, int right, int bottom)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Aug 11 2022 04:13:42 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.