File: c2bBibParser.cpp

package info (click to toggle)
cb2bib 1.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,368 kB
  • sloc: cpp: 24,179; sh: 481; makefile: 16
file content (194 lines) | stat: -rw-r--r-- 7,087 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/***************************************************************************
 *   Copyright (C) 2004-2015 by Pere Constans
 *   constans@molspaces.com
 *   cb2Bib version 1.9.2. Licensed under the GNU GPL version 3.
 *   See the LICENSE file that comes with this distribution.
 ***************************************************************************/
#include "c2bBibParser.h"

#include "c2b.h"
#include "c2bSettings.h"

#include <authorString.h>
#include <bibPreparser.h>

#include <QComboBox>
#include <QMessageBox>


c2bBibParser::c2bBibParser(QObject* parento) : bibParser(parento)
{
    _c2b_mainP = c2b::mainWidget();
    _bp_updates_gui = false;

    // Warn users if journal database file is not setup
    const QString journal_f(_settingsP->fileName("cb2Bib/JournalFile"));
    if (journal_f.isEmpty())
        QMessageBox::information(_c2b_mainP, tr("Information - cb2Bib"),
                                 tr("No Abbreviation Journal file has been specified.\n\n"
                                    "Note: Abbreviation files are specified through the cb2Bib Configure dialog."),
                                 QMessageBox::Ok);
    else
    {
        QFile f(journal_f);
        if (!f.open(QIODevice::ReadOnly | QIODevice::Text))      // jdbfile.exists wouldn't produce an errorString
            QMessageBox::warning(_c2b_mainP, tr("Warning - cb2Bib"),
                                 tr("Unable to open the Journal Abbreviation file %1 for reading.\nError: '%2'.\n\n"
                                    "Note: Abbreviation files are specified through the cb2Bib Configure dialog.")
                                 .arg(journal_f).arg(f.errorString()), QMessageBox::Ok);
        else
            f.close();
    }

    // Set status messages
    connect(this, SIGNAL(statusMessage(const QString&)), c2b::instance(), SIGNAL(statusMessage(const QString&)));

    // Set preparser log
    connect(_preparserP, SIGNAL(preparserLogAvailable(const QString&)), this, SLOT(showLog(const QString&)));
}

c2bBibParser::~c2bBibParser()
{}


void c2bBibParser::currentReferenceUpdated()
{
    // Current reference has been updated by the base class.
    // Update it in the GUI
    _bp_updates_gui = true;
    bibReferenceIterator i;
    for (i = _current_reference.constBegin(); i != _current_reference.constEnd(); ++i)
        _field_editors[i.key()]->setText(i.value());
    // Set default type to article
    if (_current_reference.typeName.isEmpty())
        _current_reference.typeName = "article";
    setReferenceType(_current_reference.typeName);
    setCiteID();
    _bp_updates_gui = false;
    emit bibModified(fieldCount() > 0);
}

void c2bBibParser::clearCurrentReference()
{
    _bp_updates_gui = true;
    // Clear by setting empty QString as a workaround for bug
    // http://bugreports.qt.nokia.com/browse/QTBUG-7866
    // Affects versions 4.6.x, expected to be fixed in 4.6.3
    for (int i = 0; i < _bibliographic_fields.count(); ++i)
        _field_editors[_bibliographic_fields.at(i)]->setText(QString());
//        _field_editors[_bibliographic_fields.at(i)]->clear();
    _field_editors["id"]->setText(QString());
//    _field_editors["id"]->clear();
    bibParser::clearCurrentReference();
    _current_reference.typeName = "article";
    _type_editor->setCurrentIndex(1);
    _bp_updates_gui = false;
    emit bibModified(false);
}

void c2bBibParser::referenceEdited(const QString& value)
{
    if (_bp_updates_gui)
        return;
    if (sender())
    {
        const QString name(sender()->objectName());
        if (name == "id")
        {
            _current_reference.citeidName = value;
            emit setDocumentFilename();
        }
        else if (name == "typecombo")
            _current_reference.typeName = value;
        else if (name == "file")
            _current_reference[name] = value;
        else
        {
            // Attention: No name checking is done.
            // Only reference editors must be connected here.
            _current_reference[name] = value;
            setCiteID();
            emit setDocumentFilename();
        }
        emit bibModified(fieldCount() > 0);
    }
}

void c2bBibParser::setCiteID()
{
    bibParser::setCiteID();
    _field_editors["id"]->setText(_current_reference.citeidName);
}

void c2bBibParser::setField()
{
    if (sender())
    {
        const QString name(sender()->objectName());
        if (_field_editors.contains(name))
            _field_editors[name]->setText(parse(name, _field_editors.value(name)->text()));
    }
}

void c2bBibParser::setReferenceType(const QString& type)
{
    int i(0);
    while (i < _type_editor->count())
    {
        if (_type_editor->itemText(i) == type)
        {
            _type_editor->setCurrentIndex(i);
            return;
        }
        ++i;
    }
    _type_editor->setCurrentIndex(0);
    _type_editor->setItemText(0, type);
}

void c2bBibParser::setWidgetMapping()
{
    // Setting Reference Types
    _type_editor->insertItems(0, _bibliographic_types);
    _type_editor->setCurrentIndex(1);
    _current_reference.typeName = "article";

    // Tracking Field Editor Line changes
    // Track using textChanged instead of editTextChanged. The latter does not track line clears
    for (int i = 0; i < _bibliographic_fields.count(); ++i)
    {
        const QString& name = _bibliographic_fields.at(i);
        connect(_field_editors.value(name), SIGNAL(returnPressed()), this, SLOT(setField()));
        connect(_field_editors.value(name), SIGNAL(textChanged(const QString&)), this, SLOT(referenceEdited(const QString&)));
    }
    connect(_field_editors.value("id"), SIGNAL(textChanged(const QString&)), this, SLOT(referenceEdited(const QString&)));
    connect(_type_editor, SIGNAL(editTextChanged(const QString&)), this, SLOT(referenceEdited(const QString&)));
}

void c2bBibParser::showLog(const QString& log)
{
    if (!_preparser_logP)
        _preparser_logP = new c2bBibPreparserLog(_c2b_mainP);
    _preparser_logP->append(log);
    _preparser_logP->show(); // Don't use exec(), it could be a recursive call if clipboard changed
}

void c2bBibParser::checkRegExpFile(const QString& fn)
{
    if (fn.isEmpty())
    {
        QMessageBox::information(_c2b_mainP, tr("Information - cb2Bib"),
                                 tr("No RegExp filename has been specified.\n\n"
                                    "Note: RegExp files are specified through the cb2Bib Configure dialog."), QMessageBox::Ok);
        return;
    }
    QFile f(fn);
    if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
        QMessageBox::warning(_c2b_mainP, tr("Warning - cb2Bib"),
                             tr("Unable to open the RegExp file %1 for reading.\nError: '%2'.\n\n"
                                "Note: RegExp files are specified through the cb2Bib Configure dialog. "
                                "After manual matching, patterns can be edited and stored in a "
                                "RegExp file for future autodetection.").arg(fn).arg(f.errorString()), QMessageBox::Ok);
    else
        f.close();
}