Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property editor: Hide header and enable resizing on all cells. #11931

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/Gui/propertyeditor/PropertyEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# include <boost/algorithm/string/predicate.hpp>
# include <QApplication>
# include <QInputDialog>
# include <QHeaderView>
# include <QMenu>
# include <QPainter>
#endif
Expand All @@ -46,7 +47,7 @@
#include "ViewProviderDocumentObject.h"


FC_LOG_LEVEL_INIT("PropertyView", true, true)

Check warning on line 50 in src/Gui/propertyeditor/PropertyEditor.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

variable '_s_fclvl' is non-const and globally accessible, consider making it const [cppcoreguidelines-avoid-non-const-global-variables]

using namespace Gui::PropertyEditor;

Expand All @@ -59,15 +60,16 @@
, binding(false)
, checkDocument(false)
, closingEditor(false)
, dragInProgress(false)
{
propertyModel = new PropertyModel(this);

Check warning on line 65 in src/Gui/propertyeditor/PropertyEditor.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

assigning newly created 'gsl::owner<>' to non-owner 'Gui::PropertyEditor::PropertyModel *' [cppcoreguidelines-owning-memory]

Check warning on line 65 in src/Gui/propertyeditor/PropertyEditor.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

'propertyModel' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
setModel(propertyModel);

setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);

delegate = new PropertyItemDelegate(this);

Check warning on line 71 in src/Gui/propertyeditor/PropertyEditor.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

assigning newly created 'gsl::owner<>' to non-owner 'Gui::PropertyEditor::PropertyItemDelegate *' [cppcoreguidelines-owning-memory]

Check warning on line 71 in src/Gui/propertyeditor/PropertyEditor.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

'delegate' should be initialized in a member initializer of the constructor [cppcoreguidelines-prefer-member-initializer]
delegate->setItemEditorFactory(new PropertyItemEditorFactory);

Check warning on line 72 in src/Gui/propertyeditor/PropertyEditor.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

initializing non-owner argument of type 'QItemEditorFactory *' with a newly created 'gsl::owner<>' [cppcoreguidelines-owning-memory]
setItemDelegate(delegate);

setAlternatingRowColors(true);
Expand All @@ -93,13 +95,17 @@
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()
{
QItemEditorFactory* f = delegate->itemEditorFactory();

Check warning on line 106 in src/Gui/propertyeditor/PropertyEditor.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

variable name 'f' is too short, expected at least 2 characters [readability-identifier-length]
delegate->setItemEditorFactory(nullptr);
delete f;

Check warning on line 108 in src/Gui/propertyeditor/PropertyEditor.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

deleting a pointer through a type that is not marked 'gsl::owner<>'; consider using a smart pointer instead [cppcoreguidelines-owning-memory]
}

void PropertyEditor::setAutomaticExpand(bool v)
Expand Down Expand Up @@ -827,4 +833,57 @@
}
}


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"

Check failure on line 889 in src/Gui/propertyeditor/PropertyEditor.cpp

View workflow job for this annotation

GitHub Actions / Lint / Lint

'moc_PropertyEditor.cpp' file not found [clang-diagnostic-error]
11 changes: 11 additions & 0 deletions src/Gui/propertyeditor/PropertyEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
Q_PROPERTY(QBrush itemBackground READ itemBackground WRITE setItemBackground DESIGNABLE true SCRIPTABLE true) // clazy:exclude=qproperty-without-notify

public:
PropertyEditor(QWidget *parent = nullptr);

Check warning on line 70 in src/Gui/propertyeditor/PropertyEditor.h

View workflow job for this annotation

GitHub Actions / Lint / Lint

Constructors callable with one argument should be marked explicit. [runtime/explicit] [5]
~PropertyEditor() override;

/** Builds up the list view with the properties. */
Expand Down Expand Up @@ -100,6 +100,7 @@
void onRowsRemoved(const QModelIndex &parent, int start, int end);

protected:
bool eventFilter(QObject* object, QEvent* event);

Check warning on line 103 in src/Gui/propertyeditor/PropertyEditor.h

View workflow job for this annotation

GitHub Actions / Lint / Lint

'eventFilter' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 103 in src/Gui/propertyeditor/PropertyEditor.h

View workflow job for this annotation

GitHub Actions / Lint / Lint

'eventFilter' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]
void closeEditor (QWidget * editor, QAbstractItemDelegate::EndEditHint hint) override;
void commitData (QWidget * editor) override;
void editorDestroyed (QObject * editor) override;
Expand All @@ -120,6 +121,10 @@
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;
Expand All @@ -133,6 +138,12 @@
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;

Expand Down
Loading