• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KIO

  • kio
  • kfile
kcommentwidget.cpp
Go to the documentation of this file.
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 <kdialog.h>
24#include <klocale.h>
25
26#include <QEvent>
27#include <QLabel>
28#include <QPointer>
29#include <QTextEdit>
30#include <QVBoxLayout>
31
32KCommentWidget::KCommentWidget(QWidget* parent) :
33 QWidget(parent),
34 m_readOnly(false),
35 m_label(0),
36 m_sizeHintHelper(0),
37 m_comment()
38{
39 m_label = new QLabel(this);
40 m_label->setWordWrap(true);
41 m_label->setAlignment(Qt::AlignTop);
42 connect(m_label, SIGNAL(linkActivated(QString)), this, SLOT(slotLinkActivated(QString)));
43
44 m_sizeHintHelper = new QLabel(this);
45 m_sizeHintHelper->hide();
46
47 QVBoxLayout* layout = new QVBoxLayout(this);
48 layout->setMargin(0);
49 layout->addWidget(m_label);
50
51 setText(m_comment);
52}
53
54KCommentWidget::~KCommentWidget()
55{
56}
57
58void KCommentWidget::setText(const QString& comment)
59{
60 QString text;
61 if (comment.isEmpty()) {
62 if (m_readOnly) {
63 text = "-";
64 } else {
65 text = "<a href=\"addComment\">" + i18nc("@label", "Add Comment...") + "</a>";
66 }
67 } else {
68 if (m_readOnly) {
69 text = Qt::escape(comment);
70 } else {
71 text = "<p>" + Qt::escape(comment) + " <a href=\"changeComment\">" + i18nc("@label", "Change...") + "</a></p>";
72 }
73 }
74
75 m_label->setText(text);
76 m_sizeHintHelper->setText(text);
77 m_comment = comment;
78}
79
80QString KCommentWidget::text() const
81{
82 return m_comment;
83}
84
85void KCommentWidget::setReadOnly(bool readOnly)
86{
87 m_readOnly = readOnly;
88 setText(m_comment);
89}
90
91bool KCommentWidget::isReadOnly() const
92{
93 return m_readOnly;
94}
95
96QSize KCommentWidget::sizeHint() const
97{
98 // Per default QLabel tries to provide a square size hint. This
99 // does not work well for complex layouts that rely on a heightForWidth()
100 // functionality with unclipped content. Use an unwrapped text label
101 // as layout helper instead, that returns the preferred size of
102 // the rich-text line.
103 return m_sizeHintHelper->sizeHint();
104}
105
106bool KCommentWidget::event(QEvent* event)
107{
108 if (event->type() == QEvent::Polish) {
109 m_label->setForegroundRole(foregroundRole());
110 }
111 return QWidget::event(event);
112}
113
114void KCommentWidget::slotLinkActivated(const QString& link)
115{
116 QPointer<KDialog> dialog = new KDialog(this);
117
118 QTextEdit* editor = new QTextEdit(dialog);
119 editor->setText(m_comment);
120
121 dialog->setMainWidget(editor);
122
123 const QString caption = (link == "changeComment") ?
124 i18nc("@title:window", "Change Comment") :
125 i18nc("@title:window", "Add Comment");
126 dialog->setCaption(caption);
127 dialog->setButtons(KDialog::Ok | KDialog::Cancel);
128 dialog->setDefaultButton(KDialog::Ok);
129
130 KConfigGroup dialogConfig(KGlobal::config(), "Nepomuk KEditCommentDialog");
131 dialog->restoreDialogSize(dialogConfig);
132
133 if (dialog->exec() == QDialog::Accepted) {
134 const QString oldText = m_comment;
135 if (dialog != 0) {
136 setText(editor->toPlainText());
137 }
138 if (oldText != m_comment) {
139 emit commentChanged(m_comment);
140 }
141 }
142
143 if (dialog != 0) {
144 dialog->saveDialogSize(dialogConfig);
145 delete dialog;
146 dialog = 0;
147 }
148}
149
150#include "kcommentwidget_p.moc"
KCommentWidget::text
QString text() const
Definition: kcommentwidget.cpp:80
KCommentWidget::isReadOnly
bool isReadOnly() const
Definition: kcommentwidget.cpp:91
KCommentWidget::event
virtual bool event(QEvent *event)
Definition: kcommentwidget.cpp:106
KCommentWidget::KCommentWidget
KCommentWidget(QWidget *parent=0)
Definition: kcommentwidget.cpp:32
KCommentWidget::commentChanged
void commentChanged(const QString &comment)
KCommentWidget::setText
void setText(const QString &comment)
Definition: kcommentwidget.cpp:58
KCommentWidget::sizeHint
virtual QSize sizeHint() const
Definition: kcommentwidget.cpp:96
KCommentWidget::setReadOnly
void setReadOnly(bool readOnly)
If set to true, the comment cannot be changed by the user.
Definition: kcommentwidget.cpp:85
KCommentWidget::~KCommentWidget
virtual ~KCommentWidget()
Definition: kcommentwidget.cpp:54
KConfigGroup
KDialog
KDialog::Ok
Ok
KDialog::Cancel
Cancel
QLabel
QTextEdit
QWidget
kcommentwidget_p.h
kdialog.h
klocale.h
i18nc
QString i18nc(const char *ctxt, const char *text)
caption
QString caption()
KGlobal::config
KSharedConfigPtr config()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal