aboutsummaryrefslogtreecommitdiff
path: root/libtiled/mapreader.h
blob: 39db8827a67c208df0f53c8446836d59d2e7677e (plain)
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
/*
 * mapreader.h
 * Copyright 2008-2010, Thorbjørn Lindeijer <thorbjorn@lindeijer.nl>
 *
 * This file is part of libtiled.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef MAPREADER_H
#define MAPREADER_H

#include "tiled_global.h"

#include <QImage>

class QFile;

namespace Tiled {

class Map;
class Tileset;

namespace Internal {
class MapReaderPrivate;
}

/**
 * A fast QXmlStreamReader based reader for the TMX and TSX formats.
 *
 * Can be subclassed when special handling of external images and tilesets is
 * needed.
 */
class TILEDSHARED_EXPORT MapReader
{
public:
    MapReader();
    ~MapReader();

    /**
     * Reads a TMX map from the given \a device. Optionally a \a path can
     * be given, which will be used to resolve relative references to external
     * images and tilesets.
     *
     * Returns 0 and sets errorString() when reading failed.
     *
     * The caller takes ownership over the newly created map.
     */
    Map *readMap(QIODevice *device, const QString &path = QString());

    /**
     * Reads a TMX map from the given \a fileName.
     * \overload
     */
    Map *readMap(const QString &fileName);

    /**
     * Reads a TSX tileset from the given \a device. Optionally a \a path can
     * be given, which will be used to resolve relative references to external
     * images.
     *
     * Returns 0 and sets errorString() when reading failed.
     *
     * The caller takes ownership over the newly created tileset.
     */
    Tileset *readTileset(QIODevice *device, const QString &path = QString());

    /**
     * Reads a TSX tileset from the given \a fileName.
     * \overload
     */
    Tileset *readTileset(const QString &fileName);

    /**
     * Returns the error message for the last occurred error.
     */
    QString errorString() const;

protected:
    /**
     * Called for each \a reference to an external file. Should return the path
     * to be used when loading this file. \a mapPath contains the path to the
     * map or tileset that is currently being loaded.
     */
    virtual QString resolveReference(const QString &reference,
                                     const QString &mapPath);

    /**
     * Called when an external image is encountered while a tileset is loaded.
     */
    virtual QImage readExternalImage(const QString &source);

    /**
     * Called when an external tileset is encountered while a map is loaded.
     * The default implementation just calls readTileset() on a new MapReader.
     *
     * If an error occurred, the \a error parameter should be set to the error
     * message.
     */
    virtual Tileset *readExternalTileset(const QString &source,
                                         QString *error);

private:
    friend class Internal::MapReaderPrivate;
    Internal::MapReaderPrivate *d;
};

} // namespace Tiled

#endif // MAPREADER_H