2 #include <score/model/Skin.hpp>
6 #include <QGraphicsItem>
7 #include <QGraphicsSceneMouseEvent>
11 #include <avnd/concepts/painter.hpp>
27 void begin_path() { path = QPainterPath{}; }
28 void close_path() { path.closeSubpath(); }
29 void stroke() { painter.strokePath(path, painter.pen()); }
30 void fill() { painter.fillPath(path, painter.brush()); }
31 void update() { item.update(); }
33 void move_to(
double x,
double y) { path.moveTo(x, y); }
34 void line_to(
double x,
double y) { path.lineTo(x, y); }
35 void arc_to(
double x,
double y,
double w,
double h,
double start,
double length)
37 path.arcTo(x, y, w, h, start, length);
40 void cubic_to(
double c1x,
double c1y,
double c2x,
double c2y,
double endx,
double endy)
42 path.cubicTo(c1x, c1y, c2x, c2y, endx, endy);
45 void quad_to(
double x1,
double y1,
double x2,
double y2)
47 path.quadTo(x1, y1, x2, y2);
50 void translate(
double x,
double y) { painter.translate(x, y); }
51 void scale(
double x,
double y) { painter.scale(x, y); }
52 void rotate(
double a) { painter.rotate(a); }
53 void reset_transform() { painter.resetTransform(); }
58 QPen p = painter.pen();
59 p.setColor(qRgba(c.r, c.g, c.b, c.a));
63 void set_stroke_width(
double w)
65 QPen p = painter.pen();
72 painter.setBrush(QColor(qRgba(c.r, c.g, c.b, c.a)));
75 void set_linear_gradient(
78 QLinearGradient gradient(QPointF(x1, y1), QPointF(x2, y2));
79 gradient.setColorAt(0, QColor(qRgba(c1.r, c1.g, c1.b, c1.a)));
80 gradient.setColorAt(1, QColor(qRgba(c2.r, c2.g, c2.b, c2.a)));
81 painter.setBrush(gradient);
86 QRadialGradient gradient(cx, cy, cr);
87 gradient.setColorAt(0, QColor(qRgba(c1.r, c1.g, c1.b, c1.a)));
88 gradient.setColorAt(1, QColor(qRgba(c2.r, c2.g, c2.b, c2.a)));
89 painter.setBrush(gradient);
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_RGBA8888);
182 auto prev = painter.renderHints() & QPainter::SmoothPixmapTransform;
183 painter.setRenderHint(QPainter::SmoothPixmapTransform,
false);
185 QRect(x, y, w, h), img, QRect(0, 0, img_w, img_h), Qt::ImageConversionFlags{});
187 painter.setRenderHint(QPainter::SmoothPixmapTransform, prev);
190 static_assert(avnd::painter<QPainterAdapter>);
192 template <
typename Item,
typename Control =
void>
197 using item_type = std::decay_t<Item>;
206 this->setFlag(QGraphicsItem::ItemClipsToShape);
207 this->setFlag(QGraphicsItem::ItemClipsChildrenToShape);
209 if constexpr(requires { this->impl.update = [
this] {}; })
211 this->impl.update = [
this] { this->update(); };
215 QRectF boundingRect()
const override
217 return {0., 0., item_type::width(), item_type::height()};
220 void paint(QPainter* painter,
const QStyleOptionGraphicsItem* option, QWidget* widget)
223 auto& skin = score::Skin::instance();
224 painter->setRenderHint(QPainter::Antialiasing,
true);
225 painter->setPen(skin.Dark.main.pen1);
227 painter->setRenderHint(QPainter::Antialiasing,
false);
239 friend button& operator|=(button& lhs, button rhs) noexcept
242 enum button&)(
reinterpret_cast<std::underlying_type_t<enum button>&
>(lhs) |=
reinterpret_cast<std::underlying_type_t<enum button>&
>(rhs));
252 friend modifier& operator|=(modifier& lhs, modifier rhs) noexcept
255 enum modifier&)(
reinterpret_cast<std::underlying_type_t<enum modifier>&
>(lhs) |=
reinterpret_cast<std::underlying_type_t<enum modifier>&
>(rhs));
260 enum button button = {};
261 enum button held_buttons = {};
262 enum modifier modifiers = {};
270 p.x =
event->pos().x();
271 p.y =
event->pos().y();
273 if(event->button() == Qt::LeftButton)
274 p.button = custom_mouse_event::left;
275 else if(event->button() == Qt::RightButton)
276 p.button = custom_mouse_event::right;
277 else if(event->button() == Qt::MiddleButton)
278 p.button = custom_mouse_event::middle;
280 if(event->buttons() & Qt::LeftButton)
281 p.held_buttons |= p.left;
282 if(event->buttons() & Qt::RightButton)
283 p.held_buttons |= p.right;
284 if(event->buttons() & Qt::MiddleButton)
285 p.held_buttons |= p.middle;
287 if(event->modifiers() & Qt::ShiftModifier)
288 p.modifiers |= p.shift;
289 if(event->modifiers() & Qt::AltModifier)
290 p.modifiers |= p.alt;
291 if(event->modifiers() & Qt::ControlModifier)
292 p.modifiers |= p.ctrl;
293 if(event->modifiers() & Qt::MetaModifier)
294 p.modifiers |= p.meta;
299 void mousePressEvent(QGraphicsSceneMouseEvent* event)
override
301 if constexpr(requires { impl.mouse_press(0, 0); })
303 if(impl.mouse_press(event->pos().x(), event->pos().y()))
306 else if constexpr(requires { impl.mouse_press(custom_mouse_event{}); })
308 if(impl.mouse_press(make_event(event)))
314 void mouseMoveEvent(QGraphicsSceneMouseEvent* event)
override
316 if constexpr(requires { impl.mouse_move(0, 0); })
318 impl.mouse_move(event->pos().x(), event->pos().y());
321 else if constexpr(requires { impl.mouse_move(custom_mouse_event{}); })
323 impl.mouse_move(make_event(event));
329 void mouseReleaseEvent(QGraphicsSceneMouseEvent* event)
override
331 if constexpr(requires { impl.mouse_release(0, 0); })
333 impl.mouse_release(event->pos().x(), event->pos().y());
336 else if constexpr(requires { impl.mouse_release(custom_mouse_event{}); })
338 impl.mouse_release(make_event(event));
348 template <
typename Item>
356 , cmd{ctx.commandStack}
358 if constexpr(requires { this->impl.transaction; })
360 this->impl.transaction.start = [] {
363 this->impl.transaction.update = [
this, &ctl](
const auto& value) {
364 auto val = oscr::to_ossia_value(value);
367 this->impl.value = value;
370 this->impl.transaction.commit = [
this] { cmd.
commit(); };
371 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: SetControlValue.hpp:13
Definition: Painter.hpp:350
Definition: Painter.hpp:194
Definition: Factories.hpp:19
Definition: Painter.hpp:231
Definition: Painter.hpp:22
Definition: Painter.hpp:18
Definition: DocumentContext.hpp:18