1 #include "qui/motif_editor/MotifModel.hpp"
5 const size_t MotifModel::model_column_size = 4;
7 MotifModel::MotifModel(MussaRef m, QObject *parent)
9 QAbstractTableModel(parent)
11 const std::set<Sequence>& motif_set = m->motifs();
12 boost::shared_ptr<AnnotationColors> cm = m->colorMapper();
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 const MussaRef MotifModel::getAnalysis() const {
30 void MotifModel::assign(
31 MotifModel::size_type num,
32 const MotifElement& val
35 motifs.assign(num, val);
38 MotifElement& MotifModel::at(
39 MotifModel::size_type index
42 return motifs.at(index);
45 MotifElement& MotifModel::back()
50 MotifModel::iterator MotifModel::begin()
52 return motifs.begin();
55 MotifModel::const_iterator MotifModel::begin() const
57 return motifs.begin();
60 void MotifModel::clear()
62 if (motifs.size() != 0) {
63 beginRemoveRows(QModelIndex(), 0, motifs.size()-1);
69 MotifModel::iterator MotifModel::end()
74 MotifModel::const_iterator MotifModel::end() const
79 bool MotifModel::empty() const
81 return motifs.empty();
84 MotifElement& MotifModel::operator[](
85 MotifModel::size_type index
91 void MotifModel::pop_back()
93 int last_element = motifs.size()-1;
94 if (last_element >= 0) {
95 beginRemoveRows(QModelIndex(), last_element, last_element);
101 void MotifModel::push_back(const MotifElement& item)
103 int last_element = motifs.size();
104 beginInsertRows(QModelIndex(), last_element, last_element);
105 motifs.push_back(item);
109 void MotifModel::push_empty()
115 MotifModel::size_type MotifModel::size() const
117 return motifs.size();
121 MotifModel::rowCount( const QModelIndex& parent) const
123 return motifs.size();
127 MotifModel::columnCount(const QModelIndex& parent) const
129 return model_column_size;
133 MotifModel::data(const QModelIndex &index, int role) const
135 if (!index.isValid())
138 if (index.row() >= motifs.size())
141 if (index.column() >= model_column_size)
144 const MotifElement& motif = motifs[index.row()];
145 if (role == Qt::DisplayRole) {
146 switch (index.column() ) {
147 case MotifModel::EnabledCell:
148 return QVariant(motif.isEnabled());
150 case MotifModel::ColorCell:
151 return QVariant(motif.getQColor());
153 case MotifModel::NameCell:
154 return QString(motif.getName().c_str());
156 case MotifModel::SequenceCell:
157 return QString(motif.getSequenceText());
165 MotifModel::headerData(
167 Qt::Orientation orientation,
171 if (role != Qt::DisplayRole)
174 if (orientation == Qt::Horizontal) {
176 case MotifModel::EnabledCell:
177 return QString("Enabled");
179 case MotifModel::ColorCell:
180 return QString("Color");
182 case MotifModel::NameCell:
183 return QString("Name");
185 case MotifModel::SequenceCell:
186 return QString("Sequence");
196 bool MotifModel::setData(
197 const QModelIndex& index,
198 const QVariant &value,
202 if (index.isValid() and role == Qt::EditRole) {
203 MotifElement& motif = motifs[index.row()];
204 switch(index.column()) {
205 case MotifModel::EnabledCell:
206 motif.setEnabled(value.toBool());
208 case MotifModel::ColorCell:
209 motif.setQColor(value.value<QColor>());
211 case MotifModel::NameCell:
212 motif.setName(value.toString().toStdString());
214 case MotifModel::SequenceCell:
215 motif.setSequence(value.toString().toStdString());
221 emit dataChanged(index, index);
222 // automatically grow the list of motifs when the last one is full
223 if (not back().isEmpty()) {
232 Qt::ItemFlags MotifModel::flags(const QModelIndex& index) const
234 if (!index.isValid())
235 return Qt::ItemIsEnabled;
237 // first column is not editable
238 if (index.column() == 0)
239 return QAbstractItemModel::flags(index);
241 return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;