BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
JobPropertiesTableModel.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Job/JobPropertiesTableModel.cpp
6 //! @brief Implements class JobPropertiesTableModel
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include "GUI/Model/Job/JobItem.h"
17 #include <QLocale>
18 
19 namespace {
20 
21 namespace Column {
22 
23 enum Columns { Name, Value };
24 }
25 const QString ColumnNames[] = {"Name", "Value"};
26 const int NumColumns = (int)std::size(ColumnNames);
27 
28 namespace Row {
29 
30 enum Rows { Name, Sample, Instrument, Status, Begin, End, Duration };
31 }
32 const QString RowNames[] = {"Name", "Sample", "Instrument", "Status", "Begin", "End", "Duration"};
33 const int NumRows = (int)std::size(RowNames);
34 
35 const QString ModelDateShortFormat = "yyyy.MM.dd hh:mm:ss";
36 
37 } // namespace
38 
39 //==================================================================================================
40 // JobPropertiesTableModel
41 //==================================================================================================
42 
43 //--------------------------------------------------------------------------------------------------
44 // public member functions
45 //--------------------------------------------------------------------------------------------------
46 
48  : QAbstractTableModel(parent)
49  , m_item(nullptr)
50 {
51 }
52 
54 {
55  if (m_item)
56  disconnect(m_item, nullptr, this, nullptr);
57 }
58 
59 int JobPropertiesTableModel::rowCount(const QModelIndex& parent) const
60 {
61  if (!parent.isValid() && m_item)
62  return NumRows;
63  return 0;
64 }
65 
66 int JobPropertiesTableModel::columnCount(const QModelIndex& parent) const
67 {
68  if (!parent.isValid() && m_item)
69  return NumColumns;
70  return 0;
71 }
72 
73 QVariant JobPropertiesTableModel::data(const QModelIndex& index, int role) const
74 {
75  if ((role != Qt::DisplayRole && role != Qt::EditRole && role != Qt::ToolTipRole)
76  || index.column() < 0 || index.column() >= NumColumns || index.row() < 0
77  || index.row() >= NumRows || !m_item)
78  return QVariant();
79 
80  switch (index.column()) {
81  case Column::Name:
82  return RowNames[index.row()];
83  case Column::Value: {
84  switch (index.row()) {
85  case Row::Name:
86  return m_item->jobName();
87  case Row::Sample:
88  return m_item->sampleName();
89  case Row::Instrument:
90  return m_item->instrumentName();
91  case Row::Status:
93  case Row::Begin:
94  if (role == Qt::ToolTipRole)
95  return QLocale().toString(m_item->beginTime(), QLocale::LongFormat);
96  return m_item->beginTime().toString(ModelDateShortFormat);
97  case Row::End:
98  if (role == Qt::ToolTipRole)
99  return QLocale().toString(m_item->endTime(), QLocale::LongFormat);
100  return m_item->endTime().toString(ModelDateShortFormat);
101  case Row::Duration: {
102  std::optional<size_t> duration = m_item->duration();
103  if (duration)
104  return QString("%1 s").arg(duration.value() / 1000., 0, 'f', 3);
105  return QVariant();
106  }
107  default:
108  return QVariant();
109  }
110  }
111  default:
112  return QVariant();
113  }
114 }
115 
116 QVariant JobPropertiesTableModel::headerData(int section, Qt::Orientation orientation,
117  int role) const
118 {
119  if (role == Qt::DisplayRole && orientation == Qt::Horizontal && section >= 0
120  && section < NumColumns)
121  return ColumnNames[section];
122  return QVariant();
123 }
124 
125 Qt::ItemFlags JobPropertiesTableModel::flags(const QModelIndex& index) const
126 {
127  Qt::ItemFlags f = QAbstractTableModel::flags(index);
128  if (index.column() == Column::Value && index.row() == Row::Name && m_item)
129  f.setFlag(Qt::ItemIsEditable);
130  return f;
131 }
132 
133 bool JobPropertiesTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
134 {
135  if (role != Qt::EditRole || index.column() != Column::Value || index.row() != Row::Name
136  || !m_item)
137  return false;
138  m_item->setJobName(value.toString());
139  return true;
140 }
141 
143 {
144  beginResetModel();
145  if (m_item)
146  disconnect(m_item, nullptr, this, nullptr);
147  m_item = item;
148  if (m_item)
150  endResetModel();
151 }
152 
153 //--------------------------------------------------------------------------------------------------
154 // private member functions
155 //--------------------------------------------------------------------------------------------------
156 
158 {
159  // name
160  connect(m_item, &JobItem::jobNameChanged, this, [=](const QString&) {
161  emit dataChanged(index(Row::Name, Column::Value), index(Row::Name, Column::Value),
162  {Qt::DisplayRole, Qt::EditRole});
163  });
164 
165  // status
166  connect(m_item, &JobItem::jobStatusChanged, this, [=](const JobStatus) {
167  emit dataChanged(index(Row::Status, Column::Value), index(Row::Status, Column::Value),
168  {Qt::DisplayRole, Qt::EditRole});
169  });
170 
171  // begin time
172  connect(m_item, &JobItem::jobBeginTimeChanged, this, [=](const QDateTime&) {
173  emit dataChanged(index(Row::Begin, Column::Value), index(Row::Begin, Column::Value),
174  {Qt::DisplayRole, Qt::EditRole});
175  });
176 
177  // end time and duration
178  connect(m_item, &JobItem::jobEndTimeChanged, this, [=](const QDateTime&) {
179  emit dataChanged(index(Row::End, Column::Value), index(Row::Duration, Column::Value),
180  {Qt::DisplayRole, Qt::EditRole});
181  });
182 }
Defines class JobItem.
Defines class JobPropertiesWidget.
QString jobStatusToString(JobStatus status)
get a string representation of the status
Definition: JobStatus.cpp:32
JobStatus
The JobStatus enum lists the possible states of a job.
Definition: JobStatus.h:22
Assembles beam, detector and their relative positions with respect to the sample.
Definition: Instrument.h:26
void jobStatusChanged(const JobStatus status)
QString instrumentName() const
Definition: JobItem.cpp:325
void jobEndTimeChanged(const QDateTime &end_time)
void setJobName(const QString &name)
Definition: JobItem.cpp:89
QDateTime endTime() const
Definition: JobItem.cpp:170
void jobBeginTimeChanged(const QDateTime &begin_time)
QString sampleName() const
Definition: JobItem.cpp:320
void jobNameChanged(const QString &name)
QString jobName() const
Definition: JobItem.cpp:84
QDateTime beginTime() const
Definition: JobItem.cpp:159
JobStatus getStatus() const
Definition: JobItem.cpp:106
std::optional< size_t > duration() const
if begin and end time are both available the duration in ms, otherwise empty
Definition: JobItem.cpp:181
Qt::ItemFlags flags(const QModelIndex &index) const override
JobPropertiesTableModel(QObject *parent=nullptr)
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
int columnCount(const QModelIndex &parent=QModelIndex()) const override
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
constexpr auto Value("value")