File indexing completed on 2024-05-12 15:27:48
0001 /*************************************************************************** 0002 File : ImportDialog.cc 0003 Project : LabPlot 0004 Description : import file data dialog 0005 -------------------------------------------------------------------- 0006 Copyright : (C) 2008-2018 Alexander Semke (alexander.semke@web.de) 0007 Copyright : (C) 2008-2015 by Stefan Gerlach (stefan.gerlach@uni.kn) 0008 0009 ***************************************************************************/ 0010 0011 /*************************************************************************** 0012 * * 0013 * This program is free software; you can redistribute it and/or modify * 0014 * it under the terms of the GNU General Public License as published by * 0015 * the Free Software Foundation; either version 2 of the License, or * 0016 * (at your option) any later version. * 0017 * * 0018 * This program is distributed in the hope that it will be useful, * 0019 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0021 * GNU General Public License for more details. * 0022 * * 0023 * You should have received a copy of the GNU General Public License * 0024 * along with this program; if not, write to the Free Software * 0025 * Foundation, Inc., 51 Franklin Street, Fifth Floor, * 0026 * Boston, MA 02110-1301 USA * 0027 * * 0028 ***************************************************************************/ 0029 0030 #include "ImportDialog.h" 0031 #include "backend/core/AspectTreeModel.h" 0032 #include "backend/core/Project.h" 0033 #include "backend/spreadsheet/Spreadsheet.h" 0034 #include "backend/matrix/Matrix.h" 0035 #include "backend/core/Workbook.h" 0036 #include "backend/lib/macros.h" 0037 #include "commonfrontend/widgets/TreeViewComboBox.h" 0038 #include "kdefrontend/MainWin.h" 0039 0040 #include <QDir> 0041 #include <QGroupBox> 0042 #include <QInputDialog> 0043 #include <QLabel> 0044 #include <QMenu> 0045 #include <QProgressBar> 0046 #include <QStatusBar> 0047 #include <QToolButton> 0048 #include <QVBoxLayout> 0049 0050 #include <KConfigGroup> 0051 #include <KLocalizedString> 0052 #include <KMessageBox> 0053 #include <KSharedConfig> 0054 0055 /*! 0056 \class ImportDialog 0057 \brief Base class for other import dialogs. Provides the "Import to" section of those dialogs. 0058 0059 \ingroup kdefrontend 0060 */ 0061 ImportDialog::ImportDialog(MainWin* parent) : QDialog(parent), 0062 vLayout(new QVBoxLayout(this)), 0063 m_mainWin(parent), 0064 m_aspectTreeModel(new AspectTreeModel(parent->project())) { 0065 0066 //menu for new data container 0067 m_newDataContainerMenu = new QMenu(this); 0068 m_newDataContainerMenu->addAction( QIcon::fromTheme("labplot-workbook-new"), i18n("New Workbook") ); 0069 m_newDataContainerMenu->addAction( QIcon::fromTheme("labplot-spreadsheet-new"), i18n("New Spreadsheet") ); 0070 m_newDataContainerMenu->addAction( QIcon::fromTheme("labplot-matrix-new"), i18n("New Matrix") ); 0071 connect(m_newDataContainerMenu, &QMenu::triggered, this, &ImportDialog::newDataContainer); 0072 } 0073 0074 ImportDialog::~ImportDialog() { 0075 if (m_aspectTreeModel) 0076 delete m_aspectTreeModel; 0077 0078 //save the last used import position for file imports, no need to do this for live data source (cbPosition=0) 0079 if (cbPosition) { 0080 KConfigGroup conf(KSharedConfig::openConfig(), "ImportDialog"); 0081 conf.writeEntry("Position", cbPosition->currentIndex()); 0082 } 0083 } 0084 0085 /*! 0086 creates widgets for the frame "Import-To" and sets the current model in the "Add to"-combobox. 0087 */ 0088 void ImportDialog::setModel() { 0089 //Frame for the "Import To"-Stuff 0090 frameAddTo = new QGroupBox(this); 0091 frameAddTo->setTitle(i18n("Import to")); 0092 0093 auto* label = new QLabel(i18n("Data container")); 0094 label->setToolTip(i18n("Data container where the data has to be imported into")); 0095 0096 auto* grid = new QGridLayout(frameAddTo); 0097 grid->addWidget(label, 0, 0); 0098 0099 cbAddTo = new TreeViewComboBox(); 0100 cbAddTo->setToolTip(i18n("Data container where the data has to be imported into")); 0101 cbAddTo->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); 0102 grid->addWidget(cbAddTo, 0, 1); 0103 0104 QList<AspectType> list{AspectType::Folder, AspectType::Spreadsheet, 0105 AspectType::Matrix, AspectType::Workbook}; 0106 cbAddTo->setTopLevelClasses(list); 0107 0108 list.removeFirst(); // do not allow selection of Folders 0109 m_aspectTreeModel->setSelectableAspects(list); 0110 0111 cbAddTo->setModel(m_aspectTreeModel); 0112 0113 tbNewDataContainer = new QToolButton(frameAddTo); 0114 tbNewDataContainer->setText(i18n("New")); 0115 tbNewDataContainer->setIcon(QIcon::fromTheme("list-add")); 0116 tbNewDataContainer->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); 0117 tbNewDataContainer->setToolTip(i18n("Add new data container to the project")); 0118 grid->addWidget( tbNewDataContainer, 0, 2); 0119 0120 lPosition = new QLabel(i18n("Position"), frameAddTo); 0121 lPosition->setEnabled(false); 0122 grid->addWidget(lPosition, 1, 0); 0123 0124 cbPosition = new QComboBox(frameAddTo); 0125 cbPosition->setEnabled(false); 0126 cbPosition->addItem(i18n("Append")); 0127 cbPosition->addItem(i18n("Prepend")); 0128 cbPosition->addItem(i18n("Replace")); 0129 KConfigGroup conf(KSharedConfig::openConfig(), "ImportDialog"); 0130 cbPosition->setCurrentIndex(conf.readEntry("Position", 0)); 0131 0132 cbPosition->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); 0133 grid->addWidget(cbPosition, 1, 1); 0134 0135 //add the "Import to"-frame to the layout after the first main widget 0136 vLayout->insertWidget(1, frameAddTo); 0137 0138 connect(tbNewDataContainer, &QToolButton::clicked, this, &ImportDialog::newDataContainerMenu); 0139 connect(cbAddTo, &TreeViewComboBox::currentModelIndexChanged, this, &ImportDialog::checkOkButton); 0140 } 0141 0142 void ImportDialog::setCurrentIndex(const QModelIndex& index) { 0143 DEBUG("ImportFileDialog::setCurrentIndex()"); 0144 QDEBUG(" index =" << index); 0145 cbAddTo->setCurrentModelIndex(index); 0146 QDEBUG("cbAddTo->currentModelIndex() =" << cbAddTo->currentModelIndex()); 0147 checkOkButton(); 0148 } 0149 0150 void ImportDialog::newDataContainer(QAction* action) { 0151 DEBUG("ImportDialog::newDataContainer()"); 0152 QString name = selectedObject(); 0153 QString type = action->iconText().split(' ')[1]; 0154 if (name.isEmpty()) 0155 name = action->iconText(); 0156 0157 bool ok; 0158 // child widgets can't have own icons 0159 auto* dlg = new QInputDialog(this); 0160 name = dlg->getText(this, i18n("Add %1", action->iconText()), i18n("%1 name:", type), QLineEdit::Normal, name, &ok); 0161 if (ok) { 0162 AbstractAspect* aspect; 0163 int actionIndex = m_newDataContainerMenu->actions().indexOf(action); 0164 if (actionIndex == 0) 0165 aspect = new Workbook(name); 0166 else if (actionIndex == 1) 0167 aspect = new Spreadsheet(name); 0168 else 0169 aspect = new Matrix(name); 0170 0171 m_mainWin->addAspectToProject(aspect); 0172 QDEBUG("cbAddTo->setCurrentModelIndex() to " << m_mainWin->model()->modelIndexOfAspect(aspect)); 0173 cbAddTo->setCurrentModelIndex(m_mainWin->model()->modelIndexOfAspect(aspect)); 0174 checkOkButton(); 0175 0176 //select "Replace" since this is the most common case when importing into a newly created container 0177 cbPosition->setCurrentIndex(2); 0178 } 0179 0180 delete dlg; 0181 } 0182 0183 void ImportDialog::newDataContainerMenu() { 0184 m_newDataContainerMenu->exec( tbNewDataContainer->mapToGlobal(tbNewDataContainer->rect().bottomLeft())); 0185 }