Scribus
Open source desktop publishing at your fingertips
loadsaveplugin.h
1 /*
2 For general Scribus (>=1.3.2) copyright and licensing information please refer
3 to the COPYING file provided with the program. Following this notice may exist
4 a copyright and/or license notice that predates the release of Scribus 1.3.2
5 for which a new license (GPL+exception) is in place.
6 */
7 #ifndef SCRIBUS_LOADSAVEPLUGIN_H
8 #define SCRIBUS_LOADSAVEPLUGIN_H
9 
10 #include "scplugin.h"
11 
12 #include <QString>
13 #include <QRegExp>
14 #include <QIODevice>
15 #include <QProgressBar>
16 #include <QStringList>
17 #include <QList>
18 
19 class FileFormat;
20 //TODO REmove includes one day
21 class ScribusView;
22 #include "scfonts.h"
23 #include "scribusdoc.h"
24 #include "undomanager.h"
25 
33 class SCRIBUS_API LoadSavePlugin : public ScPlugin
34 {
35  Q_OBJECT
36 
37  public:
38  // Construct a plugin instance.
40  ~LoadSavePlugin();
41 
42  enum loadFlags
43  {
44  lfCreateDoc = 1,
45  lfUseCurrentPage = 2,
46  lfInsertPage = 4,
47  lfInteractive = 8,
48  lfScripted = 16,
49  lfKeepColors = 32,
50  lfKeepGradients = 64,
51  lfKeepPatterns = 128,
52  lfCreateThumbnail = 256,
53  lfLoadAsPattern = 512
54  };
55 
56  // Static functions:
57 
58  // Return a list of file extensions
59  static const QStringList getExtensionsForColors(const int id = 47);
60  // Return a list of file extensions
61  static const QStringList getExtensionsForImport(const int id = 47);
62  // Return a list of file extensions
63  static const QStringList getExtensionsForPreview(const int id = 47);
64 
65  // Return a list of format descriptions suitable for use with
66  // QFileDialog. You can convert it to QString form with
67  // fileDialogSaveFilter().join(";;")
68  static const QStringList fileDialogLoadFilter();
69 
70  // Same deal but for save
71  static const QStringList fileDialogSaveFilter();
72 
73  // Get the highest priority format of a given id, or 0 if
74  // not found / not available.
75  static const FileFormat * getFormatById(const int id);
76  static FileFormat * getFormatByID(int id);
77  static FileFormat* getFormatByExt(const QString ext);
78 
79  virtual bool loadElements(const QString & data, QString fileDir, int toLayer, double Xp_in, double Yp_in, bool loc);
80  // Non-static members implemented by plugins:
81  //
82  // Load the requested format from the specified path.
83  // Default implementation always reports failure.
84  virtual bool loadFile(const QString & fileName, const FileFormat & fmt, int flags, int index = 0);
85  virtual bool loadPalette(const QString & fileName);
86 
87  // Save the requested format to the requested path.
88  virtual bool saveFile(const QString & fileName, const FileFormat & fmt);
89  virtual bool savePalette(const QString & fileName);
90  virtual QString saveElements(double xp, double yp, double wp, double hp, Selection* selection, QByteArray &prevData);
91 
92  // Return last file saved, this may be the last fileName argument passed to saveFile(),
93  // a temporary file name or an empty string if last call to saveFile() could not create
94  // a valid file
95  virtual const QString& lastSavedFile(void);
96 
97  // Examine the passed file and test to see whether it appears to be
98  // loadable with this plugin. This test must be quick and simple.
99  // It need not verify a file, just confirm that it looks like a supported
100  // file type (eg "XML doc with root element SCRIBUSXML and version 1.3.1").
101  // All plugins must implement this method.
102  virtual bool fileSupported(QIODevice* file, const QString & fileName=QString::null) const = 0;
103 
104  // Return a list of all formats supported by all currently loaded and
105  // active plugins. This list is sorted in a very specific order:
106  // First, by descending order of `id', then descending order of priority.
107  static const QList<FileFormat> & supportedFormats();
108 
109  virtual void setupTargets(ScribusDoc *targetDoc, ScribusView* targetView, ScribusMainWindow* targetMW, QProgressBar* targetMWPRogressBar, SCFonts* targetAvailableFonts);
110  virtual void getReplacedFontData(bool & getNewReplacement, QMap<QString,QString> &getReplacedFonts, QList<ScFace> &getDummyScFaces);
111  virtual bool loadPage(const QString & fileName, int pageNumber, bool Mpage, QString renamedPageName=QString::null);
112  virtual bool readStyles(const QString& fileName, ScribusDoc* doc, StyleSet<ParagraphStyle> &docParagraphStyles);
113  virtual bool readCharStyles(const QString& fileName, ScribusDoc* doc, StyleSet<CharStyle> &docCharStyles);
114  virtual bool readLineStyles(const QString& fileName, QHash<QString, multiLine> *Sty);
115  virtual bool readColors(const QString& fileName, ColorList & colors);
116  virtual bool readPageCount(const QString& fileName, int *num1, int *num2, QStringList & masterPageNames);
117  virtual QImage readThumbnail(const QString& fileName);
118 
119  protected:
120 
122  virtual bool checkFlags(int flags);
123 
125  void registerFormat(FileFormat & fmt);
126 
128  void unregisterFormat(unsigned int id);
129 
131  void unregisterAll();
132 
133  // Set standard message for file read errors
134  virtual void setFileReadError();
135 
136  // Set standard message for dom style errors with line and column
137  virtual void setDomParsingError(const QString& msg, int line, int column);
138 
139  ScribusDoc* m_Doc;
140  ScribusView* m_View; //For 1.2.x loader at the moment
141  ScribusMainWindow* m_ScMW; //For plugins when required
142  QProgressBar* m_mwProgressBar;
143  SCFonts* m_AvailableFonts;
144  QString m_lastSavedFile;
145  UndoManager * const undoManager;
146 
147  private:
148  // A list of all supported formats. This is maintained by plugins
149  // using the protected `registerFormat(...)', `unregisterFormat(...)'
150  // and `unregisterAll(...)' methods. This is sorted in a very specific
151  // order - ascending ID, then descending priority.
152  static QList<FileFormat> formats;
153 
154  // Return an iterator referencing the first format structure named `name'.
155  // If specified, only return formats implmented by `plug'.
156  // If `start' is specified, start searching at this iterator rather than the
157  // start of the list.
158  // The end iterator is returned if no match was found.
159  // Note that due to the sort order maintained in `formats', the first
160  // iterator returned by this method will always be to the highest
161  // priority format of the required ID, and each subsequent call will
162  // return the next lowest priority format.
163  static QList<FileFormat>::iterator findFormat(unsigned int id,
164  LoadSavePlugin* plug = 0,
165  QList<FileFormat>::iterator it = formats.begin());
166 
167  static QList<FileFormat>::iterator findFormat(const QString& extension,
168  LoadSavePlugin* plug = 0,
169  QList<FileFormat>::iterator it = formats.begin());
170 
171  // Print out a format list for debugging purposes
172  static void printFormatList();
173 
174  // Base implementation for fileDialogLoadFilter and fileDialogSaveFilter
175  static const QStringList getDialogFilter(bool forLoad);
176 };
177 
178 
179 
180 // Info on each supported format. A plugin must register a
181 // FileFormat structure for every format it knows how to load or
182 // save. If it both loads and saves a given format, one structure must
183 // be registered for load and one for save.
184 // Plugins must unregister formats when being unloaded to ensure that
185 // no attempt is made to load a file using a plugin that's no longer
186 // available.
187 //
188 // This class also provides methods to ask the implementation to load / save a
189 // file in this format.
190 class SCRIBUS_API FileFormat
191 {
192  public:
193  // Default ctor to make QValueList happy
194  FileFormat() : load(false), save(false), thumb(false), colorReading(false), plug(0) {}
195  // Standard ctor that sets up a valid FileFormat
196  FileFormat(LoadSavePlugin * plug) : load(false), save(false), thumb(false), colorReading(false), plug(plug) {}
197 
198  bool loadElements(const QString & data, QString fileDir, int toLayer, double Xp_in, double Yp_in, bool loc) const;
199  // Load a file with this format
200  bool loadFile(const QString & fileName, int flags, int index = 0) const;
201  bool loadPalette(const QString & fileName) const;
202  // Save a file with this format
203  bool saveFile(const QString & fileName) const;
204  bool savePalette(const QString & fileName) const;
205  QString saveElements(double xp, double yp, double wp, double hp, Selection* selection, QByteArray &prevData) const;
206  // Get last saved file
207  QString lastSavedFile(void) const;
208 
209  void setupTargets(ScribusDoc *targetDoc, ScribusView* targetView, ScribusMainWindow* targetMW, QProgressBar* targetMWPRogressBar, SCFonts* targetAvailableFonts) const;
210  void getReplacedFontData(bool & getNewReplacement, QMap<QString,QString> &getReplacedFonts, QList<ScFace> &getDummyScFaces) const;
211  bool loadPage(const QString & fileName, int pageNumber, bool Mpage, QString renamedPageName=QString::null) const;
212  bool readStyles(const QString& fileName, ScribusDoc* doc, StyleSet<ParagraphStyle> &docParagraphStyles) const;
213  bool readCharStyles(const QString& fileName, ScribusDoc* doc, StyleSet<CharStyle> &docCharStyles) const;
214  bool readLineStyles(const QString& fileName, QHash<QString,multiLine> *Sty) const;
215  bool readColors(const QString& fileName, ColorList & colors) const;
216  bool readPageCount(const QString& fileName, int *num1, int *num2, QStringList & masterPageNames) const;
217  QImage readThumbnail(const QString& fileName) const;
218  //
219  // Data members
220  //
221  // An integer ID code used to idenfify formats. Should be unique
222  // across all FileFormat structures except where they implement
223  // support for the same file format, eg sla 1.2.x, sla 1.3.x and
224  // "new format" SLA should all have equal IDs (with different
225  // priorities to control what order they're tried in when a user
226  // tries to open a file).
227  // Note that dialog box options are sorted in descending `id' order.
228  uint formatId;
229  // The human-readable, translated name of this file format.
230  QString trName;
231  // A filter in the format used by QFileDialog that should be used to
232  // select for this format.
233  QString filter;
234  // Regexp to match filenames for this format
235  // QRegExp nameMatch;
236  // MIME type(s) that should be matched by this format.
237  QStringList mimeTypes;
238  // Extension list supported by format
239  QStringList fileExtensions;
240  // Can we load it?
241  bool load;
242  // Can we save it?
243  bool save;
244  // Do we support thumbnails
245  bool thumb;
246  // Can we load colors?
247  bool colorReading;
248  // Priority of this format from 0 (lowest, tried last) to
249  // 255 (highest, tried first). 64-128 recommended in general.
250  // Priority controls the order options are displayed in when a file
251  // of a given type is selected in a dialog, and controls the order
252  // loaders are tried in when multiple plugins support the same file
253  // type.
254  unsigned short int priority;
255  // For convenience, a pointer back to the plugin to use to open
256  // this format.
257  LoadSavePlugin * plug;
258 // LoadSavePlugin * const plug;
259 };
260 
261 
262 
263 #endif
Definition: loadsaveplugin.h:190
Abstract super class for all Scribus plug-ins.
Definition: scplugin.h:90
This Class is the base class for your application. It sets up the main window and providing a menubar...
Definition: scribus.h:123
Definition: sccolor.h:155
the Document Class
Definition: scribusdoc.h:90
Superclass for all file import/export/load/save plugins.
Definition: loadsaveplugin.h:33
UndoManager handles the undo stack.
Definition: undomanager.h:81
Definition: scribusview.h:87
Main class SCFonts. Subclass of QDict. This class replaces the previous SCFonts typedef...
Definition: scfonts.h:36
Definition: selection.h:34