aseprite/src/app/docs.cpp

120 lines
2.2 KiB
C++
Raw Normal View History

// Aseprite
// Copyright (C) 2018 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
2018-07-07 19:38:04 +08:00
#include "app/docs.h"
2018-07-07 22:54:44 +08:00
#include "app/doc.h"
#include "base/fs.h"
#include "base/mutex.h"
#include <algorithm>
namespace app {
2018-07-07 19:38:04 +08:00
Docs::Docs(Context* ctx)
: m_ctx(ctx)
{
ASSERT(ctx != NULL);
}
2018-07-07 19:38:04 +08:00
Docs::~Docs()
{
deleteAll();
}
2018-07-07 22:54:44 +08:00
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);
2018-07-07 19:38:04 +08:00
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::createBasicSprite(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();
}
2018-07-07 22:54:44 +08:00
void Docs::remove(Doc* doc)
{
iterator it = std::find(begin(), end(), doc);
if (it == end()) // Already removed.
return;
m_docs.erase(it);
2018-07-07 19:38:04 +08:00
notify_observers(&DocsObserver::onRemoveDocument, doc);
doc->setContext(NULL);
}
2018-07-07 22:54:44 +08:00
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);
}
2018-07-07 22:54:44 +08:00
Doc* Docs::getById(ObjectId id) const
{
for (const auto& doc : *this) {
if (doc->id() == id)
return doc;
}
return NULL;
}
2018-07-07 22:54:44 +08:00
Doc* Docs::getByName(const std::string& name) const
{
for (const auto& doc : *this) {
if (doc->name() == name)
return doc;
}
return NULL;
}
2018-07-07 22:54:44 +08:00
Doc* Docs::getByFileName(const std::string& filename) const
{
2016-04-12 06:17:39 +08:00
std::string fn = base::normalize_path(filename);
for (const auto& doc : *this) {
2016-04-12 06:17:39 +08:00
if (doc->filename() == fn)
return doc;
}
return NULL;
}
2018-07-07 19:38:04 +08:00
void Docs::deleteAll()
{
while (!empty()) {
ASSERT(m_ctx == back()->context());
delete back();
}
}
} // namespace app