Skip to content
Open
Show file tree
Hide file tree
Changes from 15 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
6 changes: 6 additions & 0 deletions include/QtNodes/internal/BasicGraphicsScene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class NODE_EDITOR_PUBLIC BasicGraphicsScene : public QGraphicsScene
*/
virtual QMenu *createSceneMenu(QPointF const scenePos);

QMenu *createZoomMenu(QPointF const scenePos);

Q_SIGNALS:
void modified(BasicGraphicsScene *);
void nodeMoved(NodeId const nodeId, QPointF const &newLocation);
Expand All @@ -124,6 +126,10 @@ class NODE_EDITOR_PUBLIC BasicGraphicsScene : public QGraphicsScene
/// Signal allows showing custom context menu upon clicking a node.
void nodeContextMenu(NodeId const nodeId, QPointF const pos);

/// Signals to call Graphics View's zoomFit methods
void zoomFitAllClicked();
void zoomFitSelectedClicked();

private:
/**
* @brief Creates Node and Connection graphics objects.
Expand Down
4 changes: 4 additions & 0 deletions include/QtNodes/internal/GraphicsView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public Q_SLOTS:

void onPasteObjects();

void zoomFitAll();

void zoomFitSelected();

Q_SIGNALS:
void scaleChanged(double scale);

Expand Down
11 changes: 7 additions & 4 deletions include/QtNodes/internal/NodeDelegateModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ namespace QtNodes {
struct NodeValidationState
{
enum class State : int {
Valid = 0, ///< All required inputs are present and correct.
Warning = 1, ///< Some inputs are missing or questionable, processing may be unreliable.
Error = 2, ///< Inputs or settings are invalid, preventing successful computation.
Valid = 0, ///< All required inputs are present and correct.
Warning = 1, ///< Some inputs are missing or questionable, processing may be unreliable.
Error = 2, ///< Inputs or settings are invalid, preventing successful computation.
};
bool isValid() { return _state == State::Valid; };
QString const message() { return _stateMessage; }
Expand All @@ -39,7 +39,9 @@ class StyleCollection;
* AbstractGraphModel.
* This class is the same what has been called NodeDataModel before v3.
*/
class NODE_EDITOR_PUBLIC NodeDelegateModel : public QObject, public Serializable
class NODE_EDITOR_PUBLIC NodeDelegateModel
: public QObject
, public Serializable
{
Q_OBJECT

Expand Down Expand Up @@ -80,6 +82,7 @@ class NODE_EDITOR_PUBLIC NodeDelegateModel : public QObject, public Serializable
virtual ConnectionPolicy portConnectionPolicy(PortType, PortIndex) const;

NodeStyle const &nodeStyle() const;

void setNodeStyle(NodeStyle const &style);

virtual void setInData(std::shared_ptr<NodeData> nodeData, PortIndex const portIndex) = 0;
Expand Down
68 changes: 68 additions & 0 deletions src/BasicGraphicsScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

#include <QUndoStack>

#include <QHeaderView>
#include <QLineEdit>
#include <QTreeWidget>
#include <QWidgetAction>
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QGraphicsSceneMoveEvent>

Expand Down Expand Up @@ -198,6 +202,70 @@ QMenu *BasicGraphicsScene::createSceneMenu(QPointF const scenePos)
return nullptr;
}

QMenu *BasicGraphicsScene::createZoomMenu(QPointF const scenePos)
{
Q_UNUSED(scenePos);

QMenu *menu = new QMenu();

auto *txtBox = new QLineEdit(menu);
txtBox->setPlaceholderText(QStringLiteral("Filter"));
txtBox->setClearButtonEnabled(true);

auto *txtBoxAction = new QWidgetAction(menu);
txtBoxAction->setDefaultWidget(txtBox);
menu->addAction(txtBoxAction);

QTreeWidget *treeView = new QTreeWidget(menu);
treeView->header()->close();

treeView->setMaximumHeight(100);
treeView->setMaximumWidth(150);

auto *treeViewAction = new QWidgetAction(menu);
treeViewAction->setDefaultWidget(treeView);
menu->addAction(treeViewAction);

auto freezeItem = new QTreeWidgetItem(treeView);
freezeItem->setText(0, "Zoom Fit All");

auto unfreezeItem = new QTreeWidgetItem(treeView);
unfreezeItem->setText(0, "Zoom Fit Selected");

treeView->expandAll();

connect(treeView, &QTreeWidget::itemClicked, [this, menu](QTreeWidgetItem *item, int) {
if (item->text(0) == "Zoom Fit All") {
Q_EMIT zoomFitAllClicked();

menu->close();
return;
}
if (item->text(0) == "Zoom Fit Selected") {
Q_EMIT zoomFitSelectedClicked();

menu->close();
return;
}
});

// Filter
connect(txtBox, &QLineEdit::textChanged, [treeView](const QString &text) {
QTreeWidgetItemIterator it(treeView);
while (*it) {
auto modelName = (*it)->text(0);
const bool match = (modelName.contains(text, Qt::CaseInsensitive));
(*it)->setHidden(!match);
++it;
}
});

txtBox->setFocus();
menu->setAttribute(Qt::WA_DeleteOnClose);

return menu;
}

