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