| 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_QTGUI_LAYERMANAGER_H |
| 7 |
#define AVOGADRO_QTGUI_LAYERMANAGER_H |
| 8 |
|
| 9 |
#include "avogadroqtguiexport.h" |
| 10 |
|
| 11 |
#include <avogadro/core/layermanager.h> |
| 12 |
#include <cassert> |
| 13 |
#include <iostream> |
| 14 |
|
| 15 |
namespace Avogadro { |
| 16 |
namespace QtGui { |
| 17 |
|
| 18 |
/** |
| 19 |
* @class PluginLayerManager pluginlayermanager.h |
| 20 |
* <avogadro/qtgui/pluginlayermanager.h> |
| 21 |
* @brief The PluginLayerManager class is a set of common layer dependent |
| 22 |
* operators useful for Layer dependent QtPlugins. |
| 23 |
*/ |
| 24 |
class AVOGADROQTGUI_EXPORT PluginLayerManager : protected Core::LayerManager |
| 25 |
{ |
| 26 |
public: |
| 27 |
PluginLayerManager(const std::string& name = "undef"); |
| 28 |
|
| 29 |
~PluginLayerManager(); |
| 30 |
|
| 31 |
/** @return if the active layer in the molecule is locked. */ |
| 32 |
bool activeLayerLocked() const; |
| 33 |
bool atomLocked(size_t atom) const; |
| 34 |
|
| 35 |
/** check if there's existent data in the key and reload it in the custom |
| 36 |
* class. */ |
| 37 |
template <typename T> |
| 38 |
void load() |
| 39 |
{ |
| 40 |
if (m_activeMolecule != nullptr) { |
| 41 |
auto& info = m_molToInfo[m_activeMolecule]; |
| 42 |
if (info->loaded.find(m_name) == info->loaded.end()) { |
| 43 |
for (size_t i = 0; i < info->settings[m_name].size(); ++i) { |
| 44 |
auto serial = info->settings[m_name][i]->getSave(); |
| 45 |
if (serial != "") { |
| 46 |
T* aux = new T; |
| 47 |
aux->deserialize(serial); |
| 48 |
delete info->settings[m_name][i]; |
| 49 |
info->settings[m_name][i] = aux; |
| 50 |
} |
| 51 |
} |
| 52 |
info->loaded.insert(m_name); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
/** @return if the plugin is enabled in any layer */ |
| 58 |
bool isEnabled() const; |
| 59 |
|
| 60 |
/** @return if the plugin is enabled in the active layer */ |
| 61 |
bool isActiveLayerEnabled() const; |
| 62 |
|
| 63 |
/** set active layer @p enable */ |
| 64 |
void setEnabled(bool enable); |
| 65 |
|
| 66 |
/** @return @p atom layer enabled globally and in plugin */ |
| 67 |
bool atomEnabled(Index atom) const; |
| 68 |
|
| 69 |
/** @return @p atom layer enabled globally, in plugin and in @p layer */ |
| 70 |
bool atomEnabled(size_t layer, Index atom) const; |
| 71 |
|
| 72 |
/** @return if @p atom1 or @p atom2 is enabled */ |
| 73 |
bool bondEnabled(Index atom1, Index atom2) const; |
| 74 |
|
| 75 |
/** @return layer id from @p atom */ |
| 76 |
size_t getLayerID(Index atom) const; |
| 77 |
/** @return layer count */ |
| 78 |
size_t layerCount() const; |
| 79 |
|
| 80 |
/** @return custom data T derived from LayerData. if @p layer is equal to |
| 81 |
* MaxIndex returns activeLayer */ |
| 82 |
template <typename T> |
| 83 |
T* getSetting(size_t layer = MaxIndex) |
| 84 |
{ |
| 85 |
auto info = m_molToInfo[m_activeMolecule]; |
| 86 |
|
| 87 |
if (layer == MaxIndex) { |
| 88 |
layer = info->layer.activeLayer(); |
| 89 |
} |
| 90 |
|
| 91 |
assert(layer <= info->layer.maxLayer()); |
| 92 |
if (info->settings.find(m_name) == info->settings.end()) { |
| 93 |
info->settings[m_name] = Core::Array<Core::LayerData*>(); |
| 94 |
} |
| 95 |
|
| 96 |
// do we need to create new layers in the array? |
| 97 |
while (info->settings[m_name].size() < layer + 1) { |
| 98 |
info->settings[m_name].push_back(new T()); |
| 99 |
} |
| 100 |
auto* result = static_cast<T*>(info->settings[m_name][layer]); |
| 101 |
return result; |
| 102 |
} |
| 103 |
|
| 104 |
private: |
| 105 |
// layer key identifier |
| 106 |
std::string m_name; |
| 107 |
}; |
| 108 |
|
| 109 |
} // namespace QtGui |
| 110 |
} // namespace Avogadro |
| 111 |
|
| 112 |
#endif |