File: fragSpecDefDlg.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 (1041 lines) | stat: -rw-r--r-- 29,763 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
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
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
/* 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
#include <QMessageBox>


/////////////////////// Local includes
#include "application.hpp"
#include "fragSpecDefDlg.hpp"
#include "polChemDef.hpp"
#include "polChemDefWnd.hpp"


namespace massXpert
{

  FragSpecDefDlg::FragSpecDefDlg(PolChemDef *polChemDef, 
				  PolChemDefWnd *polChemDefWnd)
  {
    Q_ASSERT(polChemDef);
    mp_polChemDef = polChemDef;
    mp_list = polChemDef->fragSpecListPtr();
  
    Q_ASSERT(polChemDefWnd);
    mp_polChemDefWnd = polChemDefWnd;
  
    if (!initialize())
      {
	qDebug() << "Failed to initialize the fragmentation definition window";
      }
  }


  void 
  FragSpecDefDlg::closeEvent(QCloseEvent *event)
  {  
    // No real close, because we did not ask that
    // close==destruction. Thus we only hide the dialog remembering its
    // position and size.
  
  
    mp_polChemDefWnd->m_ui.fragmentationPushButton->setChecked(false);

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

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

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

    settings.endGroup();
  }

  

  FragSpecDefDlg::~FragSpecDefDlg()
  {
  }

  
  bool
  FragSpecDefDlg::initialize()
  {
    m_ui.setupUi(this);

    // Set all the fragSpecs to the list widget.

    for (int iter = 0; iter < mp_list->size(); ++iter)
      {
	FragSpec *fragSpec = mp_list->at(iter);
      
	m_ui.fragSpecListWidget->addItem(fragSpec->name());
      }

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

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

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

    // The combobox listing the available ends.

    QStringList endList;
    endList << "N/A" << "NE" << "LE" << "RE";
  
    m_ui.fragEndComboBox->addItems(endList);
  

    // Make the connections.

    connect(m_ui.addFragSpecPushButton, 
	     SIGNAL(clicked()),
	     this, 
	     SLOT(addFragSpecPushButtonClicked()));
  
    connect(m_ui.removeFragSpecPushButton, 
	     SIGNAL(clicked()),
	     this, 
	     SLOT(removeFragSpecPushButtonClicked()));
  
    connect(m_ui.moveUpFragSpecPushButton, 
	     SIGNAL(clicked()),
	     this, 
	     SLOT(moveUpFragSpecPushButtonClicked()));
  
    connect(m_ui.moveDownFragSpecPushButton, 
	     SIGNAL(clicked()),
	     this, 
	     SLOT(moveDownFragSpecPushButtonClicked()));
  
    connect(m_ui.addFragRulePushButton, 
	     SIGNAL(clicked()),
	     this, 
	     SLOT(addFragRulePushButtonClicked()));
  
    connect(m_ui.removeFragRulePushButton, 
	     SIGNAL(clicked()),
	     this, 
	     SLOT(removeFragRulePushButtonClicked()));
  
    connect(m_ui.moveUpFragRulePushButton, 
	     SIGNAL(clicked()),
	     this, 
	     SLOT(moveUpFragRulePushButtonClicked()));
  
    connect(m_ui.moveDownFragRulePushButton, 
	     SIGNAL(clicked()),
	     this, 
	     SLOT(moveDownFragRulePushButtonClicked()));
  
    connect(m_ui.applyFragSpecPushButton, 
	     SIGNAL(clicked()),
	     this, 
	     SLOT(applyFragSpecPushButtonClicked()));
  
    connect(m_ui.applyFragRulePushButton, 
	     SIGNAL(clicked()),
	     this, 
	     SLOT(applyFragRulePushButtonClicked()));
  
    connect(m_ui.validatePushButton, 
	     SIGNAL(clicked()),
	     this, 
	     SLOT(validatePushButtonClicked()));

    connect(m_ui.fragSpecListWidget, 
	     SIGNAL(itemSelectionChanged()),
	     this, 
	     SLOT(fragSpecListWidgetItemSelectionChanged()));
  
    connect(m_ui.fragRuleListWidget, 
	     SIGNAL(itemSelectionChanged()),
	     this, 
	     SLOT(fragRuleListWidgetItemSelectionChanged()));
  
    return true;
  }


  void 
  FragSpecDefDlg::addFragSpecPushButtonClicked()
  {
    // We are asked to add a new fragSpec. We'll add it right after the
    // current item.
  
    // Returns -1 if the list is empty.
    int index = m_ui.fragSpecListWidget->currentRow();
  
    FragSpec *newFragSpec = new FragSpec(mp_polChemDef,
					  tr("Type Spec Name"),
					  tr("Type Spec Formula"),
					  MXT_FRAG_END_NONE);
  
    mp_list->insert(index, newFragSpec);
    m_ui.fragSpecListWidget->insertItem(index, newFragSpec->name());

    setModified();
  
    // Needed so that the setCurrentRow() call below actually set the
    // current row!
    if (index <= 0)
      index = 0;
  
    m_ui.fragSpecListWidget->setCurrentRow(index);

    // Erase fragRule data that might be left over by precedent current
    // fragSpec.
    updateFragRuleDetails(0);

    // Set the focus to the lineEdit that holds the name of the fragSpec.
    m_ui.fragSpecNameLineEdit->setFocus();
    m_ui.fragSpecNameLineEdit->selectAll();
  }


  void 
  FragSpecDefDlg::removeFragSpecPushButtonClicked()
  {
    QList<QListWidgetItem *> selectedList = 
      m_ui.fragSpecListWidget->selectedItems();
  
    if (selectedList.size() != 1)
      return;

    // Get the index of the current fragSpec.
    int index = m_ui.fragSpecListWidget->currentRow();
  
    QListWidgetItem *item = m_ui.fragSpecListWidget->takeItem(index);
    delete item;

    FragSpec *fragSpec = mp_list->takeAt(index);
    Q_ASSERT(fragSpec);
    delete fragSpec;
  
    setModified();
  
    // If there are remaining items, we want to set the next item the
    // currentItem. If not, then, the currentItem should be the one
    // preceding the fragSpec that we removed.

    if (m_ui.fragSpecListWidget->count() >= index + 1)
      {
	m_ui.fragSpecListWidget->setCurrentRow(index);
	fragSpecListWidgetItemSelectionChanged();
      }
  
    // If there are no more items in the fragSpec list, remove all the items
    // from the fragRuleList.
  
    if (!m_ui.fragSpecListWidget->count())
      {
	m_ui.fragRuleListWidget->clear();
	clearAllDetails();
      }
  }


  void 
  FragSpecDefDlg::moveUpFragSpecPushButtonClicked()
  {
    // Move the current row to one index less.

    // If no fragSpec is selected, just return.

    QList<QListWidgetItem *> selectedList = 
      m_ui.fragSpecListWidget->selectedItems();
  
    if (selectedList.size() != 1)
      return;

    // Get the index of the fragSpec and the fragSpec itself. 
    int index = m_ui.fragSpecListWidget->currentRow();

    // If the item is already at top of list, do nothing.
    if (!index)
      return;

    mp_list->move(index, index - 1);

    QListWidgetItem *item = m_ui.fragSpecListWidget->takeItem(index);

    m_ui.fragSpecListWidget->insertItem(index - 1, item);
    m_ui.fragSpecListWidget->setCurrentRow(index - 1);
    fragSpecListWidgetItemSelectionChanged();

    setModified();
  }


  void 
  FragSpecDefDlg::moveDownFragSpecPushButtonClicked()
  {
    // Move the current row to one index less.

    // If no fragSpec is selected, just return.

    QList<QListWidgetItem *> selectedList = 
      m_ui.fragSpecListWidget->selectedItems();
  
    if (selectedList.size() != 1)
      return;

    // Get the index of the fragSpec and the fragSpec itself. 
    int index = m_ui.fragSpecListWidget->currentRow();

    // If the item is already at bottom of list, do nothing.
    if (index == m_ui.fragSpecListWidget->count() - 1)
      return;

    mp_list->move(index, index + 1);

    QListWidgetItem *item = m_ui.fragSpecListWidget->takeItem(index);
    m_ui.fragSpecListWidget->insertItem(index + 1, item);
    m_ui.fragSpecListWidget->setCurrentRow(index + 1);
    fragSpecListWidgetItemSelectionChanged();

    setModified();
  }


  void 
  FragSpecDefDlg::addFragRulePushButtonClicked()
  {
    // We are asked to add a new fragRule. We'll add it right after the
    // current item. Note however, that one fragSpec has to be selected.
  
    QList<QListWidgetItem *> selectedList = 
      m_ui.fragSpecListWidget->selectedItems();
  
    if (selectedList.size() != 1)
      {
	QMessageBox::information(this, 
				  tr("massXpert - Fragmentation definition"),
				  tr("Please, select a fragmentation first."),
				  QMessageBox::Ok);
	return;
      }
  
    // Get the index of the current fragSpec so that we know to which fragSpec
    // we'll add the fragRule.
    int index = m_ui.fragSpecListWidget->currentRow();
  
    // What's the actual fragSpec?
    FragSpec *fragSpec = mp_list->at(index);
    Q_ASSERT(fragSpec);

    // Allocate the new fragRule.
    FragRule *newFragRule = new FragRule(mp_polChemDef,
					  tr("Type Rule Name"));
  
    // Get the row index of the current fragRule item. Returns -1 if the
    // list is empty.
    index = m_ui.fragRuleListWidget->currentRow();
  
    m_ui.fragRuleListWidget->insertItem(index, 
					 newFragRule->name());

    // Needed so that the setCurrentRow() call below actually set the
    // current row!
    if (index <= 0)
      index = 0;
  
    fragSpec->ruleList().insert(index, newFragRule);
  
    m_ui.fragRuleListWidget->setCurrentRow(index);

    setModified();

    // Set the focus to the lineEdit that holds the mass of the fragRule.
    m_ui.fragRuleNameLineEdit->setFocus();
    m_ui.fragRuleNameLineEdit->selectAll();
  }


  void 
  FragSpecDefDlg::removeFragRulePushButtonClicked()
  {
    QList<QListWidgetItem *> selectedList = 
      m_ui.fragRuleListWidget->selectedItems();
  
    if (selectedList.size() != 1)
      return;

  
    // Get the index of the current fragSpec so that we know from
    // which fragSpec we'll remove the fragRule.
    int index = m_ui.fragSpecListWidget->currentRow();
  
    FragSpec *fragSpec = mp_list->at(index);
    Q_ASSERT(fragSpec);

    // Get the index of the current fragRule.
    index = m_ui.fragRuleListWidget->currentRow();

    // First remove the item from the listwidget because that will have
    // fragRuleListWidgetItemSelectionChanged() triggered and we have to
    // have the item in the fragRule list in the fragSpec! Otherwise a crash
    // occurs.
    QListWidgetItem *item = m_ui.fragRuleListWidget->takeItem(index);
    delete item;

    // Remove the fragRule from the fragSpec proper.
  
    QList<FragRule *> fragRuleList = fragSpec->ruleList();
  
    FragRule *fragRule = fragRuleList.at(index);
  
    fragSpec->ruleList().removeAt(index);
    delete fragRule;
   
    // If there are remaining items, we want to set the next item the
    // currentItem. If not, then, the currentItem should be the one
    // preceding the fragSpec that we removed.

    if (m_ui.fragRuleListWidget->count() >= index + 1)
      {
	m_ui.fragRuleListWidget->setCurrentRow(index);
	fragRuleListWidgetItemSelectionChanged();
      }
  
    // If there are no more items in the fragRule list, remove all the
    // details.
  
    if (!m_ui.fragRuleListWidget->count())
      {
	updateFragRuleDetails(0);
      }
    else
      {
      }

    setModified();
  }
 

  void 
  FragSpecDefDlg::moveUpFragRulePushButtonClicked()
  {
    // Move the current row to one index less.

    // If no fragRule is selected, just return.

    QList<QListWidgetItem *> selectedList = 
      m_ui.fragRuleListWidget->selectedItems();
  
    if (selectedList.size() != 1)
      return;

    // Get the fragSpec to which the fragRule belongs. 
    int index = m_ui.fragSpecListWidget->currentRow();
    FragSpec *fragSpec = mp_list->at(index);

    // Get the index of the current fragRule item.
    index = m_ui.fragRuleListWidget->currentRow();
  
    // If the item is already at top of list, do nothing.
    if (!index)
      return;
  
    // Get the fragRule itself from the fragSpec.
    FragRule *fragRule = fragSpec->ruleList().at(index);
  
    fragSpec->ruleList().removeAt(index);
    fragSpec->ruleList().insert(index - 1, fragRule);

    QListWidgetItem *item = m_ui.fragRuleListWidget->takeItem(index);
    m_ui.fragRuleListWidget->insertItem(index - 1, item);
    m_ui.fragRuleListWidget->setCurrentRow(index - 1);
    fragRuleListWidgetItemSelectionChanged();

    setModified();
  }


  void 
  FragSpecDefDlg::moveDownFragRulePushButtonClicked()
  {
    // Move the current row to one index less.

    // If no fragRule is selected, just return.

    QList<QListWidgetItem *> selectedList = 
      m_ui.fragRuleListWidget->selectedItems();
  
    if (selectedList.size() != 1)
      return;

    // Get the fragSpec to which the fragRule belongs. 
    int index = m_ui.fragSpecListWidget->currentRow();
    FragSpec *fragSpec = mp_list->at(index);

    // Get the index of the current fragRule item.
    index = m_ui.fragRuleListWidget->currentRow();
  
    // If the item is already at top of list, do nothing.
    if (index == m_ui.fragRuleListWidget->count() - 1)
      return;
  
    // Get the fragRule itself from the fragSpec.
    FragRule *fragRule = fragSpec->ruleList().at(index);

    fragSpec->ruleList().removeAt(index);
    fragSpec->ruleList().insert(index + 1, fragRule);
  
    QListWidgetItem *item = m_ui.fragRuleListWidget->takeItem(index);
    m_ui.fragRuleListWidget->insertItem(index + 1, item);
    m_ui.fragRuleListWidget->setCurrentRow(index + 1);
    fragRuleListWidgetItemSelectionChanged();

    setModified();
  }


  void 
  FragSpecDefDlg::applyFragSpecPushButtonClicked()
  {
    // We are asked to apply the data for the fragSpec.

    // If no fragSpec is selected, just return.

    QList<QListWidgetItem *> selectedList = 
      m_ui.fragSpecListWidget->selectedItems();
  
    if (selectedList.size() != 1)
      return;

    // Get the index of the current fragSpec item.
    int index = m_ui.fragSpecListWidget->currentRow();

    FragSpec *fragSpec = mp_list->at(index);
  
    // We do not want more than one fragSpec by the same name.

    QString editName = m_ui.fragSpecNameLineEdit->text();
    QString editFormula = m_ui.fragSpecFormulaLineEdit->text();

    // If a fragSpec is found in the list with the same name, and that
    // fragSpec is not the one that is current in the fragSpec list,
    // then we are making a double entry, which is not allowed.

    int nameRes = FragSpec::isNameInList(editName, *mp_list);
    if (nameRes != -1 && nameRes != index)
      {
	QMessageBox::warning(this, 
			      tr("massXpert - Fragmentation definition"),
			      tr("A fragmentation with same name "
				  "exists already."),
			      QMessageBox::Ok);
	return;
      }

    QString fragEndString = m_ui.fragEndComboBox->currentText();
    MxtFragEnd fragEnd;
  
    if (fragEndString == "NE")
      fragEnd = MXT_FRAG_END_NONE;
    else if (fragEndString == "BE")
      fragEnd = MXT_FRAG_END_BOTH;
    else if (fragEndString == "LE")
      fragEnd = MXT_FRAG_END_LEFT;
    else if (fragEndString == "RE")
      fragEnd = MXT_FRAG_END_RIGHT;
    else
      {
	QMessageBox::warning(this, 
			      tr("massXpert - Fragmentation definition"),
			      tr("The fragmentation end is not correct. "
				  "Choose either 'NE' or 'LE' or 'RE'."),
			      QMessageBox::Ok);
	return;
      }

    // At this point, validate the formula:
  
    Formula formula(editFormula);
  
    if (!formula.validate(mp_polChemDef->atomList()))
      {
	QMessageBox::warning(this, 
			      tr("massXpert - Fragmentation definition"),
			      tr("The formula failed to validate."),
			      QMessageBox::Ok);
	return;
      }

    // At this point, check if the fragmented monomer should
    // contribute something to the mass of the fragment (usually one
    // would expect that the contribution be negative, that is that
    // the monomer gets decomposed somehow). This integer value
    // indicates if this monomer's mass should be added (positive
    // integer) or removed (negative integer) and how may times it
    // should thus be. The user is required to state what part of the
    // monomer structure should be readded.

    // The rationale is that we can easily remove the mass of the
    // whole monomer, which we know, of course. However, since full
    // removal of a monomer mass does not make sense, the user should
    // indicate which part of the monomer should be readded. This
    // mechanism is typically used when making definitions of
    // fragmentations which involve lateral chain decomposition (the
    // base in DNA, the side chain for proteins and other chemical
    // groups for sugars).

    // For example, let's imagine, we are dealing with the
    // decomposition of guanine in a DNA oligonucleotide. We want to
    // define the fragmentation pattern 'a' with full decomposition of
    // the base. 

    // First off, the monomer of a DNA is constituted of two parts :

    // 1. The skeleton, which does not change when the nucleotide
    // changes (that is which is constant which ever the base is at
    // any given monomer position), this is the sugar plus the
    // phosphate (C5H8O5P);

    // 2. The base, which changes each time a monomer changes (that's
    // the lateral chain of the monomer). Adenine has formula C5H4N5,
    // for example.

    // Now, if we wanted to define the fragmentation a-B (that is a
    // with base decomposition), we would define -O (that is the
    // normal a fragmentation formula), +OH (as the left cap) and we
    // would have to set two more parameters in order to remove the
    // mass of the nitrogenous base (lateral chain).

    // 1. Ask that the mass of the whole monomer be removed (set
    // monomerContrib below to -1). But that would be removing too
    // much stuff. We should now readd the mass of the skeleton, so
    // that the net mass loss corresponds to decomposing only the base
    // (adenine, for example).

    // 2. Ask that the mass of the skeleton is readded. This is done
    // by appending +C5H8O5P to the -O formula of the normal a
    // fragmentation pattern.

    // Finally, if we had to perform something similar wity protein,
    // we would be having the same mechanism, unless the skeleton part
    // of a proteinaceous residual chain is CH.


    int monomerContrib = m_ui.monomerContributionSpinBox->value();
  
    // Now that all data were validated, we can set them all.

    fragSpec->setName(editName);  
    fragSpec->setFragEnd(fragEnd);
    fragSpec->setFormula(editFormula);
    fragSpec->setComment(m_ui.fragSpecCommentLineEdit->text());
    fragSpec->setMonomerContribution(monomerContrib);

  
    // Update the list widget item.

    QListWidgetItem *item = m_ui.fragSpecListWidget->currentItem();
    item->setData(Qt::DisplayRole, fragSpec->name());

    setModified();
  }


  void 
  FragSpecDefDlg::applyFragRulePushButtonClicked()
  {
    // We are asked to apply the data for the fragRule.

    // If no fragRule is selected, just return.

    QList<QListWidgetItem *> selectedList = 
      m_ui.fragRuleListWidget->selectedItems();
  
    if (selectedList.size() != 1)
      return;

    // Get the fragSpec to which the fragRule belongs. 
    int index = m_ui.fragSpecListWidget->currentRow();
    FragSpec *fragSpec = mp_list->at(index);

    // Get the index of the current fragRule item.
    index = m_ui.fragRuleListWidget->currentRow();
  
    // Get the fragRule itself from the fragSpec.
    FragRule *fragRule = fragSpec->ruleList().at(index);
  
    QString editName = m_ui.fragRuleNameLineEdit->text();
    QString editFormula = m_ui.fragRuleFormulaLineEdit->text();

    QString prevCode = m_ui.prevCodeLineEdit->text();
    QString currCode = m_ui.currCodeLineEdit->text();
    QString nextCode = m_ui.nextCodeLineEdit->text();
  
    // If a fragRule is found in the list with the same name, and that
    // fragRule is not the one that is current in the fragRule list,
    // then we are making a double entry, which is not allowed.

    int nameRes = FragRule::isNameInList(editName, 
					  fragSpec->ruleList());
    if (nameRes != -1 && nameRes != index)
      {
	QMessageBox::warning(this, 
			      tr("massXpert - Fragmentation definition"),
			      tr("A fragmentation rule with same name "
				  "exists already."),
			      QMessageBox::Ok);
	return;
      }

    if (!prevCode.isEmpty())
      {
	const QList<Monomer *> &monomerList = mp_polChemDef->monomerList();
      
	if(Monomer::isCodeInList(prevCode, monomerList) == -1)
	  {
	    QMessageBox::warning(this, 
				  tr("massXpert - Fragmentation definition"),
				  tr("The previous code is not known."),
				  QMessageBox::Ok);
	    return;
	  }
      }
  
    if (!currCode.isEmpty())
      {
	const QList<Monomer *> &monomerList = mp_polChemDef->monomerList();
      
	if(Monomer::isCodeInList(currCode, monomerList) == -1)
	  {
	    QMessageBox::warning(this, 
				  tr("massXpert - Fragmentation definition"),
				  tr("The current code is not known."),
				  QMessageBox::Ok);
	    return;
	  }
      }
  
    if (!nextCode.isEmpty())
      {
	const QList<Monomer *> &monomerList = mp_polChemDef->monomerList();
      
	if(Monomer::isCodeInList(nextCode, monomerList) == -1)
	  {
	    QMessageBox::warning(this, 
				  tr("massXpert - Fragmentation definition"),
				  tr("The next code is not known."),
				  QMessageBox::Ok);
	    return;
	  }
      }
  
    Formula formula(editFormula);
  
    if (!formula.validate(mp_polChemDef->atomList()))
      {
	QMessageBox::warning(this, 
			      tr("massXpert - Fragmentation definition"),
			      tr("The formula failed to validate."),
			      QMessageBox::Ok);
	return;
      }

  
    fragRule->setName(editName);
    fragRule->setFormula(editFormula);

    fragRule->setPrevCode(prevCode);
    fragRule->setCurrCode(currCode);
    fragRule->setNextCode(nextCode);

    fragRule->setComment(m_ui.fragRuleCommentLineEdit->text());
  
    // Update the list widget item.

    QListWidgetItem *item = m_ui.fragRuleListWidget->currentItem();
    item->setData(Qt::DisplayRole, fragRule->name());

    setModified();
  }


  bool
  FragSpecDefDlg::validatePushButtonClicked()
  {
    QStringList errorList;
  
    // All we have to do is validate the fragSpec definition. For that we'll
    // go in the listwidget items one after the other and make sure that
    // everything is fine and that colinearity is perfect between the
    // fragSpec list and the listwidget.
  
    int itemCount = m_ui.fragSpecListWidget->count();
  
    if (itemCount != mp_list->size())
      {
	errorList << QString(tr("\nThe number of fragmentations in "
				  "the list widget \n"
				  "and in the list of fragmentations "
				  "is not identical.\n"));
      
	QMessageBox::warning(this, 
			      tr("massXpert - Fragmentation definition"),
			      errorList.join("\n"),
			      QMessageBox::Ok);
	return false;
      }

    for (int iter = 0; iter < mp_list->size(); ++iter)
      {
	QListWidgetItem *item = m_ui.fragSpecListWidget->item(iter);
      
	FragSpec *fragSpec = mp_list->at(iter);
      
	if(item->text() != fragSpec->name())
	  errorList << QString(tr("\nFragmentation at index %1 has not "
				    "the same\n"
				    "name as the list widget item at the\n"
				    "same index.\n")
				.arg(iter));
      
	if(!fragSpec->validate())
	  errorList << QString(tr("\nFragmentation at index %1 failed "
				    "to validate.\n")
				.arg(iter));
      }

    if (errorList.size())
      {
	QMessageBox::warning(this, 
			      tr("massXpert - Fragmentation definition"),
			      errorList.join("\n"),
			      QMessageBox::Ok);
	return false;
      }
    else
      {
	QMessageBox::warning(this, 
			      tr("massXpert - Fragmentation definition"),
			     ("Validation: success\n"),
			      QMessageBox::Ok);
      }
  
    return true;
  }


  void 
  FragSpecDefDlg::fragSpecListWidgetItemSelectionChanged()
  {
    // The fragSpec item has changed. Empty the fragRule list and update its
    // contents. Update the details for the fragSpec.

    // The list is a single-item-selection list.
  
    QList<QListWidgetItem *> selectedList = 
      m_ui.fragSpecListWidget->selectedItems();
  
    if (selectedList.size() != 1)
      return;

    // Get the index of the current fragSpec.
    int index = m_ui.fragSpecListWidget->currentRow();
  
    FragSpec *fragSpec = mp_list->at(index);
    Q_ASSERT(fragSpec);
  
    // Set the data of the fragSpec to their respective widgets.
    updateFragSpecDetails(fragSpec);
  
    // The list of fragRules
    m_ui.fragRuleListWidget->clear();
  
    for (int iter = 0; iter < fragSpec->ruleList().size(); ++iter)
      {
	FragRule *fragRule = fragSpec->ruleList().at(iter);
      
	m_ui.fragRuleListWidget->addItem(fragRule->name());
      }

    if (!m_ui.fragRuleListWidget->count())
      updateFragRuleDetails(0);
    else
      {
	// And now select the first row in the fragRule list widget.
	m_ui.fragRuleListWidget->setCurrentRow(0);
      }
  }


  void 
  FragSpecDefDlg::fragRuleListWidgetItemSelectionChanged()
  {
    // The fragRule item has changed. Update the details for the fragRule.

    // The list is a single-item-selection list.
  
    QList<QListWidgetItem *> selectedList = 
      m_ui.fragRuleListWidget->selectedItems();
  
    if (selectedList.size() != 1)
      return;

    // Get the index of the current fragSpec.
    int index = m_ui.fragSpecListWidget->currentRow();

    // Find the fragRule object in the list of fragRules.
    FragSpec *fragSpec = mp_list->at(index);
    Q_ASSERT(fragSpec);

    // Get the index of the current fragRule.
    index = m_ui.fragRuleListWidget->currentRow();
  
    // Get the fragRule that is currently selected from the fragSpec's list
    // of fragRules.
    FragRule *fragRule = fragSpec->ruleList().at(index);
    Q_ASSERT(fragRule);
  
    // Set the data of the fragRule to their respective widgets.
    updateFragRuleDetails(fragRule);
  }


  void
  FragSpecDefDlg::updateFragSpecDetails(FragSpec *fragSpec)
  {
    if (fragSpec)
      {
	m_ui.fragSpecNameLineEdit->setText(fragSpec->name());
	m_ui.fragSpecFormulaLineEdit->setText(fragSpec->formula());

	int index = 0;
      
	if(fragSpec->fragEnd() == MXT_FRAG_END_NONE)
	  index = m_ui.fragEndComboBox->findText("NE");
	else if (fragSpec->fragEnd() == MXT_FRAG_END_LEFT)
	  index = m_ui.fragEndComboBox->findText("LE");
	else if (fragSpec->fragEnd() == MXT_FRAG_END_RIGHT)
	  index = m_ui.fragEndComboBox->findText("RE");
	m_ui.fragEndComboBox->setCurrentIndex(index);
	m_ui.monomerContributionSpinBox->setValue(fragSpec->
						     monomerContribution());
	m_ui.fragSpecCommentLineEdit->setText(fragSpec->comment());
      }
    else
      {
	m_ui.fragSpecNameLineEdit->setText("");
	m_ui.fragSpecFormulaLineEdit->setText("");
	int index = m_ui.fragEndComboBox->findText("N/A");
	m_ui.fragEndComboBox->setCurrentIndex(index);
	m_ui.monomerContributionSpinBox->setValue(0);
	m_ui.fragSpecCommentLineEdit->setText("");
      } 
  }


  void
  FragSpecDefDlg::updateFragRuleDetails(FragRule *fragRule)
  {
    if (fragRule)
      {
	m_ui.fragRuleNameLineEdit->setText(fragRule->name());
	m_ui.fragRuleFormulaLineEdit->setText(fragRule->formula());

	m_ui.prevCodeLineEdit->setText(fragRule->prevCode());
	m_ui.currCodeLineEdit->setText(fragRule->currCode());
	m_ui.nextCodeLineEdit->setText(fragRule->nextCode());
      
	m_ui.fragRuleCommentLineEdit->setText(fragRule->comment());
      }
    else
      {
	m_ui.fragRuleNameLineEdit->setText("");
	m_ui.fragRuleFormulaLineEdit->setText("");

	m_ui.prevCodeLineEdit->setText("");
	m_ui.currCodeLineEdit->setText("");
	m_ui.nextCodeLineEdit->setText("");
      
	m_ui.fragRuleCommentLineEdit->setText("");
      } 
  }


  void
  FragSpecDefDlg::clearAllDetails()
  {
    m_ui.fragSpecNameLineEdit->setText("");
    m_ui.fragSpecFormulaLineEdit->setText("");
    m_ui.monomerContributionSpinBox->setValue(0);
    int index = m_ui.fragEndComboBox->findText("N/A");
    m_ui.fragEndComboBox->setCurrentIndex(index);
    m_ui.monomerContributionSpinBox->setValue(0);
    m_ui.fragSpecCommentLineEdit->setText("");

    m_ui.fragRuleNameLineEdit->setText("");
    m_ui.fragRuleFormulaLineEdit->setText("");

    m_ui.prevCodeLineEdit->setText("");
    m_ui.currCodeLineEdit->setText("");
    m_ui.nextCodeLineEdit->setText("");
      
    m_ui.fragRuleCommentLineEdit->setText("");
  }


  void 
  FragSpecDefDlg::setModified()
  {
    mp_polChemDefWnd->setWindowModified(true); 
  }


  // VALIDATION
  bool
  FragSpecDefDlg::validate()
  {
    return validatePushButtonClicked();
  }

} // namespace massXpert