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