| 1 |
namespace phoenix { |
| 2 |
|
| 3 |
void pCanvas::setDroppable(bool droppable) { |
| 4 |
qtCanvas->setAcceptDrops(droppable); |
| 5 |
} |
| 6 |
|
| 7 |
void pCanvas::setGeometry(Geometry geometry) { |
| 8 |
if(canvas.state.width == 0 || canvas.state.height == 0) rasterize(); |
| 9 |
unsigned width = canvas.state.width; |
| 10 |
unsigned height = canvas.state.height; |
| 11 |
if(width == 0) width = widget.state.geometry.width; |
| 12 |
if(height == 0) height = widget.state.geometry.height; |
| 13 |
|
| 14 |
if(width < geometry.width) { |
| 15 |
geometry.x += (geometry.width - width) / 2; |
| 16 |
geometry.width = width; |
| 17 |
} |
| 18 |
|
| 19 |
if(height < geometry.height) { |
| 20 |
geometry.y += (geometry.height - height) / 2; |
| 21 |
geometry.height = height; |
| 22 |
} |
| 23 |
|
| 24 |
pWidget::setGeometry(geometry); |
| 25 |
} |
| 26 |
|
| 27 |
void pCanvas::setMode(Canvas::Mode mode) { |
| 28 |
rasterize(); |
| 29 |
qtCanvas->update(); |
| 30 |
} |
| 31 |
|
| 32 |
void pCanvas::setSize(Size size) { |
| 33 |
rasterize(); |
| 34 |
qtCanvas->update(); |
| 35 |
} |
| 36 |
|
| 37 |
void pCanvas::constructor() { |
| 38 |
qtWidget = qtCanvas = new QtCanvas(*this); |
| 39 |
qtCanvas->setMouseTracking(true); |
| 40 |
|
| 41 |
pWidget::synchronizeState(); |
| 42 |
rasterize(), qtCanvas->update(); |
| 43 |
} |
| 44 |
|
| 45 |
void pCanvas::destructor() { |
| 46 |
release(); |
| 47 |
delete qtCanvas; |
| 48 |
qtWidget = qtCanvas = nullptr; |
| 49 |
} |
| 50 |
|
| 51 |
void pCanvas::orphan() { |
| 52 |
destructor(); |
| 53 |
constructor(); |
| 54 |
} |
| 55 |
|
| 56 |
void pCanvas::rasterize() { |
| 57 |
unsigned width = canvas.state.width; |
| 58 |
unsigned height = canvas.state.height; |
| 59 |
if(width == 0) width = widget.state.geometry.width; |
| 60 |
if(height == 0) height = widget.state.geometry.height; |
| 61 |
|
| 62 |
if(canvas.state.mode != Canvas::Mode::Color) { |
| 63 |
if(width != surfaceWidth || height != surfaceHeight) release(); |
| 64 |
if(!surface) surface = new QImage(width, height, QImage::Format_ARGB32); |
| 65 |
} |
| 66 |
|
| 67 |
if(canvas.state.mode == Canvas::Mode::Gradient) { |
| 68 |
nall::image image; |
| 69 |
image.allocate(width, height); |
| 70 |
image.gradient( |
| 71 |
canvas.state.gradient[0].argb(), canvas.state.gradient[1].argb(), canvas.state.gradient[2].argb(), canvas.state.gradient[3].argb() |
| 72 |
); |
| 73 |
memcpy(surface->bits(), image.data, image.size); |
| 74 |
} |
| 75 |
|
| 76 |
if(canvas.state.mode == Canvas::Mode::Image) { |
| 77 |
nall::image image = canvas.state.image; |
| 78 |
image.scale(width, height); |
| 79 |
image.transform(0, 32, 255u << 24, 255u << 16, 255u << 8, 255u << 0); |
| 80 |
memcpy(surface->bits(), image.data, image.size); |
| 81 |
} |
| 82 |
|
| 83 |
if(canvas.state.mode == Canvas::Mode::Data) { |
| 84 |
if(width == canvas.state.width && height == canvas.state.height) { |
| 85 |
memcpy(surface->bits(), canvas.state.data, width * height * sizeof(uint32_t)); |
| 86 |
} else { |
| 87 |
memset(surface->bits(), 0x00, width * height * sizeof(uint32_t)); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
surfaceWidth = width; |
| 92 |
surfaceHeight = height; |
| 93 |
} |
| 94 |
|
| 95 |
void pCanvas::release() { |
| 96 |
if(surface == nullptr) return; |
| 97 |
delete surface; |
| 98 |
surface = nullptr; |
| 99 |
surfaceWidth = 0; |
| 100 |
surfaceHeight = 0; |
| 101 |
} |
| 102 |
|
| 103 |
void pCanvas::QtCanvas::dragEnterEvent(QDragEnterEvent* event) { |
| 104 |
if(event->mimeData()->hasUrls()) { |
| 105 |
event->acceptProposedAction(); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
void pCanvas::QtCanvas::dropEvent(QDropEvent* event) { |
| 110 |
lstring paths = DropPaths(event); |
| 111 |
if(paths.empty()) return; |
| 112 |
if(self.canvas.onDrop) self.canvas.onDrop(paths); |
| 113 |
} |
| 114 |
|
| 115 |
void pCanvas::QtCanvas::leaveEvent(QEvent* event) { |
| 116 |
if(self.canvas.onMouseLeave) self.canvas.onMouseLeave(); |
| 117 |
} |
| 118 |
|
| 119 |
void pCanvas::QtCanvas::mouseMoveEvent(QMouseEvent* event) { |
| 120 |
if(self.canvas.onMouseMove) self.canvas.onMouseMove({event->pos().x(), event->pos().y()}); |
| 121 |
} |
| 122 |
|
| 123 |
void pCanvas::QtCanvas::mousePressEvent(QMouseEvent* event) { |
| 124 |
if(!self.canvas.onMousePress) return; |
| 125 |
switch(event->button()) { |
| 126 |
case Qt::LeftButton: self.canvas.onMousePress(Mouse::Button::Left); break; |
| 127 |
case Qt::MidButton: self.canvas.onMousePress(Mouse::Button::Middle); break; |
| 128 |
case Qt::RightButton: self.canvas.onMousePress(Mouse::Button::Right); break; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
void pCanvas::QtCanvas::mouseReleaseEvent(QMouseEvent* event) { |
| 133 |
if(!self.canvas.onMouseRelease) return; |
| 134 |
switch(event->button()) { |
| 135 |
case Qt::LeftButton: self.canvas.onMouseRelease(Mouse::Button::Left); break; |
| 136 |
case Qt::MidButton: self.canvas.onMouseRelease(Mouse::Button::Middle); break; |
| 137 |
case Qt::RightButton: self.canvas.onMouseRelease(Mouse::Button::Right); break; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
void pCanvas::QtCanvas::paintEvent(QPaintEvent* event) { |
| 142 |
Canvas& canvas = self.canvas; |
| 143 |
QPainter painter(self.qtCanvas); |
| 144 |
|
| 145 |
if(canvas.state.mode == Canvas::Mode::Color) { |
| 146 |
painter.fillRect(event->rect(), QBrush(QColor(canvas.state.color.red, canvas.state.color.green, canvas.state.color.blue, canvas.state.color.alpha))); |
| 147 |
} else { |
| 148 |
painter.drawImage(0, 0, *self.surface); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
pCanvas::QtCanvas::QtCanvas(pCanvas& self) : self(self) { |
| 153 |
} |
| 154 |
|
| 155 |
} |