[KLF Application][KLF Tools][KLF Backend][KLF Home]
KLatexFormula Project
klfliblegacyengine_p.h
Go to the documentation of this file.
1 /***************************************************************************
2  * file klfliblegacyengine_p.h
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: klfliblegacyengine_p.h 603 2011-02-26 23:14:55Z phfaist $ */
23 
24 
29 #ifndef KLFLIBLEGACYENGINE_P_H
30 #define KLFLIBLEGACYENGINE_P_H
31 
32 #include <QObject>
33 #include <QMap>
34 #include <QFileInfo>
35 
36 #include "klfliblegacyengine.h"
37 
40 {
41  Q_OBJECT
42 public:
50  static inline KLFLibLegacyFileDataPrivate * instanceFor(const QString fname, bool starttimer)
51  {
52  QString f = canonicalFilePath(fname);
53  klfDbg("fname="<<fname<<"; canonical f="<<f<<"; starttimer="<<starttimer) ;
54  if (f.isEmpty()) {
55  qWarning()<<KLF_FUNC_NAME<<": error getting canonical file path for "<<fname<<".";
56  return NULL;
57  }
58  if (staticFileDataObjects.contains(f))
59  return staticFileDataObjects[f];
61  if (starttimer && !d->autoSaveTimer->isActive())
62  d->autoSaveTimer->start(180000); // 180s = 3min
63  return d;
64  }
65 
74  static QString canonicalFilePath(const QString& fname)
75  {
76  QFileInfo fi(fname);
77  if (fi.exists())
78  return fi.canonicalFilePath();
79  // non-existing file. Rely on existing directory
80  QString containdir = fi.absolutePath();
81  klfDbg("non-existing file "<<fname<<": containing dir="<<containdir) ;
82  QFileInfo di(containdir);
83  if (!di.exists() || !di.isDir()) {
84  qWarning()<<KLF_FUNC_NAME<<": Path "<<fname<<": directory "<<containdir<<" does not exist.";
85  return QString();
86  }
87  QString canonical = QFileInfo(containdir).canonicalFilePath();
88  if (canonical.isEmpty()) {
89  qWarning()<<KLF_FUNC_NAME<<": Error getting "<<containdir<<"'s canonical path.";
90  return QString();
91  }
92  if (!canonical.endsWith("/"))
93  canonical += "/";
94  canonical += fi.fileName();
95  return canonical;
96  }
97 
100  {
101  klfDbg("destroying. Possibly save? haschanges="<<haschanges) ;
102  if (haschanges)
103  save();
104 
105  staticFileDataObjects.remove(filename);
106  delete autoSaveTimer;
107  }
108 
110  inline void ref() { ++refcount; }
113  inline int deref() { return --refcount; }
114 
115  inline QString fileName() const { return filename; }
116 
117 
119 
121 
126 
136  QVariantMap metadata;
137 
139 
141 
143  int findResourceName(const QString& resname);
144  int getReservedResourceId(const QString& resourceName, int defaultId);
145 
146 
147 
149  {
151  item.preview.toImage(), item.preview.size(), item.category,
152  item.tags, toStyle(item.style));
153  }
155  {
158  // ensure latex has category & tags information
160  entry.category(), entry.tags()) ;
161  item.category = entry.category();
162  item.tags = entry.tags();
163  item.preview = QPixmap::fromImage(entry.preview());
164  item.datetime = entry.dateTime();
165  item.style = toLegacyStyle(entry.style());
166  return item;
167  }
168  static inline KLFLegacyData::KLFStyle toLegacyStyle(const KLFStyle& style)
169  {
170  KLFLegacyData::KLFStyle oldstyle;
171  oldstyle.name = style.name;
172  oldstyle.fg_color = style.fg_color;
173  oldstyle.bg_color = style.bg_color;
174  oldstyle.mathmode = style.mathmode;
175  oldstyle.preamble = style.preamble;
176  oldstyle.dpi = style.dpi;
177  return oldstyle;
178  }
179  static inline KLFStyle toStyle(const KLFLegacyData::KLFStyle& oldstyle)
180  {
181  KLFStyle style;
182  style.name = oldstyle.name;
183  style.fg_color = oldstyle.fg_color;
184  style.bg_color = oldstyle.bg_color;
185  style.mathmode = oldstyle.mathmode;
186  style.preamble = oldstyle.preamble;
187  style.dpi = oldstyle.dpi;
188  return style;
189  }
190 
191 signals:
192  void resourcePropertyChanged(int propId);
193 
194 public slots:
199  bool load(const QString& fname = QString());
200 
202  bool save(const QString& fname = QString());
203 
204  void emitResourcePropertyChanged(int propId) { emit resourcePropertyChanged(propId); }
205 
206 private:
208 
209  KLFLibLegacyFileDataPrivate(const QString& fname) : refcount(0), filename(fname)
210  {
211  klfDbg(" filename is "<<filename ) ;
212 
213  staticFileDataObjects[filename] = this;
214 
215  if (QFile::exists(fname))
216  load(); // load the data
217 
218  // by default, we're a .klf export type
220 
221  // prepare the autosave timer
222  autoSaveTimer = new QTimer(NULL);
223  autoSaveTimer->setSingleShot(false);
224  connect(autoSaveTimer, SIGNAL(timeout()), this, SLOT(save()));
225  }
226 
227  int refcount;
228 
229  QString filename;
230 
231  static QMap<QString,KLFLibLegacyFileDataPrivate*> staticFileDataObjects;
232 
233 };
234 
235 
236 
237 
238 #endif
static KLFLegacyData::KLFStyle toLegacyStyle(const KLFStyle &style)
contains(const Key &key)
int findResourceName(const QString &resname)
static KLFLibEntry toLibEntry(const KLFLegacyData::KLFLibraryItem &item)
#define klfDbg(streamableItems)
fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
static QString stripCategoryTagsFromLatex(const QString &latex)
Definition: klflib.cpp:145
QString name
KLFLegacyData::KLFStyle style
QString mathmode
QString latex() const
Definition: klflib.h:82
bool load(const QString &fname=QString())
canonicalFilePath()
static KLFLibLegacyFileDataPrivate * instanceFor(const QString fname, bool starttimer)
QString category() const
Definition: klflib.h:86
void resourcePropertyChanged(int propId)
KLFLegacyData::KLFLibrary library
endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive)
static KLFLegacyData::KLFLibraryItem toLegacyLibItem(const KLFLibEntry &entry)
void emitResourcePropertyChanged(int propId)
static QString canonicalFilePath(const QString &fname)
static QString latexAddCategoryTagsComment(const QString &latex, const QString &category, const QString &tags)
Definition: klflib.cpp:179
#define KLF_FUNC_NAME
QString preamble
QImage preview() const
Definition: klflib.h:84
KLFLegacyData::KLFLibraryResourceList resources
An entry (single formula) in the library.
Definition: klflib.h:55
start(int msec)
QDateTime dateTime() const
Definition: klflib.h:83
connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type=Qt::AutoCompatConnection)
bool save(const QString &fname=QString())
unsigned long bg_color
QString tags() const
Definition: klflib.h:87
int getReservedResourceId(const QString &resourceName, int defaultId)
static KLFStyle toStyle(const KLFLegacyData::KLFStyle &oldstyle)
unsigned long fg_color
remove(const Key &key)
KLFStyle style() const
Definition: klflib.h:88

Generated by doxygen 1.8.5