KIO
kcommentwidget.cpp
Go to the documentation of this file.
00001 /***************************************************************************** 00002 * Copyright (C) 2008 by Sebastian Trueg <trueg@kde.org> * 00003 * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> * 00004 * * 00005 * This library is free software; you can redistribute it and/or * 00006 * modify it under the terms of the GNU Library General Public * 00007 * License as published by the Free Software Foundation; either * 00008 * version 2 of the License, or (at your option) any later version. * 00009 * * 00010 * This library is distributed in the hope that it will be useful, * 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00013 * Library General Public License for more details. * 00014 * * 00015 * You should have received a copy of the GNU Library General Public License * 00016 * along with this library; see the file COPYING.LIB. If not, write to * 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * 00018 * Boston, MA 02110-1301, USA. * 00019 *****************************************************************************/ 00020 00021 #include "kcommentwidget_p.h" 00022 00023 #include <kdialog.h> 00024 #include <klocale.h> 00025 00026 #include <QEvent> 00027 #include <QLabel> 00028 #include <QPointer> 00029 #include <QTextEdit> 00030 #include <QVBoxLayout> 00031 00032 KCommentWidget::KCommentWidget(QWidget* parent) : 00033 QWidget(parent), 00034 m_readOnly(false), 00035 m_label(0), 00036 m_sizeHintHelper(0), 00037 m_comment() 00038 { 00039 m_label = new QLabel(this); 00040 m_label->setWordWrap(true); 00041 m_label->setAlignment(Qt::AlignTop); 00042 connect(m_label, SIGNAL(linkActivated(const QString&)), this, SLOT(slotLinkActivated(const QString&))); 00043 00044 m_sizeHintHelper = new QLabel(this); 00045 m_sizeHintHelper->hide(); 00046 00047 QVBoxLayout* layout = new QVBoxLayout(this); 00048 layout->setMargin(0); 00049 layout->addWidget(m_label); 00050 00051 setText(m_comment); 00052 } 00053 00054 KCommentWidget::~KCommentWidget() 00055 { 00056 } 00057 00058 void KCommentWidget::setText(const QString& comment) 00059 { 00060 QString text; 00061 if (comment.isEmpty()) { 00062 if (m_readOnly) { 00063 text = "-"; 00064 } else { 00065 text = "<a href=\"addComment\">" + i18nc("@label", "Add Comment...") + "</a>"; 00066 } 00067 } else { 00068 if (m_readOnly) { 00069 text = Qt::escape(comment); 00070 } else { 00071 text = "<p>" + Qt::escape(comment) + " <a href=\"changeComment\">" + i18nc("@label", "Change...") + "</a></p>"; 00072 } 00073 } 00074 00075 m_label->setText(text); 00076 m_sizeHintHelper->setText(text); 00077 m_comment = comment; 00078 } 00079 00080 QString KCommentWidget::text() const 00081 { 00082 return m_comment; 00083 } 00084 00085 void KCommentWidget::setReadOnly(bool readOnly) 00086 { 00087 m_readOnly = readOnly; 00088 setText(m_comment); 00089 } 00090 00091 bool KCommentWidget::isReadOnly() const 00092 { 00093 return m_readOnly; 00094 } 00095 00096 QSize KCommentWidget::sizeHint() const 00097 { 00098 // Per default QLabel tries to provide a square size hint. This 00099 // does not work well for complex layouts that rely on a heightForWidth() 00100 // functionality with unclipped content. Use an unwrapped text label 00101 // as layout helper instead, that returns the preferred size of 00102 // the rich-text line. 00103 return m_sizeHintHelper->sizeHint(); 00104 } 00105 00106 bool KCommentWidget::event(QEvent* event) 00107 { 00108 if (event->type() == QEvent::Polish) { 00109 m_label->setForegroundRole(foregroundRole()); 00110 } 00111 return QWidget::event(event); 00112 } 00113 00114 void KCommentWidget::slotLinkActivated(const QString& link) 00115 { 00116 QPointer<KDialog> dialog = new KDialog(this); 00117 00118 QTextEdit* editor = new QTextEdit(dialog); 00119 editor->setText(m_comment); 00120 00121 dialog->setMainWidget(editor); 00122 00123 const QString caption = (link == "changeComment") ? 00124 i18nc("@title:window", "Change Comment") : 00125 i18nc("@title:window", "Add Comment"); 00126 dialog->setCaption(caption); 00127 dialog->setButtons(KDialog::Ok | KDialog::Cancel); 00128 dialog->setDefaultButton(KDialog::Ok); 00129 00130 KConfigGroup dialogConfig(KGlobal::config(), "Nepomuk KEditCommentDialog"); 00131 dialog->restoreDialogSize(dialogConfig); 00132 00133 if (dialog->exec() == QDialog::Accepted) { 00134 const QString oldText = m_comment; 00135 if (dialog != 0) { 00136 setText(editor->toPlainText()); 00137 } 00138 if (oldText != m_comment) { 00139 emit commentChanged(m_comment); 00140 } 00141 } 00142 00143 if (dialog != 0) { 00144 dialog->saveDialogSize(dialogConfig); 00145 delete dialog; 00146 dialog = 0; 00147 } 00148 } 00149 00150 #include "kcommentwidget_p.moc"
KDE 4.6 API Reference