File: sailreader-xml.h

package info (click to toggle)
sailcut 1.2.4-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,580 kB
  • ctags: 875
  • sloc: cpp: 10,139; sh: 8,767; makefile: 176
file content (138 lines) | stat: -rw-r--r-- 3,748 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (C) 1993-2006 Robert & Jeremy Laine
 * See AUTHORS file for a full list of contributors.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef XMLREADER_H
#define XMLREADER_H

#include <QMessageBox>
#include "fileio.h"
#include "saildoc.h"

/** This class allows you to read sailcut objects from
 *  an XML file.
 *
 *  @see CSailXmlReader
 *  @see CSailDefXmlReader
 */
template <class objtype>
class XmlReaderTempl : public CFileIO
{
protected:
    /** the object's name */
    QString _name;

public:
    /** The constructor.
     *
     * @param name the object's name
     * @param ext the file extension to show (defaults to ".xml")
     * @param desc a description of the file type (defaults to "XML files")
     */
    XmlReaderTempl(const QString &name, const QString ext = ".xml",
                   const QString desc = "XML files")
            : CFileIO(ext, desc), _name(name)
    {}
    ;

    /** Display a dialog then read file.
     *
     * @param dest the object we read to
     * @param filename initial file name
     */
    virtual QString readDialog(objtype &dest, const QString &filename = "") const
    {
        QString newfilename = showDialogRead(filename);

        if (!newfilename.isNull())
        {
            try
            {
                dest = read(newfilename);
            }
            catch (CException e)
            {
                QMessageBox::information(0,tr("error"), tr("There was an error reading from the selected file."));
                newfilename = QString::null;
            }
        }
        return newfilename;
    };


    /** Read object from an XML file.
     *
     * @param filename the file we read from
     */
    const objtype read(const QString &filename) const
    {
        CSailDoc doc(filename);
        objtype obj;
        doc.get(doc.top, obj, _name);
        return obj;
    };
};

// some typedefs

/** A class used to read a CSail sail from an XML file. */
class CSailXmlReader : public XmlReaderTempl<CSail>
{
public:
    /** The constructor.
     *
     * @param name the sail's name
     */
    CSailXmlReader(const QString &name)
            : XmlReaderTempl<CSail>(name, ".sail3d", "3D sails")
    {}
    ;
};

/** A class used to read a CSailDef sail definition from an XML file. */
class CSailDefXmlReader : public XmlReaderTempl<CSailDef>
{
public:
    /** The constructor.
     *
     * @param name the sail definition's name
     */
    CSailDefXmlReader(const QString &name)
            : XmlReaderTempl<CSailDef>(name, ".saildef", "Sail definitions")
    {}
    ;
};

/** A class used to read a CRigDef rig definition from an XML file. */
class CRigDefXmlReader : public XmlReaderTempl<CRigDef>
{
public:
    /** The constructor.
     *
     * @param name the rig definition's name
     */
    CRigDefXmlReader(const QString &name)
            : XmlReaderTempl<CRigDef>(name, ".sailrig", "Rig definitions")
    {}
    ;
};

/** A class used to read a CPrefs set of preferences from an XML file. */
typedef XmlReaderTempl<CPrefs> CPrefsXmlReader;
//
#endif