1 /*****************************************************************************
2  * Copyright (C) 2008 by Sebastian Trueg <trueg@kde.org>                     *
3  * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at>                      *
4  *                                                                           *
5  * This library is free software; you can redistribute it and/or             *
6  * modify it under the terms of the GNU Library General Public               *
7  * License as published by the Free Software Foundation; either              *
8  * version 2 of the License, or (at your option) any later version.          *
9  *                                                                           *
10  * This library is distributed in the hope that it will be useful,           *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
13  * Library General Public License for more details.                          *
14  *                                                                           *
15  * You should have received a copy of the GNU Library General Public License *
16  * along with this library; see the file COPYING.LIB.  If not, write to      *
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,      *
18  * Boston, MA 02110-1301, USA.                                               *
19  *****************************************************************************/
20 
21 #include "kcommentwidget_p.h"
22 
23 #include <klocalizedstring.h>
24 #include <kwindowconfig.h>
25 
26 #include <QDialog>
27 #include <QDialogButtonBox>
28 #include <QEvent>
29 #include <QLabel>
30 #include <QPointer>
31 #include <QTextEdit>
32 #include <QVBoxLayout>
33 #include <ksharedconfig.h>
34 
KCommentWidget(QWidget * parent)35 KCommentWidget::KCommentWidget(QWidget *parent) :
36     QWidget(parent),
37     m_readOnly(false),
38     m_label(nullptr),
39     m_sizeHintHelper(nullptr),
40     m_comment()
41 {
42     m_label = new QLabel(this);
43     m_label->setWordWrap(true);
44     m_label->setAlignment(Qt::AlignTop);
45     connect(m_label, SIGNAL(linkActivated(QString)), this, SLOT(slotLinkActivated(QString)));
46 
47     m_sizeHintHelper = new QLabel(this);
48     m_sizeHintHelper->hide();
49 
50     QVBoxLayout *layout = new QVBoxLayout(this);
51     layout->setContentsMargins(0, 0, 0, 0);
52     layout->addWidget(m_label);
53 
54     setText(m_comment);
55 }
56 
~KCommentWidget()57 KCommentWidget::~KCommentWidget()
58 {
59 }
60 
setText(const QString & comment)61 void KCommentWidget::setText(const QString &comment)
62 {
63     QString text;
64     if (comment.isEmpty()) {
65         if (m_readOnly) {
66             text = "-";
67         } else {
68             text = "<a href=\"addComment\">" + i18nc("@label", "Add Comment...") + "</a>";
69         }
70     } else {
71         if (m_readOnly) {
72             text = comment.toHtmlEscaped();
73         } else {
74             text = "<p>" + comment.toHtmlEscaped() + " <a href=\"changeComment\">" + i18nc("@label", "Change...") + "</a></p>";
75         }
76     }
77 
78     m_label->setText(text);
79     m_sizeHintHelper->setText(text);
80     m_comment = comment;
81 }
82 
text() const83 QString KCommentWidget::text() const
84 {
85     return m_comment;
86 }
87 
setReadOnly(bool readOnly)88 void KCommentWidget::setReadOnly(bool readOnly)
89 {
90     m_readOnly = readOnly;
91     setText(m_comment);
92 }
93 
isReadOnly() const94 bool KCommentWidget::isReadOnly() const
95 {
96     return m_readOnly;
97 }
98 
sizeHint() const99 QSize KCommentWidget::sizeHint() const
100 {
101     // Per default QLabel tries to provide a square size hint. This
102     // does not work well for complex layouts that rely on a heightForWidth()
103     // functionality with unclipped content. Use an unwrapped text label
104     // as layout helper instead, that returns the preferred size of
105     // the rich-text line.
106     return m_sizeHintHelper->sizeHint();
107 }
108 
event(QEvent * event)109 bool KCommentWidget::event(QEvent *event)
110 {
111     if (event->type() == QEvent::Polish) {
112         m_label->setForegroundRole(foregroundRole());
113     }
114     return QWidget::event(event);
115 }
116 
slotLinkActivated(const QString & link)117 void KCommentWidget::slotLinkActivated(const QString &link)
118 {
119     QPointer<QDialog> dialog = new QDialog(this);
120     QVBoxLayout *layout = new QVBoxLayout;
121     dialog->setLayout(layout);
122 
123     QTextEdit *editor = new QTextEdit(dialog);
124     editor->setText(m_comment);
125     layout->addWidget(editor);
126 
127     const QString caption = (link == "changeComment") ?
128                             i18nc("@title:window", "Change Comment") :
129                             i18nc("@title:window", "Add Comment");
130     dialog->setWindowTitle(caption);
131 
132     QDialogButtonBox *buttonBox = new QDialogButtonBox(dialog);
133     buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
134     connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
135     connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
136     layout->addWidget(buttonBox);
137 
138     KConfigGroup dialogConfig(KSharedConfig::openConfig(), "Nepomuk KEditCommentDialog");
139     KWindowConfig::restoreWindowSize(dialog->windowHandle(), dialogConfig);
140 
141     if (dialog->exec() == QDialog::Accepted) {
142         const QString oldText = m_comment;
143         if (dialog != nullptr) {
144             setText(editor->toPlainText());
145         }
146         if (oldText != m_comment) {
147             emit commentChanged(m_comment);
148         }
149     }
150 
151     if (dialog != nullptr) {
152         KWindowConfig::saveWindowSize(dialog->windowHandle(), dialogConfig);
153         delete dialog;
154         dialog = nullptr;
155     }
156 }
157 
158 #include "moc_kcommentwidget_p.cpp"
159