QXRD  0.11.16
qxrdcalibrantdialog.cpp
Go to the documentation of this file.
1 #include "qxrdcalibrantdialog.h"
2 #include "ui_qxrdcalibrantdialog.h"
5 #include "qxrdcalibrantlibrary.h"
6 #include "qxrdcalibrant.h"
7 #include "qxrdcenterfinder.h"
8 #include <QMenu>
9 #include <QAction>
10 #include <QClipboard>
12 #include "qxrdexperiment.h"
13 
15  QDockWidget(),
16  m_Experiment(expt),
17  m_CenterFinder(cf)
18 {
19  setupUi(this);
20 
23 
24  if (cfp && exp) {
25  m_CalibrantLibrary = exp->calibrantLibrary();
26  m_CalibrantLibraryModel = exp->calibrantLibraryModel();
27 
28  m_CalibrantDSpacings = exp->calibrantDSpacings();
29  m_CalibrantDSpacingsModel = exp->calibrantDSpacingsModel();
30  }
31 
34 
35  if (lib && dsp) {
36  m_CalibrantTableView -> setModel(lib.data());
37  m_CalibrantDSpacingsView -> setModel(dsp.data());
38  }
39 
40  m_CalibrantTableView->horizontalHeader()->setStretchLastSection(true);
41  m_CalibrantDSpacingsView->horizontalHeader()->setStretchLastSection(true);
42 
43 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
44  m_CalibrantTableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
45  m_CalibrantTableView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
46  m_CalibrantDSpacingsView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
47  m_CalibrantDSpacingsView->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
48 #else
49  m_CalibrantTableView->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
50  m_CalibrantTableView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
51  m_CalibrantDSpacingsView->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
52  m_CalibrantDSpacingsView->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
53 #endif
54 
55  connect(m_CalibrantTableView, &QTableView::customContextMenuRequested, this, &QxrdCalibrantDialog::calibrantTableContextMenu);
56  connect(m_CalibrantDSpacingsView, &QTableView::customContextMenuRequested, this, &QxrdCalibrantDialog::calibrantDSpacingsContextMenu);
57 
58  connect(m_CalibrantTableView, &QTableView::clicked, this, &QxrdCalibrantDialog::onCalibrantClick);
59  connect(m_CalibrantTableView, &QTableView::doubleClicked, this, &QxrdCalibrantDialog::onCalibrantDoubleClick);
60 
61  connect(lib.data(), &QAbstractItemModel::dataChanged,
63 
64  if (cfp) {
65  connect(cfp->prop_Energy(), &QcepDoubleProperty::valueChanged,
67  }
68 
70 }
71 
73 {
74 }
75 
76 
78 {
83 
84  if (lib && dsp && cf && model) {
85  double energy = cf->get_Energy();
86 
87  dsp->clear();
88 
89  for (int i=0; i<lib->count(); i++) {
90  QxrdCalibrantPtr cal = lib->calibrant(i);
91 
92  if (cal && cal->get_IsUsed()) {
93  dsp->merge(cal->dSpacings(energy));
94  }
95  }
96 
97  model -> everythingChanged(dsp->count());
98  }
99 }
100 
102 {
103  QModelIndex index = m_CalibrantTableView->indexAt(pos);
105  QxrdCalibrantPtr cal;
106 
107  if (index.isValid() && lib) {
108  cal = lib->calibrant(index.row());
109  }
110 
111  QMenu *menu = new QMenu(this);
112  QAction *copy = menu->addAction("Copy");
113  QAction *dupe = NULL;
114  QAction *props = NULL;
115  QAction *del = NULL;
116 
117  if (cal) {
118  dupe = menu->addAction(tr("Duplicate Calibrant %1").arg(cal->get_Name()));
119  props = menu->addAction(tr("Calibrant %1 Properties...").arg(cal->get_Name()));
120 
121  if (!cal->isLocked()) {
122  del = menu->addAction(tr("Delete Calibrant %1...").arg(cal->get_Name()));
123  }
124  }
125 
126  QAction *chosen = menu->exec(m_CalibrantTableView->viewport()->mapToGlobal(pos));
127 
128  if (chosen) {
129  if (chosen == copy) {
130  doCopyFromTable(m_CalibrantTableView);
131  } else if (chosen == dupe) {
132  doDuplicateCalibrant(index.row());
133  } else if (chosen == props) {
134  doCalibrantProperties(index.row());
135  } else if (chosen == del) {
136  doDeleteCalibrant(index.row());
137  }
138  }
139 }
140 
142 {
143  QModelIndex index = m_CalibrantDSpacingsView->indexAt(pos);
144  QMenu *menu = new QMenu(this);
145  QAction *copy = menu->addAction("Copy");
146  QAction *chosen = menu->exec(m_CalibrantDSpacingsView->viewport()->mapToGlobal(pos));
147 
148  if (chosen == copy) {
149  doCopyFromTable(m_CalibrantDSpacingsView);
150  }
151 }
152 
154 {
155  QAbstractItemModel *model = table->model();
156  QItemSelectionModel *selected = table->selectionModel();
157 
158  int nRows = model->rowCount();
159  int nCols = model->columnCount();
160 
161  int firstRowSel = -1, lastRowSel = -1;
162  int firstColSel = -1, lastColSel = -1;
163 
164  for (int row = 0; row<nRows; row++) {
165  for (int col = 0; col<nCols; col++) {
166  if (selected->isSelected(model->index(row, col))) {
167  if (firstRowSel == -1 || firstRowSel > row) {
168  firstRowSel = row;
169  }
170 
171  if (firstColSel == -1 || firstColSel > col) {
172  firstColSel = col;
173  }
174 
175  if (lastRowSel == -1 || lastRowSel < row) {
176  lastRowSel = row;
177  }
178 
179  if (lastColSel == -1 || lastColSel < col) {
180  lastColSel = col;
181  }
182  }
183  }
184  }
185 
186  if (firstRowSel >= 0 && lastRowSel >= 0 && firstColSel >= 0 && lastColSel >= 0) {
187  QString result;
188 
189  for (int row = firstRowSel; row <= lastRowSel; row++) {
190  for (int col = firstColSel; col <= lastColSel; col++) {
191  if (col != firstColSel) {
192  if (selected->columnIntersectsSelection(col,QModelIndex())) {
193  result.append("\t");
194  }
195  }
196 
197  if (selected->isSelected(model->index(row,col))) {
198  result.append(model->data(model->index(row,col)).toString());
199  }
200  }
201 
202  result.append("\n");
203  }
204 
205  QClipboard *clip = QApplication::clipboard();
206 
207  clip->setText(result);
208  } else {
209  printf("No cells selected\n");
210  }
211 }
212 
213 void QxrdCalibrantDialog::onCalibrantClick(const QModelIndex &item)
214 {
215 // printf("Single click [%d,%d]\n", item.column(), item.row());
216 }
217 
218 void QxrdCalibrantDialog::onCalibrantDoubleClick(const QModelIndex &item)
219 {
220 // printf("Double click [%d,%d]\n", item.column(), item.row());
221 
222  if (item.column() == 0) {
224 
225  if (lib) {
226  lib->toggleIsUsed(item.row());
227  }
228  }
229 }
230 
232 {
233  printf("Duplicate Calibrant %d\n", n);
234 }
235 
237 {
238  printf("Delete Calibrant %d\n", n);
239 }
240 
242 {
245 
246  if (lib && mdl) {
247  QxrdCalibrantPtr cal = lib->calibrant(n);
248 
249  QxrdCalibrantPropertiesDialog dlg(this, cal);
250 
251  if (dlg.exec() == QDialog::Accepted) {
252  mdl->calibrantChanged(n);
253  }
254  }
255 }
QSharedPointer< QxrdExperiment > QxrdExperimentPtr
QSharedPointer< QxrdCenterFinder > QxrdCenterFinderPtr
QxrdExperimentWPtr m_Experiment
QSharedPointer< QxrdCalibrantLibraryModel > QxrdCalibrantLibraryModelPtr
void doCopyFromTable(QTableView *table)
QWeakPointer< QxrdCenterFinder > QxrdCenterFinderWPtr
QxrdCalibrantDSpacingsModelWPtr m_CalibrantDSpacingsModel
QxrdCalibrantDialog(QxrdExperimentPtr expt, QxrdCenterFinderWPtr cf)
QxrdCalibrantDSpacingsWPtr m_CalibrantDSpacings
QSharedPointer< QxrdCalibrantLibrary > QxrdCalibrantLibraryPtr
QSharedPointer< QxrdCalibrantDSpacingsModel > QxrdCalibrantDSpacingsModelPtr
QSharedPointer< QxrdCalibrant > QxrdCalibrantPtr
QxrdCalibrantLibraryModelWPtr m_CalibrantLibraryModel
QSharedPointer< QxrdCalibrantDSpacings > QxrdCalibrantDSpacingsPtr
void onCalibrantClick(const QModelIndex &item)
void onCalibrantDoubleClick(const QModelIndex &item)
void calibrantDSpacingsContextMenu(const QPoint &pos)
void valueChanged(double val, int index)
QxrdCalibrantLibraryWPtr m_CalibrantLibrary
QxrdCenterFinderWPtr m_CenterFinder
void calibrantTableContextMenu(const QPoint &pos)