diff --git a/src/kdefrontend/datasources/ImportDialog.cpp b/src/kdefrontend/datasources/ImportDialog.cpp index 7fbbbf2a8..50ab279eb 100644 --- a/src/kdefrontend/datasources/ImportDialog.cpp +++ b/src/kdefrontend/datasources/ImportDialog.cpp @@ -1,187 +1,185 @@ /*************************************************************************** File : ImportDialog.cc Project : LabPlot Description : import file data dialog -------------------------------------------------------------------- Copyright : (C) 2008-2017 Alexander Semke (alexander.semke@web.de) Copyright : (C) 2008-2015 by Stefan Gerlach (stefan.gerlach@uni.kn) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program 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 program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #include "ImportDialog.h" #include "backend/core/AspectTreeModel.h" #include "backend/core/Project.h" #include "backend/spreadsheet/Spreadsheet.h" #include "backend/matrix/Matrix.h" #include "backend/core/Workbook.h" #include "backend/lib/macros.h" #include "commonfrontend/widgets/TreeViewComboBox.h" #include "kdefrontend/MainWin.h" #include #include #include #include #include #include #include #include #include #include +#include #include #include #include /*! \class ImportDialog \brief Base class for other import dialogs. Provides the "Import to" section of those dialogs. \ingroup kdefrontend */ -ImportDialog::ImportDialog(MainWin* parent) : KDialog(parent), - cbAddTo(0), cbPosition(0), m_mainWin(parent), m_newDataContainerMenu(0), - m_aspectTreeModel(new AspectTreeModel(parent->project()) ) { - - QWidget* mainWidget = new QWidget(this); - vLayout = new QVBoxLayout(mainWidget); - vLayout->setSpacing(0); - vLayout->setContentsMargins(0,0,0,0); - setMainWidget(mainWidget); +ImportDialog::ImportDialog(MainWin* parent) : QDialog(parent), + vLayout(new QVBoxLayout(this)), + okButton(nullptr), + cbPosition(nullptr), + cbAddTo(nullptr), + m_mainWin(parent), + m_newDataContainerMenu(nullptr), + m_aspectTreeModel(new AspectTreeModel(parent->project())) { //menu for new data container m_newDataContainerMenu = new QMenu(this); m_newDataContainerMenu->addAction( QIcon::fromTheme("labplot-workbook-new"), i18n("new Workbook") ); m_newDataContainerMenu->addAction( QIcon::fromTheme("labplot-spreadsheet-new"), i18n("new Spreadsheet") ); m_newDataContainerMenu->addAction( QIcon::fromTheme("labplot-matrix-new"), i18n("new Matrix") ); connect(m_newDataContainerMenu, SIGNAL(triggered(QAction*)), this, SLOT(newDataContainer(QAction*))); - - //ok is only available if a valid container was selected - enableButtonOk(false); } ImportDialog::~ImportDialog() { if (m_aspectTreeModel) delete m_aspectTreeModel; //save the last used import position for file imports, no need to do this for live data source (cbPosition=0) if (cbPosition) { KConfigGroup conf(KSharedConfig::openConfig(), "ImportDialog"); conf.writeEntry("Position", cbPosition->currentIndex()); } } /*! creates widgets for the frame "Import-To" and sets the current model in the "Add to"-combobox. */ void ImportDialog::setModel() { //Frame for the "Import To"-Stuff frameAddTo = new QGroupBox(this); frameAddTo->setTitle(i18n("Import To")); QGridLayout *grid = new QGridLayout(frameAddTo); grid->addWidget(new QLabel(i18n("Data container"), frameAddTo), 0, 0); cbAddTo = new TreeViewComboBox(frameAddTo); cbAddTo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); grid->addWidget(cbAddTo, 0, 1); QList list; list << "Folder" << "Spreadsheet" << "Matrix" << "Workbook"; cbAddTo->setTopLevelClasses(list); list.clear(); list << "Spreadsheet" << "Matrix" << "Workbook"; m_aspectTreeModel->setSelectableAspects(list); cbAddTo->setModel(m_aspectTreeModel); tbNewDataContainer = new QToolButton(frameAddTo); tbNewDataContainer->setIcon(QIcon::fromTheme("list-add")); tbNewDataContainer->setToolTip(i18n("Add new data container")); grid->addWidget( tbNewDataContainer, 0, 2); lPosition = new QLabel(i18n("Position"), frameAddTo); lPosition->setEnabled(false); grid->addWidget(lPosition, 1, 0); cbPosition = new QComboBox(frameAddTo); cbPosition->setEnabled(false); cbPosition->addItem(i18n("Append")); cbPosition->addItem(i18n("Prepend")); cbPosition->addItem(i18n("Replace")); KConfigGroup conf(KSharedConfig::openConfig(), "ImportDialog"); cbPosition->setCurrentIndex(conf.readEntry("Position", 0)); cbPosition->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); grid->addWidget(cbPosition, 1, 1); - vLayout->addWidget(frameAddTo); + //add the "Import to"-frame to the layout after the first main widget + vLayout->insertWidget(1, frameAddTo); connect(tbNewDataContainer, SIGNAL(clicked(bool)), this, SLOT(newDataContainerMenu())); connect(cbAddTo, SIGNAL(currentModelIndexChanged(QModelIndex)), this, SLOT(checkOkButton())); } void ImportDialog::setCurrentIndex(const QModelIndex& index) { DEBUG("ImportFileDialog::setCurrentIndex()"); QDEBUG(" index =" << index); cbAddTo->setCurrentModelIndex(index); QDEBUG("cbAddTo->currentModelIndex() =" << cbAddTo->currentModelIndex()); checkOkButton(); } void ImportDialog::newDataContainer(QAction* action) { DEBUG("ImportDialog::newDataContainer()"); QString name = selectedObject(); QString type = action->iconText().split(' ')[1]; if (name.isEmpty()) name = action->iconText(); bool ok; // child widgets can't have own icons QInputDialog* dlg = new QInputDialog(this); name = dlg->getText(this, i18n("Add %1", action->iconText()), i18n("%1 name:", type), QLineEdit::Normal, name, &ok); if (ok) { AbstractAspect* aspect; int actionIndex = m_newDataContainerMenu->actions().indexOf(action); if (actionIndex == 0) aspect = new Workbook(0, name); else if (actionIndex == 1) aspect = new Spreadsheet(0, name); else aspect = new Matrix(0, name); m_mainWin->addAspectToProject(aspect); QDEBUG("cbAddTo->setCurrentModelIndex() to " << m_mainWin->model()->modelIndexOfAspect(aspect)); cbAddTo->setCurrentModelIndex(m_mainWin->model()->modelIndexOfAspect(aspect)); checkOkButton(); //select "Replace" since this is the most common case when importing into a newly created container cbPosition->setCurrentIndex(2); } delete dlg; } void ImportDialog::newDataContainerMenu() { m_newDataContainerMenu->exec( tbNewDataContainer->mapToGlobal(tbNewDataContainer->rect().bottomLeft())); } diff --git a/src/kdefrontend/datasources/ImportDialog.h b/src/kdefrontend/datasources/ImportDialog.h index 677eb8597..aa491e90f 100644 --- a/src/kdefrontend/datasources/ImportDialog.h +++ b/src/kdefrontend/datasources/ImportDialog.h @@ -1,81 +1,82 @@ /*************************************************************************** File : ImportDialog.h Project : LabPlot Description : import data dialog -------------------------------------------------------------------- Copyright : (C) 2016-2017 Alexander Semke (alexander.semke@web.de) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program 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 program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #ifndef IMPORTDIALOG_H #define IMPORTDIALOG_H -#include +#include class AbstractAspect; class AspectTreeModel; class MainWin; class TreeViewComboBox; class QMenu; class QAbstractItemModel; class QLabel; class QModelIndex; class QVBoxLayout; class QComboBox; class QGroupBox; class QToolButton; class QStatusBar; -class ImportDialog : public KDialog { +class ImportDialog : public QDialog { Q_OBJECT public: explicit ImportDialog(MainWin*); ~ImportDialog(); virtual void importTo(QStatusBar*) const = 0; void setCurrentIndex(const QModelIndex&); virtual QString selectedObject() const = 0; protected: void setModel(); QVBoxLayout* vLayout; - TreeViewComboBox* cbAddTo; + QPushButton* okButton; QLabel* lPosition; QComboBox* cbPosition; + TreeViewComboBox* cbAddTo; MainWin* m_mainWin; QGroupBox* frameAddTo; QToolButton* tbNewDataContainer; QMenu* m_newDataContainerMenu; AspectTreeModel* m_aspectTreeModel; protected slots: virtual void checkOkButton() = 0; private slots: void newDataContainerMenu(); void newDataContainer(QAction*); }; #endif //IMPORTDIALOG_H diff --git a/src/kdefrontend/datasources/ImportFileDialog.cpp b/src/kdefrontend/datasources/ImportFileDialog.cpp index 46bccdc5a..8d3a64815 100644 --- a/src/kdefrontend/datasources/ImportFileDialog.cpp +++ b/src/kdefrontend/datasources/ImportFileDialog.cpp @@ -1,406 +1,414 @@ /*************************************************************************** File : ImportDialog.cc Project : LabPlot Description : import file data dialog -------------------------------------------------------------------- Copyright : (C) 2008-2017 Alexander Semke (alexander.semke@web.de) Copyright : (C) 2008-2015 by Stefan Gerlach (stefan.gerlach@uni.kn) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program 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 program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #include "ImportFileDialog.h" #include "ImportFileWidget.h" #include "backend/core/AspectTreeModel.h" #include "backend/datasources/LiveDataSource.h" #include "backend/datasources/filters/AbstractFileFilter.h" #include "backend/datasources/filters/HDFFilter.h" #include "backend/datasources/filters/NetCDFFilter.h" #include "backend/spreadsheet/Spreadsheet.h" #include "backend/matrix/Matrix.h" #include "backend/core/Workbook.h" #include "commonfrontend/widgets/TreeViewComboBox.h" #include "kdefrontend/MainWin.h" #include #include #include #include #include +#include #include #include #include #include #include #include #include #include /*! \class ImportFileDialog \brief Dialog for importing data from a file. Embeds \c ImportFileWidget and provides the standard buttons. \ingroup kdefrontend */ ImportFileDialog::ImportFileDialog(MainWin* parent, bool liveDataSource, const QString& fileName) : ImportDialog(parent), m_importFileWidget(new ImportFileWidget(this, fileName)), m_showOptions(false) { vLayout->addWidget(m_importFileWidget); - setButtons(KDialog::Ok | KDialog::User1 | KDialog::Cancel); + + //dialog buttons + QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Reset |QDialogButtonBox::Cancel); + okButton = buttonBox->button(QDialogButtonBox::Ok); + m_optionsButton = buttonBox->button(QDialogButtonBox::Reset); //we highjack the default "Reset" button and use if for showing/hiding the options + okButton->setEnabled(false); //ok is only available if a valid container was selected + vLayout->addWidget(buttonBox); //hide the data-source related widgets if (!liveDataSource) { setModel(); //TODO: disable for file data sources m_importFileWidget->hideDataSource(); } else m_importFileWidget->initializeAndFillPortsAndBaudRates(); - connect(this, SIGNAL(user1Clicked()), this, SLOT(toggleOptions())); + //Signals/Slots connect(m_importFileWidget, SIGNAL(checkedFitsTableToMatrix(bool)), this, SLOT(checkOnFitsTableToMatrix(bool))); - connect(m_importFileWidget, SIGNAL(fileNameChanged()), this, SLOT(checkOkButton())); connect(m_importFileWidget, SIGNAL(sourceTypeChanged()), this, SLOT(checkOkButton())); connect(m_importFileWidget, SIGNAL(hostChanged()), this, SLOT(checkOkButton())); connect(m_importFileWidget, SIGNAL(portChanged()), this, SLOT(checkOkButton())); + connect(m_optionsButton, SIGNAL(clicked()), this, SLOT(toggleOptions())); + connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); + connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); if (!liveDataSource) { - setCaption(i18n("Import Data to Spreadsheet or Matrix")); + setWindowTitle(i18n("Import Data to Spreadsheet or Matrix")); m_importFileWidget->hideDataSource(); } else - setCaption(i18n("Add new live data source")); + setWindowTitle(i18n("Add new live data source")); setWindowIcon(QIcon::fromTheme("document-import-database")); QTimer::singleShot(0, this, &ImportFileDialog::loadSettings); } void ImportFileDialog::loadSettings() { //restore saved settings QApplication::processEvents(QEventLoop::AllEvents, 0); KConfigGroup conf(KSharedConfig::openConfig(), "ImportFileDialog"); m_showOptions = conf.readEntry("ShowOptions", false); - m_showOptions ? setButtonText(KDialog::User1, i18n("Hide Options")) : setButtonText(KDialog::User1, i18n("Show Options")); + m_showOptions ? m_optionsButton->setText(i18n("Hide Options")) : m_optionsButton->setText(i18n("Show Options")); m_importFileWidget->showOptions(m_showOptions); KWindowConfig::restoreWindowSize(windowHandle(), conf); } ImportFileDialog::~ImportFileDialog() { //save current settings KConfigGroup conf(KSharedConfig::openConfig(), "ImportFileDialog"); conf.writeEntry("ShowOptions", m_showOptions); if (cbPosition) conf.writeEntry("Position", cbPosition->currentIndex()); KWindowConfig::saveWindowSize(windowHandle(), conf); } /*! triggers data import to the live data source \c source */ void ImportFileDialog::importToLiveDataSource(LiveDataSource* source, QStatusBar* statusBar) const { m_importFileWidget->saveSettings(source); //show a progress bar in the status bar QProgressBar* progressBar = new QProgressBar(); progressBar->setRange(0, 100); connect(source->filter(), SIGNAL(completed(int)), progressBar, SLOT(setValue(int))); statusBar->clearMessage(); statusBar->addWidget(progressBar, 1); WAIT_CURSOR; QTime timer; timer.start(); source->read(); statusBar->showMessage( i18n("Live data source created in %1 seconds.", (float)timer.elapsed()/1000) ); RESET_CURSOR; statusBar->removeWidget(progressBar); source->ready(); } /*! triggers data import to the currently selected data container */ void ImportFileDialog::importTo(QStatusBar* statusBar) const { DEBUG("ImportFileDialog::importTo()"); QDEBUG("cbAddTo->currentModelIndex() =" << cbAddTo->currentModelIndex()); AbstractAspect* aspect = static_cast(cbAddTo->currentModelIndex().internalPointer()); if (!aspect) { DEBUG("ERROR in importTo(): No aspect available"); DEBUG("cbAddTo->currentModelIndex().isValid() = " << cbAddTo->currentModelIndex().isValid()); DEBUG("cbAddTo->currentModelIndex() row/column = " << cbAddTo->currentModelIndex().row() << ' ' << cbAddTo->currentModelIndex().column()); return; } if (m_importFileWidget->isFileEmpty()) { KMessageBox::information(0, i18n("No data to import."), i18n("No Data")); return; } QString fileName = m_importFileWidget->fileName(); AbstractFileFilter* filter = m_importFileWidget->currentFileFilter(); AbstractFileFilter::ImportMode mode = AbstractFileFilter::ImportMode(cbPosition->currentIndex()); //show a progress bar in the status bar QProgressBar* progressBar = new QProgressBar(); progressBar->setRange(0, 100); connect(filter, SIGNAL(completed(int)), progressBar, SLOT(setValue(int))); statusBar->clearMessage(); statusBar->addWidget(progressBar, 1); WAIT_CURSOR; QApplication::processEvents(QEventLoop::AllEvents, 100); QTime timer; timer.start(); if (aspect->inherits("Matrix")) { Matrix* matrix = qobject_cast(aspect); filter->readDataFromFile(fileName, matrix, mode); } else if (aspect->inherits("Spreadsheet")) { Spreadsheet* spreadsheet = qobject_cast(aspect); filter->readDataFromFile(fileName, spreadsheet, mode); } else if (aspect->inherits("Workbook")) { Workbook* workbook = qobject_cast(aspect); QVector sheets = workbook->children(); QStringList names; LiveDataSource::FileType fileType = m_importFileWidget->currentFileType(); if (fileType == LiveDataSource::HDF) names = m_importFileWidget->selectedHDFNames(); else if (fileType == LiveDataSource::NETCDF) names = m_importFileWidget->selectedNetCDFNames(); //multiple extensions selected // multiple data sets/variables for HDF/NetCDF if (fileType == LiveDataSource::HDF || fileType == LiveDataSource::NETCDF) { int nrNames = names.size(), offset = sheets.size(); int start=0; if (mode == AbstractFileFilter::Replace) start=offset; // add additional sheets for (int i = start; i < nrNames; ++i) { Spreadsheet *spreadsheet = new Spreadsheet(0, i18n("Spreadsheet")); if (mode == AbstractFileFilter::Prepend) workbook->insertChildBefore(spreadsheet,sheets[0]); else workbook->addChild(spreadsheet); } if (mode != AbstractFileFilter::Append) offset = 0; // import to sheets sheets = workbook->children(); for (int i = 0; i < nrNames; ++i) { if (fileType == LiveDataSource::HDF) ((HDFFilter*) filter)->setCurrentDataSetName(names[i]); else ((NetCDFFilter*) filter)->setCurrentVarName(names[i]); if (sheets[i+offset]->inherits("Matrix")) filter->readDataFromFile(fileName, qobject_cast(sheets[i+offset])); else if (sheets[i+offset]->inherits("Spreadsheet")) filter->readDataFromFile(fileName, qobject_cast(sheets[i+offset])); } } else { // single import file types // use active spreadsheet/matrix if present, else new spreadsheet Spreadsheet* spreadsheet = workbook->currentSpreadsheet(); Matrix* matrix = workbook->currentMatrix(); if (spreadsheet) filter->readDataFromFile(fileName, spreadsheet, mode); else if (matrix) filter->readDataFromFile(fileName, matrix, mode); else { spreadsheet = new Spreadsheet(0, i18n("Spreadsheet")); workbook->addChild(spreadsheet); filter->readDataFromFile(fileName, spreadsheet, mode); } } } statusBar->showMessage( i18n("File %1 imported in %2 seconds.", fileName, (float)timer.elapsed()/1000) ); RESET_CURSOR; statusBar->removeWidget(progressBar); delete filter; } void ImportFileDialog::toggleOptions() { m_importFileWidget->showOptions(!m_showOptions); m_showOptions = !m_showOptions; - m_showOptions ? setButtonText(KDialog::User1,i18n("Hide Options")) : setButtonText(KDialog::User1,i18n("Show Options")); + m_showOptions ? m_optionsButton->setText(i18n("Hide Options")) : m_optionsButton->setText(i18n("Show Options")); //resize the dialog - mainWidget()->resize(layout()->minimumSize()); layout()->activate(); resize( QSize(this->width(), 0).expandedTo(minimumSize()) ); } void ImportFileDialog::checkOnFitsTableToMatrix(const bool enable) { if (cbAddTo) { QDEBUG("cbAddTo->currentModelIndex() = " << cbAddTo->currentModelIndex()); AbstractAspect* aspect = static_cast(cbAddTo->currentModelIndex().internalPointer()); if (!aspect) { DEBUG("ERROR: no aspect available."); return; } if(aspect->inherits("Matrix")) - enableButtonOk(enable); + okButton->setEnabled(enable); } } void ImportFileDialog::checkOkButton() { DEBUG("ImportFileDialog::checkOkButton()"); if (cbAddTo) { //only check for the target container when no file data source is being added QDEBUG(" cbAddTo->currentModelIndex() = " << cbAddTo->currentModelIndex()); AbstractAspect* aspect = static_cast(cbAddTo->currentModelIndex().internalPointer()); if (!aspect) { - enableButtonOk(false); + okButton->setEnabled(false); lPosition->setEnabled(false); cbPosition->setEnabled(false); DEBUG("WARNING: no aspect available."); return; } else { DEBUG("Aspect available."); lPosition->setEnabled(true); cbPosition->setEnabled(true); //when doing ASCII import to a matrix, hide the options for using the file header (first line) //to name the columns since the column names are fixed in a matrix const Matrix* matrix = dynamic_cast(aspect); m_importFileWidget->showAsciiHeaderOptions(matrix == NULL); } } QString fileName = m_importFileWidget->fileName(); #ifndef HAVE_WINDOWS if (!fileName.isEmpty() && fileName.left(1) != QDir::separator()) fileName = QDir::homePath() + QDir::separator() + fileName; #endif DEBUG(" fileName = " << fileName.toUtf8().constData()); bool enable = !m_importFileWidget->host().isEmpty() && !m_importFileWidget->port().isEmpty(); switch (m_importFileWidget->currentSourceType()) { case LiveDataSource::SourceType::FileOrPipe: - enableButtonOk( QFile::exists(fileName) ); + okButton->setEnabled( QFile::exists(fileName) ); break; case LiveDataSource::SourceType::LocalSocket: if (QFile::exists(fileName)) { QLocalSocket* socket = new QLocalSocket(this); socket->connectToServer(fileName, QLocalSocket::ReadOnly); bool localSocketConnected = socket->waitForConnected(2000); - enableButtonOk(localSocketConnected); + okButton->setEnabled(localSocketConnected); if (socket->state() == QLocalSocket::ConnectedState) { socket->disconnectFromServer(); socket->waitForDisconnected(1000); connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater())); } else { delete socket; } } else { - enableButtonOk(false); + okButton->setEnabled(false); } break; case LiveDataSource::SourceType::NetworkTcpSocket: if (enable) { QTcpSocket* socket = new QTcpSocket(this); socket = new QTcpSocket(this); socket->connectToHost(m_importFileWidget->host(), m_importFileWidget->port().toInt(), QTcpSocket::ReadOnly); bool tcpSocketConnected = socket->waitForConnected(2000); - enableButtonOk(tcpSocketConnected); + okButton->setEnabled(tcpSocketConnected); if (socket->state() == QTcpSocket::ConnectedState) { socket->disconnectFromHost(); socket->waitForDisconnected(1000); connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater())); } else { delete socket; } } else { - enableButtonOk(false); + okButton->setEnabled(false); } break; case LiveDataSource::SourceType::NetworkUdpSocket: if (enable) { QUdpSocket* socket = new QUdpSocket(this); socket->connectToHost(m_importFileWidget->host(), m_importFileWidget->port().toInt(), QUdpSocket::ReadOnly); bool udpSocketConnected = socket->waitForConnected(2000); - enableButtonOk(udpSocketConnected); + okButton->setEnabled(udpSocketConnected); if (socket->state() == QUdpSocket::ConnectedState) { socket->disconnectFromHost(); socket->waitForDisconnected(1000); connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater())); } else { delete socket; } } else { - enableButtonOk(false); + okButton->setEnabled(false); } break; case LiveDataSource::SourceType::SerialPort: if (!m_importFileWidget->serialPort().isEmpty()) { QSerialPort* serialPort = new QSerialPort(this); serialPort->setBaudRate(m_importFileWidget->baudRate()); serialPort->setPortName(m_importFileWidget->serialPort()); bool serialPortOpened = serialPort->open(QIODevice::ReadOnly); - enableButtonOk(serialPortOpened); + okButton->setEnabled(serialPortOpened); } else { - enableButtonOk(false); + okButton->setEnabled(false); } break; default: break; } } QString ImportFileDialog::selectedObject() const { QString path = m_importFileWidget->fileName(); //determine the file name only QString name = path.right( path.length()-path.lastIndexOf(QDir::separator())-1 ); //strip away the extension if available if (name.indexOf('.') != -1) name = name.left(name.lastIndexOf('.')); return name; } diff --git a/src/kdefrontend/datasources/ImportFileDialog.h b/src/kdefrontend/datasources/ImportFileDialog.h index 899f1e084..f4ee47fc8 100644 --- a/src/kdefrontend/datasources/ImportFileDialog.h +++ b/src/kdefrontend/datasources/ImportFileDialog.h @@ -1,69 +1,69 @@ /*************************************************************************** File : ImportFileDialog.h Project : LabPlot Description : import data dialog -------------------------------------------------------------------- Copyright : (C) 2008-2017 Alexander Semke (alexander.semke@web.de) Copyright : (C) 2008-2015 by Stefan Gerlach (stefan.gerlach@uni.kn) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program 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 program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #ifndef IMPORTFILEDIALOG_H #define IMPORTFILEDIALOG_H #include "ImportDialog.h" -class AbstractAspect; class MainWin; class ImportFileWidget; class LiveDataSource; -class TreeViewComboBox; class QStatusBar; class QMenu; class ImportFileDialog : public ImportDialog { Q_OBJECT public: explicit ImportFileDialog(MainWin*, bool liveDataSource = false, const QString& fileName = QString()); ~ImportFileDialog(); virtual QString selectedObject() const; void importToLiveDataSource(LiveDataSource*, QStatusBar*) const; virtual void importTo(QStatusBar*) const; private: - ImportFileWidget* m_importFileWidget; bool m_showOptions; QMenu* m_newDataContainerMenu; + QPushButton* m_optionsButton; + protected slots: virtual void checkOkButton(); + private slots: void toggleOptions(); void checkOnFitsTableToMatrix(const bool enable); void loadSettings(); }; #endif //IMPORTFILEDIALOG_H diff --git a/src/kdefrontend/datasources/ImportSQLDatabaseDialog.cpp b/src/kdefrontend/datasources/ImportSQLDatabaseDialog.cpp index 3ff06c436..5b728c1c5 100644 --- a/src/kdefrontend/datasources/ImportSQLDatabaseDialog.cpp +++ b/src/kdefrontend/datasources/ImportSQLDatabaseDialog.cpp @@ -1,163 +1,171 @@ /*************************************************************************** File : ImportSQLDatabaseDialog.cpp Project : LabPlot Description : import SQL dataase dialog -------------------------------------------------------------------- Copyright : (C) 2016 by Ankit Wagadre (wagadre.ankit@gmail.com) Copyright : (C) 2016-2017 Alexander Semke (alexander.semke@web.de) ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program 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 program; if not, write to the Free Software * * Foundation, Inc., 51 Franklin Street, Fifth Floor, * * Boston, MA 02110-1301 USA * * * ***************************************************************************/ #include "ImportSQLDatabaseDialog.h" #include "ImportSQLDatabaseWidget.h" #include "backend/core/AspectTreeModel.h" #include "backend/lib/macros.h" #include "kdefrontend/MainWin.h" #include "backend/spreadsheet/Spreadsheet.h" #include "backend/matrix/Matrix.h" #include "backend/core/Workbook.h" #include "commonfrontend/widgets/TreeViewComboBox.h" +#include #include #include #include #include /*! \class ImportSQLDatabaseDialog \brief Dialog for importing data from a SQL database. Embeds \c ImportSQLDatabaseWidget and provides the standard buttons. \ingroup kdefrontend */ ImportSQLDatabaseDialog::ImportSQLDatabaseDialog(MainWin* parent) : ImportDialog(parent), importSQLDatabaseWidget(new ImportSQLDatabaseWidget(this)) { vLayout->addWidget(importSQLDatabaseWidget); - setButtons( KDialog::Ok | KDialog::Cancel ); - setCaption(i18n("Import Data to Spreadsheet or Matrix")); + setWindowTitle(i18n("Import Data to Spreadsheet or Matrix")); setWindowIcon(QIcon::fromTheme("document-import-database")); - setModel(); + //dialog buttons + QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); + okButton = buttonBox->button(QDialogButtonBox::Ok); + okButton->setEnabled(false); //ok is only available if a valid container was selected + vLayout->addWidget(buttonBox); + + //Signals/Slots connect(importSQLDatabaseWidget, SIGNAL(stateChanged()), this, SLOT(checkOkButton())); + connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); + connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); QTimer::singleShot(0, this, &ImportSQLDatabaseDialog::loadSettings); } void ImportSQLDatabaseDialog::loadSettings() { //restore saved settings QApplication::processEvents(QEventLoop::AllEvents, 0); KConfigGroup conf(KSharedConfig::openConfig(), "ImportSQLDatabaseDialog"); KWindowConfig::restoreWindowSize(windowHandle(), conf); } ImportSQLDatabaseDialog::~ImportSQLDatabaseDialog() { //save current settings KConfigGroup conf(KSharedConfig::openConfig(), "ImportSQLDatabaseDialog"); KWindowConfig::saveWindowSize(windowHandle(), conf); } void ImportSQLDatabaseDialog::importTo(QStatusBar* statusBar) const { DEBUG("ImportSQLDatabaseDialog::import()"); AbstractAspect* aspect = static_cast(cbAddTo->currentModelIndex().internalPointer()); if (!aspect) { DEBUG("ERROR: No aspect available!"); return; } AbstractFileFilter::ImportMode mode = AbstractFileFilter::ImportMode(cbPosition->currentIndex()); //show a progress bar in the status bar QProgressBar* progressBar = new QProgressBar(); progressBar->setMinimum(0); progressBar->setMaximum(100); connect(importSQLDatabaseWidget, SIGNAL(completed(int)), progressBar, SLOT(setValue(int))); statusBar->clearMessage(); statusBar->addWidget(progressBar, 1); WAIT_CURSOR; QApplication::processEvents(QEventLoop::AllEvents, 100); QTime timer; timer.start(); if (aspect->inherits("Matrix")) { Matrix* matrix = qobject_cast(aspect); importSQLDatabaseWidget->read(matrix, mode); } else if (aspect->inherits("Spreadsheet")) { Spreadsheet* spreadsheet = qobject_cast(aspect); importSQLDatabaseWidget->read(spreadsheet, mode); } else if (aspect->inherits("Workbook")) { // use active spreadsheet or matrix (only if numeric data is going to be improted) if present, // create a new spreadsheet in the selected workbook otherwise Workbook* workbook = qobject_cast(aspect); Spreadsheet* spreadsheet = workbook->currentSpreadsheet(); Matrix* matrix = workbook->currentMatrix(); if (spreadsheet) importSQLDatabaseWidget->read(spreadsheet, mode); else if (matrix && importSQLDatabaseWidget->isNumericData()) importSQLDatabaseWidget->read(matrix, mode); else { spreadsheet = new Spreadsheet(0, i18n("Spreadsheet")); workbook->addChild(spreadsheet); importSQLDatabaseWidget->read(spreadsheet, mode); } } statusBar->showMessage( i18n("Data imported in %1 seconds.", (float)timer.elapsed()/1000) ); RESET_CURSOR; statusBar->removeWidget(progressBar); } QString ImportSQLDatabaseDialog::selectedObject() const { return importSQLDatabaseWidget->selectedTable(); } void ImportSQLDatabaseDialog::checkOkButton() { DEBUG("ImportSQLDatabaseDialog::checkOkButton()"); AbstractAspect* aspect = static_cast(cbAddTo->currentModelIndex().internalPointer()); if (!aspect) { - enableButtonOk(false); + okButton->setEnabled(false); cbPosition->setEnabled(false); return; } //check whether a valid connection and an object to import were selected if (!importSQLDatabaseWidget->isValid()) { - enableButtonOk(false); + okButton->setEnabled(false); cbPosition->setEnabled(false); return; } //for matrix containers allow to import only numerical data if (dynamic_cast(aspect) && !importSQLDatabaseWidget->isNumericData()) { - enableButtonOk(false); + okButton->setEnabled(false); cbPosition->setEnabled(false); return; } - enableButtonOk(true); + okButton->setEnabled(true); cbPosition->setEnabled(true); } diff --git a/src/kdefrontend/ui/datasources/importfilewidget.ui b/src/kdefrontend/ui/datasources/importfilewidget.ui index a0d33a822..e07cf194d 100644 --- a/src/kdefrontend/ui/datasources/importfilewidget.ui +++ b/src/kdefrontend/ui/datasources/importfilewidget.ui @@ -1,684 +1,696 @@ ImportFileWidget 0 0 679 1219 + + 0 + + + 0 + + + 0 + + + 0 + 0 0 Data source Name Source 0 0 Select the file to import false 0 0 Show file info Port Port Baud rate Qt::Vertical QSizePolicy::Fixed 20 13 Type Filter false 0 0 false Save the current filter settings false 0 0 Manage filters Qt::Vertical QSizePolicy::Fixed 20 10 Specify the name of the file to import. true Host File or named pipe Network TCP socket Network UDP socket Local socket Serial port false false 0 0 Format Options 0 0 0 0 0 Data format Preview 0 0 Number of rows to preview: 1 10000 100 Qt::Horizontal 23 20 Refresh true QTextEdit::NoWrap true Data portion to read Qt::Vertical 20 172 Start row: Specify the start row for import 1 2147483647 1 Qt::Horizontal QSizePolicy::Fixed 40 20 Qt::Horizontal 108 20 End row: Specify the end row to import; -1 stands for the last row -1 2147483647 -1 Start column: Specify the start column for import 1 2147483647 Qt::Horizontal QSizePolicy::Fixed 40 20 End column: Specify the end column to import; -1 stands for the last column -1 2147483647 -1 Qt::Horizontal 108 20 Update Options 0 0 Periodically On new data If this option is checked, only the link to the file is stored in the project file but not it's content. Link the file true Read Keep last N values Update Sample rate 1 10000 5 Continuously fixed From end Till the end Update interval 0 0 5 60000 1000 ms KComboBox QComboBox
kcombobox.h
diff --git a/src/kdefrontend/ui/datasources/importsqldatabasewidget.ui b/src/kdefrontend/ui/datasources/importsqldatabasewidget.ui index 9a040882f..de5bd9dc9 100644 --- a/src/kdefrontend/ui/datasources/importsqldatabasewidget.ui +++ b/src/kdefrontend/ui/datasources/importsqldatabasewidget.ui @@ -1,406 +1,418 @@ ImportSQLDatabaseWidget 0 0 675 634 + + 0 + + + 0 + + + 0 + + + 0 + Database Connection 0 0 0 0 Import from Qt::Vertical QSizePolicy::Fixed 20 20 Number format DateTime format 0 0 true Qt::Vertical false 0 0 Query 0 100 0 Preview Qt::Horizontal 40 20 Number of rows to preview: 1 10000 100 Refresh Qt::Horizontal false 300 0 true true Data portion to read Start row: Specify the start row for import 1 2147483647 1 Qt::Horizontal QSizePolicy::Fixed 40 20 End row: Specify the end row to import; -1 stands for the last row -1 2147483647 -1 Qt::Horizontal 523 20 Start column: Specify the start column for import 1 2147483647 Qt::Horizontal QSizePolicy::Fixed 40 20 End column: Specify the end column to import; -1 stands for the last column -1 2147483647 -1 Qt::Horizontal 523 20 Qt::Vertical 20 139 KComboBox QComboBox
kcombobox.h