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

0001 /***************************************************************************
0002     Copyright (C) 2005-2021 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 "linefieldwidget.h"
0026 #include "../field.h"
0027 #include "../fieldformat.h"
0028 #include "../fieldcompletion.h"
0029 #include "../document.h"
0030 #include "../gui/lineedit.h"
0031 #include "../utils/isbnvalidator.h"
0032 
0033 using Tellico::GUI::LineFieldWidget;
0034 
0035 LineFieldWidget::LineFieldWidget(Tellico::Data::FieldPtr field_, QWidget* parent_)
0036     : FieldWidget(field_, parent_) {
0037 
0038   m_lineEdit = new GUI::LineEdit(this);
0039   m_lineEdit->setAllowSpellCheck(true);
0040   m_lineEdit->setEnableSpellCheck(field_->formatType() != FieldFormat::FormatName);
0041   connect(m_lineEdit, &QLineEdit::textChanged, this, &LineFieldWidget::checkModified);
0042 
0043   registerWidget();
0044 
0045   if(field_->hasFlag(Data::Field::AllowCompletion)) {
0046     createCompletionObject(field_->name());
0047   }
0048 
0049   if(field_->name() == QLatin1String("isbn")) {
0050     m_lineEdit->setValidator(new ISBNValidator(this));
0051   }
0052 }
0053 
0054 QString LineFieldWidget::text() const {
0055   QString text = m_lineEdit->text();
0056   if(field()->hasFlag(Data::Field::AllowMultiple)) {
0057     text = FieldFormat::fixupValue(text);
0058   }
0059   return text.trimmed();
0060 }
0061 
0062 void LineFieldWidget::setTextImpl(const QString& text_) {
0063   m_lineEdit->setText(text_);
0064 }
0065 
0066 void LineFieldWidget::clearImpl() {
0067   m_lineEdit->clear();
0068   editMultiple(false);
0069 }
0070 
0071 void LineFieldWidget::addCompletionObjectItem(const QString& text_) {
0072   m_lineEdit->completionObject()->addItem(text_);
0073 }
0074 
0075 void LineFieldWidget::updateFieldHook(Tellico::Data::FieldPtr, Tellico::Data::FieldPtr newField_) {
0076   bool wasComplete = m_lineEdit->compObj(); // don't call completionObject() since it recreates it
0077   bool isComplete = (newField_->hasFlag(Data::Field::AllowCompletion));
0078   if(!wasComplete && isComplete) {
0079     createCompletionObject(newField_->name());
0080   } else if(wasComplete && !isComplete) {
0081     // auto-deleted (but re-created if completionObject() is called again)
0082     m_lineEdit->setCompletionObject(nullptr);
0083   }
0084 }
0085 
0086 QWidget* LineFieldWidget::widget() {
0087   return m_lineEdit;
0088 }
0089 
0090 void LineFieldWidget::createCompletionObject(const QString& fieldName_) {
0091   Q_ASSERT(m_lineEdit);
0092   FieldCompletion* completion = new FieldCompletion(true);
0093   completion->setItems(Data::Document::self()->collection()->valuesByFieldName(fieldName_));
0094   completion->setIgnoreCase(true);
0095   m_lineEdit->setCompletionObject(completion);
0096   m_lineEdit->setAutoDeleteCompletionObject(true);
0097 }