File indexing completed on 2024-05-12 05:09:48

0001 /***************************************************************************
0002     Copyright (C) 2005-2020 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "parafieldwidget.h"
0026 #include "../field.h"
0027 
0028 #include <KTextEdit>
0029 
0030 using Tellico::GUI::ParaFieldWidget;
0031 
0032 ParaFieldWidget::ParaFieldWidget(Tellico::Data::FieldPtr field_, QWidget* parent_)
0033     : FieldWidget(field_, parent_) {
0034 
0035   m_textEdit = new KTextEdit(this);
0036   m_textEdit->setAcceptRichText(false);
0037   if(field_->property(QStringLiteral("spellcheck")) != QLatin1String("false")) {
0038     m_textEdit->setCheckSpellingEnabled(true);
0039   }
0040   void (KTextEdit::* textChanged)() = &KTextEdit::textChanged;
0041   connect(m_textEdit, textChanged, this, &ParaFieldWidget::checkModified);
0042 
0043   m_brRx = QRegularExpression(QLatin1String("<br/?>"), QRegularExpression::CaseInsensitiveOption);
0044 
0045   registerWidget();
0046 }
0047 
0048 ParaFieldWidget::~ParaFieldWidget() {
0049   // saw a crash when closing Tellico and the KTextEdit d'tor called ~QSyntaxHighlighter()
0050   // when ultimately signaled a valueChange for some reason, so disconnect
0051   void (KTextEdit::* textChanged)() = &KTextEdit::textChanged;
0052   disconnect(m_textEdit, textChanged, this, &ParaFieldWidget::checkModified);
0053 }
0054 
0055 QString ParaFieldWidget::text() const {
0056   QString text = m_textEdit->toPlainText();
0057   text.replace(QLatin1Char('\n'), QLatin1String("<br/>"));
0058   return text;
0059 }
0060 
0061 void ParaFieldWidget::setTextImpl(const QString& text_) {
0062   QString s = text_;
0063   s.replace(m_brRx, QStringLiteral("\n"));
0064   m_textEdit->setPlainText(s);
0065 }
0066 
0067 void ParaFieldWidget::clearImpl() {
0068   m_textEdit->clear();
0069   editMultiple(false);
0070 }
0071 
0072 QWidget* ParaFieldWidget::widget() {
0073   return m_textEdit;
0074 }