#1875 Custom Object Editor : Add image from file to cell in grid layout

This commit is contained in:
Magne Sjaastad
2017-09-08 16:32:45 +02:00
parent 52ca5d5ddd
commit 13ef8fe076
6 changed files with 206 additions and 79 deletions

View File

@@ -59,31 +59,14 @@
namespace caf
{
/*
// Register default field editor for selected types
CAF_PDM_UI_REGISTER_DEFAULT_FIELD_EDITOR(PdmUiCheckBoxEditor, bool);
CAF_PDM_UI_REGISTER_DEFAULT_FIELD_EDITOR(PdmUiLineEditor, QString);
CAF_PDM_UI_REGISTER_DEFAULT_FIELD_EDITOR(PdmUiDateEditor, QDate);
CAF_PDM_UI_REGISTER_DEFAULT_FIELD_EDITOR(PdmUiDateEditor, QDateTime);
CAF_PDM_UI_REGISTER_DEFAULT_FIELD_EDITOR(PdmUiLineEditor, int);
CAF_PDM_UI_REGISTER_DEFAULT_FIELD_EDITOR(PdmUiLineEditor, double);
CAF_PDM_UI_REGISTER_DEFAULT_FIELD_EDITOR(PdmUiLineEditor, float);
CAF_PDM_UI_REGISTER_DEFAULT_FIELD_EDITOR(PdmUiLineEditor, quint64);
CAF_PDM_UI_REGISTER_DEFAULT_FIELD_EDITOR(PdmUiListEditor, std::vector<QString>);
CAF_PDM_UI_REGISTER_DEFAULT_FIELD_EDITOR(PdmUiListEditor, std::vector<int>);
CAF_PDM_UI_REGISTER_DEFAULT_FIELD_EDITOR(PdmUiListEditor, std::vector<unsigned int>);
CAF_PDM_UI_REGISTER_DEFAULT_FIELD_EDITOR(PdmUiListEditor, std::vector<float>);
*/
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
CustomObjectEditor::CustomObjectEditor()
{
m_columnCount = 2;
m_rowCount = 2;
m_columnCount = 3;
}
//--------------------------------------------------------------------------------------------------
@@ -105,6 +88,88 @@ void CustomObjectEditor::defineGrid(int rows, int columns)
m_columnCount = columns;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void CustomObjectEditor::addWidget(QWidget* w, int row, int column, int rowSpan, int columnSpan, Qt::Alignment /*= 0*/)
{
CAF_ASSERT(isAreaAvailable(row, column, rowSpan, columnSpan));
m_customWidgetAreas.push_back(WidgetAndArea(w, CustomObjectEditor::cellIds(row, column, rowSpan, columnSpan)));
m_layout->addWidget(w, row, column, rowSpan, columnSpan);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void CustomObjectEditor::addBlankCell(int row, int column)
{
m_customWidgetAreas.push_back(WidgetAndArea(nullptr, CustomObjectEditor::cellIds(row, column, 1, 1)));
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool CustomObjectEditor::isAreaAvailable(int row, int column, int rowSpan, int columnSpan) const
{
auto candidateCells = CustomObjectEditor::cellIds(row, column, rowSpan, columnSpan);
for (auto candidateCell : candidateCells)
{
if (isCellOccupied(candidateCell))
{
return false;
}
}
if (row + rowSpan > m_rowCount) return false;
if (column + columnSpan > m_columnCount) return false;
return true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool CustomObjectEditor::isCellOccupied(int cellId) const
{
for (auto customArea : m_customWidgetAreas)
{
for (auto occupiedCell : customArea.m_customWidgetCellIds)
{
if (cellId == occupiedCell)
{
return true;
}
}
}
return false;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void CustomObjectEditor::removeWidget(QWidget* w)
{
size_t indexToRemove = 10000;
for (size_t i = 0; i < m_customWidgetAreas.size(); i++)
{
if (w == m_customWidgetAreas[i].m_customWidget)
{
indexToRemove = i;
break;
}
}
if (indexToRemove < 10000)
{
m_layout->removeWidget(w);
m_customWidgetAreas.erase(m_customWidgetAreas.begin() + indexToRemove);
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -214,13 +279,78 @@ void CustomObjectEditor::cleanupBeforeSettingPdmObject()
m_groupBoxes.clear();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void CustomObjectEditor::resetDynamicCellCounter()
{
m_dynamicCellIndex = 0;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::pair<int, int> CustomObjectEditor::rowAndCell(int cellId) const
{
int column = cellId % m_columnCount;
int row = cellId / m_columnCount;
return std::make_pair(row, column);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int CustomObjectEditor::cellId(int row, int column) const
{
return row * m_columnCount + column;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int CustomObjectEditor::getNextAvailableCellId()
{
while (isCellOccupied(m_dynamicCellIndex) && m_dynamicCellIndex < (m_rowCount * m_columnCount))
{
m_dynamicCellIndex++;
}
if (isCellOccupied(m_dynamicCellIndex))
{
return -1;
}
else
{
return m_dynamicCellIndex++;
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<int> CustomObjectEditor::cellIds(int row, int column, int rowSpan, int columnSpan) const
{
std::vector<int> cells;
for (auto r = row; r < row + rowSpan; r++)
{
for (auto c = column; c < column + columnSpan; c++)
{
cells.push_back(cellId(r, c));
}
}
return cells;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void CustomObjectEditor::recursiveSetupFieldsAndGroupsRoot(const std::vector<PdmUiItem*>& uiItems, QWidget* parent, QGridLayout* parentLayout, const QString& uiConfigName)
{
int currentRowIndex = 0;
int currentColIndex = 0;
resetDynamicCellCounter();
QWidget* previousTabOrderWidget = NULL;
for (size_t i = 0; i < uiItems.size(); ++i)
@@ -264,13 +394,9 @@ void CustomObjectEditor::recursiveSetupFieldsAndGroupsRoot(const std::vector<Pdm
/// Insert the group box at the correct position of the parent layout
parentLayout->addWidget(groupBox, currentRowIndex, currentColIndex, 1, 1);
currentColIndex++;
if (currentColIndex > m_columnCount - 1)
{
currentColIndex = 0;
currentRowIndex++;
}
int nextCellId = getNextAvailableCellId();
std::pair<int, int> rowCol = rowAndCell(nextCellId);
parentLayout->addWidget(groupBox, rowCol.first, rowCol.second, 1, 1);
// Set Expanded state
bool isExpanded = isUiGroupExpanded(group);
@@ -280,7 +406,6 @@ void CustomObjectEditor::recursiveSetupFieldsAndGroupsRoot(const std::vector<Pdm
groupBox->setTitle(group->uiName(uiConfigName));
recursiveSetupFieldsAndGroups(groupChildren, groupBox->contentFrame(), groupBoxLayout, uiConfigName);
//currentRowIndex++;
}
}
@@ -526,53 +651,6 @@ void CustomObjectEditor::recursiveVerifyUniqueNames(const std::vector<PdmUiItem*
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
/*
caf::PdmUiFieldEditorHandle* PdmUiFieldEditorHelper::fieldEditorForField(PdmUiFieldHandle* field, const QString& uiConfigName)
{
caf::PdmUiFieldEditorHandle* fieldEditor = NULL;
// If editor type is specified, find in factory
if (!field->uiEditorTypeName(uiConfigName).isEmpty())
{
fieldEditor = caf::Factory<PdmUiFieldEditorHandle, QString>::instance()->create(field->uiEditorTypeName(uiConfigName));
}
else
{
// Find the default field editor
QString fieldTypeName = qStringTypeName(*(field->fieldHandle()));
if (fieldTypeName.indexOf("PdmPtrField") != -1)
{
fieldTypeName = caf::PdmUiComboBoxEditor::uiEditorTypeName();
}
else if (fieldTypeName.indexOf("PdmPtrArrayField") != -1)
{
fieldTypeName = caf::PdmUiListEditor::uiEditorTypeName();
}
else if (field->toUiBasedQVariant().type() != QVariant::List)
{
// Handle a single value field with valueOptions: Make a combobox
bool useOptionsOnly = true;
QList<PdmOptionItemInfo> options = field->valueOptions(&useOptionsOnly);
CAF_ASSERT(useOptionsOnly); // Not supported
if (!options.empty())
{
fieldTypeName = caf::PdmUiComboBoxEditor::uiEditorTypeName();
}
}
fieldEditor = caf::Factory<PdmUiFieldEditorHandle, QString>::instance()->create(fieldTypeName);
}
return fieldEditor;
}
*/
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -586,7 +664,6 @@ void CustomObjectEditor::groupBoxExpandedStateToggled(bool isExpanded)
if (!panel) return;
m_objectKeywordGroupUiNameExpandedState[objKeyword][panel->objectName()] = isExpanded;
}
} // end namespace caf

View File

@@ -56,6 +56,20 @@ class PdmUiItem;
class PdmUiGroup;
class WidgetAndArea
{
public:
WidgetAndArea(QWidget* w, const std::vector<int>& occupiedCellIds)
: m_customWidget(w),
m_customWidgetCellIds(occupiedCellIds)
{
}
QWidget* m_customWidget;
std::vector<int> m_customWidgetCellIds;
};
//==================================================================================================
///
//==================================================================================================
@@ -69,9 +83,14 @@ public:
void defineGrid(int rows, int columns);
//virtual void insertTopWidgetInlayout(QWidget* parentWidget);
// See QGridLayout::addWidget
void addWidget(QWidget* w, int row, int column, int rowSpan, int columnSpan, Qt::Alignment = 0);
//void insertWidget(QWidget* w, int row, int col);
void addBlankCell(int row, int column);
bool isCellOccupied(int cellId) const;
void removeWidget(QWidget* w);
bool isAreaAvailable(int row, int column, int rowSpan, int columnSpan) const;
protected:
virtual QWidget* createWidget(QWidget* parent) override;
@@ -82,6 +101,14 @@ protected slots:
void groupBoxExpandedStateToggled(bool isExpanded);
private:
void resetDynamicCellCounter();
std::pair<int, int> rowAndCell(int cellId) const;
int cellId(int row, int column) const;
int getNextAvailableCellId();
std::vector<int> cellIds(int row, int column, int rowSpan, int columnSpan) const;
void recursiveSetupFieldsAndGroupsRoot(const std::vector<PdmUiItem*>& uiItems, QWidget* parent, QGridLayout* parentLayout, const QString& uiConfigName);
void recursiveSetupFieldsAndGroups(const std::vector<PdmUiItem*>& uiItems, QWidget* parent, QGridLayout* parentLayout, const QString& uiConfigName);
bool isUiGroupExpanded(const PdmUiGroup* uiGroup);
@@ -98,6 +125,9 @@ private:
int m_rowCount;
int m_columnCount;
int m_dynamicCellIndex;
std::vector<WidgetAndArea> m_customWidgetAreas;
};

View File

@@ -463,10 +463,19 @@ MainWindow::MainWindow()
// Register default command features (add/delete item in list)
QPixmap pix;
pix.load(":/images/curvePlot.png");
m_plotLabel = new QLabel(this);
m_plotLabel->setPixmap(pix.scaled(250, 100));
m_smallPlotLabel = new QLabel(this);
m_smallPlotLabel->setPixmap(pix.scaled(100, 50));
createActions();
createDockPanels();
buildTestModel();
setPdmRoot(m_testRoot);
sm_mainWindowInstance = this;
@@ -643,7 +652,13 @@ void MainWindow::setPdmRoot(caf::PdmObjectHandle* pdmRoot)
pdmRoot->descendantsIncludingThisOfType(obj);
if (obj.size() == 1)
{
m_customObjectEditor->defineGrid(5, 4);
m_customObjectEditor->setPdmObject(obj[0]);
m_customObjectEditor->addBlankCell(0, 0);
m_customObjectEditor->addWidget(m_plotLabel, 0, 1, 1, 2);
m_customObjectEditor->addWidget(m_smallPlotLabel, 1, 2, 2, 1);
m_customObjectEditor->updateUi();
}
}

View File

@@ -7,6 +7,7 @@
class DemoPdmObject;
class QTreeView;
class QUndoView;
class QLabel;
namespace caf
{
@@ -62,5 +63,8 @@ private:
caf::PdmObjectCollection* m_testRoot;
caf::CustomObjectEditor* m_customObjectEditor;
QLabel* m_plotLabel;
QLabel* m_smallPlotLabel;
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

View File

@@ -1,5 +1,6 @@
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/">
<file>images/curvePlot.png</file>
<file>images/logo32.png</file>
<file>images/mac/editcopy.png</file>
<file>images/mac/editcut.png</file>