-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpositionlist.cpp
More file actions
415 lines (312 loc) · 12.3 KB
/
positionlist.cpp
File metadata and controls
415 lines (312 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "positionlist.h"
#include "ui_positionlist.h"
#include "additional_classes.h"
#include <QClipboard>
#include <QPainter>
NTableDelegate::NTableDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}
QWidget* NTableDelegate::createEditor(QWidget* parent,const QStyleOptionViewItem &option,const QModelIndex &index) const
{
QLineEdit* editor = new QLineEdit(parent);
QDoubleValidator* val = new QDoubleValidator(editor);
val->setNotation(QDoubleValidator::StandardNotation);
editor->setValidator(val);
return editor;
}
void NTableDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
double value = index.model()->data(index,Qt::EditRole).toDouble();
QLineEdit* line = static_cast<QLineEdit*>(editor);
line->setText(QString().setNum(value));
}
void NTableDelegate::setModelData(QWidget* editor,QAbstractItemModel* model,const QModelIndex &index) const
{
QLineEdit* line = static_cast<QLineEdit*>(editor);
QString value = line->text();
model->setData(index,value);
}
void NTableDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}
/*
class PListHeader : public QHeaderView {
public:
using QHeaderView::QHeaderView;
protected:
void paintSection(QPainter* painter, const QRect &rect, int logicalIndex) const override {
painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);
painter->restore();
if ( ! model() || logicalIndex != 3)
return;
QStyleOptionButton option;
option.init(this);
QRect checkbox_rect = style()->subElementRect(QStyle::SubElement::SE_CheckBoxIndicator, &option, this);
checkbox_rect.moveRight(rect.right());
bool checked = model()->headerData(logicalIndex, orientation(), Qt::CheckStateRole).toBool();
//option.text=QString("Do");
option.rect = checkbox_rect;
option.state = checked ? QStyle::State_On : QStyle::State_Off;
option.state = QStyle::State_Off;
style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter);
}
void mouseReleaseEvent(QMouseEvent* event) override
{
QHeaderView::mouseReleaseEvent(event);
if(model())
{
int section = logicalIndexAt(event->pos());
if (section >= 0)
{
bool checked = model()->headerData(section, orientation(), Qt::CheckStateRole).toBool();
model()->setHeaderData(section, orientation(), !checked, Qt::CheckStateRole);
viewport()->update();
}
}
}
};
*/
void QTableWidgetWithCopyPaste::copy() {
QString selected_text;
foreach ( QTableWidgetItem * item , selectedItems() )
selected_text += item->text() + '\n';
qApp->clipboard()->setText(selected_text);
}
void QTableWidgetWithCopyPaste::paste() {
QString selected_text = qApp->clipboard()->text();
QStringList cells = selected_text.split(QRegExp(QLatin1String("\\n|\\t| ")));
if ( cells.empty() )
return;
int ccell=0;
foreach ( QTableWidgetItem * item , selectedItems() )
item->setText( ccell < cells.size() ? cells.at(ccell++) : "");
}
void QTableWidgetWithCopyPaste::keyPressEvent(QKeyEvent * event)
{
if(event->matches(QKeySequence::Copy) )
copy();
else if(event->matches(QKeySequence::Paste) )
paste();
else
QTableWidget::keyPressEvent(event);
}
PositionList::PositionList(QWidget *parent)
: QWidget(parent)
, allOK(false)
, freezListUpdates(false)
, ui(new Ui::PositionList)
, motui(new QCaMotorGUI)
{
ui->setupUi(this);
setFocusProxy(ui->nof);
// replace header items with identical QTableWidgetOtem
for (int col=0; col < ui->list->columnCount() ; col++) {
QTableWidgetOtem *otem = new QTableWidgetOtem(ui->list->horizontalHeaderItem(col));
if (col==0)
otem->setProperty(configProp, "positions");
if (col==3)
otem->setProperty(configProp, "todos");
ui->list->setHorizontalHeaderItem(col, otem);
}
ui->list->setItemDelegateForColumn(0, new NTableDelegate(ui->list));
QHeaderView * header = ui->list->horizontalHeader();
//PListHeader * header = new PListHeader(Qt::Horizontal, ui->list);
//ui->list->setHorizontalHeader(header);
header->setStretchLastSection(false);
#if QT_VERSION >= 0x050000
header->setSectionResizeMode(0, QHeaderView::Stretch);
header->setSectionResizeMode(1, QHeaderView::Fixed);
header->setSectionResizeMode(2, QHeaderView::Fixed);
header->setSectionResizeMode(3, QHeaderView::Fixed);
#else
header->setResizeMode(0, QHeaderView::Stretch);
header->setResizeMode(1, QHeaderView::Fixed);
header->setResizeMode(2, QHeaderView::Fixed);
header->setResizeMode(3, QHeaderView::Fixed);
#endif
connect( ui->nof, SIGNAL(valueChanged(int)), SLOT(updateNoF()) );
connect( ui->list, SIGNAL(itemChanged(QTableWidgetItem*)), SLOT(updateAmOK()));
connect( ui->irregular, SIGNAL(toggled(bool)), SLOT(updateAmOK()));
connect( ui->step, SIGNAL(valueChanged(double)), SLOT(updateAmOK()));
connect( ui->nof, SIGNAL(valueChanged(int)), SIGNAL(parameterChanged()));
connect( ui->step, SIGNAL(valueChanged(double)), SIGNAL(parameterChanged()));
connect( ui->irregular, SIGNAL(toggled(bool)), SIGNAL(parameterChanged()));
connect( ui->list, SIGNAL(itemChanged(QTableWidgetItem*)), SIGNAL(parameterChanged()));
connect( ui->list->horizontalHeader(), SIGNAL(sectionClicked(int)), SLOT(updateToDo(int)));
connect( ui->irregular, SIGNAL(toggled(bool)), ui->step, SLOT(setDisabled(bool)));
updateNoF();
motui->setupButton()->setProperty(configProp, ui->placeMotor->property(configProp));
ui->placeMotor->setProperty(configProp,QVariant());
motui->setupButton()->setWhatsThis("Motor");
place(motui->setupButton(), ui->placeMotor);
place(motui->currentPosition(true), ui->placePosition);
QCaMotor * mot = motui->motor();
connect( mot, SIGNAL(changedConnected(bool)), SLOT(updateNoF()));
connect( mot, SIGNAL(changedConnected(bool)), SLOT(updateAmOK()));
connect( mot, SIGNAL(changedPv(QString)), SLOT(updateAmOK()));
connect( mot, SIGNAL(changedUserLoLimit(double)), SLOT(updateAmOK()));
connect( mot, SIGNAL(changedUserHiLimit(double)), SLOT(updateAmOK()));
connect( mot, SIGNAL(changedLoLimitStatus(bool)), SLOT(updateAmOK()));
connect( mot, SIGNAL(changedHiLimitStatus(bool)), SLOT(updateAmOK()));
connect( mot, SIGNAL(changedMoving(bool)), SLOT(updateAmOK()));
connect( mot, SIGNAL(changedPv(QString)), SIGNAL(parameterChanged()));
}
PositionList::~PositionList() {
delete ui;
}
void PositionList::updateNoF() {
const int steps = ui->nof->value();
while ( steps < ui->list->rowCount() ) {
const int lrow = ui->list->rowCount()-1;
delete ui->list->cellWidget(lrow, 1);
delete ui->list->cellWidget(lrow, 2);
delete chBox(lrow);
delete ui->list->cellWidget(lrow, doMeCol);
ui->list->removeRow( ui->list->rowCount()-1 );
}
while ( steps > ui->list->rowCount() ) {
const int crc = ui->list->rowCount();
ui->list->insertRow(crc);
QToolButton * btn;
btn = new QToolButton(this);
connect(btn, SIGNAL(clicked(bool)), SLOT(moveMotorHere()));
btn->setText("...");
btn->setToolTip("Move motor here");
ui->list->setCellWidget(crc, 1, btn );
ui->list->resizeColumnToContents(1);
btn = new QToolButton(this);
connect(btn, SIGNAL(clicked(bool)), SLOT(getMotorPosition()));
btn->setText("...");
btn->setToolTip("Get current motor position");
ui->list->setCellWidget(crc, 2, btn );
ui->list->resizeColumnToContents(2);
QWidget * wdg = new QWidget;
QSCheckBox * dome = new QSCheckBox(wdg);
dome->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
dome->setToolTip("If not ticked, will skip this scan in the experiment.");
//dome->setStyleSheet( "text-align: center; margin-left:50%; margin-right:50%;" );
dome->setChecked(true);
QHBoxLayout * layout = new QHBoxLayout(wdg);
layout->addWidget(dome);
wdg->setLayout(layout);
ui->list->setCellWidget(crc, doMeCol, wdg);
//ui->list->setCellWidget(crc, doMeCol, dome);
ui->list->resizeColumnToContents(doMeCol);
const double mpos = motui ? motui->motor()->getUserPosition() : 0 ;
ui->list->setItem(crc, 0, new QTableWidgetItem( QString::number(mpos) ) );
ui->list->setVerticalHeaderItem(crc, new QTableWidgetItem(QString::number(crc)) );
}
updateAmOK();
}
void PositionList::updateAmOK() {
if (!motui)
return;
QCaMotor * mot = motui->motor();
const int steps = ui->list->rowCount();
const bool isRegular = ! ui->irregular->isChecked();
bool newAllOK=true;
for ( int crow=0 ; crow < steps ; crow++ ) {
QTableWidgetItem * item = ui->list->item(crow, 0);
ui->list->blockSignals(true);
if ( ! isRegular )
item->setFlags( item->flags() | Qt::ItemIsEditable );
else
item->setFlags( item->flags() & ~Qt::ItemIsEditable );
ui->list->blockSignals(false);
if ( isRegular && ! freezListUpdates
&& mot->isConnected() && ! mot->isMoving() ) {
double pos = mot->getUserPosition() + item->row() * ui->step->value();
if ( pos != item->text().toDouble() )
item->setText( QString::number(pos) );
}
ui->list->cellWidget(crow, 1)->setEnabled( ! mot->isMoving() );
ui->list->cellWidget(crow, 2)->setEnabled( ! mot->isMoving() );
bool isDouble;
const double pos = item->text().toDouble(&isDouble);
const bool isOK = ! mot->isConnected() ||
( isDouble && pos > mot->getUserLoLimit() && pos < mot->getUserHiLimit() );
item->setBackground( isOK ? QBrush() : QBrush(QColor(Qt::red)));
newAllOK &= isOK;
}
static const QString warnStyle = "background-color: rgba(255, 0, 0, 128);";
ui->list->setColumnHidden(1, isRegular );
ui->list->setColumnHidden(2, isRegular );
ui->irregular->setStyleSheet( newAllOK || isRegular ? "" : warnStyle );
bool isOK = ! isRegular || ! mot->isConnected() || ui->step->value() != 0.0;
ui->step->setStyleSheet(isOK ? "" : warnStyle);
newAllOK &= isOK;
isOK = mot->getPv().isEmpty() || ( mot->isConnected() && ! mot->isMoving() );
motui->setupButton()->setStyleSheet(isOK ? "" : warnStyle);
newAllOK &= isOK;
isOK = mot->getPv().isEmpty() || ! mot->getLimitStatus();
motui->currentPosition(true)->setStyleSheet(isOK ? "" : warnStyle);
newAllOK &= isOK;
if (newAllOK != allOK)
emit amOKchanged(allOK=newAllOK);
}
void PositionList::emphasizeRow(int row) {
if ( row < 0 || row >= ui->list->rowCount() )
ui->list->setCurrentItem(0);
else
ui->list->setCurrentCell(row, 0);
}
void PositionList::moveMotorHere() {
for ( int crow=0 ; crow<ui->list->rowCount() ; crow++ )
if ( sender() == ui->list->cellWidget(crow, 1) ) {
motui->motor()->goUserPosition( position(crow), QCaMotor::STARTED );
return;
}
}
void PositionList::getMotorPosition() {
for ( int crow=0 ; crow<ui->list->rowCount() ; crow++ )
if ( sender() == ui->list->cellWidget(crow, 2) ) {
const double mpos = motui ? motui->motor()->getUserPosition() : 0 ;
ui->list->item(crow, 0)->setText(QString::number(mpos));
}
}
void PositionList::position(int row, double pos) {
if (row<0 || row>ui->list->rowCount())
return;
ui->list->item(row, 0)->setText(QString::number(pos));
}
void PositionList::done(int row) {
if (row<0)
for ( int crow=0 ; crow<ui->list->rowCount() ; crow++ )
done(crow);
else if (row < ui->list->rowCount())
chBox(row)->setChecked(false);
}
void PositionList::todo(int row) {
if (row<0)
for ( int crow=0 ; crow<ui->list->rowCount() ; crow++ )
todo(crow);
else if (row < ui->list->rowCount())
chBox(row)->setChecked(true);
}
bool PositionList::doAny() const {
for (int crow=0 ; crow < ui->list->rowCount() ; crow++)
if (doMe(crow))
return true;
return false;
}
bool PositionList::doAll() const {
for (int crow=0 ; crow < ui->list->rowCount() ; crow++)
if (!doMe(crow))
return false;
return true;
}
int PositionList::nextToDo() const {
for ( int crow=0 ; crow<ui->list->rowCount() ; crow++ )
if (chBox(crow)->isChecked() )
return crow;
return -1;
}
void PositionList::updateToDo(int index) {
if (index!=3)
return;
const bool setState = ! doAll();
for (int crow=0 ; crow < ui->list->rowCount() ; crow++)
chBox(crow)->setChecked(setState);
}