1 #include "qui/motif_editor/MotifModel.hpp"
5 MotifModel::MotifModel(MussaRef m, QObject *parent)
7 QAbstractTableModel(parent)
9 const std::set<Sequence>& motif_set = m->motifs();
10 boost::shared_ptr<AnnotationColors> cm = m->colorMapper();
12 std::set<Sequence>::const_iterator motif_i;
13 for(motif_i = motif_set.begin();
14 motif_i != motif_set.end();
17 // ideally we'd know what the motif color was?
18 MotifElement new_element(*motif_i, cm->lookup("motif", motif_i->get_sequence()));
19 push_back(new_element);
24 const MussaRef MotifModel::getAnalysis() const {
28 void MotifModel::assign(
29 MotifModel::size_type num,
30 const MotifElement& val
33 motifs.assign(num, val);
36 MotifElement& MotifModel::at(
37 MotifModel::size_type index
40 return motifs.at(index);
43 MotifElement& MotifModel::back()
48 MotifModel::iterator MotifModel::begin()
50 return motifs.begin();
53 MotifModel::const_iterator MotifModel::begin() const
55 return motifs.begin();
58 void MotifModel::clear()
60 if (motifs.size() != 0) {
61 beginRemoveRows(QModelIndex(), 0, motifs.size()-1);
67 MotifModel::iterator MotifModel::end()
72 MotifModel::const_iterator MotifModel::end() const
77 bool MotifModel::empty() const
79 return motifs.empty();
82 MotifElement& MotifModel::operator[](
83 MotifModel::size_type index
89 void MotifModel::pop_back()
91 int last_element = motifs.size()-1;
92 if (last_element >= 0) {
93 beginRemoveRows(QModelIndex(), last_element, last_element);
99 void MotifModel::push_back(const MotifElement& item)
101 int last_element = motifs.size();
102 beginInsertRows(QModelIndex(), last_element, last_element);
103 motifs.push_back(item);
107 void MotifModel::push_empty()
113 MotifModel::size_type MotifModel::size() const
115 return motifs.size();
119 MotifModel::rowCount( const QModelIndex& parent) const
121 return motifs.size();
125 MotifModel::columnCount(const QModelIndex& parent) const
131 MotifModel::data(const QModelIndex &index, int role) const
133 if (!index.isValid())
136 if (index.row() >= motifs.size())
139 if (index.column() >= 4)
142 const MotifElement& motif = motifs[index.row()];
143 if (role == Qt::DisplayRole) {
144 switch (index.column() ) {
145 case MotifModel::EnabledCell:
146 return QVariant(motif.isEnabled());
148 case MotifModel::ColorCell:
149 return QVariant(motif.getQColor());
151 case MotifModel::NameCell:
152 return QString(motif.getName().c_str());
154 case MotifModel::SequenceCell:
155 return QString(motif.getSequenceText());
163 MotifModel::headerData(
165 Qt::Orientation orientation,
169 if (role != Qt::DisplayRole)
172 if (orientation == Qt::Horizontal) {
174 case MotifModel::EnabledCell:
175 return QString("Enabled");
177 case MotifModel::ColorCell:
178 return QString("Color");
180 case MotifModel::NameCell:
181 return QString("Name");
183 case MotifModel::SequenceCell:
184 return QString("Sequence");
194 bool MotifModel::setData(
195 const QModelIndex& index,
196 const QVariant &value,
200 if (index.isValid() and role == Qt::EditRole) {
201 MotifElement& motif = motifs[index.row()];
202 switch(index.column()) {
203 case MotifModel::EnabledCell:
204 motif.setEnabled(value.toBool());
206 case MotifModel::ColorCell:
207 motif.setQColor(value.value<QColor>());
209 case MotifModel::NameCell:
210 motif.setName(value.toString().toStdString());
212 case MotifModel::SequenceCell:
213 motif.setSequence(value.toString().toStdString());
219 emit dataChanged(index, index);
220 // automatically grow the list of motifs when the last one is full
221 if (not back().isEmpty()) {
230 Qt::ItemFlags MotifModel::flags(const QModelIndex& index) const
232 if (!index.isValid())
233 return Qt::ItemIsEnabled;
235 // first column is not editable
236 if (index.column() == 0)
237 return QAbstractItemModel::flags(index);
239 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;