File indexing completed on 2024-05-12 15:27:51
0001 /*************************************************************************** 0002 File : ImportSQLDatabaseDialog.cpp 0003 Project : LabPlot 0004 Description : import SQL dataase dialog 0005 -------------------------------------------------------------------- 0006 Copyright : (C) 2016 by Ankit Wagadre (wagadre.ankit@gmail.com) 0007 Copyright : (C) 2016-2017 Alexander Semke (alexander.semke@web.de) 0008 0009 ***************************************************************************/ 0010 0011 /*************************************************************************** 0012 * * 0013 * This program is free software; you can redistribute it and/or modify * 0014 * it under the terms of the GNU General Public License as published by * 0015 * the Free Software Foundation; either version 2 of the License, or * 0016 * (at your option) any later version. * 0017 * * 0018 * This program is distributed in the hope that it will be useful, * 0019 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0021 * GNU General Public License for more details. * 0022 * * 0023 * You should have received a copy of the GNU General Public License * 0024 * along with this program; if not, write to the Free Software * 0025 * Foundation, Inc., 51 Franklin Street, Fifth Floor, * 0026 * Boston, MA 02110-1301 USA * 0027 * * 0028 ***************************************************************************/ 0029 0030 #include "ImportSQLDatabaseDialog.h" 0031 #include "ImportSQLDatabaseWidget.h" 0032 #include "backend/core/AspectTreeModel.h" 0033 #include "backend/lib/macros.h" 0034 #include "kdefrontend/MainWin.h" 0035 #include "backend/spreadsheet/Spreadsheet.h" 0036 #include "backend/matrix/Matrix.h" 0037 #include "backend/core/Workbook.h" 0038 #include "commonfrontend/widgets/TreeViewComboBox.h" 0039 0040 #include <QDialogButtonBox> 0041 #include <QElapsedTimer> 0042 #include <QProgressBar> 0043 #include <QStatusBar> 0044 #include <QWindow> 0045 0046 #include <KLocalizedString> 0047 #include <KSharedConfig> 0048 #include <KWindowConfig> 0049 0050 /*! 0051 \class ImportSQLDatabaseDialog 0052 \brief Dialog for importing data from a SQL database. Embeds \c ImportSQLDatabaseWidget and provides the standard buttons. 0053 0054 \ingroup kdefrontend 0055 */ 0056 ImportSQLDatabaseDialog::ImportSQLDatabaseDialog(MainWin* parent) : ImportDialog(parent), 0057 importSQLDatabaseWidget(new ImportSQLDatabaseWidget(this)) { 0058 0059 vLayout->addWidget(importSQLDatabaseWidget); 0060 0061 setWindowTitle(i18nc("@title:window", "Import Data to Spreadsheet or Matrix")); 0062 setWindowIcon(QIcon::fromTheme("document-import-database")); 0063 setModel(); 0064 0065 //dialog buttons 0066 auto* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); 0067 okButton = buttonBox->button(QDialogButtonBox::Ok); 0068 okButton->setEnabled(false); //ok is only available if a valid container was selected 0069 vLayout->addWidget(buttonBox); 0070 0071 //Signals/Slots 0072 connect(importSQLDatabaseWidget, &ImportSQLDatabaseWidget::stateChanged, this, &ImportSQLDatabaseDialog::checkOkButton); 0073 connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); 0074 connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 0075 0076 //restore saved settings if available 0077 create(); // ensure there's a window created 0078 KConfigGroup conf(KSharedConfig::openConfig(), "ImportSQLDatabaseDialog"); 0079 if (conf.exists()) { 0080 KWindowConfig::restoreWindowSize(windowHandle(), conf); 0081 resize(windowHandle()->size()); // workaround for QTBUG-40584 0082 } else 0083 resize(QSize(0, 0).expandedTo(minimumSize())); 0084 } 0085 0086 ImportSQLDatabaseDialog::~ImportSQLDatabaseDialog() { 0087 //save current settings 0088 KConfigGroup conf(KSharedConfig::openConfig(), "ImportSQLDatabaseDialog"); 0089 KWindowConfig::saveWindowSize(windowHandle(), conf); 0090 } 0091 0092 void ImportSQLDatabaseDialog::importTo(QStatusBar* statusBar) const { 0093 DEBUG("ImportSQLDatabaseDialog::import()"); 0094 AbstractAspect* aspect = static_cast<AbstractAspect*>(cbAddTo->currentModelIndex().internalPointer()); 0095 if (!aspect) { 0096 DEBUG("ERROR: No aspect available!"); 0097 return; 0098 } 0099 0100 const auto mode = AbstractFileFilter::ImportMode(cbPosition->currentIndex()); 0101 0102 //show a progress bar in the status bar 0103 auto* progressBar = new QProgressBar(); 0104 progressBar->setMinimum(0); 0105 progressBar->setMaximum(100); 0106 connect(importSQLDatabaseWidget, &ImportSQLDatabaseWidget::completed, progressBar, &QProgressBar::setValue); 0107 0108 statusBar->clearMessage(); 0109 statusBar->addWidget(progressBar, 1); 0110 0111 WAIT_CURSOR; 0112 QApplication::processEvents(QEventLoop::AllEvents, 100); 0113 0114 QElapsedTimer timer; 0115 timer.start(); 0116 if (aspect->inherits(AspectType::Matrix)) { 0117 auto* matrix = qobject_cast<Matrix*>(aspect); 0118 importSQLDatabaseWidget->read(matrix, mode); 0119 } else if (aspect->inherits(AspectType::Spreadsheet)) { 0120 auto* spreadsheet = qobject_cast<Spreadsheet*>(aspect); 0121 importSQLDatabaseWidget->read(spreadsheet, mode); 0122 } else if (aspect->inherits(AspectType::Workbook)) { 0123 // use active spreadsheet or matrix (only if numeric data is going to be imported) if present, 0124 // create a new spreadsheet in the selected workbook otherwise 0125 auto* workbook = qobject_cast<Workbook*>(aspect); 0126 Spreadsheet* spreadsheet = workbook->currentSpreadsheet(); 0127 Matrix* matrix = workbook->currentMatrix(); 0128 if (spreadsheet) 0129 importSQLDatabaseWidget->read(spreadsheet, mode); 0130 else if (matrix && importSQLDatabaseWidget->isNumericData()) 0131 importSQLDatabaseWidget->read(matrix, mode); 0132 else { 0133 spreadsheet = new Spreadsheet(i18n("Spreadsheet")); 0134 workbook->addChild(spreadsheet); 0135 importSQLDatabaseWidget->read(spreadsheet, mode); 0136 } 0137 } 0138 statusBar->showMessage( i18n("Data imported in %1 seconds.", (float)timer.elapsed()/1000) ); 0139 0140 RESET_CURSOR; 0141 statusBar->removeWidget(progressBar); 0142 } 0143 0144 QString ImportSQLDatabaseDialog::selectedObject() const { 0145 return importSQLDatabaseWidget->selectedTable(); 0146 } 0147 0148 void ImportSQLDatabaseDialog::checkOkButton() { 0149 DEBUG("ImportSQLDatabaseDialog::checkOkButton()"); 0150 0151 AbstractAspect* aspect = static_cast<AbstractAspect*>(cbAddTo->currentModelIndex().internalPointer()); 0152 if (!aspect) { 0153 okButton->setEnabled(false); 0154 okButton->setToolTip(i18n("Select a data container where the data has to be imported into.")); 0155 cbPosition->setEnabled(false); 0156 return; 0157 } 0158 0159 //check whether a valid connection and an object to import were selected 0160 if (!importSQLDatabaseWidget->isValid()) { 0161 okButton->setEnabled(false); 0162 okButton->setToolTip(i18n("Select a valid database object (table or query result set) that has to be imported.")); 0163 cbPosition->setEnabled(false); 0164 return; 0165 } 0166 0167 //for matrix containers allow to import only numerical data 0168 if (dynamic_cast<const Matrix*>(aspect) && !importSQLDatabaseWidget->isNumericData()) { 0169 okButton->setEnabled(false); 0170 okButton->setToolTip(i18n("Cannot import into a matrix since the data contains non-numerical data.")); 0171 cbPosition->setEnabled(false); 0172 return; 0173 } 0174 0175 okButton->setEnabled(true); 0176 okButton->setToolTip(i18n("Close the dialog and import the data.")); 0177 cbPosition->setEnabled(true); 0178 }