Add a view id tag to project tree and tooltip

* Remove it from property editor
This commit is contained in:
Gaute Lindkvist
2019-11-04 15:26:03 +01:00
parent f222f73718
commit 03ff43237d
21 changed files with 261 additions and 35 deletions

View File

@@ -351,6 +351,7 @@ void PdmUiTreeSelectionEditor::configureAndUpdateUi(const QString& uiConfigName)
}
}
}
// If the tree doesn't have grand children we treat this as a straight list
m_treeView->setRootIsDecorated(m_model->hasGrandChildren());

View File

@@ -51,7 +51,9 @@
#include <QGridLayout>
#include <QMenu>
#include <QModelIndexList>
#include <QPainter>
#include <QSortFilterProxyModel>
#include <QStyleOptionViewItem>
#include <QTreeView>
#include <QWidget>
@@ -108,6 +110,7 @@ PdmUiTreeViewEditor::PdmUiTreeViewEditor()
m_layout = nullptr;
m_treeView = nullptr;
m_treeViewModel = nullptr;
m_delegate = nullptr;
}
//--------------------------------------------------------------------------------------------------
@@ -133,6 +136,10 @@ QWidget* PdmUiTreeViewEditor::createWidget(QWidget* parent)
m_treeView->setModel(m_treeViewModel);
m_treeView->installEventFilter(this);
m_delegate = new PdmUiTreeViewItemDelegate(m_treeView, m_treeViewModel);
m_treeView->setItemDelegate(m_delegate);
connect(treeView()->selectionModel(), SIGNAL(selectionChanged( const QItemSelection & , const QItemSelection & )), SLOT(slotOnSelectionChanged( const QItemSelection & , const QItemSelection & )));
m_layout->addWidget(m_treeView);
@@ -153,7 +160,7 @@ void PdmUiTreeViewEditor::configureAndUpdateUi(const QString& uiConfigName)
PdmUiObjectHandle* uiObjectHandle = dynamic_cast<PdmUiObjectHandle*>(this->pdmItemRoot());
if (uiObjectHandle)
{
uiObjectHandle->objectEditorAttribute(uiConfigName, &editorAttributes);
uiObjectHandle->objectEditorAttribute(uiConfigName, &editorAttributes);
}
}
@@ -169,6 +176,12 @@ void PdmUiTreeViewEditor::configureAndUpdateUi(const QString& uiConfigName)
selectAsCurrentItem(uiObjectHandle);
}
}
if (m_delegate)
{
m_delegate->clearAttributes();
updateItemDelegateForSubTree();
}
}
//--------------------------------------------------------------------------------------------------
@@ -213,7 +226,12 @@ void PdmUiTreeViewEditor::selectedUiItems(std::vector<PdmUiItem*>& objects)
//--------------------------------------------------------------------------------------------------
void PdmUiTreeViewEditor::updateMySubTree(PdmUiItem* uiItem)
{
if (m_treeViewModel) { m_treeViewModel->updateSubTree(uiItem); }
if (m_treeViewModel)
{
m_treeViewModel->updateSubTree(uiItem);
QModelIndex index = m_treeViewModel->findModelIndex(uiItem);
updateItemDelegateForSubTree(index);
}
}
//--------------------------------------------------------------------------------------------------
@@ -422,6 +440,29 @@ void PdmUiTreeViewEditor::updateSelectionManager()
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTreeViewEditor::updateItemDelegateForSubTree(const QModelIndex& modelIndex /*= QModelIndex()*/)
{
auto allIndices = m_treeViewModel->allIndicesRecursive();
for (QModelIndex index : allIndices)
{
PdmUiItem* uiItem = m_treeViewModel->uiItemFromModelIndex(index);
PdmUiObjectHandle* uiObjectHandle = dynamic_cast<PdmUiObjectHandle*>(uiItem);
if (uiObjectHandle)
{
PdmUiTreeViewItemAttribute attribute;
uiObjectHandle->objectEditorAttribute("", &attribute);
if (!attribute.tag.isEmpty())
{
m_delegate->addAttribute(index, attribute);
}
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -439,4 +480,98 @@ bool PdmUiTreeViewEditor::isAppendOfClassNameToUiItemTextEnabled()
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
PdmUiTreeViewItemDelegate::PdmUiTreeViewItemDelegate(QObject* parent, PdmUiTreeViewQModel* model)
: QStyledItemDelegate(parent)
, m_model(model)
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTreeViewItemDelegate::clearAttributes()
{
m_attributes.clear();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTreeViewItemDelegate::addAttribute(QModelIndex index, const PdmUiTreeViewItemAttribute& attribute)
{
m_attributes[index] = attribute;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void PdmUiTreeViewItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyledItemDelegate::paint(painter, option, index);
// Save painter so we can restore it
painter->save();
auto it = m_attributes.find(index);
if (it == m_attributes.end()) return;
const int insideTopBottomMargins = 1;
const int insideleftRightMargins = 6;
const int outsideLeftRightMargins = 4;
QFont font = QApplication::font();
if (font.pixelSize() > 0)
{
font.setPixelSize(std::max(1, font.pixelSize() - 2));
}
else
{
font.setPointSize(std::max(1, font.pointSize() - 2));
}
painter->setFont(font);
QString text = it->second.tag;
QColor bgColor = it->second.bgColor;
QColor fgColor = it->second.fgColor;
QSize textSize(QFontMetrics(font).size(Qt::TextSingleLine, text));
QRect rect = option.rect;
QSize fullSize = rect.size();
int textDiff = (fullSize.height() - textSize.height());
QRect textRect;
if (it->second.position == PdmUiTreeViewItemAttribute::AT_END)
{
QPoint bottomRight = rect.bottomRight() - QPoint(outsideLeftRightMargins, 0);
QPoint textBottomRight = bottomRight - QPoint(insideleftRightMargins, textDiff / 2);
QPoint textTopLeft = textBottomRight - QPoint(textSize.width(), textSize.height());
textRect = QRect(textTopLeft, textBottomRight);
}
else
{
QPoint textTopLeft = QPoint(0, rect.topLeft().y()) + QPoint(outsideLeftRightMargins + insideleftRightMargins, + textDiff / 2);
QPoint textBottomRight = textTopLeft + QPoint(textSize.width(), textSize.height());
textRect = QRect(textTopLeft, textBottomRight);
}
QRect tagRect = textRect.marginsAdded(QMargins(insideleftRightMargins, insideTopBottomMargins, insideleftRightMargins, insideTopBottomMargins));
QBrush brush(bgColor);
painter->setBrush(brush);
painter->setPen(bgColor);
painter->setRenderHint(QPainter::Antialiasing);
const double xRoundingRadiusPercent = 50.0;
const double yRoundingRadiusPercent = 25.0;
painter->drawRoundedRect(tagRect, xRoundingRadiusPercent, yRoundingRadiusPercent, Qt::RelativeSize);
painter->setPen(fgColor);
painter->drawText(textRect, Qt::AlignCenter, text);
// Restore painter
painter->restore();
}
} // end namespace caf

View File

@@ -42,9 +42,11 @@
#include "cafPdmUiTreeViewQModel.h"
#include <QAbstractItemModel>
#include <QColor>
#include <QPointer>
#include <QWidget>
#include <QItemSelectionModel>
#include <QStyledItemDelegate>
#include <QTreeView>
@@ -61,9 +63,45 @@ namespace caf
class PdmChildArrayFieldHandle;
class PdmUiDragDropInterface;
class PdmUiItem;
class PdmUiTreeViewEditor;
class PdmUiTreeViewQModel;
class PdmUiTreeViewWidget;
class PdmUiTreeViewItemAttribute : public PdmUiEditorAttribute
{
public:
enum Position
{
IN_FRONT,
AT_END
};
PdmUiTreeViewItemAttribute()
: tag()
, position(AT_END)
, bgColor(Qt::red)
, fgColor(Qt::white)
{
}
QString tag;
Position position;
QColor bgColor;
QColor fgColor;
};
class PdmUiTreeViewItemDelegate : public QStyledItemDelegate
{
public:
PdmUiTreeViewItemDelegate(QObject* parent, PdmUiTreeViewQModel* model);
void clearAttributes();
void addAttribute(QModelIndex index, const PdmUiTreeViewItemAttribute& attribute);
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
private:
PdmUiTreeViewQModel* m_model;
std::map<QModelIndex, PdmUiTreeViewItemAttribute> m_attributes;
};
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -131,6 +169,7 @@ private:
PdmChildArrayFieldHandle* currentChildArrayFieldHandle();
void updateSelectionManager();
void updateItemDelegateForSubTree(const QModelIndex& modelIndex = QModelIndex());
bool eventFilter(QObject *obj, QEvent *event) override;
@@ -139,7 +178,8 @@ private:
QVBoxLayout* m_layout;
PdmUiTreeViewWidget* m_treeView;
PdmUiTreeViewQModel* m_treeViewModel;
PdmUiTreeViewQModel* m_treeViewModel;
PdmUiTreeViewItemDelegate* m_delegate;
bool m_useDefaultContextMenu;
bool m_updateSelectionManager;

View File

@@ -396,6 +396,28 @@ void PdmUiTreeViewQModel::updateEditorsForSubTree(PdmUiTreeOrdering* root)
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::list<QModelIndex> PdmUiTreeViewQModel::allIndicesRecursive(const QModelIndex& current) const
{
std::list<QModelIndex> currentAndDescendants;
currentAndDescendants.push_back(current);
int rows = rowCount(current);
int cols = columnCount(current);
for (int row = 0; row < rows; ++row)
{
for (int col = 0; col < cols; ++col)
{
QModelIndex childIndex = index(row, col, current);
std::list<QModelIndex> subList = allIndicesRecursive(childIndex);
currentAndDescendants.insert(currentAndDescendants.end(), subList.begin(), subList.end());
}
}
return currentAndDescendants;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@@ -75,6 +75,8 @@ public:
void setDragDropInterface(PdmUiDragDropInterface* dragDropInterface);
PdmUiDragDropInterface* dragDropInterface();
std::list<QModelIndex> allIndicesRecursive(const QModelIndex& current = QModelIndex()) const;
private:
void updateSubTreeRecursive(const QModelIndex& uiSubTreeRootModelIdx, PdmUiTreeOrdering* uiModelSubTreeRoot, PdmUiTreeOrdering* updatedPdmSubTreeRoot);