File: c2bReferenceList.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 (676 lines) | stat: -rw-r--r-- 24,692 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/***************************************************************************
 *   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 "c2bReferenceList.h"

#include "c2b.h"
#include "c2bBibParser.h"
#include "c2bCoreCiter.h"
#include "c2bRLWebSearchSettings.h"
#include "c2bSettings.h"
#include "c2bTextBrowser.h"
#include "c2bTextEdit.h"
#include "c2bUtils.h"

#include <bibSearcher.h>

#include <QHeaderView>
#include <QMenu>
#include <QScrollBar>
#include <QTimer>


class TreeWidgetItem : public QTreeWidgetItem
{
    // Reimplement sorting
public:
    explicit TreeWidgetItem(const QStringList& strings) : QTreeWidgetItem(strings) {}
    bool operator< (const QTreeWidgetItem& other) const
    {
        const int sortCol(treeWidget()->sortColumn());
        const QString str(c2bUtils::toAscii(text(sortCol), c2bUtils::Collation));
        const QString other_str(c2bUtils::toAscii(other.text(sortCol), c2bUtils::Collation));
        return str < other_str;
    }
};


c2bReferenceList::c2bReferenceList(c2bTextEdit* ed, c2bTextBrowser* br, QWidget* parentw) : QWidget(parentw)
{
    ui.setupUi(this);
    _editorP = ed;
    _browserP = br;
    _citerP = new c2bCoreCiter(this);
    _settingsP = c2bSettingsP;
    _settingsP->setDefaultValue("c2bReferenceList/SearchEngine", "https://www.google.com/search?ie=UTF-8&q=");
    _settingsP->setDefaultValue("c2bReferenceList/SearchEngineAddQuotes", false);
    setupFilterBar();

    _key_index = 0;
    _id_index = 1;
    _year_index = 2;
    _journal_index = 3;
    _author_index = 4;
    _title_index = 5;
    QStringList header;
    header.append(tr("Key"));
    header.append(tr("ID"));
    header.append(tr("Year"));
    header.append(tr("Journal"));
    header.append(tr("Author"));
    header.append(tr("Title"));
    ui.listView->sortItems(_key_index, Qt::AscendingOrder);
    ui.listView->setHeaderLabels(header);
    ui.listView->hideColumn(_id_index);
    ui.listView->setContextMenuPolicy(Qt::ActionsContextMenu);

    citeEntryAction = new QAction(this);
    citeEntryAction->setText(tr("Cite Selected Entries"));
    _settingsP->setKeysequence("Shortcuts/Editor/", citeEntryAction);
    connect(citeEntryAction, SIGNAL(triggered()), this, SLOT(citeEntry()));
    ui.listView->addAction(citeEntryAction);

    c2bUtils::addSeparator(ui.listView);
    openFileAction = new QAction(this);
    openFileAction->setText(tr("Open Document File"));
    openFileAction->setStatusTip(tr("Open document file"));
    _settingsP->setKeysequence("Shortcuts/Editor/", openFileAction);
    connect(openFileAction, SIGNAL(triggered()), this, SLOT(openFile()));
    ui.listView->addAction(openFileAction);

    openUrlAction = new QAction(this);
    openUrlAction->setText(tr("Open Document URL"));
    openUrlAction->setStatusTip(tr("Open document URL"));
    _settingsP->setKeysequence("Shortcuts/Editor/", openUrlAction);
    connect(openUrlAction, SIGNAL(triggered()), this, SLOT(openUrl()));
    ui.listView->addAction(openUrlAction);

    openDOIAction = new QAction(this);
    openDOIAction->setText(tr("Browse Reference by DOI"));
    openDOIAction->setStatusTip(tr("Browse reference by DOI"));
    _settingsP->setKeysequence("Shortcuts/Editor/", openDOIAction);
    connect(openDOIAction, SIGNAL(triggered()), this, SLOT(openDOI()));
    ui.listView->addAction(openDOIAction);

    c2bUtils::addSeparator(ui.listView);
    openAuthorsWebSearchAction = new QAction(this);
    openAuthorsWebSearchAction->setText(tr("Web Search by Author"));
    openAuthorsWebSearchAction->setStatusTip(tr("Web search by Author"));
    _settingsP->setKeysequence("Shortcuts/Editor/", openAuthorsWebSearchAction);
    connect(openAuthorsWebSearchAction, SIGNAL(triggered()), this, SLOT(openAuthorWebSearch()));
    ui.listView->addAction(openAuthorsWebSearchAction);
    openTitleWebSearchAction = new QAction(this);
    openTitleWebSearchAction->setText(tr("Web Search by Title"));
    openTitleWebSearchAction->setStatusTip(tr("Web search by Title"));
    _settingsP->setKeysequence("Shortcuts/Editor/", openTitleWebSearchAction);
    connect(openTitleWebSearchAction, SIGNAL(triggered()), this, SLOT(openTitleWebSearch()));
    ui.listView->addAction(openTitleWebSearchAction);

    c2bUtils::addSeparator(ui.listView);
    webSearchSettingsAction = new QAction(this);
    webSearchSettingsAction->setText(tr("Web Search Settings"));
    webSearchSettingsAction->setStatusTip(tr("Web search settings"));
    _settingsP->setKeysequence("Shortcuts/Editor/", webSearchSettingsAction);
    connect(webSearchSettingsAction, SIGNAL(triggered()), this, SLOT(webSearchSettings()));
    ui.listView->addAction(webSearchSettingsAction);

    c2bUtils::addSeparator(ui.listView);
    clearSelectionsAction = new QAction(this);
    clearSelectionsAction->setText(tr("Clear Entry Selection"));
    clearSelectionsAction->setStatusTip(tr("Clear entry selection"));
    _settingsP->setKeysequence("Shortcuts/Editor/", clearSelectionsAction);
    connect(clearSelectionsAction, SIGNAL(triggered()), this, SLOT(clearSelections()));
    ui.listView->addAction(clearSelectionsAction);

    c2bUtils::addSeparator(ui.listView);
    refreshListAction = new QAction(this);
    refreshListAction->setShortcut(QKeySequence(QKeySequence::Refresh));
    refreshListAction->setText(tr("Refresh List and Browser"));
    refreshListAction->setStatusTip(tr("Refresh list and browser"));
    connect(refreshListAction, SIGNAL(triggered()), this, SLOT(refreshList()));
    ui.listView->addAction(refreshListAction);
    connect(ui.listView, SIGNAL(itemActivated(QTreeWidgetItem*, int)), this, SLOT(goToReference(QTreeWidgetItem*)));

    // Actions dependent on selection and availability
    itemSelectionChanged();
    connect(ui.listView, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelectionChanged()));

    // Browser functionality
    _references_html = c2bUtils::fileToString(":/htm/htm/references.html");
    _references_html.replace("GET_CB2BIB_VERSION_NUMBER", C2B_VERSION);
    _bib_item_html = c2bUtils::fileToString(":/htm/htm/bib_item.html");
    QString references_css(c2bUtils::fileToString(_settingsP->fileName("cb2Bib/BrowserCssFile")));
    if (references_css.isEmpty())
        references_css = c2bUtils::fileToString(":/htm/htm/references.css");
    _browserP->document()->setDefaultStyleSheet(references_css);

    openTextLocalSearchAction = new QAction(this);
    openTextLocalSearchAction->setText(tr("Local Search for Selected Text"));
    openTextLocalSearchAction->setStatusTip(tr("Local search for selected text"));
    _settingsP->setKeysequence("Shortcuts/Editor/", openTextLocalSearchAction);
    connect(openTextLocalSearchAction, SIGNAL(triggered()), this, SLOT(openTextLocalSearch()));
    openTextLocalSearchAction->setEnabled(false);
    connect(_browserP, SIGNAL(copyAvailable(bool)), openTextLocalSearchAction, SLOT(setEnabled(bool)));
    openTextWebSearchAction = new QAction(this);
    openTextWebSearchAction->setText(tr("Web Search for Selected Text"));
    openTextWebSearchAction->setStatusTip(tr("Web search for selected text"));
    _settingsP->setKeysequence("Shortcuts/Editor/", openTextWebSearchAction);
    connect(openTextWebSearchAction, SIGNAL(triggered()), this, SLOT(openTextWebSearch()));
    openTextWebSearchAction->setEnabled(false);
    connect(_browserP, SIGNAL(copyAvailable(bool)), openTextWebSearchAction, SLOT(setEnabled(bool)));

    QList<QAction*> al = ui.listView->actions();
    for (int i = 0; i < 2; ++i)
        _browserP->addAction(al.at(i));
    _browserP->addAction(openTextLocalSearchAction);
    _browserP->addAction(openTextWebSearchAction);
    c2bUtils::addSeparator(_browserP);
    for (int i = 6; i < al.count(); ++i)
        _browserP->addAction(al.at(i));
    _browserP->setContextMenuPolicy(Qt::ActionsContextMenu);

    connect(_browserP, SIGNAL(anchorClicked(const QUrl&)), this, SLOT(openLink(const QUrl&)));
    connect(_browserP, SIGNAL(highlighted(const QString&)), parentw->parent(), SLOT(showMessage(const QString&)));
}

c2bReferenceList::~c2bReferenceList()
{}


void c2bReferenceList::loadList(const QString& txt, bibSearcher* searcher)
{
    QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
    c2bBibParser* bpP = c2b::bibParser();
    QString references_html(_references_html);
    QString references;

    QStringList fields;
    fields.append("abstract");
    fields.append("author");
    fields.append("booktitle");
    fields.append("doi");
    fields.append("editor");
    fields.append("file");
    fields.append("journal");
    fields.append("pages");
    fields.append("title");
    fields.append("url");
    fields.append("volume");
    fields.append("year");
    bibReference ref;
    bpP->initReferenceParsing(_editorP->editorFilename(), fields, &ref);
    const QRegExp initials1("\\b\\w\\b");
    const QRegExp initials2("[^\\w\\s]");
    const QRegExp nonletters("[^\\w\\s-]");
    int ref_counter(0);

    while (bpP->referencesIn(txt, &ref))
    {
        ref_counter++;

        QString author(ref.anyAuthor());
        QString author_long;
        if (!author.isEmpty())
        {
            author = bpP->authorFromBibTeX(author);
            author_long = author;
            author_long.remove(nonletters);
            author_long.replace(" and ", ", ");
            c2bUtils::simplifyString(author_long);
            author_long += '.';
            author.remove(initials1);
            author.remove(initials2);
            author.replace(" and ", ", ");
            c2bUtils::simplifyString(author);
        }

        QString title(ref.anyTitle());
        c2bUtils::cleanTitle(title, true);

        QString doi(ref.value("doi"));
        if (!doi.isEmpty())
            if (!doi.startsWith("http://"))
                doi = "http://dx.doi.org/" + QUrl::toPercentEncoding(doi);

        const QString file(ref.value("file"));
        const QString url(ref.value("url"));
        QString link;
        if (!file.isEmpty())
            link = QDir::cleanPath(file);
        else if (!url.isEmpty())
            link = url;
        else if (!doi.isEmpty())
            link = doi;

        QString ID;
        ID.setNum(ref_counter);
        QString year(ref.value("year"));
        const QString abstract(ref.value("abstract"));
        const QString journal(ref.anyJournal());
        const QString key(ref.citeidName);

        _doiList.append(doi);
        _fileList.append(file);
        _urlList.append(url);
        _positionList.append(ref.positionValue);
        QStringList dataList;
        dataList.append(key);
        dataList.append(ID);
        dataList.append(year);
        dataList.append(journal);
        dataList.append(author);
        dataList.append(title);
        TreeWidgetItem* qi = new TreeWidgetItem(dataList);
        qi->setStatusTip(_journal_index, bpP->fullJournal(journal));
        qi->setStatusTip(_author_index, author_long);
        qi->setStatusTip(_title_index, title);
        ui.listView->insertTopLevelItem(0, qi);

        QString pages(ref.value("pages"));
        const QString volume(ref.value("volume"));
        if (!pages.isEmpty())
        {
            if (!volume.isEmpty())
                pages = ", " + pages;
            else
                pages = ' ' + pages;
        }
        if (!year.isEmpty())
            year = '(' + year + ").";
        QString bib_item_html(_bib_item_html);
        bib_item_html.replace("GET_REFERENCE_ANCHOR_ID", ID);
        bib_item_html.replace("GET_REFERENCE_ANCHOR_NAME", ID);
        bib_item_html.replace("GET_REFERENCE_AUTHOR", author_long);
        bib_item_html.replace("GET_REFERENCE_LINK", link);
        bib_item_html.replace("GET_REFERENCE_PAGES", pages);
        bib_item_html.replace("GET_REFERENCE_SOURCE", journal);
        bib_item_html.replace("GET_REFERENCE_TITLE", c2bUtils::toHtmlString(title));
        bib_item_html.replace("GET_REFERENCE_VOLUME", volume);
        bib_item_html.replace("GET_REFERENCE_YEAR", year);
        if (searcher)
            bib_item_html.replace("GET_REFERENCE_ABSTRACT", searcher->highlight(abstract) + searcher->hitValue(ref.rawReference));
        else
            bib_item_html.replace("GET_REFERENCE_ABSTRACT", c2bUtils::toHtmlString(abstract));
        references += bib_item_html;
    }

    ui.listView->sortItems(ui.listView->sortColumn(), ui.listView->header()->sortIndicatorOrder());
    ui.listView->resizeColumnToContents(_year_index);
    references_html.replace("GET_REFERENCES", references);
    _browserP->updateHtml(references_html);
    QApplication::restoreOverrideCursor();
    c2b::showMessage(tr("Parsed %1 references.").arg(ref_counter));
}

void c2bReferenceList::citeEntry()
{
    QStringList keys;
    QTreeWidgetItemIterator it(ui.listView, QTreeWidgetItemIterator::Selected | QTreeWidgetItemIterator::NotHidden);
    while (*it)
    {
        if (!(*it)->text(_key_index).isEmpty())
            keys.append((*it)->text(_key_index));
        ++it;
    }
    _citerP->cite(keys);
}

void c2bReferenceList::openDOI()
{
    bool zdoi(false);
    QTreeWidgetItemIterator it(ui.listView, QTreeWidgetItemIterator::Selected | QTreeWidgetItemIterator::NotHidden);
    while (*it)
    {
        const QString doi(_doiList.at((*it)->text(_id_index).toInt() - 1));
        if (!doi.isEmpty())
        {
            zdoi = true;
            c2bUtils::openFile(doi, parentWidget());
        }
        ++it;
    }
    if (!zdoi)
        c2b::showMessage(tr("No DOI entry among selected references."));
}

void c2bReferenceList::openFile()
{
    bool zfile(false);
    QTreeWidgetItemIterator it(ui.listView, QTreeWidgetItemIterator::Selected | QTreeWidgetItemIterator::NotHidden);
    while (*it)
    {
        const QString file(_fileList.at((*it)->text(_id_index).toInt() - 1));
        if (!file.isEmpty())
        {
            zfile = true;
            c2bUtils::openFile(file, parentWidget());
        }
        ++it;
    }
    if (!zfile)
        c2b::showMessage(tr("No file entry among selected references."));
}

void c2bReferenceList::openUrl()
{
    bool zurl(false);
    QTreeWidgetItemIterator it(ui.listView, QTreeWidgetItemIterator::Selected | QTreeWidgetItemIterator::NotHidden);
    while (*it)
    {
        const QString url(_urlList.at((*it)->text(_id_index).toInt() - 1));
        if (!url.isEmpty())
        {
            zurl = true;
            c2bUtils::openFile(url, parentWidget());
        }
        ++it;
    }
    if (!zurl)
        c2b::showMessage(tr("No URL entry among selected references."));
}

void c2bReferenceList::openLink(const QUrl& ln)
{
    QString ln_str(ln.toString());
    if (ln_str.isEmpty())
        c2b::showMessage(tr("No link available for this reference."));
    else if (ln_str.startsWith("Select reference "))
        scrollToReference(ln_str.remove(QRegExp("\\D")));
    else if (ln_str.startsWith("Edit reference"))
        emit openFile(ln_str.replace(QRegExp("^.+\\[(.+)\\]$"), "\\1"));
    else
        c2bUtils::openFile(ln_str, parentWidget());
}

void c2bReferenceList::openAuthorWebSearch()
{
    const QString searchEngine(_settingsP->value("c2bReferenceList/SearchEngine").toString());
    bool zwebs(false);
    QTreeWidgetItemIterator it(ui.listView, QTreeWidgetItemIterator::Selected | QTreeWidgetItemIterator::NotHidden);
    while (*it)
    {
        if (!(*it)->text(_author_index).isEmpty())
        {
            zwebs = true;
            QString webs((*it)->text(_author_index));
            webs.remove(',');
            webs = searchEngine + webs.simplified();
            c2bUtils::openFile(webs, parentWidget());
        }
        ++it;
    }
    if (!zwebs)
        c2b::showMessage(tr("No author entry among selected references."));
}

void c2bReferenceList::openTitleWebSearch()
{
    const QString searchEngine(_settingsP->value("c2bReferenceList/SearchEngine").toString());
    const bool addQuotes(_settingsP->value("c2bReferenceList/SearchEngineAddQuotes").toBool());
    bool zwebs(false);
    QTreeWidgetItemIterator it(ui.listView, QTreeWidgetItemIterator::Selected | QTreeWidgetItemIterator::NotHidden);
    while (*it)
    {
        if (!(*it)->text(_title_index).isEmpty())
        {
            zwebs = true;
            QString webs((*it)->text(_title_index));
            if (addQuotes)
                webs = '\"' + webs + '\"';
            webs = searchEngine + webs;
            c2bUtils::openFile(webs, parentWidget());
        }
        ++it;
    }
    if (!zwebs)
        c2b::showMessage(tr("No title entry among selected references."));
}

void c2bReferenceList::openTextWebSearch()
{
    const QString searchEngine(_settingsP->value("c2bReferenceList/SearchEngine").toString());
    QString webs(_browserP->textCursor().selectedText());
    if (_settingsP->value("c2bReferenceList/SearchEngineAddQuotes").toBool())
        webs = '\"' + webs + '\"';
    webs = searchEngine + webs;
    c2bUtils::openFile(webs, parentWidget());
}

void c2bReferenceList::webSearchSettings()
{
    c2bRLWebSearchSettings searchSettings(this);
    searchSettings.exec();
}

void c2bReferenceList::openTextLocalSearch()
{
    const QString text(_browserP->textCursor().selectedText());
    const QString fn(_editorP->editorFilename());
    c2b::doSearchInFiles(text, fn);
}

void c2bReferenceList::refreshList()
{
    _doiList.clear();
    _fileList.clear();
    _urlList.clear();
    _positionList.clear();
    ui.listView->clear();
    ui.comboFilter->clearEditText();
    loadList(_editorP->toPlainText());
}

void c2bReferenceList::goToReference(const QString& id)
{
    QTextCursor tc(_editorP->textCursor());
    tc.setPosition(_positionList.at(id.toInt() - 1));
    _editorP->setTextCursor(tc);
    _editorP->verticalScrollBar()->setValue(_editorP->verticalScrollBar()->maximum());
    _editorP->ensureCursorVisible();

    _browserP->scrollToAnchor(id);
    int scrollPosition(_browserP->verticalScrollBar()->value());
    _browserP->verticalScrollBar()->setValue(scrollPosition - 10);
}

void c2bReferenceList::goToReference(const int position)
{
    // Maps position to internal reference id. It is needed by the search browser, when a
    // citation is edited in the original file. If this file is changed multiple times,
    // positions in the search browser get outdated, so the mapping position-id is inexact.
    // Thus, search for the closet reference.
    int ip(0);
    int pdiff_min(1000000);
    for (int i = 0; i < _positionList.count(); ++i)
    {
        int p(_positionList.at(i));
        if (p == position)
        {
            ip = i;
            break;
        }
        int pdiff(abs(p - position));
        if (pdiff < pdiff_min)
        {
            pdiff_min = pdiff;
            ip = i;
        }
    }
    QString id;
    goToReference(id.setNum(ip + 1));
}

void c2bReferenceList::goToReference(QTreeWidgetItem* ref)
{
    if (ref == 0)
        return;
    const QString id(ref->text(_id_index));
    goToReference(id);
}

void c2bReferenceList::scrollToReference(const QString& id)
{
    if (id.isEmpty())
        return;
    QList<QTreeWidgetItem*> items = ui.listView->findItems(id, Qt::MatchExactly, 1);
    QTreeWidgetItem* item = items.at(0);
    ui.listView->setFocus();
    item->setSelected(!item->isSelected());
    ui.listView->scrollToItem(item);

    QTextCursor tc(_editorP->textCursor());
    tc.setPosition(_positionList.at(id.toInt() - 1));
    _editorP->setTextCursor(tc);
    _editorP->verticalScrollBar()->setValue(_editorP->verticalScrollBar()->maximum());
    _editorP->ensureCursorVisible();
}

void c2bReferenceList::clearSelections()
{
    disconnect(ui.listView, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelectionChanged()));
    QTreeWidgetItemIterator it(ui.listView, QTreeWidgetItemIterator::Selected);
    while (*it)
    {
        (*it)->setSelected(false);
        ++it;
    }
    itemSelectionChanged();
    connect(ui.listView, SIGNAL(itemSelectionChanged()), this, SLOT(itemSelectionChanged()));
}

void c2bReferenceList::itemSelectionChanged()
{
    int entries(0);
    int doi(0);
    int file(0);
    int url(0);
    int author(0);
    int title(0);
    QTreeWidgetItemIterator it(ui.listView, QTreeWidgetItemIterator::Selected | QTreeWidgetItemIterator::NotHidden);
    while (*it)
    {
        if (!_doiList.at((*it)->text(_id_index).toInt() - 1).isEmpty())
            ++doi;
        if (!_fileList.at((*it)->text(_id_index).toInt() - 1).isEmpty())
            ++file;
        if (!_urlList.at((*it)->text(_id_index).toInt() - 1).isEmpty())
            ++url;
        if (!(*it)->text(_author_index).isEmpty())
            ++author;
        if (!(*it)->text(_title_index).isEmpty())
            ++title;
        ++entries;
        ++it;
    }
    citeEntryAction->setEnabled(entries > 0);
    citeEntryAction->setStatusTip(tr("Cite selected entries. %1 Entries selected.").arg(entries));
    openDOIAction->setEnabled(doi > 0);
    openDOIAction->setStatusTip(tr("Browse reference by DOI. %1 DOIs in selected entries.").arg(doi));
    openFileAction->setEnabled(file > 0);
    openFileAction->setStatusTip(tr("Open document file. %1 Files in selected entries.").arg(file));
    openUrlAction->setEnabled(url > 0);
    openUrlAction->setStatusTip(tr("Open document URL. %1 URLs in selected entries.").arg(url));
    openAuthorsWebSearchAction->setEnabled(author > 0);
    openAuthorsWebSearchAction->setStatusTip(tr("Web search by Author. %1 Entries selected.").arg(author));
    openTitleWebSearchAction->setEnabled(title > 0);
    openTitleWebSearchAction->setStatusTip(tr("Web search by Title. %1 Entries selected.").arg(title));
    emit hasSelectedItems(entries > 0);
}

QStringList c2bReferenceList::selectedKeys() const
{
    QStringList keys;
    QTreeWidgetItemIterator it(ui.listView, QTreeWidgetItemIterator::Selected | QTreeWidgetItemIterator::NotHidden);
    while (*it)
    {
        if (!(*it)->text(_key_index).isEmpty())
            keys.append((*it)->text(_key_index));
        ++it;
    }
    return keys;
}

void c2bReferenceList::setLyXPipe()
{
    _citerP->setLyXPipe();
}


/***************************************************************************
    Filter Bar
 ***************************************************************************/

