forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocs.cpp
118 lines (94 loc) · 2.16 KB
/
docs.cpp
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
// Aseprite
// Copyright (C) 2018-2019 Igara Studio S.A.
// Copyright (c) 2001-2018 David Capello
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/docs.h"
#include "app/doc.h"
#include "base/fs.h"
#include <algorithm>
namespace app {
Docs::Docs(Context* ctx)
: m_ctx(ctx)
{
ASSERT(ctx != NULL);
}
Docs::~Docs()
{
deleteAll();
}
Doc* Docs::add(Doc* 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(&DocsObserver::onAddDocument, doc);
return doc;
}
Doc* Docs::add(int width, int height, doc::ColorMode colorMode, int ncolors)
{
std::unique_ptr<Doc> doc(
new Doc(Sprite::MakeStdSprite(ImageSpec(colorMode, width, height), ncolors)));
doc->setFilename("Sprite");
doc->setContext(m_ctx); // Change the document context to add the doc in this collection
return doc.release();
}
void Docs::remove(Doc* doc)
{
iterator it = std::find(begin(), end(), doc);
if (it == end()) // Already removed.
return;
m_docs.erase(it);
notify_observers(&DocsObserver::onRemoveDocument, doc);
doc->setContext(NULL);
}
void Docs::move(Doc* 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);
}
Doc* Docs::getById(ObjectId id) const
{
for (const auto& doc : *this) {
if (doc->id() == id)
return doc;
}
return NULL;
}
Doc* Docs::getByName(const std::string& name) const
{
for (const auto& doc : *this) {
if (doc->name() == name)
return doc;
}
return NULL;
}
Doc* Docs::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 Docs::deleteAll()
{
while (!empty()) {
ASSERT(m_ctx == back()->context());
delete back();
}
}
} // namespace app