1 #include "qui/motif_editor/MotifModel.hpp"
5 MotifModel::MotifModel(MussaRef m, QObject *parent)
6 : QAbstractTableModel(parent)
8 const std::set<Sequence>& motif_set = m->motifs();
9 boost::shared_ptr<AnnotationColors> cm = m->colorMapper();
11 std::set<Sequence>::const_iterator motif_i;
12 for(motif_i = motif_set.begin();
13 motif_i != motif_set.end();
16 // ideally we'd know what the motif color was?
17 MotifElement new_element(*motif_i, cm->lookup("motif", motif_i->get_sequence()));
18 push_back(new_element);
23 void MotifModel::assign(
24 MotifModel::size_type num,
25 const MotifElement& val
28 motifs.assign(num, val);
31 MotifElement& MotifModel::at(
32 MotifModel::size_type index
35 return motifs.at(index);
38 MotifElement& MotifModel::back()
43 MotifModel::iterator MotifModel::begin()
45 return motifs.begin();
48 MotifModel::const_iterator MotifModel::begin() const
50 return motifs.begin();
53 void MotifModel::clear()
55 if (motifs.size() != 0) {
56 beginRemoveRows(QModelIndex(), 0, motifs.size()-1);
62 MotifModel::iterator MotifModel::end()
67 MotifModel::const_iterator MotifModel::end() const
72 bool MotifModel::empty() const
74 return motifs.empty();
77 MotifElement& MotifModel::operator[](
78 MotifModel::size_type index
84 void MotifModel::pop_back()
86 int last_element = motifs.size()-1;
87 if (last_element >= 0) {
88 beginRemoveRows(QModelIndex(), last_element, last_element);
94 void MotifModel::push_back(const MotifElement& item)
96 int last_element = motifs.size();
97 beginInsertRows(QModelIndex(), last_element, last_element);
98 motifs.push_back(item);
102 void MotifModel::push_empty()
108 MotifModel::size_type MotifModel::size() const
110 return motifs.size();
114 MotifModel::rowCount( const QModelIndex& parent) const
116 return motifs.size();
120 MotifModel::columnCount(const QModelIndex& parent) const
126 MotifModel::data(const QModelIndex &index, int role) const
128 if (!index.isValid())
131 if (index.row() >= motifs.size())
134 if (index.column() >= 4)
137 const MotifElement& motif = motifs[index.row()];
138 if (role == Qt::DisplayRole) {
139 switch (index.column() ) {
140 case MotifModel::EnabledCell:
141 return QVariant(motif.isEnabled());
143 case MotifModel::ColorCell:
144 return QVariant(motif.getQColor());
146 case MotifModel::NameCell:
147 return QString(motif.getName().c_str());
149 case MotifModel::SequenceCell:
150 return QString(motif.getSequenceText());
158 MotifModel::headerData(
160 Qt::Orientation orientation,
164 if (role != Qt::DisplayRole)
167 if (orientation == Qt::Horizontal) {
169 case MotifModel::EnabledCell:
170 return QString("Enabled");
172 case MotifModel::ColorCell:
173 return QString("Color");
175 case MotifModel::NameCell:
176 return QString("Name");
178 case MotifModel::SequenceCell:
179 return QString("Sequence");
189 bool MotifModel::setData(
190 const QModelIndex& index,
191 const QVariant &value,
195 if (index.isValid() and role == Qt::EditRole) {
196 MotifElement& motif = motifs[index.row()];
197 switch(index.column()) {
198 case MotifModel::EnabledCell:
199 motif.setEnabled(value.toBool());
201 case MotifModel::ColorCell:
202 motif.setQColor(value.value<QColor>());
204 case MotifModel::NameCell:
205 motif.setName(value.toString().toStdString());
207 case MotifModel::SequenceCell:
208 motif.setSequence(value.toString().toStdString());
214 emit dataChanged(index, index);
215 // automatically grow the list of motifs when the last one is full
216 if (not back().isEmpty()) {
225 Qt::ItemFlags MotifModel::flags(const QModelIndex& index) const
227 if (!index.isValid())
228 return Qt::ItemIsEnabled;
230 // first column is not editable
231 if (index.column() == 0)
232 return QAbstractItemModel::flags(index);
234 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;