Skip to content

Commit

Permalink
Fix atomlist, copy buf implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
sgsaenger committed Oct 8, 2024
1 parent 37df279 commit 65df52c
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 59 deletions.
12 changes: 3 additions & 9 deletions gui/qt/glwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,8 @@ void GLWidget::mousePressEvent(QMouseEvent *e)
break;
case MouseMode::Select:
if(e->button() == Qt::MouseButton::RightButton){
// TODO: this assumes viewport is active -> use viewport over vApp?
vApp.invokeOnSel([](Step::selection &sel){
// TODO: sort out const-correctness
sel = const_cast<Step&>(vApp.curStep()).select(SelectionFilter{});
});
// clear selection
vApp.updateSelection({});
}
break;
case MouseMode::Modify:
Expand Down Expand Up @@ -378,10 +375,7 @@ void GLWidget::mouseReleaseEvent(QMouseEvent *e)
filter.indices = std::move(pick);
// create new selection from filter
// TODO: this assumes viewport is active -> use viewport over vApp?
vApp.invokeOnSel([](Step::selection &sel, const SelectionFilter &filter){
// TODO: sort out const-correctness
sel = const_cast<Step&>(vApp.curStep()).select(filter);
}, filter);
vApp.updateSelection(filter);
rectPos = mousePos;
}
break;
Expand Down
15 changes: 3 additions & 12 deletions gui/qt/mainwidgets/molwidget_aux/atomlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,12 @@ void AtomList::selectionChanged()
for(const auto& i: idx){
filter.indices.emplace_back(static_cast<size_t>(i.row()), SizeVec{});
}
vApp.invokeOnSel([](Step::selection &sel, const SelectionFilter &filter){
// TODO: sort out const-correctness
sel = const_cast<Step&>(vApp.curStep()).select(filter);
}, filter);
vApp.updateSelection(filter);
}

void AtomList::fmtSelectionHandler(int index)
{
// TODO: sort out const-correctness
atomModel.setStep(const_cast<Step&>(vApp.curStep()).asFmt(static_cast<AtomFmt>(index-2)));
atomModel.setStep(vApp.curStep().asFmt(static_cast<AtomFmt>(index-2)));
}

void AtomList::fmtButtonHandler()
Expand All @@ -149,15 +145,10 @@ void AtomList::fmtButtonHandler()
// modify actual Step
auto fmt = static_cast<AtomFmt>(ifmt-2);
vApp.invokeOnStep(&Step::setFmt, fmt, true);
// TODO: sort out const-correctness
ownStep = const_cast<Step&>(vApp.curStep()).asFmt(fmt);

// reset selection
SelectionFilter filter{};
filter.mode = SelectionFilter::Mode::Index;
filter.indices = vApp.curSel().getAtoms().indices;
vApp.invokeOnSel([](Step::selection &sel, const SelectionFilter &filter){
// TODO: sort out const-correctness
sel = const_cast<Step&>(vApp.curStep()).select(filter);
}, filter);
vApp.updateSelection(filter);
}
1 change: 0 additions & 1 deletion gui/qt/mainwidgets/molwidget_aux/atomlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ private slots:

Ui::AtomList *ui;

