Baloo Widgets

kcommentwidget.cpp
1 /*****************************************************************************
2  * Copyright (C) 2008 by Sebastian Trueg <[email protected]> *
3  * Copyright (C) 2009 by Peter Penz <[email protected]> *
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 #include "keditcommentdialog.h"
23 
24 #include <KLocalizedString>
25 #include <KConfigGui/KWindowConfig>
26 #include <KSharedConfig>
27 
28 #include <QEvent>
29 #include <QPointer>
30 #include <QLabel>
31 #include <QTextEdit>
32 #include <QVBoxLayout>
33 
34 KCommentWidget::KCommentWidget(QWidget* parent) :
35  QWidget(parent),
36  m_readOnly(false),
37  m_label(nullptr),
38  m_sizeHintHelper(nullptr),
39  m_comment()
40 {
41  m_label = new QLabel(this);
42  m_label->setWordWrap(true);
43  m_label->setAlignment(Qt::AlignTop);
44  connect(m_label, &QLabel::linkActivated, this, &KCommentWidget::slotLinkActivated);
45 
46  m_sizeHintHelper = new QLabel(this);
47  m_sizeHintHelper->hide();
48 
49  QVBoxLayout* layout = new QVBoxLayout(this);
50  layout->setContentsMargins(0, 0, 0, 0);
51  layout->addWidget(m_label);
52 
53  setText(m_comment);
54 }
55 
56 KCommentWidget::~KCommentWidget()
57 {
58 }
59 
60 void KCommentWidget::setText(const QString& comment)
61 {
62  QString text;
63  if (comment.isEmpty()) {
64  if (m_readOnly) {
65  text = QStringLiteral("-");
66  } else {
67  text = QStringLiteral("<a href=\"addComment\">") + i18nc("@label", "Add...") + QStringLiteral("</a>");
68  }
69  } else {
70  if (m_readOnly) {
71  text = comment.toHtmlEscaped();
72  } else {
73  text = QStringLiteral("<p>") + comment.toHtmlEscaped() + QStringLiteral(" <a href=\"editComment\">") + i18nc("@label", "Edit...") + QStringLiteral("</a></p>");
74  }
75  }
76 
77  m_label->setText(text);
78  m_sizeHintHelper->setText(text);
79  m_comment = comment;
80 }
81 
82 QString KCommentWidget::text() const
83 {
84  return m_comment;
85 }
86 
87 void KCommentWidget::setReadOnly(bool readOnly)
88 {
89  m_readOnly = readOnly;
90  setText(m_comment);
91 }
92 
93 bool KCommentWidget::isReadOnly() const
94 {
95  return m_readOnly;
96 }
97 
98 QSize KCommentWidget::sizeHint() const
99 {
100  // Per default QLabel tries to provide a square size hint. This
101  // does not work well for complex layouts that rely on a heightForWidth()
102  // functionality with unclipped content. Use an unwrapped text label
103  // as layout helper instead, that returns the preferred size of
104  // the rich-text line.
105  return m_sizeHintHelper->sizeHint();
106 }
107 
108 bool KCommentWidget::event(QEvent* event)
109 {
110  if (event->type() == QEvent::Polish) {
111  m_label->setForegroundRole(foregroundRole());
112  }
113  return QWidget::event(event);
114 }
115 
116 void KCommentWidget::slotLinkActivated(const QString& link)
117 {
118  const QString caption = (link == QLatin1String("editComment")) ?
119  i18nc("@title:window", "Edit Comment") :
120  i18nc("@title:window", "Add Comment");
121 
122  QPointer<KEditCommentDialog> dialog = new KEditCommentDialog(this, m_comment, caption);
123 
124  KConfigGroup dialogConfig(KSharedConfig::openConfig(), "Baloo KEditCommentDialog");
125  KWindowConfig::restoreWindowSize(dialog->windowHandle(), dialogConfig);
126 
127  if (dialog->exec() == QDialog::Accepted) {
128  const QString oldText = m_comment;
129  if (dialog != nullptr) {
130  setText(dialog->getCommentText());
131  }
132  if (oldText != m_comment) {
133  Q_EMIT commentChanged(m_comment);
134  }
135  }
136 
137  if (dialog != nullptr) {
138  KWindowConfig::saveWindowSize(dialog->windowHandle(), dialogConfig);
139  delete dialog;
140  dialog = nullptr;
141  }
142 }
QString caption()
virtual QLayout * layout() override
QEvent::Type type() const const
void setContentsMargins(int left, int top, int right, int bottom)
AlignTop
virtual bool event(QEvent *e)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
bool isEmpty() const const
void linkActivated(const QString &link)
QString toHtmlEscaped() const const
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
virtual bool event(QEvent *event) override
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 18 2021 23:07:21 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.