Warning, file /libraries/baloo-widgets/src/kcommentwidget.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 SPDX-FileCopyrightText: 2008 Sebastian Trueg <trueg@kde.org> 0003 SPDX-FileCopyrightText: 2009 Peter Penz <peter.penz@gmx.at> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "kcommentwidget_p.h" 0009 #include "keditcommentdialog.h" 0010 #include "widgetsdebug.h" 0011 0012 #include <KLocalizedString> 0013 #include <KSharedConfig> 0014 #include <KWindowConfig> 0015 0016 #include <QEvent> 0017 #include <QLabel> 0018 #include <QPointer> 0019 #include <QVBoxLayout> 0020 0021 KCommentWidget::KCommentWidget(QWidget *parent) 0022 : QWidget(parent) 0023 , m_label(new QLabel(this)) 0024 , m_sizeHintHelper(new QLabel(this)) 0025 { 0026 m_label->setWordWrap(true); 0027 m_label->setAlignment(Qt::AlignTop); 0028 connect(m_label, &QLabel::linkActivated, this, &KCommentWidget::slotLinkActivated); 0029 0030 m_sizeHintHelper->hide(); 0031 0032 auto layout = new QVBoxLayout(this); 0033 layout->setContentsMargins(0, 0, 0, 0); 0034 layout->addWidget(m_label); 0035 0036 setText(m_comment); 0037 } 0038 0039 KCommentWidget::~KCommentWidget() = default; 0040 0041 void KCommentWidget::setText(const QString &comment) 0042 { 0043 QString content; 0044 if (comment.isEmpty()) { 0045 if (m_readOnly) { 0046 content = QStringLiteral("-"); 0047 } else { 0048 content = QStringLiteral("<a href=\"addComment\">%1</a>").arg(i18nc("@label", "Add...")); 0049 } 0050 } else { 0051 if (m_readOnly) { 0052 content = comment.toHtmlEscaped(); 0053 } else { 0054 content = QStringLiteral("<p>%1 <a href=\"editComment\">%2</a></p>").arg(comment.toHtmlEscaped(), i18nc("@label", "Edit...")); 0055 } 0056 } 0057 0058 m_label->setText(content); 0059 m_sizeHintHelper->setText(content); 0060 m_comment = comment; 0061 } 0062 0063 QString KCommentWidget::text() const 0064 { 0065 return m_comment; 0066 } 0067 0068 void KCommentWidget::setReadOnly(bool readOnly) 0069 { 0070 m_readOnly = readOnly; 0071 setText(m_comment); 0072 } 0073 0074 bool KCommentWidget::isReadOnly() const 0075 { 0076 return m_readOnly; 0077 } 0078 0079 QSize KCommentWidget::sizeHint() const 0080 { 0081 // Per default QLabel tries to provide a square size hint. This 0082 // does not work well for complex layouts that rely on a heightForWidth() 0083 // functionality with unclipped content. Use an unwrapped text label 0084 // as layout helper instead, that returns the preferred size of 0085 // the rich-text line. 0086 return m_sizeHintHelper->sizeHint(); 0087 } 0088 0089 bool KCommentWidget::event(QEvent *event) 0090 { 0091 if (event->type() == QEvent::Polish) { 0092 m_label->setForegroundRole(foregroundRole()); 0093 } 0094 return QWidget::event(event); 0095 } 0096 0097 void KCommentWidget::slotLinkActivated(const QString &link) 0098 { 0099 const QString caption = (link == QLatin1String("editComment")) ? i18nc("@title:window", "Edit Comment") : i18nc("@title:window", "Add Comment"); 0100 0101 QPointer<KEditCommentDialog> dialog = new KEditCommentDialog(this, m_comment, caption); 0102 0103 KConfigGroup dialogConfig(KSharedConfig::openConfig(), "Baloo KEditCommentDialog"); 0104 KWindowConfig::restoreWindowSize(dialog->windowHandle(), dialogConfig); 0105 0106 dialog->exec(); 0107 if (dialog.isNull()) { 0108 qCWarning(Baloo::WIDGETS) << "Comment dialog destroyed while running"; 0109 Q_ASSERT(!dialog.isNull()); 0110 return; 0111 } 0112 0113 if (dialog->result() == QDialog::Accepted) { 0114 const QString oldText = m_comment; 0115 setText(dialog->getCommentText()); 0116 0117 if (oldText != m_comment) { 0118 Q_EMIT commentChanged(m_comment); 0119 } 0120 } 0121 0122 KWindowConfig::saveWindowSize(dialog->windowHandle(), dialogConfig); 0123 delete dialog; 0124 }