| 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 "customelementdialog.h" |
| 7 |
#include "ui_customelementdialog.h" |
| 8 |
|
| 9 |
#include "elementtranslator.h" |
| 10 |
#include "molecule.h" |
| 11 |
|
| 12 |
#include <avogadro/core/elements.h> |
| 13 |
|
| 14 |
#include <QtWidgets/QComboBox> |
| 15 |
|
| 16 |
#include <set> |
| 17 |
|
| 18 |
using Avogadro::Core::Elements; |
| 19 |
|
| 20 |
namespace Avogadro::QtGui { |
| 21 |
|
| 22 |
CustomElementDialog::CustomElementDialog(Molecule& mol, QWidget* p) |
| 23 |
: QDialog(p), m_ui(new Ui::CustomElementDialog), m_molecule(mol) |
| 24 |
{ |
| 25 |
m_ui->setupUi(this); |
| 26 |
prepareElements(); |
| 27 |
prepareForm(); |
| 28 |
} |
| 29 |
|
| 30 |
CustomElementDialog::~CustomElementDialog() |
| 31 |
{ |
| 32 |
delete m_ui; |
| 33 |
} |
| 34 |
|
| 35 |
void CustomElementDialog::resolve(QWidget* p, Molecule& mol) |
| 36 |
{ |
| 37 |
CustomElementDialog dlg(mol, p); |
| 38 |
int reply = dlg.exec(); |
| 39 |
if (static_cast<DialogCode>(reply) == Accepted) |
| 40 |
dlg.apply(); |
| 41 |
} |
| 42 |
|
| 43 |
namespace { |
| 44 |
struct RemapAtomicNumbers |
| 45 |
{ |
| 46 |
typedef std::map<unsigned char, unsigned char> MapType; |
| 47 |
const MapType& map; |
| 48 |
|
| 49 |
RemapAtomicNumbers(const MapType& m) : map(m) {} |
| 50 |
RemapAtomicNumbers(const RemapAtomicNumbers& o) : map(o.map) {} |
| 51 |
|
| 52 |
unsigned char operator()(unsigned char old) const |
| 53 |
{ |
| 54 |
if (Core::isCustomElement(old)) { |
| 55 |
auto it = map.find(old); |
| 56 |
return it == map.end() ? old : it->second; |
| 57 |
} |
| 58 |
return old; |
| 59 |
} |
| 60 |
}; |
| 61 |
} // namespace |
| 62 |
|
| 63 |
void CustomElementDialog::apply() |
| 64 |
{ |
| 65 |
RemapAtomicNumbers::MapType oldToNew; |
| 66 |
Molecule::CustomElementMap newMap; |
| 67 |
const Molecule::CustomElementMap& oldMap = m_molecule.customElementMap(); |
| 68 |
unsigned char newIdGenerator = CustomElementMin; |
| 69 |
foreach (QComboBox* combo, findChildren<QComboBox*>()) { |
| 70 |
unsigned char oldId = |
| 71 |
static_cast<unsigned char>(combo->property("id").toUInt()); |
| 72 |
|
| 73 |
int currentIndex = combo->currentIndex(); |
| 74 |
|
| 75 |
if (currentIndex == 0) { |
| 76 |
// Reuse old name: |
| 77 |
unsigned char newId = newIdGenerator++; |
| 78 |
auto it = oldMap.find(oldId); |
| 79 |
newMap.insert(std::make_pair(newId, it->second)); |
| 80 |
oldToNew.insert(std::make_pair(oldId, newId)); |
| 81 |
} else { |
| 82 |
// New element assigned: |
| 83 |
auto newId = static_cast<unsigned char>(currentIndex); |
| 84 |
oldToNew.insert(std::make_pair(oldId, newId)); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if (newMap.size() != oldMap.size()) { |
| 89 |
Core::Array<unsigned char> atomicNumbers = m_molecule.atomicNumbers(); |
| 90 |
std::transform(atomicNumbers.begin(), atomicNumbers.end(), |
| 91 |
atomicNumbers.begin(), RemapAtomicNumbers(oldToNew)); |
| 92 |
m_molecule.setAtomicNumbers(atomicNumbers); |
| 93 |
m_molecule.setCustomElementMap(newMap); |
| 94 |
m_molecule.emitChanged(Molecule::Atoms | Molecule::Modified); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
void CustomElementDialog::prepareElements() |
| 99 |
{ |
| 100 |
int maxNumber = ElementTranslator::numberOfElements(); |
| 101 |
m_elements.reserve(maxNumber); |
| 102 |
for (int i = 1; i <= maxNumber; ++i) |
| 103 |
m_elements.append(ElementTranslator::name(i)); |
| 104 |
} |
| 105 |
|
| 106 |
namespace { |
| 107 |
struct CustomElementFilter |
| 108 |
{ |
| 109 |
std::set<unsigned char> customElements; |
| 110 |
void operator()(unsigned char atomicNumber) |
| 111 |
{ |
| 112 |
if (Core::isCustomElement(atomicNumber)) |
| 113 |
customElements.insert(atomicNumber); |
| 114 |
} |
| 115 |
|
| 116 |
operator std::set<unsigned char>() const { return customElements; } |
| 117 |
}; |
| 118 |
} // namespace |
| 119 |
|
| 120 |
void CustomElementDialog::prepareForm() |
| 121 |
{ |
| 122 |
const Molecule::CustomElementMap& map = m_molecule.customElementMap(); |
| 123 |
const Core::Array<unsigned char>& atomicNumbers = m_molecule.atomicNumbers(); |
| 124 |
|
| 125 |
std::set<unsigned char> customElements = std::for_each( |
| 126 |
atomicNumbers.begin(), atomicNumbers.end(), CustomElementFilter()); |
| 127 |
|
| 128 |
Molecule::CustomElementMap::const_iterator match; |
| 129 |
for (unsigned char customElement : customElements) { |
| 130 |
if ((match = map.find(customElement)) != map.end()) |
| 131 |
addRow(customElement, QString::fromStdString(match->second)); |
| 132 |
else |
| 133 |
addRow(customElement, |
| 134 |
QString::fromStdString(Elements::name(customElement))); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
void CustomElementDialog::addRow(unsigned char elementId, const QString& name) |
| 139 |
{ |
| 140 |
auto* combo = new QComboBox(this); |
| 141 |
combo->setProperty("id", static_cast<unsigned int>(elementId)); |
| 142 |
combo->addItem(name); |
| 143 |
combo->addItems(m_elements); |
| 144 |
|
| 145 |
unsigned int atomicNumber = Elements::guessAtomicNumber(name.toStdString()); |
| 146 |
if (atomicNumber != InvalidElement) |
| 147 |
combo->setCurrentIndex(static_cast<int>(atomicNumber)); |
| 148 |
else |
| 149 |
combo->setCurrentIndex(0); |
| 150 |
|
| 151 |
m_ui->form->addRow(name + ":", combo); |
| 152 |
} |
| 153 |
|
| 154 |
} // namespace Avogadro::QtGui |