"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/c2bCiterWidget.cpp" (12 Feb 2021, 11857 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 "c2bCiterWidget.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 "c2bCiterWidget.h"
    8 
    9 #include "c2b.h"
   10 #include "c2bCiterModel.h"
   11 #include "c2bClipboard.h"
   12 #include "c2bCollectionIndex.h"
   13 #include "c2bCoreCiter.h"
   14 #include "c2bFileDialog.h"
   15 #include "c2bSettings.h"
   16 #include "c2bUtils.h"
   17 
   18 #include <QKeyEvent>
   19 #include <QLabel>
   20 #include <QMenu>
   21 
   22 
   23 c2bCiterWidget::c2bCiterWidget(QWidget* parentw) : QWidget(parentw)
   24 {
   25     ui.setupUi(this);
   26     ui.clipboardIcon->setVisible(true);
   27     ui.filterIcon->setVisible(false);
   28     _citerP = new c2bCoreCiter(this);
   29     _tray_icon = 0;
   30     setActions();
   31 
   32     _settingsP = c2bSettingsP;
   33     resize(_settingsP->value("c2bCiter/size", size()).toSize());
   34     move(_settingsP->value("c2bCiter/position", pos()).toPoint());
   35 
   36     _clipboard_connected = true;
   37     _citations = new c2bCiterModel(this);
   38 
   39     connect(_citations, SIGNAL(statusLabel(QString)), this, SLOT(showLabel(QString)));
   40     connect(_citations, SIGNAL(statusMessage(QString)), this, SLOT(showMessage(QString)));
   41     connect(c2b::clipboard(), SIGNAL(cbDataChanged(QString)), this, SLOT(filterDataChanged(QString)));
   42     connect(ui.citationsView, SIGNAL(activated(QModelIndex)), _citations, SLOT(selectCitation(QModelIndex)));
   43     connect(ui.citationsView, SIGNAL(citeReferences()), this, SLOT(citeReferences()));
   44     connect(ui.citationsView, SIGNAL(editReference()), this, SLOT(editReference()));
   45     connect(ui.citationsView, SIGNAL(hideCiter()), this, SLOT(hide()));
   46     connect(ui.citationsView, SIGNAL(openAnnote()), this, SIGNAL(openAnnote()));
   47     connect(ui.citationsView, SIGNAL(openFile()), this, SLOT(openFile()));
   48     connect(ui.citationsView, SIGNAL(openUrl()), this, SLOT(openUrl()));
   49     connect(ui.citationsView, SIGNAL(patternFilterChanged(bool,QString)), this,
   50             SLOT(updatePatternFilter(bool,QString)));
   51     connect(ui.citationsView, SIGNAL(statusMessage(QString)), this, SLOT(showMessage(QString)));
   52 }
   53 
   54 c2bCiterWidget::~c2bCiterWidget()
   55 {
   56     if (!isFullScreen())
   57     {
   58         _settingsP->setValue("c2bCiter/position", pos());
   59         _settingsP->setValue("c2bCiter/size", size());
   60     }
   61     _settingsP->setValue("c2bCiter/format", static_cast<int>(_citations->lastCitationFormat()));
   62 }
   63 
   64 
   65 void c2bCiterWidget::load()
   66 {
   67     _bibtex_files.clear();
   68     if (_settingsP->cl_citer_filenames.count() == 0)
   69         _bibtex_files = c2bUtils::filesInDir(QFileInfo(_settingsP->fileName("cb2Bib/BibTeXFile")).absolutePath(),
   70                                              QStringList() << "*.bib");
   71     else
   72         for (int i = 0; i < _settingsP->cl_citer_filenames.count(); ++i)
   73             _bibtex_files += c2bUtils::filesInDir(_settingsP->cl_citer_filenames.at(i), QStringList() << "*.bib");
   74     c2bCiter::State state;
   75     state.format = static_cast<c2bCiter::Format>(_settingsP->value("c2bCiter/format", state.format).toInt());
   76     reload(state);
   77 }
   78 
   79 void c2bCiterWidget::reload()
   80 {
   81     const c2bCiter::State state(ui.citationsView->currentState());
   82     reload(state);
   83 }
   84 
   85 void c2bCiterWidget::reload(const c2bCiter::State& state)
   86 {
   87     QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
   88     updatePatternFilter(false, QString());
   89     if (_bibtex_files.count() == 1)
   90         setWindowTitle(
   91             tr("%1 - cb2Bib").arg(QDir::toNativeSeparators(QFileInfo(_bibtex_files.at(0)).absoluteFilePath())));
   92     else if (_bibtex_files.count() > 1)
   93         setWindowTitle(tr("%1 - cb2Bib").arg(QDir::toNativeSeparators(QFileInfo(_bibtex_files.at(0)).absolutePath())));
   94     else
   95         setWindowTitle(tr("Citer - cb2Bib"));
   96     ui.citationsView->reloadModel(_citations, _bibtex_files, state);
   97     QApplication::restoreOverrideCursor();
   98 }
   99 
  100 void c2bCiterWidget::toggleVisible()
  101 {
  102     if (isVisible())
  103         hide();
  104     else
  105         show();
  106 }
  107 
  108 void c2bCiterWidget::show()
  109 {
  110     if (isHidden())
  111         QWidget::show();
  112     raise();
  113     activateWindow();
  114     ui.citationsView->setFocus();
  115 }
  116 
  117 void c2bCiterWidget::setTrayIcon()
  118 {
  119     _tray_icon = new QSystemTrayIcon(this);
  120     _tray_icon->setIcon(QIcon(":/icons/icons/cb2bib.ico"));
  121     _tray_icon->setToolTip("c2bBib Citer");
  122     QMenu* tray_icon_menu = new QMenu(this);
  123     foreach (QAction* a, actions())
  124     {
  125         tray_icon_menu->addAction(a);
  126     }
  127     _tray_icon->setContextMenu(tray_icon_menu);
  128     _tray_icon->show();
  129     connect(_tray_icon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this,
  130             SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
  131 }
  132 
  133 void c2bCiterWidget::trayIconActivated(QSystemTrayIcon::ActivationReason r)
  134 {
  135     if (r == QSystemTrayIcon::Context)
  136         return;
  137     show();
  138 }
  139 
  140 void c2bCiterWidget::setActions()
  141 {
  142     QAction* a;
  143     a = new QAction(this);
  144     a->setText(tr("Citer Help"));
  145     a->setShortcut(QKeySequence(QKeySequence::HelpContents));
  146     addAction(a);
  147     connect(a, SIGNAL(triggered()), this, SLOT(help()));
  148     c2bUtils::addSeparator(this);
  149 
  150     a = new QAction(this);
  151     a->setText(tr("Open Directory"));
  152     a->setShortcut(QKeySequence(QKeySequence::Open));
  153     addAction(a);
  154     connect(a, SIGNAL(triggered()), this, SLOT(openDirectory()));
  155     a = new QAction(this);
  156     a->setText(tr("Open Files"));
  157     a->setShortcut(QKeySequence(Qt::ALT + Qt::Key_O));
  158     addAction(a);
  159     connect(a, SIGNAL(triggered()), this, SLOT(openFiles()));
  160     c2bUtils::addSeparator(this);
  161 
  162     a = new QAction(this);
  163     a->setText(tr("Refresh"));
  164     a->setShortcut(QKeySequence(QKeySequence::Refresh));
  165     addAction(a);
  166     connect(a, SIGNAL(triggered()), this, SLOT(reload()));
  167     c2bUtils::addSeparator(this);
  168 
  169     a = new QAction(this);
  170     a->setText(tr("Search In Files"));
  171     a->setShortcut(QKeySequence(QKeySequence::Find));
  172     addAction(a);
  173     connect(a, SIGNAL(triggered()), this, SLOT(doSearchInFiles()));
  174     c2bUtils::addSeparator(this);
  175 
  176     a = new QAction(this);
  177     a->setText(tr("Set LyX Pipe"));
  178     a->setShortcut(QKeySequence(Qt::ALT + Qt::Key_L));
  179     addAction(a);
  180     connect(a, SIGNAL(triggered()), _citerP, SLOT(setLyXPipe()));
  181     c2bUtils::addSeparator(this);
  182 
  183     a = new QAction(this);
  184     a->setText(tr("Toggle cb2Bib"));
  185     a->setShortcut(QKeySequence(Qt::Key_F2));
  186     addAction(a);
  187     connect(a, SIGNAL(triggered()), this, SLOT(toggleCb2Bib()));
  188     a = new QAction(this);
  189     a->setText(tr("Toggle Clipboard"));
  190     a->setShortcut(QKeySequence(Qt::ALT + Qt::Key_C));
  191     addAction(a);
  192     connect(a, SIGNAL(triggered()), this, SLOT(toggleClipboard()));
  193     a = new QAction(this);
  194     a->setText(tr("Toggle Full Screen"));
  195     a->setShortcut(QKeySequence(Qt::ALT + Qt::Key_F));
  196     addAction(a);
  197     connect(a, SIGNAL(triggered()), this, SLOT(toggleFullScreen()));
  198     c2bUtils::addSeparator(this);
  199 
  200     a = new QAction(this);
  201     a->setText(tr("Index Documents"));
  202     addAction(a);
  203     connect(a, SIGNAL(triggered()), this, SLOT(indexDocuments()));
  204     c2bUtils::addSeparator(this);
  205 
  206     a = new QAction(this);
  207     a->setText(tr("Exit"));
  208     a->setShortcut(QKeySequence(QKeySequence::Quit));
  209     addAction(a);
  210     connect(a, SIGNAL(triggered()), c2b::instance(), SLOT(exitRequested()));
  211 }
  212 
  213 void c2bCiterWidget::openDirectory()
  214 {
  215     QString dir;
  216     if (_bibtex_files.count() > 0)
  217         dir = _bibtex_files.at(0);
  218     dir = c2bFileDialog::getExistingDirectory(this, tr("Select BibTeX directory"), dir);
  219     if (dir.isEmpty())
  220         return;
  221     _bibtex_files = c2bUtils::filesInDir(dir, QStringList() << "*.bib");
  222     reload();
  223 }
  224 
  225 void c2bCiterWidget::openFiles()
  226 {
  227     QString dir;
  228     if (_bibtex_files.count() > 0)
  229         dir = _bibtex_files.at(0);
  230     const QStringList fns(
  231         c2bFileDialog::getOpenFilenames(this, "Select BibTeX filenames", dir, "BibTeX (*.bib);;All (*)"));
  232     if (fns.count() > 0)
  233     {
  234         _bibtex_files = fns;
  235         reload();
  236     }
  237 }
  238 
  239 void c2bCiterWidget::doSearchInFiles()
  240 {
  241     const QString fn(_citations->isKeywordView() ? QString()
  242                      : _citations->dataBibTeXFile(ui.citationsView->currentIndex()));
  243     if (fn.isEmpty())
  244         showMessage(tr("No active reference."));
  245     else
  246         c2b::doSearchInFiles(QString(), fn);
  247 }
  248 
  249 void c2bCiterWidget::toggleCb2Bib()
  250 {
  251     if (c2b::mainWidget()->isVisible())
  252         c2b::mainWidget()->hide();
  253     else
  254         c2bUtils::setWidgetOnTop(c2b::mainWidget());
  255 }
  256 
  257 void c2bCiterWidget::toggleClipboard()
  258 {
  259     _clipboard_connected = !_clipboard_connected;
  260     ui.clipboardIcon->setVisible(_clipboard_connected);
  261     if (_clipboard_connected)
  262         showMessage(tr("Filter set to clipboard contents."));
  263 }
  264 
  265 void c2bCiterWidget::toggleFullScreen()
  266 {
  267     if (isFullScreen())
  268         showNormal();
  269     else
  270         showFullScreen();
  271 }
  272 
  273 void c2bCiterWidget::indexDocuments()
  274 {
  275     c2bCollectionIndex* ci(new c2bCollectionIndex(0));
  276     ci->setAttribute(Qt::WA_DeleteOnClose);
  277     ci->show();
  278     ci->index();
  279 }
  280 
  281 void c2bCiterWidget::filterDataChanged(const QString& filter)
  282 {
  283     if (_clipboard_connected && isVisible())
  284     {
  285         const QString p(c2bUtils::toAscii(filter, c2bUtils::Collation).simplified());
  286         updatePatternFilter(false, p);
  287         ui.citationsView->setPatternFilter(p);
  288     }
  289 }
  290 
  291 void c2bCiterWidget::updatePatternFilter(const bool mode, const QString& pattern)
  292 {
  293     ui.filterIcon->setVisible(mode);
  294     if (mode)
  295         showLabel(tr("Filter: %1_").arg(pattern));
  296     else
  297     {
  298         if (pattern.isEmpty())
  299             showLabel(_citations->isKeywordView() ? tr("Glossary of terms") : pattern);
  300         else
  301             showLabel(tr("Filter: %1").arg(pattern));
  302     }
  303 }
  304 
  305 void c2bCiterWidget::citeReferences()
  306 {
  307     const QStringList k(_citations->dataSelectedCiteIds());
  308     if (k.count() > 0)
  309     {
  310         hide();
  311         _citerP->cite(k);
  312     }
  313     else
  314         showMessage(tr("No selected references."));
  315 }
  316 
  317 void c2bCiterWidget::editReference()
  318 {
  319     if (_citations->isKeywordView())
  320     {
  321         showMessage(tr("No active reference."));
  322         return;
  323     }
  324     const QString r(_citations->dataBibTeXPosition(ui.citationsView->currentIndex()));
  325     if (r.isEmpty())
  326         showMessage(tr("No active reference."));
  327     else
  328         emit openFile(r);
  329 }
  330 
  331 void c2bCiterWidget::openFile()
  332 {
  333     if (_citations->count() == 0)
  334         return;
  335     if (_citations->isKeywordView())
  336     {
  337         showMessage(tr("No active reference."));
  338         return;
  339     }
  340     const QString f(_citations->dataFile(ui.citationsView->currentIndex()));
  341     if (f.isEmpty())
  342         showMessage(tr("No File entry in this reference."));
  343     else
  344     {
  345         showMessage(tr("Opening %1").arg(f));
  346         c2bUtils::openFile(f, parentWidget());
  347     }
  348 }
  349 
  350 void c2bCiterWidget::openUrl()
  351 {
  352     if (_citations->count() == 0)
  353         return;
  354     if (_citations->isKeywordView())
  355     {
  356         showMessage(tr("No active reference."));
  357         return;
  358     }
  359     const QString u(_citations->dataUrl(ui.citationsView->currentIndex()));
  360     if (u.isEmpty())
  361         showMessage(tr("No URL entry in this reference."));
  362     else
  363     {
  364         showMessage(tr("Opening %1").arg(QUrl::fromPercentEncoding(u.toUtf8())));
  365         c2bUtils::openFile(u, parentWidget());
  366     }
  367 }
  368 
  369 void c2bCiterWidget::showLabel()
  370 {
  371     ui.statusBar->showMessage(_status_label, 0);
  372 }
  373 
  374 void c2bCiterWidget::showLabel(const QString& label)
  375 {
  376     _status_label = label;
  377     ui.statusBar->showMessage(_status_label, 0);
  378 }
  379 
  380 void c2bCiterWidget::showMessage(const QString& message)
  381 {
  382     QTimer::singleShot(C2B_MESSAGE_TIME, this, SLOT(showLabel()));
  383     ui.statusBar->showMessage(message, 0);
  384 }
  385 
  386 void c2bCiterWidget::help()
  387 {
  388     c2bUtils::displayHelp("https://www.molspaces.com/cb2bib/doc/c2bciter/");
  389 }