]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/information/kcommentwidget.cpp
* No need to reload all tags when one is deleted.
[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 <QEvent>
27 #include <QLabel>
28 #include <QTextEdit>
29 #include <QVBoxLayout>
30
31 KCommentWidget::KCommentWidget(QWidget* parent) :
32 QWidget(parent),
33 m_readOnly(false),
34 m_label(0),
35 m_comment()
36 {
37 m_label = new QLabel(this);
38 m_label->setFont(KGlobalSettings::smallestReadableFont());
39 m_label->setWordWrap(true);
40 m_label->setAlignment(Qt::AlignTop);
41 connect(m_label, SIGNAL(linkActivated(const QString&)), this, SLOT(slotLinkActivated(const QString&)));
42
43 QVBoxLayout* layout = new QVBoxLayout(this);
44 layout->setMargin(0);
45 layout->addWidget(m_label);
46
47 setText(m_comment);
48 }
49
50 KCommentWidget::~KCommentWidget()
51 {
52 }
53
54 void KCommentWidget::setText(const QString& comment)
55 {
56 QString text;
57 if (comment.isEmpty()) {
58 if (m_readOnly) {
59 text = "-";
60 } else {
61 text = "<a href=\"addComment\">" + i18nc("@label", "Add Comment...") + "</a>";
62 }
63 } else {
64 if (m_readOnly) {
65 text = comment;
66 } else {
67 text = "<p>" + comment + " <a href=\"changeComment\">" + i18nc("@label", "Change...") + "</a></p>";
68 }
69 }
70
71 m_label->setText(text);
72 m_comment = comment;
73 }
74
75 QString KCommentWidget::text() const
76 {
77 return m_comment;
78 }
79
80 void KCommentWidget::setReadOnly(bool readOnly)
81 {
82 m_readOnly = readOnly;
83 setText(m_comment);
84 }
85
86 bool KCommentWidget::isReadOnly() const
87 {
88 return m_readOnly;
89 }
90
91 bool KCommentWidget::event(QEvent* event)
92 {
93 if (event->type() == QEvent::Polish) {
94 m_label->setForegroundRole(foregroundRole());
95 }
96 return QWidget::event(event);
97 }
98
99 void KCommentWidget::slotLinkActivated(const QString& link)
100 {
101 KDialog dialog(this, Qt::Dialog);
102
103 QTextEdit* editor = new QTextEdit(&dialog);
104 editor->setText(m_comment);
105
106 dialog.setMainWidget(editor);
107
108 const QString caption = (link == "changeComment") ?
109 i18nc("@title:window", "Change Comment") :
110 i18nc("@title:window", "Add Comment");
111 dialog.setCaption(caption);
112 dialog.setButtons(KDialog::Ok | KDialog::Cancel);
113 dialog.setDefaultButton(KDialog::Ok);
114
115 KConfigGroup dialogConfig(KGlobal::config(), "Nepomuk KEditCommentDialog");
116 dialog.restoreDialogSize(dialogConfig);
117
118 if (dialog.exec() == QDialog::Accepted) {
119 const QString oldText = m_comment;
120 setText(editor->toPlainText());
121 if (oldText != m_comment) {
122 emit commentChanged(m_comment);
123 }
124 }
125
126 dialog.saveDialogSize(dialogConfig, KConfigBase::Persistent);
127 }
128
129 #include "kcommentwidget_p.moc"