1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
#if defined(Hiro_Canvas)
namespace hiro {
auto pCanvas::construct() -> void {
qtWidget = qtCanvas = new QtCanvas(*this);
qtCanvas->setMouseTracking(true);
pWidget::construct();
_rasterize();
qtCanvas->update();
}
auto pCanvas::destruct() -> void {
_release();
delete qtCanvas;
qtWidget = qtCanvas = nullptr;
}
auto pCanvas::minimumSize() const -> Size {
if(auto& icon = state().icon) return {icon.width(), icon.height()};
return {0, 0};
}
auto pCanvas::setAlignment(Alignment) -> void {
update();
}
auto pCanvas::setColor(Color color) -> void {
update();
}
auto pCanvas::setDroppable(bool droppable) -> void {
qtCanvas->setAcceptDrops(droppable);
}
auto pCanvas::setFocusable(bool focusable) -> void {
//TODO
}
auto pCanvas::setGeometry(Geometry geometry) -> void {
update();
pWidget::setGeometry(geometry);
}
auto pCanvas::setGradient(Gradient gradient) -> void {
update();
}
auto pCanvas::setIcon(const image& icon) -> void {
update();
}
auto pCanvas::update() -> void {
_rasterize();
qtCanvas->update();
}
auto pCanvas::_rasterize() -> void {
s32 width = 0;
s32 height = 0;
if(auto& icon = state().icon) {
width = icon.width();
height = icon.height();
} else {
width = pSizable::state().geometry.width();
height = pSizable::state().geometry.height();
}
if(width <= 0 || height <= 0) return;
if(width != qtImageWidth || height != qtImageHeight) _release();
qtImageWidth = width;
qtImageHeight = height;
if(!qtImage) qtImage = new QImage(width, height, QImage::Format_ARGB32);
auto buffer = (u32*)qtImage->bits();
if(auto& icon = state().icon) {
memory::copy<u32>(buffer, state().icon.data(), width * height);
} else if(auto& gradient = state().gradient) {
auto& colors = gradient.state.colors;
image fill;
fill.allocate(width, height);
fill.gradient(colors[0].value(), colors[1].value(), colors[2].value(), colors[3].value());
memory::copy(buffer, fill.data(), fill.size());
} else {
u32 color = state().color.value();
for(auto n : range(width * height)) buffer[n] = color;
}
}
auto pCanvas::_release() -> void {
if(qtImage) {
delete qtImage;
qtImage = nullptr;
}
qtImageWidth = 0;
qtImageHeight = 0;
}
auto QtCanvas::dragEnterEvent(QDragEnterEvent* event) -> void {
if(event->mimeData()->hasUrls()) {
event->acceptProposedAction();
}
}
auto QtCanvas::dropEvent(QDropEvent* event) -> void {
if(auto paths = DropPaths(event)) p.self().doDrop(paths);
}
auto QtCanvas::leaveEvent(QEvent* event) -> void {
p.self().doMouseLeave();
}
auto QtCanvas::mouseMoveEvent(QMouseEvent* event) -> void {
p.self().doMouseMove({event->pos().x(), event->pos().y()});
}
auto QtCanvas::mousePressEvent(QMouseEvent* event) -> void {
switch(event->button()) {
case Qt::LeftButton: p.self().doMousePress(Mouse::Button::Left); break;
case Qt::MidButton: p.self().doMousePress(Mouse::Button::Middle); break;
case Qt::RightButton: p.self().doMousePress(Mouse::Button::Right); break;
}
}
auto QtCanvas::mouseReleaseEvent(QMouseEvent* event) -> void {
switch(event->button()) {
case Qt::LeftButton: p.self().doMouseRelease(Mouse::Button::Left); break;
case Qt::MidButton: p.self().doMouseRelease(Mouse::Button::Middle); break;
case Qt::RightButton: p.self().doMouseRelease(Mouse::Button::Right); break;
}
}
auto QtCanvas::paintEvent(QPaintEvent* event) -> void {
if(!p.qtImage) return;
s32 sx = 0, sy = 0, dx = 0, dy = 0;
s32 width = p.qtImageWidth;
s32 height = p.qtImageHeight;
auto geometry = p.pSizable::state().geometry;
auto alignment = p.state().alignment ? p.state().alignment : Alignment{0.5, 0.5};
if(width <= geometry.width()) {
sx = 0;
dx = (geometry.width() - width) * alignment.horizontal();
} else {
sx = (width - geometry.width()) * alignment.horizontal();
dx = 0;
width = geometry.width();
}
if(height <= geometry.height()) {
sy = 0;
dy = (geometry.height() - height) * alignment.vertical();
} else {
sy = (height - geometry.height()) * alignment.vertical();
dy = 0;
height = geometry.height();
}
QPainter painter(p.qtCanvas);
painter.drawImage(dx, dy, *p.qtImage, sx, sy, width, height);
}
}
#endif
|