1 #include "qui/motif_editor/MotifModel.hpp"
5 MotifModel::MotifModel(QObject *parent)
6 : QAbstractTableModel(parent)
10 void MotifModel::assign(
11 MotifModel::size_type num,
12 const MotifElement& val
15 motifs.assign(num, val);
18 MotifElement& MotifModel::at(
19 MotifModel::size_type index
22 return motifs.at(index);
25 MotifElement& MotifModel::back()
30 MotifModel::iterator MotifModel::begin()
32 return motifs.begin();
35 MotifModel::const_iterator MotifModel::begin() const
37 return motifs.begin();
40 void MotifModel::clear()
42 if (motifs.size() != 0) {
43 beginRemoveRows(QModelIndex(), 0, motifs.size()-1);
49 MotifModel::iterator MotifModel::end()
54 MotifModel::const_iterator MotifModel::end() const
59 bool MotifModel::empty() const
61 return motifs.empty();
64 MotifElement& MotifModel::operator[](
65 MotifModel::size_type index
71 void MotifModel::pop_back()
73 int last_element = motifs.size()-1;
74 if (last_element >= 0) {
75 beginRemoveRows(QModelIndex(), last_element, last_element);
81 void MotifModel::push_back(const MotifElement& item)
83 int last_element = motifs.size();
84 beginInsertRows(QModelIndex(), last_element, last_element);
85 motifs.push_back(item);
89 void MotifModel::push_empty()
95 MotifModel::size_type MotifModel::size() const
101 MotifModel::rowCount( const QModelIndex& parent) const
103 return motifs.size();
107 MotifModel::columnCount(const QModelIndex& parent) const
113 MotifModel::data(const QModelIndex &index, int role) const
115 if (!index.isValid())
118 if (index.row() >= motifs.size())
121 if (index.column() >= 4)
124 const MotifElement& motif = motifs[index.row()];
125 if (role == Qt::DisplayRole) {
126 switch (index.column() ) {
127 case MotifModel::EnabledCell:
128 return QVariant(motif.isEnabled());
130 case MotifModel::ColorCell:
131 return QVariant(motif.getQColor());
133 case MotifModel::NameCell:
134 return QString(motif.getName().c_str());
136 case MotifModel::SequenceCell:
137 return QString(motif.getSequenceText());
145 MotifModel::headerData(
147 Qt::Orientation orientation,
151 if (role != Qt::DisplayRole)
154 if (orientation == Qt::Horizontal) {
156 case MotifModel::EnabledCell:
157 return QString("Enabled");
159 case MotifModel::ColorCell:
160 return QString("Color");
162 case MotifModel::NameCell:
163 return QString("Name");
165 case MotifModel::SequenceCell:
166 return QString("Sequence");
176 bool MotifModel::setData(
177 const QModelIndex& index,
178 const QVariant &value,
182 if (index.isValid() and role == Qt::EditRole) {
183 MotifElement& motif = motifs[index.row()];
184 switch(index.column()) {
185 case MotifModel::EnabledCell:
186 motif.setEnabled(value.toBool());
188 case MotifModel::ColorCell:
189 motif.setQColor(value.value<QColor>());
191 case MotifModel::NameCell:
192 motif.setName(value.toString().toStdString());
194 case MotifModel::SequenceCell:
195 motif.setSequence(value.toString().toStdString());
201 emit dataChanged(index, index);
202 // automatically grow the list of motifs when the last one is full
203 if (not back().isEmpty()) {
212 Qt::ItemFlags MotifModel::flags(const QModelIndex& index) const
214 if (!index.isValid())
215 return Qt::ItemIsEnabled;
217 // first column is not editable
218 if (index.column() == 0)
219 return QAbstractItemModel::flags(index);
221 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;