forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocuments.cpp
More file actions
125 lines (100 loc) · 2.5 KB
/
documents.cpp
File metadata and controls
125 lines (100 loc) · 2.5 KB
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
// Aseprite Document Library
// Copyright (c) 2001-2016 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "doc/documents.h"
#include "base/fs.h"
#include "base/mutex.h"
#include "doc/document.h"
#include <algorithm>
namespace doc {
Documents::Documents(Context* ctx)
: m_ctx(ctx)
{
ASSERT(ctx != NULL);
}
Documents::~Documents()
{
deleteAll();
}
Document* Documents::add(int width, int height, ColorMode mode, int ncolors)
{
// Ask to observers to create the document (maybe a doc::Document or
// a derived class).
CreateDocumentArgs args;
notify_observers(&DocumentsObserver::onCreateDocument, &args);
if (!args.document())
args.setDocument(new Document());
base::UniquePtr<Document> doc(args.document());
doc->sprites().add(width, height, mode, ncolors);
doc->setFilename("Sprite");
doc->setContext(m_ctx); // Change the document context to add the doc in this collection
return doc.release();
}
Document* Documents::add(Document* doc)
{
ASSERT(doc != NULL);
ASSERT(doc->id() != doc::NullId);
if (doc->context() != m_ctx) {
doc->setContext(m_ctx);
ASSERT(std::find(begin(), end(), doc) != end());
return doc;
}
m_docs.insert(begin(), doc);
notify_observers(&DocumentsObserver::onAddDocument, doc);
return doc;
}
void Documents::remove(Document* doc)
{
iterator it = std::find(begin(), end(), doc);
if (it == end()) // Already removed.
return;
m_docs.erase(it);
notify_observers(&DocumentsObserver::onRemoveDocument, doc);
doc->setContext(NULL);
}
void Documents::move(Document* doc, int index)
{
iterator it = std::find(begin(), end(), doc);
ASSERT(it != end());
if (it != end())
m_docs.erase(it);
m_docs.insert(begin()+index, doc);
}
Document* Documents::getById(ObjectId id) const
{
for (const auto& doc : *this) {
if (doc->id() == id)
return doc;
}
return NULL;
}
Document* Documents::getByName(const std::string& name) const
{
for (const auto& doc : *this) {
if (doc->name() == name)
return doc;
}
return NULL;
}
Document* Documents::getByFileName(const std::string& filename) const
{
std::string fn = base::normalize_path(filename);
for (const auto& doc : *this) {
if (doc->filename() == fn)
return doc;
}
return NULL;
}
void Documents::deleteAll()
{
while (!empty()) {
ASSERT(m_ctx == back()->context());
delete back();
}
}
} // namespace doc