BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
importfilewidget.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/dataloader/importfilewidget.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
17 #include "mvvm/utils/binutils.h"
18 #include "mvvm/utils/fileutils.h"
19 #include <QFileDialog>
20 #include <QItemSelectionModel>
21 #include <QListView>
22 #include <QMessageBox>
23 #include <QSettings>
24 #include <QStringListModel>
25 #include <QVBoxLayout>
26 
27 namespace gui2 {
28 
29 namespace {
30 const QString current_workdir_key = "currentworkdir";
31 
32 const QString workdir_setting_name()
33 {
34  return Constants::DataLoaderGroupKey + "/" + current_workdir_key;
35 }
36 
37 } // namespace
38 
40  : QWidget(parent), m_listView(new QListView), m_listModel(new QStringListModel(this))
41 {
42  readSettings();
43 
44  auto layout = new QVBoxLayout(this);
45  layout->setContentsMargins(0, 0, 0, 0);
46  layout->addWidget(m_listView);
47 
48  m_listView->setModel(m_listModel);
49  m_listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
50  m_listView->setAlternatingRowColors(true);
51  m_listView->setSelectionMode(QAbstractItemView::ExtendedSelection);
52 
53  connect(m_listView->selectionModel(), &QItemSelectionModel::selectionChanged, this,
55 }
56 
58 {
59  writeSettings();
60 }
61 
62 //! Summons dialog for file selections, update list view with file names.
63 
65 {
66  QFileDialog dialog(this, "Select one or more files to load", m_currentWorkdir);
67  dialog.setFileMode(QFileDialog::ExistingFiles);
68  dialog.setNameFilter("Text (*.txt *.csv *.dat);; Other (*.*)");
69  dialog.setOption(QFileDialog::DontUseNativeDialog);
70  QStringList file_names = dialog.exec() ? dialog.selectedFiles() : QStringList();
71 
72  file_names = validateForBinaryFiles(file_names);
73 
74  if (file_names.empty())
75  return;
76 
77  updateCurrentWorkdir(file_names);
78  addFileNamesToModel(file_names);
79 }
80 
81 //! Removes currently selected file
82 
84 {
85  auto selected = m_listView->selectionModel()->selectedIndexes();
86  while (!selected.empty()) {
87  m_listModel->removeRow(selected.back().row());
88  selected = m_listView->selectionModel()->selectedIndexes();
89  }
90 
91  emit fileNamesChanged();
92 
94 }
95 
96 //! Retuns the list of all file names imported by the user.
97 
98 QStringList ImportFileWidget::fileNames() const
99 {
100  return m_listModel->stringList();
101 }
102 
103 //! Retuns the list of currently selected file names.
104 
106 {
107  QStringList result;
108  for (auto index : m_listView->selectionModel()->selectedIndexes())
109  result.append(m_listModel->data(index).toString());
110  return result;
111 }
112 
113 //! Loads widget settings.
114 
116 {
117  QSettings settings;
118  m_currentWorkdir = QDir::homePath();
119 
120  if (settings.contains(workdir_setting_name()))
121  m_currentWorkdir = settings.value(workdir_setting_name()).toString();
122 }
123 
124 //! Writes widget settings.
125 
127 {
128  QSettings settings;
129  settings.setValue(workdir_setting_name(), m_currentWorkdir);
130 }
131 
132 //! Returns list validated for binary files.
133 
134 QStringList ImportFileWidget::validateForBinaryFiles(const QStringList& file_names)
135 {
136  QStringList result;
137  for (const auto& file_name : file_names) {
138  if (ModelView::Utils::is_binary(file_name.toStdString())) {
139  QMessageBox msgBox;
140  msgBox.setText(file_name + "\nmay be a binary file. Open it anyway?");
141  msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
142  int ret = msgBox.exec();
143  if (ret == QMessageBox::Yes)
144  result.push_back(file_name);
145  } else {
146  result.push_back(file_name);
147  }
148  }
149  return result;
150 }
151 
152 //! Updates current working dir.
153 
154 void ImportFileWidget::updateCurrentWorkdir(const QStringList& file_names)
155 {
156  auto file_name = file_names.back();
157  auto parent_path = ModelView::Utils::parent_path(file_name.toStdString());
158  m_currentWorkdir = QString::fromStdString(parent_path);
159 }
160 
161 //! Adds given list of file names to the model.
162 
163 void ImportFileWidget::addFileNamesToModel(const QStringList& file_names)
164 {
165  auto current_names = fileNames();
166  QStringList updated_names = current_names + file_names;
167  updated_names.removeDuplicates();
168  m_listModel->setStringList(updated_names);
169 
170  emit fileNamesChanged();
171 
173 }
174 
176 {
177  if (m_listView->selectionModel()->selectedIndexes().empty()) {
178  auto flags = QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows;
179  auto toSelect = m_listModel->index(m_listModel->rowCount() - 1);
180  m_listView->selectionModel()->select(toSelect, flags);
181  }
182 }
183 
184 } // namespace gui2
Defines class CLASS?
Defines class CLASS?
QStringList fileNames() const
Retuns the list of all file names imported by the user.
void updateCurrentWorkdir(const QStringList &file_names)
Updates current working dir.
void writeSettings()
Writes widget settings.
void readSettings()
Loads widget settings.
void onRemoveFileRequest()
Removes currently selected file.
ImportFileWidget(QWidget *parent=nullptr)
void addFileNamesToModel(const QStringList &file_names)
Adds given list of file names to the model.
void onAddFilesRequest()
Summons dialog for file selections, update list view with file names.
QStringList validateForBinaryFiles(const QStringList &file_names)
Returns list validated for binary files.
QStringList selectedFileNames() const
Retuns the list of currently selected file names.
QStringListModel * m_listModel
Defines class CLASS?
Defines class CLASS?
bool is_binary(const std::string &filename)
Returns true if file is binary.
Definition: binutils.cpp:33
MVVM_MODEL_EXPORT std::string parent_path(const std::string &path)
Returns the path to the parent directory.
Definition: fileutils.cpp:111
const QString DataLoaderGroupKey
Constants for QSettings.
Definition: app_constants.h:24
Based on Qt example "codeeditor" Copyright (C) 2016 The Qt Company Ltd.
Definition: app_constants.h:20