| 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 "constraintsmodel.h" |
| 7 |
|
| 8 |
#include <QtCore/QDebug> |
| 9 |
#include <QtCore/QString> |
| 10 |
|
| 11 |
using namespace std; |
| 12 |
|
| 13 |
namespace Avogadro { |
| 14 |
using Core::Constraint; |
| 15 |
|
| 16 |
namespace QtPlugins { |
| 17 |
|
| 18 |
void ConstraintsModel::emitDataChanged() |
| 19 |
{ |
| 20 |
emit dataChanged(QModelIndex(), QModelIndex()); |
| 21 |
} |
| 22 |
|
| 23 |
void ConstraintsModel::setConstraints( |
| 24 |
const std::vector<Core::Constraint>& constraints) |
| 25 |
{ |
| 26 |
beginResetModel(); |
| 27 |
m_constraints = constraints; |
| 28 |
endResetModel(); |
| 29 |
} |
| 30 |
|
| 31 |
int ConstraintsModel::rowCount(const QModelIndex&) const |
| 32 |
{ |
| 33 |
return m_constraints.size(); |
| 34 |
} |
| 35 |
|
| 36 |
int ConstraintsModel::columnCount(const QModelIndex&) const |
| 37 |
{ |
| 38 |
// Type, value, atom 1, 2, 3, 4 |
| 39 |
return 6; |
| 40 |
} |
| 41 |
|
| 42 |
QVariant ConstraintsModel::data(const QModelIndex& index, int role) const |
| 43 |
{ |
| 44 |
if (!index.isValid()) |
| 45 |
return QVariant(); |
| 46 |
|
| 47 |
if (index.row() >= m_constraints.size()) |
| 48 |
return QVariant(); |
| 49 |
|
| 50 |
Constraint currentConstraint = m_constraints[index.row()]; |
| 51 |
Index aIndex = currentConstraint.aIndex(); |
| 52 |
Index bIndex = currentConstraint.bIndex(); |
| 53 |
Index cIndex = currentConstraint.cIndex(); |
| 54 |
Index dIndex = currentConstraint.dIndex(); |
| 55 |
|
| 56 |
if (role == Qt::DisplayRole || role == Qt::UserRole) |
| 57 |
switch (index.column()) { |
| 58 |
case 0: |
| 59 |
if (currentConstraint.type() == 1) |
| 60 |
return tr("Distance"); |
| 61 |
else if (currentConstraint.type() == 2) |
| 62 |
return tr("Angle"); |
| 63 |
else if (currentConstraint.type() == 3) |
| 64 |
return tr("Torsion Angle"); |
| 65 |
// these aren't really implemented in the UI yet |
| 66 |
// but we're saving the strings for translation |
| 67 |
else if (currentConstraint.type() == 4) |
| 68 |
return tr("Freeze Atom", "fix / remain constant"); |
| 69 |
else if (currentConstraint.type() == 5) |
| 70 |
return tr("Freeze X Axis", "fix / remain constant"); |
| 71 |
else if (currentConstraint.type() == 6) |
| 72 |
return tr("Freeze Y Axis", "fix / remain constant"); |
| 73 |
else if (currentConstraint.type() == 7) |
| 74 |
return tr("Freeze Z Axis", "fix / remain constant"); |
| 75 |
break; |
| 76 |
case 1: |
| 77 |
// TODO handle fixed-length number and sorting |
| 78 |
if (role == Qt::UserRole) |
| 79 |
return currentConstraint.value(); |
| 80 |
|
| 81 |
if (currentConstraint.type() == 1) |
| 82 |
return QString("%L1 Å").arg(currentConstraint.value(), 0, 'f', 3); |
| 83 |
else if (currentConstraint.type() == 2 || currentConstraint.type() == 3) |
| 84 |
return QString("%L1 °").arg(currentConstraint.value(), 0, 'f', 3); |
| 85 |
else |
| 86 |
return "--"; |
| 87 |
break; |
| 88 |
case 2: |
| 89 |
if (aIndex != MaxIndex) |
| 90 |
return QVariant(static_cast<qulonglong>(aIndex) + 1); |
| 91 |
else |
| 92 |
return "--"; |
| 93 |
break; |
| 94 |
case 3: |
| 95 |
if (bIndex != MaxIndex) |
| 96 |
return QVariant(static_cast<qulonglong>(bIndex) + 1); |
| 97 |
else |
| 98 |
return "--"; |
| 99 |
break; |
| 100 |
case 4: |
| 101 |
if (cIndex != MaxIndex) |
| 102 |
return QVariant(static_cast<qulonglong>(cIndex) + 1); |
| 103 |
else |
| 104 |
return "--"; |
| 105 |
break; |
| 106 |
case 5: |
| 107 |
if (dIndex != MaxIndex) |
| 108 |
return QVariant(static_cast<qulonglong>(dIndex) + 1); |
| 109 |
else |
| 110 |
return "--"; |
| 111 |
break; |
| 112 |
} |
| 113 |
|
| 114 |
return QVariant(); |
| 115 |
} |
| 116 |
|
| 117 |
QVariant ConstraintsModel::headerData(int section, Qt::Orientation orientation, |
| 118 |
int role) const |
| 119 |
{ |
| 120 |
if (role != Qt::DisplayRole) |
| 121 |
return QVariant(); |
| 122 |
|
| 123 |
if (orientation == Qt::Horizontal) { |
| 124 |
switch (section) { |
| 125 |
case 0: |
| 126 |
return tr("Type"); |
| 127 |
break; |
| 128 |
case 1: |
| 129 |
return tr("Value"); |
| 130 |
break; |
| 131 |
case 2: |
| 132 |
return tr("Atom 1"); |
| 133 |
break; |
| 134 |
case 3: |
| 135 |
return tr("Atom 2"); |
| 136 |
break; |
| 137 |
case 4: |
| 138 |
return tr("Atom 3"); |
| 139 |
break; |
| 140 |
case 5: |
| 141 |
return tr("Atom 4"); |
| 142 |
break; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
return section + 1; |
| 147 |
} |
| 148 |
|
| 149 |
void ConstraintsModel::addConstraint(int type, int a, int b, int c, int d, |
| 150 |
double value) |
| 151 |
{ |
| 152 |
beginInsertRows(QModelIndex(), m_constraints.size(), m_constraints.size()); |
| 153 |
m_constraints.push_back(Constraint(a, b, c, d, value)); |
| 154 |
endInsertRows(); |
| 155 |
} |
| 156 |
|
| 157 |
void ConstraintsModel::clear() |
| 158 |
{ |
| 159 |
if (m_constraints.size()) { |
| 160 |
beginRemoveRows(QModelIndex(), 0, m_constraints.size() - 1); |
| 161 |
m_constraints.clear(); |
| 162 |
endRemoveRows(); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
void ConstraintsModel::deleteConstraint(int index) |
| 167 |
{ |
| 168 |
if (m_constraints.size() && (index >= 0)) { |
| 169 |
beginRemoveRows(QModelIndex(), index, index); |
| 170 |
auto position = m_constraints.begin() + index; |
| 171 |
m_constraints.erase(position); |
| 172 |
endRemoveRows(); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
Core::Constraint ConstraintsModel::constraint(int index) |
| 177 |
{ |
| 178 |
if (index < 0 || index >= m_constraints.size()) |
| 179 |
return Constraint(MaxIndex, MaxIndex, MaxIndex, MaxIndex, 0.0); |
| 180 |
else |
| 181 |
return m_constraints[index]; |
| 182 |
} |
| 183 |
|
| 184 |
} // namespace QtPlugins |
| 185 |
} // end namespace Avogadro |