Win32 SaveAs Patch
[mussa.git] / qui / SequenceLocationModel.cpp
1 #include "qui/SequenceLocationModel.hpp"
2
3 SequenceLocationModel::SequenceLocationModel(QObject *parent) 
4   : QAbstractTableModel(parent)
5 {
6 }
7
8 void SequenceLocationModel::assign(
9     SequenceLocationModel::size_type num, 
10     const SequenceLocation& val
11 )
12 {
13   sequence_locations.assign(num, val);
14 }
15
16 SequenceLocation& SequenceLocationModel::at(
17     SequenceLocationModel::size_type index
18 )
19 {
20   return sequence_locations.at(index);
21 }
22
23 SequenceLocation& SequenceLocationModel::back()
24 {
25   return sequence_locations.back();
26 }
27
28 SequenceLocationModel::iterator SequenceLocationModel::begin()
29 {
30   return sequence_locations.begin();
31 }
32
33 SequenceLocationModel::const_iterator SequenceLocationModel::begin() const
34 {
35   return sequence_locations.begin();
36 }
37
38 void SequenceLocationModel::clear()
39 {
40   if (sequence_locations.size() != 0) {
41     beginRemoveRows(QModelIndex(), 0, sequence_locations.size()-1);
42     sequence_locations.clear();
43     endRemoveRows();
44   }
45 }
46
47 SequenceLocationModel::iterator SequenceLocationModel::end()
48 {
49   return sequence_locations.end();
50 }
51
52 SequenceLocationModel::const_iterator SequenceLocationModel::end() const
53 {
54   return sequence_locations.end();
55 }
56
57 bool SequenceLocationModel::empty() const
58 {
59   return sequence_locations.empty();
60 }
61
62 SequenceLocation& SequenceLocationModel::operator[](
63     SequenceLocationModel::size_type index
64 )
65 {
66   return sequence_locations[index];
67 }
68
69 void SequenceLocationModel::pop_back()
70 {
71   int last_element = sequence_locations.size()-1;
72   if (last_element >= 0) {
73     beginRemoveRows(QModelIndex(), last_element, last_element);
74     sequence_locations.pop_back();
75     endRemoveRows();
76   } 
77 }
78
79 void SequenceLocationModel::push_back(SequenceLocation& item)
80 {
81   int last_element = sequence_locations.size();
82   beginInsertRows(QModelIndex(), last_element, last_element);
83   sequence_locations.push_back(item);
84   endInsertRows();
85 }
86
87 SequenceLocationModel::size_type SequenceLocationModel::size() const
88 {
89   return sequence_locations.size();
90 }
91
92 int 
93 SequenceLocationModel::rowCount( const QModelIndex& parent) const
94 {
95   return sequence_locations.size();
96 }
97
98 int 
99 SequenceLocationModel::columnCount(const QModelIndex& parent) const
100 {
101   return 3;
102 }
103
104 QVariant 
105 SequenceLocationModel::data(const QModelIndex &index, int role) const
106 {
107   if (!index.isValid())
108     return QVariant();
109   
110   if (index.row() >= sequence_locations.size())
111     return QVariant();
112
113   if (index.column() >= 3)
114     return QVariant();
115
116   if (role == Qt::DisplayRole) {
117     if (index.column() == 0 ) {
118       const Sequence& seq = sequence_locations[index.row()].getSequence();
119       std::string name(seq.get_name());
120       if (name.size() == 0) {
121         return QString(tr("Unnamed Sequence"));
122       } else {
123         return QVariant(QString(name.c_str()));
124       }
125     } else if (index.column() == 1) {
126       return QVariant(sequence_locations[index.row()].getLeft());
127     } else if (index.column() == 2) {
128       return QVariant(sequence_locations[index.row()].getRight());
129     }
130   }
131   return QVariant();
132 }
133
134 QVariant 
135 SequenceLocationModel::headerData(
136     int section, 
137     Qt::Orientation orientation, 
138     int role
139 ) const
140 {
141   if (role != Qt::DisplayRole)
142     return QVariant();
143
144   if (orientation == Qt::Horizontal) {
145     switch(section) {
146       case 0:
147         return QString("Sequence");
148         break;
149       case 1:
150         return QString("Left");
151         break;
152       case 2:
153         return QString("Right");
154         break;
155       default:
156         return QVariant();
157         break;
158     }
159   }
160   return QVariant();
161 }
162
163 bool SequenceLocationModel::setData(
164    const QModelIndex& index, 
165    const QVariant &value, 
166    int role
167 )
168 {
169    bool isInt;
170    int intValue(value.toInt(&isInt));
171    if (index.isValid() and role == Qt::EditRole and isInt) {
172     SequenceLocation& location = sequence_locations[index.row()];
173     switch(index.column()) {
174       case 0:
175         return false;
176         break;
177       case 1:
178         location.setLeft(intValue);
179         break;
180       case 2:
181         location.setRight(intValue);
182         break;
183       default:
184         return false;
185         break;
186     }
187     emit dataChanged(index, index);
188     return true;
189   } else {
190     return false;
191   }
192 }
193
194 Qt::ItemFlags SequenceLocationModel::flags(const QModelIndex& index) const
195 {
196   if (!index.isValid())
197     return Qt::ItemIsEnabled;
198
199   // first column is not editable
200   if (index.column() == 0)
201     return QAbstractItemModel::flags(index);
202   else
203     return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
204 }
205