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