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 "ExportConsensusVariationsDialog.h"
23 
24 #include <QMessageBox>
25 #include <QPushButton>
26 
27 #include <U2Algorithm/AssemblyConsensusAlgorithmRegistry.h>
28 
29 #include <U2Core/DocumentModel.h>
30 #include <U2Core/U2OpStatusUtils.h>
31 #include <U2Core/U2SafePoints.h>
32 
33 #include <U2Gui/HelpButton.h>
34 #include <U2Gui/RegionSelector.h>
35 #include <U2Gui/SaveDocumentController.h>
36 
37 namespace U2 {
38 
ExportConsensusVariationsDialog(QWidget * p,const ExportConsensusVariationsTaskSettings & settings_,const U2Region & visibleRegion)39 ExportConsensusVariationsDialog::ExportConsensusVariationsDialog(QWidget *p, const ExportConsensusVariationsTaskSettings &settings_, const U2Region &visibleRegion)
40     : QDialog(p), settings(settings_) {
41     setupUi(this);
42     setWindowTitle(tr("Export Consensus Variations"));
43     new HelpButton(this, buttonBox, "65929848");
44     buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Export"));
45     buttonBox->button(QDialogButtonBox::Cancel)->setText(tr("Cancel"));
46     // hide for this dialog
47     sequenceNameLabel->hide();
48     sequenceNameLineEdit->hide();
49 
50     initSaveController();
51 
52     U2OpStatus2Log os;
53     QList<RegionPreset> presets = QList<RegionPreset>() << RegionPreset(tr("Visible"), visibleRegion);
54     regionSelector = new RegionSelector(this, settings.model->getModelLength(os), false, nullptr, false, presets);
55 
56     int insertPos = verticalLayout->count() - 3;
57     verticalLayout->insertWidget(insertPos, regionSelector);
58 
59     filepathLineEdit->setText(settings.fileName);
60     sequenceNameLineEdit->setText(settings.seqObjName);
61     addToProjectCheckBox->setChecked(settings.addToProject);
62     regionSelector->setCustomRegion(settings.region);
63     keepGapsCheckBox->setChecked(settings.keepGaps);
64 
65     QList<QString> algos = AppContext::getAssemblyConsensusAlgorithmRegistry()->getAlgorithmIds();
66     algorithmComboBox->addItems(algos);
67     algorithmComboBox->setCurrentIndex(algos.indexOf(settings.consensusAlgorithm->getId()));
68 
69     variationModeComboBox->addItem(tr("Variations"), Mode_Variations);
70     variationModeComboBox->addItem(tr("Similar"), Mode_Similar);
71     variationModeComboBox->addItem(tr("All"), Mode_All);
72 
73     QPushButton *okPushButton = buttonBox->button(QDialogButtonBox::Ok);
74     QPushButton *cancelPushButton = buttonBox->button(QDialogButtonBox::Cancel);
75 
76     connect(okPushButton, SIGNAL(clicked()), SLOT(accept()));
77     connect(cancelPushButton, SIGNAL(clicked()), SLOT(reject()));
78     setMaximumHeight(layout()->minimumSize().height());
79 }
80 
accept()81 void ExportConsensusVariationsDialog::accept() {
82     bool isRegionOk;
83     settings.fileName = saveController->getSaveFileName();
84     settings.formatId = saveController->getFormatIdToSave();
85     settings.seqObjName = sequenceNameLineEdit->text();
86     settings.addToProject = addToProjectCheckBox->isChecked();
87     settings.region = regionSelector->getRegion(&isRegionOk);
88     settings.keepGaps = keepGapsCheckBox->isChecked();
89 
90     QString algoId = algorithmComboBox->currentText();
91     if (algoId != settings.consensusAlgorithm->getId()) {
92         AssemblyConsensusAlgorithmFactory *f = AppContext::getAssemblyConsensusAlgorithmRegistry()->getAlgorithmFactory(algoId);
93         SAFE_POINT(f != nullptr, QString("ExportConsensusDialog: consensus algorithm factory %1 not found").arg(algoId), );
94         settings.consensusAlgorithm = QSharedPointer<AssemblyConsensusAlgorithm>(f->createAlgorithm());
95     }
96 
97     int modeIdx = variationModeComboBox->currentIndex();
98     if (modeIdx == -1) {
99         QMessageBox::critical(this, tr("Error!"), tr("Select consensus variation mode"));
100         variationModeComboBox->setFocus(Qt::OtherFocusReason);
101         return;
102     }
103     settings.mode = static_cast<CallVariationsMode>(variationModeComboBox->itemData(modeIdx).toInt());
104 
105     if (!isRegionOk) {
106         regionSelector->showErrorMessage();
107         regionSelector->setFocus(Qt::OtherFocusReason);
108         return;
109     }
110     // TODO: check if exists
111     if (settings.fileName.isEmpty()) {
112         QMessageBox::critical(this, tr("Error!"), tr("Select destination file"));
113         filepathLineEdit->setFocus(Qt::OtherFocusReason);
114         return;
115     }
116     if (settings.seqObjName.isEmpty()) {
117         QMessageBox::critical(this, tr("Error!"), tr("Sequence name cannot be empty"));
118         sequenceNameLineEdit->setFocus(Qt::OtherFocusReason);
119         return;
120     }
121 
122     QDialog::accept();
123 }
124 
getSettings() const125 const ExportConsensusVariationsTaskSettings &ExportConsensusVariationsDialog::getSettings() const {
126     return settings;
127 }
128 
initSaveController()129 void ExportConsensusVariationsDialog::initSaveController() {
130     SaveDocumentControllerConfig config;
131     config.defaultFileName = settings.fileName;
132     config.defaultFormatId = settings.formatId;
133     config.fileDialogButton = filepathToolButton;
134     config.fileNameEdit = filepathLineEdit;
135     config.formatCombo = documentFormatComboBox;
136     config.parentWidget = this;
137     config.saveTitle = tr("Export Consensus Variations");
138 
139     DocumentFormatConstraints formatConstraints;
140     formatConstraints.supportedObjectTypes << GObjectTypes::VARIANT_TRACK;
141     formatConstraints.addFlagToSupport(DocumentFormatFlag_SupportWriting);
142     formatConstraints.addFlagToExclude(DocumentFormatFlag_SingleObjectFormat);
143 
144     saveController = new SaveDocumentController(config, formatConstraints, this);
145 }
146 
147 }  // namespace U2
148