File: cleavageDlg.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 (859 lines) | stat: -rw-r--r-- 22,261 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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/* massXpert - the true massist's program.
   --------------------------------------
   Copyright(C) 2006,2007 Filippo Rusconi

   http://www.massxpert.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
#include <QMessageBox>
#include <QFileDialog>
#include <QDebug>


/////////////////////// Local includes
#include "cleavageDlg.hpp"
#include "application.hpp"


namespace massXpert
{

  CleavageDlg::CleavageDlg(QWidget *parent,
			    Polymer *polymer,
			    const PolChemDef *polChemDef,
			    const CalcOptions &calcOptions,
			    const IonizeRule *ionizeRule)
    : QDialog(parent),
      mp_polymer(polymer), 
      mp_polChemDef(polChemDef), 
      m_calcOptions(calcOptions),
      mp_ionizeRule(ionizeRule)
  {
    Q_ASSERT(parent);
    Q_ASSERT(!mp_polymer.isNull() && mp_polChemDef && mp_ionizeRule);

    m_ui.setupUi(this);
  
    mp_editorWnd = static_cast<SequenceEditorWnd *>(parent);

    populateCleaveSpecListWidget();
    populateSelectedOligomerData();
    
    setupTreeView();
    
    m_ui.leftCapPixmapLabel->setPixmap(QPixmap(":/images/redled.png"));
    m_ui.rightCapPixmapLabel->setPixmap(QPixmap(":/images/redled.png"));

    m_ui.leftModifPixmapLabel->setPixmap(QPixmap(":/images/redled.png"));
    m_ui.rightModifPixmapLabel->setPixmap(QPixmap(":/images/redled.png"));

    m_ui.modifsPixmapLabel->setPixmap(QPixmap(":/images/redled.png"));

    m_ui.crossLinksPixmapLabel->setPixmap(QPixmap(":/images/redled.png"));

    mpa_cleaver = 0;
  
    // The tolerance when filtering mono/avg masses...
    QStringList stringList;
  
    stringList << tr("AMU") << tr("PCT") << tr("PPM");
  
    m_ui.toleranceComboBox->insertItems(0, stringList);
  
    m_ui.toleranceComboBox->setToolTip(tr("AMU: atom mass unit \n"
					    "PCT: percent \n"
					    "PPM: part per million"));

    filterAct = new QAction(tr("Toggle Filtering"), this);
    filterAct->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_F));
    this->addAction(filterAct);
    connect(filterAct, 
	     SIGNAL(triggered()), 
	     this, 
	     SLOT(filterOptionsToggled()));

    m_ui.filteringOptionsGroupBox->addAction(filterAct);
    // When the dialog box is created it is created with the groupbox
    // unchecked.
    m_ui.filteringOptionsFrame->setVisible(false);
  
    // When the filtering group box will be opened, the focus will be on the 
    // first widget of the groupbox:
    mp_focusWidget = m_ui.filterPartialLineEdit;

    // The results-exporting menus. ////////////////////////////////

    QStringList comboBoxItemList;

    comboBoxItemList 
      << tr("To Clipboard") 
      << tr("To File")
      << tr("Select File");
  
    m_ui.exportResultsComboBox->addItems(comboBoxItemList);
  
    connect(m_ui.exportResultsComboBox,
	     SIGNAL(activated(int)),
	     this,
	     SLOT(exportResults(int)));

    mpa_resultsString = new QString();
  
    //////////////////////////////////// The results-exporting menus.
  

    QSettings settings 
     (static_cast<Application *>(qApp)->configSettingsFilePath(), 
       QSettings::IniFormat);
  
    settings.beginGroup("cleavage_dlg");

    restoreGeometry(settings.value("geometry").toByteArray());

    m_ui.oligomersSplitter->
      restoreState(settings.value("oligomersSplitter").toByteArray());
  
    m_ui.oligoDetailsSplitter->
      restoreState(settings.value("oligoDetailsSplitter").toByteArray());
  
    settings.endGroup();


    connect(m_ui.cleavePushButton,
	     SIGNAL(clicked()),
	     this,
	     SLOT(cleave()));

    connect(m_ui.filterPartialLineEdit,
	     SIGNAL(returnPressed()),
	     this,
	     SLOT(filterPartial()));
  
    connect(m_ui.filterMonoMassLineEdit,
	     SIGNAL(returnPressed()),
	     this,
	     SLOT(filterMonoMass()));
  
    connect(m_ui.filterAvgMassLineEdit,
	     SIGNAL(returnPressed()),
	     this,
	     SLOT(filterAvgMass()));
  
    connect(m_ui.filterChargeLineEdit,
	     SIGNAL(returnPressed()),
	     this,
	     SLOT(filterCharge()));

    connect(m_ui.filteringOptionsGroupBox,
	     SIGNAL(clicked(bool)),
	     this,
	     SLOT(filterOptions(bool)));
  }


  CleavageDlg::~CleavageDlg()
  {
    delete mpa_resultsString;
  
    delete mpa_oligomerTreeViewModel;

    delete mpa_proxyModel;
  
    delete mpa_cleaver;
  }


bool
  CleavageDlg::populateSelectedOligomerData() 
  {
    CoordinateList coordList;
    Coordinates *coordinates = 0;

    if (!mp_editorWnd->mpa_editorGraphicsView->selectionIndices(&coordList) )
      {
	// We get the selection indices corresponding to the virtual
	// selection, that is the sequence region encompassing the
	// first residue of the sequence up to the monomer left of
	// point.

	// In this case we want the cleavage to be performed on the
	// whole polymer sequence. Set its coordinates to the list of
	// coordinates.

	coordinates = new Coordinates(0, mp_polymer->size() - 1);

	// Set the newly created coordinates to the coordinates list
	// that we'll feed into the calculation options.

	coordList.setCoordinates(*coordinates);
	
	// We do not need the coordinates object anymore, delete it.
	delete coordinates;
	coordinates = 0;
      }

    if (coordList.size() > 1)
      {
	QMessageBox::information(this, tr("massxpert - Polymer Cleavage"),
				  tr("Cleavage simulations with\n"
				      "multi-region selection are not" 
				      "supported."),
				  QMessageBox::Ok);

	// Set the selection data as if all of the sequence was to be
	// cleaved.
	
	coordinates = new Coordinates(0, mp_polymer->size() - 1);

	// Set the newly created coordinates to the coordinates list
	// that we'll feed into the calculation options.

	coordList.setCoordinates(*coordinates);
	
	// We do not need the coordinates object anymore, delete it.
	delete coordinates;
	coordinates = 0;
      }

    // Now, if the checkbox asking for whole sequence cleavage is
    // checked, then we have to make the cleavage on the whole
    // sequence.
    if (m_ui.wholeSequenceCheckBox->isChecked())
      {
	// The cleavage is to be performed on the whole polymer
	// sequence.

	coordinates = new Coordinates(0, mp_polymer->size() - 1);

	// Set the newly created coordinates to the coordinates list
	// that we'll feed into the calculation options.

	coordList.setCoordinates(*coordinates);
	
	// We do not need the coordinates object anymore, delete it.
	delete coordinates;
	coordinates = 0;
      }
    
    m_calcOptions.setCoordinateList(coordList);
  
    coordinates = coordList.last();
    
    m_ui.oligomerStartLabel->
      setText(QString().setNum(coordinates->start() + 1));
    
    m_ui.oligomerEndLabel->
      setText(QString().setNum(coordinates->end() + 1));


    // We have to count the incomplete cross-links.

    const CrossLinkList &crossLinkList = mp_polymer->crossLinkList();
	  
    int crossLinkPartial = 0;
	  
    for (int iter = 0; iter < crossLinkList.size(); ++iter)
      {
	CrossLink *crossLink = crossLinkList.at(iter);
	  
	int ret = crossLink->encompassedBy(coordList);
	  
	if(ret == MXP_CROSS_LINK_ENCOMPASSED_FULL)
	  {
	    // 		qDebug() << __FILE__ << __LINE__
	    // 			  << "CrossLink at iter:" << iter
	    // 			  << "is fully encompassed";
	  }
	else if (ret == MXP_CROSS_LINK_ENCOMPASSED_PARTIAL)
	  {
	    // 		qDebug() << __FILE__ << __LINE__
	    // 			  << "CrossLink at iter:" << iter
	    // 			  << "is partially encompassed";
	      
	    ++crossLinkPartial;
	  }
	else
	  {
	    // 		qDebug() << __FILE__ << __LINE__
	    // 			  << "CrossLink at iter:" << iter
	    // 			  << "is not encompassed at all";
	  }
      }

    if (crossLinkPartial)
      {
	// Alert the user on the fact that the currently selected
	// region does not encompass all of the cross-linked material.

	QMessageBox::information(this, 
				 tr("massXpert - Polymer Cleavage"),
				 tr("There are incomplete cross-links"),
				 QMessageBox::Ok);
      }

    return true;
  }
  

  void
  CleavageDlg::populateCleaveSpecListWidget()
  {
    PolChemDef *polChemDef = mp_editorWnd->polChemDef();
    Q_ASSERT(polChemDef);
  
    for (int iter = 0; iter < polChemDef->cleaveSpecList().size(); ++iter)
      {
	CleaveSpec *cleaveSpec = polChemDef->cleaveSpecList().at(iter);
	Q_ASSERT(cleaveSpec);
      
	m_ui.cleavageAgentListWidget->addItem(cleaveSpec->name());
      }
  
    return;
  }


  void 
  CleavageDlg::setupTreeView()
  {
     // Model stuff all thought for sorting.
    mpa_oligomerTreeViewModel = 
      new CleaveOligomerTreeViewModel(&m_oligomerList, this);

    mpa_proxyModel = new CleaveOligomerTreeViewSortProxyModel(this);
    mpa_proxyModel->setSourceModel(mpa_oligomerTreeViewModel);
    mpa_proxyModel->setFilterKeyColumn(-1);
  
    m_ui.oligomerTreeView->setModel(mpa_proxyModel);
    m_ui.oligomerTreeView->setParentDlg(this);
    mpa_oligomerTreeViewModel->setTreeView(m_ui.oligomerTreeView);
  }
  

  SequenceEditorWnd *
  CleavageDlg::editorWnd()
  {
    return mp_editorWnd;
  }


  void 
  CleavageDlg::cleave()
  {
    // First update the selection data from the sequence editor window.
    if (!populateSelectedOligomerData())
      return;
    
    // What's the cleavage agent ?
    int value = m_ui.cleavageAgentListWidget->currentRow();

    CleaveSpec *cleaveSpec = mp_polChemDef->cleaveSpecList().at(value);
    
    value = m_ui.partialCleavageSpinBox->value();
  
    CleaveOptions cleaveOptions(*cleaveSpec, value, false);
  
    // Set the ionization levels.
    int valueStart = m_ui.ionizeLevelStartSpinBox->value();
    int valueEnd = m_ui.ionizeLevelEndSpinBox->value();

    cleaveOptions.setIonizeLevels(valueStart, valueEnd);
    
    OligomerList oligomerList;
    
    delete mpa_cleaver;
    mpa_cleaver = 0;
    
    mpa_cleaver= new Cleaver(mp_polymer, mp_polChemDef, cleaveOptions,
			     m_calcOptions, *mp_ionizeRule);

    mpa_cleaver->setOligomerList(&oligomerList);
    
    if (!mpa_cleaver->cleave())
      {
	QMessageBox::critical(this, 
			       tr("massXpert - Polymer Cleavage"),
			       tr("Failed to perform cleavage."),
			       QMessageBox::Ok);
	return;
      }
        
    // We now have new oligomers in oligomerList. We either stack
    // these on top of the previous ones, or we replace the previous
    // ones with the new ones.
    // Are we stacking new oligomer on top of the old ones?

    if (!m_ui.stackOligomersCheckBox->isChecked())
      mpa_oligomerTreeViewModel->removeOligomers();
    
    
    // At this point we can set up the data to the treeview model.
    int initialOligomers = oligomerList.size();
    int addedOligomers = mpa_oligomerTreeViewModel->addOligomers(&oligomerList);
    
    if (initialOligomers != addedOligomers)
      qFatal("Fatal error at %s@%d. Program aborted.",
	      __FILE__, __LINE__);

    if (oligomerList.size())
      qFatal("Fatal error at %s@%d. Program aborted.",
	      __FILE__, __LINE__);
        
    // Logically, the oligomerList should be empty by now.
    Q_ASSERT(oligomerList.isEmpty());
    
    updateCleavageDetails();

    // Set focus to the treeView.
    m_ui.oligomerTreeView->setFocus();
    
    QString title;
    
    int oligomerCount = mpa_oligomerTreeViewModel->rowCount();
    
    Application *application = static_cast<Application *>(qApp);
    QLocale locale = application->locale();

    if (!oligomerCount)
      title = tr("Oligomers(empty list)");
    else if (oligomerCount == 1)
      title = tr("Oligomers(one item)");
    else
      title = tr("Oligomers(%1 items)")
	.arg(locale.toString(oligomerCount));
	
    m_ui.oligomerGroupBox->setTitle(title);
  }


  void
  CleavageDlg::updateCleavageDetails()
  {
    if (m_calcOptions.capping() & MXT_CAP_LEFT)
      m_ui.leftCapPixmapLabel->setPixmap(QPixmap(":/images/greenled.png"));
    else
      m_ui.leftCapPixmapLabel->setPixmap(QPixmap(":/images/redled.png"));

    if (m_calcOptions.capping() & MXT_CAP_RIGHT)
      m_ui.rightCapPixmapLabel->setPixmap(QPixmap(":/images/greenled.png"));
    else
      m_ui.rightCapPixmapLabel->setPixmap(QPixmap(":/images/redled.png"));


    if (m_calcOptions.monomerEntities() & MXT_MONOMER_CHEMENT_MODIF)
      m_ui.modifsPixmapLabel->setPixmap(QPixmap(":/images/greenled.png"));
    else
      m_ui.modifsPixmapLabel->setPixmap(QPixmap(":/images/redled.png"));

    if (m_calcOptions.monomerEntities() & MXT_MONOMER_CHEMENT_CROSS_LINK)
      m_ui.crossLinksPixmapLabel->setPixmap(QPixmap(":/images/greenled.png"));
    else
      m_ui.crossLinksPixmapLabel->setPixmap(QPixmap(":/images/redled.png"));


    if (m_calcOptions.polymerEntities() & MXT_POLYMER_CHEMENT_LEFT_END_MODIF)
      m_ui.leftModifPixmapLabel->setPixmap(QPixmap(":/images/greenled.png"));
    else
      m_ui.leftModifPixmapLabel->setPixmap(QPixmap(":/images/redled.png"));

    if (m_calcOptions.polymerEntities() & MXT_POLYMER_CHEMENT_RIGHT_END_MODIF)
      m_ui.rightModifPixmapLabel->setPixmap(QPixmap(":/images/greenled.png"));
    else
      m_ui.rightModifPixmapLabel->setPixmap(QPixmap(":/images/redled.png"));
  }


  void
  CleavageDlg::updateOligomerSequence(QString *text)
  {
    Q_ASSERT(text);
  
    m_ui.oligomerSequenceTextEdit->clear();
    m_ui.oligomerSequenceTextEdit->append(*text);
  }


  void 
  CleavageDlg::closeEvent(QCloseEvent *event)
  {
    if (event)
      printf("%s", "");
  
    QSettings settings 
     (static_cast<Application *>(qApp)->configSettingsFilePath(), 
       QSettings::IniFormat);
  
    settings.beginGroup("cleavage_dlg");

    settings.setValue("geometry", saveGeometry());

    settings.setValue("oligomersSplitter", 
		       m_ui.oligomersSplitter->saveState());

    settings.setValue("oligoDetailsSplitter", 
		       m_ui.oligoDetailsSplitter->saveState());

    settings.endGroup();
  }



  bool
  CleavageDlg::calculateTolerance(double mass)
  {
    // Get the tolerance that is in its lineEdit.
  
    QString text = m_ui.toleranceLineEdit->text();
    double tolerance = 0;
    bool ok = false;
  
    if (!text.isEmpty())
      {
	// Convert the string to a double.
	Application *application = static_cast<Application *>(qApp);
	QLocale locale = application->locale();

	ok = false;
	tolerance = locale.toDouble(text, &ok);
      
	if(!tolerance && !ok)
	  return false;
      }
    else
      {
	m_tolerance = 0;
      }
    
    // What's the item currently selected in the comboBox?
    int index = m_ui.toleranceComboBox->currentIndex();
  
    if (index == 0)
      {
	// MXT_MASS_TOLERANCE_AMU
	m_tolerance = tolerance;
      }
    else if (index == 1)
      {
	// MXT_MASS_TOLERANCE_PCT
	m_tolerance =(tolerance / 100) * mass;
      }
    else if (index == 2)
      {
	// MXT_MASS_TOLERANCE_PPM
	m_tolerance =(tolerance / 1000000) * mass;
      }
    else
      Q_ASSERT(0);
  
    return true;
  }


  void 
  CleavageDlg::filterOptions(bool checked)
  {
    if (!checked)
      {
	mpa_proxyModel->setFilterKeyColumn(-1);
      
	mpa_proxyModel->applyNewFilter();

	m_ui.filteringOptionsFrame->setVisible(false);
      }
    else
      {
	m_ui.filteringOptionsFrame->setVisible(true);

	// In this case, set focus to the last focused widget in the
	// groupbox or the first widget in the groubox if this is the
	// first time the filtering is used.
	mp_focusWidget->setFocus();
      }
  }


  void 
  CleavageDlg::filterOptionsToggled()
  {
    bool isChecked = m_ui.filteringOptionsGroupBox->isChecked();
  
    m_ui.filteringOptionsGroupBox->setChecked(!isChecked);
    filterOptions(!isChecked);
  }


  void 
  CleavageDlg::filterPartial()
  {
    // First off, we have to get the partial that is in the lineEdit.
  
    QString text = m_ui.filterPartialLineEdit->text();

    if (text.isEmpty())
      return;
  
    // Convert the string to a int.
    Application *application = static_cast<Application *>(qApp);
    QLocale locale = application->locale();

    bool ok = false;
    int partial = locale.toInt(text, &ok);
  
    if (!partial && !ok)
      return;

    mpa_proxyModel->setPartialFilter(partial);
    mpa_proxyModel->setFilterKeyColumn(0);
    mpa_proxyModel->applyNewFilter();

    mp_focusWidget = m_ui.filterPartialLineEdit;
  }




  void 
  CleavageDlg::filterMonoMass()
  {
    // First off, we have to get the mass that is in the lineEdit.

    QString text = m_ui.filterMonoMassLineEdit->text();
  
    if (text.isEmpty())
      return;
  
    // Convert the string to a double.
    Application *application = static_cast<Application *>(qApp);
    QLocale locale = application->locale();

    bool ok = false;
    double mass = locale.toDouble(text, &ok);
  
    if (!mass && !ok)
      return;

    // At this point, depending on the item that is currently selected
    // in the comboBox, we'll have to actually compute the tolerance.

    if (!calculateTolerance(mass))
      return ;

    mpa_proxyModel->setMonoFilter(mass);
    mpa_proxyModel->setTolerance(m_tolerance);
  
    mpa_proxyModel->setFilterKeyColumn(3);
    mpa_proxyModel->applyNewFilter();

    mp_focusWidget = m_ui.filterMonoMassLineEdit;
  }


  void 
  CleavageDlg::filterAvgMass()
  {
    // First off, we have to get the mass that is in the lineEdit.

    QString text = m_ui.filterAvgMassLineEdit->text();
  
    if (text.isEmpty())
      return;
  
    // Convert the string to a double.
    Application *application = static_cast<Application *>(qApp);
    QLocale locale = application->locale();

    bool ok = false;
    double mass = locale.toDouble(text, &ok);
  
    if (!mass && !ok)
      return;

    // At this point, depending on the item that is currently selected
    // in the comboBox, we'll have to actually compute the tolerance.

    if (!calculateTolerance(mass))
      return ;

    mpa_proxyModel->setAvgFilter(mass);
    mpa_proxyModel->setTolerance(m_tolerance);
  
    mpa_proxyModel->setFilterKeyColumn(4);
    mpa_proxyModel->applyNewFilter();

    mp_focusWidget = m_ui.filterAvgMassLineEdit;
  }


  void 
  CleavageDlg::filterCharge()
  {
    // First off, we have to get the charge that is in the lineEdit.

    QString text = m_ui.filterChargeLineEdit->text();

    if (text.isEmpty())
      return;
  
    // Convert the string to a int.
    Application *application = static_cast<Application *>(qApp);
    QLocale locale = application->locale();

    bool ok = false;
    int charge = locale.toInt(text, &ok);

    if (!charge && !ok)
      return;

    mpa_proxyModel->setChargeFilter(charge);
    mpa_proxyModel->setFilterKeyColumn(5);
    mpa_proxyModel->applyNewFilter();

    mp_focusWidget = m_ui.filterChargeLineEdit;
  }



  // The results-exporting functions. ////////////////////////////////
  // The results-exporting functions. ////////////////////////////////
  // The results-exporting functions. ////////////////////////////////
  void
  CleavageDlg::exportResults(int index)
  {
    // Remember that we had set up the combobox with the following strings:
    // << tr("To Clipboard") 
    // << tr("To File")
    // << tr("Select File");

    if (index == 0)
      {
	exportResultsClipboard();
      }
    else if (index == 1)
      {
	exportResultsFile();
      }
    else if (index == 2)
      {
	selectResultsFile();
      }
    else 
      Q_ASSERT(0);
  
  }


  void
  CleavageDlg::prepareResultsTxtString()
  {
    mpa_resultsString->clear();
  
    *mpa_resultsString += QObject::tr("\n---------------------------\n"
				       "Polymer sequence cleavage: \n"
				       "---------------------------\n");
  
    bool withSequence = m_ui.withSequenceCheckBox->isChecked();
    
    QString *text = 
      m_ui.oligomerTreeView->selectedOligomersAsPlainText(withSequence);

    *mpa_resultsString += *text;
    
    delete text;
  }


  bool 
  CleavageDlg::exportResultsClipboard()
  {
    prepareResultsTxtString();
  
    QClipboard *clipboard = QApplication::clipboard();

    clipboard->setText(*mpa_resultsString, QClipboard::Clipboard);
  
    return true;
  }


  bool 
  CleavageDlg::exportResultsFile()
  {
    if (m_resultsFilePath.isEmpty())
      {
	if(!selectResultsFile())
	  return false;
      }
  
    QFile file(m_resultsFilePath);
  
    if (!file.open(QIODevice::WriteOnly | QIODevice::Append))
      {
	QMessageBox::information(this, 
				  tr("massXpert - Export Data"),
				  tr("Failed to open file in append mode."),
				  QMessageBox::Ok);
	return false;
      }
  
    QTextStream stream(&file);
    stream.setCodec("UTF-8");

    prepareResultsTxtString();
  
    stream << *mpa_resultsString;
  
    file.close();

    return true;
  }


  bool 
  CleavageDlg::selectResultsFile()
  {
    m_resultsFilePath = 
      QFileDialog::getSaveFileName(this, tr("Select file to export data to"),
				    QDir::homePath(),
				    tr("Data files(*.dat *.DAT)"));
  
    if (m_resultsFilePath.isEmpty())
      return false;

    return true;
  }
  //////////////////////////////////// The results-exporting functions.
  //////////////////////////////////// The results-exporting functions.
  //////////////////////////////////// The results-exporting functions.

} // namespace massXpert