Skip to content

Commit

Permalink
Update Viewport handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sgsaenger committed Oct 13, 2024
1 parent 33cf77f commit af5988d
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 126 deletions.
14 changes: 2 additions & 12 deletions gui/common/guiwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,13 @@ void GuiWrapper::drawSel(void *context)
void GuiWrapper::setMainStep(Step* step)
{
curStep = step;
updateMainStep();
}

void GuiWrapper::setMainSel(Step::selection* sel)
{
curSel = sel;
updateMainSelection();
}

void GuiWrapper::updateMainStep()
{
mainStep.update(curStep, settings.atRadVdW.val,
settings.atRadFac.val, settings.bondRad.val);
}

void GuiWrapper::updateMainSelection()
void GuiWrapper::setMainSel(Step::selection* sel)
{
curSel = sel;
selection.update(curSel, settings.atRadVdW.val, settings.atRadFac.val);
}

Expand Down
2 changes: 0 additions & 2 deletions gui/common/guiwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ class GuiWrapper: protected QOpenGLExtraFunctions{
const Vec& pos, unsigned long width, unsigned long height);
void setMainStep(Step* step);
void setMainSel(Step::selection* sel);
void updateMainStep();
void updateMainSelection();
void resizeViewMat(long w, long h);
void zoomViewMat(float i);
void rotateViewMat(float x, float y, float z);
Expand Down
4 changes: 2 additions & 2 deletions gui/qt/glwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ void GLWidget::focusInEvent(QFocusEvent *)
{
// make sure our viewport is the active one
auto vp = static_cast<ViewPort*>(parentWidget());
if(!vp->active){
vp->master->changeViewports(vp, MainWindow::VP_ACTIVE);
if(!vp->isActive()){
vp->master->setActiveViewport(vp);
}
}
134 changes: 64 additions & 70 deletions gui/qt/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ MainWindow::MainWindow(QString path):
void MainWindow::setupViewports()
{
// create first level of splitters and main viewport
viewports.push_back(new ViewPort{this, true});
viewports.push_back(new ViewPort{this});
vsplit = new QSplitter{this};
vsplit->setOrientation(Qt::Vertical);
vsplit->setChildrenCollapsible(false);
Expand All @@ -53,7 +53,7 @@ void MainWindow::setupViewports()
hsplits.back()->addWidget(viewports.front());
setCentralWidget(vsplit);
curVP = viewports.front();
curVP->makeActive(true);
curVP->setFrameShadow(QFrame::Shadow::Sunken);
curVP->ui->closeButton->setDisabled(true);
}

Expand Down Expand Up @@ -351,13 +351,14 @@ void MainWindow::setupHelpMenu()

void MainWindow::updateWidgets(GUI::change_t change)
{
throw Error{"this should not be called anymore"};
// pull in mol/step selection from active viewport
if((change & GUI::molChanged) == GUI::molChanged){
vApp.setActiveMol(*curVP->curMol);
}
if((change & GUI::stepChanged) == GUI::stepChanged){
vApp.setActiveStep(*curVP->curStep, *curVP->curSel);
}
// if((change & GUI::molChanged) == GUI::molChanged){
// vApp.setActiveMol(*curVP->curMol);
// }
// if((change & GUI::stepChanged) == GUI::stepChanged){
// vApp.setActiveStep(*curVP->curStep, *curVP->curSel);
// }
// notify widgets
for(auto& w: viewports){
w->updateWidget(change);
Expand All @@ -367,72 +368,65 @@ void MainWindow::updateWidgets(GUI::change_t change)
// }
}

void MainWindow::changeViewports(ViewPort *sender, VPChange change)
{
try{
switch(change){
case VP_CLOSE:
{
bool wasActive = sender->active;
auto pos = std::find(viewports.begin(), viewports.end(), sender);
if(pos == viewports.end()){
throw Error{"Invalid viewport"};
}else{
viewports.erase(pos);
delete sender;
// clean-up hsplits if sender was last child
for(auto it = hsplits.begin(); it != hsplits.end(); ++it){
if(!(*it)->count()){
delete *it;
hsplits.erase(it);
break;
}
}
}
// make sure we don't close last viewport
if(viewports.size() == 1)
viewports.front()->ui->closeButton->setDisabled(true);
if(wasActive)
viewports.front()->openGLWidget->setFocus();
}
break;
case VP_VSPLIT:
if(viewports.size() == 1) viewports.front()->ui->closeButton->setEnabled(true);
viewports.push_back(new ViewPort{*sender});
viewports.back()->updateWidget(GUI::molChanged);
hsplits.push_back(new QSplitter{vsplit});
hsplits.back()->setChildrenCollapsible(false);
hsplits.back()->addWidget(viewports.back());
vsplit->addWidget(hsplits.back());
break;
case VP_HSPLIT:
if(viewports.size() == 1) viewports.front()->ui->closeButton->setEnabled(true);
viewports.push_back(new ViewPort{*sender});
viewports.back()->updateWidget(GUI::molChanged);
for(auto& h: hsplits){
for(auto& c: h->children()){
if (c == sender){
h->addWidget(viewports.back());
return;
}
}
void MainWindow::setActiveViewport(ViewPort* sender) {
// deactivate other viewports
for(auto& vp: viewports){
vp->setFrameShadow(QFrame::Shadow::Raised);
}
sender->setFrameShadow(QFrame::Shadow::Sunken);
// make sender current viewport
curVP = sender;
vApp.setActiveMol(*curVP->curMol);
vApp.setActiveStep(*curVP->curStep, *curVP->curSel);
}

void MainWindow::splitViewportHoriz(ViewPort* sender) {
viewports.front()->ui->closeButton->setEnabled(true);
viewports.push_back(new ViewPort{*sender});
viewports.back()->updateWidget(GUI::molChanged);
for(auto& h: hsplits){
for(auto& c: h->children()){
if (c == sender){
h->addWidget(viewports.back());
return;
}
throw Error{"Could not determine horizontal splitter for viewport"};
case VP_ACTIVE:
// deactivate other viewports
for(auto& vp: viewports){
vp->makeActive(vp == sender);
}
}
throw Error{"Could not determine horizontal splitter for viewport"};
}

void MainWindow::splitViewportVert(ViewPort* sender) {
viewports.front()->ui->closeButton->setEnabled(true);
viewports.push_back(new ViewPort{*sender});
viewports.back()->updateWidget(GUI::molChanged);
hsplits.push_back(new QSplitter{vsplit});
hsplits.back()->setChildrenCollapsible(false);
hsplits.back()->addWidget(viewports.back());
vsplit->addWidget(hsplits.back());
}

void MainWindow::closeViewport(ViewPort* sender) {
bool wasActive = sender == curVP;
auto pos = std::find(viewports.begin(), viewports.end(), sender);
if(pos == viewports.end()){
throw Error{"Invalid viewport"};
}else{
viewports.erase(pos);
delete sender;
// clean-up hsplits if sender was last child
for(auto it = hsplits.begin(); it != hsplits.end(); ++it){
if(!(*it)->count()){
delete *it;
hsplits.erase(it);
break;
}
// make sender current viewport
curVP = sender;
updateWidgets(GUI::molChanged);
break;
default:
throw Error{"Invalid viewport change"};
}
}catch(const Error& e){
QMessageBox::information(this, "ViewPort Error", e.what());
}
// make sure we don't close last viewport
if(viewports.size() == 1)
viewports.front()->ui->closeButton->setDisabled(true);
if(wasActive)
viewports.front()->openGLWidget->setFocus();
}

void MainWindow::newData(IOTuple &&d)
Expand Down
6 changes: 4 additions & 2 deletions gui/qt/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ class MainWindow : public QMainWindow

// Viewports
std::vector<ViewPort*> viewports;
enum VPChange{VP_CLOSE, VP_VSPLIT, VP_HSPLIT, VP_ACTIVE};
void changeViewports(ViewPort* sender, VPChange change);
void setActiveViewport(ViewPort* sender);
void splitViewportHoriz(ViewPort* sender);
void splitViewportVert(ViewPort* sender);
void closeViewport(ViewPort* sender);
ViewPort* curVP{nullptr};
void updateWidgets(Vipster::GUI::change_t change);
void newData(Vipster::IOTuple&& d);
Expand Down
53 changes: 21 additions & 32 deletions gui/qt/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

using namespace Vipster;

ViewPort::ViewPort(MainWindow *parent, bool active) :
ViewPort::ViewPort(MainWindow *parent) :
QFrame(parent),
ui(new Ui::ViewPort),
master{parent},
active{active}
master{parent}
{
ui->setupUi(this);
setFocusPolicy(Qt::StrongFocus);
Expand Down Expand Up @@ -50,12 +49,17 @@ ViewPort::ViewPort(MainWindow *parent, bool active) :
[&](const Vipster::ConfigState &){
updateState();
});

// Viewport management buttons
connect(ui->closeButton, &QPushButton::clicked, this, [&](){master->closeViewport(this);});
connect(ui->vSplitButton, &QPushButton::clicked, this, [&](){master->splitViewportVert(this);});
connect(ui->hSplitButton, &QPushButton::clicked, this, [&](){master->splitViewportHoriz(this);});
}

ViewPort::ViewPort(const ViewPort &vp) :
ViewPort{vp.master, false}
ViewPort{vp.master}
{
//TODO: make this optional?
// synchronize mol/step state
moldata = vp.moldata;
for(const auto& p: vp.stepdata){
if(p.second.sel){
Expand All @@ -66,15 +70,21 @@ ViewPort::ViewPort(const ViewPort &vp) :
}
}
}

// synchronize/initialize GUI state
updateMoleculeList(vApp.molecules);
ui->molList->setCurrentIndex(vp.ui->molList->currentIndex());
ui->stepEdit->setValue(vp.ui->stepEdit->value());
}

ViewPort::~ViewPort()
{
delete ui;
}

bool ViewPort::isActive() {
return this == master->curVP;
}

void ViewPort::updateState() {
// set widget's bond mode accordingly
setBondMode(vApp.getState(*curStep).automatic_bonds);
Expand Down Expand Up @@ -103,7 +113,7 @@ void ViewPort::updateMoleculeList(const std::list<Molecule> &molecules){
ui->molList->addItem(mol.name.c_str());
}

if (active || shrunk) {
if (!curMol || shrunk || isActive()) {
setMol(molecules.size()-1);
}
}
Expand Down Expand Up @@ -197,7 +207,7 @@ void ViewPort::setMol(int index)
setStep(moldata[curMol].curStep, true);

// update app state if required
if (active) {
if (isActive()) {
vApp.setActiveMol(*curMol);
}
}
Expand Down Expand Up @@ -243,7 +253,7 @@ void ViewPort::setStep(int i, bool setMol)
updateState();

// Update application state
if(active){
if(isActive()){
vApp.setActiveStep(*curStep, *curSel);
}
}
Expand All @@ -254,8 +264,8 @@ void ViewPort::setMult(int i)
if(sender() == ui->xMultBox){ mult[0] = static_cast<uint8_t>(i); }
else if(sender() == ui->yMultBox){ mult[1] = static_cast<uint8_t>(i); }
else if(sender() == ui->zMultBox){ mult[2] = static_cast<uint8_t>(i); }
// save mult if needed
if (active) moldata[curMol].mult = mult;
moldata[curMol].mult = mult;

// trigger redraw
openGLWidget->setMult(mult);
}
Expand Down Expand Up @@ -327,24 +337,3 @@ void ViewPort::on_mouseMode_currentIndexChanged(int i)
{
openGLWidget->setMouseMode(static_cast<GLWidget::MouseMode>(i));
}

void ViewPort::on_closeButton_clicked()
{
master->changeViewports(this, MainWindow::VP_CLOSE);
}

void ViewPort::on_vSplitButton_clicked()
{
master->changeViewports(this, MainWindow::VP_VSPLIT);
}

void ViewPort::on_hSplitButton_clicked()
{
master->changeViewports(this, MainWindow::VP_HSPLIT);
}

void ViewPort::makeActive(bool a)
{
active = a;
setFrameShadow(active ? QFrame::Sunken : QFrame::Raised);
}
11 changes: 5 additions & 6 deletions gui/qt/viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@ class ViewPort : public QFrame
friend class MainWindow;
friend class GLWidget;
public:
explicit ViewPort(MainWindow *parent, bool active=false);
explicit ViewPort(MainWindow *parent);
explicit ViewPort(const ViewPort &vp);
~ViewPort() override;

void updateWidget(Vipster::GUI::change_t change);
void makeActive(bool active);

void addExtraData(const std::shared_ptr<Vipster::GUI::Data> &dat, bool global);
void delExtraData(const std::shared_ptr<Vipster::GUI::Data> &dat, bool global);
bool hasExtraData(const std::shared_ptr<Vipster::GUI::Data> &dat, bool global);
struct {
std::vector<std::weak_ptr<Vipster::GUI::Data>> extras{};
} vpdata{};

struct MolState{
size_t curStep{0};
Vipster::GUI::PBCVec mult{1,1,1};
Expand All @@ -59,21 +61,18 @@ public slots:

private slots:
void on_mouseMode_currentIndexChanged(int index);
void on_closeButton_clicked();
void on_vSplitButton_clicked();
void on_hSplitButton_clicked();
void updateMoleculeList(const std::list<Vipster::Molecule> &molecules);

public:
void updateState();
bool isActive();

Ui::ViewPort *ui;
MainWindow *master;
GLWidget *openGLWidget{nullptr};
Vipster::Molecule* curMol{nullptr};
Vipster::Step* curStep{nullptr};
Vipster::Step::selection* curSel{nullptr};
bool active{true};
QTimer playTimer{};
};

Expand Down

0 comments on commit af5988d

Please sign in to comment.