File: modifDefDlg.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 (577 lines) | stat: -rw-r--r-- 14,546 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
/* 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 "modifDefDlg.hpp"
#include "polChemDef.hpp"
#include "polChemDefWnd.hpp"


namespace massXpert
{

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


  void 
  ModifDefDlg::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.modifPushButton->setChecked(false);

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

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

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

    settings.endGroup();
  }

  

  ModifDefDlg::~ModifDefDlg()
  {
  }

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

    // Set all the modifs to the list widget.

    for (int iter = 0; iter < mp_list->size(); ++iter)
      {
	Modif *modif = mp_list->at(iter);
      
	m_ui.modifListWidget->addItem(modif->name());
      }

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

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

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


    // Make the connections.

    connect(m_ui.addModifPushButton, 
	     SIGNAL(clicked()),
	     this, 
	     SLOT(addModifPushButtonClicked()));
  
    connect(m_ui.removeModifPushButton, 
	     SIGNAL(clicked()),
	     this, 
	     SLOT(removeModifPushButtonClicked()));
  
    connect(m_ui.moveUpModifPushButton, 
	     SIGNAL(clicked()),
	     this, 
	     SLOT(moveUpModifPushButtonClicked()));
  
    connect(m_ui.moveDownModifPushButton, 
	     SIGNAL(clicked()),
	     this, 
	     SLOT(moveDownModifPushButtonClicked()));
  
    connect(m_ui.applyModifPushButton, 
	     SIGNAL(clicked()),
	     this, 
	     SLOT(applyModifPushButtonClicked()));
  
    connect(m_ui.validatePushButton, 
	     SIGNAL(clicked()),
	     this, 
	     SLOT(validatePushButtonClicked()));

    connect(m_ui.modifListWidget, 
	     SIGNAL(itemSelectionChanged()),
	     this, 
	     SLOT(modifListWidgetItemSelectionChanged()));
  

    return true;
  }


  void 
  ModifDefDlg::addModifPushButtonClicked()
  {
    // We are asked to add a new modif. We'll add it right after the
    // current item.
  
    // Returns -1 if the list is empty.
    int index = m_ui.modifListWidget->currentRow();
  
    Modif *newModif = new Modif(mp_polChemDef,
				 tr("Type Name"),
				 tr("Type Formula"));
  
    mp_list->insert(index, newModif);
    m_ui.modifListWidget->insertItem(index, newModif->name());

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

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


  void 
  ModifDefDlg::removeModifPushButtonClicked()
  {
    QList<QListWidgetItem *> selectedList = 
      m_ui.modifListWidget->selectedItems();
  
    if (selectedList.size() != 1)
      return;

    // Get the index of the current modif.
    int index = m_ui.modifListWidget->currentRow();
  
    // Get a pointer to the modification, as we have to make one
    // check.

    Modif *modif = mp_list->at(index);
    Q_ASSERT(modif);
    
    // It is essential that we check if the modification being removed
    // is already used or not in a cross-linker, as it would not be
    // possible to tolerate this.

    const QList<CrossLinker *> &list = mp_polChemDef->crossLinkerList();
    
    for (int iter = 0; iter < list.size(); ++iter)
      {
	CrossLinker *crossLinker = list.at(iter);
	
	if (crossLinker->hasModif (modif->name()) != -1)
	  {
	    QString message(tr("A modif with same name is used"
			       " in cross-linker %1.")
			    .arg(crossLinker->name()));
	    
	    QMessageBox::warning(this, 
				 tr("massXpert - Modif definition"),
				 message,
				 QMessageBox::Ok);
	    
	    return;
	  }
      }
    
    // At this point we know we can delete the modif, after we have
    // removed it from the modification list of the polymer chemistry
    // definition.
    mp_list->removeAt(index);
    delete modif;

    // And the item.
    QListWidgetItem *item = m_ui.modifListWidget->takeItem(index);
    delete item;
    
    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 modif that we removed.

    if (m_ui.modifListWidget->count() >= index + 1)
      {
	m_ui.modifListWidget->setCurrentRow(index);
	modifListWidgetItemSelectionChanged();
      }
  
    // If there are no more items in the list, remove all the items
    // from the isotopeList.
  
    if (!m_ui.modifListWidget->count())
      {
	clearAllDetails();
      }
  }


  void 
  ModifDefDlg::moveUpModifPushButtonClicked()
  {
    // Move the current row to one index less.

    // If no modif is selected, just return.

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

    // Get the index of the modif and the modif itself. 
    int index = m_ui.modifListWidget->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.modifListWidget->takeItem(index);

    m_ui.modifListWidget->insertItem(index - 1, item);
    m_ui.modifListWidget->setCurrentRow(index - 1);
    modifListWidgetItemSelectionChanged();

    setModified();
  }


  void 
  ModifDefDlg::moveDownModifPushButtonClicked()
  {
    // Move the current row to one index less.

    // If no modif is selected, just return.

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

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

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

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

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

    setModified();
  }


  void 
  ModifDefDlg::applyModifPushButtonClicked()
  {
    // We are asked to apply the data for the modif.

    // If no modif is selected, just return.

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

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

    Modif *modif = mp_list->at(index);
  
    // We do not want more than one modif by the same name or the same
    // symbol.

    QString editName = m_ui.nameLineEdit->text();

    QString editFormula = m_ui.formulaLineEdit->text();
  
    QString editTargets = m_ui.targetLineEdit->text();

    int maxCount = m_ui.maxCountSpinBox->value();
    
    // If a modif is found in the list with the name string, and that
    // modif is not the one that is current in the modif list, then we
    // are making a double entry, which is not allowed.

    int nameRes = Modif::isNameInList(editName, *mp_list);
    if (nameRes != -1 && nameRes != index)
      {
	QMessageBox::warning(this, 
			      tr("massXpert - Modif definition"),
			      tr("A modif with same name exists already."),
			      QMessageBox::Ok);
	return;
      }
  
    // A modif name cannot have the same name as a crossLinker. This is
    // because the name of the modif cannot clash with the name of a
    // crossLinker when setting the graphical vignette of the
    // modif.
    if (mp_polChemDef->crossLinker(editName))
      {
	QMessageBox::warning(this, 
			      tr("massXpert - Modif definition"),
			      tr("The name of the modification is already "
				  "used by a cross-linker."),
			      QMessageBox::Ok);
	return;
      }
  
    // At this point, validate the formula:
  
    Formula formula(editFormula);
  
    if (!formula.validate(mp_polChemDef->atomList()))
      {
	QMessageBox::warning(this, 
			      tr("massXpert - Modif definition"),
			      tr("The formula failed to validate."),
			      QMessageBox::Ok);
	return;
      }

    // Validate the max count value.

    if (maxCount <= 0)
      {
	QMessageBox::warning(this, 
			      tr("massXpert - Modif definition"),
			      tr("The max count value is invalid."),
			      QMessageBox::Ok);
	return;
      }

    modif->setMaxCount(maxCount);
        
    // Validate the target list.

    Modif tempModif(*modif);

    tempModif.setTargets(editTargets);
  
    if (!tempModif.validateTargets())
      {
	QMessageBox::warning(this, 
			      tr("massXpert - Modif definition"),
			      tr("The targets string is invalid."),
			      QMessageBox::Ok);
	return;
      }
  
    modif->setTargets(tempModif.targets());
  
  
    // Finally we can update the modif's data:
  
    modif->setName(editName);
    modif->setFormula(editFormula);
  
    // Update the list widget item.

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

    setModified();
  }


  bool
  ModifDefDlg::validatePushButtonClicked()
  {
    QStringList errorList;
  
    // All we have to do is validate the modif 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
    // modif list and the listwidget.
  
    int itemCount = m_ui.modifListWidget->count();
  
    if (itemCount != mp_list->size())
      {
	errorList << QString(tr("\nThe number of modifs in the list widget \n"
				  "and in the list of modifs is not "
				  "identical.\n"));
      
	QMessageBox::warning(this, 
			      tr("massXpert - Modif definition"),
			      errorList.join("\n"),
			      QMessageBox::Ok);
	return false;
      }

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

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


  void 
  ModifDefDlg::modifListWidgetItemSelectionChanged()
  {
    // The modif item has changed. Update the details for the modif.

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

    // Get the index of the current modif.
    int index = m_ui.modifListWidget->currentRow();
  
    Modif *modif = mp_list->at(index);
    Q_ASSERT(modif);
  
    // Set the data of the modif to their respective widgets.
    updateModifDetails(modif);
  }


  void
  ModifDefDlg::updateModifDetails(Modif *modif)
  {
    if (modif)
      {
	m_ui.nameLineEdit->setText(modif->name());
	m_ui.formulaLineEdit->setText(modif->formula());
	m_ui.targetLineEdit->setText(modif->targets());
	m_ui.maxCountSpinBox->setValue(modif->maxCount());
      }
    else
      clearAllDetails();
  }
  

  void
  ModifDefDlg::clearAllDetails()
  {
    m_ui.nameLineEdit->setText("");
    m_ui.formulaLineEdit->setText("");
    m_ui.targetLineEdit->setText("");
    m_ui.maxCountSpinBox->setValue(1);
  }


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


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

} // namespace massXpert