[KLF Application][KLF Tools][KLF Backend][KLF Home]
KLatexFormula Project
klfconfig.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * file klfconfig.cpp
3  * This file is part of the KLatexFormula Project.
4  * Copyright (C) 2011 by Philippe Faist
5  * philippe.faist at bluewin.ch
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  ***************************************************************************/
22 /* $Id: klfconfig.cpp 627 2011-04-12 12:36:22Z phfaist $ */
23 
24 #include <iostream>
25 
26 #include <QApplication>
27 #include <QMessageBox>
28 #include <QObject>
29 #include <QDir>
30 #include <QTextStream>
31 #include <QFont>
32 #include <QFontDatabase>
33 #include <QMap>
34 #include <QString>
35 #include <QListView> // icon view flow
36 #include <QLocale>
37 #include <QDesktopServices> // "My Documents" or "Documents" directory
38 
39 #include <klfmainwin.h>
40 
41 #include <klfutil.h>
42 #include "klfmain.h"
43 #include "klfconfig.h"
44 
45 
46 static const char * __klf_fallback_share_dir =
47 #if defined(Q_OS_WIN32) || defined(Q_OS_WIN64) // windows
48  ".."; // note: program is in a bin/ directory by default (this is for nsis-installer)
49 #elif defined(Q_OS_MAC) || defined(Q_OS_DARWIN) // Mac OS X
50  "../Resources";
51 #else // unix-like system
52  "../share/klatexformula";
53 #endif
54 
55 
56 static const char * __klf_share_dir =
57 #ifdef KLF_SHARE_DIR // defined by the build system
58  KLF_SHARE_DIR;
59 #else
60  NULL;
61 #endif
62 
64 
66 {
67  if (!__klf_share_dir_cached.isEmpty())
68  return __klf_share_dir_cached;
69 
70  klfDbg(klfFmtCC("cmake-share-dir=%s; fallback-share-dir=%s\n", __klf_share_dir,
72 
73  QString sharedir;
74  if (__klf_share_dir != NULL)
75  sharedir = QLatin1String(__klf_share_dir);
76  else
78 
79  __klf_share_dir_cached = klfPrefixedPath(sharedir); // prefixed by app-dir-path
80  klfDbg("share dir is "<<__klf_share_dir_cached) ;
82 }
83 
84 
85 
86 // global variable to access our config
87 // remember to initialize it in main() in main.cpp !
89 
90 
91 
92 /*void settings_write_QTextCharFormat(QSettings& s, const QString& basename,
93  const QTextCharFormat& charfmt)
94  {
95  s.setValue(basename+"_charformat", charfmt);
96  }
97 */
99  const QTextCharFormat& dflt)
100 {
101  QVariant val = s.value(basename+"_charformat", dflt);
102  QTextFormat tf = val.value<QTextFormat>();
103  return tf.toCharFormat();
104 }
105 /*
106  template<class T>
107  void settings_write_list(QSettings& s, const QString& basename, const QList<T>& list)
108  {
109  QList<QVariant> l;
110  int k;
111  for (k = 0; k < list.size(); ++k)
112  l.append(QVariant(list[k]));
113  s.setValue(basename+"_list", l);
114  }
115 */
116 
117 template<class T>
118 static QList<T> settings_read_list(QSettings& s, const QString& basename, const QList<T>& dflt)
119 {
120  QList<QVariant> l = s.value(basename+"_list", QList<QVariant>()).toList();
121  if (l.size() == 0)
122  return dflt;
123  QList<T> list;
124  int k;
125  for (k = 0; k < l.size(); ++k)
126  list.append(l[k].value<T>());
127  return list;
128 }
129 
130 
131 // -----------------------------------------------------
132 
133 
135 {
136 }
137 
138 
139 #define KLFCONFIG_TEST_FIXED_FONT(found_fcode, fdb, fcode, f, fps) \
140  if (!found_fcode && fdb.isFixedPitch(f)) { \
141  fcode = QFont(f, fps); \
142  found_fcode = true; \
143  }
144 
145 static int adjust_font_size(QFont f, int idealMXHeightPx)
146 {
147  // idealMXHeightPx is the ideal height of the string "MX" in pixels.
148  int ps = QFontInfo(f).pointSize();
149  // start with a little bit smaller font
150  ps -= 3;
151  const int cutoff = 20; //just a cutoff to be sure
152  // and increase font size up to something "ideal"
153  while (ps < cutoff && QFontMetrics(f).size(Qt::TextSingleLine, "MX").height() < idealMXHeightPx) {
154  f.setPointSize(++ps);
155  klfDbg(f.family()<<": Will try instead with font size="<<ps) ;
156  }
157  if (ps >= cutoff)
158  ps = 10; // the default point size in bad case scenario
159 
160  return ps;
161 }
162 
163 
165 {
167 
168  homeConfigDir = QDir::homePath() + "/.klatexformula";
170  homeConfigSettingsFile = homeConfigDir + "/klatexformula.conf";
172  homeConfigDirRCCResources = homeConfigDir + "/rccresources";
173  homeConfigDirPlugins = homeConfigDir + "/plugins";
174  homeConfigDirPluginData = homeConfigDir + "/plugindata";
175  homeConfigDirI18n = homeConfigDir + "/i18n";
176 
177  //debug: QMessageBox::information(0, "", QString("global share dir=")+globalShareDir);
178 
179  if (qApp->inherits("QApplication")) { // and not QCoreApplication...
180 
181  QFontDatabase fdb;
183 
184  //#ifdef Q_WS_X11
185  // // setting pixel size avoids bug with Qt/X11 of fonts having their metrics badly calculated (...?)
186  // f.setPixelSize(15);
187  //#endif
188 
189  defaultStdFont = f;
190 
191  QFont cmuappfont = f;
192  if (fdb.families().contains("CMU Sans Serif")) {
193  // CMU Sans Serif is available ;-)
194  int fps = QFontInfo(f).pointSize();
195  cmuappfont = QFont("CMU Sans Serif", fps);
196  // ideal height of the string "MX" in pixels. This value was CAREFULLY ADJUSTED.
197  // please change it only if you feel sure. (fonts have to look nice on most platforms)
198 #ifdef Q_WS_X11
199  int fIdealHeight = 17;
200 #else
201  int fIdealHeight = 15;
202 #endif
203  fps = adjust_font_size(cmuappfont, fIdealHeight);
204  cmuappfont.setPointSize(fps);
205  }
206 
207  QFont fcode;
208  bool found_fcode = false;
209  int ps = 11;
210  KLFCONFIG_TEST_FIXED_FONT(found_fcode, fdb, fcode, "Courier 10 Pitch", ps);
211  KLFCONFIG_TEST_FIXED_FONT(found_fcode, fdb, fcode, "ETL Fixed", ps);
212  KLFCONFIG_TEST_FIXED_FONT(found_fcode, fdb, fcode, "Courier New", ps);
213  KLFCONFIG_TEST_FIXED_FONT(found_fcode, fdb, fcode, "Efont Fixed", ps);
214  KLFCONFIG_TEST_FIXED_FONT(found_fcode, fdb, fcode, "Adobe Courier", ps);
215  KLFCONFIG_TEST_FIXED_FONT(found_fcode, fdb, fcode, "Courier", ps);
216  KLFCONFIG_TEST_FIXED_FONT(found_fcode, fdb, fcode, "Misc Fixed", ps);
217  KLFCONFIG_TEST_FIXED_FONT(found_fcode, fdb, fcode, "Monospace", ps);
218  if ( ! found_fcode )
219  fcode = f;
220  // guess good font size for code font
221 #ifdef Q_WS_X11
222  int fcodeIdealHeight = 20; // the ideal height of the string "MX" in pixels
223 #else
224  int fcodeIdealHeight = 18; // the ideal height of the string "MX" in pixels
225 #endif
226 // #if QT_VERSION < 0x040500
227 // fcodeIdealHeight += 2; // fix for Qt 4.4
228 // #endif
229  ps = adjust_font_size(fcode, fcodeIdealHeight);
230  fcode.setPointSize(ps);
231  QFont fcodeMain = fcode;
232  fcodeMain.setPointSize(ps+1);
233 
234  // by default, this is first run!
235  Core.thisVersionMajFirstRun = true;
236  Core.thisVersionMajMinFirstRun = true;
237  Core.thisVersionMajMinRelFirstRun = true;
238  Core.thisVersionExactFirstRun = true;
239 
240  Core.libraryFileName = "library.klf.db";
241  Core.libraryLibScheme = "klf+sqlite";
242 
243  defaultCMUFont = cmuappfont;
244  defaultTTFont = fcode;
245 
246  UI.locale = QLocale::system().name();
247  klfDbg("System locale: "<<QLocale::system().name());
248 #ifdef KLF_NO_CMU_FONT
249  UI.useSystemAppFont = true;
250  UI.applicationFont = defaultStdFont;
251 #else
252  UI.useSystemAppFont = false;
253  UI.applicationFont = cmuappfont;
254 #endif
255  UI.latexEditFont = fcodeMain;
256  UI.preambleEditFont = fcode;
257  UI.previewTooltipMaxSize = QSize(800, 600);
258  UI.labelOutputFixedSize = QSize(280, 80);
259  UI.lastSaveDir = QDir::homePath();
260  UI.symbolsPerLine = 6;
261  UI.userColorList = QList<QColor>();
262  UI.userColorList.append(QColor(0,0,0));
263  UI.userColorList.append(QColor(255,255,255));
264  UI.userColorList.append(QColor(170,0,0));
265  UI.userColorList.append(QColor(0,0,128));
266  UI.userColorList.append(QColor(0,0,255));
267  UI.userColorList.append(QColor(0,85,0));
268  UI.userColorList.append(QColor(255,85,0));
269  UI.userColorList.append(QColor(0,255,255));
270  UI.userColorList.append(QColor(85,0,127));
271  UI.userColorList.append(QColor(128,255,255));
272  UI.colorChooseWidgetRecent = QList<QColor>();
273  UI.colorChooseWidgetCustom = QList<QColor>();
274  UI.maxUserColors = 12;
275  UI.enableToolTipPreview = false;
276  UI.enableRealTimePreview = true;
277  UI.autosaveLibraryMin = 5;
278  UI.showHintPopups = true;
279  UI.clearLatexOnly = false;
280  UI.copyExportProfile = "default";
281  UI.dragExportProfile = "default";
282  UI.glowEffect = false;
283  UI.glowEffectColor = QColor(128, 255, 128, 12);
284  UI.glowEffectRadius = 4;
285  UI.customMathModes = QStringList();
286  UI.showExportProfilesLabel = true;
287  UI.menuExportProfileAffectsDrag = true;
288  UI.menuExportProfileAffectsCopy = true;
289 
290  SyntaxHighlighter.configFlags = 0x05;
291  SyntaxHighlighter.fmtKeyword = QTextCharFormat();
292  SyntaxHighlighter.fmtKeyword.setForeground(QColor(0, 0, 128));
293  SyntaxHighlighter.fmtComment = QTextCharFormat();
294  SyntaxHighlighter.fmtComment.setForeground(QColor(180, 0, 0));
295  SyntaxHighlighter.fmtComment.setFontItalic(true);
296  SyntaxHighlighter.fmtParenMatch = QTextCharFormat();
297  SyntaxHighlighter.fmtParenMatch.setBackground(QColor(180, 238, 180));
298  SyntaxHighlighter.fmtParenMismatch = QTextCharFormat();
299  SyntaxHighlighter.fmtParenMismatch.setBackground(QColor(255, 20, 147));
300  SyntaxHighlighter.fmtLonelyParen = QTextCharFormat();
301  SyntaxHighlighter.fmtLonelyParen.setForeground(QColor(255, 0, 255));
302  SyntaxHighlighter.fmtLonelyParen.setFontWeight(QFont::Bold);
303  }
304 
305  // invalid value, by convention ".". if the config is not read from file, then settings will
306  // be detected in detectMissingSettings()
307  BackendSettings.tempDir = ".";
308  BackendSettings.execLatex = ".";
309  BackendSettings.execDvips = ".";
310  BackendSettings.execGs = ".";
311  BackendSettings.execEpstopdf = ".";
312  BackendSettings.execenv = QStringList();
313 
314  BackendSettings.lborderoffset = 0;
315  BackendSettings.tborderoffset = 0;
316  BackendSettings.rborderoffset = 0;
317  BackendSettings.bborderoffset = 0;
318  BackendSettings.outlineFonts = true;
319 
320  LibraryBrowser.colorFound = QColor(128, 255, 128);
321  LibraryBrowser.colorNotFound = QColor(255, 128, 128);
322  LibraryBrowser.restoreURLs = false;
323  LibraryBrowser.confirmClose = true;
324  LibraryBrowser.groupSubCategories = true;
325  LibraryBrowser.iconViewFlow = QListView::TopToBottom;
326  LibraryBrowser.historyTagCopyToArchive = true;
327  LibraryBrowser.lastFileDialogPath = // "My Documents":
328  QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
329  LibraryBrowser.treePreviewSizePercent = 75;
330  LibraryBrowser.listPreviewSizePercent = 75;
331  LibraryBrowser.iconPreviewSizePercent = 100;
332 
334 }
335 
337 {
338  int neededsettings =
339  !BackendSettings.tempDir.compare(".") << 0 |
340  !BackendSettings.execLatex.compare(".") << 1 |
341  !BackendSettings.execDvips.compare(".") << 2 |
342  !BackendSettings.execGs.compare(".") << 3 |
343  !BackendSettings.execEpstopdf.compare(".") << 4 ;
344 
345  if (neededsettings) {
346  KLFBackend::klfSettings defaultsettings;
347  KLFBackend::detectSettings(&defaultsettings);
348  if (neededsettings & (1<<0))
349  BackendSettings.tempDir = defaultsettings.tempdir;
350  if (neededsettings & (1<<1))
351  BackendSettings.execLatex = defaultsettings.latexexec;
352  if (neededsettings & (1<<2))
353  BackendSettings.execDvips = defaultsettings.dvipsexec;
354  if (neededsettings & (1<<3))
355  BackendSettings.execGs = defaultsettings.gsexec;
356  if (neededsettings & (1<<4))
357  BackendSettings.execEpstopdf = defaultsettings.epstopdfexec;
358  BackendSettings.execenv << defaultsettings.execenv;
359  }
360 
361 }
362 
363 
364 
366 {
367  if ( !klfEnsureDir(homeConfigDir) )
368  return -1;
370  return -1;
372  return -1;
374  return -1;
376  return -1;
377 
378  return 0;
379 }
380 
381 
383 {
384  return QFileInfo(BackendSettings.execLatex).isExecutable() &&
385  QFileInfo(BackendSettings.execDvips).isExecutable() &&
387  ( BackendSettings.execEpstopdf.isEmpty() ||
388  QFileInfo(BackendSettings.execEpstopdf).isExecutable()) ;
389 }
390 
391 
392 
393 
395 {
396  klfDbgT(" reading config.") ;
397 
399 
400  QString globalconfig = globalShareDir+"/klatexformula.conf";
401  klfDbg("Testing for global config file "<<globalconfig);
402  if (QFile::exists(globalconfig)) {
403  klfDbg("Reading configuration from "<<globalconfig);
404  // pre-load global settings
405  readFromConfig_v2(globalconfig);
406  }
407 
409  klfDbg("Reading configuration from "<<homeConfigSettingsFile<<" ...");
410  return readFromConfig_v2(homeConfigSettingsFile);
411  }
413  return readFromConfig_v1();
414  }
415 
416  return -1;
417 }
418 
419 template<class T>
420 static void klf_config_read(QSettings &s, const QString& baseName, T *target,
421  const char * listOrMapType = NULL)
422 {
423  // qDebug("klf_config_read<...>(%s)", qPrintable(baseName));
424  QVariant defVal = QVariant::fromValue<T>(*target);
425  QVariant valstrv = s.value(baseName, QVariant());
426  // klfDbg( "\tRead value "<<valstr ) ;
427  if (valstrv.isNull()) {
428  // no such entry in config
429  klfDbg("No entry "<<baseName<<" in config.") ;
430  return;
431  }
432  QString valstr = valstrv.toString();
433  QVariant val = klfLoadVariantFromText(valstr.toLatin1(), defVal.typeName(), listOrMapType);
434  if (val.isValid())
435  *target = val.value<T>();
436 }
437 template<>
439  QTextCharFormat *target,
440  const char * /*listOrMapType*/)
441 {
442  qDebug("klf_config_read<QTextCharFormat>(%s)", qPrintable(baseName));
443  QTextFormat fmt = *target;
444  klf_config_read(s, baseName, &fmt);
445  *target = fmt.toCharFormat();
446 }
447 
448 template<class T>
449 static void klf_config_read_list(QSettings &s, const QString& baseName, QList<T> *target)
450 {
451  QVariantList vlist = klfListToVariantList(*target);
452  klf_config_read(s, baseName, &vlist, QVariant::fromValue<T>(T()).typeName());
453  *target = klfVariantListToList<T>(vlist);
454 }
455 
456 
457 template<class T>
458 static void klf_config_write(QSettings &s, const QString& baseName, const T * value)
459 {
460  QVariant val = QVariant::fromValue<T>(*value);
461  QByteArray datastr = klfSaveVariantToText(val);
462  s.setValue(baseName, QVariant::fromValue<QString>(QString::fromLocal8Bit(datastr)));
463 }
464 
465 template<class T>
466 static void klf_config_write_list(QSettings &s, const QString& baseName, const QList<T> * target)
467 {
468  QVariantList vlist = klfListToVariantList(*target);
469  klf_config_write(s, baseName, &vlist);
470 }
471 
473 {
474  QString s = QString("versionFirstRun-%1_").arg(N);
475  if (N >= 4)
476  return s + QLatin1String(KLF_VERSION_STRING);
477 
478  if (N-- > 0)
479  s += QString("%1").arg(KLF_VERSION_MAJ);
480  if (N-- > 0)
481  s += QString(".%1").arg(KLF_VERSION_MIN);
482  if (N-- > 0)
483  s += QString(".%1").arg(KLF_VERSION_REL);
484  return s;
485 }
486 
487 int KLFConfig::readFromConfig_v2(const QString& fname)
488 {
489  KLF_DEBUG_TIME_BLOCK(KLF_FUNC_NAME) ;
490 
491  QSettings s(fname, QSettings::IniFormat);
492 
493  qDebug("Reading base configuration");
494 
495  s.beginGroup("Core");
496  klf_config_read(s, firstRunConfigKey(1), &Core.thisVersionMajFirstRun);
497  klf_config_read(s, firstRunConfigKey(2), &Core.thisVersionMajMinFirstRun);
498  klf_config_read(s, firstRunConfigKey(3), &Core.thisVersionMajMinRelFirstRun);
499  klf_config_read(s, firstRunConfigKey(4), &Core.thisVersionExactFirstRun);
500  klf_config_read(s, "libraryfilename", &Core.libraryFileName);
501  klf_config_read(s, "librarylibscheme", &Core.libraryLibScheme);
502  s.endGroup();
503 
504  s.beginGroup("UI");
505  klf_config_read(s, "locale", &UI.locale);
506  klf_config_read(s, "usesystemfont", &UI.useSystemAppFont);
507  klf_config_read(s, "applicationfont", &UI.applicationFont);
508  klf_config_read(s, "latexeditfont", &UI.latexEditFont);
509  klf_config_read(s, "preambleeditfont", &UI.preambleEditFont);
510  klf_config_read(s, "previewtooltipmaxsize", &UI.previewTooltipMaxSize);
511  klf_config_read(s, "lbloutputfixedsize", &UI.labelOutputFixedSize);
512  klf_config_read(s, "lastsavedir", &UI.lastSaveDir);
513  klf_config_read(s, "symbolsperline", &UI.symbolsPerLine);
514  klf_config_read_list(s, "usercolorlist", &UI.userColorList);
515  klf_config_read_list(s, "colorchoosewidgetrecent", &UI.colorChooseWidgetRecent);
516  klf_config_read_list(s, "colorchoosewidgetcustom", &UI.colorChooseWidgetCustom);
517  klf_config_read(s, "maxusercolors", &UI.maxUserColors);
518  klf_config_read(s, "enabletooltippreview", &UI.enableToolTipPreview);
519  klf_config_read(s, "enablerealtimepreview", &UI.enableRealTimePreview);
520  klf_config_read(s, "autosavelibrarymin", &UI.autosaveLibraryMin);
521  klf_config_read(s, "showhintpopups", &UI.showHintPopups);
522  klf_config_read(s, "clearlatexonly", &UI.clearLatexOnly);
523  klf_config_read(s, "copyexportprofile", &UI.copyExportProfile);
524  klf_config_read(s, "dragexportprofile", &UI.dragExportProfile);
525  klf_config_read(s, "gloweffect", &UI.glowEffect);
526  klf_config_read(s, "gloweffectcolor", &UI.glowEffectColor);
527  klfDbg("Read glow effect color from config: color="<<UI.glowEffectColor
528  <<", alpha="<<UI.glowEffectColor.alpha());
529  klf_config_read(s, "gloweffectradius", &UI.glowEffectRadius);
530  klf_config_read(s, "custommathmodes", &UI.customMathModes);
531  klf_config_read(s, "showexportprofileslabel", &UI.showExportProfilesLabel);
532  klf_config_read(s, "menuexportprofileaffectsdrag", &UI.menuExportProfileAffectsDrag);
533  klf_config_read(s, "menuexportprofileaffectscopy", &UI.menuExportProfileAffectsCopy);
534  s.endGroup();
535 
536  s.beginGroup("SyntaxHighlighter");
537  klf_config_read(s, "configflags", &SyntaxHighlighter.configFlags);
538  klf_config_read<QTextCharFormat>(s, "keyword", &SyntaxHighlighter.fmtKeyword);
539  klf_config_read<QTextCharFormat>(s, "comment", &SyntaxHighlighter.fmtComment);
540  klf_config_read<QTextCharFormat>(s, "parenmatch", &SyntaxHighlighter.fmtParenMatch);
541  klf_config_read<QTextCharFormat>(s, "parenmismatch", &SyntaxHighlighter.fmtParenMismatch);
542  klf_config_read<QTextCharFormat>(s, "lonelyparen", &SyntaxHighlighter.fmtLonelyParen);
543  s.endGroup();
544 
545  s.beginGroup("BackendSettings");
546  klf_config_read(s, "tempdir", &BackendSettings.tempDir);
547  klf_config_read(s, "latexexec", &BackendSettings.execLatex);
548  klf_config_read(s, "dvipsexec", &BackendSettings.execDvips);
549  klf_config_read(s, "gsexec", &BackendSettings.execGs);
550  klf_config_read(s, "epstopdfexec", &BackendSettings.execEpstopdf);
551  klf_config_read(s, "execenv", &BackendSettings.execenv);
552  klf_config_read(s, "lborderoffset", &BackendSettings.lborderoffset);
553  klf_config_read(s, "tborderoffset", &BackendSettings.tborderoffset);
554  klf_config_read(s, "rborderoffset", &BackendSettings.rborderoffset);
555  klf_config_read(s, "bborderoffset", &BackendSettings.bborderoffset);
556  klf_config_read(s, "outlinefonts", &BackendSettings.outlineFonts);
557  s.endGroup();
558 
559  s.beginGroup("LibraryBrowser");
560  klf_config_read(s, "colorfound", &LibraryBrowser.colorFound);
561  klf_config_read(s, "colornotfound", &LibraryBrowser.colorNotFound);
562  klf_config_read(s, "restoreurls", &LibraryBrowser.restoreURLs);
563  klf_config_read(s, "confirmclose", &LibraryBrowser.confirmClose);
564  klf_config_read(s, "groupsubcategories", &LibraryBrowser.groupSubCategories);
565  klf_config_read(s, "iconviewflow", &LibraryBrowser.iconViewFlow);
566  klf_config_read(s, "historytagcopytoarchive", &LibraryBrowser.historyTagCopyToArchive);
567  klf_config_read(s, "lastfiledialogpath", &LibraryBrowser.lastFileDialogPath);
568  klf_config_read(s, "treepreviewsizepercent", &LibraryBrowser.treePreviewSizePercent);
569  klf_config_read(s, "listpreviewsizepercent", &LibraryBrowser.listPreviewSizePercent);
570  klf_config_read(s, "iconpreviewsizepercent", &LibraryBrowser.iconPreviewSizePercent);
571  s.endGroup();
572 
573  // Special treatment for Plugins.pluginConfig
574  // for reading, we cannot rely on klf_plugins since we are called before plugins are loaded!
575  int k, j;
576  QDir plugindatadir = QDir(homeConfigDirPluginData);
577  QStringList plugindirs = plugindatadir.entryList(QDir::Dirs);
578  for (k = 0; k < plugindirs.size(); ++k) {
579  if (plugindirs[k] == "." || plugindirs[k] == "..")
580  continue;
581  qDebug("Reading config for plugin %s", qPrintable(plugindirs[k]));
582  QString fn = plugindatadir.absoluteFilePath(plugindirs[k])+"/"+plugindirs[k]+".conf";
583  if ( ! QFile::exists(fn) ) {
584  qDebug("\tskipping plugin %s since the file %s does not exist.",
585  qPrintable(plugindirs[k]), qPrintable(fn));
586  continue;
587  }
588  QSettings psettings(fn, QSettings::IniFormat);
589  QVariantMap pconfmap;
590  QStringList keys = psettings.allKeys();
591  for (j = 0; j < keys.size(); ++j) {
592  pconfmap[keys[j]] = psettings.value(keys[j]);
593  }
594  Plugins.pluginConfig[plugindirs[k]] = pconfmap;
595  }
596 
597  // POST-CONFIG-READ SETUP
598 
599  // forbid empty locale
600  if (klfconfig.UI.locale.isEmpty())
601  klfconfig.UI.locale = "en_US";
602  // set Qt default locale to ours
603  QLocale::setDefault(klfconfig.UI.locale);
604 
605  return 0;
606 }
607 
608 
610 {
612  QSettings s(homeConfigSettingsFile, QSettings::IniFormat);
613 
614  bool thisVersionFirstRunFalse = false;
615 
616  s.beginGroup("Core");
617  klf_config_write(s, firstRunConfigKey(1), &thisVersionFirstRunFalse);
618  klf_config_write(s, firstRunConfigKey(2), &thisVersionFirstRunFalse);
619  klf_config_write(s, firstRunConfigKey(3), &thisVersionFirstRunFalse);
620  klf_config_write(s, firstRunConfigKey(4), &thisVersionFirstRunFalse);
621  klf_config_write(s, "libraryfilename", &Core.libraryFileName);
622  klf_config_write(s, "librarylibscheme", &Core.libraryLibScheme);
623  s.endGroup();
624 
625  s.beginGroup("UI");
626  klf_config_write(s, "locale", &UI.locale);
627  klf_config_write(s, "usesystemfont", &UI.useSystemAppFont);
628  klf_config_write(s, "applicationfont", &UI.applicationFont);
629  klf_config_write(s, "latexeditfont", &UI.latexEditFont);
630  klf_config_write(s, "preambleeditfont", &UI.preambleEditFont);
631  klf_config_write(s, "previewtooltipmaxsize", &UI.previewTooltipMaxSize);
632  klf_config_write(s, "lbloutputfixedsize", &UI.labelOutputFixedSize);
633  klf_config_write(s, "lastsavedir", &UI.lastSaveDir);
634  klf_config_write(s, "symbolsperline", &UI.symbolsPerLine);
635  klf_config_write_list(s, "usercolorlist", &UI.userColorList);
636  klf_config_write_list(s, "colorchoosewidgetrecent", &UI.colorChooseWidgetRecent);
637  klf_config_write_list(s, "colorchoosewidgetcustom", &UI.colorChooseWidgetCustom);
638  klf_config_write(s, "maxusercolors", &UI.maxUserColors);
639  klf_config_write(s, "enabletooltippreview", &UI.enableToolTipPreview);
640  klf_config_write(s, "enablerealtimepreview", &UI.enableRealTimePreview);
641  klf_config_write(s, "autosavelibrarymin", &UI.autosaveLibraryMin);
642  klf_config_write(s, "showhintpopups", &UI.showHintPopups);
643  klf_config_write(s, "clearlatexonly", &UI.clearLatexOnly);
644  klf_config_write(s, "copyexportprofile", &UI.copyExportProfile);
645  klf_config_write(s, "dragexportprofile", &UI.dragExportProfile);
646  klf_config_write(s, "gloweffect", &UI.glowEffect);
647  klf_config_write(s, "gloweffectcolor", &UI.glowEffectColor);
648  klf_config_write(s, "gloweffectradius", &UI.glowEffectRadius);
649  klf_config_write(s, "custommathmodes", &UI.customMathModes);
650  klf_config_write(s, "showexportprofileslabel", &UI.showExportProfilesLabel);
651  klf_config_write(s, "menuexportprofileaffectsdrag", &UI.menuExportProfileAffectsDrag);
652  klf_config_write(s, "menuexportprofileaffectscopy", &UI.menuExportProfileAffectsCopy);
653  s.endGroup();
654 
655  s.beginGroup("SyntaxHighlighter");
656  klf_config_write(s, "configflags", &SyntaxHighlighter.configFlags);
657  klf_config_write<QTextFormat>(s, "keyword", &SyntaxHighlighter.fmtKeyword);
658  klf_config_write<QTextFormat>(s, "comment", &SyntaxHighlighter.fmtComment);
659  klf_config_write<QTextFormat>(s, "parenmatch", &SyntaxHighlighter.fmtParenMatch);
660  klf_config_write<QTextFormat>(s, "parenmismatch", &SyntaxHighlighter.fmtParenMismatch);
661  klf_config_write<QTextFormat>(s, "lonelyparen", &SyntaxHighlighter.fmtLonelyParen);
662  s.endGroup();
663 
664  s.beginGroup("BackendSettings");
665  klf_config_write(s, "tempdir", &BackendSettings.tempDir);
666  klf_config_write(s, "latexexec", &BackendSettings.execLatex);
667  klf_config_write(s, "dvipsexec", &BackendSettings.execDvips);
668  klf_config_write(s, "gsexec", &BackendSettings.execGs);
669  klf_config_write(s, "epstopdfexec", &BackendSettings.execEpstopdf);
670  klf_config_write(s, "execenv", &BackendSettings.execenv);
671  klf_config_write(s, "lborderoffset", &BackendSettings.lborderoffset);
672  klf_config_write(s, "tborderoffset", &BackendSettings.tborderoffset);
673  klf_config_write(s, "rborderoffset", &BackendSettings.rborderoffset);
674  klf_config_write(s, "bborderoffset", &BackendSettings.bborderoffset);
675  klf_config_write(s, "outlinefonts", &BackendSettings.outlineFonts);
676  s.endGroup();
677 
678  s.beginGroup("LibraryBrowser");
679  klf_config_write(s, "colorfound", &LibraryBrowser.colorFound);
680  klf_config_write(s, "colornotfound", &LibraryBrowser.colorNotFound);
681  klf_config_write(s, "restoreurls", &LibraryBrowser.restoreURLs);
682  klf_config_write(s, "confirmclose", &LibraryBrowser.confirmClose);
683  klf_config_write(s, "groupsubcategories", &LibraryBrowser.groupSubCategories);
684  klf_config_write(s, "iconviewflow", &LibraryBrowser.iconViewFlow);
685  klf_config_write(s, "historytagcopytoarchive", &LibraryBrowser.historyTagCopyToArchive);
686  klf_config_write(s, "lastfiledialogpath", &LibraryBrowser.lastFileDialogPath);
687  klf_config_write(s, "treepreviewsizepercent", &LibraryBrowser.treePreviewSizePercent);
688  klf_config_write(s, "listpreviewsizepercent", &LibraryBrowser.listPreviewSizePercent);
689  klf_config_write(s, "iconpreviewsizepercent", &LibraryBrowser.iconPreviewSizePercent);
690  s.endGroup();
691 
692  // Special treatment for Plugins.pluginConfig
693  int k;
694  for (k = 0; k < klf_plugins.size(); ++k) {
695  QString fn = homeConfigDirPluginData+"/"+klf_plugins[k].name+"/"+klf_plugins[k].name+".conf";
696  QSettings psettings(fn, QSettings::IniFormat);
697  QVariantMap pconfmap = Plugins.pluginConfig[klf_plugins[k].name];
698  QVariantMap::const_iterator it;
699  for (it = pconfmap.begin(); it != pconfmap.end(); ++it) {
700  psettings.setValue(it.key(), it.value());
701  }
702  psettings.sync();
703  }
704 
705  s.sync();
706  return 0;
707 }
708 
709 
710 
711 
713 {
714  KLF_DEBUG_BLOCK(KLF_FUNC_NAME) ; klfDbg("... for plugin: "<<name) ;
715  return KLFPluginConfigAccess(this, name);
716 }
717 
718 
719 // --------------------------------------
720 
721 
722 
723 
725 {
726  _config = NULL;
727  _pluginname = QString::null;
728 }
730  : _config(other._config), _pluginname(other._pluginname)
731 {
732  klfDbg("made copy. _config="<<_config<<"; _pluginname="<<_pluginname) ;
733  if (_config != NULL) {
734  klfDbg("_config->homeConfigDir: "<<_config->homeConfigDir) ;
735  }
736 }
738 {
739 }
740 
742 {
743  _config = configObject;
744  _pluginname = pluginName;
745 
746  klfDbg("_config="<<_config<<", _pluginname="<<_pluginname) ;
747  if (_config != NULL) {
748  klfDbg("_config->homeConfigDir: "<<_config->homeConfigDir) ;
749  }
750 }
751 
752 
753 
755 {
756  if ( _config == NULL ) {
757  qWarning("KLFPluginConfigAccess::homeConfigDir: Invalid Config Pointer!\n");
758  return QString();
759  }
760  klfDbg("_config->homeConfigDir="<<_config->homeConfigDir) ;
761  return _config->homeConfigDir;
762 }
763 
765 {
766  if ( _config == NULL ) {
767  qWarning("KLFPluginConfigAccess::homeConfigDir: Invalid Config Pointer!\n");
768  return QString();
769  }
770 
771  return _config->globalShareDir;
772 }
773 
775 {
776  if ( _config == NULL ) {
777  qWarning("KLFPluginConfigAccess::tempDir: Invalid Config Pointer!\n");
778  return QString();
779  }
780 
781  return _config->BackendSettings.tempDir;
782 }
783 
785 {
786  if ( _config == NULL ) {
787  qWarning("KLFPluginConfigAccess::homeConfigPluginDataDir: Invalid Config Pointer!\n");
788  return QString();
789  }
790  klfDbg("_config->homeConfigDirPluginData is "<<_config->homeConfigDirPluginData) ;
791 
792  QString d = _config->homeConfigDirPluginData + "/" + _pluginname;
793  if ( createIfNeeded && ! klfEnsureDir(d) ) {
794  qWarning("KLFPluginConfigAccess::homeConfigPluginDataDir: Can't create directory: `%s'",
795  qPrintable(d));
796  return QString();
797  }
798  return d;
799 }
800 
802 {
803  if ( _config == NULL ) {
804  qWarning("KLFPluginConfigAccess::readValue: Invalid Config Pointer!\n");
805  return QVariant();
806  }
807 
808  if ( ! _config->Plugins.pluginConfig[_pluginname].contains(key) )
809  return QVariant();
810 
811  return _config->Plugins.pluginConfig[_pluginname][key];
812 }
813 
815 {
816  if ( _config == NULL ) {
817  qWarning("KLFPluginConfigAccess::makeDefaultValue: Invalid Config Pointer!\n");
818  return QVariant();
819  }
820 
821  if (_config->Plugins.pluginConfig[_pluginname].contains(key))
822  return _config->Plugins.pluginConfig[_pluginname][key];
823 
824  // assign the value into the plugin config, and return it
825  return ( _config->Plugins.pluginConfig[_pluginname][key] = defaultValue );
826 }
827 void KLFPluginConfigAccess::writeValue(const QString& key, const QVariant& value)
828 {
829  if ( _config == NULL ) {
830  qWarning("KLFPluginConfigAccess::writeValue: Invalid Config Pointer!\n");
831  return;
832  }
833 
834  _config->Plugins.pluginConfig[_pluginname][key] = value;
835 }
836 
837 
838 
839 
840 
841 
842 
843 
844 // ----------------------
845 
846 
847 
848 
849 
850 
851 
852 
853 
854 // KEPT FOR COMPATIBILITY WITH OLDER VERSIONS
855 
856 int KLFConfig::readFromConfig_v1()
857 {
858  QSettings s(homeConfigSettingsFileIni, QSettings::IniFormat);
859 
860  s.beginGroup("UI");
861  UI.locale = s.value("locale", UI.locale).toString();
862  // ingnore KLF 3.1 app font setting, we have our nice CMU Sans Serif font ;-)
863  // UI.applicationFont = s.value("applicationfont", UI.applicationFont).value<QFont>();
864  UI.latexEditFont = s.value("latexeditfont", UI.latexEditFont).value<QFont>();
865  UI.preambleEditFont = s.value("preambleeditfont", UI.preambleEditFont).value<QFont>();
866  UI.previewTooltipMaxSize = s.value("previewtooltipmaxsize", UI.previewTooltipMaxSize).toSize();
867  UI.labelOutputFixedSize = s.value("lbloutputfixedsize", UI.labelOutputFixedSize ).toSize();
868  UI.lastSaveDir = s.value("lastsavedir", UI.lastSaveDir).toString();
869  UI.symbolsPerLine = s.value("symbolsperline", UI.symbolsPerLine).toInt();
870  UI.userColorList = settings_read_list(s, "usercolorlist", UI.userColorList);
871  UI.colorChooseWidgetRecent = settings_read_list(s, "colorchoosewidgetrecent", UI.colorChooseWidgetRecent);
872  UI.colorChooseWidgetCustom = settings_read_list(s, "colorchoosewidgetcustom", UI.colorChooseWidgetCustom);
873  UI.maxUserColors = s.value("maxusercolors", UI.maxUserColors).toInt();
874  UI.enableToolTipPreview = s.value("enabletooltippreview", UI.enableToolTipPreview).toBool();
875  UI.enableRealTimePreview = s.value("enablerealtimepreview", UI.enableRealTimePreview).toBool();
876  UI.autosaveLibraryMin = s.value("autosavelibrarymin", UI.autosaveLibraryMin).toInt();
877  s.endGroup();
878 
879  s.beginGroup("SyntaxHighlighter");
880  SyntaxHighlighter.configFlags = s.value("configflags", SyntaxHighlighter.configFlags).toUInt();
881  SyntaxHighlighter.fmtKeyword = settings_read_QTextCharFormat(s, "keyword", SyntaxHighlighter.fmtKeyword);
882  SyntaxHighlighter.fmtComment = settings_read_QTextCharFormat(s, "comment", SyntaxHighlighter.fmtComment);
883  SyntaxHighlighter.fmtParenMatch = settings_read_QTextCharFormat(s, "parenmatch", SyntaxHighlighter.fmtParenMatch);
884  SyntaxHighlighter.fmtParenMismatch = settings_read_QTextCharFormat(s, "parenmismatch", SyntaxHighlighter.fmtParenMismatch);
885  SyntaxHighlighter.fmtLonelyParen = settings_read_QTextCharFormat(s, "lonelyparen", SyntaxHighlighter.fmtLonelyParen);
886  s.endGroup();
887 
888  s.beginGroup("BackendSettings");
889  BackendSettings.tempDir = s.value("tempdir", BackendSettings.tempDir).toString();
890  BackendSettings.execLatex = s.value("latexexec", BackendSettings.execLatex).toString();
891  BackendSettings.execDvips = s.value("dvipsexec", BackendSettings.execDvips).toString();
892  BackendSettings.execGs = s.value("gsexec", BackendSettings.execGs).toString();
893  BackendSettings.execEpstopdf = s.value("epstopdfexec", BackendSettings.execEpstopdf).toString();
894  BackendSettings.lborderoffset = s.value("lborderoffset", BackendSettings.lborderoffset).toInt();
895  BackendSettings.tborderoffset = s.value("tborderoffset", BackendSettings.tborderoffset).toInt();
896  BackendSettings.rborderoffset = s.value("rborderoffset", BackendSettings.rborderoffset).toInt();
897  BackendSettings.bborderoffset = s.value("bborderoffset", BackendSettings.bborderoffset).toInt();
898  s.endGroup();
899 
900  s.beginGroup("LibraryBrowser");
901  LibraryBrowser.colorFound = s.value("colorfound", LibraryBrowser.colorFound).value<QColor>();
902  LibraryBrowser.colorNotFound = s.value("colornotfound", LibraryBrowser.colorNotFound).value<QColor>();
903  s.endGroup();
904 
905  // Special treatment for Plugins.pluginConfig
906  s.beginGroup("Plugins/Config");
907  QStringList pluginList = s.childGroups();
908  s.endGroup();
909  int j;
910  for (j = 0; j < pluginList.size(); ++j) {
911  QString name = pluginList[j];
912  s.beginGroup( QString("Plugins/Config/%1").arg(name) );
913  QMap<QString,QVariant> thispluginconfig;
914  QStringList plconfkeys = s.childKeys();
915  int k;
916  for (k = 0; k < plconfkeys.size(); ++k) {
917  thispluginconfig[plconfkeys[k]] = s.value(plconfkeys[k]);
918  }
919  klfconfig.Plugins.pluginConfig[name] = thispluginconfig;
920  s.endGroup();
921  }
922 
923  return 0;
924 }
925 
virtual QVariant readValue(const QString &key) const
read a value in the config
Definition: klfconfig.cpp:801
static QString __klf_share_dir_cached
Definition: klfconfig.cpp:63
QFont defaultCMUFont
Definition: klfconfig.h:259
setPointSize(int pointSize)
QString locale
When setting this, don't forget to call QLocale::setDefault().
Definition: klfconfig.h:177
contains(const Key &key)
QFont defaultTTFont
Definition: klfconfig.h:263
QString homeConfigDirRCCResources
Definition: klfconfig.h:155
QMap< QString, QMap< QString, QVariant > > pluginConfig
Definition: klfconfig.h:254
#define klfDbgT(streamableItems)
KLFConfig klfconfig
Definition: klfconfig.cpp:88
bool checkExePaths()
Definition: klfconfig.cpp:382
struct KLFConfig::@1 UI
virtual ~KLFPluginConfigAccess()
Definition: klfconfig.cpp:737
arg(const QString &a, int fieldWidth=0, const QChar &fillChar=QLatin1Char( ' ')
#define klfDbg(streamableItems)
virtual QString homeConfigPluginDataDir(bool createIfNeeded=true) const
Definition: klfconfig.cpp:784
#define KLF_DEBUG_BLOCK(msg)
QString homeConfigDirI18n
Definition: klfconfig.h:158
QString tempDir
Definition: klfconfig.h:220
Utility class for plugins to access their configuration space in KLFConfig.
Definition: klfconfig.h:61
homePath()
KLF_EXPORT QString klfPrefixedPath(const QString &path, const QString &reference)
struct KLFConfig::@0 Core
value(int i)
QFont defaultStdFont
Definition: klfconfig.h:261
Structure that stores klatexformula's configuration in memory.
Definition: klfconfig.h:141
setValue(const QString &key, const QVariant &value)
static void klf_config_write(QSettings &s, const QString &baseName, const T *value)
Definition: klfconfig.cpp:458
fromLocal8Bit(const char *str, int size=-1)
append(const T &value)
int readFromConfig()
Definition: klfconfig.cpp:394
static void klf_config_read_list(QSettings &s, const QString &baseName, QList< T > *target)
Definition: klfconfig.cpp:449
KLF_EXPORT QVariant klfLoadVariantFromText(const QByteArray &stringdata, const char *dataTypeName, const char *listOrMapDataTypeName)
setDefault(const QLocale &locale)
entryList(const QStringList &nameFilters, Filters filters=NoFilter, SortFlags sort=NoSort)
QString homeConfigSettingsFileIni
OLD config file.
Definition: klfconfig.h:154
static void klf_config_write_list(QSettings &s, const QString &baseName, const QList< T > *target)
Definition: klfconfig.cpp:466
KLF_EXPORT bool klfEnsureDir(const QString &dir)
int ensureHomeConfigDir()
Definition: klfconfig.cpp:365
KLF_EXPORT QString klf_share_dir_abspath()
Definition: klfconfig.cpp:65
#define KLF_DEBUG_TIME_BLOCK(msg)
virtual QVariant makeDefaultValue(const QString &key, const QVariant &defaultValue)
write the value if inexistant in config
Definition: klfconfig.cpp:814
#define klfFmtCC
#define KLF_FUNC_NAME
struct KLFConfig::@2 SyntaxHighlighter
QList< KLFPluginInfo > klf_plugins
Definition: klfmain.cpp:50
struct KLFConfig::@3 BackendSettings
QString homeConfigDir
Definition: klfconfig.h:151
value(const QString &key, const QVariant &defaultValue=QVariant()
static const char * __klf_fallback_share_dir
Definition: klfconfig.cpp:46
QString homeConfigDirPluginData
Definition: klfconfig.h:157
void detectMissingSettings()
Definition: klfconfig.cpp:336
static void klf_config_read(QSettings &s, const QString &baseName, T *target, const char *listOrMapType=NULL)
Definition: klfconfig.cpp:420
families(WritingSystem writingSystem=Any)
int writeToConfig()
Definition: klfconfig.cpp:609
static QTextCharFormat settings_read_QTextCharFormat(QSettings &s, const QString &basename, const QTextCharFormat &dflt)
Definition: klfconfig.cpp:98
QVariantList klfListToVariantList(const QList< T > &list)
static const char * __klf_share_dir
Definition: klfconfig.cpp:56
static QString firstRunConfigKey(int N)
Definition: klfconfig.cpp:472
KLFPluginConfigAccess getPluginConfigAccess(const QString &name)
Definition: klfconfig.cpp:712
absoluteFilePath(const QString &fileName)
static QList< T > settings_read_list(QSettings &s, const QString &basename, const QList< T > &dflt)
Definition: klfconfig.cpp:118
KLF_EXPORT QByteArray klfSaveVariantToText(const QVariant &value, bool saveListAndMapsAsXML)
void loadDefaults()
Definition: klfconfig.cpp:164
QString globalShareDir
Definition: klfconfig.h:152
static int adjust_font_size(QFont f, int idealMXHeightPx)
Definition: klfconfig.cpp:145
#define KLFCONFIG_TEST_FIXED_FONT(found_fcode, fdb, fcode, f, fps)
Definition: klfconfig.cpp:139
virtual QString tempDir() const
Definition: klfconfig.cpp:774
virtual void writeValue(const QString &key, const QVariant &value)
write a value to settings
Definition: klfconfig.cpp:827
virtual QString homeConfigDir() const
Definition: klfconfig.cpp:754
static bool detectSettings(klfSettings *settings, const QString &extraPath=QString())
struct KLFConfig::@4 LibraryBrowser
virtual QString globalShareDir() const
Definition: klfconfig.cpp:764
QString homeConfigSettingsFile
current (now, "new" klatexformula.conf) settings file
Definition: klfconfig.h:153
void klf_config_read< QTextCharFormat >(QSettings &s, const QString &baseName, QTextCharFormat *target, const char *)
Definition: klfconfig.cpp:438
struct KLFConfig::@5 Plugins
beginGroup(const QString &prefix)
QString homeConfigDirPlugins
Definition: klfconfig.h:156
value(const Key &key)

Generated by doxygen 1.8.6