File: sequenceSelection.cpp

package info (click to toggle)
massxpert 2.3.6-1squeeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 20,736 kB
  • ctags: 3,541
  • sloc: cpp: 44,108; xml: 7,381; sh: 604; makefile: 108; ansic: 7
file content (532 lines) | stat: -rw-r--r-- 13,650 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/* massXpert - the true massist's program.
   --------------------------------------
   Copyright(C) 2006,2007 Filippo Rusconi

   http://www.filomace.org/massXpert

   This file is part of the massXpert project.

   The massxpert project is the successor to the "GNU polyxmass"
   project that is an official GNU project package(see
   www.gnu.org). The massXpert project is not endorsed by the GNU
   project, although it is released ---in its entirety--- under the
   GNU General Public License. A huge part of the code in massXpert
   is actually a C++ rewrite of code in GNU polyxmass. As such
   massXpert was started at the Centre National de la Recherche
   Scientifique(FRANCE), that granted me the formal authorization to
   publish it under this Free Software License.

   This software is free software; you can redistribute it and/or
   modify it under the terms of the GNU  General Public
   License version 3, as published by the Free Software Foundation.
   

   This software is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this software; if not, write to the

   Free Software Foundation, Inc.,

   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/


/////////////////////// Qt includes


/////////////////////// Local includes
#include "sequenceSelection.hpp"
#include "sequenceEditorGraphicsView.hpp"


namespace massXpert
{


  SequenceSelection::SequenceSelection(SequenceEditorGraphicsView *view)
    : mp_view(view)
  {
  }
  
  
  SequenceSelection::~SequenceSelection()
  {
    while(!m_regionSelectionList.isEmpty())
      delete m_regionSelectionList.takeFirst();
  }
  

  void  
  SequenceSelection::setView(SequenceEditorGraphicsView *view)
  {
    mp_view = view;
  }
  
  
  SequenceEditorGraphicsView * 
  SequenceSelection::view()
  {
    return mp_view;
  }
  
  
  const QList<RegionSelection *>& 
  SequenceSelection::regionSelectionList() const
  {
    return m_regionSelectionList;
  }
  
  int
  SequenceSelection::selectRegion(const QPointF &point1,
				   const QPointF &point2,
				   bool multiRegionSelection,
				   bool multiSelectionRegion)
  {
    // We are asked to create a region selection encompassing the two
    // points passed as parameter.
    
    if (!isManhattanLength(point1, point2))
      {
// 	qDebug() << __FILE__ << __LINE__ 
// 		  << "selectRegion: isManhattanLength failed with:"
// 		  << point1 << point2;
	
	return 0;
      }
    
//     qDebug() << __FILE__ << __LINE__ 
// 	      << "selectRegion with params:" 
// 	      << point1 << point2;
    
    if (!multiRegionSelection)
      {
	// multiRegionSelection: there can be more than one region
	// selected in the sequence.

	// The caller asks that there cannot be more than one selected
	// region at any given time in the sequence. Thus we first
	// eliminate any previously existing region selection.

	deselectRegions();
      }
    
    if (!multiSelectionRegion)
      {
	// multiSelectionRegion: one region in the sequence might be
	// selected more than once.

	// The caller asks that if some regionSelection already
	// encompasses the region we are about to select, then we
	// first should remove it.
	
	RegionSelection *preSelection = 
	  regionSelectionEncompassing(point1, point2);
    
	if(preSelection)
	  {
// 	    qDebug() << __FILE__ << __LINE__
// 	      << "selectRegion: delectRegion found encompassing:"
// 		      << preSelection;
	
	    deselectRegion(preSelection);
	  }
      }
    
     // At this point we know we can select the region.

    RegionSelection *selection = new RegionSelection(mp_view);

    // Returns the number of actual selection rectangle graphics items
    // ware created to draw the whole selection mark.
    int result = selection->drawMark(point1, point2);

    if (result < 1)
      {
	delete selection;
	return result;
      }

//     qDebug() << __FILE__ << __LINE__ 
// 	      << "selectRegion indices:" << selection
// 	      << "[" << selection->startIndex() << "--"
// 	      << selection->endIndex() << "]";
    
    
    m_regionSelectionList.append(selection);
    
//     qDebug() <<__FILE__ << __LINE__
// 	      << "selectRegion count:" <<m_regionSelectionList.size();
    
    return result;
  }
  

  int
  SequenceSelection::selectRegion(int index1, int index2,
				   bool multiRegionSelection,
				   bool multiSelectionRegion)
  {
    // We are asked to create a region selection encompassing the two
    // indices passed as parameter.
    
//     qDebug() << __FILE__ << __LINE__ 
// 	      << "selectRegion with params:" 
// 	      << index1 << index2;
    
    if (!multiRegionSelection)
      {
	// multiRegionSelection: there can be more than one region
	// selected in the sequence.

	// The caller asks that there cannot be more than one selected
	// region at any given time in the sequence. Thus we first
	// eliminate any previously existing region selection.

	deselectRegions();
      }
    
    if (!multiSelectionRegion)
      {
	// multiSelectionRegion: one region in the sequence might be
	// selected more than once.

	// The caller asks that if some regionSelection already
	// encompasses the region we are about to select, then we
	// first should remove it.
	
	RegionSelection *preSelection = 
	  regionSelectionEncompassing(index1, index2);
    
	if(preSelection)
	  {
// 	    qDebug() << __FILE__ << __LINE__
// 	      << "selectRegion: delectRegion found encompassing:"
// 		      << preSelection;
	
	    deselectRegion(preSelection);
	  }
      }
    
     // At this point we know we can select the region.

    RegionSelection *selection = new RegionSelection(mp_view);

    // Returns the number of actual selection rectangle graphics items
    // ware created to draw the whole selection mark.
    int result = selection->drawMark(index1, index2);

    if (result < 1)
      {
	delete selection;
	return result;
      }

//     qDebug() << __FILE__ << __LINE__ 
// 	      << "selectRegion indices:" << selection
// 	      << "[" << selection->startIndex() << "--"
// 	      << selection->endIndex() << "]";
    
    
    m_regionSelectionList.append(selection);
    
//     qDebug() <<__FILE__ << __LINE__
// 	      << "selectRegion count:" <<m_regionSelectionList.size();
    
    return result;
  }
  

  int
  SequenceSelection::deselectRegions()
  {
    while(!m_regionSelectionList.isEmpty())
      delete m_regionSelectionList.takeFirst();
    
//     qDebug() <<__FILE__ << __LINE__
// 	      << "selectRegion count:" <<m_regionSelectionList.size();

    return 1;
  }
  

  int
  SequenceSelection::reselectRegions()
  {
    for (int iter = 0; iter < m_regionSelectionList.size(); ++iter)
      {
	RegionSelection *iterRegionSelection = m_regionSelectionList.at(iter);
	
	iterRegionSelection->redrawMark();
      }
    
    return m_regionSelectionList.size();
  }
  

  int
  SequenceSelection::deselectRegion(RegionSelection *regionSelection)
  {
    Q_ASSERT(regionSelection);
    
    for (int iter = 0; iter < m_regionSelectionList.size(); ++iter)
      {
	RegionSelection *iterRegionSelection = m_regionSelectionList.at(iter);
	
	if(iterRegionSelection == regionSelection)
	  {
	    m_regionSelectionList.takeAt(iter);
	    
// 	    qDebug() << __FILE__ << __LINE__ 
// 		      << "deselectRegion:" << iterRegionSelection;
	    
	    delete(iterRegionSelection);
	    
	    return 1;
	  }
      }
    
    return 0;
  }
  

  int
  SequenceSelection::deselectLastRegion()
  {
    if (m_regionSelectionList.isEmpty())
      return 0;
    
    RegionSelection *selection = m_regionSelectionList.takeLast();
    
//     qDebug() << __FILE__ << __LINE__ 
// 	      << "deselectLastRegion:" << selection;
    
    delete(selection);
  
//     qDebug() <<__FILE__ << __LINE__
// 	      << "selectRegion count:" <<m_regionSelectionList.size();

    return 1;
  }

  
  int
  SequenceSelection::deselectRegionsButLast()
  {
    if (m_regionSelectionList.isEmpty())
      return 0;
    
    while(m_regionSelectionList.size() > 1)
      delete m_regionSelectionList.takeFirst();
    
    return 1;
  }

  int 
  SequenceSelection::deselectMultiSelectionRegionsButFirstSelection()
  {
    // If any region is selected more than once, then remove all the
    // selections but the first one. The result is that there might be
    // more than one selected regions in the sequence, BUT each region
    // is selected only once.

    int listIndex = 0;
    
    for (int iter = 0; iter < m_regionSelectionList.size(); ++iter)
      {
	RegionSelection *iterRegionSelection = m_regionSelectionList.at(iter);

	RegionSelection *foundRegionSelection = 
	  regionSelectionEncompassing(iterRegionSelection->startIndex(),
				       iterRegionSelection->endIndex(),
				       &listIndex);
	
	while(foundRegionSelection)
	  {
	    if (iterRegionSelection != foundRegionSelection)
	      {
		// Use the removeAll function, because we only have
		// one such pointer anyway, and the removeOne()
		// function is only available with Qt 4.4.0.
		m_regionSelectionList.removeAll(foundRegionSelection);
		delete(foundRegionSelection);

		--listIndex;
	      }
		    
	    ++listIndex;
	    
	    foundRegionSelection = 
	      regionSelectionEncompassing(iterRegionSelection->startIndex(),
					   iterRegionSelection->endIndex(),
					   &listIndex);
	  }
      }
    
    return 1;
  }
  

  RegionSelection *
  SequenceSelection::regionSelectionEncompassing(int index1, int index2,
						  int *listIndex)
  {
    // Return the region selection that contains the vignette at index
    // 'index1' and 'index2'

    int iter = 0;
    
//     qDebug() << __FILE__ << __LINE__
// 	      << "regionSelectionEncompassing: searching for indices:"
// 	      << index1 << "and" << index2;
        
    if (listIndex)
      {
	if(*listIndex >= 0 && *listIndex < m_regionSelectionList.size())
	  iter = *listIndex;
	else if (*listIndex >= m_regionSelectionList.size())
	  return 0;
      }
        
    for (; iter < m_regionSelectionList.size(); ++iter)
      {
	RegionSelection *selection = m_regionSelectionList.at(iter);
	
	if(selection->encompassing(index1, index2))
	  {
	    if (listIndex)
	      *listIndex = iter;
	    
// 	    qDebug() << __FILE__ << __LINE__
// 		      << "regionSelectionEncompassing: found selection:"
// 		      << selection->startIndex() << selection->endIndex();

	    return selection;
	  }
      }
    
    return 0;
  }
  
  
  RegionSelection *
  SequenceSelection::regionSelectionEncompassing(const QPointF &point1,
						  const QPointF &point2,
						  int *listIndex)
  {
    // Because we get QPointF data, the secondIndex that will be
    // computed below will be one unit greater than the formal last
    // index of the selection. Thus, we first sort the computed
    // values, so as to know which index is for the end of the
    // selection, and we decrement it by one.

    int firstIndex = mp_view->vignetteIndex(point1);
    int secondIndex = mp_view->vignetteIndex(point2);

//     qDebug() << __FILE__ << __LINE__ 
// 	      << "regionSelectionEncompassing:"
// 	      << point1 << point2 << "giving indices:"
// 	      << firstIndex << "--" << secondIndex;
    
    int temp;
    
    if (firstIndex > secondIndex)
      {
	temp = secondIndex;
	secondIndex = firstIndex;
	firstIndex = temp;
      }
    
    // The line below seems faulty as it makes it impossible to select
    // to contigyous regions because the previous region would always
    // be found overlapping to the new due to the secondIndex - 1
    // below.

//     return regionSelectionEncompassing(firstIndex, secondIndex - 1, 
// 					listIndex);

    return regionSelectionEncompassing(firstIndex, secondIndex, listIndex);
  }
  
  
  double 
  SequenceSelection::manhattanLength()
  {
    return mp_view->requestedVignetteSize() / 0.3;
  }
  
  
  bool 
  SequenceSelection::isManhattanLength(const QPointF &point1,
					const QPointF &point2)
  {
    double manhattan = manhattanLength();
    int requestedVignetteSize = mp_view->requestedVignetteSize();
    
    if (abs(static_cast<int>(point1.x() - point2.x())) < 
	requestedVignetteSize / manhattan
	&&
	abs(static_cast<int>(point1.y() - point2.y())) < 
	requestedVignetteSize  / manhattan)
      {
	return false;
      }
    
    return true;
  }

  
  int 
  SequenceSelection::regionSelectionCount()
  {
    return m_regionSelectionList.size();
  }
  

  bool
  SequenceSelection::selectionIndices(CoordinateList *coordList)
  {
    int count = 0;
    
    // The coordList should be empty. If not we empty it right away.
    
    if (coordList && !coordList->isEmpty())
      coordList->empty();
    
    for (int iter = 0; iter < m_regionSelectionList.size(); ++iter)
      {
	RegionSelection *regionSelection = m_regionSelectionList.at(iter);
	
	// Allocate a Coordinates instance to store the start and end
	// indices of the regionSelection.
	Coordinates *coord = regionSelection->coordinates();
	
	if(coordList)
	  coordList->append(coord);

	++count;
      }
    
    return count;
  }
 
 
  void 
  SequenceSelection::debugSelectionPutStdErr()
  {
    qDebug() << __FILE__ << __LINE__
	      << "SequenceSelection:";
    
    for (int iter = 0; iter < m_regionSelectionList.size(); ++iter)
      {
	RegionSelection *iterRegionSelection = m_regionSelectionList.at(iter);
	iterRegionSelection->debugSelectionIndicesPutStdErr();
      }
  }


} // namespace massXpert