2 #include <score/model/Skin.hpp>
6 #include <QGraphicsItem>
7 #include <QGraphicsSceneMouseEvent>
11 #include <avnd/concepts/painter.hpp>
12 #include <halp/texture.hpp>
24 void begin_path() { path = QPainterPath{}; }
25 void close_path() { path.closeSubpath(); }
26 void stroke() { painter.strokePath(path, painter.pen()); }
27 void fill() { painter.fillPath(path, painter.brush()); }
28 void update() { item.update(); }
30 void move_to(
double x,
double y) { path.moveTo(x, y); }
31 void line_to(
double x,
double y) { path.lineTo(x, y); }
32 void arc_to(
double x,
double y,
double w,
double h,
double start,
double length)
34 path.arcTo(x, y, w, h, start, length);
37 void cubic_to(
double c1x,
double c1y,
double c2x,
double c2y,
double endx,
double endy)
39 path.cubicTo(c1x, c1y, c2x, c2y, endx, endy);
42 void quad_to(
double x1,
double y1,
double x2,
double y2)
44 path.quadTo(x1, y1, x2, y2);
47 void translate(
double x,
double y) { painter.translate(x, y); }
48 void scale(
double x,
double y) { painter.scale(x, y); }
49 void rotate(
double a) { painter.rotate(a); }
50 void reset_transform() { painter.resetTransform(); }
53 void set_stroke_color(halp::rgba_color c)
55 QPen p = painter.pen();
56 p.setColor(qRgba(c.r, c.g, c.b, c.a));
60 void set_stroke_width(
double w)
62 QPen p = painter.pen();
67 void set_fill_color(halp::rgba_color c)
69 painter.setBrush(QColor(qRgba(c.r, c.g, c.b, c.a)));
72 void set_linear_gradient(
73 double x1,
double y1,
double x2,
double y2, halp::rgba_color c1,
76 QLinearGradient gradient(QPointF(x1, y1), QPointF(x2, y2));
77 gradient.setColorAt(0, QColor(qRgba(c1.r, c1.g, c1.b, c1.a)));
78 gradient.setColorAt(1, QColor(qRgba(c2.r, c2.g, c2.b, c2.a)));
79 painter.setBrush(gradient);
82 void set_radial_gradient(
83 double cx,
double cy,
double cr, halp::rgba_color c1, halp::rgba_color c2)
85 QRadialGradient gradient(cx, cy, cr);
86 gradient.setColorAt(0, QColor(qRgba(c1.r, c1.g, c1.b, c1.a)));
87 gradient.setColorAt(1, QColor(qRgba(c2.r, c2.g, c2.b, c2.a)));
88 painter.setBrush(gradient);
91 void set_conical_gradient(
92 double x,
double y,
double a, halp::rgba_color c1, halp::rgba_color c2)
94 QConicalGradient gradient(x, y, a);
95 gradient.setColorAt(0, QColor(qRgba(c1.r, c1.g, c1.b, c1.a)));
96 gradient.setColorAt(1, QColor(qRgba(c2.r, c2.g, c2.b, c2.a)));
97 painter.setBrush(gradient);
101 void set_font(std::string_view f)
103 auto font = painter.font();
104 font.setFamily(QString::fromUtf8(f.data(), f.size()));
105 painter.setFont(font);
108 void set_font_size(
double f)
110 auto font = painter.font();
111 font.setPointSize(f);
112 painter.setFont(font);
115 void draw_text(
double x,
double y, std::string_view str)
117 path.addText(x, y, painter.font(), QString::fromUtf8(str.data(), str.size()));
121 void draw_line(
double x1,
double y1,
double x2,
double y2)
128 void draw_triangle(
double x1,
double y1,
double x2,
double y2,
double x3,
double y3)
134 painter.drawPath(path);
138 void draw_rect(
double x,
double y,
double w,
double h) { path.addRect(x, y, w, h); }
141 void draw_rounded_rect(
double x,
double y,
double w,
double h,
double r)
143 path.addRoundedRect(x, y, w, h, r, r);
147 void draw_pixmap(
double x,
double y,
const QString& str)
149 painter.drawPixmap(x, y, str);
153 void draw_ellipse(
double x,
double y,
double w,
double h)
155 path.addEllipse(x, y, w, h);
159 void draw_circle(
double cx,
double cy,
double cr)
161 path.addEllipse(QPointF{cx, cy}, cr, cr);
165 void draw_polygon(
const double* tab,
int count)
169 for(
int i = 0; i < count * 2; i += 2)
173 poly << QPointF(x, y);
175 path.addPolygon(poly);
179 int x,
int y,
int w,
int h,
const unsigned char* image,
int img_w,
int img_h)
181 auto img = QImage(image, img_w, img_h, QImage::Format_RGB32);
182 painter.drawImage(QRect(x, y, w, h), img, QRect(0, 0, img_w, img_h));
185 static_assert(avnd::painter<QPainterAdapter>);
187 template <
typename Item,
typename Control =
void>
192 using item_type = std::decay_t<Item>;
201 this->setFlag(QGraphicsItem::ItemClipsToShape);
202 this->setFlag(QGraphicsItem::ItemClipsChildrenToShape);
204 if constexpr(requires { this->impl.update = [
this] {}; })
206 this->impl.update = [
this] { this->update(); };
210 QRectF boundingRect()
const override
212 return {0., 0., item_type::width(), item_type::height()};
215 void paint(QPainter* painter,
const QStyleOptionGraphicsItem* option, QWidget* widget)
218 auto& skin = score::Skin::instance();
219 painter->setRenderHint(QPainter::Antialiasing,
true);
220 painter->setPen(skin.Dark.main.pen1);
222 painter->setRenderHint(QPainter::Antialiasing,
false);
234 friend button& operator|=(button& lhs, button rhs) noexcept
237 enum button&)(
reinterpret_cast<std::underlying_type_t<enum button>&
>(lhs) |=
reinterpret_cast<std::underlying_type_t<enum button>&
>(rhs));
247 friend modifier& operator|=(modifier& lhs, modifier rhs) noexcept
250 enum modifier&)(
reinterpret_cast<std::underlying_type_t<enum modifier>&
>(lhs) |=
reinterpret_cast<std::underlying_type_t<enum modifier>&
>(rhs));
255 enum button button = {};
256 enum button held_buttons = {};
257 enum modifier modifiers = {};
265 p.x =
event->pos().x();
266 p.y =
event->pos().y();
268 if(event->button() == Qt::LeftButton)
269 p.button = custom_mouse_event::left;
270 else if(event->button() == Qt::RightButton)
271 p.button = custom_mouse_event::right;
272 else if(event->button() == Qt::MiddleButton)
273 p.button = custom_mouse_event::middle;
275 if(event->buttons() & Qt::LeftButton)
276 p.held_buttons |= p.left;
277 if(event->buttons() & Qt::RightButton)
278 p.held_buttons |= p.right;
279 if(event->buttons() & Qt::MiddleButton)
280 p.held_buttons |= p.middle;
282 if(event->modifiers() & Qt::ShiftModifier)
283 p.modifiers |= p.shift;
284 if(event->modifiers() & Qt::AltModifier)
285 p.modifiers |= p.alt;
286 if(event->modifiers() & Qt::ControlModifier)
287 p.modifiers |= p.ctrl;
288 if(event->modifiers() & Qt::MetaModifier)
289 p.modifiers |= p.meta;
294 void mousePressEvent(QGraphicsSceneMouseEvent* event)
override
296 if constexpr(requires { impl.mouse_press(0, 0); })
298 if(impl.mouse_press(event->pos().x(), event->pos().y()))
301 else if constexpr(requires { impl.mouse_press(custom_mouse_event{}); })
303 if(impl.mouse_press(make_event(event)))
309 void mouseMoveEvent(QGraphicsSceneMouseEvent* event)
override
311 if constexpr(requires { impl.mouse_move(0, 0); })
313 impl.mouse_move(event->pos().x(), event->pos().y());
316 else if constexpr(requires { impl.mouse_move(custom_mouse_event{}); })
318 impl.mouse_move(make_event(event));
324 void mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
override
326 if constexpr(requires { impl.mouse_release(0, 0); })
328 impl.mouse_release(event->pos().x(), event->pos().y());
331 else if constexpr(requires { impl.mouse_release(custom_mouse_event{}); })
333 impl.mouse_release(make_event(event));
343 template <
typename Item>
351 , cmd{ctx.commandStack}
353 if constexpr(requires { this->impl.transaction; })
355 this->impl.transaction.start = [] {
358 this->impl.transaction.update = [
this, &ctl](
const auto& value) {
359 auto val = oscr::to_ossia_value(value);
362 this->impl.value = value;
365 this->impl.transaction.commit = [
this] { cmd.
commit(); };
366 this->impl.transaction.rollback = [
this] { cmd.
rollback(); };
The OngoingCommandDispatcher class.
Definition: OngoingCommandDispatcher.hpp:27
void submit(Args &&... args)
Definition: OngoingCommandDispatcher.hpp:37
void commit()
Definition: OngoingCommandDispatcher.hpp:61
void rollback()
If the command has to be reverted, for instance when pressing escape.
Definition: OngoingCommandDispatcher.hpp:71
Definition: score-lib-process/Process/Dataflow/Port.hpp:202
Definition: SetControlValue.hpp:13
Definition: Painter.hpp:345
Definition: Painter.hpp:189
Definition: Painter.hpp:226
Definition: Painter.hpp:19
Definition: DocumentContext.hpp:18