File: regionSelection.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 (542 lines) | stat: -rw-r--r-- 13,340 bytes parent folder | download
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
533
534
535
536
537
538
539
540
541
542
/* 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 "regionSelection.hpp"
#include "sequenceEditorGraphicsView.hpp"
#include "monomer.hpp"


namespace massXpert
{


  RegionSelection::RegionSelection(SequenceEditorGraphicsView *view)
    : mp_view(view), m_rectangleCount(3)
  {
    Q_ASSERT(mp_view);
    
    //    m_rectangleCount = 3;
    
    m_startIndex = -1;
    m_endIndex = -1;
  }
  
  
  RegionSelection::~RegionSelection()
  {
    eraseMark();
  }
  

  SequenceEditorGraphicsView * 
  RegionSelection::view()
  {
    return mp_view;
  }
  

  void 
  RegionSelection::setCoordinates(int index1, int index2)
  {
    if (index1 <= index2)
      {
	m_startIndex = index1;
	m_endIndex = index2;
      }
    else
      {
	m_startIndex = index2;
	m_endIndex = index1;
      }

    // We have to increment m_endPoint by one unit, because the index
    // passed as argument is the "real" index, while we need a
    // position to compute the m_endPoint.

    m_startPoint = mp_view->vignetteLocation(m_startIndex, MXT_NORTH_WEST);
    m_endPoint = mp_view->vignetteLocation(m_endIndex + 1, MXT_SOUTH_WEST);

    mp_startMonomer = mp_view->polymer()->at(m_startIndex);
    mp_endMonomer = mp_view->polymer()->at(m_endIndex);
  }
  
  
  void 
  RegionSelection::setCoordinates(const QPointF &point1, const QPointF &point2)
  {
    // Convert the point data into monomer indices and then back to
    // points that will be used to compute precise point coordinates
    // to actually draw the mark when asked.

    int firstIndex = mp_view->vignetteIndex(point1);
    int secondIndex = mp_view->vignetteIndex(point2);
    
 //     qDebug() << __FILE__ << __LINE__
//  	      << "firstIndex:" << firstIndex
//  	      << "secondIndex:" << secondIndex;

    // We want the indices to be in the increasing order and the
    // points also.
    
    if (firstIndex <= secondIndex)
      {
	m_startIndex = firstIndex;
	m_endIndex = secondIndex;
      }
    else
      {
	m_startIndex = secondIndex;
	m_endIndex = firstIndex;
      }

//     qDebug() << __FILE__ << __LINE__
// 	      << "m_startIndex:" << m_startIndex
// 	      << "m_endIndex:" << m_endIndex;
    
    // If m_startIndex is less than 0, then set it to 0.
    if(m_startIndex < 0)
      m_startIndex = 0;
    
    m_startPoint = mp_view->vignetteLocation(m_startIndex, MXT_NORTH_WEST);
    m_endPoint = mp_view->vignetteLocation(m_endIndex, MXT_SOUTH_WEST);

    // Because this region selection might be used for other scopes
    // than simple drawing of the selection on the sequence editor we
    // have to provide for the real end selection index, that is
    // m_endIndex - 1. However, we only do this decrement if endIndex
    // is at least 1, otherwise we'd have a negative m_endIndex, and
    // this makes the call mp_endMonomer = mp_view->polymer()->at
    //(m_endIndex); crash the program.
  
    if (m_endIndex)
      --m_endIndex;


    // Now, if the sequence selection is performed from right of the
    // last monomer in the sequence, m_startIndex would be >=
    // mp_view->polymer()->size(), and this would make the call to
    // mp_view->polymer()->at(m_startIndex); crash the program. So
    // before willing to identificate which MxpMonomer * is the first
    // monomer in the selection, that is the last monomer of the
    // sequence, we should make the following check. Drawing of the
    // selection will be ok, because all the related work has been
    // done already.

    if (m_startIndex >= mp_view->polymer()->size())
      {
	if(m_startIndex)
	  --m_startIndex;
      }
    
    // For the same reason above, the call to at(m_endIndex) has to
    // be performed after the --m_endIndex; instruction above.

//      qDebug() << __FILE__ << __LINE__
//  	      << "m_startIndex:" << m_startIndex;

    mp_startMonomer = mp_view->polymer()->at(m_startIndex);

//      qDebug() << __FILE__ << __LINE__
//  	      << "m_endIndex:" << m_endIndex;
    
    mp_endMonomer = mp_view->polymer()->at(m_endIndex);
  }
  

  const Monomer *
  RegionSelection::startMonomer() const
  {
    return mp_startMonomer;
  }
  
  
  const Monomer * 
  RegionSelection::endMonomer() const
  {
    return mp_endMonomer;
  }
  

  int
  RegionSelection::startIndex() const
  {
    return m_startIndex;
  }
  
  
  int 
  RegionSelection::endIndex() const
  {
    return m_endIndex;
  }
  

  int
  RegionSelection::redrawMark()
  {
    eraseMark();

    // Force the recalculation of the [start--end] QPointF's with the
    // unchanged indices. This function is typically called when the
    // sequence editor window is resized or when the size of the
    // vignettes is changed... Thus, the indices are not changed, what
    // change are the positions of the vignettes as described by the
    // QPointF member data.

    setCoordinates(m_startIndex, m_endIndex);
    
    return drawMark();
  }
  

  int  
  RegionSelection::drawMark(const QPointF &point1, const QPointF &point2)
  {
    setCoordinates(point1, point2);
    
    return drawMark();
  }
    

  int  
  RegionSelection::drawMark(int index1, int index2)
  {
    setCoordinates(index1, index2);
    
    return drawMark();
  }


  // Returns the number of rectangle graphics items created.
  int  
  RegionSelection::drawMark()
  {
    QColor color(100, 100, 100, 127);
    QBrush brush(color, Qt::SolidPattern);
    QPen pen(Qt::NoPen);
    
    int requestedVignetteSize = mp_view->requestedVignetteSize();
    int leftMargin = mp_view->leftMargin();
    int columns = mp_view->columns();
    int width = 0;
    int height = 0;
    
    int selectedRowCount =
      static_cast<int>((m_endPoint.y() - m_startPoint.y()) / 
			requestedVignetteSize);
    
    if (selectedRowCount == 1)
      {
	// The two vignettes are located on the same line, this is the
	// easiest situation, we just draw a single rectangle to cover
	// the region.

	QRectF *rect = new QRectF();
	m_rectangleList.append(rect);
	
	QGraphicsRectItem *mark = new QGraphicsRectItem(*(rect));
	
	mark->setRect(m_startPoint.x(), m_startPoint.y(),
		       m_endPoint.x() - m_startPoint.x(),
		       requestedVignetteSize);

	mark->setPos(0, 0);
	mark->setZValue(10);

	mark->setBrush(brush);
	mark->setPen(pen);

	mp_view->scene()->addItem(mark);
		
	m_markList.append(mark);

	return m_markList.size();
      }
    else if (selectedRowCount == 2)
      {
	// Can we deal with the selection with a single two-row
	// rectangle?

	if(m_startPoint.x() ==  leftMargin 
	    &&
	    m_endPoint.x() == leftMargin + 
	    columns * requestedVignetteSize)
	  {
	    // This is like having two full rows selected : a single
	    // scene()-wide rectangle over two rows.
	  
	    width = leftMargin + columns * requestedVignetteSize;
	  
	    QRectF *rect = new QRectF();
	    m_rectangleList.append(rect);
	
	    QGraphicsRectItem *mark = new QGraphicsRectItem(*(rect));
	
	    mark->setRect(m_startPoint.x(), m_startPoint.y(),
			   width, selectedRowCount * requestedVignetteSize);
	    mark->setPos(0, 0);
	    mark->setZValue(10);
	
	    mark->setBrush(brush);
	    mark->setPen(pen);
	
	    mp_view->scene()->addItem(mark);
		
	    m_markList.append(mark);

	    return m_markList.size();
	  }
	
	// We have to draw two rectangles.
      
	// First row.

	width = static_cast<int>(leftMargin + columns * requestedVignetteSize -
				  m_startPoint.x());
      
	QRectF *rect = new QRectF();
	m_rectangleList.append(rect);
	
	QGraphicsRectItem *mark = new QGraphicsRectItem(*(rect));
	
	mark->setRect(m_startPoint.x(), m_startPoint.y(),
		       width, requestedVignetteSize);
	mark->setPos(0, 0);
	mark->setZValue(10);
	    
	mark->setBrush(brush);
	mark->setPen(pen);
	    
	mp_view->scene()->addItem(mark);
		
	m_markList.append(mark);
	  
	// Second row.

	width = static_cast<int>(m_endPoint.x() - leftMargin);
      
	rect = new QRectF();
	m_rectangleList.append(rect);
	
	mark = new QGraphicsRectItem(*(rect));
	
	mark->setRect(leftMargin, m_endPoint.y() - requestedVignetteSize,
		       width, requestedVignetteSize);

	mark->setPos(0, 0);
	mark->setZValue(10);
	    
	mark->setBrush(brush);
	mark->setPen(pen);
	    
	mp_view->scene()->addItem(mark);
		
	m_markList.append(mark);
      }
    else if (selectedRowCount > 2)
      {
	// At this point we have to draw three distinct rectangles as
	// the first point of first row does not begin at left of the
	// row and the last point of last line does not end at end of
	// row. There is room for optimization here. I know it.

	// First row

	width = 
	  static_cast<int>(leftMargin +
			   columns * requestedVignetteSize - m_startPoint.x());
	height = requestedVignetteSize;
      
	QRectF *rect = new QRectF();
	m_rectangleList.append(rect);
	
	QGraphicsRectItem *mark = new QGraphicsRectItem(*(rect));
	
	mark->setRect(m_startPoint.x(), m_startPoint.y(),
		       width, height);
	mark->setPos(0, 0);
	mark->setZValue(10);
	    
	mark->setBrush(brush);
	mark->setPen(pen);
	    
	mp_view->scene()->addItem(mark);
		
	m_markList.append(mark);
      
	// Middle full-width region
      
	width = columns * requestedVignetteSize;
	height = static_cast<int>(m_endPoint.y() - m_startPoint.y() -
				  2* requestedVignetteSize);
	
	rect = new QRectF();
	m_rectangleList.append(rect);
	
	mark = new QGraphicsRectItem(*(rect));
	
	mark->setRect(leftMargin, m_startPoint.y() + requestedVignetteSize,
		       width, height);

	mark->setPos(0, 0);
	mark->setZValue(10);
	    
	mark->setBrush(brush);
	mark->setPen(pen);
	    
	mp_view->scene()->addItem(mark);
		
	m_markList.append(mark);
	  
	// Last row

	width = static_cast<int>(m_endPoint.x() - leftMargin);
	height = requestedVignetteSize;
      
	rect = new QRectF();
	m_rectangleList.append(rect);
	
	mark = new QGraphicsRectItem(*(rect));
	
	mark->setRect(leftMargin,
		       m_endPoint.y() - requestedVignetteSize,
		       width, requestedVignetteSize);
	
	mark->setPos(0, 0);
	mark->setZValue(10);
	    
	mark->setBrush(brush);
	mark->setPen(pen);
	    
	mp_view->scene()->addItem(mark);
		
	m_markList.append(mark);
      }

    return m_markList.size();
  }  
  

  int  
  RegionSelection::eraseMark()
  {
    while(!m_rectangleList.isEmpty())
      delete m_rectangleList.takeFirst();

    // Be careful, the scene might have been deleted! Remember, the
    // scene get deleted before the view!  No problem, because the
    // items that were set to the scene do belong to the scene and
    // thus are freed along with the scene.
    
    QGraphicsScene *scene = mp_view->scene();

    if (!scene)
      return 1;
    
    while(!m_markList.isEmpty())
      {
	QGraphicsRectItem *mark = m_markList.takeFirst();
		
	scene->removeItem(mark);
	
	delete mark;
      }
        
    return 1;
  }

  
  bool
  RegionSelection::encompassing(int index1, int index2)
  {
    // We should be able to tell if *this region encompasses the
    // region contained in [index1--index2]

    if (index1 == -1 && index2 == -1)
      return false;
    
    if (index2 == -1)
      {
	if(index1 >= m_startIndex && index1 <= m_endIndex)
	  return true;
	else
	  return false;
      }
    
    int temp;
    
    if (index1 > index2)
      {
	temp = index2;
	index2 = index1;
	index1 = temp;
      }
    
    // The tested region([index1--index2] must start before the end
    // of *this region and must end before the start of *this region.

    if (index1 <= m_endIndex && index2 >= m_startIndex)
      return true;
      
    return false;
  }


  Coordinates *
  RegionSelection::coordinates()
  {
    Coordinates *coordinates = new Coordinates(m_startIndex, m_endIndex);
    return coordinates;
  }
  
  
  void 
  RegionSelection::debugSelectionIndicesPutStdErr()
  {
    qDebug() << __FILE__ << __LINE__
	      << "RegionSelection: ["
	      << m_startIndex << "--" << m_endIndex 
	      << "]";
  }
  

} // namespace massXpert