diff --git a/src/kdefrontend/datasources/ImportProjectDialog.cpp b/src/kdefrontend/datasources/ImportProjectDialog.cpp index 521612774..e1d1ec3ff 100644 --- a/src/kdefrontend/datasources/ImportProjectDialog.cpp +++ b/src/kdefrontend/datasources/ImportProjectDialog.cpp @@ -1,421 +1,423 @@ /*************************************************************************** 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" +#ifdef HAVE_LIBORIGIN #include "backend/datasources/projects/OriginProjectParser.h" +#endif #include "kdefrontend/MainWin.h" #include "commonfrontend/widgets/TreeViewComboBox.h" #include #include #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); ui.chbUnusedObjects->hide(); 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); 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(ui.chbUnusedObjects, &QCheckBox::stateChanged, this, &ImportProjectDialog::refreshPreview); 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 = i18nc("@title:window", "Import LabPlot Project"); break; case (ProjectOrigin): #ifdef HAVE_LIBORIGIN m_projectParser = new OriginProjectParser(); title = i18nc("@title:window", "Import Origin Project"); #endif 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()"); //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 < indexes.size()/4; ++i) { QModelIndex index = indexes.at(i*4); const AbstractAspect* aspect = static_cast(index.internalPointer()); //path of the current aspect and the pathes of all aspects it depends on selectedPathes << aspect->path(); QDEBUG(" aspect path: " << aspect->path()); for (const auto* depAspect : aspect->dependsOn()) selectedPathes << depAspect->path(); } selectedPathes.removeDuplicates(); Folder* targetFolder = static_cast(m_cbAddTo->currentModelIndex().internalPointer()); //check whether the selected pathes already exist in the target folder and warn the user const QString& targetFolderPath = targetFolder->path(); const Project* targetProject = targetFolder->project(); QStringList targetAllPathes; for (const auto* aspect : targetProject->children(AbstractAspect::Recursive)) { if (!dynamic_cast(aspect)) targetAllPathes << aspect->path(); } QStringList existingPathes; for (const auto& path : selectedPathes) { const QString& newPath = targetFolderPath + path.right(path.length() - path.indexOf('/')); if (targetAllPathes.indexOf(newPath) != -1) existingPathes << path; } QDEBUG("project objects to be imported: " << selectedPathes); QDEBUG("all already available project objects: " << targetAllPathes); QDEBUG("already available project objects to be overwritten: " << existingPathes); if (!existingPathes.isEmpty()) { QString msg = i18np("The object listed below already exists in target folder and will be overwritten:", "The objects listed below already exist in target folder and will be overwritten:", existingPathes.size()); msg += '\n'; for (const auto& path : existingPathes) msg += '\n' + path.right(path.length() - path.indexOf('/') - 1); //strip away the name of the root folder "Project" msg += "\n\n" + i18n("Do you want to proceed?"); const int rc = KMessageBox::warningYesNo(nullptr, msg, i18n("Override existing objects?")); if (rc == KMessageBox::No) return; } //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); //import the selected project objects into the specified folder QTime timer; timer.start(); connect(m_projectParser, SIGNAL(completed(int)), progressBar, SLOT(setValue(int))); #ifdef HAVE_LIBORIGIN if (m_projectType == ProjectOrigin && ui.chbUnusedObjects->isVisible() && ui.chbUnusedObjects->isChecked()) reinterpret_cast(m_projectParser)->setImportUnusedObjects(true); #endif m_projectParser->importTo(targetFolder, 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); #ifdef HAVE_LIBORIGIN if (m_projectType == ProjectOrigin) { OriginProjectParser* originParser = reinterpret_cast(m_projectParser); if (originParser->hasUnusedObjects()) ui.chbUnusedObjects->show(); else ui.chbUnusedObjects->hide(); originParser->setImportUnusedObjects(ui.chbUnusedObjects->isVisible() && ui.chbUnusedObjects->isChecked()); } #endif 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(const QItemSelection& selected, const QItemSelection& deselected) { Q_UNUSED(deselected); //determine the dependent objects and select/deselect them too const QModelIndexList& indexes = selected.indexes(); if (indexes.isEmpty()) return; //for the just selected aspect, determine all the objects it depends on and select them, too //TODO: we need a better "selection", maybe with tri-state check boxes in the tree view const AbstractAspect* aspect = static_cast(indexes.at(0).internalPointer()); const QVector aspects = aspect->dependsOn(); AspectTreeModel* model = reinterpret_cast(ui.tvPreview->model()); for (const auto* aspect : aspects) { QModelIndex index = model->modelIndexOfAspect(aspect, 0); ui.tvPreview->selectionModel()->select(index, QItemSelectionModel::Select | QItemSelectionModel::Rows); } //Ok-button is only enabled if some project objects were selected bool enable = (selected.indexes().size() != 0); m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enable); if (enable) m_buttonBox->button(QDialogButtonBox::Ok)->setToolTip(i18n("Close the dialog and import the selected objects.")); else m_buttonBox->button(QDialogButtonBox::Ok)->setToolTip(i18n("Select object(s) to be imported.")); } /*! 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 = i18nc("@title:window", "Open LabPlot Project"); lastDirConfEntryName = QLatin1String("LastImportLabPlotProjectDir"); supportedFormats = i18n("LabPlot Projects (%1)", Project::supportedExtensions()); break; case (ProjectOrigin): #ifdef HAVE_LIBORIGIN title = i18nc("@title:window", "Open Origin Project"); lastDirConfEntryName = QLatin1String("LastImportOriginProjecttDir"); supportedFormats = i18n("Origin Projects (%1)", OriginProjectParser::supportedExtensions()); #endif 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.at(0) != 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/tests/import_export/project/ProjectImportTest.cpp b/tests/import_export/project/ProjectImportTest.cpp index 11ac7845b..fde0e14ef 100755 --- a/tests/import_export/project/ProjectImportTest.cpp +++ b/tests/import_export/project/ProjectImportTest.cpp @@ -1,379 +1,383 @@ /*************************************************************************** File : ProjectImportTest.cpp Project : LabPlot Description : Tests for project imports -------------------------------------------------------------------- Copyright : (C) 2018 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 "ProjectImportTest.h" +#ifdef HAVE_LIBORIGIN #include "backend/datasources/projects/OriginProjectParser.h" +#endif #include "backend/core/Project.h" #include "backend/core/Workbook.h" #include "backend/matrix/Matrix.h" #include "backend/worksheet/Worksheet.h" #include "backend/worksheet/plots/cartesian/CartesianPlot.h" #include "backend/spreadsheet/Spreadsheet.h" void ProjectImportTest::initTestCase() { const QString currentDir = __FILE__; m_dataDir = currentDir.left(currentDir.lastIndexOf(QDir::separator())) + QDir::separator() + QLatin1String("data") + QDir::separator(); // needed in order to have the signals triggered by SignallingUndoCommand, see LabPlot.cpp //TODO: redesign/remove this qRegisterMetaType("const AbstractAspect*"); qRegisterMetaType("const AbstractColumn*"); } //############################################################################## //##################### import of LabPlot projects ############################ //############################################################################## +#ifdef HAVE_LIBORIGIN //############################################################################## //###################### import of Origin projects ############################ //############################################################################## //project tree of the file "origin8_test_tree_import.opj" /* test_tree_import\ \Book3 \Folder \Book2 \Sheet1 \Sheet2 \MBook2 \MSheet1 \MSheet2 \Folder1 \MBook1 \Sheet1 \Book1 \Sheet1 \Book4 \MSheet1 \Graph2 \Excel1 */ void ProjectImportTest::testOrigin01() { //import the opj file into LabPlot's project object OriginProjectParser parser; parser.setProjectFileName(m_dataDir + QLatin1String("origin8_test_tree_import.opj")); Project project; parser.importTo(&project, QStringList()); //check the project tree for the imported project //first child of the root folder, spreadsheet "Book3" AbstractAspect* aspect = project.child(0); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Book3")); QCOMPARE(dynamic_cast(aspect) != nullptr, true); //first child of the root folder, folder "Folder" -> import into a Folder aspect = project.child(1); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Folder")); QCOMPARE(dynamic_cast(aspect) != nullptr, true); //first child of "Folder", workbook "Book2" with two sheets -> import into a Workbook with two Spreadsheets aspect = project.child(1)->child(0); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Book2")); QCOMPARE(dynamic_cast(aspect) != nullptr, true); aspect = project.child(1)->child(0)->child(0); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Sheet1")); QCOMPARE(dynamic_cast(aspect) != nullptr, true); aspect = project.child(1)->child(0)->child(1); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Sheet2")); QCOMPARE(dynamic_cast(aspect) != nullptr, true); //second child of "Folder", matrixbook "MBook" with two matrix sheets -> import into a Workbook with two Matrices aspect = project.child(1)->child(1); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("MBook2")); QCOMPARE(dynamic_cast(aspect) != nullptr, true); aspect = project.child(1)->child(1)->child(0); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("MSheet1")); QCOMPARE(dynamic_cast(aspect) != nullptr, true); aspect = project.child(1)->child(1)->child(1); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("MSheet2")); QCOMPARE(dynamic_cast(aspect) != nullptr, true); //second child of the root folder, folder "Folder1" -> import into a Folder aspect = project.child(2); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Folder1")); QCOMPARE(dynamic_cast(aspect) != nullptr, true); //first child of "Folder1", matrix "MBook1" aspect = project.child(2)->child(0); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("MBook1")); QCOMPARE(dynamic_cast(aspect) != nullptr, true); //second child of "Folder1", workbook "Book1" with on sheet -> import into a Spreadsheet aspect = project.child(2)->child(1); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Book1")); QCOMPARE(dynamic_cast(aspect) != nullptr, true); //second child of "Folder1", workbook "Book4" with on sheet -> import into a Spreadsheet aspect = project.child(2)->child(2); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Book4")); QCOMPARE(dynamic_cast(aspect) != nullptr, true); //third child of "Folder1", graph "Graph"-> import into a Worksheet aspect = project.child(2)->child(3); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Graph2")); QCOMPARE(dynamic_cast(aspect) != nullptr, true); //TODO: check the created plot in the worksheet // TODO: loose window: spreadsheet "Excel1" //aspect = project.child(3); //QCOMPARE(aspect != nullptr, true); //QCOMPARE(aspect->name(), QLatin1String("Excel1")); //QCOMPARE(dynamic_cast(aspect) != nullptr, true); } /* * import one single folder child */ void ProjectImportTest::testOrigin02() { //import one single object OriginProjectParser parser; parser.setProjectFileName(m_dataDir + QLatin1String("origin8_test_tree_import.opj")); Project project; QStringList selectedPathes = {QLatin1String("test_tree_import/Folder1/Book1"), QLatin1String("test_tree_import/Folder1"), QLatin1String("test_tree_import")}; parser.importTo(&project, selectedPathes); //check the project tree for the imported project //first child of the root folder, folder "Folder1" -> import into a Folder AbstractAspect* aspect = project.child(0); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Folder1")); QCOMPARE(dynamic_cast(aspect) != nullptr, true); QCOMPARE(aspect->childCount(), 1); //first child of "Folder", workbook "Book" with one sheet -> import into a Spreadsheet aspect = project.child(0)->child(0); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Book1")); QCOMPARE(dynamic_cast(aspect) != nullptr, true); } /* * 1. import one single folder child * 2. import another folder child * 3. check that both children are available after the second import */ void ProjectImportTest::testOrigin03() { //import one single object OriginProjectParser parser; parser.setProjectFileName(m_dataDir + QLatin1String("origin8_test_tree_import.opj")); Project project; //import one single child in "Folder1" QStringList selectedPathes = {QLatin1String("test_tree_import/Folder1/Book1"), QLatin1String("test_tree_import/Folder1"), QLatin1String("test_tree_import")}; parser.importTo(&project, selectedPathes); //first child of the root folder, folder "Folder1" -> import into a Folder AbstractAspect* aspect = project.child(0); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Folder1")); QCOMPARE(dynamic_cast(aspect) != nullptr, true); QCOMPARE(aspect->childCount(), 1); //first child of "Folder", workbook "Book1" with one sheet -> import into a Spreadsheet aspect = project.child(0)->child(0); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Book1")); QCOMPARE(dynamic_cast(aspect) != nullptr, true); //import another child in "Folder1" selectedPathes.clear(); selectedPathes << QLatin1String("test_tree_import/Folder1/Book4") << QLatin1String("test_tree_import/Folder1") << QLatin1String("test_tree_import"); parser.importTo(&project, selectedPathes); //the first child should still be available in the project -> check it aspect = project.child(0); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Folder1")); QCOMPARE(dynamic_cast(aspect) != nullptr, true); aspect = project.child(0)->child(0); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Book1")); QCOMPARE(dynamic_cast(aspect) != nullptr, true); //check the second child, workbook "Book4" with one sheet -> import into a Spreadsheet aspect = project.child(0)->child(1); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Book4")); QCOMPARE(dynamic_cast(aspect) != nullptr, true); QCOMPARE(aspect->childCount(), 2); } /* * 1. import a spreadsheet * 2. modify a cell in it * 3. import the same spreadsheet once more * 4. check that the changes were really over-written */ void ProjectImportTest::testOrigin04() { OriginProjectParser parser; parser.setProjectFileName(m_dataDir + QLatin1String("origin8_test_tree_import.opj")); Project project; //import "Book1" QStringList selectedPathes = {QLatin1String("test_tree_import/Folder1/Book1"), QLatin1String("test_tree_import/Folder1"), QLatin1String("test_tree_import")}; parser.importTo(&project, selectedPathes); //first child of folder "Folder1", workbook "Book1" with one sheet -> import into a spreadsheet AbstractAspect* aspect = project.child(0); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Folder1")); aspect = project.child(0)->child(0); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Book1")); Spreadsheet* spreadsheet = dynamic_cast(aspect); QCOMPARE(spreadsheet != nullptr, true); //the (0,0)-cell has the value 1.0 QCOMPARE(spreadsheet->column(0)->valueAt(0), 1.0); //set the value to 5.0 spreadsheet->column(0)->setValueAt(0, 5.0); QCOMPARE(spreadsheet->column(0)->valueAt(0), 5.0); //re-import parser.importTo(&project, selectedPathes); //check the folder structure and the value of the (0,0)-cell again aspect = project.child(0)->child(0); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Book1")); spreadsheet = dynamic_cast(aspect); QCOMPARE(spreadsheet != nullptr, true); QCOMPARE(spreadsheet->column(0)->valueAt(0), 1.0); } void ProjectImportTest::testOriginTextNumericColumns() { OriginProjectParser parser; parser.setProjectFileName(m_dataDir + QLatin1String("origin8_test_workbook.opj")); Project project; //import "Book1" QStringList selectedPathes = {QLatin1String("origin8_test_workbook/Folder1/Book1"), QLatin1String("origin8_test_workbook/Folder1"), QLatin1String("origin8_test_workbook")}; parser.importTo(&project, selectedPathes); //first child of folder "Folder1", workbook "Book1" with one sheet -> import into a spreadsheet AbstractAspect* aspect = project.child(0); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Folder1")); aspect = project.child(0)->child(0); QCOMPARE(aspect != nullptr, true); QCOMPARE(aspect->name(), QLatin1String("Book1")); Spreadsheet* spreadsheet = dynamic_cast(aspect); QCOMPARE(spreadsheet != nullptr, true); //check the values in the imported columns QCOMPARE(spreadsheet->columnCount(), 6); //1st column, Origin::TextNumeric: //first non-empty value is numerical, column is set to Numeric, empty or text values in the column a set to NAN Column* column = spreadsheet->column(0); QCOMPARE(column->columnMode(), AbstractColumn::Numeric); QCOMPARE(!std::isnan(column->valueAt(0)), false); QCOMPARE(column->valueAt(1), 1.1); QCOMPARE(column->valueAt(2), 2.2); QCOMPARE(!std::isnan(column->valueAt(3)), false); QCOMPARE(!std::isnan(column->valueAt(4)), false); //2nd column, Origin::TextNumeric: //first non-empty value is string, the column is set to Text, numerical values are converted to strings column = spreadsheet->column(1); QCOMPARE(column->columnMode(), AbstractColumn::Text); QCOMPARE(column->textAt(0).isEmpty(), true); QCOMPARE(column->textAt(1), QLatin1String("a")); QCOMPARE(column->textAt(2), QLatin1String("b")); QCOMPARE(column->textAt(3), QLatin1String("1.1")); QCOMPARE(column->textAt(4), QLatin1String("2.2")); //3rd column, Origin::TextNumeric: //first is numerical, column is set to Numeric, empty or text values in the column a set to NAN column = spreadsheet->column(2); QCOMPARE(column->columnMode(), AbstractColumn::Numeric); QCOMPARE(column->valueAt(0), 1.1); QCOMPARE(column->valueAt(1), 2.2); QCOMPARE(!std::isnan(column->valueAt(2)), false); QCOMPARE(!std::isnan(column->valueAt(3)), false); QCOMPARE(column->valueAt(4), 3.3); //4th column, Origin::TextNumeric: //first value is string, the column is set to Text, numerical values are converted to strings column = spreadsheet->column(3); QCOMPARE(column->columnMode(), AbstractColumn::Text); QCOMPARE(column->textAt(0), QLatin1String("a")); QCOMPARE(column->textAt(1), QLatin1String("b")); QCOMPARE(column->textAt(2), QLatin1String("1.1")); QCOMPARE(column->textAt(3), QLatin1String("2.2")); QCOMPARE(column->textAt(4), QLatin1String("c")); //5th column, Origin::Numeric //column is set to Numeric, empty values in the column a set to NAN column = spreadsheet->column(4); QCOMPARE(column->columnMode(), AbstractColumn::Numeric); QCOMPARE(!std::isnan(column->valueAt(0)), false); QCOMPARE(column->valueAt(1), 1.1); QCOMPARE(column->valueAt(2), 2.2); QCOMPARE(column->valueAt(3), 3.3); QCOMPARE(!std::isnan(column->valueAt(4)), false); //6th column, Origin::Numeric //column is set to Numeric, empty values in the column a set to NAN column = spreadsheet->column(5); QCOMPARE(column->columnMode(), AbstractColumn::Numeric); QCOMPARE(column->valueAt(0), 1.1); QCOMPARE(column->valueAt(1), 2.2); QCOMPARE(column->valueAt(2), 3.3); QCOMPARE(!std::isnan(column->valueAt(3)), false); QCOMPARE(!std::isnan(column->valueAt(4)), false); } +#endif QTEST_MAIN(ProjectImportTest) diff --git a/tests/import_export/project/ProjectImportTest.h b/tests/import_export/project/ProjectImportTest.h index 2e27bcd04..22b414a39 100755 --- a/tests/import_export/project/ProjectImportTest.h +++ b/tests/import_export/project/ProjectImportTest.h @@ -1,50 +1,52 @@ /*************************************************************************** File : ProjectImportTest.h Project : LabPlot Description : Tests for project imports -------------------------------------------------------------------- Copyright : (C) 2018 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 PROJECTIMPORTTEST_H #define PROJECTIMPORTTEST_H #include class ProjectImportTest : public QObject { Q_OBJECT private slots: void initTestCase(); //import of LabPlot projects +#ifdef HAVE_LIBORIGIN //import of Origin projects void testOrigin01(); void testOrigin02(); void testOrigin03(); void testOrigin04(); void testOriginTextNumericColumns(); +#endif private: QString m_dataDir; }; #endif