Skip to content
Snippets Groups Projects
Commit 9c3be690 authored by Mikhail Svechnikov's avatar Mikhail Svechnikov
Browse files

move layers

parent 50c9d84f
No related branches found
No related tags found
1 merge request!2741GUI sample builder supports periodic stacks (#20)
......@@ -174,7 +174,10 @@ void SampleItem::removeComponent(const ItemWithLayers* component)
void SampleItem::moveComponent(ItemWithLayers* component, ItemWithLayers* aboveThisComponent)
{
m_outer_stack->moveComponent(component, aboveThisComponent);
LayerStackItem* containingStack = parentOfComponent(component);
ASSERT(containingStack);
containingStack->moveComponent(component, aboveThisComponent);
updateTopBottom();
}
......
......@@ -62,7 +62,7 @@ LayerContainerForm::LayerContainerForm(QWidget* parent, ItemWithLayers* item,
}
// move
{
m_move_button = new WidgetMoverButton(this, this, 1);
m_move_button = new WidgetMoverButton(this, this, 0);
m_move_button->setToolTip("Move " + what + " up/down");
connect(m_move_button, &WidgetMoverButton::finishedMoving, ec,
&SampleEditorController::onStoppedToMoveLayer);
......
......@@ -20,17 +20,15 @@
#include "GUI/View/Base/LayoutUtil.h"
#include "GUI/View/Sample/HeinzFormLayout.h"
#include "GUI/View/Sample/LayerForm.h"
#include "GUI/View/Sample/SampleEditorController.h"
#include <QPushButton>
namespace {
//! Widget with a button to add a layer or stack (the "Add layer/add stack" buttons shown between
//! items)
class AddLayerContainerWidget : public QWidget {
class AddButtonsWidget : public QWidget {
public:
AddLayerContainerWidget(LayerStackForm* parentStackForm, const ItemWithLayers* itemBefore,
SampleEditorController* ec)
AddButtonsWidget(LayerStackForm* parentStackForm, const ItemWithLayers* itemBefore,
SampleEditorController* ec)
: QWidget(parentStackForm)
, m_item(itemBefore)
{
......@@ -49,9 +47,9 @@ public:
l->addWidget(stack_btn);
connect(stack_btn, &QPushButton::clicked,
[=] { ec->addLayerStackItem(*parentStackForm, itemBefore); });
l->addStretch();
}
const ItemWithLayers* m_item;
};
......@@ -59,21 +57,17 @@ public:
LayerStackForm::LayerStackForm(QWidget* parent, LayerStackItem* stack, SampleEditorController* ec)
: LayerContainerForm(parent, stack, ec, "stack")
, m_stack_layout(new QVBoxLayout)
, m_components_layout(new QVBoxLayout)
{
m_layout->addRow(m_stack_layout);
for (auto* item : stack->componentItems()) {
m_stack_layout->addWidget(new AddLayerContainerWidget(this, item, m_ec), 0, Qt::AlignTop);
if (auto* layer = dynamic_cast<LayerItem*>(item))
m_stack_layout->addWidget(new LayerForm(this, layer, m_ec), 0, Qt::AlignTop);
else if (auto* stack = dynamic_cast<LayerStackItem*>(item))
m_stack_layout->addWidget(new LayerStackForm(this, stack, m_ec), 0, Qt::AlignTop);
else
ASSERT_NEVER;
}
m_stack_layout->addWidget(new AddLayerContainerWidget(this, nullptr, m_ec), 0, Qt::AlignTop);
m_stack_layout->setSizeConstraint(QLayout::SetMinimumSize);
auto* components_widget = new QWidget;
components_widget->setLayout(m_components_layout);
m_layout->addRow(components_widget);
m_components_layout->addWidget(new AddButtonsWidget(this, nullptr, m_ec), 0, Qt::AlignTop);
for (auto* item : stack->componentItems())
addComponentForm(item);
m_components_layout->setSizeConstraint(QLayout::SetMinimumSize);
updatePositionDependentElements();
......@@ -96,46 +90,47 @@ void LayerStackForm::addComponentForm(ItemWithLayers* componentItem)
else
ASSERT_NEVER;
auto* buttons = new AddButtonsWidget(this, componentItem, m_ec);
const int row_in_layout = rowInLayout(componentItem);
// same row => button is above!
m_stack_layout->insertWidget(
row_in_layout, new AddLayerContainerWidget(this, componentItem, m_ec), 0, Qt::AlignTop);
m_stack_layout->insertWidget(row_in_layout, newForm, 0, Qt::AlignTop);
m_components_layout->insertWidget(row_in_layout, buttons, 0, Qt::AlignTop);
m_components_layout->insertWidget(row_in_layout, newForm, 0, Qt::AlignTop);
}
void LayerStackForm::onComponentMoved(const ItemWithLayers* componentItem)
{
LayerContainerForm* wl = nullptr;
AddLayerContainerWidget* al = nullptr;
for (int index = 0; index < m_stack_layout->count(); index++) {
if (auto* w =
dynamic_cast<AddLayerContainerWidget*>(m_stack_layout->itemAt(index)->widget()))
AddButtonsWidget* al = nullptr;
for (int index = 0; index < m_components_layout->count(); index++) {
if (auto* w = dynamic_cast<AddButtonsWidget*>(m_components_layout->itemAt(index)->widget()))
if (w->m_item == componentItem) {
al = w;
m_stack_layout->takeAt(index);
m_components_layout->takeAt(index);
break;
}
}
for (int index = 0; index < m_stack_layout->count(); index++) {
if (auto* w = dynamic_cast<LayerContainerForm*>(m_stack_layout->itemAt(index)->widget()))
for (int index = 0; index < m_components_layout->count(); index++) {
if (auto* w =
dynamic_cast<LayerContainerForm*>(m_components_layout->itemAt(index)->widget()))
if (w->item() == componentItem) {
wl = w;
m_stack_layout->takeAt(index);
m_components_layout->takeAt(index);
break;
}
}
const int row_in_layout = rowInLayout(componentItem);
m_stack_layout->insertWidget(row_in_layout, wl, 0, Qt::AlignTop);
// same row => button is above!
m_stack_layout->insertWidget(row_in_layout, al, 0, Qt::AlignTop);
m_components_layout->insertWidget(row_in_layout, al, 0, Qt::AlignTop);
m_components_layout->insertWidget(row_in_layout, wl, 0, Qt::AlignTop);
}
void LayerStackForm::removeComponentForm(ItemWithLayers* componentItem)
{
LayerContainerForm* componentForm = nullptr;
AddLayerContainerWidget* addLayerWidget = nullptr;
AddButtonsWidget* addLayerWidget = nullptr;
for (auto* c : findChildren<QWidget*>()) {
if (auto* w = dynamic_cast<AddLayerContainerWidget*>(c))
if (auto* w = dynamic_cast<AddButtonsWidget*>(c))
if (w->m_item == componentItem)
addLayerWidget = w;
......@@ -147,7 +142,6 @@ void LayerStackForm::removeComponentForm(ItemWithLayers* componentItem)
ASSERT(componentForm);
ASSERT(addLayerWidget);
// delete editors which are subscribed to SessionItems
GUI::Util::Layout::clearLayout(componentForm->layout());
componentForm->hide();
componentForm->setParent(nullptr); // so it is not findable in update routines
......@@ -156,6 +150,20 @@ void LayerStackForm::removeComponentForm(ItemWithLayers* componentItem)
delete addLayerWidget;
}
LayerContainerForm* LayerStackForm::findNextLayerContainerForm(QWidget* w)
{
while (w != nullptr && dynamic_cast<LayerContainerForm*>(w) == nullptr) {
const auto index = m_components_layout->indexOf(w);
if (index + 1 < m_components_layout->count())
w = m_components_layout->itemAt(index + 1)->widget();
else
return nullptr;
}
return dynamic_cast<LayerContainerForm*>(w);
}
void LayerStackForm::updatePositionDependentElements()
{
LayerContainerForm::updatePositionDependentElements();
......
......@@ -18,7 +18,6 @@
#include "GUI/View/Sample/LayerContainerForm.h"
#include <QBoxLayout>
class LayerItem;
class LayerStackItem;
class LayerStackForm : public LayerContainerForm {
......@@ -40,6 +39,12 @@ public:
//! Any widgets related to the item will be deleted or scheduled for later deletion.
void removeComponentForm(ItemWithLayers* componentItem);
//! Search for the next LayerContainerForm, starting from the given widget.
//!
//! The search starts with the given widget itself. If it is a LayerContainerForm, it is
//! returned. If no following LayerContainerForm is found, nullptr is returned.
LayerContainerForm* findNextLayerContainerForm(QWidget* w);
void updatePositionDependentElements() override;
private:
......@@ -47,7 +52,7 @@ private:
int rowInLayout(const ItemWithLayers* componentItem) const;
void updateTitle();
QVBoxLayout* m_stack_layout;
QVBoxLayout* m_components_layout;
};
#endif // BORNAGAIN_GUI_VIEW_SAMPLE_LAYERSTACKFORM_H
......@@ -421,18 +421,25 @@ void SampleEditorController::setDensityRelatedValue(InterferenceItem* interferen
void SampleEditorController::onStoppedToMoveLayer(QWidget* widgetToMove,
QWidget* moveAboveThisWidget)
{
auto* itemToMove = dynamic_cast<LayerForm*>(widgetToMove)->layerItem();
auto* formToMove = dynamic_cast<LayerContainerForm*>(widgetToMove);
ASSERT(formToMove);
auto* itemToMove = formToMove->item();
const auto* moveAboveThisLayerForm = m_sample_form->findNextLayerForm(moveAboveThisWidget);
auto* moveAboveThisItem =
moveAboveThisLayerForm != nullptr ? moveAboveThisLayerForm->layerItem() : nullptr;
LayerStackItem* parentStack = m_sample_item->parentOfComponent(itemToMove);
ASSERT(parentStack);
LayerStackForm* parentStackForm = m_sample_form->formOfStackItem(parentStack);
ASSERT(parentStackForm);
const LayerContainerForm* moveAboveThisLayerForm =
parentStackForm->findNextLayerContainerForm(moveAboveThisWidget);
auto* moveAboveThisItem = moveAboveThisLayerForm ? moveAboveThisLayerForm->item() : nullptr;
m_sample_item->moveComponent(itemToMove, moveAboveThisItem);
m_sample_form->onLayerMoved(itemToMove);
parentStackForm->onComponentMoved(itemToMove);
m_sample_form->updateRowVisibilities();
// #baLayerEditor: tab order!
emit gDoc->sampleChanged();
}
......
......@@ -83,40 +83,6 @@ SampleForm::SampleForm(SampleItem* sampleItem, SampleEditorController* ec)
Qt::AlignTop);
}
void SampleForm::onLayerMoved(ItemWithLayers* item)
{
// LayerContainerForm* wl = nullptr;
// AddLayerContainerWidget* al = nullptr;
// for (int index = 0; index < m_layout->count(); index++) {
// if (auto* w =
// dynamic_cast<AddLayerContainerWidget*>(m_layout->itemAt(index)->widget()))
// if (w->m_item == item) {
// al = w;
// m_layout->takeAt(index);
// break;
// }
// }
// for (int index = 0; index < m_layout->count(); index++) {
// if (auto* w = dynamic_cast<LayerContainerForm*>(m_layout->itemAt(index)->widget()))
// if (w->item() == item) {
// wl = w;
// m_layout->takeAt(index);
// break;
// }
// }
// const int rowInMultiLayer = Vec::indexOfPtr(item, m_sample_item->uniqueLayerItems());
// const int rowInLayout = rowInMultiLayer * 2 + 1;
// m_layout->insertWidget(rowInLayout, wl, 0, Qt::AlignTop);
// // same row => button is above!
// m_layout->insertWidget(rowInLayout, al, 0, Qt::AlignTop);
// updateRowVisibilities();
}
void SampleForm::updateRowVisibilities()
{
for (auto* c : findChildren<LayerContainerForm*>())
......@@ -131,9 +97,9 @@ LayerStackForm* SampleForm::formOfStackItem(const LayerStackItem* searchedStackI
return nullptr;
}
LayerForm* SampleForm::findNextLayerForm(QWidget* w)
LayerContainerForm* SampleForm::findNextLayerContainerForm(QWidget* w)
{
while (w != nullptr && dynamic_cast<LayerForm*>(w) == nullptr) {
while (w != nullptr && dynamic_cast<LayerContainerForm*>(w) == nullptr) {
const auto index = m_layout->indexOf(w);
if (index + 1 < m_layout->count())
w = m_layout->itemAt(index + 1)->widget();
......@@ -141,5 +107,5 @@ LayerForm* SampleForm::findNextLayerForm(QWidget* w)
return nullptr;
}
return dynamic_cast<LayerForm*>(w);
return dynamic_cast<LayerContainerForm*>(w);
}
......@@ -20,9 +20,9 @@
class HeinzFormLayout;
class ItemWithLayers;
class LayerForm;
class LayerStackForm;
class LayerContainerForm;
class LayerItem;
class LayerStackForm;
class LayerStackItem;
class QVBoxLayout;
class SampleEditorController;
......@@ -34,20 +34,15 @@ class SampleForm : public QWidget {
public:
SampleForm(SampleItem* sampleItem, SampleEditorController* ec);
//! Call this when an ItemWithLayers has been moved to a different position.
//!
//! This updates the item's position in the layout.
void onLayerMoved(ItemWithLayers* layerItem);
void updateRowVisibilities();
LayerStackForm* formOfStackItem(const LayerStackItem* searchedStackItem);
//! Search for the next LayerForm, starting from the given widget.
//! Search for the next LayerContainerForm, starting from the given widget.
//!
//! The search starts with the given widget itself If it is a LayerForm, it is returned.
//! If no following LayerForm is found, nullptr is returned.
LayerForm* findNextLayerForm(QWidget* w);
//! The search starts with the given widget itself. If it is a LayerContainerForm, it is
//! returned. If no following LayerContainerForm is found, nullptr is returned.
LayerContainerForm* findNextLayerContainerForm(QWidget* w);
private:
QVBoxLayout* m_layout;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment