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 if (motif_set.size() == 0) {
14 std::set<Sequence>::const_iterator motif_i;
15 for(motif_i = motif_set.begin();
16 motif_i != motif_set.end();
19 // ideally we'd know what the motif color was?
20 MotifElement new_element(*motif_i, cm->lookup("motif", motif_i->get_sequence()));
21 push_back(new_element);
26 void MotifModel::assign(
27 MotifModel::size_type num,
28 const MotifElement& val
31 motifs.assign(num, val);
34 MotifElement& MotifModel::at(
35 MotifModel::size_type index
38 return motifs.at(index);
41 MotifElement& MotifModel::back()
46 MotifModel::iterator MotifModel::begin()
48 return motifs.begin();
51 MotifModel::const_iterator MotifModel::begin() const
53 return motifs.begin();
56 void MotifModel::clear()
58 if (motifs.size() != 0) {
59 beginRemoveRows(QModelIndex(), 0, motifs.size()-1);
65 MotifModel::iterator MotifModel::end()
70 MotifModel::const_iterator MotifModel::end() const
75 bool MotifModel::empty() const
77 return motifs.empty();
80 MotifElement& MotifModel::operator[](
81 MotifModel::size_type index
87 void MotifModel::pop_back()
89 int last_element = motifs.size()-1;
90 if (last_element >= 0) {
91 beginRemoveRows(QModelIndex(), last_element, last_element);
97 void MotifModel::push_back(const MotifElement& item)
99 int last_element = motifs.size();
100 beginInsertRows(QModelIndex(), last_element, last_element);
101 motifs.push_back(item);
105 void MotifModel::push_empty()
111 MotifModel::size_type MotifModel::size() const
113 return motifs.size();
117 MotifModel::rowCount( const QModelIndex& parent) const
119 return motifs.size();
123 MotifModel::columnCount(const QModelIndex& parent) const
129 MotifModel::data(const QModelIndex &index, int role) const
131 if (!index.isValid())
134 if (index.row() >= motifs.size())
137 if (index.column() >= 4)
140 const MotifElement& motif = motifs[index.row()];
141 if (role == Qt::DisplayRole) {
142 switch (index.column() ) {
143 case MotifModel::EnabledCell:
144 return QVariant(motif.isEnabled());
146 case MotifModel::ColorCell:
147 return QVariant(motif.getQColor());
149 case MotifModel::NameCell:
150 return QString(motif.getName().c_str());
152 case MotifModel::SequenceCell:
153 return QString(motif.getSequenceText());
161 MotifModel::headerData(
163 Qt::Orientation orientation,
167 if (role != Qt::DisplayRole)
170 if (orientation == Qt::Horizontal) {
172 case MotifModel::EnabledCell:
173 return QString("Enabled");
175 case MotifModel::ColorCell:
176 return QString("Color");
178 case MotifModel::NameCell:
179 return QString("Name");
181 case MotifModel::SequenceCell:
182 return QString("Sequence");
192 bool MotifModel::setData(
193 const QModelIndex& index,
194 const QVariant &value,
198 if (index.isValid() and role == Qt::EditRole) {
199 MotifElement& motif = motifs[index.row()];
200 switch(index.column()) {
201 case MotifModel::EnabledCell:
202 motif.setEnabled(value.toBool());
204 case MotifModel::ColorCell:
205 motif.setQColor(value.value<QColor>());
207 case MotifModel::NameCell:
208 motif.setName(value.toString().toStdString());
210 case MotifModel::SequenceCell:
211 motif.setSequence(value.toString().toStdString());
217 emit dataChanged(index, index);
218 // automatically grow the list of motifs when the last one is full
219 if (not back().isEmpty()) {
228 Qt::ItemFlags MotifModel::flags(const QModelIndex& index) const
230 if (!index.isValid())
231 return Qt::ItemIsEnabled;
233 // first column is not editable
234 if (index.column() == 0)
235 return QAbstractItemModel::flags(index);
237 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;