"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/c2bBibParser.cpp" (12 Feb 2021, 7101 Bytes) of package /linux/privat/cb2bib-2.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "c2bBibParser.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.0.0_vs_2.0.1.

    1 /***************************************************************************
    2  *   Copyright (C) 2004-2021 by Pere Constans
    3  *   constans@molspaces.com
    4  *   cb2Bib version 2.0.1. Licensed under the GNU GPL version 3.
    5  *   See the LICENSE file that comes with this distribution.
    6  ***************************************************************************/
    7 #include "c2bBibParser.h"
    8 
    9 #include "c2b.h"
   10 #include "c2bSettings.h"
   11 
   12 #include <bibPreparser.h>
   13 
   14 #include <QComboBox>
   15 #include <QMessageBox>
   16 
   17 
   18 c2bBibParser::c2bBibParser(QObject* parento) : bibParser(parento)
   19 {
   20     _c2b_mainP = c2b::mainWidget();
   21     _bp_updates_gui = false;
   22 
   23     // Warn users if journal database file is not setup
   24     const QString journal_f(_settingsP->fileName("cb2Bib/JournalFile"));
   25     if (journal_f.isEmpty())
   26         QMessageBox::information(_c2b_mainP, tr("Information - cb2Bib"),
   27                                  tr("No Abbreviation Journal file has been specified.\n\n"
   28                                     "Note: Abbreviation files are specified through the cb2Bib Configure dialog."),
   29                                  QMessageBox::Ok);
   30     else
   31     {
   32         QFile f(journal_f);
   33         if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) // jdbfile.exists wouldn't produce an errorString
   34             QMessageBox::warning(_c2b_mainP, tr("Warning - cb2Bib"),
   35                                  tr("Unable to open the Journal Abbreviation file %1 for reading.\nError: '%2'.\n\n"
   36                                     "Note: Abbreviation files are specified through the cb2Bib Configure dialog.")
   37                                  .arg(journal_f, f.errorString()),
   38                                  QMessageBox::Ok);
   39         else
   40             f.close();
   41     }
   42 
   43     // Set status messages
   44     connect(this, SIGNAL(statusMessage(QString)), c2b::instance(), SIGNAL(statusMessage(QString)));
   45 
   46     // Set preparser log
   47     connect(_preparserP, SIGNAL(preparserLogAvailable(QString)), this, SLOT(showLog(QString)));
   48 }
   49 
   50 c2bBibParser::~c2bBibParser() {}
   51 
   52 
   53 void c2bBibParser::currentReferenceUpdated()
   54 {
   55     // Current reference has been updated by the base class.
   56     // Update it in the GUI
   57     _bp_updates_gui = true;
   58     bibReferenceIterator i;
   59     for (i = _current_reference.constBegin(); i != _current_reference.constEnd(); ++i)
   60         _field_editors[i.key()]->setText(i.value());
   61     // Set default type to article
   62     if (_current_reference.typeName.isEmpty())
   63         _current_reference.typeName = "article";
   64     setReferenceType(_current_reference.typeName);
   65     setCiteID();
   66     _bp_updates_gui = false;
   67     emit bibModified(fieldCount() > 0);
   68 }
   69 
   70 void c2bBibParser::clearCurrentReference()
   71 {
   72     _bp_updates_gui = true;
   73     // Clear by setting empty QString as a workaround for bug
   74     // http://bugreports.qt.nokia.com/browse/QTBUG-7866
   75     // Affects versions 4.6.x, expected to be fixed in 4.6.3
   76     for (int i = 0; i < _bibliographic_fields.count(); ++i)
   77         _field_editors[_bibliographic_fields.at(i)]->setText(QString());
   78     // _field_editors[_bibliographic_fields.at(i)]->clear();
   79     _field_editors["id"]->setText(QString());
   80     // _field_editors["id"]->clear();
   81     bibParser::clearCurrentReference();
   82     _current_reference.typeName = "article";
   83     _type_editor->setCurrentIndex(1);
   84     _bp_updates_gui = false;
   85     emit bibModified(false);
   86 }
   87 
   88 void c2bBibParser::referenceEdited(const QString& value)
   89 {
   90     if (_bp_updates_gui)
   91         return;
   92     if (sender())
   93     {
   94         const QString name(sender()->objectName());
   95         if (name == "id")
   96         {
   97             _current_reference.citeidName = value;
   98             emit setDocumentFilename();
   99         }
  100         else if (name == "typecombo")
  101             _current_reference.typeName = value;
  102         else if (name == "file")
  103             _current_reference[name] = value;
  104         else
  105         {
  106             // Attention: No name checking is done.
  107             // Only reference editors must be connected here.
  108             _current_reference[name] = value;
  109             setCiteID();
  110             emit setDocumentFilename();
  111         }
  112         emit bibModified(fieldCount() > 0);
  113     }
  114 }
  115 
  116 void c2bBibParser::setCiteID()
  117 {
  118     bibParser::setCiteID();
  119     _field_editors["id"]->setText(_current_reference.citeidName);
  120 }
  121 
  122 void c2bBibParser::setField()
  123 {
  124     if (sender())
  125     {
  126         const QString name(sender()->objectName());
  127         if (_field_editors.contains(name))
  128             _field_editors[name]->setText(parse(name, _field_editors.value(name)->text()));
  129     }
  130 }
  131 
  132 void c2bBibParser::setReferenceType(const QString& type)
  133 {
  134     int i(0);
  135     while (i < _type_editor->count())
  136     {
  137         if (_type_editor->itemText(i) == type)
  138         {
  139             _type_editor->setCurrentIndex(i);
  140             return;
  141         }
  142         ++i;
  143     }
  144     _type_editor->setCurrentIndex(0);
  145     _type_editor->setItemText(0, type);
  146 }
  147 
  148 void c2bBibParser::setWidgetMapping()
  149 {
  150     // Setting Reference Types
  151     _type_editor->insertItems(0, _bibliographic_types);
  152     _type_editor->setCurrentIndex(1);
  153     _current_reference.typeName = "article";
  154 
  155     // Tracking Field Editor Line changes
  156     // Track using textChanged instead of editTextChanged. The latter does not track line clears
  157     for (int i = 0; i < _bibliographic_fields.count(); ++i)
  158     {
  159         const QString& name = _bibliographic_fields.at(i);
  160         connect(_field_editors.value(name), SIGNAL(returnPressed()), this, SLOT(setField()));
  161         connect(_field_editors.value(name), SIGNAL(textChanged(QString)), this, SLOT(referenceEdited(QString)));
  162     }
  163     connect(_field_editors.value("id"), SIGNAL(textChanged(QString)), this, SLOT(referenceEdited(QString)));
  164     connect(_type_editor, SIGNAL(editTextChanged(QString)), this, SLOT(referenceEdited(QString)));
  165 }
  166 
  167 void c2bBibParser::showLog(const QString& log)
  168 {
  169     if (!_preparser_logP)
  170         _preparser_logP = new c2bBibPreparserLog(_c2b_mainP);
  171     _preparser_logP->append(log);
  172     _preparser_logP->show(); // Don't use exec(), it could be a recursive call if clipboard changed
  173 }
  174 
  175 void c2bBibParser::checkRegExpFile(const QString& fn)
  176 {
  177     if (fn.isEmpty())
  178     {
  179         QMessageBox::information(_c2b_mainP, tr("Information - cb2Bib"),
  180                                  tr("No RegExp filename has been specified.\n\n"
  181                                     "Note: RegExp files are specified through the cb2Bib Configure dialog."),
  182                                  QMessageBox::Ok);
  183         return;
  184     }
  185     QFile f(fn);
  186     if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
  187         QMessageBox::warning(_c2b_mainP, tr("Warning - cb2Bib"),
  188                              tr("Unable to open the RegExp file %1 for reading.\nError: '%2'.\n\n"
  189                                 "Note: RegExp files are specified through the cb2Bib Configure dialog. "
  190                                 "After manual matching, patterns can be edited and stored in a "
  191                                 "RegExp file for future autodetection.")
  192                              .arg(fn, f.errorString()),
  193                              QMessageBox::Ok);
  194     else
  195         f.close();
  196 }