Loading...
Searching...
No Matches
DesktopLayout.hpp
1#pragma once
2#include <Gfx/Window/WindowSettings.hpp>
3
4#include <QGraphicsRectItem>
5#include <QGraphicsScene>
6#include <QGraphicsView>
7
8#include <functional>
9#include <vector>
10
11namespace Gfx
12{
13
14class DesktopLayoutCanvas;
15
16// Represents an output window positioned on the virtual desktop
17class DesktopLayoutItem final : public QGraphicsRectItem
18{
19public:
20 explicit DesktopLayoutItem(
21 int index, const QRectF& rect, DesktopLayoutCanvas* canvas,
22 QGraphicsItem* parent = nullptr);
23
24 int outputIndex() const noexcept { return m_index; }
25 void setOutputIndex(int idx);
26
27 OutputLockMode lockMode{OutputLockMode::Free};
28 double aspectRatio{16.0 / 9.0}; // width/height, used for AspectRatio lock mode
29
30 void applyLockedState();
31
32 std::function<void()> onChanged;
33
34 void paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
35 override;
36
37protected:
38 QVariant itemChange(GraphicsItemChange change, const QVariant& value) override;
39 void mousePressEvent(QGraphicsSceneMouseEvent* event) override;
40 void mouseMoveEvent(QGraphicsSceneMouseEvent* event) override;
41 void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override;
42 void hoverMoveEvent(QGraphicsSceneHoverEvent* event) override;
43
44private:
45 enum ResizeEdge
46 {
47 None = 0,
48 Left = 1,
49 Right = 2,
50 Top = 4,
51 Bottom = 8
52 };
53 int hitTestEdges(const QPointF& pos) const;
54
55 int m_index{};
56 int m_resizeEdges{None};
57 QPointF m_dragStart{};
58 QRectF m_rectStart{};
59 QPointF m_moveAnchorScene{};
60 QPointF m_posAtPress{};
61 DesktopLayoutCanvas* m_canvas{};
62};
63
64// QGraphicsView that displays OS screens and draggable output window items
65class DesktopLayoutCanvas final : public QGraphicsView
66{
67public:
68 explicit DesktopLayoutCanvas(QWidget* parent = nullptr);
70
71 void refreshScreens();
72
73 void setWindowItems(const std::vector<OutputMapping>& mappings);
74 void addOutput(QPoint pos, QSize size);
75 void removeOutput(int index);
76 void updateItem(int index, QPoint pos, QSize size);
77 void selectItem(int index);
78
79 // Desktop coords <-> scene coords
80 QPointF desktopToScene(QPointF desktopPos) const;
81 QPointF sceneToDesktop(QPointF scenePos) const;
82 QSizeF desktopSizeToScene(QSize desktopSize) const;
83 QSize sceneSizeToDesktop(QSizeF sceneSize) const;
84
85 // Returns the screen index with largest overlap for the given desktop rect, or -1
86 int detectScreen(QRect windowRect) const;
87
88 bool snapEnabled() const noexcept { return m_snapEnabled; }
89 void setSnapEnabled(bool enabled);
90
91 // Returns snap-adjusted position for a moving item (in scene coords)
92 QPointF snapPosition(const DesktopLayoutItem* item, QPointF proposedPos) const;
93
94 std::function<void(int)> onSelectionChanged;
95 std::function<void(int)> onItemGeometryChanged;
96
97protected:
98 void resizeEvent(QResizeEvent* event) override;
99
100private:
101 void rebuildScreenRects();
102 void setupItemCallbacks(DesktopLayoutItem* item);
103
104 QGraphicsScene m_scene;
105 std::vector<QGraphicsRectItem*> m_screenRects;
106 std::vector<QGraphicsSimpleTextItem*> m_screenLabels;
107
108 QRectF m_virtualDesktopBounds; // in desktop coords
109 double m_scaleFactor{1.0};
110 QPointF m_sceneOffset; // scene offset = -bounds.topLeft() * scale
111 bool m_snapEnabled{true};
112};
113
114}
Definition DesktopLayout.hpp:66
Definition DesktopLayout.hpp:18
Binds the rendering pipeline to ossia processes.
Definition CameraDevice.cpp:30