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