1 /**
2  * UGENE - Integrated Bioinformatics Tools.
3  * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4  * http://ugene.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 
22 #include "RemovePartFromSequenceDialogController.h"
23 
24 #include <QDir>
25 #include <QMessageBox>
26 #include <QPushButton>
27 
28 #include <U2Core/AnnotationData.h>
29 #include <U2Core/AppContext.h>
30 #include <U2Core/BaseDocumentFormats.h>
31 #include <U2Core/DocumentModel.h>
32 #include <U2Core/ModifySequenceObjectTask.h>
33 #include <U2Core/U2SafePoints.h>
34 
35 #include <U2Formats/DocumentFormatUtils.h>
36 #include <U2Formats/GenbankLocationParser.h>
37 
38 #include <U2Gui/DialogUtils.h>
39 #include <U2Gui/HelpButton.h>
40 #include <U2Gui/LastUsedDirHelper.h>
41 #include <U2Gui/SaveDocumentController.h>
42 #include <U2Gui/U2FileDialog.h>
43 
44 #include "ui_RemovePartFromSequenceDialog.h"
45 
46 namespace U2 {
47 
RemovePartFromSequenceDialogController(U2Region _toDelete,U2Region _source,const QString & docUrl,QWidget * p)48 RemovePartFromSequenceDialogController::RemovePartFromSequenceDialogController(U2Region _toDelete,
49                                                                                U2Region _source,
50                                                                                const QString &docUrl,
51                                                                                QWidget *p)
52     : QDialog(p),
53       toDelete(_toDelete),
54       source(_source),
55       ui(new Ui_RemovePartFromSequenceDialog),
56       saveController(nullptr) {
57     ui->setupUi(this);
58     new HelpButton(this, ui->buttonBox, "65929426");
59 
60     ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Remove"));
61     ui->buttonBox->button(QDialogButtonBox::Cancel)->setText(tr("Cancel"));
62 
63     initSaveController(docUrl);
64 
65     SharedAnnotationData ad(new AnnotationData);
66     ad->location->regions << toDelete;
67     ui->removeLocationEdit->setText(U1AnnotationUtils::buildLocationString(ad));
68 
69     connect(ui->mergeAnnotationsBox, SIGNAL(toggled(bool)), this, SLOT(sl_mergeAnnotationsToggled(bool)));
70 }
71 
accept()72 void RemovePartFromSequenceDialogController::accept() {
73     QString genbankRegion = ui->removeLocationEdit->text();
74     U2Location location;
75     Genbank::LocationParser::parseLocation(genbankRegion.toLatin1().constData(), genbankRegion.length(), location);
76     if (location->isMultiRegion()) {
77         QMessageBox::critical(this, this->windowTitle(), tr("There must be only one region to delete"));
78         return;
79     }
80     if (location->isEmpty()) {
81         QMessageBox::critical(this, this->windowTitle(), tr("Unable to parse region to delete"));
82         return;
83     }
84     toDelete = location->regions.first();
85 
86     if (toDelete == source) {
87         QMessageBox::critical(this, this->windowTitle(), tr("Cannot remove the whole sequence"));
88         return;
89     }
90 
91     if (toDelete.startPos < source.startPos || toDelete.endPos() > source.endPos()) {
92         QMessageBox::critical(this, this->windowTitle(), tr("Region to delete is out of sequence bounds"));
93         return;
94     }
95 
96     QDialog::accept();
97 }
98 
getStrategy()99 U1AnnotationUtils::AnnotationStrategyForResize RemovePartFromSequenceDialogController::getStrategy() {
100     if (ui->removeRB->isChecked()) {
101         return U1AnnotationUtils::AnnotationStrategyForResize_Remove;
102     } else {
103         assert(ui->resizeRB->isChecked());
104         return U1AnnotationUtils::AnnotationStrategyForResize_Resize;
105     }
106 }
107 
getRegionToDelete() const108 U2Region RemovePartFromSequenceDialogController::getRegionToDelete() const {
109     return toDelete;
110 }
111 
recalculateQualifiers() const112 bool RemovePartFromSequenceDialogController::recalculateQualifiers() const {
113     return ui->recalculateQualsCheckBox->isChecked();
114 }
115 
sl_mergeAnnotationsToggled(bool)116 void RemovePartFromSequenceDialogController::sl_mergeAnnotationsToggled(bool) {
117     const QString fastaFormatName = DocumentFormatUtils::getFormatNameById(BaseDocumentFormats::FASTA);
118     CHECK(!fastaFormatName.isEmpty(), );
119 
120     if (ui->mergeAnnotationsBox->isChecked()) {
121         ui->formatBox->removeItem(ui->formatBox->findText(fastaFormatName));
122     } else {
123         ui->formatBox->addItem(fastaFormatName);
124     }
125     ui->formatBox->model()->sort(0);
126 }
127 
initSaveController(const QString & docUrl)128 void RemovePartFromSequenceDialogController::initSaveController(const QString &docUrl) {
129     const QFileInfo fi(docUrl);
130 
131     SaveDocumentControllerConfig config;
132     config.defaultFileName = fi.absoluteDir().absolutePath() + "/" + fi.baseName() + "_new" + "." + fi.completeSuffix();
133     config.defaultFormatId = BaseDocumentFormats::FASTA;
134     config.fileDialogButton = ui->browseButton;
135     config.fileNameEdit = ui->filepathEdit;
136     config.formatCombo = ui->formatBox;
137     config.parentWidget = this;
138     config.saveTitle = tr("Select file to save...");
139 
140     const QList<DocumentFormatId> formats = QList<DocumentFormatId>() << BaseDocumentFormats::FASTA
141                                                                       << BaseDocumentFormats::PLAIN_GENBANK;
142 
143     saveController = new SaveDocumentController(config, formats, this);
144 }
145 
modifyCurrentDocument() const146 bool RemovePartFromSequenceDialogController::modifyCurrentDocument() const {
147     return !ui->saveToAnotherBox->isChecked();
148 }
149 
getNewDocumentPath() const150 QString RemovePartFromSequenceDialogController::getNewDocumentPath() const {
151     return saveController->getSaveFileName();
152 }
153 
mergeAnnotations() const154 bool RemovePartFromSequenceDialogController::mergeAnnotations() const {
155     return (ui->mergeAnnotationsBox->isChecked() && !modifyCurrentDocument());
156 }
157 
getDocumentFormatId() const158 DocumentFormatId RemovePartFromSequenceDialogController::getDocumentFormatId() const {
159     return saveController->getFormatIdToSave();
160 }
161 
~RemovePartFromSequenceDialogController()162 RemovePartFromSequenceDialogController::~RemovePartFromSequenceDialogController() {
163     delete ui;
164 }
165 
166 }  // namespace U2
167