File indexing completed on 2024-05-05 16:08:23

0001 /*****************************************************************************
0002  * Copyright (C) 2008 by Sebastian Trueg <trueg@kde.org>                     *
0003  * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at>                      *
0004  *                                                                           *
0005  * This library is free software; you can redistribute it and/or             *
0006  * modify it under the terms of the GNU Library General Public               *
0007  * License as published by the Free Software Foundation; either              *
0008  * version 2 of the License, or (at your option) any later version.          *
0009  *                                                                           *
0010  * This library is distributed in the hope that it will be useful,           *
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
0013  * Library General Public License for more details.                          *
0014  *                                                                           *
0015  * You should have received a copy of the GNU Library General Public License *
0016  * along with this library; see the file COPYING.LIB.  If not, write to      *
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,      *
0018  * Boston, MA 02110-1301, USA.                                               *
0019  *****************************************************************************/
0020 
0021 #include "kcommentwidget_p.h"
0022 
0023 #include <klocalizedstring.h>
0024 #include <kwindowconfig.h>
0025 
0026 #include <QDialog>
0027 #include <QDialogButtonBox>
0028 #include <QEvent>
0029 #include <QLabel>
0030 #include <QPointer>
0031 #include <QTextEdit>
0032 #include <QVBoxLayout>
0033 #include <ksharedconfig.h>
0034 
0035 KCommentWidget::KCommentWidget(QWidget *parent) :
0036     QWidget(parent),
0037     m_readOnly(false),
0038     m_label(nullptr),
0039     m_sizeHintHelper(nullptr),
0040     m_comment()
0041 {
0042     m_label = new QLabel(this);
0043     m_label->setWordWrap(true);
0044     m_label->setAlignment(Qt::AlignTop);
0045     connect(m_label, SIGNAL(linkActivated(QString)), this, SLOT(slotLinkActivated(QString)));
0046 
0047     m_sizeHintHelper = new QLabel(this);
0048     m_sizeHintHelper->hide();
0049 
0050     QVBoxLayout *layout = new QVBoxLayout(this);
0051     layout->setContentsMargins(0, 0, 0, 0);
0052     layout->addWidget(m_label);
0053 
0054     setText(m_comment);
0055 }
0056 
0057 KCommentWidget::~KCommentWidget()
0058 {
0059 }
0060 
0061 void KCommentWidget::setText(const QString &comment)
0062 {
0063     QString text;
0064     if (comment.isEmpty()) {
0065         if (m_readOnly) {
0066             text = "-";
0067         } else {
0068             text = "<a href=\"addComment\">" + i18nc("@label", "Add Comment...") + "</a>";
0069         }
0070     } else {
0071         if (m_readOnly) {
0072             text = comment.toHtmlEscaped();
0073         } else {
0074             text = "<p>" + comment.toHtmlEscaped() + " <a href=\"changeComment\">" + i18nc("@label", "Change...") + "</a></p>";
0075         }
0076     }
0077 
0078     m_label->setText(text);
0079     m_sizeHintHelper->setText(text);
0080     m_comment = comment;
0081 }
0082 
0083 QString KCommentWidget::text() const
0084 {
0085     return m_comment;
0086 }
0087 
0088 void KCommentWidget::setReadOnly(bool readOnly)
0089 {
0090     m_readOnly = readOnly;
0091     setText(m_comment);
0092 }
0093 
0094 bool KCommentWidget::isReadOnly() const
0095 {
0096     return m_readOnly;
0097 }
0098 
0099 QSize KCommentWidget::sizeHint() const
0100 {
0101     // Per default QLabel tries to provide a square size hint. This
0102     // does not work well for complex layouts that rely on a heightForWidth()
0103     // functionality with unclipped content. Use an unwrapped text label
0104     // as layout helper instead, that returns the preferred size of
0105     // the rich-text line.
0106     return m_sizeHintHelper->sizeHint();
0107 }
0108 
0109 bool KCommentWidget::event(QEvent *event)
0110 {
0111     if (event->type() == QEvent::Polish) {
0112         m_label->setForegroundRole(foregroundRole());
0113     }
0114     return QWidget::event(event);
0115 }
0116 
0117 void KCommentWidget::slotLinkActivated(const QString &link)
0118 {
0119     QPointer<QDialog> dialog = new QDialog(this);
0120     QVBoxLayout *layout = new QVBoxLayout;
0121     dialog->setLayout(layout);
0122 
0123     QTextEdit *editor = new QTextEdit(dialog);
0124     editor->setText(m_comment);
0125     layout->addWidget(editor);
0126 
0127     const QString caption = (link == "changeComment") ?
0128                             i18nc("@title:window", "Change Comment") :
0129                             i18nc("@title:window", "Add Comment");
0130     dialog->setWindowTitle(caption);
0131 
0132     QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog);
0133     buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0134     connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
0135     connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
0136     layout->addWidget(buttonBox);
0137 
0138     KConfigGroup dialogConfig(KSharedConfig::openConfig(), "Nepomuk KEditCommentDialog");
0139     KWindowConfig::restoreWindowSize(dialog->windowHandle(), dialogConfig);
0140 
0141     if (dialog->exec() == QDialog::Accepted) {
0142         const QString oldText = m_comment;
0143         if (dialog != nullptr) {
0144             setText(editor->toPlainText());
0145         }
0146         if (oldText != m_comment) {
0147             emit commentChanged(m_comment);
0148         }
0149     }
0150 
0151     if (dialog != nullptr) {
0152         KWindowConfig::saveWindowSize(dialog->windowHandle(), dialogConfig);
0153         delete dialog;
0154         dialog = nullptr;
0155     }
0156 }
0157 
0158 #include "moc_kcommentwidget_p.cpp"