diff --git a/src/backend/datasources/projects/LabPlotProjectParser.cpp b/src/backend/datasources/projects/LabPlotProjectParser.cpp index 8f8d14021..78d1c99ed 100644 --- a/src/backend/datasources/projects/LabPlotProjectParser.cpp +++ b/src/backend/datasources/projects/LabPlotProjectParser.cpp @@ -1,68 +1,68 @@ /*************************************************************************** File : LabPlotProjectParser.h Project : LabPlot Description : parser for LabPlot projects -------------------------------------------------------------------- Copyright : (C) 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 "backend/datasources/projects/LabPlotProjectParser.h" #include "backend/core/Project.h" #include "backend/core/AspectTreeModel.h" /*! \class LabPlotProjectParser \brief parser for LabPlot projects. \ingroup datasources */ LabPlotProjectParser::LabPlotProjectParser() : ProjectParser() { m_topLevelClasses<<"Folder"<<"Workbook"<<"Spreadsheet"<<"Matrix"<<"Worksheet"<<"CantorWorksheet"<<"Datapicker"<<"LiveDataSource"; } LabPlotProjectParser::~LabPlotProjectParser() { if (m_project != nullptr) delete m_project; } QAbstractItemModel* LabPlotProjectParser::model() { WAIT_CURSOR; if (m_project == nullptr) m_project = new Project(); AspectTreeModel* model = nullptr; bool rc = m_project->load(m_projectFileName, true); if (rc) { model = new AspectTreeModel(m_project); model->setReadOnly(true); } RESET_CURSOR; return model; } -void LabPlotProjectParser::importTo(Folder* folder) { +void LabPlotProjectParser::importTo(Folder* folder, const QStringList& selectedPathes) { Q_UNUSED(folder); - + Q_UNUSED(selectedPathes); } diff --git a/src/backend/datasources/projects/LabPlotProjectParser.h b/src/backend/datasources/projects/LabPlotProjectParser.h index 8c96965f6..f21855809 100644 --- a/src/backend/datasources/projects/LabPlotProjectParser.h +++ b/src/backend/datasources/projects/LabPlotProjectParser.h @@ -1,44 +1,44 @@ /*************************************************************************** File : LabPlotProjectParser.h Project : LabPlot Description : parser for LabPlot projects -------------------------------------------------------------------- Copyright : (C) 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 LABPLOTPROJECTPARSER_H #define LABPLOTPROJECTPARSER_H #include "backend/datasources/projects/ProjectParser.h" class LabPlotProjectParser : public ProjectParser { Q_OBJECT public: LabPlotProjectParser(); virtual ~LabPlotProjectParser() override; virtual QAbstractItemModel* model() override; - virtual void importTo(Folder*) override; + virtual void importTo(Folder*, const QStringList&) override; }; #endif // LABPLOTPROJECTPARSER_H diff --git a/src/backend/datasources/projects/OriginProjectParser.cpp b/src/backend/datasources/projects/OriginProjectParser.cpp index d6e8b55ac..405741113 100644 --- a/src/backend/datasources/projects/OriginProjectParser.cpp +++ b/src/backend/datasources/projects/OriginProjectParser.cpp @@ -1,71 +1,72 @@ /*************************************************************************** File : OriginProjectParser.h Project : LabPlot Description : parser for Origin projects -------------------------------------------------------------------- Copyright : (C) 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 "backend/datasources/projects/OriginProjectParser.h" #include "backend/core/Project.h" #include "backend/core/AspectTreeModel.h" #include #include "kdefrontend/datasources/ImportOpj.h" /*! \class OriginProjectParser \brief parser for Origin projects. \ingroup datasources */ OriginProjectParser::OriginProjectParser() : ProjectParser() { m_topLevelClasses << "Folder" << "Workbook" << "Spreadsheet" << "Matrix" << "Worksheet"; } QAbstractItemModel* OriginProjectParser::model() { WAIT_CURSOR; if (m_project == nullptr) m_project = new Project(); //parse the OPJ file and create a Project object for the preview ImportOpj(m_project, m_projectFileName, true); bool rc = true; //TODO // m_project->setName("Test"); RESET_CURSOR; AspectTreeModel* model = nullptr; if (rc) { model = new AspectTreeModel(m_project); model->setReadOnly(true); } return model; } -void OriginProjectParser::importTo(Folder* folder) { +void OriginProjectParser::importTo(Folder* folder, const QStringList& selectedPathes) { + Q_UNUSED(selectedPathes); ImportOpj(folder, m_projectFileName, true); } diff --git a/src/backend/datasources/projects/OriginProjectParser.h b/src/backend/datasources/projects/OriginProjectParser.h index 8e7435061..b85b6ff09 100644 --- a/src/backend/datasources/projects/OriginProjectParser.h +++ b/src/backend/datasources/projects/OriginProjectParser.h @@ -1,43 +1,43 @@ /*************************************************************************** File : OriginProjectParser.h Project : LabPlot Description : parser for Origin projects -------------------------------------------------------------------- Copyright : (C) 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 ORIGINPROJECTPARSER_H #define ORIGINPROJECTPARSER_H #include "backend/datasources/projects/ProjectParser.h" class OriginProjectParser : public ProjectParser { Q_OBJECT public: OriginProjectParser(); virtual QAbstractItemModel* model() override; - virtual void importTo(Folder*) override; + virtual void importTo(Folder*, const QStringList&) override; }; #endif // ORIGINPROJECTPARSER_H diff --git a/src/backend/datasources/projects/ProjectParser.h b/src/backend/datasources/projects/ProjectParser.h index 89a353ab8..96771e3b3 100644 --- a/src/backend/datasources/projects/ProjectParser.h +++ b/src/backend/datasources/projects/ProjectParser.h @@ -1,62 +1,62 @@ /*************************************************************************** File : ProjectParser.h Project : LabPlot Description : base class for project parsers -------------------------------------------------------------------- Copyright : (C) 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 PROJECTPARSER_H #define PROJECTPARSER_H #include class QAbstractItemModel; class QString; class Folder; class Project; class ProjectParser : public QObject { Q_OBJECT public: ProjectParser(); virtual ~ProjectParser() {}; void setProjectFileName(const QString&); const QString& projectFileName() const; virtual QAbstractItemModel* model() = 0; - virtual void importTo(Folder*) = 0; + virtual void importTo(Folder*, const QStringList&) = 0; QList topLevelClasses() const ; protected: QString m_projectFileName; Project* m_project; QList m_topLevelClasses; signals: void completed(int); }; #endif // PROJECTPARSER_H diff --git a/src/kdefrontend/datasources/ImportProjectDialog.cpp b/src/kdefrontend/datasources/ImportProjectDialog.cpp index 9f45473e5..14e1ddb63 100644 --- a/src/kdefrontend/datasources/ImportProjectDialog.cpp +++ b/src/kdefrontend/datasources/ImportProjectDialog.cpp @@ -1,314 +1,337 @@ /*************************************************************************** File : ImportProjectDialog.cpp Project : LabPlot Description : import project dialog -------------------------------------------------------------------- Copyright : (C) 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 "ImportProjectDialog.h" #include "backend/core/AspectTreeModel.h" #include "backend/core/Project.h" #include "backend/datasources/projects/LabPlotProjectParser.h" #include "backend/datasources/projects/OriginProjectParser.h" #include "kdefrontend/MainWin.h" #include "commonfrontend/widgets/TreeViewComboBox.h" #include #include #include #include #include #include #include #include /*! \class ImportProjectDialog \brief Dialog for importing project files. \ingroup kdefrontend */ ImportProjectDialog::ImportProjectDialog(MainWin* parent, ProjectType type) : QDialog(parent), m_mainWin(parent), m_projectParser(nullptr), m_projectType(type), m_aspectTreeModel(new AspectTreeModel(parent->project())) { QVBoxLayout* vLayout = new QVBoxLayout(this); //main widget QWidget* mainWidget = new QWidget(this); ui.setupUi(mainWidget); vLayout->addWidget(mainWidget); + ui.tvPreview->setAnimated(true); + ui.tvPreview->setAlternatingRowColors(true); + ui.tvPreview->setSelectionBehavior(QAbstractItemView::SelectRows); + ui.tvPreview->setSelectionMode(QAbstractItemView::ExtendedSelection); + ui.tvPreview->setUniformRowHeights(true); + ui.bOpen->setIcon( QIcon::fromTheme("document-open") ); m_cbAddTo = new TreeViewComboBox(ui.gbImportTo); m_cbAddTo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); ui.gbImportTo->layout()->addWidget(m_cbAddTo); QList list; list << "Folder"; m_cbAddTo->setTopLevelClasses(list); m_aspectTreeModel->setSelectableAspects(list); m_cbAddTo->setModel(m_aspectTreeModel); m_bNewFolder = new QPushButton(ui.gbImportTo); m_bNewFolder->setIcon(QIcon::fromTheme("list-add")); m_bNewFolder->setToolTip(i18n("Add new folder")); ui.gbImportTo->layout()->addWidget(m_bNewFolder); //dialog buttons m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); - m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true); //TODO: ok is only available if some project objects were selected vLayout->addWidget(m_buttonBox); + //ok-button is only enabled if some project objects were selected (s.a. ImportProjectDialog::selectionChanged()) + m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); + //Signals/Slots connect(ui.leFileName, SIGNAL(textChanged(QString)), SLOT(fileNameChanged(QString))); connect(ui.bOpen, SIGNAL(clicked()), this, SLOT (selectFile())); connect(m_bNewFolder, SIGNAL(clicked()), this, SLOT(newFolder())); connect(m_buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(m_buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); QString title; switch (m_projectType) { case (ProjectLabPlot): m_projectParser = new LabPlotProjectParser(); title = i18n("Import LabPlot Project"); break; case (ProjectOrigin): m_projectParser = new OriginProjectParser(); title = i18n("Import Origin Project"); break; } //dialog title and icon setWindowTitle(title); setWindowIcon(QIcon::fromTheme("document-import")); QTimer::singleShot(0, this, &ImportProjectDialog::loadSettings); } void ImportProjectDialog::loadSettings() { //restore saved settings KConfigGroup conf(KSharedConfig::openConfig(), "ImportProjectDialog"); KWindowConfig::restoreWindowSize(windowHandle(), conf); QString lastImportedFile; switch (m_projectType) { case (ProjectLabPlot): lastImportedFile = QLatin1String("LastImportedLabPlotProject"); break; case (ProjectOrigin): lastImportedFile = QLatin1String("LastImportedOriginProject"); break; } QApplication::processEvents(QEventLoop::AllEvents, 100); ui.leFileName->setText(conf.readEntry(lastImportedFile, "")); } ImportProjectDialog::~ImportProjectDialog() { //save current settings KConfigGroup conf(KSharedConfig::openConfig(), "ImportProjectDialog"); KWindowConfig::saveWindowSize(windowHandle(), conf); QString lastImportedFile; switch (m_projectType) { case (ProjectLabPlot): lastImportedFile = QLatin1String("LastImportedLabPlotProject"); break; case (ProjectOrigin): lastImportedFile = QLatin1String("LastImportedOriginProject"); break; } conf.writeEntry(lastImportedFile, ui.leFileName->text()); } void ImportProjectDialog::setCurrentFolder(const Folder* folder) { m_cbAddTo->setCurrentModelIndex(m_aspectTreeModel->modelIndexOfAspect(folder)); } void ImportProjectDialog::importTo(QStatusBar* statusBar) const { DEBUG("ImportProjectDialog::importTo()"); //show a progress bar in the status bar QProgressBar* progressBar = new QProgressBar(); progressBar->setMinimum(0); progressBar->setMaximum(100); statusBar->clearMessage(); statusBar->addWidget(progressBar, 1); QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); QApplication::processEvents(QEventLoop::AllEvents, 100); + //determine the selected objects, convert the model indexes to string pathes + const QModelIndexList& indexes = ui.tvPreview->selectionModel()->selectedIndexes(); + QStringList selectedPathes; + for (int i=0; i(index.internalPointer()); + selectedPathes << aspect->path(); + } + QDEBUG("project objects to be imported: " << selectedPathes); + //import the selected project objects into the specified folder QTime timer; timer.start(); Folder* folder = static_cast(m_cbAddTo->currentModelIndex().internalPointer()); connect(m_projectParser, SIGNAL(completed(int)), progressBar, SLOT(setValue(int))); - m_projectParser->importTo(folder); + m_projectParser->importTo(folder ,selectedPathes); statusBar->showMessage( i18n("Project data imported in %1 seconds.", (float)timer.elapsed()/1000) ); QApplication::restoreOverrideCursor(); statusBar->removeWidget(progressBar); } /*! * show the content of the project in the tree view */ void ImportProjectDialog::refreshPreview() { QString project = ui.leFileName->text(); m_projectParser->setProjectFileName(project); ui.tvPreview->setModel(m_projectParser->model()); + connect(ui.tvPreview->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), + this, SLOT(selectionChanged(QItemSelection,QItemSelection)) ); + //show top-level containers only if (ui.tvPreview->model()) { QModelIndex root = ui.tvPreview->model()->index(0,0); showTopLevelOnly(root); } //extand the tree to show all available top-level objects and adjust the header sizes ui.tvPreview->expandAll(); ui.tvPreview->header()->resizeSections(QHeaderView::ResizeToContents); } /*! Hides the non-toplevel items of the model used in the tree view. */ void ImportProjectDialog::showTopLevelOnly(const QModelIndex& index) { int rows = index.model()->rowCount(index); for (int i = 0; i < rows; ++i) { QModelIndex child = index.child(i, 0); showTopLevelOnly(child); const AbstractAspect* aspect = static_cast(child.internalPointer()); ui.tvPreview->setRowHidden(i, index, !isTopLevel(aspect)); } } /*! checks whether \c aspect is one of the allowed top level types */ bool ImportProjectDialog::isTopLevel(const AbstractAspect* aspect) const { foreach (const char* classString, m_projectParser->topLevelClasses()) { if (aspect->inherits(classString)) return true; } return false; } //############################################################################## //################################# SLOTS #################################### //############################################################################## -void ImportProjectDialog::selectionChanged() { - //TODO: +void ImportProjectDialog::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected) { + Q_UNUSED(deselected); + //determine the dependent objects and select/deselect them too + //TODO: - //Ok-button is only available if some project objects were selected - bool objectsSelected = true; //TODO + //Ok-button is only enabled if some project objects were selected + bool objectsSelected = (selected.indexes().size() != 0); m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(objectsSelected); } /*! opens a file dialog and lets the user select the project file. */ void ImportProjectDialog::selectFile() { KConfigGroup conf(KSharedConfig::openConfig(), "ImportProjectDialog"); QString title; QString lastDir; QString supportedFormats; QString lastDirConfEntryName; switch (m_projectType) { case (ProjectLabPlot): title = i18n("Open LabPlot Project"); lastDirConfEntryName = QLatin1String("LastImportLabPlotProjectDir"); supportedFormats = i18n("LabPlot Projects (*.lml *.lml.gz *.lml.bz2 *.lml.xz *.LML *.LML.GZ *.LML.BZ2 *.LML.XZ)"); break; case (ProjectOrigin): title = i18n("Open Origin Project"); lastDirConfEntryName = QLatin1String("LastImportOriginProjecttDir"); supportedFormats = i18n("Origin Projects (*.opj *.OPJ)"); break; } lastDir = conf.readEntry(lastDirConfEntryName, ""); QString path = QFileDialog::getOpenFileName(this, title, lastDir, supportedFormats); if (path.isEmpty()) return; //cancel was clicked in the file-dialog int pos = path.lastIndexOf(QDir::separator()); if (pos != -1) { QString newDir = path.left(pos); if (newDir != lastDir) conf.writeEntry(lastDirConfEntryName, newDir); } ui.leFileName->setText(path); refreshPreview(); } void ImportProjectDialog::fileNameChanged(const QString& name) { QString fileName = name; #ifndef HAVE_WINDOWS // make relative path if ( !fileName.isEmpty() && fileName.left(1) != QDir::separator()) fileName = QDir::homePath() + QDir::separator() + fileName; #endif bool fileExists = QFile::exists(fileName); if (fileExists) ui.leFileName->setStyleSheet(""); else ui.leFileName->setStyleSheet("QLineEdit{background:red;}"); if (!fileExists) { //file doesn't exist -> delete the content preview that is still potentially //available from the previously selected file ui.tvPreview->setModel(nullptr); m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); return; } refreshPreview(); } void ImportProjectDialog::newFolder() { QString path = ui.leFileName->text(); QString name = path.right( path.length()-path.lastIndexOf(QDir::separator())-1 ); bool ok; QInputDialog* dlg = new QInputDialog(this); name = dlg->getText(this, i18n("Add new folder"), i18n("Folder name:"), QLineEdit::Normal, name, &ok); if (ok) { Folder* folder = new Folder(name); m_mainWin->addAspectToProject(folder); m_cbAddTo->setCurrentModelIndex(m_mainWin->model()->modelIndexOfAspect(folder)); } delete dlg; } diff --git a/src/kdefrontend/datasources/ImportProjectDialog.h b/src/kdefrontend/datasources/ImportProjectDialog.h index bf4eb12d0..f7cb074e8 100644 --- a/src/kdefrontend/datasources/ImportProjectDialog.h +++ b/src/kdefrontend/datasources/ImportProjectDialog.h @@ -1,78 +1,78 @@ /*************************************************************************** File : ImportProjectDialog.h Project : LabPlot Description : import project dialog -------------------------------------------------------------------- Copyright : (C) 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 IMPORTPROJECTDIALOG_H #define IMPORTPROJECTDIALOG_H #include #include "ui_importprojectwidget.h" class AbstractAspect; class AspectTreeModel; class Folder; class ProjectParser; class TreeViewComboBox; class MainWin; class QDialogButtonBox; class QStatusBar; class ImportProjectDialog : public QDialog { Q_OBJECT public: enum ProjectType {ProjectLabPlot, ProjectOrigin}; explicit ImportProjectDialog(MainWin*, ProjectType); ~ImportProjectDialog(); void setCurrentFolder(const Folder*); void importTo(QStatusBar*) const; private: Ui::ImportProjectWidget ui; MainWin* m_mainWin; ProjectParser* m_projectParser; ProjectType m_projectType; AspectTreeModel* m_aspectTreeModel; TreeViewComboBox* m_cbAddTo; QPushButton* m_bNewFolder; QDialogButtonBox* m_buttonBox; void refreshPreview(); void showTopLevelOnly(const QModelIndex&); bool isTopLevel(const AbstractAspect*) const; private slots: void loadSettings(); void fileNameChanged(const QString&); - void selectionChanged(); + void selectionChanged(const QItemSelection&, const QItemSelection&); void selectFile(); void newFolder(); }; #endif //IMPORTPROJECTDIALOG_H