BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
RealDataSelectorWidget Class Reference

Description

The RealDataSelectorWidget represents left panel of ImportDataView. Contains a tree to select data set (m_itemTree & m_itemTreeModel) and properties of currently selected data (RealDataPropertiesWidget).

Definition at line 38 of file RealDataSelectorWidget.h.

Inheritance diagram for RealDataSelectorWidget:
[legend]
Collaboration diagram for RealDataSelectorWidget:
[legend]

Signals

void selectionChanged (RealDataItem *)
 

Public Member Functions

 RealDataSelectorWidget (QWidget *parent, ProjectDocument *document)
 
RealDataItemcurrentItem ()
 
QSize minimumSizeHint () const override
 
void setCurrentItem (RealDataItem *item)
 
QSize sizeHint () const override
 

Public Attributes

QAction * m_import1dDataAction
 
QAction * m_import2dDataAction
 
QAction * m_removeDataAction
 
QAction * m_renameDataAction
 

Private Member Functions

QModelIndex currentIndex ()
 
QList< QAction * > getOverlayActions (const QModelIndex &index, bool asHover)
 
void importData1D ()
 
void importData2D ()
 
void onContextMenuRequest (const QPoint &point)
 
void onSelectionChanged ()
 
void removeCurrentItem ()
 
void renameCurrentItem ()
 
void updateActionEnabling ()
 
void updateActionEnabling (const RealDataItem *item) const
 
void updateFunctionalities ()
 

Private Attributes

ProjectDocumentm_document
 
QTreeView * m_itemTree
 
RealDataTreeModelm_itemTreeModel
 
RealDataPropertiesWidgetm_propertiesWidget
 

Constructor & Destructor Documentation

◆ RealDataSelectorWidget()

RealDataSelectorWidget::RealDataSelectorWidget ( QWidget *  parent,
ProjectDocument document 
)

Definition at line 40 of file RealDataSelectorWidget.cpp.

