Debian Codegraph @

dsreditwindow.cpp

raw symbols SCIP
1 /*
2 ----------------------------------------------------------------------------
3 "THE BEER-WARE LICENSE" (Revision 42):
4 <[email protected]> wrote this file. As long as you retain
5 this notice you can do whatever you want with this stuff. If we meet some day,
6 and you think this stuff is worth it, you can buy me a beer in return.
7 Daniel Kratzert
8 ----------------------------------------------------------------------------
9 */
10
11 #include <QtGui>
12 #include "dsreditwindow.h"
13 #include "dsrglwindow.h"
14 #include "deprecation.h"
15
16
17 DSREditWindow::DSREditWindow(Molecule *mol, DSRMol *header,
18 QString dsr_db_path, QString nametag,
19 QVector<QStringList> fragmentsList, DSRGui *parent)
20 : QWidget(parent)
21 {
22 RESTRAINT_CARDS << "SIMU" << "RIGU" << "DELU" << "SAME" << "FREE" << "DFIX" << "BUMP"
23 << "HFIX" << "BIND" << "SADI" << "CHIV" << "FLAT" << "DEFS" << "ISOR"
24 << "NCSY" << "DANG" << "EADP" << "EXYZ";
25 this->setParent(parent);
26 m_gui = parent;
27 m_mol = mol;
28 m_header = header;
29 mydsrdbpath = dsr_db_path;
30 m_frags = fragmentsList;
31 userdb = false;
32 m_nametag = nametag;
33 a = 0;
34 b = 0;
35 c = 0;
36 alpha = 0;
37 beta = 0;
38 gamma = 0;
39 oldatoms = new QString;
40 oldrestraints = new QString;
41 previous_atoms = new QStringList;
42 first_atnames = new QStringList;
43 setWindowFlags(Qt::Window);
44 setWindowTitle(QString("Edit Fragment"));
45 mainVLayout = new QVBoxLayout(this);
46 maingrid = new QGridLayout;
47 mainVLayout->addLayout(maingrid);
48 makeUnitCellEdit();
49 QLabel *cellabel = new QLabel("Unit Cell");
50 QLabel *namelabel = new QLabel("Name:");
51 nameEdit = new QLineEdit;
52 QLabel *restrlabel = new QLabel("Restraints:");
53 restrEdit = new QTextEdit();
54 QLabel *atomslabel = new QLabel("Atoms:");
55 atomsEdit = new QTextEdit;
56 atomsEdit->setAcceptRichText(false);
57 QLabel *resiclabel = new QLabel("Residue Class:");
58 resicledit = new QLineEdit;
59 resicledit->setMaximumWidth(getCharWidth(6));
60 resicledit->setMinimumWidth(getCharWidth(6));
61 use_selected = new QPushButton("Use selected Atoms");
62 use_selected->setMaximumWidth(getCharWidth(30));
63 myedgl = new DSRGlWindow(this, mol, *header, nametag);
64 updateButton = new QPushButton("Update Fragment");
65 addButton = new QPushButton("Add as New");
66 deleteButton = new QPushButton("Delete Fragment");
67 mailButton = new QPushButton("Mail Fragment home");
68 clearButton = new QPushButton("Clear All");
69 renameButton = new QPushButton("Enter Rename Mode");
70 accept_renameButton = new QPushButton("OK");
71 //accept_renameButton->setStyleSheet("QPushButton{background: rgb(124, 255, 117);}");
72 abort_renameButton = new QPushButton("Abort");
73 //abort_renameButton->setStyleSheet("QPushButton{background: rgb(255, 180, 180);}");
74 buttonlayout = new QGridLayout;
75 infoLabel = new QLabel;
76 // buttongrid row column spanrow spancolumn:
77 buttonlayout->addWidget(addButton, 0, 0);
78 buttonlayout->addWidget(updateButton, 0, 1);
79 buttonlayout->addWidget(deleteButton, 0, 2);
80 buttonlayout->addWidget(mailButton, 1, 0);
81 buttonlayout->addWidget(clearButton, 1, 1);
82 buttonlayout->addWidget(renameButton, 1, 2);
83 buttonlayout->addWidget(infoLabel, 2, 0, 2, 3);
84 //infoLabel->setStyleSheet("border: 1px solid lightgrey");
85 addButton->setDisabled(true);
86
87 // maingrid row column spanrow spancolumn:
88 maingrid->setRowStretch(1, 2);
89 maingrid->setRowStretch(3, 2);
90 maingrid->setRowStretch(4, 2);
91 maingrid->setColumnStretch(0, 0);
92 maingrid->setColumnStretch(1, 2);
93 maingrid->setColumnStretch(2, 0);
94 maingrid->setColumnStretch(3, 0);
95 maingrid->setColumnStretch(4, 2);
96 maingrid->addWidget(myedgl, 0, 4, 4, 3);
97 maingrid->addWidget(cellabel, 0, 0, 1, 1);
98 maingrid->addLayout(cellLayout, 0, 1, 1, 3);
99 maingrid->addWidget(namelabel, 1, 0, 1, 1);
100 maingrid->addWidget(nameEdit, 1, 1, 1, 1);
101 maingrid->addWidget(resiclabel, 1, 2, 1, 1);
102 maingrid->addWidget(resicledit, 1, 3, 1, 1);
103 maingrid->addWidget(use_selected,2, 1, 1, 2);
104 maingrid->addWidget(atomslabel, 3, 0, 1, 1);
105 maingrid->addWidget(atomsEdit, 3, 1, 1, 3);
106 maingrid->addWidget(restrlabel, 4, 0, 2, 1);
107 maingrid->addWidget(restrEdit, 4, 1, 2, 3);
108 maingrid->addLayout(buttonlayout,4, 4, 2, 3, Qt::AlignTop);
109
110 addButton->setToolTip("Add this fragment to the database.\n"
111 "Fragments from the database supplied by DSR will\n"
112 "always be stored as a new fragment in the user database.");
113 deleteButton->setToolTip("Deletes the currently opened fragment. \n"
114 "It can only delete fragments from the user database in dsr_usr_db.txt");
115 nameEdit->setToolTip("Insert any chemical name or sum formula here.");
116 resicledit->setToolTip("A residue class has to start with a letter \n"
117 "and can be up to four characters including numbers.");
118 use_selected->setToolTip("Uses the selected atoms in ShlelXle as new atoms\n"
119 "for the atom list below.");
120 atomsEdit->setToolTip("Insert atoms with following syntax:\n"
121 "Name Atomic Number x y z\n"
122 "C6 6 0.00460 2.39814 1.55732");
123 updateButton->setToolTip("Updates the fragment with the current changes.");
124 deleteButton->setToolTip("Erases the current fragment from the database.\n"
125 "(Only user-made fragments can be deleted)");
126 mailButton->setToolTip("Sends the current fragment to the author of DSR.\n"
127 "By sending the fragment to Daniel Kratzert, you\n"
128 "allow him to distribute it with the DSR program.");
129 renameButton->setToolTip("Enter rename mode to rename atoms.\n"
130 "Restraints will be updated according to the new atom names.\n"
131 "You can not add/delete atoms in rename mode.\n"
132 "Renaming is only available if no previous changes were made.");
133 clearButton->setToolTip("Clear all settings to define a new fragment.");
134 accept_renameButton->setToolTip("Accept the changes and return to fragment editor.");
135 abort_renameButton->setToolTip("Revert all changes and return to fragment editor.");
136 restrEdit->setToolTip("Insert any valid SHELXL restraint.");
137 use_selected->setStyleSheet("QPushButton {"
138 "font-weight: bold; "
139 "}");
140 infoLabel->setText("");
141 QFont labelfont("Arial bold", 12);
142 infoLabel->setFont(labelfont);
143 infoLabel->setWordWrap(true); // otherwise the widget gets elongated by long text.
144 myedgl->update();
145 // Only do this if we selected a fragment in the list!
146 // Before, we have no information from the db header.
147 if (!nametag.isEmpty() && !header->cell.isEmpty()) {
148 setCellFromDB(header);
149 }
150 if (!nametag.isEmpty() && !header->comment.isEmpty()) {
151 setNamefromDB(header);
152 }
153 if (!nametag.isEmpty() && !header->residue.isEmpty()) {
154 setResidueFromDB(header);
155 }
156 if (!nametag.isEmpty() && !header->atoms.isEmpty()) {
157 setAtomsFromDB(header);
158 }
159 if (!nametag.isEmpty() && !header->restr.isEmpty()) {
160 setRestraintsFromDB(header);
161 }
162 if (!nametag.isEmpty() && !header->dbtype.isEmpty()) {
163 setDBTypeFromDB(header);
164 }
165 if (!userdb) {
166 deleteButton->setEnabled(false);
167 }
168 if (m_nametag.isEmpty()) {
169 updateButton->setDisabled(true);
170 }
171 connectSignalsAndSlots();
172 connect(deleteButton, SIGNAL(clicked()),
173 parent->mygl, SLOT(clear_molecule()));
174 }
175
176 DSREditWindow::~DSREditWindow(){
177 }
178
179 void DSREditWindow::connectSignalsAndSlots()
180 //! handles all signals and slots
181 {
182 // check and set residue class from edit field:
183 connect(resicledit, SIGNAL(textChanged(QString)),
184 this, SLOT(checkResiClassInput(QString)));
185 // set fragname variable from edit field:
186 connect(nameEdit, SIGNAL(textChanged(QString)),
187 this, SLOT(setFragmentName(QString)));
188 // set cell from edit fields:
189 connect(cell_a, SIGNAL(textChanged(QString)),
190 this, SLOT(set_a_value(QString)));
191 connect(cell_b, SIGNAL(textChanged(QString)),
192 this, SLOT(set_b_value(QString)));
193 connect(cell_c, SIGNAL(textChanged(QString)),
194 this, SLOT(set_c_value(QString)));
195 connect(cell_alpha, SIGNAL(textChanged(QString)),
196 this, SLOT(set_alpha_value(QString)));
197 connect(cell_beta, SIGNAL(textChanged(QString)),
198 this, SLOT(set_beta_value(QString)));
199 connect(cell_gamma, SIGNAL(textChanged(QString)),
200 this, SLOT(set_gamma_value(QString)));
201 // set MyAtoms from atomsEdit field:
202 connect(atomsEdit, SIGNAL(textChanged()),
203 this, SLOT(setAtoms()));
204 connect(restrEdit, SIGNAL(textChanged()),
205 this, SLOT(setRestraints()));
206 // display an error message if there is any error in the
207 // format of atoms or restraints:
208 connect(this, SIGNAL(line_error(QString)),
209 this, SLOT(setInfoLabel(QString)));
210 connect(addButton, SIGNAL(clicked()),
211 this, SLOT(addFragment()));
212 connect(deleteButton, SIGNAL(clicked()),
213 this, SLOT(deleteCurrentFragment()));
214 connect(deleteButton, SIGNAL(clicked()),
215 this, SLOT(updateFraglist()));
216 connect(deleteButton, SIGNAL(clicked()), // the information in this window gets useless,
217 this, SLOT(close())); // thus it is closed.
218 connect(updateButton, SIGNAL(clicked()),
219 this, SLOT(updateFragment()));
220 connect(use_selected, SIGNAL(clicked()),
221 this, SLOT(setSelectedAtoms()));
222 connect(mailButton, SIGNAL(clicked()),
223 this, SLOT(mailHome()));
224 connect(renameButton, SIGNAL(clicked()),
225 this, SLOT(enterRenameMode()));
226 connect(accept_renameButton, SIGNAL(clicked()),
227 this, SLOT(leaveRenameMode()));
228 connect(clearButton, SIGNAL(clicked()),
229 this, SLOT(clearAll()));
230 }
231
232
233 QSize DSREditWindow::minimumSizeHint() const
234 {
235 return QSize(700, 700);
236 }
237
238 QSize DSREditWindow::sizeHint() const
239 {
240 return QSize(900, 700);
241 }
242
243
244 void DSREditWindow::clearAll(){
245 cell_a->clear();
246 cell_b->clear();
247 cell_c->clear();
248 cell_alpha->clear();
249 cell_beta->clear();
250 cell_gamma->clear();
251 nameEdit->clear();
252 resicledit->clear();
253 atomsEdit->clear();
254 restrEdit->clear();
255 restrEdit->append("Add at least one restraint here. (And delete this text)");
256 addButton->setDisabled(true); // Otherwise, it is possible to create a damaged fragment.
257 updateButton->setDisabled(true);
258 myedgl->clear_molecule();
259 }
260
261 void DSREditWindow::enterRenameMode(){
262 //! prepare window to rename atoms
263 first_atnames->clear();
264 foreach (QString line, atomsEdit->toPlainText().split(QRegularExpression("\n|\r\n|\r"))) {
265 first_atnames->append(line.split(' ').at(0));
266 }
267 addButton->hide();
268 updateButton->hide();
269 mailButton->hide();
270 restrEdit->setDisabled(true);
271 nameEdit->setDisabled(true);
272 resicledit->setDisabled(true);
273 cell_a->setDisabled(true);
274 cell_b->setDisabled(true);
275 cell_c->setDisabled(true);
276 cell_alpha->setDisabled(true);
277 cell_beta->setDisabled(true);
278 cell_gamma->setDisabled(true);
279 renameButton->hide();
280 deleteButton->hide();
281 use_selected->setDisabled(true);
282 buttonlayout->addWidget(accept_renameButton, 0, 0, 1, 2);
283 buttonlayout->addWidget(abort_renameButton, 1, 0, 1, 2);
284 accept_renameButton->show();
285 abort_renameButton->show();
286 atomsEdit->disconnect();
287 oldatoms->clear();
288 oldatoms->append(atomsEdit->toPlainText());
289 oldrestraints->clear();
290 oldrestraints->append(restrEdit->toPlainText());
291 connect(atomsEdit, SIGNAL(textChanged()),
292 this, SLOT(renameAtoms()));
293 connect(abort_renameButton, SIGNAL(clicked()),
294 this, SLOT(abortRenameMode()));
295 restrEdit->disconnect();
296 new_header = new DSRMol;
297 new_header->atoms.clear();
298 }
299
300 void DSREditWindow::leaveRenameMode(){
301 //! go out of rename mode and rename the restraints
302 //rename_restraints()
303 renameButton->show();
304 deleteButton->show();
305 restrEdit->setEnabled(true);
306 nameEdit->setEnabled(true);
307 resicledit->setEnabled(true);
308 cell_a->setEnabled(true);
309 cell_b->setEnabled(true);
310 cell_c->setEnabled(true);
311 cell_alpha->setEnabled(true);
312 cell_beta->setEnabled(true);
313 cell_gamma->setEnabled(true);
314 use_selected->setEnabled(true);
315 mailButton->show();
316 updateButton->show();
317 addButton->show();
318 accept_renameButton->hide();
319 abort_renameButton->hide();
320 atomsEdit->disconnect();
321 restrEdit->disconnect();
322 // re-attach signal after rename mode
323 connect(atomsEdit, SIGNAL(textChanged()),
324 this, SLOT(setAtoms()));
325 connect(restrEdit, SIGNAL(textChanged()),
326 this, SLOT(setRestraints()));
327 setAtoms();
328 setRestraints();
329 }
330
331 void DSREditWindow::abortRenameMode(){
332 //! go out of rename mode and do nothing
333 //rename_restraints()
334 renameButton->show();
335 deleteButton->show();
336 restrEdit->setEnabled(true);
337 nameEdit->setEnabled(true);
338 resicledit->setEnabled(true);
339 cell_a->setEnabled(true);
340 cell_b->setEnabled(true);
341 cell_c->setEnabled(true);
342 cell_alpha->setEnabled(true);
343 cell_beta->setEnabled(true);
344 cell_gamma->setEnabled(true);
345 use_selected->setEnabled(true);
346 mailButton->show();
347 updateButton->show();
348 addButton->show();
349 accept_renameButton->hide();
350 abort_renameButton->hide();
351 atomsEdit->disconnect();
352 restrEdit->disconnect();
353 // re-attach signal after rename mode
354 connect(atomsEdit, SIGNAL(textChanged()),
355 this, SLOT(setAtoms()));
356 connect(restrEdit, SIGNAL(textChanged()),
357 this, SLOT(setRestraints()));
358 atomsEdit->setText(oldatoms->toLatin1());
359 restrEdit->setText(oldrestraints->toLatin1());
360 myedgl->display_fragment(*m_header);
361 }
362
363 void DSREditWindow::updateFraglist(){
364 emit updated(m_nametag);
365 }
366
367 void DSREditWindow::mailHome() {
368 //! mail the current fragment to [email protected]
369 //! Formating according to RFC 6068
370 //! Mac and Windows like to have different mailstrings
371 QString mailstring;
372 #if defined Q_WS_WIN32 || defined Q_OS_WIN
373 mailstring = "mailto:[email protected]?"
374 "subject=New%20fragment%20for%20DSR"
375 "&body=Dear%20Daniel,%0D%0A"
376 "please%20add%20this%20fragment%20to%20the%20DSR%20database:%0D%0A%0D%0A"
377 +QString("%1").arg(combineDataToNewFragment().join("%0D%0A"))
378 +"%0D%0A";
379 #else
380 mailstring = "mailto:[email protected]?"
381 "subject=New fragment for DSR"
382 "&body=Dear Daniel,\n"
383 "please add this fragment to the DSR database:\n\n"
384 +QString("%1").arg(combineDataToNewFragment().join("\n"))
385 +"\n";
386 #endif
387 QDesktopServices::openUrl(QUrl(mailstring, QUrl::TolerantMode));
388 }
389
390 void DSREditWindow::addFragment() {
391 //! combines new fragment and write db to the harddisk
392 QStringList origdb;
393 QStringList newdb;
394 if (!validateCell()) {
395 emit line_error(QString(tr("<font color=red><b>No valid unit cell!</font>")));
396 return;
397 }
398 if (nameEdit->text().isEmpty()) {
399 emit line_error(QString(tr("<font color=red><b>Please give the fragment a name.</font>")));
400 return;
401 }
402 origdb = readFragmentDB();
403 newdb = combineDataToNewFragment();
404 origdb.append(newdb);
405 userdb = true;
406 writeFragmentDB(origdb);
407 this->updateFraglist();
408 this->close();
409 }
410
411 void DSREditWindow::updateFragment() {
412 //! this method deletes the old and writes the new fragment
413 QStringList origdb;
414 QStringList newFragment;
415 QString delfrag; // I will delete this later
416 delfrag = m_nametag;
417 if (!validateCell()) {
418 emit line_error(QString(tr("<font color=red><b>No valid unit cell!</font>")));
419 return;
420 }
421 if (nameEdit->text().isEmpty()) {
422 emit line_error(QString(tr("<font color=red><b>Please give the fragment a name.</font>")));
423 return;
424 }
425 origdb = readFragmentDB();
426 newFragment = combineDataToNewFragment();
427 origdb.append(newFragment);
428 userdb = true;
429 writeFragmentDB(origdb);
430 deleteFragment(delfrag);
431 this->updateFraglist();
432 this->close();
433 }
434
435 QStringList DSREditWindow::combineDataToNewFragment(){
436 //! combines all collected data to a fragment string for the database
437 QStringList fragStringList;
438 m_nametag = inventNameTag(5);
439 if (resiclass.isEmpty()) {
440 resiclass = inventNameTag(4);
441 }
442 fragStringList.append("\n<"+m_nametag+">");
443 fragStringList.append("rem Name: "+fragname+"");
444 fragStringList.append("rem Src: ShelXle DSR GUI");
445 fragStringList.append("RESI "+resiclass);
446 foreach (QStringList line, myRestraints) {
447 fragStringList.append(line.join(" "));
448 }
449 fragStringList.append("FRAG 17 "+QString("%1 %2 %3 %4 %5 %6")
450 .arg(a).arg(b).arg(c)
451 .arg(alpha).arg(beta).arg(gamma));
452 QString atline;
453 foreach (QStringList line, myAtoms) {
454 if (line.length() < 5 ) continue;
455 atline.clear();
456 atline = QString("%1 %2 %3 %4 %5")
457 .arg(line.at(0), -5, ' ')
458 .arg(line.at(1), -3, ' ')
459 .arg(line.at(2).toDouble(), 10, 'f', 5)
460 .arg(line.at(3).toDouble(), 10, 'f', 5)
461 .arg(line.at(4).toDouble(), 10, 'f', 5);
462 fragStringList.append(atline);
463 }
464 fragStringList.append("</"+m_nametag+">");
465 return fragStringList;
466 }
467
468 bool DSREditWindow::validateCell() {
469 //! validates if the cell parameter make sense
470 if (a > 0 && b > 0 && c > 0) {
471 if ((alpha > 0 && beta > 0 && gamma > 0) && (alpha+beta+gamma <= 360)) {
472 return true;
473 }
474 }
475 return false;
476 }
477
478 void DSREditWindow::setDBTypeFromDB(DSRMol *header) {
479 //! set userdb false or true if "dsr_db" or "dsr_user_db"
480 if (header->dbtype == QString("dsr_user_db")) {
481 userdb = true;
482 }
483 }
484
485 void DSREditWindow::setInfoLabel(QString myerror) {
486 //! display the error of the error signa in infoLabel
487 infoLabel->setText(myerror);
488 }
489
490 void DSREditWindow::setNamefromDB(DSRMol *header) {
491 //! Set the name of the linedit field nameEdit to the fragment name from the database
492 //! This method runs if you select a fragment and then click on the edit button.
493 QString name;
494 name = header->comment;
495 nameEdit->setText(name);
496 fragname = name;
497 }
498
499 void DSREditWindow::setResidueFromDB(DSRMol *header) {
500 //! get residue from header and populate resiclass variable and edit field
501 QString residueClass;
502 residueClass = header->residue;
503 resicledit->setText(residueClass);
504 resiclass = residueClass;
505 }
506
507 void DSREditWindow::setAtomsFromDB(DSRMol *header) {
508 //! Fills the atoms edit field with atom data.
509 QFont font("Courier");
510 atomsEdit->setLineWrapMode(QTextEdit::NoWrap);
511 font.setStyleHint(QFont::TypeWriter);
512 atomsEdit->setMinimumHeight(200);
513 atomsEdit->setFont(font);
514 QTextCursor cursor = atomsEdit->textCursor();
515 QString atomEditorLine;
516 foreach (QStringList atom, header->atoms) {
517 myAtoms.append(atom);
518 atomEditorLine.clear();
519 int num = 0;
520 // This relies on the .format() by DSR!:
521 foreach (QString column, atom) { // format each column:
522 switch (num) {
523 case 0: // name
524 column = column.leftJustified(6, ' ');
525 break;
526 case 1: // sfac
527 column = column.leftJustified(4, ' ');
528 break;
529 case 2: // x
530 column = column.rightJustified(9, ' ')+" ";
531 break;
532 case 3: // y
533 column = column.rightJustified(9, ' ')+" ";
534 break;
535 case 4: // z
536 column = column.rightJustified(9, ' ');
537 break;
538 default: // anything else, just in case...
539 column = column.leftJustified(9, ' ');
540 break;
541 }
542 atomEditorLine = atomEditorLine+column;
543 num++;
544 }
545 atomsEdit->append(atomEditorLine);
546 }
547 cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor);
548 atomsEdit->setTextCursor(cursor);
549 }
550
551 void DSREditWindow::setRestraintsFromDB(DSRMol *header) {
552 //! Fils the restraints edit field with restraints from the db
553 QFont font("Courier");
554 restrEdit->setLineWrapMode(QTextEdit::NoWrap);
555 font.setStyleHint(QFont::TypeWriter);
556 restrEdit->setMinimumHeight(200);
557 restrEdit->setFont(font);
558 QTextCursor cursor = restrEdit->textCursor();
559 QStringList restr;
560 restr = header->restr;
561 myRestraints.clear();
562 for (int i=0; i<restr.size(); i++) {
563 restrEdit->append(restr.at(i));
564 myRestraints.append(restr.at(i).split(" ", skipEmptyParts));
565 }
566 cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor);
567 restrEdit->setTextCursor(cursor);
568 }
569
570 void DSREditWindow::setCellFromDB(DSRMol *header) {
571 // 2
572 //! Sets the cell information from the dsr db.
573 //! Usually a 1 1 1 90 90 90 cell should come from DSR
574 //! because it exports cartesian coordinates for this GUI
575 a = header->cell.at(0);
576 b = header->cell.at(1);
577 c = header->cell.at(2);
578 alpha = header->cell.at(3);
579 beta = header->cell.at(4);
580 gamma = header->cell.at(5);
581 cell_a->setText(QString("%1").arg(a));
582 cell_b->setText(QString("%1").arg(b));
583 cell_c->setText(QString("%1").arg(c));
584 cell_alpha->setText(QString("%1").arg(alpha));
585 cell_beta->setText(QString("%1").arg(beta));
586 cell_gamma->setText(QString("%1").arg(gamma));
587 cell_a->setMaximumWidth(getCharWidth(10));
588 cell_a->setMinimumWidth(getCharWidth(6));
589 cell_b->setMaximumWidth(getCharWidth(10));
590 cell_b->setMinimumWidth(getCharWidth(6));
591 cell_c->setMaximumWidth(getCharWidth(10));
592 cell_c->setMinimumWidth(getCharWidth(6));
593 cell_alpha->setMaximumWidth(getCharWidth(10));
594 cell_alpha->setMinimumWidth(getCharWidth(6));
595 cell_beta->setMaximumWidth(getCharWidth(10));
596 cell_beta->setMinimumWidth(getCharWidth(6));
597 cell_gamma->setMaximumWidth(getCharWidth(10));
598 cell_gamma->setMinimumWidth(getCharWidth(6));
599 }
600
601 void DSREditWindow::checkResiClassInput(QString text) {
602 //! check wether the input text to residue class field is valid.
603 if (!text[0].isLetter()) {
604 resicledit->clear(); // first character has to be a letter!
605 resiclass.clear();
606 return;
607 }
608 resiclass.clear();
609 resiclass = text.toUpper();
610 resicledit->setText(text.left(4).toUpper()); // resi class allows up to 4 characters!
611 }
612
613 void DSREditWindow::setFragmentName(QString name) {
614 //! runs if fragment name is changed
615 fragname = name;
616 }
617
618
619 void DSREditWindow::renameRestraints(QStringList new_atnames, int cursopos){
620 //! renames the restraints in rename mode
621 QTextCursor cursor = atomsEdit->textCursor();
622 cursor.setPosition(cursopos, QTextCursor::MoveAnchor);
623 QStringList origrestr;
624 origrestr = restrEdit->toPlainText().split(QRegularExpression("\n|\r\n|\r"));
625 atomsEdit->setTextCursor(cursor);
626 // the previous atoms are the names before I typed a new one and renaming starts
627 // I want to rename previous names with new names
628 if (new_atnames.length() == previous_atoms->length()) {
629 restrEdit->clear();
630 foreach (QString rline, origrestr) {
631 QStringList rline_list;
632 rline_list = rline.split(' ');
633 // go through all new atoms and replace previous names with new names in restraints list:
634 int num = 0;
635 foreach (QString newatom, new_atnames) {
636 for(int i = 0; i < rline_list.length(); i++) {
637 if (rline_list.at(i).toUpper() == previous_atoms->at(num).toUpper()) {
638 rline_list[i] = newatom.toUpper().toLatin1();
639 }
640 }
641 num++;
642 }
643 restrEdit->append(rline_list.join(" "));
644 }
645 }
646 }
647
648 void DSREditWindow::renameAtoms(){
649 //! Takes the text from atomEdit and fills myAtoms Qvector<QStringlist>
650 //! The special thing in this method is that only the Names can be changed
651 //! The text after the fourth character will constantly be overwritten by
652 //! the previous coordinates.
653 //! It should be impossble to delete an atom.
654 int cursopos = atomsEdit->textCursor().position();
655 myAtoms.clear();
656 atomsEdit->disconnect();
657 badLine = false;
658 QString error;
659 QStringList line_split;
660 QStringList new_atnames; // <- the new atom names
661 int num = 0;
662 error = QString(tr(""));
663 emit line_error(error);
664 int length = atomsEdit->toPlainText().split(QRegularExpression("\n|\r\n|\r")).length();
665 if (previous_atoms->length() == 0) {
666 previous_atoms->append(*first_atnames);
667 }
668 foreach (QString line, atomsEdit->toPlainText().split(QRegularExpression("\n|\r\n|\r"))) {
669 if (length != previous_atoms->length()) { // make sure not to delete an atom!
670 atomsEdit->undo(); // In this case, undo last action
671 connect(atomsEdit, SIGNAL(textChanged()),
672 this, SLOT(renameAtoms()));
673 return;
674 }
675 line_split = line.split(" ", skipEmptyParts);
676 if (line_split.length() > 0){
677 line_split[0] = line_split.at(0).left(4);
678 new_atnames.append(line_split.at(0).toUpper()); // collect new atom names after after a character was changed
679 } else { // This is to make sure there is no empty line. An empty line will cause the rename mode to abort.
680 this->atomsEdit->undo();
681 this->abortRenameMode();
682 error = QString(tr("<font color=red><b>Do not delete atoms in rename mode!</font>"));
683 emit line_error(error);
684 return;
685 }
686 num++;
687 if (new_atnames.count(line_split.at(0)) >= 2) {
688 // warn user about duplicates and do not rename atom:
689 error = QString(tr("<font color=red><b>Attempt to create duplicate atom \"%1\" detected. "
690 "<br>Please make them unique.</font>").arg(line_split.at(0)));
691 emit line_error(error);
692 atomsEdit->undo(); // In this case, undo last action. Makes duplicates impossible
693 connect(atomsEdit, SIGNAL(textChanged()),
694 this, SLOT(renameAtoms()));
695 return;
696 }
697 if (!line_split.at(0).contains(QRegularExpression("^([A-Za-z]{1,3})\\d{0,3}[A-Za-z\\'\\\"\\#]{0,2}\\d{0,1}"))) {
698 error = QString(tr("<font color=red><b>Invalid atom name found.</font>"));
699 emit line_error(error);
700 connect(atomsEdit, SIGNAL(textChanged()),
701 this, SLOT(renameAtoms()));
702 return;
703 }
704 }
705 atomsEdit->clear();
706 int onum = 0;
707 foreach (QString line, oldatoms->split(QRegularExpression("\n|\r\n|\r"))) {
708 // use the new names + the previous atoms
709 line = new_atnames.at(onum).leftJustified(4, ' ').left(4)+line.remove(0, 4);
710 atomsEdit->append(line);
711 myAtoms.append(line.split(' ', skipEmptyParts));
712 onum++;
713 }
714 renameRestraints(new_atnames, cursopos);
715 previous_atoms->clear();
716 previous_atoms->append(new_atnames);
717 new_header->atoms.clear();
718 foreach(QStringList line, myAtoms) {
719 new_header->atoms.append(line);
720 }
721 connect(atomsEdit, SIGNAL(textChanged()),
722 this, SLOT(renameAtoms()));
723 myedgl->display_fragment(*new_header);
724 }
725
726
727 void DSREditWindow::setAtoms(){
728 //! takes the text from atomEdit and fills myAtoms Qvector<QStringlist>
729 renameButton->setDisabled(true);
730 myAtoms.clear();
731 badLine = false;
732 QString error;
733 QStringList line_split;
734 QStringList atnames;
735 int num = 0;
736 foreach (QString line, atomsEdit->toPlainText().split(QRegularExpression("\n|\r\n|\r"))) {
737 line_split = line.split(" ", skipEmptyParts);
738 line_split = checkAtomLine(line_split, num+1);
739 myAtoms.append(line_split);
740 if (line_split.length() > 0){
741 atnames.append(line_split.at(0).toUpper());
742 if (atnames.count(line_split.at(0).toUpper()) >= 2) {
743 error = QString(tr("<font color=red><b>Duplicate atom \"%1\" found! Please make them unique.</font>").arg(line_split.at(0)));
744 // Can not do undo(), because it would prevent adding duplicate names with "add selected atoms"
745 // and this has to be possible because of residues:
746 //atomsEdit->undo();
747 emit line_error(error);
748 badLine = true;
749 //return;
750 }
751 }
752 num++;
753 }
754 if (myAtoms.size() <= 1 && myAtoms[0].isEmpty()) {
755 addButton->setDisabled(true);
756 updateButton->setDisabled(true);
757 error = QString(tr("<font color=red><b>Please add at least one atom to add/update the fragment.</font>"));
758 emit line_error(error);
759 return;
760 } else {
761 addButton->setEnabled(true);
762 updateButton->setEnabled(true);
763 }
764 if (!badLine) {
765 emit line_error("");
766 addButton->setEnabled(true);
767 updateButton->setEnabled(true);
768 } else {
769 updateButton->setDisabled(true);
770 addButton->setDisabled(true);
771 return;
772 }
773 }
774
775 void DSREditWindow::setSelectedAtoms() {
776 //! uses selected atoms for atom field and sets cell accordingly
777 atomsEdit->clear();
778 int numselect = m_mol->selectedatoms.size();
779 QString line;
780 QString atname;
781 QString firstatom;
782 QString lastatom;
783 for (int i = 0; i < numselect; i++) {
784 line.clear();
785 atname.clear();
786 // The two split() make sure no illegal suffices remain on the labels:
787 atname = m_mol->selectedatoms.at(i).Label.split('_').at(0).split(QChar(187)).at(0);
788 if (i == 0) {
789 firstatom = atname;
790 }
791 if (i+1 == numselect) {
792 lastatom = atname;
793 }
794 line = QString("%1 %2 %3 %4 %5")
795 .arg(atname, -5) // Atom name
796 .arg(m_mol->selectedatoms.at(i).an+1, -2) // strangely ShelXle stores AN-1 here
797 .arg(m_mol->selectedatoms.at(i).pos.x, 10, 'f', 5) // X
798 .arg(m_mol->selectedatoms.at(i).pos.y, 10, 'f', 5) // Y
799 .arg(m_mol->selectedatoms.at(i).pos.z, 10, 'f', 5); // Z
800 atomsEdit->append(line);
801 }
802 // make sure at least some restraints end up in the database:
803 if ((restrEdit->toPlainText().simplified().size() < 4)
804 && (firstatom.size() > 0) && (lastatom.size() > 0)) {
805 restrEdit->clear();
806 restrEdit->append(QString("RIGU %1 > %2").arg(firstatom, lastatom));
807 restrEdit->append(QString("SIMU %1 > %2").arg(firstatom, lastatom));
808 }
809 cell_a->setText("1");
810 cell_b->setText("1");
811 cell_c->setText("1");
812 cell_alpha->setText("90");
813 cell_beta->setText("90");
814 cell_gamma->setText("90");
815 updateButton->show();
816 addButton->show();
817 }
818
819 QStringList DSREditWindow::checkAtomLine(QStringList line, int num) {
820 //! check an atom text line for consistency
821 //! like if there are all xyz parameters or the name makes sense
822 QString error;
823 error.clear();
824 bool isAtFloat = false;
825 if (!line.isEmpty()) {
826 line.at(0).toFloat(&isAtFloat);
827 }
828 bool is_scatt_int = true;
829 if (line.length() >= 2) {
830 line.at(1).toFloat(&is_scatt_int);
831 }
832 if (line.size() > 0 && line.size() < 5) {
833 error = QString(tr("<font color=red><b>Error in atom line %1: Too few parameters.</b></font>")).arg(num);
834 emit line_error(error);
835 badLine = true;
836 } else if (line.size() > 5) {
837 error = QString(tr("<font color=red><b>Error in atom line %1: Too many parameters.</font>")).arg(num);
838 emit line_error(error);
839 badLine = true;
840 } else if (line.join("").contains(",")) {
841 error = QString(tr("<font color=red><b>Error in atom line %1: Numbers should not contain comma.</font>")).arg(num);
842 emit line_error(error);
843 badLine = true;
844 } else if (!line.isEmpty() && line[0][0].isDigit()) {
845 error = QString(tr("<font color=red><b>Error in atom line %1: Atoms can not begin with a number.</font>")).arg(num);
846 emit line_error(error);
847 badLine = true;
848 } else if (!line.isEmpty() && line[0].length() > 4) {
849 error = QString(tr("<font color=red><b>Error in atom line %1: Atom name must not have more than four characters.</font>")).arg(num);
850 emit line_error(error);
851 badLine = true;
852 } else if (!line.isEmpty() && line[0].contains("_")) {
853 error = QString(tr("<font color=red><b>Error in atom line %1: Atom name must not have underscores.</font>")).arg(num);
854 emit line_error(error);
855 badLine = true;
856 } else if (isAtFloat) {
857 error = QString(tr("<font color=red><b>Error in atom line %1: Atoms can not be only numbers.</font>")).arg(num);
858 emit line_error(error);
859 badLine = true;
860 } else if (!myisAscii(line.join(""))) {
861 error = QString(tr("<font color=red><b>Error in atom line %1: Non-ascii characters are not allowed.</font>")).arg(num);
862 emit line_error(error);
863 badLine = true;
864 } else if (!is_scatt_int) {
865 error = QString(tr("<font color=red><b>Error in atom line %1: Scattering factor can be only integer numbers.</font>")).arg(num);
866 emit line_error(error);
867 badLine = true;
868 } else if (!validateCell()) {
869 error = QString(tr("<font color=red><b>Please define proper unit cell parameters!</font>"));
870 emit line_error(error);
871 badLine = true;
872 }else {
873 int num2 = 0;
874 foreach (QString item, line) {
875 bool ok = false;
876 item.toFloat(&ok);
877 if (num2 >= 2 && !ok){
878 error = QString(tr("<font color=red><b>Error in atom line %1: columns 3 to 5 contain non-numerical character(s).</font>")).arg(num);
879 emit line_error(error);
880 badLine = true;
881 }
882 num2++;
883 }
884 }
885 if (badLine == true) {
886 addButton->setDisabled(true);
887 updateButton->setDisabled(true);
888 } else {
889 addButton->setEnabled(true);
890 updateButton->setEnabled(true);
891 }
892 return line;
893 }
894
895 bool DSREditWindow::myisAscii(QString line) {
896 //! test if line contains non-ascii characters
897 for (int num=0; num<line.length(); num++) {
898 if (line.at(num).unicode() > 127) return false;
899 }
900 return true;
901 }
902
903 void DSREditWindow::setRestraints() {
904 //! takes text from restrEdit and fills myRestraints Qvector<QStringlist>
905 renameButton->setDisabled(true);
906 myRestraints.clear();
907 badLine = false;
908 QString error;
909 QStringList line_split;
910 int num = 0;
911 foreach (QString line, restrEdit->toPlainText().split(QRegularExpression("\n|\r\n|\r"))) {
912 line_split = line.split(" ", skipEmptyParts);
913 checkRestraintLine(line_split, num+1);
914 myRestraints.append(line_split);
915 num++;
916 }
917 if (!badLine) {
918 emit line_error("");
919 } else {
920 return;
921 }
922 if (myRestraints[0].isEmpty()) {
923 addButton->setDisabled(true);
924 updateButton->setDisabled(true);
925 error = QString(tr("<font color=red><b>Please add at least one restraint to add/update the fragment.</font>"));
926 emit line_error(error);
927 } else {
928 addButton->setEnabled(true);
929 updateButton->setEnabled(true);
930 }
931 }
932
933 void DSREditWindow::checkRestraintLine(QStringList restrline, int num) {
934 //! Check each line of the restraints for errors. Especially atoms that
935 //! are not in the atoms list.
936 bool ok;
937 QString error;
938 QStringList atomList;
939 foreach (QStringList atomline, myAtoms) {
940 if (atomline.isEmpty()) continue;
941 atomList.append(atomline.at(0).toUpper());
942 }
943 foreach (QString item, restrline.mid(0, 1)) {
944 if (!RESTRAINT_CARDS.contains(item.toUpper())){
945 error = QString(tr("<font color=red><b>Invalid restraint %1 in line %2.</font>")).arg(item).arg(num);
946 updateButton->setDisabled(true);
947 addButton->setDisabled(true);
948 emit line_error(error);
949 badLine = true;
950 }
951 if (badLine == true) {
952 return;
953 }
954 }
955 foreach (QString item, restrline.mid(1, -1)) {
956 item.toFloat(&ok);
957 if (ok) {continue;}
958 if (item == ">") {continue;}
959 if (item == "<") {continue;}
960 if (item == "_") {continue;}
961 if (item == "$") {continue;}
962 if (!atomList.contains(item.toUpper())) {
963 error = QString(tr("<font color=red><b>Atom %1 in restraints line %2 not in atom list!</font>")).arg(item).arg(num);
964 updateButton->setDisabled(true);
965 addButton->setDisabled(true);
966 emit line_error(error);
967 badLine = true;
968 }
969 if (badLine == true) {
970 return;
971 }
972 error = "";
973 emit line_error(error);
974 }
975 num++;
976 }
977
978 void DSREditWindow::makeUnitCellEdit(){
979 //! The unit cell input field
980 cellLayout = new QHBoxLayout;
981 cell_a = new QLineEdit;
982 cell_b = new QLineEdit;
983 cell_c = new QLineEdit;
984 cell_alpha = new QLineEdit;
985 cell_beta = new QLineEdit;
986 cell_gamma = new QLineEdit;
987 QLabel *alabel = new QLabel("a:");
988 QLabel *blabel = new QLabel("b:");
989 QLabel *clabel = new QLabel("c:");
990 QLabel *alphalabel = new QLabel(QChar(0xb1, 0x03)+QString(":"));
991 QLabel *betalabel = new QLabel(QChar(0xb2, 0x03)+QString(":"));
992 QLabel *gammalabel = new QLabel(QChar(0xb3, 0x03)+QString(":"));
993 cellLayout->addWidget(alabel);
994 cellLayout->addWidget(cell_a);
995 cellLayout->addWidget(blabel);
996 cellLayout->addWidget(cell_b);
997 cellLayout->addWidget(clabel);
998 cellLayout->addWidget(cell_c);
999 cellLayout->addWidget(alphalabel);
1000 cellLayout->addWidget(cell_alpha);
1001 cellLayout->addWidget(betalabel);
1002 cellLayout->addWidget(cell_beta);
1003 cellLayout->addWidget(gammalabel);
1004 cellLayout->addWidget(cell_gamma);
1005 QDoubleValidator *cellaValidator = new QDoubleValidator(0, 999, 6, cell_a);
1006 cellaValidator->setNotation(QDoubleValidator::StandardNotation);
1007 QDoubleValidator *cellbValidator = new QDoubleValidator(0, 999, 6, cell_b);
1008 cellbValidator->setNotation(QDoubleValidator::StandardNotation);
1009 QDoubleValidator *cellcValidator = new QDoubleValidator(0, 999, 6, cell_c);
1010 cellcValidator->setNotation(QDoubleValidator::StandardNotation);
1011 QDoubleValidator *cellalValidator = new QDoubleValidator(0, 360, 6, cell_alpha);
1012 cellalValidator->setNotation(QDoubleValidator::StandardNotation);
1013 QDoubleValidator *cellbeValidator = new QDoubleValidator(0, 360, 6, cell_beta);
1014 cellbeValidator->setNotation(QDoubleValidator::StandardNotation);
1015 QDoubleValidator *cellgaValidator = new QDoubleValidator(0, 360, 6, cell_gamma);
1016 cellgaValidator->setNotation(QDoubleValidator::StandardNotation);
1017 //connect(cell_a, SIGNAL(textChanged(QString)),cell_a, SLOT(validate_cellinput()));
1018 cell_a->setValidator(cellaValidator);
1019 cell_b->setValidator(cellbValidator);
1020 cell_c->setValidator(cellcValidator);
1021 cell_alpha->setValidator(cellalValidator);
1022 cell_beta->setValidator(cellbeValidator);
1023 cell_gamma->setValidator(cellgaValidator);
1024 cell_a->setMaximumWidth(getCharWidth(7));
1025 cell_b->setMaximumWidth(getCharWidth(7));
1026 cell_c->setMaximumWidth(getCharWidth(7));
1027 cell_alpha->setMaximumWidth(getCharWidth(7));
1028 cell_beta->setMaximumWidth(getCharWidth(7));
1029 cell_gamma->setMaximumWidth(getCharWidth(7));
1030 }
1031
1032 void DSREditWindow::set_a_value(QString a) {
1033 cell_a->setStyleSheet("QLineEdit");
1034 if (a.contains(",")){
1035 a.replace(",", ".");
1036 cell_a->setText(a);
1037 }
1038 bool ok;
1039 this->a = a.toDouble(&ok);
1040 if (!ok) {
1041 cell_a->setStyleSheet("QLineEdit{background: rgb(255, 180, 180);}");
1042 }
1043 if (validateCell()) {
1044 emit line_error("");
1045 } else {
1046 emit line_error(QString(tr("<font color=red><b>Please define proper unit cell parameters!</font>")));
1047 }
1048 }
1049
1050 void DSREditWindow::set_b_value(QString b) {
1051 cell_b->setStyleSheet("QLineEdit");
1052 if (b.contains(",")){
1053 b.replace(",", ".");
1054 cell_b->setText(b);
1055 }
1056 bool ok;
1057 this->b = b.toDouble(&ok);
1058 if (!ok) {
1059 cell_b->setStyleSheet("QLineEdit{background: rgb(255, 180, 180);}");
1060 }
1061 if (validateCell()) {
1062 emit line_error("");
1063 } else {
1064 emit line_error(QString(tr("<font color=red><b>Please define proper unit cell parameters!</font>")));
1065 }
1066 }
1067 void DSREditWindow::set_c_value(QString c) {
1068 cell_c->setStyleSheet("QLineEdit");
1069 if (c.contains(",")){
1070 c.replace(",", ".");
1071 cell_c->setText(c);
1072 }
1073 bool ok;
1074 this->c = c.toDouble(&ok);
1075 if (!ok) {
1076 cell_c->setStyleSheet("QLineEdit{background: rgb(255, 180, 180);}");
1077 }
1078 if (validateCell()) {
1079 emit line_error("");
1080 } else {
1081 emit line_error(QString(tr("<font color=red><b>Please define proper unit cell parameters!</font>")));
1082 }
1083 }
1084
1085 void DSREditWindow::set_alpha_value(QString alpha) {
1086 cell_alpha->setStyleSheet("QLineEdit");
1087 if (alpha.contains(",")){
1088 alpha.replace(",", ".");
1089 cell_alpha->setText(alpha);
1090 }
1091 bool ok;
1092 this->alpha = alpha.toDouble(&ok);
1093 if (!ok) {
1094 cell_alpha->setStyleSheet("QLineEdit{background: rgb(255, 180, 180);}");
1095 }
1096 if (validateCell()) {
1097 emit line_error("");
1098 } else {
1099 emit line_error(QString(tr("<font color=red><b>Please define proper unit cell parameters!</font>")));
1100 }
1101 }
1102
1103 void DSREditWindow::set_beta_value(QString beta) {
1104 cell_beta->setStyleSheet("QLineEdit");
1105 if (beta.contains(",")){
1106 beta.replace(",", ".");
1107 cell_beta->setText(beta);
1108 }
1109 bool ok;
1110 this->beta = beta.toDouble(&ok);
1111 if (!ok) {
1112 cell_beta->setStyleSheet("QLineEdit{background: rgb(255, 180, 180);}");
1113 }
1114 if (validateCell()) {
1115 emit line_error("");
1116 } else {
1117 emit line_error(QString(tr("<font color=red><b>Please define proper unit cell parameters!</font>")));
1118 }
1119 }
1120
1121 void DSREditWindow::set_gamma_value(QString gamma) {
1122 cell_gamma->setStyleSheet("QLineEdit");
1123 if (gamma.contains(",")){
1124 gamma.replace(",", ".");
1125 cell_gamma->setText(gamma);
1126 }
1127 bool ok;
1128 this->gamma = gamma.toDouble(&ok);
1129 if (!ok) {
1130 cell_gamma->setStyleSheet("QLineEdit{background: rgb(255, 180, 180);}");
1131 }
1132 if (validateCell()) {
1133 emit line_error("");
1134 } else {
1135 emit line_error(QString(tr("<font color=red><b>Please define proper unit cell parameters!</font>")));
1136 }
1137 }
1138
1139 int DSREditWindow::getCharWidth(int numchars) {
1140 //! returns the width of a numchar amount of characters
1141 QString buchstaben;
1142 buchstaben.clear();
1143 for (int i=1; i<=numchars; i++) {
1144 buchstaben += "#";
1145 }
1146
1147 #if (QT_VERSION >= QT_VERSION_CHECK(5, 11, 0))
1148 return QFontMetrics(this->font()).horizontalAdvance(buchstaben);
1149 }
1150 #else
1151 return QFontMetrics(this->font()).width(buchstaben);
1152 }
1153 #endif
1154
1155 void DSREditWindow::deleteCurrentFragment(){
1156 //! delete the currently open fragment
1157 deleteFragment(m_nametag);
1158 }
1159
1160 void DSREditWindow::deleteFragment(QString nametag){
1161 //! deletes the fragment with tag "nametag" from and only
1162 //! from the dsr_user_db.txt.
1163 QStringList udb = readFragmentDB();
1164 QStringList udb2;
1165 udb2.clear();
1166 int startlineNumber, endlineNumber;
1167 startlineNumber = -1;
1168 endlineNumber = -1;
1169 for (int i=0; i<udb.size(); i++) {
1170 if (udb[i].toUpper().startsWith("<"+nametag.toUpper()+">")){
1171 if (startlineNumber > 0) {
1172 setInfoLabel(QString(tr("Duplicate entry for %1 found in the users database")).arg(nametag));
1173 return;} // in this case we have a dublicate entry
1174 startlineNumber = i;
1175 }
1176 if (udb[i].toUpper().startsWith("</"+nametag.toUpper()+">")){
1177 if (endlineNumber > 0) {
1178 setInfoLabel(QString(tr("Duplicate entry for %1 found in the users database")).arg(nametag));
1179 return;} // in this case we have a dublicate entry
1180 endlineNumber = i;
1181 }
1182 }
1183 if ((startlineNumber < 0) || (endlineNumber < 0)) {
1184 setInfoLabel(QString(tr("Start or end tag %1 missing in the users database")).arg(nametag));
1185 return; // in this case any of the db tags was not found
1186 }
1187 if (startlineNumber > 0) {
1188 udb2 = udb.mid(0, startlineNumber);
1189 }
1190 udb2 = udb2+udb.mid(endlineNumber+1, -1);
1191 writeFragmentDB(udb2);
1192 }
1193
1194 QStringList DSREditWindow::readFragmentDB() {
1195 //! read the entire user databse into a stringlist
1196 QFile file(mydsrdbpath+"/dsr_user_db.txt");
1197 QStringList stringList;
1198 if (file.open(QFile::ReadOnly | QFile::Text)) {
1199 QTextStream textStream(&file);
1200 while (true) {
1201 QString line = textStream.readLine();
1202 if (line.isNull()) {
1203 break;
1204 }
1205 if (line.isEmpty()) {
1206 continue;
1207 } else {
1208 stringList.append(line);
1209 }
1210 }
1211 }
1212 file.close();
1213 return stringList;
1214 }
1215
1216 void DSREditWindow::writeFragmentDB(QStringList database) {
1217 //! write the entire user databse into a text file
1218 if (userdb == false) {
1219 return;
1220 } // never delete from main DB
1221 QFile file(mydsrdbpath+"/dsr_user_db.txt");
1222 if (file.open(QFile::ReadWrite | QFile::Text)) {
1223 QTextStream out(&file);
1224 file.resize(0); // delete original
1225 out << database.join("\n");
1226 }
1227 file.close();
1228 }
1229
1230 bool DSREditWindow::doesFragExist(QString name, bool fullname=false) {
1231 //! checks if a fragment nametag is already in the database
1232 //! setting fullname to true checks for the full name instead of the tag
1233 int column = 0;
1234 if (fullname) {
1235 column = 1;
1236 }
1237 foreach (QStringList line, m_frags) {
1238 if (line.at(column).toLower() == name.toLower()) {
1239 return true;
1240 }
1241 }
1242 return false;
1243 }
1244
1245 QString DSREditWindow::randString(int len) {
1246 //! returns a random string of size len
1247 //! from http://stackoverflow.com/questions/3244999/create-a-random-string-or-number-in-qt4
1248 QString str;
1249 str.resize(len);
1250 #if (QT_VERSION>=0x050a00)
1251 for (int s = 0; s < len ; ++s) {
1252 str[s] = QChar('A' + char(QRandomGenerator::global()->generate() % ('Z' - 'A')));
1253 }
1254 #else
1255 for (int s = 0; s < len ; ++s) {
1256 str[s] = QChar('A' + char(qrand() % ('Z' - 'A')));
1257 }
1258 #endif
1259 return str;
1260 }
1261
1262 QString DSREditWindow::inventNameTag(int length=5){
1263 //! invents a random name tag for a new fragment
1264 QString name;
1265 bool there = true;
1266 while (there) {
1267 name = randString(length);
1268 there = doesFragExist(name, false);
1269 }
1270 return name;
1271 }
1272