test MotifModel and MotifElement
[mussa.git] / qui / motif_editor / MotifModel.cpp
1 #include "qui/motif_editor/MotifModel.hpp"
2
3 #include <QColor>
4
5 const size_t MotifModel::model_column_size = 4;
6
7 MotifModel::MotifModel(MussaRef m, QObject *parent) 
8   : analysis(m),
9     QAbstractTableModel(parent)
10 {
11   const std::set<Sequence>& motif_set = m->motifs();
12   boost::shared_ptr<AnnotationColors> cm = m->colorMapper();
13   
14   std::set<Sequence>::const_iterator motif_i;
15   for(motif_i = motif_set.begin();
16       motif_i != motif_set.end();
17       ++motif_i)
18   {
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);
22   }
23   push_empty();
24 }
25
26 const MussaRef MotifModel::getAnalysis() const {
27   return analysis;
28 }
29
30 void MotifModel::assign(
31     MotifModel::size_type num, 
32     const MotifElement& val
33 )
34 {
35   motifs.assign(num, val);
36 }
37
38 MotifElement& MotifModel::at(
39     MotifModel::size_type index
40 )
41 {
42   return motifs.at(index);
43 }
44
45 MotifElement& MotifModel::back()
46 {
47   return motifs.back();
48 }
49
50 MotifModel::iterator MotifModel::begin()
51 {
52   return motifs.begin();
53 }
54
55 MotifModel::const_iterator MotifModel::begin() const
56 {
57   return motifs.begin();
58 }
59
60 void MotifModel::clear()
61 {
62   if (motifs.size() != 0) {
63     beginRemoveRows(QModelIndex(), 0, motifs.size()-1);
64     motifs.clear();
65     endRemoveRows();
66   }
67 }
68
69 MotifModel::iterator MotifModel::end()
70 {
71   return motifs.end();
72 }
73
74 MotifModel::const_iterator MotifModel::end() const
75 {
76   return motifs.end();
77 }
78
79 bool MotifModel::empty() const
80 {
81   return motifs.empty();
82 }
83
84 MotifElement& MotifModel::operator[](
85     MotifModel::size_type index
86 )
87 {
88   return motifs[index];
89 }
90
91 void MotifModel::pop_back()
92 {
93   int last_element = motifs.size()-1;
94   if (last_element >= 0) {
95     beginRemoveRows(QModelIndex(), last_element, last_element);
96     motifs.pop_back();
97     endRemoveRows();
98   } 
99 }
100
101 void MotifModel::push_back(const MotifElement& item)
102 {
103   int last_element = motifs.size();
104   beginInsertRows(QModelIndex(), last_element, last_element);
105   motifs.push_back(item);
106   endInsertRows();
107 }
108
109 void MotifModel::push_empty()
110 {
111   MotifElement blank;
112   push_back(blank);
113 }
114
115 MotifModel::size_type MotifModel::size() const
116 {
117   return motifs.size();
118 }
119
120 int 
121 MotifModel::rowCount( const QModelIndex& parent) const
122 {
123   return motifs.size();
124 }
125
126 int 
127 MotifModel::columnCount(const QModelIndex& parent) const
128 {
129   return model_column_size;
130 }
131
132 QVariant 
133 MotifModel::data(const QModelIndex &index, int role) const
134 {
135   if (!index.isValid())
136     return QVariant();
137   
138   if (index.row() >= motifs.size())
139     return QVariant();
140
141   if (index.column() >= model_column_size)
142     return QVariant();
143
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());
149       break;
150       case MotifModel::ColorCell: 
151         return QVariant(motif.getQColor());
152       break;
153                 case MotifModel::NameCell:
154         return QString(motif.getName().c_str());
155         break;
156       case MotifModel::SequenceCell:
157         return QString(motif.getSequenceText());
158         break;
159      }
160   }
161   return QVariant();
162 }
163
164 QVariant 
165 MotifModel::headerData(
166     int section, 
167     Qt::Orientation orientation, 
168     int role
169 ) const
170 {
171   if (role != Qt::DisplayRole)
172     return QVariant();
173
174   if (orientation == Qt::Horizontal) {
175     switch(section) {
176       case MotifModel::EnabledCell:
177         return QString("Enabled");
178         break;
179       case MotifModel::ColorCell:
180         return QString("Color");
181         break;
182       case MotifModel::NameCell:
183         return QString("Name");
184         break;
185       case MotifModel::SequenceCell:
186         return QString("Sequence");
187         break;
188       default:
189         return QVariant();
190         break;
191     }
192   }
193   return QVariant();
194 }
195
196 bool MotifModel::setData(
197    const QModelIndex& index, 
198    const QVariant &value, 
199    int role
200 )
201 {
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());
207         break;
208       case MotifModel::ColorCell:
209         motif.setQColor(value.value<QColor>());
210         break;
211       case MotifModel::NameCell:
212         motif.setName(value.toString().toStdString());
213         break;
214       case MotifModel::SequenceCell:
215         motif.setSequence(value.toString().toStdString());
216         break;
217       default:
218         return false;
219         break;
220     }
221     emit dataChanged(index, index);
222     // automatically grow the list of motifs when the last one is full
223     if  (not back().isEmpty()) {
224       push_empty();
225     }
226     return true;
227   } else {
228     return false;
229   }
230 }
231
232 Qt::ItemFlags MotifModel::flags(const QModelIndex& index) const
233 {
234   if (!index.isValid())
235     return Qt::ItemIsEnabled;
236
237   // first column is not editable
238   if (index.column() == 0)
239     return QAbstractItemModel::flags(index);
240   else
241     return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
242 }
243