"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/c2bSearchInFiles.cpp" (12 Feb 2021, 7714 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 "c2bSearchInFiles.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 "c2bSearchInFiles.h"
    8 
    9 #include "c2b.h"
   10 #include "c2bUtils.h"
   11 
   12 #include <bibSearcher.h>
   13 
   14 #include <time.h>
   15 
   16 
   17 c2bSearchInFiles::c2bSearchInFiles(const QString& file, QWidget* parentw) : QDialog(parentw)
   18 {
   19     ui.setupUi(this);
   20     setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
   21     // Set blank title to columns 0 and 4. Qt 4.4.0 needs it, otherwise it takes column number as default title.
   22     // Recheck whenever ui files are generated for 4.4 instead of current 4.2.
   23     ui.patternList->headerItem()->setText(0, QApplication::translate("c2bSearchInFiles", "", 0));
   24     ui.patternList->headerItem()->setText(4, QApplication::translate("c2bSearchInFiles", "", 0));
   25 
   26     for (int i = 0; i < ITEMS_IN_SEARCH_PATTERN; ++i)
   27         ui.patternList->resizeColumnToContents(i);
   28     ui.buttonBox->button(QDialogButtonBox::Abort)->setAutoDefault(false);
   29     ui.buttonBox->button(QDialogButtonBox::Abort)->setDefault(false);
   30     ui.buttonBox->button(QDialogButtonBox::Abort)->setEnabled(false);
   31     ui.buttonBox->button(QDialogButtonBox::Close)->setAutoDefault(false);
   32     ui.buttonBox->button(QDialogButtonBox::Close)->setDefault(false);
   33     _searchB = new QPushButton(tr("Search"));
   34     ui.buttonBox->addButton(_searchB, QDialogButtonBox::ActionRole);
   35     _searchB->setAutoDefault(true);
   36     _searchB->setDefault(true);
   37     _searchB->setEnabled(false);
   38 
   39     _bibtex_file.clear();
   40     _bibtex_dir.clear();
   41     setBibTeXFile(file);
   42 
   43     connect(_searchB, SIGNAL(clicked()), this, SLOT(searchB_clicked()));
   44     connect(ui.allBibFiles, SIGNAL(clicked()), this, SLOT(setBibTeXFile()));
   45     connect(ui.buttonBox, SIGNAL(helpRequested()), this, SLOT(help()));
   46     connect(ui.pattern, SIGNAL(patternAvailable(bool)), this, SLOT(updateForm()));
   47     connect(ui.patternList, SIGNAL(itemActivated(QTreeWidgetItem*,int)), this, SLOT(on_editPatternB_clicked()));
   48     connect(ui.patternList, SIGNAL(itemClicked(QTreeWidgetItem*,int)), this, SLOT(updateForm()));
   49 }
   50 
   51 c2bSearchInFiles::~c2bSearchInFiles() {}
   52 
   53 
   54 void c2bSearchInFiles::updateForm()
   55 {
   56     ui.addPatternB->setEnabled(ui.pattern->isAvailable());
   57     ui.Boolean->setEnabled((ui.patternList->topLevelItemCount() > 1) ||
   58                            (ui.pattern->isAvailable() && ui.patternList->topLevelItemCount() > 0));
   59     _searchB->setEnabled(!_bibtex_dir.isEmpty() &&
   60                          (ui.pattern->isAvailable() || ui.patternList->topLevelItemCount() > 0));
   61     if (ui.patternList->currentItem())
   62     {
   63         ui.deletePatternB->setEnabled(true);
   64         ui.editPatternB->setEnabled(true);
   65     }
   66     else
   67     {
   68         ui.deletePatternB->setEnabled(false);
   69         ui.editPatternB->setEnabled(false);
   70     }
   71     ui.searchLog->setText(_search_log);
   72 }
   73 
   74 void c2bSearchInFiles::on_addPatternB_clicked()
   75 {
   76     new QTreeWidgetItem(ui.patternList, ui.pattern->patterns());
   77     for (int i = 0; i < ITEMS_IN_SEARCH_PATTERN; ++i)
   78         ui.patternList->resizeColumnToContents(i);
   79     ui.pattern->clear();
   80     updateForm();
   81 }
   82 
   83 void c2bSearchInFiles::on_deletePatternB_clicked()
   84 {
   85     QTreeWidgetItem* item = ui.patternList->currentItem();
   86     delete item;
   87     item = ui.patternList->currentItem();
   88     if (item)
   89         ui.patternList->currentItem()->setSelected(true);
   90     updateForm();
   91 }
   92 
   93 void c2bSearchInFiles::on_editPatternB_clicked()
   94 {
   95     QDialog editor(this);
   96     ui_editor.setupUi(&editor);
   97     QTreeWidgetItem* item = ui.patternList->currentItem();
   98     ui_editor.pattern->setPattern(item);
   99     connect(ui_editor.pattern, SIGNAL(patternAvailable(bool)), ui_editor.buttonBox->button(QDialogButtonBox::Ok),
  100             SLOT(setEnabled(bool)));
  101     if (editor.exec() == QDialog::Accepted)
  102     {
  103         const QStringList pattern(ui_editor.pattern->patterns());
  104         for (int i = 0; i < ITEMS_IN_SEARCH_PATTERN; ++i)
  105             item->setText(i, pattern.at(i));
  106         for (int i = 0; i < ITEMS_IN_SEARCH_PATTERN; ++i)
  107             ui.patternList->resizeColumnToContents(i);
  108     }
  109 }
  110 
  111 void c2bSearchInFiles::searchB_clicked()
  112 {
  113     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
  114     _searchB->setEnabled(false);
  115     ui.buttonBox->button(QDialogButtonBox::Abort)->setEnabled(true);
  116 
  117     ui.searchLog->setText(_search_log);
  118     bibSearcher searcher(c2b::bibParser());
  119     connect(ui.buttonBox->button(QDialogButtonBox::Abort), SIGNAL(clicked()), &searcher, SLOT(abort()));
  120     searcher.setSearchScope(_bibtex_file, _bibtex_dir, ui.allBibFiles->isChecked(), ui.documentFiles->isChecked());
  121     searcher.setSimplifySource(ui.simplifySource->isChecked());
  122     searcher.setBoolean(ui.Boolean->currentText() == "All");
  123     if (ui.addPatternB->isEnabled())
  124     {
  125         const QStringList p(ui.pattern->patterns());
  126         searcher.addPattern(p.at(0) == "NOT", p.at(1) == "Sensitive", p.at(2), p.at(3), p.at(4).at(0), p.at(5));
  127     }
  128     QTreeWidgetItemIterator it(ui.patternList);
  129     while (*it)
  130     {
  131         QStringList p;
  132         for (int i = 0; i < ITEMS_IN_SEARCH_PATTERN; ++i)
  133             p.append((*it)->text(i));
  134         searcher.addPattern(p.at(0) == "NOT", p.at(1) == "Sensitive", p.at(2), p.at(3), p.at(4).at(0), p.at(5));
  135         ++it;
  136     }
  137     if (searcher.patternsCount() == 0)
  138         return;
  139 
  140     const unsigned long start_time(clock());
  141 
  142     searcher.exec();
  143 
  144     const double time(double(clock() - start_time) / double(CLOCKS_PER_SEC));
  145 
  146     QString search_status;
  147     if (searcher.hitsCount() > 0 || searcher.errorsCount() > 0)
  148     {
  149         if (searcher.hitsCount() > 0)
  150             search_status = tr("Found %1 hits from %2 references (%3s)")
  151                             .arg(searcher.hitsCount())
  152                             .arg(searcher.referencesCount())
  153                             .arg(time, 0, 'g', 3);
  154         else
  155             ui.searchLog->setText(_search_log);
  156         QApplication::processEvents();
  157         emit searchDone(_bibtex_dir, &searcher);
  158     }
  159     else if (searcher.errorsCount() < 0)
  160         search_status = tr("Search aborted");
  161     else
  162         search_status = tr("No reference found (%1s)").arg(time, 0, 'g', 3);
  163     if (!search_status.isEmpty())
  164     {
  165         ui.searchLog->setText(search_status);
  166         c2b::showMessage(search_status);
  167     }
  168     ui.buttonBox->button(QDialogButtonBox::Abort)->setEnabled(false);
  169     _searchB->setEnabled(true);
  170     QApplication::restoreOverrideCursor();
  171 }
  172 
  173 void c2bSearchInFiles::setBibTeXFile(const QString& file)
  174 {
  175     if (!file.isEmpty())
  176     {
  177         QFileInfo fi(file);
  178         QDir d(fi.absolutePath());
  179         if (d.exists())
  180         {
  181             _bibtex_file = fi.absoluteFilePath();
  182             _bibtex_dir = fi.absolutePath();
  183         }
  184         else
  185             _bibtex_dir.clear();
  186     }
  187     if (_bibtex_dir.isEmpty())
  188         _search_log = tr("No existing file or directory set for searching");
  189     else if (ui.allBibFiles->isChecked())
  190         _search_log = tr("Search in files at %1").arg(_bibtex_dir);
  191     else
  192         _search_log = tr("Search in file %1").arg(_bibtex_file);
  193     updateForm();
  194 }
  195 
  196 void c2bSearchInFiles::setCurrentText(const QString& text)
  197 {
  198     if (text.isEmpty())
  199         return;
  200     ui.pattern->setText(text);
  201     updateForm();
  202 }
  203 
  204 void c2bSearchInFiles::show()
  205 {
  206     c2bUtils::setWidgetOnTop(this);
  207     ui.pattern->setFocus();
  208     QDialog::show();
  209 }
  210 
  211 void c2bSearchInFiles::help()
  212 {
  213     c2bUtils::displayHelp("https://www.molspaces.com/cb2bib/doc/bibsearch/");
  214 }