| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "layermanager.h" |
| 7 |
#include <cassert> |
| 8 |
|
| 9 |
namespace Avogadro::Core { |
| 10 |
|
| 11 |
using std::make_shared; |
| 12 |
using std::map; |
| 13 |
using std::shared_ptr; |
| 14 |
|
| 15 |
const Molecule* LayerManager::m_activeMolecule = nullptr; |
| 16 |
map<const Molecule*, shared_ptr<MoleculeInfo>> LayerManager::m_molToInfo; |
| 17 |
|
| 18 |
Layer& LayerManager::getMoleculeLayer() |
| 19 |
{ |
| 20 |
assert(m_activeMolecule != nullptr); |
| 21 |
auto it = m_molToInfo.find(m_activeMolecule); |
| 22 |
assert(it != m_molToInfo.end()); |
| 23 |
return it->second->layer; |
| 24 |
} |
| 25 |
|
| 26 |
Layer& LayerManager::getMoleculeLayer(const Molecule* mol) |
| 27 |
{ |
| 28 |
assert(mol != nullptr); |
| 29 |
auto it = m_molToInfo.find(mol); |
| 30 |
if (it == m_molToInfo.end()) { |
| 31 |
m_molToInfo[mol] = make_shared<MoleculeInfo>(mol); |
| 32 |
} |
| 33 |
return m_molToInfo[mol]->layer; |
| 34 |
} |
| 35 |
|
| 36 |
shared_ptr<MoleculeInfo> LayerManager::getMoleculeInfo() |
| 37 |
{ |
| 38 |
assert(m_activeMolecule != nullptr); |
| 39 |
return m_molToInfo[m_activeMolecule]; |
| 40 |
} |
| 41 |
|
| 42 |
shared_ptr<MoleculeInfo> LayerManager::getMoleculeInfo(const Molecule* mol) |
| 43 |
{ |
| 44 |
assert(mol != nullptr); |
| 45 |
auto it = m_molToInfo.find(mol); |
| 46 |
if (it == m_molToInfo.end()) { |
| 47 |
m_molToInfo[mol] = make_shared<MoleculeInfo>(mol); |
| 48 |
} |
| 49 |
return m_molToInfo[mol]; |
| 50 |
} |
| 51 |
|
| 52 |
Layer& LayerManager::getMoleculeLayer(const Molecule* original, |
| 53 |
const Molecule* copy) |
| 54 |
{ |
| 55 |
assert(original != nullptr); |
| 56 |
assert(copy != nullptr); |
| 57 |
|
| 58 |
auto it = m_molToInfo.find(original); |
| 59 |
if (it == m_molToInfo.end()) { |
| 60 |
auto molecule = make_shared<MoleculeInfo>(original); |
| 61 |
m_molToInfo[original] = molecule; |
| 62 |
m_molToInfo[copy] = molecule; |
| 63 |
return m_molToInfo[original]->layer; |
| 64 |
} else { |
| 65 |
m_molToInfo[copy] = it->second; |
| 66 |
return it->second->layer; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
void LayerManager::deleteMolecule(const Molecule* mol) |
| 71 |
{ |
| 72 |
assert(mol != nullptr); |
| 73 |
|
| 74 |
auto aux = m_molToInfo.find(mol); |
| 75 |
if (aux != m_molToInfo.end()) { |
| 76 |
auto id = aux->second->mol; |
| 77 |
if (id == mol) { |
| 78 |
auto it = m_molToInfo.begin(); |
| 79 |
while (it != m_molToInfo.end()) { |
| 80 |
if (id == it->second->mol) { |
| 81 |
it = m_molToInfo.erase(it); |
| 82 |
} else { |
| 83 |
++it; |
| 84 |
} |
| 85 |
} |
| 86 |
} else { |
| 87 |
if (m_activeMolecule == mol) { |
| 88 |
m_activeMolecule = aux->second->mol; |
| 89 |
} |
| 90 |
m_molToInfo.erase(aux); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
size_t LayerManager::layerCount() |
| 96 |
{ |
| 97 |
assert(m_activeMolecule != nullptr); |
| 98 |
return m_molToInfo[m_activeMolecule]->layer.maxLayer() + 1; |
| 99 |
} |
| 100 |
|
| 101 |
} // namespace Avogadro::Core |