Skip to content
Snippets Groups Projects
Commit 4820c8cc authored by Pospelov, Gennady's avatar Pospelov, Gennady
Browse files

PropertyWidgetItem to hold label, editor and model mapping within ComponentFlatView

parent fa5208e1
No related branches found
No related tags found
No related merge requests found
......@@ -20,6 +20,7 @@
#include "SessionItem.h"
#include "SessionModel.h"
#include "LayoutUtils.h"
#include "PropertyWidgetItem.h"
#include <QLabel>
#include <QVBoxLayout>
#include <QGridLayout>
......@@ -48,32 +49,26 @@ void ComponentFlatView::addItemProperties(SessionItem* item)
int nrow(0);
for (auto child : ComponentUtils::componentItems(*item)) {
auto editor = PropertyEditorFactory::CreateEditor(*child);
if (!editor)
continue;
auto label = new QLabel(child->displayName());
label->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
m_gridLayout->addWidget(label, nrow, 0);
m_gridLayout->addWidget(editor, nrow, 1);
auto mapper = new QDataWidgetMapper(this);
mapper->setModel(child->model());
mapper->setRootIndex(child->parent()->index()); // item might be != child->parent()
mapper->setCurrentModelIndex(child->index());
mapper->addMapping(label, 0);
mapper->addMapping(editor, 1);
auto widget = createWidget(child);
if (!widget)
continue;
++nrow;
widget->addToGrid(m_gridLayout, ++nrow);
m_widgetItems.push_back(widget);
}
}
void ComponentFlatView::clearLayout()
{
{
Q_ASSERT(m_gridLayout);
LayoutUtils::clearLayout(m_gridLayout);
LayoutUtils::clearLayout(m_gridLayout, false);
for(auto widget: m_widgetItems)
delete widget;
m_widgetItems.clear();
}
void ComponentFlatView::initGridLayout()
......@@ -84,3 +79,15 @@ void ComponentFlatView::initGridLayout()
m_mainLayout->addLayout(m_gridLayout);
m_mainLayout->addStretch(1);
}
PropertyWidgetItem* ComponentFlatView::createWidget(SessionItem* item)
{
auto editor = PropertyEditorFactory::CreateEditor(*item);
if (!editor)
return nullptr;
auto result = new PropertyWidgetItem(this);
result->setItemEditor(item, editor);
return result;
}
......@@ -23,6 +23,7 @@
class SessionItem;
class QGridLayout;
class QBoxLayout;
class PropertyWidgetItem;
//! Component property widget for SessionItems. On the contrary to ComponentTreeView
//! properties are presented as widgets in grid layout.
......@@ -39,9 +40,11 @@ public:
private:
void clearLayout();
void initGridLayout();
PropertyWidgetItem* createWidget(SessionItem* item);
QBoxLayout* m_mainLayout;
QGridLayout* m_gridLayout;
QVector<PropertyWidgetItem*> m_widgetItems;
};
#endif
// ************************************************************************** //
//
// BornAgain: simulate and fit scattering at grazing incidence
//
//! @file GUI/coregui/Views/PropertyEditor/PropertyWidgetItem.cpp
//! @brief Implements class PropertyWidgetItem
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2016
//! @authors Scientific Computing Group at MLZ Garching
//! @authors Céline Durniak, Marina Ganeva, David Li, Gennady Pospelov
//! @authors Walter Van Herck, Joachim Wuttke
//
// ************************************************************************** //
#include "PropertyWidgetItem.h"
#include "SessionItem.h"
#include "SessionModel.h"
#include <QLabel>
#include <QWidget>
#include <QDataWidgetMapper>
#include <QGridLayout>
PropertyWidgetItem::PropertyWidgetItem(QWidget* parent)
: QObject(parent)
, m_label(new QLabel)
, m_editor(nullptr)
, m_dataMapper(new QDataWidgetMapper(this))
{
m_label->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
}
PropertyWidgetItem::~PropertyWidgetItem()
{
delete m_label;
delete m_editor;
}
void PropertyWidgetItem::setItemEditor(SessionItem* item, QWidget* editor)
{
delete m_editor;
m_editor = editor;
m_label->setText(item->displayName());
m_dataMapper->setModel(item->model());
m_dataMapper->setRootIndex(item->parent()->index());
m_dataMapper->setCurrentModelIndex(item->index());
m_dataMapper->addMapping(m_label, 0);
m_dataMapper->addMapping(m_editor, 1);
m_label->setEnabled(item->isEnabled());
m_editor->setEnabled(item->isEnabled());
}
void PropertyWidgetItem::addToGrid(QGridLayout* gridLayout, int nrow)
{
Q_ASSERT(m_label);
Q_ASSERT(m_editor);
gridLayout->addWidget(m_label, nrow, 0);
gridLayout->addWidget(m_editor, nrow, 1);
}
// ************************************************************************** //
//
// BornAgain: simulate and fit scattering at grazing incidence
//
//! @file GUI/coregui/Views/PropertyEditor/PropertyWidgetItem.h
//! @brief Defines class PropertyWidgetItem
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2016
//! @authors Scientific Computing Group at MLZ Garching
//! @authors Céline Durniak, Marina Ganeva, David Li, Gennady Pospelov
//! @authors Walter Van Herck, Joachim Wuttke
//
// ************************************************************************** //
#ifndef PROPERTYWIDGETITEM_H
#define PROPERTYWIDGETITEM_H
#include "WinDllMacros.h"
#include <QObject>
class QLabel;
class QWiget;
class QDataWidgetMapper;
class QGridLayout;
class SessionItem;
//! Container to hold label and editor for PropertyItem.
//! Contains also logic to map editor to SessionModel.
class BA_CORE_API_ PropertyWidgetItem : public QObject
{
Q_OBJECT
public:
explicit PropertyWidgetItem(QWidget* parent = nullptr);
~PropertyWidgetItem();
void setItemEditor(SessionItem* item, QWidget* editor);
void addToGrid(QGridLayout* gridLayout, int nrow);
private:
QLabel* m_label;
QWidget* m_editor;
QDataWidgetMapper* m_dataMapper;
};
#endif
......@@ -63,10 +63,10 @@ void LayoutUtils::removeColumn(QGridLayout* layout, int column, bool deleteWidge
layout->setColumnStretch(column, 0);
}
void LayoutUtils::clearLayout(QGridLayout* layout)
void LayoutUtils::clearLayout(QGridLayout* layout, bool deleteWidgets)
{
for (int i_row = 0; i_row<layout->rowCount(); ++i_row) {
LayoutUtils::removeRow(layout, i_row);
LayoutUtils::removeRow(layout, i_row, deleteWidgets);
}
}
......
......@@ -34,7 +34,7 @@ BA_CORE_API_ void removeRow(QGridLayout *layout, int row, bool deleteWidgets = t
BA_CORE_API_ void removeColumn(QGridLayout *layout, int column, bool deleteWidgets = true);
//! Clear layout completely.
BA_CORE_API_ void clearLayout(QGridLayout* layout);
BA_CORE_API_ void clearLayout(QGridLayout* layout, bool deleteWidgets = true);
}
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment