"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/c2bSettings.cpp" (12 Feb 2021, 12080 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 "c2bSettings.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 "c2bSettings.h"
    8 #include "c2bUtils.h"
    9 
   10 #include <QApplication>
   11 #include <QClipboard>
   12 #include <QMessageBox>
   13 #include <QProgressBar>
   14 #include <QShortcut>
   15 #include <QTimer>
   16 
   17 
   18 /**
   19     Global settings for cb2Bib
   20 */
   21 c2bSettings::c2bSettings(const QString& organization, const QString& application, QObject* parento)
   22     : settings(organization, application, parento), _progress_bar(0) {}
   23 
   24 c2bSettings::c2bSettings(const QString& fn, QObject* parento) : settings(fn, parento), _progress_bar(0) {}
   25 
   26 c2bSettings::~c2bSettings()
   27 {
   28     delete _progress_bar;
   29 }
   30 
   31 
   32 c2bSettings* c2bSettings::initSettings()
   33 {
   34     c2bSettings* s;
   35     if (c2bSettings::isConfiguredMode())
   36         s = new c2bSettings(c2bSettings::configuredFilename());
   37     else
   38         s = new c2bSettings(qApp->organizationName(), qApp->applicationName());
   39     s->init();
   40     return s;
   41 }
   42 
   43 void c2bSettings::init()
   44 {
   45     settings::init();
   46     _progress_bar = 0;
   47     if (cl_console_mode)
   48         return;
   49     setDefaults();
   50     if (cl_import)
   51         // Process cl_import_filename and update cl_cb2bib_import_mode
   52         processReferenceFile();
   53     if (cl_cb2bib_mode && !cl_cb2bib_import_mode)
   54     {
   55         setValue("cb2Bib/isRunning", true);
   56         sync();
   57         connect(qApp, SIGNAL(lastWindowClosed()), this, SLOT(guiExits()));
   58     }
   59 }
   60 
   61 void c2bSettings::notify()
   62 {
   63     update();
   64     emit newSettings();
   65 }
   66 
   67 void c2bSettings::update()
   68 {
   69     // Values for very general GUI settings
   70     c2bMonoFont = value("cb2Bib/MonoFont").value<QFont>();
   71     colors.loadColors(this);
   72 }
   73 
   74 void c2bSettings::setDefaults()
   75 {
   76     QHash<QString, QVariant> defaults;
   77     defaults["cb2Bib/MonoFont"] = QFont("Courier");
   78 
   79     const QStringList keys(defaults.keys());
   80     for (int i = 0; i < keys.count(); ++i)
   81         setValue(keys.at(i), value(keys.at(i), defaults.value(keys.at(i))));
   82 
   83     // Shortcuts
   84     _default_keysequences.clear();
   85     _default_keysequences["Shortcuts/cb2Bib/Button About and Bookmarks"] = QKeySequence(Qt::CTRL + Qt::Key_Tab);
   86     _default_keysequences["Shortcuts/cb2Bib/Button Configure"] = QKeySequence();
   87     _default_keysequences["Shortcuts/cb2Bib/Button Network Query"] = QKeySequence(Qt::ALT + Qt::Key_N);
   88     _default_keysequences["Shortcuts/cb2Bib/Button Start PDFImport"] = QKeySequence();
   89     _default_keysequences["Shortcuts/cb2Bib/Button Toggle Connection"] = QKeySequence();
   90     _default_keysequences["Shortcuts/cb2Bib/Button View BibTeX"] = QKeySequence(Qt::ALT + Qt::Key_V);
   91     _default_keysequences["Shortcuts/cb2Bib/Check Repeated"] = QKeySequence(Qt::ALT + Qt::Key_X);
   92     _default_keysequences["Shortcuts/cb2Bib/Clear All Fields"] = QKeySequence();
   93     _default_keysequences["Shortcuts/cb2Bib/Delete Temporary BibTeX File"] = QKeySequence(Qt::ALT + Qt::Key_D);
   94     _default_keysequences["Shortcuts/cb2Bib/Edit Bookmarks"] = QKeySequence(Qt::ALT + Qt::Key_B);
   95     _default_keysequences["Shortcuts/cb2Bib/Edit Journal Abbreviations"] = QKeySequence(Qt::ALT + Qt::Key_J);
   96     _default_keysequences["Shortcuts/cb2Bib/Edit Regular Expressions"] = QKeySequence(Qt::ALT + Qt::Key_E);
   97     _default_keysequences["Shortcuts/cb2Bib/Guess Bibliographic Fields"] = QKeySequence(Qt::ALT + Qt::Key_G);
   98     _default_keysequences["Shortcuts/cb2Bib/Insert Regular Expression"] = QKeySequence(Qt::ALT + Qt::Key_I);
   99     _default_keysequences["Shortcuts/cb2Bib/Open Document"] = QKeySequence(Qt::ALT + Qt::Key_O);
  100     _default_keysequences["Shortcuts/cb2Bib/Postprocess BibTeX"] = QKeySequence(Qt::ALT + Qt::Key_P);
  101     _default_keysequences["Shortcuts/cb2Bib/Preparse Clipboard"] = QKeySequence(Qt::ALT + Qt::Key_C);
  102     _default_keysequences["Shortcuts/cb2Bib/Restart Recognition"] = QKeySequence(Qt::ALT + Qt::Key_R);
  103     _default_keysequences["Shortcuts/cb2Bib/Select BibTeX File"] = QKeySequence(Qt::ALT + Qt::Key_S);
  104     _default_keysequences["Shortcuts/cb2Bib/Select Document File"] = QKeySequence(Qt::ALT + Qt::Key_F);
  105     _default_keysequences["Shortcuts/cb2Bib/Annote Editor"] = QKeySequence(Qt::ALT + Qt::Key_A);
  106     _default_keysequences["Shortcuts/cb2Bib/Toggle Reference Tabs"] = QKeySequence(Qt::Key_F4);
  107     _default_keysequences["Shortcuts/cb2Bib/View Tagged Clipboard Data"] = QKeySequence();
  108     _default_keysequences["Shortcuts/cb2Bib/Write Metadata to Document"] = QKeySequence(Qt::ALT + Qt::Key_W);
  109 
  110     _default_keysequences["Shortcuts/Editor/Browse Reference by DOI"] = QKeySequence();
  111     _default_keysequences["Shortcuts/Editor/Cite Selected Entries"] = QKeySequence(Qt::ALT + Qt::Key_C);
  112     _default_keysequences["Shortcuts/Editor/Clear Entry Selection"] = QKeySequence(Qt::Key_Delete);
  113     _default_keysequences["Shortcuts/Editor/Export Entries"] = QKeySequence();
  114     _default_keysequences["Shortcuts/Editor/Local Search for Selected Text"] = QKeySequence();
  115     _default_keysequences["Shortcuts/Editor/Open Document File"] = QKeySequence();
  116     _default_keysequences["Shortcuts/Editor/Open Document URL"] = QKeySequence();
  117     _default_keysequences["Shortcuts/Editor/Paste Current BibTeX"] = QKeySequence(Qt::SHIFT + Qt::Key_Insert);
  118     _default_keysequences["Shortcuts/Editor/Restart Network Query"] = QKeySequence(Qt::ALT + Qt::Key_N);
  119     _default_keysequences["Shortcuts/Editor/Restart Recognition"] = QKeySequence(Qt::ALT + Qt::Key_R);
  120     _default_keysequences["Shortcuts/Editor/Save and Postprocess"] = QKeySequence(Qt::ALT + Qt::Key_P);
  121     _default_keysequences["Shortcuts/Editor/Selection to LaTeX"] = QKeySequence();
  122     _default_keysequences["Shortcuts/Editor/Selection to Unicode"] = QKeySequence();
  123     _default_keysequences["Shortcuts/Editor/Set Journals to Abbreviated Name"] = QKeySequence();
  124     _default_keysequences["Shortcuts/Editor/Set Journals to Full Name"] = QKeySequence();
  125     _default_keysequences["Shortcuts/Editor/Set LyX Pipe"] = QKeySequence(Qt::ALT + Qt::Key_L);
  126     _default_keysequences["Shortcuts/Editor/Toggle Editing and Browsing"] = QKeySequence(Qt::CTRL + Qt::Key_Tab);
  127     _default_keysequences["Shortcuts/Editor/Toggle Word Wrapping"] = QKeySequence();
  128     _default_keysequences["Shortcuts/Editor/Update Documents Metadata"] = QKeySequence();
  129     _default_keysequences["Shortcuts/Editor/View Note in External Browser"] = QKeySequence();
  130     _default_keysequences["Shortcuts/Editor/Web Search Settings"] = QKeySequence();
  131     _default_keysequences["Shortcuts/Editor/Web Search by Author"] = QKeySequence();
  132     _default_keysequences["Shortcuts/Editor/Web Search by Title"] = QKeySequence();
  133     _default_keysequences["Shortcuts/Editor/Web Search for Selected Text"] = QKeySequence();
  134 
  135     const QStringList sc(_default_keysequences.keys());
  136     for (int i = 0; i < sc.count(); ++i)
  137         setValue(sc.at(i), value(sc.at(i), _default_keysequences.value(sc.at(i))));
  138 
  139     update();
  140 }
  141 
  142 void c2bSettings::setKeysequences(const QHash<QString, QKeySequence>& keysequences)
  143 {
  144     if (keysequences.size() == 0)
  145         return;
  146     QHash<QString, QKeySequence>::const_iterator it(keysequences.constBegin());
  147     while (it != keysequences.constEnd())
  148     {
  149         setValue(it.key(), it.value());
  150         ++it;
  151     }
  152 
  153     // Set keysequences to created objects
  154     const QString sctag("Shortcuts/");
  155     foreach (QWidget* w, QApplication::topLevelWidgets())
  156     {
  157         const QList<QAction*> alist(w->findChildren<QAction*>());
  158         for (int i = 0; i < alist.count(); ++i)
  159         {
  160             const QString fullname(alist.at(i)->data().toString());
  161             if (fullname.startsWith(sctag))
  162                 alist.at(i)->setShortcut(keysequence(fullname));
  163         }
  164         const QList<QShortcut*> sclist(w->findChildren<QShortcut*>());
  165         for (int i = 0; i < sclist.count(); ++i)
  166         {
  167             const QString fullname(sclist.at(i)->objectName());
  168             if (fullname.startsWith(sctag))
  169                 sclist.at(i)->setKey(keysequence(fullname));
  170         }
  171     }
  172 }
  173 
  174 void c2bSettings::setKeysequence(const QString& path, QAction* action)
  175 {
  176     if (!action)
  177         return;
  178     const QString fullname(path + action->text());
  179     if (_default_keysequences.contains(fullname))
  180     {
  181         action->setShortcut(keysequence(fullname));
  182         action->setData(fullname);
  183     }
  184     else
  185         c2bUtils::warn(tr("Action '%1' is not registered in default keysequence list").arg(fullname));
  186 }
  187 
  188 void c2bSettings::setKeysequence(const QString& fullname, QShortcut* shortcut)
  189 {
  190     if (!shortcut)
  191         return;
  192     if (_default_keysequences.contains(fullname))
  193     {
  194         shortcut->setObjectName(fullname);
  195         shortcut->setKey(keysequence(fullname));
  196     }
  197     else
  198         c2bUtils::warn(tr("Shortcut '%1' is not registered in default keysequence list").arg(fullname));
  199 }
  200 
  201 #ifdef Q_OS_WIN
  202 #include <QFileDialog>
  203 QString c2bSettings::decodeFilename(const QString& fn) const
  204 {
  205     // This function is meant for fn given by the command shell and that will end up being visible
  206     const QString dfn(settings::decodeFilename(fn));
  207     QFileDialog fd(0, QString(), dfn);
  208     const QStringList fns(fd.selectedFiles());
  209     if (fns.count() == 0)
  210         return dfn;
  211     const QString wdfn(fns.first());
  212     if (QFileInfo(wdfn).exists())
  213         return QDir::toNativeSeparators(wdfn);
  214     else
  215         return dfn;
  216 }
  217 #endif
  218 
  219 /***************************************************************************
  220  *  Downloaded reference extraction
  221  ***************************************************************************/
  222 
  223 void c2bSettings::processReferenceFile()
  224 {
  225     // The "Download reference to reference manager" functionality to cb2Bib is implemented as follows:
  226     // - Put downloaded reference into the clipboard
  227     // - If no cb2Bib instance is running, launch cb2Bib GUI, otherwise exit
  228     // This is simple, enough, and consistent with a 'clipboard to BibTeX' program.
  229     // The 'single instance' implementation is naive, but it is sufficient for cb2Bib, and does not prevent
  230     // from having multiple cb2Bib instances in its usual deployment, if so desired.
  231     // Aware of QClipboard X11 note: "You should not store or retrieve the clipboard contents in response
  232     // to timer or non-user-input events". Functions processReferenceFile() and processprocessReferenceFileEvent()
  233     // do not follow it. It seems to work smooth, for all platforms, and, overall, it is a feasible solution
  234     // in this particular case.
  235 
  236     QFile file(cl_import_filename);
  237     if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
  238     {
  239         cl_cb2bib_import_mode = true;
  240         QMessageBox::warning(0, tr("Warning - cb2Bib"),
  241                              tr("Unable to open the reference file %1 for reading.\nError: '%2'.")
  242                              .arg(cl_import_filename, file.errorString()),
  243                              QMessageBox::Ok);
  244         QTimer::singleShot(500, qApp, SLOT(quit()));
  245     }
  246     else
  247     {
  248         if (value("cb2Bib/isRunning", false).toBool())
  249         {
  250             cl_cb2bib_import_mode = true;
  251             if (!_progress_bar)
  252                 _progress_bar = new QProgressBar;
  253             _progress_bar->setRange(0, 100);
  254             _progress_bar->setWindowTitle(QObject::tr("Importing..."));
  255             _progress_bar->show();
  256             _progress_bar->setValue(50);
  257         }
  258         QTimer::singleShot(500, this, SLOT(processReferenceFileEvent()));
  259     }
  260 }
  261 
  262 void c2bSettings::processReferenceFileEvent()
  263 {
  264     const QString reference(c2bUtils::fileToString(cl_import_filename).trimmed());
  265     QClipboard* cb = qApp->clipboard();
  266     cb->setText(reference, QClipboard::Clipboard);
  267     if (cl_cb2bib_import_mode)
  268     {
  269         if (_progress_bar)
  270             _progress_bar->setValue(100);
  271         QTimer::singleShot(500, qApp, SLOT(quit()));
  272     }
  273 }
  274 
  275 void c2bSettings::guiExits()
  276 {
  277     setValue("cb2Bib/isRunning", false);
  278     sync();
  279 }