]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/kcommentwidget.cpp
Stop crashing (apparently I have HAVE_NEPOMUK but the runtime check fails so the...
[dolphin.git] / src / panels / information / kcommentwidget.cpp
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 version 2 as published by the Free Software Foundation. *
8 * *
9 * This library is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Library General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Library General Public License *
15 * along with this library; see the file COPYING.LIB. If not, write to *
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301, USA. *
18 *****************************************************************************/
19
20 #include "kcommentwidget_p.h"
21
22 #include <kdialog.h>
23 #include <kglobalsettings.h>
24 #include <klocale.h>
25
26 #include <QLabel>
27 #include <QTextEdit>
28 #include <QVBoxLayout>
29
30 KCommentWidget::KCommentWidget(QWidget* parent) :
31 QWidget(parent),
32 m_readOnly(false),
33 m_label(0),
34 m_comment()
35 {
36 m_label = new QLabel(this);
37 m_label->setFont(KGlobalSettings::smallestReadableFont());
38 m_label->setWordWrap(true);
39 m_label->setAlignment(Qt::AlignTop);
40 connect(m_label, SIGNAL(linkActivated(const QString&)), this, SLOT(slotLinkActivated(const QString&)));
41
42 QVBoxLayout* layout = new QVBoxLayout(this);
43 layout->setMargin(0);
44 layout->addWidget(m_label);
45
46 setText(m_comment);
47 }
48
49 KCommentWidget::~KCommentWidget()
50 {
51 }
52
53 void KCommentWidget::setText(const QString& comment)
54 {
55 QString text;
56 if (comment.isEmpty()) {
57 if (m_readOnly) {
58 text = "-";
59 } else {
60 text = "<a href=\"addComment\">" + i18nc("@label", "Add Comment...") + "</a>";
61 }
62 } else {
63 if (m_readOnly) {
64 text = comment;
65 } else {
66 text = "<p>" + comment + " <a href=\"changeComment\">" + i18nc("@label", "Change...") + "</a></p>";
67 }
68 }
69
70 m_label->setText(text);
71 m_comment = comment;
72 }
73
74 QString KCommentWidget::text() const
75 {
76 return m_comment;
77 }
78
79 void KCommentWidget::setReadOnly(bool readOnly)
80 {
81 m_readOnly = readOnly;
82 setText(m_comment);
83 }
84
85 bool KCommentWidget::isReadOnly() const
86 {
87 return m_readOnly;
88 }
89
90 void KCommentWidget::slotLinkActivated(const QString& link)
91 {
92 KDialog dialog(this, Qt::Dialog);
93
94 QTextEdit* editor = new QTextEdit(&dialog);
95 editor->setText(m_comment);
96
97 dialog.setMainWidget(editor);
98
99 const QString caption = (link == "changeComment") ?
100 i18nc("@title:window", "Change Comment") :
101 i18nc("@title:window", "Add Comment");
102 dialog.setCaption(caption);
103 dialog.setButtons(KDialog::Ok | KDialog::Cancel);
104 dialog.setDefaultButton(KDialog::Ok);
105
106 KConfigGroup dialogConfig(KGlobal::config(), "Nepomuk KEditCommentDialog");
107 dialog.restoreDialogSize(dialogConfig);
108
109 if (dialog.exec() == QDialog::Accepted) {
110 const QString oldText = m_comment;
111 setText(editor->toPlainText());
112 if (oldText != m_comment) {
113 emit commentChanged(m_comment);
114 }
115 }
116
117 dialog.saveDialogSize(dialogConfig, KConfigBase::Persistent);
118 }
119
120 #include "kcommentwidget_p.moc"