std::optional<Vipster::Step::formatter> ownStep{};
AtomModel atomModel{};
QList<QAction*> headerActions;
};
Expand Down
10 changes: 5 additions & 5 deletions gui/qt/mainwidgets/molwidget_aux/atommodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ bool AtomModel::setData(const QModelIndex &index, const QVariant &value, int rol
case c_x:
case c_y:
case c_z:
vApp.invokeOnStep([](Step &s, int i, int c, double d){
s.at(i).coord[c] = d;
}, index.row(), col-c_x, value.toDouble());
vApp.invokeOnStep([](Step &s, AtomFmt fmt, int i, int c, double d){
s.asFmt(fmt).at(i).coord[c] = d;
}, curStep->getFmt(), index.row(), col-c_x, value.toDouble());
break;
case c_charge:
vApp.invokeOnStep([](Step &s, int i, double charge){
Expand All @@ -150,8 +150,8 @@ bool AtomModel::setData(const QModelIndex &index, const QVariant &value, int rol
case c_fx:
case c_fy:
case c_fz:
vApp.invokeOnStep([](Step &s, int i, int dir, double charge){
s.at(i).properties->forces[dir] = charge;
vApp.invokeOnStep([](Step &s, int i, int dir, double force){
s.at(i).properties->forces[dir] = force;
}, index.row(), col-c_fx, value.toDouble());
break;
}
Expand Down
35 changes: 25 additions & 10 deletions gui/qt/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,21 +217,32 @@ void MainWindow::setupEditMenu()
Qt::Key_N);

// Delete selected Atom(s)
auto delAtoms = [](Step &s, const Step::const_selection &sel)
{
std::set<size_t> indices{};
for(const auto [idx, _]: sel.getAtoms().indices){
indices.insert(idx);
}
for(auto it = indices.rbegin(); it != indices.rend(); ++it)
{
if(*it < s.getNat()){
s.delAtom(*it);
}
}
};
auto *delAction = editMenu.addAction("&Delete atom(s)",
[](){
// TODO: figure out const-correctness
// BUG: this clears selection but does not trigger signal
vApp.invokeOnStep(&Step::delAtoms<Step::atom_source>, const_cast<Step::selection&>(vApp.curSel()));
[&](){
vApp.invokeOnStep(delAtoms, vApp.curSel());
vApp.updateSelection({});
},
Qt::Key_Delete);

// Cut atoms
auto *cutAction = editMenu.addAction("C&ut atom(s)",
[](){
[&](){
vApp.selectionToCopy();
// TODO: figure out const-correctness
// BUG: this clears selection but does not trigger signal
vApp.invokeOnStep(&Step::delAtoms<Step::atom_source>, const_cast<Step::selection&>(vApp.curSel()));
vApp.invokeOnStep(delAtoms, vApp.curSel());
vApp.updateSelection({});
},
QKeySequence::Cut);
cutAction->setEnabled(false);
Expand All @@ -251,12 +262,16 @@ void MainWindow::setupEditMenu()
// Paste atoms
auto *pasteAction = editMenu.addAction("&Paste atom(s)",
[](){
vApp.invokeOnStep(static_cast<void(Step::*)(const Step::const_selection& s)>(&Step::newAtoms), *vApp.copyBuf);
vApp.invokeOnStep([](Step &s){
if (vApp.copyBuf.getNat() > 0) {
s.newAtoms(vApp.copyBuf);
}
});
},
QKeySequence::Paste);
pasteAction->setEnabled(false);
connect(&vApp, &Application::copyBufChanged,
pasteAction, [=](const Step::selection &buf){pasteAction->setEnabled(buf.getNat() > 0);});
pasteAction, [=](const std::optional<Step> &buf){pasteAction->setEnabled(buf->getNat() > 0);});

// Separator
editMenu.addSeparator();
Expand Down
10 changes: 8 additions & 2 deletions gui/qt/vipsterapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ void Application::setActiveStep(Step &step, Step::selection &sel)
emit activeStepChanged(step, sel);
}

void Application::updateSelection(SelectionFilter filter)
{
*pCurSel = pCurStep->select(filter);
emit selChanged(*pCurSel);
}

Application::StepState& Application::getState(const Step &step)
{
// initialize state if required
Expand Down Expand Up @@ -102,6 +108,6 @@ void Application::newIOData(IOTuple &&t){
}

void Application::selectionToCopy(){
copyBuf = std::make_unique<Step::selection>(*pCurSel);
emit copyBufChanged(*copyBuf);
copyBuf = *pCurSel;
emit copyBufChanged(copyBuf);
}
9 changes: 5 additions & 4 deletions gui/qt/vipsterapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class Application: public QObject

// TODO: integrate with OS-specific copy-paste functionality instead
void selectionToCopy();
std::unique_ptr<Step::selection> copyBuf{}; // TODO: are lifetime semantics of selection sufficient?? convert to optional instead
Step copyBuf{};

template<typename F, typename... Args>
auto invokeOnStep(F &&f, Args &&...args)
Expand All @@ -170,18 +170,19 @@ class Application: public QObject
{
if constexpr (!std::is_void_v<decltype(invokeImpl(*pCurSel, std::forward<F>(f), std::forward<Args>(args)...))>) {
auto tmp = invokeImpl(*pCurSel, std::forward<F>(f), std::forward<Args>(args)...);
emit selChanged(*pCurSel);
emit stepChanged(*pCurStep);
return tmp;
} else {
invokeImpl(*pCurSel, std::forward<F>(f), std::forward<Args>(args)...);
emit selChanged(*pCurSel);
emit stepChanged(*pCurStep);
}
}
void updateSelection(SelectionFilter filter);
signals:
void activeStepChanged(const Vipster::Step &step, const Vipster::Step::selection &sel);
void stepChanged(const Vipster::Step &step);
void selChanged(const Vipster::Step::selection &sel);
void copyBufChanged(const Vipster::Step::selection &buf);
void copyBufChanged(const Vipster::Step &buf);

public:
// GUI-Data related to a loaded Step
Expand Down
16 changes: 0 additions & 16 deletions vipster/step.h
Original file line number Diff line number Diff line change
Expand Up @@ -347,22 +347,6 @@ class Step: public StepMutable<detail::AtomList>
}
}
void delAtom(size_t i);
// TODO: this is a weird interface - either ensure that it is a selection object refering to own instance, or create a more abstract i.e. index based interface
template<typename T>
void delAtoms(StepConst<detail::Selection<T>>& s)
{
std::set<size_t> indices{};
for(const auto [idx, _]: s.getAtoms().indices){
indices.insert(idx);
}
for(auto it = indices.rbegin(); it != indices.rend(); ++it)
{
if(*it < getNat()){
delAtom(*it);
}
}
s = select({});
}

// Cell
void enableCell(bool val) noexcept;
Expand Down

0 comments on commit 65df52c

Please sign in to comment.