41  : QWidget(parent)
42  , m_itemTree(new QTreeView(this))
43  , m_itemTreeModel(new RealDataTreeModel(this, document->realDataModel()))
44  , m_propertiesWidget(new RealDataPropertiesWidget(this, document))
45  , m_document(document)
46  , m_import1dDataAction(new QAction(this))
47  , m_import2dDataAction(new QAction(this))
48  , m_renameDataAction(new QAction(this))
49  , m_removeDataAction(new QAction(this))
50 {
51  setProperty("stylable", true);
52  setAttribute(Qt::WA_StyledBackground, true);
53 
54  setMinimumSize(250, 600);
55  setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
56  setWindowTitle("RealDataSelectorWidget");
57 
58  m_import2dDataAction->setText("Import 2D data");
59  m_import2dDataAction->setIcon(QIcon(":/images/shape-square-plus.svg"));
60  m_import2dDataAction->setIconText("2D");
61  m_import2dDataAction->setToolTip("Import 2D data");
62  connect(m_import2dDataAction, &QAction::triggered, [this]() { importData2D(); });
63 
64  m_import1dDataAction->setText("Import 1D data");
65  m_import1dDataAction->setIcon(QIcon(":/images/shape-square-plus.svg"));
66  m_import1dDataAction->setIconText("1D");
67  m_import1dDataAction->setToolTip("Import 1D data");
68  connect(m_import1dDataAction, &QAction::triggered, [this]() { importData1D(); });
69 
70  m_renameDataAction->setText("Rename");
71  m_renameDataAction->setIcon(QIcon()); // #baTODO: Icon needed?
72  m_renameDataAction->setIconText("Rename");
73  m_renameDataAction->setToolTip("Rename data");
74  connect(m_renameDataAction, &QAction::triggered, this,
76 
77  m_removeDataAction->setText("Remove");
78  m_removeDataAction->setIcon(QIcon(":/images/delete.svg"));
79  m_removeDataAction->setIconText("Remove");
80  m_removeDataAction->setToolTip("Remove selected data");
81  connect(m_removeDataAction, &QAction::triggered, this,
83 
84  // #baimport ++ following line necessary for tree as well? relevant for RealItems at all?
85  // m_itemTree->setAttribute(Qt::WA_MacShowFocusRect, false);
86  m_itemTree->setItemsExpandable(false);
87  m_itemTree->setRootIsDecorated(false);
88  m_itemTree->setHeaderHidden(true);
89  m_itemTree->setContextMenuPolicy(Qt::CustomContextMenu);
90  m_itemTree->setModel(m_itemTreeModel);
91 
92  auto* splitter = new QSplitter;
93  splitter->setOrientation(Qt::Vertical);
94  splitter->addWidget(m_itemTree);
95  splitter->addWidget(m_propertiesWidget);
96  splitter->setChildrenCollapsible(true);
97 
98  auto* mainLayout = new QVBoxLayout;
99  mainLayout->setMargin(0);
100  mainLayout->setSpacing(0);
101  mainLayout->setContentsMargins(0, 0, 0, 0);
102  mainLayout->addWidget(splitter);
103 
104  setLayout(mainLayout);
105 
106  connect(m_itemTree, &QTreeView::customContextMenuRequested, this,
108 
109  connect(m_document, &ProjectDocument::modified, this,
111 
112  connect(m_itemTreeModel, &QAbstractItemModel::modelReset,
113  [this]() { m_itemTree->expandAll(); });
114 
115  connect(m_itemTreeModel, &QAbstractItemModel::rowsInserted,
116  [this]() { m_itemTree->expandAll(); });
117 
118  connect(m_itemTree->selectionModel(), &QItemSelectionModel::selectionChanged, this,
119  &RealDataSelectorWidget::onSelectionChanged, Qt::UniqueConnection);
120 
121  m_itemTree->expandAll();
124 
125  // ensure a current item when widget is shown
127  if (currentItem())
129 
131  m_itemTree, [=](const QModelIndex& i, bool h) { return getOverlayActions(i, h); });
132 }
static void install(QAbstractItemView *view, FnGetActions fnGetActions)
RealDataModel * realDataModel() const
void modified()
Emitted for any modifications in the document.
The RealDataPropertiesWidget class holds instrument selector to link with RealDataItem....
void selectionChanged(RealDataItem *)
void setCurrentItem(RealDataItem *item)
RealDataTreeModel * m_itemTreeModel
void onContextMenuRequest(const QPoint &point)
QList< QAction * > getOverlayActions(const QModelIndex &index, bool asHover)
RealDataPropertiesWidget * m_propertiesWidget
Tree model for real data item selection. Used for the tree in the import view.
RealDataItem * topMostItem() const
The topmost visible item. Can be null of course.

References currentItem(), getOverlayActions(), importData1D(), importData2D(), ItemViewOverlayButtons::install(), m_document, m_import1dDataAction, m_import2dDataAction, m_itemTree, m_itemTreeModel, m_propertiesWidget, m_removeDataAction, m_renameDataAction, ProjectDocument::modified(), onContextMenuRequest(), onSelectionChanged(), removeCurrentItem(), renameCurrentItem(), selectionChanged(), setCurrentItem(), RealDataTreeModel::topMostItem(), updateActionEnabling(), and updateFunctionalities().

Here is the call graph for this function:

Member Function Documentation

◆ currentIndex()

QModelIndex RealDataSelectorWidget::currentIndex ( )
private

Definition at line 157 of file RealDataSelectorWidget.cpp.

158 {
159  return m_itemTree->selectionModel()->currentIndex();
160 }

References m_itemTree.

Referenced by currentItem(), and renameCurrentItem().

◆ currentItem()

RealDataItem * RealDataSelectorWidget::currentItem ( )

Definition at line 144 of file RealDataSelectorWidget.cpp.

145 {
147 }
RealDataItem * itemForIndex(const QModelIndex &index) const

References currentIndex(), RealDataTreeModel::itemForIndex(), and m_itemTreeModel.

Referenced by ImportDataView::ImportDataView(), RealDataSelectorWidget(), onSelectionChanged(), removeCurrentItem(), and updateActionEnabling().

Here is the call graph for this function:

◆ getOverlayActions()

QList< QAction * > RealDataSelectorWidget::getOverlayActions ( const QModelIndex &  index,
bool  asHover 
)
private

Definition at line 162 of file RealDataSelectorWidget.cpp.

163 {
164  if (m_itemTreeModel->isHeadline(index)) {
165  if (asHover)
166  return {};
167 
168  return (index == m_itemTreeModel->indexOfHeadline(1))
169  ? QList<QAction*>({m_import1dDataAction})
170  : QList<QAction*>({m_import2dDataAction});
171  }
172 
173  // -- index belongs to item
174  if (!asHover)
175  return {};
176 
177  auto* item = m_itemTreeModel->itemForIndex(index);
178  if (item == nullptr)
179  return {};
180 
181  auto* removeAction = new QAction(this);
182  removeAction->setText("Remove");
183  removeAction->setIcon(QIcon(":/images/delete.svg"));
184  removeAction->setIconText("Remove");
185  removeAction->setToolTip("Remove this data");
186  connect(removeAction, &QAction::triggered, [=]() { m_itemTreeModel->removeItem(item); });
187 
188  return {removeAction};
189 }
bool isHeadline(const QModelIndex &index) const
void removeItem(RealDataItem *item)
QModelIndex indexOfHeadline(int rank) const

References RealDataTreeModel::indexOfHeadline(), RealDataTreeModel::isHeadline(), RealDataTreeModel::itemForIndex(), m_import1dDataAction, m_import2dDataAction, m_itemTreeModel, and RealDataTreeModel::removeItem().

Referenced by RealDataSelectorWidget().

Here is the call graph for this function:

◆ importData1D()

void RealDataSelectorWidget::importData1D ( )
private

Definition at line 217 of file RealDataSelectorWidget.cpp.

218 {
219  QMap<QString, AbstractDataLoader1D*> loaderOfFilter;
220  QString filters;
221  for (auto* loader : DataLoaders1D::instance().loaders()) {
222  const QString filter =
223  loader->name() + " (*.txt *.csv *.dat)"; // #baimport - take file filters from loader
224  loaderOfFilter[filter] = loader;
225 
226  if (!filters.isEmpty())
227  filters += ";;";
228  filters += filter;
229  }
230  filters += ";;Other (*.*)";
231 
232  QString selectedFilter = ProjectManager::instance()->recentlyUsedImportFilter1D();
233  const QString dirname = ProjectManager::instance()->userImportDir();
234 
235  const QStringList fileNames = QFileDialog::getOpenFileNames(
236  Q_NULLPTR, "Open Intensity Files", dirname, filters, &selectedFilter,
237  appSettings->useNativeFileDialog() ? QFileDialog::Options()
238  : QFileDialog::DontUseNativeDialog);
239 
240  if (fileNames.isEmpty())
241  return;
242 
245 
246  const AbstractDataLoader* selectedLoader = loaderOfFilter.value(selectedFilter, nullptr);
247  for (const auto& fileName : fileNames) {
249  realDataItem->setDataName(QFileInfo(fileName).baseName());
250  realDataItem->setNativeFileName(fileName);
251 
252  const QString errorText =
253  GUI::View::ImportDataUtils::Import1dData(realDataItem, selectedLoader);
254  if (errorText.isEmpty())
255  setCurrentItem(realDataItem);
256  else {
257  m_itemTreeModel->removeItem(realDataItem);
258  GUI::View::Helpers::warning(this, "File import",
259  QString("The file '%1' could not be imported.")
260  .arg(QDir::toNativeSeparators(fileName)),
261  errorText);
262  }
263  }
264 }
ApplicationSettings * appSettings
global pointer to the instance
Abstract base class for all data loaders (classes to import real data).
bool useNativeFileDialog() const
static DataLoaders1D & instance()
The one and only instance.
void setRecentlyUsedImportFilter1D(const QString &filter)
static ProjectManager * instance()
void setImportDirFromFilePath(const QString &filePath)
Sets user import directory in system settings.
QString userImportDir() const
Returns directory name which was used by the user to import files.
QString recentlyUsedImportFilter1D() const
Provides access to experimental data, for display and fitting. Owns an AbstractDataLoader.
Definition: RealDataItem.h:33
void setDataName(const QString &name)
void setNativeFileName(const QString &filename)
The name from where the native data was originally imported.
RealDataItem * insertSpecularDataItem()
QString baseName(const QString &fileName)
Returns base name of file.
Definition: Path.cpp:133
void warning(QWidget *parent, const QString &title, const QString &text, const QString &detailedText)
Definition: MessageBox.cpp:37
QString Import1dData(RealDataItem *realDataItem, const AbstractDataLoader *selectedLoader)
Imports 1D data into the given item.

References appSettings, GUI::Util::Path::baseName(), GUI::View::ImportDataUtils::Import1dData(), RealDataTreeModel::insertSpecularDataItem(), DataLoaders1D::instance(), ProjectManager::instance(), m_itemTreeModel, ProjectManager::recentlyUsedImportFilter1D(), RealDataTreeModel::removeItem(), setCurrentItem(), RealDataItem::setDataName(), ProjectManager::setImportDirFromFilePath(), RealDataItem::setNativeFileName(), ProjectManager::setRecentlyUsedImportFilter1D(), ApplicationSettings::useNativeFileDialog(), ProjectManager::userImportDir(), and GUI::View::Helpers::warning().

Referenced by RealDataSelectorWidget().

Here is the call graph for this function:

◆ importData2D()

void RealDataSelectorWidget::importData2D ( )
private

Definition at line 266 of file RealDataSelectorWidget.cpp.

267 {
268  QMap<QString, IOFactory::LoaderSelector> loaderOfFilter;
269  QString filters;
270 
271  const auto addFilter = [&](IOFactory::LoaderSelector loaderSelector, const QString& filter) {
272  if (!filters.isEmpty())
273  filters += ";;";
274  filters += filter;
275  loaderOfFilter[filter] = loaderSelector;
276  };
277  addFilter(IOFactory::tiff, "TIFF (*.tif *.tiff *.tif.gz)");
278  addFilter(IOFactory::nicos, "Nicos/SANSDRaw (*.001)");
279  addFilter(IOFactory::bornagain, "BornAgain (*.int.gz)");
280  addFilter(IOFactory::automatic, "CSV (*.txt *.csv *.dat)");
281  addFilter(IOFactory::automatic, "All (*.*)");
282 
283  QString selectedFilter = ProjectManager::instance()->recentlyUsedImportFilter2D();
284 
285  const QString dirname = ProjectManager::instance()->userImportDir();
286  const QStringList fileNames = QFileDialog::getOpenFileNames(
287  Q_NULLPTR, "Open Intensity Files", dirname, filters, &selectedFilter,
288  appSettings->useNativeFileDialog() ? QFileDialog::Options()
289  : QFileDialog::DontUseNativeDialog);
290  if (fileNames.isEmpty())
291  return;
292 
295 
296  const auto selectedLoader = loaderOfFilter.value(selectedFilter, IOFactory::automatic);
297 
298  for (const auto& fileName : fileNames) {
299  std::unique_ptr<Datafield> data =
300  GUI::View::ImportDataUtils::Import2dData(fileName, selectedLoader);
301  if (!data)
302  continue;
304  realDataItem->setDataName(QFileInfo(fileName).baseName());
305 
306  try {
307  if (data->rank() != 2)
308  throw Error("The content could not be interpreted correctly as 2-dimensional.");
309 
310  realDataItem->setDatafield(data.release());
311  setCurrentItem(realDataItem);
312  } catch (std::exception& ex) {
313  m_itemTreeModel->removeItem(realDataItem);
314 
315  const QString message = QString("Error while trying to read file\n\n'%1'\n\n%2")
316  .arg(fileName)
317  .arg(QString::fromStdString(std::string(ex.what())));
318  GUI::View::Helpers::warning(this, "File import", message);
319  }
320  }
321 }
QString recentlyUsedImportFilter2D() const
void setRecentlyUsedImportFilter2D(const QString &filter)
void setDatafield(Datafield *data)
Sets Datafield to underlying item. Creates it if not existing.
RealDataItem * insertIntensityDataItem()
std::unique_ptr< Datafield > Import2dData(const QString &fileName, IOFactory::LoaderSelector loader)
Imports 2D data, stores them as Datafield, and returns owning pointer.

References appSettings, GUI::Util::Path::baseName(), Error, GUI::View::ImportDataUtils::Import2dData(), RealDataTreeModel::insertIntensityDataItem(), ProjectManager::instance(), m_itemTreeModel, ProjectManager::recentlyUsedImportFilter2D(), RealDataTreeModel::removeItem(), setCurrentItem(), RealDataItem::setDatafield(), RealDataItem::setDataName(), ProjectManager::setImportDirFromFilePath(), ProjectManager::setRecentlyUsedImportFilter2D(), ApplicationSettings::useNativeFileDialog(), ProjectManager::userImportDir(), and GUI::View::Helpers::warning().

Referenced by RealDataSelectorWidget().

Here is the call graph for this function:

◆ minimumSizeHint()

QSize RealDataSelectorWidget::minimumSizeHint ( ) const
override

Definition at line 139 of file RealDataSelectorWidget.cpp.

140 {
141  return QSize(128, 200);
142 }

◆ onContextMenuRequest()

void RealDataSelectorWidget::onContextMenuRequest ( const QPoint &  point)
private

Definition at line 198 of file RealDataSelectorWidget.cpp.

199 {
200  auto* realDataItemAtPoint = m_itemTreeModel->itemForIndex(m_itemTree->indexAt(point));
201  updateActionEnabling(realDataItemAtPoint);
202 
203  QMenu menu;
204  menu.setToolTipsVisible(true);
205 
206  if (realDataItemAtPoint != nullptr) {
207  menu.addAction(m_renameDataAction);
208  menu.addAction(m_removeDataAction);
209  menu.addSeparator();
210  }
211 
212  menu.addAction(m_import2dDataAction);
213  menu.addAction(m_import1dDataAction);
214  menu.exec(m_itemTree->mapToGlobal(point));
215 }

References RealDataTreeModel::itemForIndex(), m_import1dDataAction, m_import2dDataAction, m_itemTree, m_itemTreeModel, m_removeDataAction, m_renameDataAction, and updateActionEnabling().

Referenced by RealDataSelectorWidget().

Here is the call graph for this function:

◆ onSelectionChanged()

void RealDataSelectorWidget::onSelectionChanged ( )
private

Definition at line 191 of file RealDataSelectorWidget.cpp.

192 {
196 }
void setItem(RealDataItem *item)
Set current RealDataItem to display in instrument selector.

References currentItem(), m_propertiesWidget, selectionChanged(), RealDataPropertiesWidget::setItem(), and updateActionEnabling().

Referenced by RealDataSelectorWidget().

Here is the call graph for this function:

◆ removeCurrentItem()

void RealDataSelectorWidget::removeCurrentItem ( )
private

Definition at line 334 of file RealDataSelectorWidget.cpp.

335 {
337 }

References currentItem(), m_itemTreeModel, and RealDataTreeModel::removeItem().

Referenced by RealDataSelectorWidget().

Here is the call graph for this function:

◆ renameCurrentItem()

void RealDataSelectorWidget::renameCurrentItem ( )
private

Definition at line 323 of file RealDataSelectorWidget.cpp.

324 {
325  if (currentIndex().isValid()) {
326  // First delete any existing index widget. This is necessary if overlay buttons are used,
327  // because these buttons use the indexWidget() functionality, which itself uses the same
328  // internals as an item editor
329  m_itemTree->setIndexWidget(currentIndex(), nullptr);
330  m_itemTree->edit(currentIndex());
331  }
332 }

References currentIndex(), and m_itemTree.

Referenced by RealDataSelectorWidget().

Here is the call graph for this function:

◆ selectionChanged

void RealDataSelectorWidget::selectionChanged ( RealDataItem )
signal

◆ setCurrentItem()

void RealDataSelectorWidget::setCurrentItem ( RealDataItem item)

Definition at line 149 of file RealDataSelectorWidget.cpp.

150 {
151  m_itemTree->selectionModel()->clearSelection();
152  QModelIndex index = m_itemTreeModel->indexForItem(item);
153  if (index.isValid())
154  m_itemTree->selectionModel()->setCurrentIndex(index, QItemSelectionModel::SelectCurrent);
155 }
QModelIndex indexForItem(RealDataItem *item) const

References RealDataTreeModel::indexForItem(), m_itemTree, and m_itemTreeModel.

Referenced by RealDataSelectorWidget(), importData1D(), and importData2D().

Here is the call graph for this function:

◆ sizeHint()

QSize RealDataSelectorWidget::sizeHint ( ) const
override

Definition at line 134 of file RealDataSelectorWidget.cpp.

135 {
136  return QSize(200, 400);
137 }

◆ updateActionEnabling() [1/2]

void RealDataSelectorWidget::updateActionEnabling ( )
private

Definition at line 339 of file RealDataSelectorWidget.cpp.

340 {
342 }

References currentItem().

Referenced by RealDataSelectorWidget(), onContextMenuRequest(), and onSelectionChanged().

Here is the call graph for this function:

◆ updateActionEnabling() [2/2]

void RealDataSelectorWidget::updateActionEnabling ( const RealDataItem item) const
private

Definition at line 344 of file RealDataSelectorWidget.cpp.

345 {
346  m_import2dDataAction->setEnabled(true);
347  m_import1dDataAction->setEnabled(true);
348 
349  m_removeDataAction->setEnabled(item != nullptr);
350  m_renameDataAction->setEnabled(item != nullptr);
351 }

References m_import1dDataAction, m_import2dDataAction, m_removeDataAction, and m_renameDataAction.

◆ updateFunctionalities()

void RealDataSelectorWidget::updateFunctionalities ( )
private

Definition at line 353 of file RealDataSelectorWidget.cpp.

354 {
355  QSet<int> visibleRanks;
357  visibleRanks << 1;
361  visibleRanks << 2;
362 
363  m_itemTreeModel->setVisibleRanks(visibleRanks);
364 }
Functionalities functionalities() const
void setVisibleRanks(QSet< int > visibleRanks)

References ProjectDocument::DepthProbe, ProjectDocument::functionalities(), ProjectDocument::Gisas, m_document, m_itemTreeModel, ProjectDocument::Offspec, RealDataTreeModel::setVisibleRanks(), and ProjectDocument::Specular.

Referenced by RealDataSelectorWidget().

Here is the call graph for this function:

Member Data Documentation

◆ m_document

ProjectDocument* RealDataSelectorWidget::m_document
private

Definition at line 72 of file RealDataSelectorWidget.h.

Referenced by RealDataSelectorWidget(), and updateFunctionalities().

◆ m_import1dDataAction

QAction* RealDataSelectorWidget::m_import1dDataAction

◆ m_import2dDataAction

QAction* RealDataSelectorWidget::m_import2dDataAction

◆ m_itemTree

QTreeView* RealDataSelectorWidget::m_itemTree
private

◆ m_itemTreeModel

◆ m_propertiesWidget

RealDataPropertiesWidget* RealDataSelectorWidget::m_propertiesWidget
private

Definition at line 70 of file RealDataSelectorWidget.h.

Referenced by RealDataSelectorWidget(), and onSelectionChanged().

◆ m_removeDataAction

QAction* RealDataSelectorWidget::m_removeDataAction

◆ m_renameDataAction

QAction* RealDataSelectorWidget::m_renameDataAction

The documentation for this class was generated from the following files: