| 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 "pluginlayermanager.h" |
| 7 |
|
| 8 |
#include <avogadro/qtplugins/ballandstick/ballandstick.h> |
| 9 |
#include <avogadro/qtplugins/cartoons/cartoons.h> |
| 10 |
|
| 11 |
#include <QtCore/QSettings> |
| 12 |
#include <cassert> |
| 13 |
#include <vector> |
| 14 |
|
| 15 |
namespace Avogadro::QtGui { |
| 16 |
|
| 17 |
using std::string; |
| 18 |
using std::vector; |
| 19 |
|
| 20 |
PluginLayerManager::PluginLayerManager(const string& name) : m_name(name) {} |
| 21 |
|
| 22 |
PluginLayerManager::~PluginLayerManager() |
| 23 |
{ |
| 24 |
for (auto& info : m_molToInfo) { |
| 25 |
auto itEnable = info.second->enable.find(m_name); |
| 26 |
if (itEnable != info.second->enable.end()) { |
| 27 |
info.second->enable.erase(itEnable); |
| 28 |
} |
| 29 |
|
| 30 |
auto itSettings = info.second->settings.find(m_name); |
| 31 |
if (itSettings != info.second->settings.end()) { |
| 32 |
info.second->settings.erase(itSettings); |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
bool PluginLayerManager::isEnabled() const |
| 38 |
{ |
| 39 |
if (m_activeMolecule == nullptr || m_molToInfo[m_activeMolecule] == nullptr || |
| 40 |
m_molToInfo[m_activeMolecule]->enable.find(m_name) == |
| 41 |
m_molToInfo[m_activeMolecule]->enable.end()) { |
| 42 |
return false; |
| 43 |
} |
| 44 |
for (const auto& b : m_molToInfo[m_activeMolecule]->enable[m_name]) { |
| 45 |
if (b) { |
| 46 |
return true; |
| 47 |
} |
| 48 |
} |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
bool PluginLayerManager::isActiveLayerEnabled() const |
| 53 |
{ |
| 54 |
if (m_activeMolecule == nullptr || m_molToInfo[m_activeMolecule] == nullptr || |
| 55 |
m_molToInfo[m_activeMolecule]->enable.find(m_name) == |
| 56 |
m_molToInfo[m_activeMolecule]->enable.end()) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
auto& molecule = m_molToInfo[m_activeMolecule]; |
| 60 |
size_t active = molecule->layer.activeLayer(); |
| 61 |
if (active < molecule->enable[m_name].size()) { |
| 62 |
return molecule->enable[m_name][active]; |
| 63 |
} |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
void PluginLayerManager::setEnabled(bool enable) |
| 68 |
{ |
| 69 |
if (m_activeMolecule == nullptr || m_molToInfo[m_activeMolecule] == nullptr) { |
| 70 |
return; |
| 71 |
} |
| 72 |
auto& molecule = m_molToInfo[m_activeMolecule]; |
| 73 |
auto it = molecule->enable.find(m_name); |
| 74 |
if (it == molecule->enable.end()) { |
| 75 |
molecule->enable[m_name] = vector<bool>(); |
| 76 |
} |
| 77 |
size_t qttyLayers = molecule->layer.layerCount(); |
| 78 |
if (molecule->enable[m_name].size() != qttyLayers) { |
| 79 |
molecule->enable[m_name].resize(qttyLayers, false); |
| 80 |
} |
| 81 |
size_t activeLayer = molecule->layer.activeLayer(); |
| 82 |
molecule->enable[m_name][activeLayer] = enable; |
| 83 |
} |
| 84 |
|
| 85 |
bool PluginLayerManager::atomEnabled(Index atom) const |
| 86 |
{ |
| 87 |
if (m_activeMolecule == nullptr || m_molToInfo[m_activeMolecule] == nullptr || |
| 88 |
m_molToInfo[m_activeMolecule]->enable.find(m_name) == |
| 89 |
m_molToInfo[m_activeMolecule]->enable.end()) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
auto& molecule = m_molToInfo[m_activeMolecule]; |
| 93 |
size_t layer = molecule->layer.getLayerID(atom); |
| 94 |
if (layer == MaxIndex) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
return layer < molecule->enable[m_name].size() && |
| 98 |
molecule->enable[m_name][layer] && molecule->visible[layer]; |
| 99 |
} |
| 100 |
|
| 101 |
size_t PluginLayerManager::getLayerID(Index atom) const |
| 102 |
{ |
| 103 |
assert(m_activeMolecule != nullptr); |
| 104 |
auto& molecule = m_molToInfo[m_activeMolecule]; |
| 105 |
assert(atom < molecule->layer.atomCount()); |
| 106 |
return molecule->layer.getLayerID(atom); |
| 107 |
} |
| 108 |
|
| 109 |
bool PluginLayerManager::atomEnabled(size_t layerFilter, Index atom) const |
| 110 |
{ |
| 111 |
bool enabled = atomEnabled(atom); |
| 112 |
if (!enabled) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
auto& molecule = m_molToInfo[m_activeMolecule]; |
| 116 |
size_t layer = molecule->layer.getLayerID(atom); |
| 117 |
return layer == layerFilter; |
| 118 |
} |
| 119 |
|
| 120 |
bool PluginLayerManager::bondEnabled(Index atom1, Index atom2) const |
| 121 |
{ |
| 122 |
return atomEnabled(atom1) || atomEnabled(atom2); |
| 123 |
} |
| 124 |
|
| 125 |
bool PluginLayerManager::activeLayerLocked() const |
| 126 |
{ |
| 127 |
assert(m_activeMolecule != nullptr); |
| 128 |
auto& molecule = m_molToInfo[m_activeMolecule]; |
| 129 |
size_t active = molecule->layer.activeLayer(); |
| 130 |
return molecule->locked[active]; |
| 131 |
} |
| 132 |
|
| 133 |
bool PluginLayerManager::atomLocked(size_t atom) const |
| 134 |
{ |
| 135 |
assert(m_activeMolecule != nullptr); |
| 136 |
auto& molecule = m_molToInfo[m_activeMolecule]; |
| 137 |
size_t layer = molecule->layer.getLayerID(atom); |
| 138 |
return molecule->locked[layer]; |
| 139 |
} |
| 140 |
|
| 141 |
size_t PluginLayerManager::layerCount() const |
| 142 |
{ |
| 143 |
return LayerManager::layerCount(); |
| 144 |
} |
| 145 |
} // namespace Avogadro::QtGui |