| 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 "editortoolwidget.h" |
| 7 |
#include "ui_editortoolwidget.h" |
| 8 |
|
| 9 |
#include <avogadro/core/elements.h> |
| 10 |
|
| 11 |
#include <avogadro/qtgui/elementtranslator.h> |
| 12 |
#include <avogadro/qtgui/periodictableview.h> |
| 13 |
|
| 14 |
#include <QtCore/QList> |
| 15 |
#include <QtCore/QSettings> |
| 16 |
|
| 17 |
#include <algorithm> |
| 18 |
|
| 19 |
namespace { |
| 20 |
// The ItemData of the "Other" entry in the combo box |
| 21 |
const int ELEMENT_SELECTOR_TAG = 255; |
| 22 |
} // namespace |
| 23 |
|
| 24 |
namespace Avogadro::QtPlugins { |
| 25 |
|
| 26 |
EditorToolWidget::EditorToolWidget(QWidget* parent_) |
| 27 |
: QWidget(parent_), m_ui(new Ui::EditorToolWidget), |
| 28 |
m_elementSelector(nullptr), m_currentElement(6) |
| 29 |
{ |
| 30 |
m_ui->setupUi(this); |
| 31 |
|
| 32 |
buildElements(); |
| 33 |
buildBondOrders(); |
| 34 |
|
| 35 |
connect(m_ui->element, SIGNAL(currentIndexChanged(int)), this, |
| 36 |
SLOT(elementChanged(int))); |
| 37 |
|
| 38 |
// Show carbon at startup. |
| 39 |
selectElement(6); |
| 40 |
} |
| 41 |
|
| 42 |
EditorToolWidget::~EditorToolWidget() |
| 43 |
{ |
| 44 |
delete m_ui; |
| 45 |
} |
| 46 |
|
| 47 |
void EditorToolWidget::setAtomicNumber(unsigned char atomicNum) |
| 48 |
{ |
| 49 |
selectElement(atomicNum); |
| 50 |
|
| 51 |
if (m_elementSelector) |
| 52 |
m_elementSelector->setElement(static_cast<int>(atomicNum)); |
| 53 |
} |
| 54 |
|
| 55 |
unsigned char EditorToolWidget::atomicNumber() const |
| 56 |
{ |
| 57 |
int curIndex = m_ui->element->currentIndex(); |
| 58 |
QVariant itemData = m_ui->element->itemData(curIndex); |
| 59 |
if (!itemData.isValid()) |
| 60 |
return 0; |
| 61 |
|
| 62 |
auto atomicNum = static_cast<unsigned char>(itemData.toUInt()); |
| 63 |
|
| 64 |
// "Other…" selected.... |
| 65 |
if (atomicNum == 0 && m_elementSelector) |
| 66 |
atomicNum = static_cast<unsigned char>(m_elementSelector->element()); |
| 67 |
|
| 68 |
return atomicNum; |
| 69 |
} |
| 70 |
|
| 71 |
void EditorToolWidget::setBondOrder(unsigned char order) |
| 72 |
{ |
| 73 |
if (order < m_ui->bondOrder->count()) |
| 74 |
m_ui->bondOrder->setCurrentIndex(static_cast<int>(order)); |
| 75 |
} |
| 76 |
|
| 77 |
unsigned char EditorToolWidget::bondOrder() const |
| 78 |
{ |
| 79 |
return static_cast<unsigned char>(m_ui->bondOrder->currentIndex()); |
| 80 |
} |
| 81 |
|
| 82 |
bool EditorToolWidget::adjustHydrogens() const |
| 83 |
{ |
| 84 |
return m_ui->adjustHydrogens->isChecked(); |
| 85 |
} |
| 86 |
|
| 87 |
void EditorToolWidget::elementChanged(int index) |
| 88 |
{ |
| 89 |
QVariant itemData = m_ui->element->itemData(index); |
| 90 |
if (itemData.isValid()) { |
| 91 |
if (itemData.toInt() == ELEMENT_SELECTOR_TAG) { |
| 92 |
if (!m_elementSelector) { |
| 93 |
m_elementSelector = new QtGui::PeriodicTableView(this); |
| 94 |
connect(m_elementSelector, SIGNAL(elementChanged(int)), this, |
| 95 |
SLOT(elementSelectedFromTable(int))); |
| 96 |
} |
| 97 |
m_elementSelector->setElement(m_currentElement); |
| 98 |
m_elementSelector->show(); |
| 99 |
} else { |
| 100 |
if (m_elementSelector) |
| 101 |
m_elementSelector->setElement(itemData.toInt()); |
| 102 |
m_currentElement = static_cast<unsigned char>(itemData.toInt()); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
void EditorToolWidget::updateElementCombo() |
| 108 |
{ |
| 109 |
// Build set of all elements: |
| 110 |
QList<unsigned char> allElements; |
| 111 |
allElements << m_defaultElements; |
| 112 |
allElements << m_userElements; |
| 113 |
std::sort(allElements.begin(), allElements.end()); |
| 114 |
|
| 115 |
// Cache selected atomic number for later |
| 116 |
QVariant selectedData; |
| 117 |
int curIndex = m_ui->element->currentIndex(); |
| 118 |
if (curIndex >= 0) |
| 119 |
selectedData = m_ui->element->itemData(curIndex); |
| 120 |
|
| 121 |
// Clear and repopulate combo |
| 122 |
m_ui->element->clear(); |
| 123 |
foreach (unsigned char atomicNum, allElements) { |
| 124 |
m_ui->element->addItem(QString("%1 (%2)") |
| 125 |
.arg(QtGui::ElementTranslator::name(atomicNum)) |
| 126 |
.arg(atomicNum), |
| 127 |
atomicNum); |
| 128 |
} |
| 129 |
m_ui->element->insertSeparator(m_ui->element->count()); |
| 130 |
m_ui->element->addItem(tr("Other…"), ELEMENT_SELECTOR_TAG); |
| 131 |
|
| 132 |
// Reset the element if it still exists |
| 133 |
selectElement(static_cast<unsigned char>( |
| 134 |
selectedData.isValid() ? selectedData.toInt() : -1)); |
| 135 |
} |
| 136 |
|
| 137 |
void EditorToolWidget::addUserElement(unsigned char element) |
| 138 |
{ |
| 139 |
// Never add any of the common elements to the user list. |
| 140 |
if (m_defaultElements.contains(element)) |
| 141 |
return; |
| 142 |
|
| 143 |
// If the element is already in the user list, move it to the back of the |
| 144 |
// list. |
| 145 |
if (m_userElements.removeOne(element)) { |
| 146 |
m_userElements << element; |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
m_userElements << element; |
| 151 |
|
| 152 |
// Limit the number of user elements |
| 153 |
/// @todo Make this number of user elements configurable. |
| 154 |
while (m_userElements.size() > 15) |
| 155 |
m_userElements.pop_front(); |
| 156 |
|
| 157 |
updateElementCombo(); |
| 158 |
saveElements(); |
| 159 |
} |
| 160 |
|
| 161 |
void EditorToolWidget::elementSelectedFromTable(int element) |
| 162 |
{ |
| 163 |
addUserElement(static_cast<unsigned char>(element)); |
| 164 |
selectElement(static_cast<unsigned char>(element)); |
| 165 |
} |
| 166 |
|
| 167 |
void EditorToolWidget::selectElement(unsigned char element) |
| 168 |
{ |
| 169 |
int curIndex = element > 0 ? m_ui->element->findData(element) : -1; |
| 170 |
if (curIndex >= 0) |
| 171 |
m_ui->element->setCurrentIndex(curIndex); |
| 172 |
else { |
| 173 |
addUserElement(element); |
| 174 |
curIndex = m_ui->element->findData(element); |
| 175 |
if (curIndex >= 0) |
| 176 |
m_ui->element->setCurrentIndex(curIndex); |
| 177 |
// if we can't find it after adding it, something is very wrong! |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
void EditorToolWidget::buildElements() |
| 182 |
{ |
| 183 |
// Common elements that are always shown in the combo box. |
| 184 |
if (m_defaultElements.isEmpty()) { |
| 185 |
m_defaultElements.append(1); // Hydrogen |
| 186 |
m_defaultElements.append(5); // Boron |
| 187 |
m_defaultElements.append(6); // Carbon |
| 188 |
m_defaultElements.append(7); // Nitrogen |
| 189 |
m_defaultElements.append(8); // Oxygen |
| 190 |
m_defaultElements.append(9); // Fluorine |
| 191 |
m_defaultElements.append(15); // Phosphorus |
| 192 |
m_defaultElements.append(16); // Sulfur |
| 193 |
m_defaultElements.append(17); // Chlorine |
| 194 |
m_defaultElements.append(35); // Bromine |
| 195 |
} |
| 196 |
|
| 197 |
// User-added elements |
| 198 |
QVariantList userElementsVar = |
| 199 |
QSettings().value("editortool/userElements").toList(); |
| 200 |
foreach (const QVariant& var, userElementsVar) |
| 201 |
m_userElements << static_cast<unsigned char>(var.toUInt()); |
| 202 |
|
| 203 |
updateElementCombo(); |
| 204 |
} |
| 205 |
|
| 206 |
void EditorToolWidget::buildBondOrders() |
| 207 |
{ |
| 208 |
m_ui->bondOrder->clear(); |
| 209 |
m_ui->bondOrder->addItem(tr("Automatic"), 0); |
| 210 |
m_ui->bondOrder->addItem(tr("Single"), 1); |
| 211 |
m_ui->bondOrder->addItem(tr("Double"), 2); |
| 212 |
m_ui->bondOrder->addItem(tr("Triple"), 3); |
| 213 |
} |
| 214 |
|
| 215 |
void EditorToolWidget::saveElements() |
| 216 |
{ |
| 217 |
QVariantList atomicNums; |
| 218 |
for (unsigned char m_userElement : m_userElements) |
| 219 |
atomicNums << QVariant(m_userElement); |
| 220 |
|
| 221 |
QSettings().setValue("editortool/userElements", atomicNums); |
| 222 |
} |
| 223 |
|
| 224 |
} // namespace Avogadro::QtPlugins |