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