| 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 |
#ifndef AVOGADRO_CORE_LAYERMANAGER_H |
| 7 |
#define AVOGADRO_CORE_LAYERMANAGER_H |
| 8 |
|
| 9 |
#include "avogadrocoreexport.h" |
| 10 |
|
| 11 |
#include "array.h" |
| 12 |
#include "layer.h" |
| 13 |
|
| 14 |
#include <cassert> |
| 15 |
#include <map> |
| 16 |
#include <memory> |
| 17 |
#include <set> |
| 18 |
#include <string> |
| 19 |
#include <vector> |
| 20 |
|
| 21 |
namespace Avogadro::Core { |
| 22 |
|
| 23 |
class Molecule; |
| 24 |
|
| 25 |
/** |
| 26 |
* @class LayerData layermanager.h <avogadro/core/layermanager.h> |
| 27 |
* @brief Interface to store layer data structure. |
| 28 |
*/ |
| 29 |
struct LayerData |
| 30 |
{ |
| 31 |
LayerData(std::string save = "") { deserialize(save); } |
| 32 |
|
| 33 |
/** save custom data, base save should never be called */ |
| 34 |
virtual std::string serialize() { return ""; } |
| 35 |
|
| 36 |
/** load the saved @p save data and wait to know the class type to recreate it |
| 37 |
*/ |
| 38 |
virtual void deserialize(std::string save) { m_save = save; } |
| 39 |
|
| 40 |
virtual ~LayerData() = default; |
| 41 |
|
| 42 |
virtual LayerData* clone() { return new LayerData(serialize()); }; |
| 43 |
|
| 44 |
/** get the saved data */ |
| 45 |
std::string getSave() const { return m_save; } |
| 46 |
|
| 47 |
protected: |
| 48 |
std::string boolToString(bool b) { return b ? "true" : "false"; } |
| 49 |
bool stringToBool(std::string b) { return b == "true"; } |
| 50 |
std::string m_save; |
| 51 |
}; |
| 52 |
|
| 53 |
/** |
| 54 |
* @class MoleculeInfo layermanager.h <avogadro/core/layermanager.h> |
| 55 |
* @brief All layer dependent data. Original molecule @p mol, is layer hidden |
| 56 |
* @p visible, accepts edits @p locked, and key-value data like @p enable, |
| 57 |
* and custom data @p settings. |
| 58 |
*/ |
| 59 |
struct MoleculeInfo |
| 60 |
{ |
| 61 |
const Molecule* mol; |
| 62 |
std::vector<bool> visible; |
| 63 |
std::vector<bool> locked; |
| 64 |
std::map<std::string, std::vector<bool>> enable; |
| 65 |
std::map<std::string, Core::Array<LayerData*>> settings; |
| 66 |
Layer layer; |
| 67 |
std::set<std::string> loaded; |
| 68 |
|
| 69 |
MoleculeInfo(const Molecule* m) : mol(m) |
| 70 |
{ |
| 71 |
locked.push_back(false); |
| 72 |
visible.push_back(true); |
| 73 |
} |
| 74 |
|
| 75 |
void clear() |
| 76 |
{ |
| 77 |
visible.clear(); |
| 78 |
locked.clear(); |
| 79 |
enable.clear(); |
| 80 |
layer.clear(); |
| 81 |
} |
| 82 |
}; |
| 83 |
/** |
| 84 |
* @class LayerManager layermanager.h <avogadro/core/layermanager.h> |
| 85 |
* @brief |
| 86 |
*/ |
| 87 |
class AVOGADROCORE_EXPORT LayerManager |
| 88 |
{ |
| 89 |
public: |
| 90 |
/** @return active molecule Layer */ |
| 91 |
static Layer& getMoleculeLayer(); |
| 92 |
|
| 93 |
/** @return Layer from @p mol and creates MoleculeInfo if not exists */ |
| 94 |
static Layer& getMoleculeLayer(const Molecule* mol); |
| 95 |
|
| 96 |
/** @return Layer from @p original and links @p original MoleculeInfo to @p |
| 97 |
* copy */ |
| 98 |
static Layer& getMoleculeLayer(const Molecule* original, |
| 99 |
const Molecule* copy); |
| 100 |
|
| 101 |
/** @return the MoleculeInfo from active molecule */ |
| 102 |
static std::shared_ptr<MoleculeInfo> getMoleculeInfo(); |
| 103 |
|
| 104 |
/** @return the MoleculeInfo from @p mol */ |
| 105 |
static std::shared_ptr<MoleculeInfo> getMoleculeInfo(const Molecule* mol); |
| 106 |
|
| 107 |
/** remove all data related to @p mol */ |
| 108 |
static void deleteMolecule(const Molecule* mol); |
| 109 |
|
| 110 |
/** @return the layer quantity from activeMolecule */ |
| 111 |
static size_t layerCount(); |
| 112 |
|
| 113 |
protected: |
| 114 |
static const Molecule* m_activeMolecule; |
| 115 |
static std::map<const Molecule*, std::shared_ptr<MoleculeInfo>> m_molToInfo; |
| 116 |
}; |
| 117 |
|
| 118 |
} // namespace Avogadro::Core |
| 119 |
|
| 120 |
#endif |