1 /******************************************************************************
2 
3   This source file is part of the Avogadro project.
4 
5   Copyright 2015 Kitware, Inc.
6 
7   This source code is released under the New BSD License, (the "License").
8 
9   Unless required by applicable law or agreed to in writing, software
10   distributed under the License is distributed on an "AS IS" BASIS,
11   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   See the License for the specific language governing permissions and
13   limitations under the License.
14 
15 ******************************************************************************/
16 
17 #include "vibrationmodel.h"
18 
19 #include <avogadro/qtgui/molecule.h>
20 
21 namespace Avogadro {
22 namespace QtPlugins {
23 
VibrationModel(QObject * p)24 VibrationModel::VibrationModel(QObject* p)
25   : QAbstractItemModel(p), m_molecule(nullptr)
26 {
27 }
28 
parent(const QModelIndex &) const29 QModelIndex VibrationModel::parent(const QModelIndex&) const
30 {
31   return QModelIndex();
32 }
33 
rowCount(const QModelIndex & p) const34 int VibrationModel::rowCount(const QModelIndex& p) const
35 {
36   if (p.isValid() || !m_molecule)
37     return 0;
38   else
39     return m_molecule->vibrationFrequencies().size();
40 }
41 
columnCount(const QModelIndex &) const42 int VibrationModel::columnCount(const QModelIndex&) const
43 {
44   return 2;
45 }
46 
flags(const QModelIndex &) const47 Qt::ItemFlags VibrationModel::flags(const QModelIndex&) const
48 {
49   return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
50 }
51 
headerData(int section,Qt::Orientation orientation,int role) const52 QVariant VibrationModel::headerData(int section, Qt::Orientation orientation,
53                                     int role) const
54 {
55   if (role == Qt::DisplayRole) {
56     if (orientation == Qt::Horizontal) {
57       switch (section) {
58         case 0:
59           return QString("Frequency (cm⁻¹)");
60         case 1:
61           return QString("Intensity (KM/mol)");
62       }
63     }
64   }
65   return QVariant();
66 }
67 
setData(const QModelIndex &,const QVariant &,int)68 bool VibrationModel::setData(const QModelIndex&, const QVariant&, int)
69 {
70   return false;
71 }
72 
data(const QModelIndex & idx,int role) const73 QVariant VibrationModel::data(const QModelIndex& idx, int role) const
74 {
75   if (!idx.isValid() || idx.column() > 2 || !m_molecule ||
76       m_molecule->vibrationFrequencies().size() <= idx.row()) {
77     return QVariant();
78   }
79 
80   if (role == Qt::DisplayRole) {
81     switch (idx.column()) {
82       case 0:
83         if (m_molecule->vibrationFrequencies().size() > idx.row())
84           return m_molecule->vibrationFrequencies()[idx.row()];
85         else
86           return "No value";
87       case 1:
88         if (m_molecule->vibrationIntensities().size() > idx.row())
89           return m_molecule->vibrationIntensities()[idx.row()];
90         else
91           return "No value";
92       default:
93         return "Invalid";
94     }
95   }
96 
97   return QVariant();
98 }
99 
index(int row,int column,const QModelIndex & p) const100 QModelIndex VibrationModel::index(int row, int column,
101                                   const QModelIndex& p) const
102 {
103   if (!p.isValid())
104     if (row >= 0 && m_molecule &&
105         row < m_molecule->vibrationFrequencies().size())
106       return createIndex(row, column);
107   return QModelIndex();
108 }
109 
clear()110 void VibrationModel::clear()
111 {
112 }
113 }
114 }
115