void BasicGraphicsScene::traverseGraphAndPopulateGraphicsObjects()
{
auto allNodeIds = _graphModel.allNodeIds();
Expand Down
5 changes: 0 additions & 5 deletions src/DefaultNodePainter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#include <cmath>


namespace QtNodes {

void DefaultNodePainter::paint(QPainter *painter, NodeGraphicsObject &ngo) const
Expand Down Expand Up @@ -82,7 +81,6 @@ void DefaultNodePainter::drawNodeRect(QPainter *painter, NodeGraphicsObject &ngo
QPen p(color, penWidth);
painter->setPen(p);


QLinearGradient gradient(QPointF(0.0, 0.0), QPointF(2.0, size.height()));
gradient.setColorAt(0.0, nodeStyle.GradientColor0);
gradient.setColorAt(0.10, nodeStyle.GradientColor1);
Expand Down Expand Up @@ -113,7 +111,6 @@ void DefaultNodePainter::drawConnectionPoints(QPainter *painter, NodeGraphicsObj
auto reducedDiameter = diameter * 0.6;

for (PortType portType : {PortType::Out, PortType::In}) {

auto portCountRole = (portType == PortType::Out) ? NodeRole::OutPortCount
: NodeRole::InPortCount;
size_t const n = model.nodeData(nodeId, portCountRole).toUInt();
Expand Down Expand Up @@ -329,13 +326,11 @@ void DefaultNodePainter::drawValidationIcon(QPainter *painter, NodeGraphicsObjec
painter->setBrush(color);
painter->drawEllipse(center, iconSize.width() / 2.0 + 2.0, iconSize.height() / 2.0 + 2.0);


QPainter imgPainter(&pixmap);
imgPainter.setCompositionMode(QPainter::CompositionMode_SourceIn);
imgPainter.fillRect(pixmap.rect(), nodeStyle.FontColor);
imgPainter.end();


painter->drawPixmap(center.toPoint() - QPoint(iconSize.width() / 2, iconSize.height() / 2),
pixmap);

Expand Down
38 changes: 32 additions & 6 deletions src/GraphicsView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ void GraphicsView::setScene(BasicGraphicsScene *scene)
auto redoAction = scene->undoStack().createRedoAction(this, tr("&Redo"));
redoAction->setShortcuts(QKeySequence::Redo);
addAction(redoAction);

/// Connections to context menu funcionality
connect(scene, &BasicGraphicsScene::zoomFitAllClicked, this, &GraphicsView::zoomFitAll);
connect(scene, &BasicGraphicsScene::zoomFitSelectedClicked, this, &GraphicsView::zoomFitSelected);
}

void GraphicsView::centerScene()
Expand All @@ -166,18 +170,20 @@ void GraphicsView::centerScene()

void GraphicsView::contextMenuEvent(QContextMenuEvent *event)
{
QGraphicsView::contextMenuEvent(event);
QMenu *menu;

if (itemAt(event->pos())) {
QGraphicsView::contextMenuEvent(event);
return;
menu = nodeScene()->createZoomMenu(mapToScene(event->pos()));
} else {
menu = nodeScene()->createSceneMenu(mapToScene(event->pos()));
}

auto const scenePos = mapToScene(event->pos());

QMenu *menu = nodeScene()->createSceneMenu(scenePos);

if (menu) {
menu->exec(event->globalPos());
}

return;
}

void GraphicsView::wheelEvent(QWheelEvent *event)
Expand Down Expand Up @@ -407,3 +413,23 @@ QPointF GraphicsView::scenePastePosition()

return mapToScene(origin);
}

void GraphicsView::zoomFitAll()
{
fitInView(scene()->itemsBoundingRect(), Qt::KeepAspectRatio);
}

void GraphicsView::zoomFitSelected()
{
if(scene()->selectedItems().count() > 0){

QRectF unitedBoundingRect{};

for(QGraphicsItem * item : scene()->selectedItems())
{
unitedBoundingRect = unitedBoundingRect.united(item->mapRectToScene(item->boundingRect()));
}

fitInView(unitedBoundingRect, Qt::KeepAspectRatio);
}
}
Loading