1 #ifndef __qwt3d_io_2003_07_04_23_27__
2 #define __qwt3d_io_2003_07_04_23_27__
3 
4 #include <vector>
5 #include <algorithm>
6 
7 #include <qstring.h>
8 #include <qstringlist.h>
9 #include "qwt3d_global.h"
10 
11 namespace Qwt3D
12 {
13 
14 class Plot3D;
15 /**
16 IO provides a generic interface for standard and user written I/O handlers.
17 It also provides functionality for the registering of such handlers in the
18 framework.\n
19 The interface mimics roughly Qt's QImageIO functions for defining
20 image input/output functions.
21 */
22 class QWT3D_EXPORT IO
23 {
24 
25 public:
26   /*!
27     The function type that can be processed by the define... members.
28     An extension is the IO::Functor.
29   */
30   typedef bool (*Function)(Plot3D*, QString const& fname);
31 
32 
33   /*!
34     This class gives more flexibility in implementing
35     userdefined IO handlers than the simple IO::Function type.
36   */
37   class Functor
38   {
39   public:
~Functor()40     virtual ~Functor() {}
41     /*! Must clone the content of *this for an object of a derived class with
42     \c new and return the pointer. Like operator() the predefined Functors
43     hide this function from the user, still allowing IO access
44     (friend declaration)
45     */
46     virtual Functor* clone() const = 0;
47     /*! The workhorse of the user-defined implementation. Eventually, the
48     framework will call this operator.
49     */
50     virtual bool operator()(Plot3D* plot, QString const& fname) = 0;
51     virtual bool operator()(QImage* image, QString const& fname) = 0;
52   };
53 
54   static bool defineInputHandler( QString const& format, Function func);
55   static bool defineOutputHandler( QString const& format, Function func);
56   static bool defineInputHandler( QString const& format, Functor const& func);
57   static bool defineOutputHandler( QString const& format, Functor const& func);
58   static bool save(Plot3D*, QString const& fname, QString const& format);
59   static bool save(QImage*, QString const& fname, QString const& format);
60   static bool load(Plot3D*, QString const& fname, QString const& format);
61   static QStringList inputFormatList();
62   static QStringList outputFormatList();
63   static Functor* outputHandler(QString const& format);
64   static Functor* inputHandler(QString const& format);
65 
66   static QString title;
67 
68 private:
IO()69   IO(){}
70 
71   //! Lightweight Functor encapsulating an IO::Function
72   class Wrapper : public Functor
73   {
74   public:
75     //! Performs actual input
clone()76     Functor* clone() const { return new Wrapper(*this); }
77     //! Creates a Wrapper object from a function pointer
Wrapper(Function h)78     explicit Wrapper(Function h) : hdl(h) {}
79     //! Returns a pointer to the wrapped function
operator()80     bool operator()(Plot3D* plot, QString const& fname)
81     {
82       return (hdl) ? (*hdl)(plot, fname) : false;
83     }
operator()84     bool operator()(QImage* image, QString const& fname)
85     {
86     	Q_UNUSED(image); Q_UNUSED(fname); return false;	// image function pointer is not supported
87     }
88   private:
89     Function hdl;
90   };
91 
92   struct Entry
93   {
94     Entry();
95     ~Entry();
96 
97     Entry(Entry const& e);
98     void operator=(Entry const& e);
99 
100     Entry(QString const& s, Functor const& f);
101     Entry(QString const& s, Function f);
102 
103     QString fmt;
104     Functor* iofunc;
105   };
106 
107   struct FormatCompare
108   {
109     explicit FormatCompare(Entry const& e);
110     bool operator() (Entry const& e);
111 
112     Entry e_;
113   };
114 
115   struct FormatCompare2
116   {
117     explicit FormatCompare2(QString s);
118     bool operator() (Entry const& e);
119 
120     QString s_;
121   };
122 
123   typedef std::vector<Entry> Container;
124   typedef Container::iterator IT;
125 
126   static bool add_unique(Container& l, Entry const& e);
127   static IT find(Container& l, QString const& fmt);
128   static Container& rlist();
129   static Container& wlist();
130   static void setupHandler();
131 };
132 
133 //! Provides Qt's Pixmap output facilities
134 class QWT3D_EXPORT PixmapWriter : public IO::Functor
135 {
136 friend class IO;
137 public:
PixmapWriter()138   PixmapWriter() : quality_(-1) {}
139   void setQuality(int val);
140 private:
clone()141   IO::Functor* clone() const {return new PixmapWriter(*this);}
142   bool operator()(Plot3D* plot, QString const& fname);
143   bool operator()(QImage* image, QString const& fname);
144   QString fmt_;
145   int quality_;
146 };
147 
148 } //ns
149 
150 #endif
151