Property editor: Hide headers and enable resizing on all cells.
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
# include <boost/algorithm/string/predicate.hpp>
|
||||
# include <QApplication>
|
||||
# include <QInputDialog>
|
||||
# include <QHeaderView>
|
||||
# include <QMenu>
|
||||
# include <QPainter>
|
||||
#endif
|
||||
@@ -59,6 +60,7 @@ PropertyEditor::PropertyEditor(QWidget *parent)
|
||||
, binding(false)
|
||||
, checkDocument(false)
|
||||
, closingEditor(false)
|
||||
, dragInProgress(false)
|
||||
{
|
||||
propertyModel = new PropertyModel(this);
|
||||
setModel(propertyModel);
|
||||
@@ -93,6 +95,10 @@ PropertyEditor::PropertyEditor(QWidget *parent)
|
||||
connect(this, &QTreeView::collapsed, this, &PropertyEditor::onItemCollapsed);
|
||||
connect(propertyModel, &QAbstractItemModel::rowsMoved, this, &PropertyEditor::onRowsMoved);
|
||||
connect(propertyModel, &QAbstractItemModel::rowsRemoved, this, &PropertyEditor::onRowsRemoved);
|
||||
|
||||
setHeaderHidden(true);
|
||||
viewport()->installEventFilter(this);
|
||||
viewport()->setMouseTracking(true);
|
||||
}
|
||||
|
||||
PropertyEditor::~PropertyEditor()
|
||||
@@ -827,4 +833,57 @@ void PropertyEditor::contextMenuEvent(QContextMenuEvent *) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool PropertyEditor::eventFilter(QObject* object, QEvent* event) {
|
||||
if (object == viewport()) {
|
||||
QMouseEvent* mouse_event = dynamic_cast<QMouseEvent*>(event);
|
||||
if (mouse_event) {
|
||||
if (mouse_event->type() == QEvent::MouseMove) {
|
||||
if (dragInProgress) { // apply dragging
|
||||
QHeaderView* header_view = header();
|
||||
int delta = mouse_event->pos().x() - dragPreviousPos;
|
||||
dragPreviousPos = mouse_event->pos().x();
|
||||
//using minimal size = dragSensibility * 2 to prevent collapsing
|
||||
header_view->resizeSection(dragSection,
|
||||
qMax(dragSensibility * 2, header_view->sectionSize(dragSection) + delta));
|
||||
return true;
|
||||
}
|
||||
else { // set mouse cursor shape
|
||||
if (indexResizable(mouse_event->pos()).isValid()) {
|
||||
viewport()->setCursor(Qt::SplitHCursor);
|
||||
}
|
||||
else {
|
||||
viewport()->setCursor(QCursor());
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (mouse_event->type() == QEvent::MouseButtonPress && mouse_event->button() == Qt::LeftButton && !dragInProgress) {
|
||||
if (indexResizable(mouse_event->pos()).isValid()) {
|
||||
dragInProgress = true;
|
||||
dragPreviousPos = mouse_event->x();
|
||||
dragSection = indexResizable(mouse_event->pos()).column();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (mouse_event->type() == QEvent::MouseButtonRelease &&
|
||||
mouse_event->button() == Qt::LeftButton && dragInProgress) {
|
||||
dragInProgress = false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
QModelIndex PropertyEditor::indexResizable(QPoint mouse_pos) {
|
||||
QModelIndex index = indexAt(mouse_pos - QPoint(dragSensibility + 1, 0));
|
||||
if (index.isValid()) {
|
||||
if (qAbs(visualRect(index).right() - mouse_pos.x()) < dragSensibility &&
|
||||
header()->sectionResizeMode(index.column()) == QHeaderView::Interactive) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
return QModelIndex();
|
||||
}
|
||||
|
||||
#include "moc_PropertyEditor.cpp"
|
||||
|
||||
@@ -100,6 +100,7 @@ protected Q_SLOTS:
|
||||
void onRowsRemoved(const QModelIndex &parent, int start, int end);
|
||||
|
||||
protected:
|
||||
bool eventFilter(QObject* object, QEvent* event);
|
||||
void closeEditor (QWidget * editor, QAbstractItemDelegate::EndEditHint hint) override;
|
||||
void commitData (QWidget * editor) override;
|
||||
void editorDestroyed (QObject * editor) override;
|
||||
@@ -120,6 +121,10 @@ private:
|
||||
void closeTransaction();
|
||||
void recomputeDocument(App::Document*);
|
||||
|
||||
// check if mouse_pos is around right or bottom side of a cell
|
||||
// and return the index of that cell if found
|
||||
QModelIndex indexResizable(QPoint mouse_pos);
|
||||
|
||||
private:
|
||||
PropertyItemDelegate *delegate;
|
||||
PropertyModel* propertyModel;
|
||||
@@ -133,6 +138,12 @@ private:
|
||||
bool binding;
|
||||
bool checkDocument;
|
||||
bool closingEditor;
|
||||
bool dragInProgress;
|
||||
|
||||
//max distance between mouse and a cell, small enough to trigger resize
|
||||
int dragSensibility = 5; // NOLINT
|
||||
int dragSection = 0;
|
||||
int dragPreviousPos = 0;
|
||||
|
||||
int transactionID = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user