void c2bReferenceList::setupFilterBar()
{
    const QStringList types(searchPattern::types());
    _filter_mode = _settingsP->value("c2bReferenceList/FilterMode", types.first()).toString();
    QActionGroup* filterMode = new QActionGroup(this);
    filterMode->setExclusive(true);
    for (int i = 0; i < types.count(); ++i)
    {
        const QString& t = types.at(i);
        QAction* act = filterMode->addAction(t);
        act->setCheckable(true);
        if (_filter_mode == t) act->setChecked(true);
    }
    QMenu* filterModeM = new QMenu(this);
    filterModeM->addActions(filterMode->actions());
    ui.filterB->setMenu(filterModeM);
    connect(filterMode, SIGNAL(triggered(QAction*)), this, SLOT(setFilterMode(QAction*)));
    connect(ui.comboFilter, SIGNAL(editTextChanged(const QString&)), this, SLOT(blockFilterSignals()));
}

void c2bReferenceList::setFilterMode(QAction* mode)
{
    _filter_mode = mode->text();
    _settingsP->setValue("c2bReferenceList/FilterMode", _filter_mode);
    filterList();
}

void c2bReferenceList::blockFilterSignals()
{
    ui.comboFilter->blockSignals(true);
    QTimer::singleShot(500, this, SLOT(filterList()));
}

void c2bReferenceList::filterList()
{
    const QString text(ui.comboFilter->currentText());
    if (text.isEmpty())
    {
        QTreeWidgetItemIterator it(ui.listView);
        while (*it)
        {
            (*it)->setHidden(false);
            ++it;
        }
    }
    else
    {
        searchPattern pattern(text, _filter_mode);
        QTreeWidgetItemIterator it(ui.listView);
        while (*it)
        {
            QTreeWidgetItem* item = *it;
            if (pattern.matches(item->text(_author_index)) || pattern.matches(item->text(_title_index)))
                item->setHidden(false);
            else
                item->setHidden(true);
            ++it;
        }
        QTimer::singleShot(5000, this, SLOT(keepFilterPattern()));
    }
    itemSelectionChanged();
    ui.comboFilter->blockSignals(false);
}

void c2bReferenceList::keepFilterPattern()
{
    const QString text(ui.comboFilter->currentText());
    if (!text.isEmpty())
        if (ui.comboFilter->findText(text) < 0)
            ui.comboFilter->addItem(text);
}