1 /* massXpert - the true massist's program.
2    --------------------------------------
3    Copyright(C) 2006,2007 Filippo Rusconi
4 
5    http://www.massxpert.org/massXpert
6 
7    This file is part of the massXpert project.
8 
9    The massxpert project is the successor to the "GNU polyxmass"
10    project that is an official GNU project package(see
11    www.gnu.org). The massXpert project is not endorsed by the GNU
12    project, although it is released ---in its entirety--- under the
13    GNU General Public License. A huge part of the code in massXpert
14    is actually a C++ rewrite of code in GNU polyxmass. As such
15    massXpert was started at the Centre National de la Recherche
16    Scientifique(FRANCE), that granted me the formal authorization to
17    publish it under this Free Software License.
18 
19    This software is free software; you can redistribute it and/or
20    modify it under the terms of the GNU  General Public
21    License version 3, as published by the Free Software Foundation.
22 
23 
24    This software is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27    General Public License for more details.
28 
29    You should have received a copy of the GNU General Public License
30    along with this software; if not, write to the
31 
32    Free Software Foundation, Inc.,
33 
34    51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
35 */
36 
37 
38 /////////////////////// Qt includes
39 #include <QDebug>
40 
41 
42 /////////////////////// Local includes
43 #include "massSearchOligomerTableViewModel.hpp"
44 #include "massSearchOligomerTableViewSortProxyModel.hpp"
45 #include "application.hpp"
46 
47 
48 namespace massXpert
49 {
50 
51   MassSearchOligomerTableViewSortProxyModel::
MassSearchOligomerTableViewSortProxyModel(QObject * parent)52   MassSearchOligomerTableViewSortProxyModel(QObject *parent)
53     : QSortFilterProxyModel(parent)
54   {
55   }
56 
57 
58   MassSearchOligomerTableViewSortProxyModel::
~MassSearchOligomerTableViewSortProxyModel()59   ~MassSearchOligomerTableViewSortProxyModel()
60   {
61   }
62 
63 
64 
65   void
setSearchedFilter(double value)66   MassSearchOligomerTableViewSortProxyModel::setSearchedFilter(double value)
67   {
68     m_searchedFilter = value;
69   }
70 
71 
72   void
setErrorFilter(double value)73   MassSearchOligomerTableViewSortProxyModel::setErrorFilter(double value)
74   {
75     m_errorFilter = value;
76   }
77 
78 
79   void
setMonoFilter(double value)80   MassSearchOligomerTableViewSortProxyModel::setMonoFilter(double value)
81   {
82     m_monoFilter = value;
83   }
84 
85 
86   void
setAvgFilter(double value)87   MassSearchOligomerTableViewSortProxyModel::setAvgFilter(double value)
88   {
89     m_avgFilter = value;
90   }
91 
92 
93   void
setTolerance(double value)94   MassSearchOligomerTableViewSortProxyModel::setTolerance(double value)
95   {
96     m_tolerance = value;
97   }
98 
99 
100   bool
lessThan(const QModelIndex & left,const QModelIndex & right) const101   MassSearchOligomerTableViewSortProxyModel::lessThan(const QModelIndex &left,
102 						      const QModelIndex &right)
103     const
104   {
105     Application *application = static_cast<Application *>(qApp);
106     QLocale locale = application->locale();
107 
108     QVariant leftData = sourceModel()->data(left);
109     QVariant rightData = sourceModel()->data(right);
110 
111     if (leftData.type() == QVariant::Int)
112       {
113 	// The Partial column
114 
115 	int leftValue = leftData.toInt();
116 	int rightValue = rightData.toInt();
117 
118 	return(leftValue < rightValue);
119       }
120     if (leftData.type() == QVariant::Bool)
121       {
122 	// The Modif column
123 
124 	int leftValue = leftData.toInt();
125 	int rightValue = rightData.toInt();
126 
127 	return(leftValue < rightValue);
128       }
129     else if (leftData.type() == QVariant::String &&
130 	    (left.column() == MASS_SEARCH_OLIGO_SEARCHED_COLUMN ||
131 	      left.column() == MASS_SEARCH_OLIGO_ERROR_COLUMN ||
132 	      left.column() == MASS_SEARCH_OLIGO_MONO_COLUMN ||
133 	      left.column() == MASS_SEARCH_OLIGO_AVG_COLUMN))
134       {
135 	bool ok = false;
136 
137 	double leftValue = locale.toDouble(leftData.toString(), &ok);
138 	Q_ASSERT(ok);
139 	double rightValue = locale.toDouble(rightData.toString(), &ok);
140 	Q_ASSERT(ok);
141 
142 	return(leftValue < rightValue);
143       }
144 
145     if (leftData.type() == QVariant::String)
146       {
147 	QString leftString = leftData.toString();
148 	QString rightString = rightData.toString();
149 
150 	if(!leftString.isEmpty() && leftString.at(0) == '[')
151 	  return sortCoordinates(leftString, rightString);
152 	else if (!leftString.isEmpty() && leftString.contains('#'))
153 	  return sortName(leftString, rightString);
154 	else
155 	  return(QString::localeAwareCompare(leftString, rightString) < 0);
156       }
157 
158 
159 
160     return true;
161   }
162 
163 
164   bool
sortCoordinates(QString left,QString right) const165   MassSearchOligomerTableViewSortProxyModel::sortCoordinates(QString left,
166 							     QString right) const
167   {
168     QString local = left;
169 
170     local.remove(0, 1);
171     local.remove(local.size() - 1, 1);
172 
173     int dash = local.indexOf('-');
174 
175     QString leftStartStr = local.left(dash);
176 
177     bool ok = false;
178     int leftStartInt = leftStartStr.toInt(&ok);
179 
180     local.remove(0, dash + 1);
181 
182     QString leftEndStr = local;
183 
184     ok = false;
185     int leftEndInt = leftEndStr.toInt(&ok);
186 
187     //    qDebug() << "left coordinates" << leftStartInt << "--" << leftEndInt;
188 
189     local = right;
190 
191     local.remove(0, 1);
192     local.remove(local.size() - 1, 1);
193 
194     dash = local.indexOf('-');
195 
196     QString rightStartStr = local.left(dash);
197 
198     ok = false;
199     int rightStartInt = rightStartStr.toInt(&ok);
200 
201     local.remove(0, dash + 1);
202 
203     QString rightEndStr = local;
204 
205     ok = false;
206     int rightEndInt = rightEndStr.toInt(&ok);
207 
208     //    qDebug() << "right coordinates" << rightStartInt << "--" << rightEndInt;
209 
210 
211     if (leftStartInt < rightStartInt)
212       return true;
213 
214     if (leftEndInt < rightEndInt)
215       return true;
216 
217     return false;
218   }
219 
220 
221   bool
sortName(QString left,QString right) const222   MassSearchOligomerTableViewSortProxyModel::sortName(QString left,
223 						      QString right) const
224   {
225     QString local = left;
226 
227 
228     int diesis = local.indexOf('#');
229 
230     QString leftPartialStr = local.left(diesis);
231 
232     bool ok = false;
233     int leftPartialInt = leftPartialStr.toInt(&ok);
234 
235     local.remove(0, diesis + 1);
236 
237     QString leftOligoStr = local;
238 
239     ok = false;
240     int leftOligoInt = leftOligoStr.toInt(&ok);
241 
242     //    qDebug() << "left: partial#oligo"
243     // 	     << leftPartialInt << "#" << leftOligoInt;
244 
245     local = right;
246 
247     diesis = local.indexOf('#');
248 
249     QString rightPartialStr = local.left(diesis);
250 
251     ok = false;
252     int rightPartialInt = rightPartialStr.toInt(&ok);
253 
254     local.remove(0, diesis + 1);
255 
256     QString rightOligoStr = local;
257 
258     ok = false;
259     int rightOligoInt = rightOligoStr.toInt(&ok);
260 
261     //    qDebug() << "right: partial#oligo"
262     // 	     << rightPartialInt << "#" << rightOligoInt;
263 
264     if (leftPartialInt < rightPartialInt)
265       return true;
266 
267     if (leftOligoInt < rightOligoInt)
268       return true;
269 
270     return false;
271   }
272 
273 
274   bool
filterAcceptsRow(int sourceRow,const QModelIndex & sourceParent) const275   MassSearchOligomerTableViewSortProxyModel::filterAcceptsRow
276  (int sourceRow, const QModelIndex & sourceParent) const
277   {
278     Application *application = static_cast<Application *>(qApp);
279     QLocale locale = application->locale();
280 
281     int filterKeyColumnIndex = filterKeyColumn();
282 
283     if (filterKeyColumnIndex == MASS_SEARCH_OLIGO_SEARCHED_COLUMN)
284       {
285 	// The column of the searched mass.
286 	QModelIndex index =
287 	  sourceModel()->index(sourceRow,
288 				 MASS_SEARCH_OLIGO_SEARCHED_COLUMN,
289 				 sourceParent);
290 
291 	bool ok = false;
292 
293 	QVariant data = sourceModel()->data(index);
294 
295 	double searched = locale.toDouble(data.toString(), &ok);
296 	Q_ASSERT(ok);
297 
298 	if(m_searchedFilter == searched)
299 	  return true;
300 	else
301 	  return false;
302       }
303     else if (filterKeyColumnIndex == MASS_SEARCH_OLIGO_ERROR_COLUMN)
304       {
305 	// The column of the error.
306 	QModelIndex index =
307 	  sourceModel()->index(sourceRow,
308 				 MASS_SEARCH_OLIGO_ERROR_COLUMN,
309 				 sourceParent);
310 
311 	bool ok = false;
312 
313 	QVariant data = sourceModel()->data(index);
314 
315 	double error = locale.toDouble(data.toString(), &ok);
316 	Q_ASSERT(ok);
317 
318 	double left = m_errorFilter - m_tolerance;
319 	double right = m_errorFilter + m_tolerance;
320 
321 	if(left <= error && error <= right)
322 	  return true;
323 	else
324 	  return false;
325       }
326     else if (filterKeyColumnIndex == MASS_SEARCH_OLIGO_MONO_COLUMN)
327       {
328 	// The column of the mono mass.
329 	QModelIndex index =
330 	  sourceModel()->index(sourceRow,
331 				 MASS_SEARCH_OLIGO_MONO_COLUMN,
332 				 sourceParent);
333 
334 	bool ok = false;
335 
336 	QVariant data = sourceModel()->data(index);
337 
338 	double mass = locale.toDouble(data.toString(), &ok);
339 	Q_ASSERT(ok);
340 
341 	double left = m_monoFilter - m_tolerance;
342 	double right = m_monoFilter + m_tolerance;
343 
344 	if(left <= mass && mass <= right)
345 	  return true;
346 	else
347 	  return false;
348       }
349     else if (filterKeyColumnIndex == MASS_SEARCH_OLIGO_AVG_COLUMN)
350       {
351 	// The column of the avg mass.
352 	QModelIndex index =
353 	  sourceModel()->index(sourceRow,
354 				 MASS_SEARCH_OLIGO_AVG_COLUMN,
355 				 sourceParent);
356 
357 	bool ok = false;
358 
359 	QVariant data = sourceModel()->data(index);
360 
361 	double mass = locale.toDouble(data.toString(), &ok);
362 	Q_ASSERT(ok);
363 
364 	double left = m_avgFilter - m_tolerance;
365 	double right = m_avgFilter + m_tolerance;
366 
367 	if(left <= mass && mass <= right)
368 	  return true;
369 	else
370 	  return false;
371       }
372 
373     return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
374   }
375 
376 
377 
378   void
applyNewFilter()379   MassSearchOligomerTableViewSortProxyModel::applyNewFilter()
380   {
381     invalidateFilter();
382   }
383 
384 } // namespace massXpert
385