"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/c2bCiterView.cpp" (12 Feb 2021, 11821 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 "c2bCiterView.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 "c2bCiterView.h"
    8 
    9 #include "c2bCiterExcerptView.h"
   10 
   11 #include <bibSearcher.h>
   12 
   13 #include <QApplication>
   14 #include <QHeaderView>
   15 #include <QKeyEvent>
   16 
   17 
   18 c2bCiterView::c2bCiterView(QWidget* parentw) : QTableView(parentw)
   19 {
   20     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
   21     setShowGrid(false);
   22     setTabKeyNavigation(true);
   23     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
   24     setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
   25 
   26     horizontalHeader()->setSectionResizeMode(QHeaderView::Fixed);
   27     horizontalHeader()->setVisible(false);
   28     verticalHeader()->setDefaultSectionSize(fontMetrics().height() + 5);
   29     verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
   30     verticalHeader()->setVisible(false);
   31 
   32     _citationsP = 0;
   33     _filter_mode = false;
   34 }
   35 
   36 
   37 void c2bCiterView::reloadModel(QAbstractItemModel* citations, const QStringList& bibtex_files, const c2bCiter::State& s)
   38 {
   39     _filter_mode = false;
   40     _filter_string.clear();
   41     if (!citations)
   42         return;
   43     if (_citationsP != citations)
   44     {
   45         _citationsP = static_cast<c2bCiterModel*>(citations);
   46         QTableView::setModel(_citationsP);
   47         connect(_citationsP, SIGNAL(patternFilterChanged(QString)), this, SLOT(updatePatternFilter(QString)));
   48     }
   49     QModelIndex i(currentIndex());
   50     _citationsP->reloadCitations(bibtex_files, s, &i);
   51     updateViewport();
   52     updateCurrentIndex(i);
   53 }
   54 
   55 c2bCiter::State c2bCiterView::currentState() const
   56 {
   57     if (_citationsP)
   58         return _citationsP->currentState(currentIndex());
   59     else
   60         return c2bCiter::State();
   61 }
   62 
   63 void c2bCiterView::showPreviousState()
   64 {
   65     QModelIndex i(currentIndex());
   66     _citationsP->restorePreviousState(&i);
   67     updateViewport();
   68     updateCurrentIndex(i);
   69 }
   70 
   71 void c2bCiterView::showNextState()
   72 {
   73     QModelIndex i(currentIndex());
   74     _citationsP->restoreNextState(&i);
   75     updateViewport();
   76     updateCurrentIndex(i);
   77 }
   78 
   79 void c2bCiterView::clearCurrentFilter()
   80 {
   81     QModelIndex i(currentIndex());
   82     _citationsP->clearCurrentFilter(&i);
   83     updateViewport();
   84     updateCurrentIndex(i);
   85 }
   86 
   87 void c2bCiterView::setPatternFilter(const QString& pattern)
   88 {
   89     QModelIndex i(currentIndex());
   90     _filter_string = pattern;
   91     _citationsP->setPatternFilter(_filter_string, &i, _citationsP->currentFormat());
   92     updateViewport();
   93     updateCurrentIndex(i);
   94 }
   95 
   96 void c2bCiterView::updatePatternFilter(const QString& pattern)
   97 {
   98     _filter_string = pattern;
   99     emit patternFilterChanged(false, _filter_string);
  100 }
  101 
  102 void c2bCiterView::setRelatedFilter()
  103 {
  104     if (_citationsP->count() == 0)
  105         return;
  106     QModelIndex i(currentIndex());
  107     if (_citationsP->currentFormat() == c2bCiter::K)
  108     {
  109         const QString k(_citationsP->dataKeyword(i));
  110         _citationsP->setKeywordFilter(k, &i);
  111         updateViewport();
  112         updateCurrentIndex(i);
  113     }
  114     else
  115     {
  116         const QString f(_citationsP->dataFile(i));
  117         if (f.isEmpty())
  118             emit statusMessage(tr("No File contents in this reference."));
  119         else
  120         {
  121             _citationsP->setRelatedFilter(f, &i);
  122             updateViewport();
  123             updateCurrentIndex(i);
  124         }
  125     }
  126 }
  127 
  128 void c2bCiterView::toggleSelectedFilter()
  129 {
  130     QModelIndex i(currentIndex());
  131     if (_citationsP->currentFilter() == c2bCiter::Selected)
  132         _citationsP->restoreLastNonSelectedState(&i);
  133     else
  134         _citationsP->setSelectedFilter(&i);
  135     updateViewport();
  136     updateCurrentIndex(i);
  137 }
  138 
  139 void c2bCiterView::toggleKeywordView()
  140 {
  141     if (_citationsP->count() == 0)
  142         return;
  143     QModelIndex i(currentIndex());
  144     if (_citationsP->currentFormat() == c2bCiter::K)
  145     {
  146         _citationsP->restoreLastNonKeywordState(&i);
  147         updateViewport();
  148         updateCurrentIndex(i);
  149     }
  150     else
  151     {
  152         const QString f(_citationsP->dataFile(i));
  153         if (f.isEmpty())
  154             emit statusMessage(tr("No File contents in this reference."));
  155         else
  156         {
  157             _citationsP->setDocumentFilter(f, &i);
  158             updateViewport();
  159             updateCurrentIndex(i);
  160         }
  161     }
  162 }
  163 
  164 void c2bCiterView::toggleGlossaryView()
  165 {
  166     QModelIndex i(currentIndex());
  167     if (_citationsP->currentFormat() == c2bCiter::K &&
  168         (_citationsP->currentFilter() == c2bCiter::None || _citationsP->currentFilter() == c2bCiter::Pattern))
  169         _citationsP->restoreLastNonKeywordState(&i);
  170     else
  171         _citationsP->setGlossaryView(&i);
  172     updateViewport();
  173     updateCurrentIndex(i);
  174 }
  175 
  176 void c2bCiterView::keywordDocumentSearch()
  177 {
  178     const c2bCiter::KeywordData kd(_citationsP->keywordData(currentIndex()));
  179     if (!kd.valid)
  180         return;
  181 
  182     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
  183     const QString excerpt(bibSearcher::searchDocumentKeyword(kd.bibtexfn, kd.documentfn, kd.keyword));
  184     QApplication::restoreOverrideCursor();
  185     if (excerpt.isEmpty())
  186     {
  187         emit statusMessage(tr("Keyword variation '%1' not available in this reference.").arg(kd.keyword));
  188         return;
  189     }
  190     c2bCiterExcerptView cev(excerpt, this);
  191     cev.exec();
  192 }
  193 
  194 void c2bCiterView::updateCurrentIndex(const QModelIndex& i)
  195 {
  196     setCurrentIndex(i);
  197     scrollTo(i, PositionAtCenter);
  198 }
  199 
  200 void c2bCiterView::updateFormat(const c2bCiter::Format& format)
  201 {
  202     if (!_citationsP)
  203         return;
  204     QModelIndex i(currentIndex());
  205     _citationsP->updateFormat(format, &i);
  206     updateViewport();
  207     updateCurrentIndex(i);
  208 }
  209 
  210 void c2bCiterView::updateViewport()
  211 {
  212     if (!_citationsP)
  213         return;
  214     const int nc(_citationsP->columnCount());
  215     if (nc < 1)
  216         return;
  217     const int vp_w(viewport()->size().width());
  218     if (nc == 1)
  219     {
  220         setColumnWidth(0, vp_w);
  221         return;
  222     }
  223     QList<int> w = _citationsP->sizeHintForColumns();
  224 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
  225     const int char_w(fontMetrics().horizontalAdvance(QLatin1Char('N')));
  226 #else
  227     const int char_w(fontMetrics().width(QLatin1Char('N')));
  228 #endif
  229     for (int i = 0; i < nc; ++i)
  230         w[i] *= char_w;
  231     int required_w(w.at(0));
  232     if (required_w >= vp_w)
  233     {
  234         setColumnWidth(0, vp_w);
  235         for (int i = 1; i < nc; ++i)
  236             setColumnWidth(i, 0);
  237         return;
  238     }
  239     for (int i = 1; i < nc; ++i)
  240         required_w += w.at(i);
  241     if (required_w > vp_w)
  242     {
  243         int available_w(vp_w - w.at(0));
  244         int div[] = { 0, 0, 0, 0 };
  245         while (available_w > 0)
  246             for (int i = 1; i < nc; ++i)
  247                 if (div[i] < w.at(i))
  248                 {
  249                     div[i] += 1;
  250                     --available_w;
  251                 }
  252         for (int i = 1; i < nc; ++i)
  253             w[i] = div[i];
  254     }
  255     else
  256         w[nc - 1] += vp_w - required_w;
  257     for (int i = 0; i < nc; ++i)
  258         setColumnWidth(i, w.at(i));
  259 }
  260 
  261 void c2bCiterView::keyPressEvent(QKeyEvent* qevent)
  262 {
  263     if (_filter_mode)
  264     {
  265         switch (qevent->key())
  266         {
  267         case Qt::Key_Enter:
  268         case Qt::Key_Return:
  269         case Qt::Key_Down:
  270         case Qt::Key_Escape:
  271         case Qt::Key_Up:
  272         case Qt::Key_Tab:
  273             _filter_mode = false;
  274             emit patternFilterChanged(_filter_mode, _filter_string);
  275             qevent->accept();
  276             return;
  277         }
  278         switch (qevent->key())
  279         {
  280         case Qt::Key_Backspace:
  281             _filter_string.chop(1);
  282             break;
  283         default:
  284             _filter_string += c2bUtils::toAscii(qevent->text(), c2bUtils::Collation);
  285         }
  286         emit patternFilterChanged(_filter_mode, _filter_string);
  287         setPatternFilter(_filter_string);
  288         qevent->ignore();
  289         return;
  290     }
  291     if (qevent->modifiers() & Qt::ShiftModifier)
  292     {
  293         if (!qevent->text().isEmpty())
  294             QTableView::keyboardSearch(qevent->text());
  295         qevent->accept();
  296         return;
  297     }
  298     switch (qevent->key())
  299     {
  300     // Formats
  301     case Qt::Key_A:
  302         updateFormat(c2bCiter::AJYT);
  303         qevent->accept();
  304         return;
  305     case Qt::Key_I:
  306         updateFormat(c2bCiter::IT);
  307         qevent->accept();
  308         return;
  309     case Qt::Key_J:
  310         updateFormat(c2bCiter::JYA);
  311         qevent->accept();
  312         return;
  313     case Qt::Key_T:
  314         updateFormat(c2bCiter::T);
  315         qevent->accept();
  316         return;
  317     case Qt::Key_Y:
  318         updateFormat(c2bCiter::YAJT);
  319         qevent->accept();
  320         return;
  321 
  322     // Filters
  323     case Qt::Key_Delete:
  324         _citationsP->clearSelection();
  325         qevent->accept();
  326         return;
  327     case Qt::Key_D:
  328         clearCurrentFilter();
  329         qevent->accept();
  330         return;
  331     case Qt::Key_F:
  332         _filter_mode = true;
  333         emit patternFilterChanged(_filter_mode, _filter_string);
  334         qevent->accept();
  335         return;
  336     case Qt::Key_G:
  337         toggleGlossaryView();
  338         qevent->accept();
  339         return;
  340     case Qt::Key_K:
  341         toggleKeywordView();
  342         qevent->accept();
  343         return;
  344     case Qt::Key_R:
  345         setRelatedFilter();
  346         qevent->accept();
  347         return;
  348     case Qt::Key_S:
  349         toggleSelectedFilter();
  350         qevent->accept();
  351         return;
  352 
  353     // Actions
  354     case Qt::Key_Left:
  355         showPreviousState();
  356         qevent->accept();
  357         return;
  358     case Qt::Key_Right:
  359         showNextState();
  360         qevent->accept();
  361         return;
  362     case Qt::Key_C:
  363         emit citeReferences();
  364         qevent->accept();
  365         return;
  366     case Qt::Key_E:
  367         emit editReference();
  368         qevent->accept();
  369         return;
  370     case Qt::Key_O:
  371         emit openFile();
  372         qevent->accept();
  373         return;
  374     case Qt::Key_U:
  375         emit openUrl();
  376         qevent->accept();
  377         return;
  378     case Qt::Key_V:
  379         keywordDocumentSearch();
  380         qevent->accept();
  381         return;
  382     case Qt::Key_W:
  383         emit openAnnote();
  384         qevent->accept();
  385         return;
  386     case Qt::Key_Escape:
  387         emit hideCiter();
  388         qevent->accept();
  389         return;
  390 
  391     // Navigation
  392     case Qt::Key_Tab:
  393     {
  394         QKeyEvent ke(QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier);
  395         QTableView::keyPressEvent(&ke);
  396         qevent->accept();
  397         return;
  398     }
  399     case Qt::Key_Backspace:
  400     {
  401         QKeyEvent ke(QEvent::KeyPress, Qt::Key_Up, Qt::NoModifier);
  402         QTableView::keyPressEvent(&ke);
  403         qevent->accept();
  404         return;
  405     }
  406     case Qt::Key_Home:
  407     {
  408         QKeyEvent ke(QEvent::KeyPress, Qt::Key_Home, Qt::ControlModifier);
  409         QTableView::keyPressEvent(&ke);
  410         qevent->accept();
  411         return;
  412     }
  413     case Qt::Key_End:
  414     {
  415         QKeyEvent ke(QEvent::KeyPress, Qt::Key_End, Qt::ControlModifier);
  416         QTableView::keyPressEvent(&ke);
  417         qevent->accept();
  418         return;
  419     }
  420     case Qt::Key_Space:
  421     {
  422         QKeyEvent ke(QEvent::KeyPress, Qt::Key_PageDown, Qt::NoModifier);
  423         QTableView::keyPressEvent(&ke);
  424         qevent->accept();
  425         return;
  426     }
  427     case Qt::Key_Enter:
  428     case Qt::Key_Return:
  429     {
  430         QTableView::keyPressEvent(qevent);
  431         QKeyEvent ke(QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier);
  432         QTableView::keyPressEvent(&ke);
  433         qevent->accept();
  434         return;
  435     }
  436     default:
  437         QTableView::keyPressEvent(qevent);
  438         qevent->ignore();
  439     }
  440 }
  441 
  442 void c2bCiterView::resizeEvent(QResizeEvent* qevent)
  443 {
  444     updateViewport();
  445     QTableView::resizeEvent(qevent);
  446 }
  447 
  448 void c2bCiterView::keyboardSearch(const QString& /* search */) {}