Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

image_panel_cursor_info #1480

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 47 additions & 68 deletions ui/zenoedit/panel/zenoimagepanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,74 +18,6 @@
#include "viewport/displaywidget.h"


static float ziv_wheelZoomFactor = 1.25;

class ZenoImageView: public QGraphicsView {
public:
QGraphicsPixmapItem *_image = nullptr;
QGraphicsScene *scene = nullptr;
bool fitMode = true;
explicit ZenoImageView(QWidget *parent) : QGraphicsView(parent) {
scene = new QGraphicsScene;
this->setScene(scene);

setBackgroundBrush(QColor(37, 37, 37));
}

bool hasImage() {
return _image != nullptr;
}

void clearImage() {
if (hasImage()) {
scene->removeItem(_image);
_image = nullptr;
}
}

void setImage(const QImage &image) {
QPixmap pm = QPixmap::fromImage(image);
if (hasImage()) {
_image->setPixmap(pm);
}
else {
_image = this->scene->addPixmap(pm);
}
setSceneRect(QRectF(pm.rect())); // Set scene size to image size.
updateImageView();
}

void updateImageView() {
if (!hasImage()) {
return;
}
if (fitMode) {
fitInView(sceneRect(), Qt::AspectRatioMode::KeepAspectRatio);
}
}
void resizeEvent(QResizeEvent *event) override {
updateImageView();
}
void wheelEvent(QWheelEvent* event) override {
fitMode = false;
qreal zoomFactor = 1;
if (event->angleDelta().y() > 0)
zoomFactor = ziv_wheelZoomFactor;
else if (event->angleDelta().y() < 0)
zoomFactor = 1 / ziv_wheelZoomFactor;
scale(zoomFactor, zoomFactor);
}
void mousePressEvent(QMouseEvent* event) override {
fitMode = false;
setDragMode(QGraphicsView::ScrollHandDrag);
QGraphicsView::mousePressEvent(event);
}
void mouseReleaseEvent(QMouseEvent* event) override {
QGraphicsView::mouseReleaseEvent(event);
setDragMode(QGraphicsView::NoDrag);
}
};

void ZenoImagePanel::clear() {
if (image_view) {
image_view->clearImage();
Expand Down Expand Up @@ -284,5 +216,52 @@ ZenoImagePanel::ZenoImagePanel(QWidget *parent) : QWidget(parent) {
image_view->fitMode = true;
image_view->updateImageView();
});
connect(image_view, &ZenoImageView::pixelChanged, this, [=](float x, float y) {
std::string primid = pPrimName->text().toStdString();
zenovis::Scene* scene = nullptr;
auto mainWin = zenoApp->getMainWindow();
ZASSERT_EXIT(mainWin);
QVector<DisplayWidget*> wids = mainWin->viewports();
if (!wids.isEmpty())
{
auto session = wids[0]->getZenoVis()->getSession();
ZASSERT_EXIT(session);
scene = session->get_scene();
}
if (!scene)
return;
bool found = false;
for (auto const &[key, ptr]: scene->objectsMan->pairs()) {
if ((key.substr(0, key.find(":"))) != primid) {
continue;
}
auto &ud = ptr->userData();
if (ud.get2<int>("isImage", 0) == 0) {
continue;
}
found = true;
if (auto obj = dynamic_cast<zeno::PrimitiveObject *>(ptr)) {
int width = ud.get2<int>("w");
int height = ud.get2<int>("h");
int w = int(zeno::clamp(x, 0, width - 1));
int h = int(zeno::clamp(y, 0, height - 1));
int i = h * width + w;
auto c = obj->verts[i];
QString statusInfo = QString(zeno::format("width: {}, height: {} | ({}, {}) : ({}, {}, {})"
, width
, height
, w
, h
, c[0]
, c[1]
, c[2]
).c_str());
pStatusBar->setText(statusInfo);
}
}
if (found == false) {
clear();
}
});
}

80 changes: 79 additions & 1 deletion ui/zenoedit/panel/zenoimagepanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,85 @@
#define ZENO_ZENOIMAGEPANEL_H

#include <QtWidgets>
class ZenoImageView;

class ZenoImageView: public QGraphicsView {
Q_OBJECT
public:
float ziv_wheelZoomFactor = 1.25;
QGraphicsPixmapItem *_image = nullptr;
QGraphicsScene *scene = nullptr;
bool fitMode = true;
explicit ZenoImageView(QWidget *parent) : QGraphicsView(parent) {
scene = new QGraphicsScene;
this->setScene(scene);
setMouseTracking(true);

setBackgroundBrush(QColor(37, 37, 37));
}

bool hasImage() const {
return _image != nullptr;
}

void clearImage() {
if (hasImage()) {
scene->removeItem(_image);
_image = nullptr;
}
}

void setImage(const QImage &image) {
QPixmap pm = QPixmap::fromImage(image);
if (hasImage()) {
_image->setPixmap(pm);
}
else {
_image = this->scene->addPixmap(pm);
}
setSceneRect(QRectF(pm.rect())); // Set scene size to image size.
updateImageView();
}

void updateImageView() {
if (!hasImage()) {
return;
}
if (fitMode) {
fitInView(sceneRect(), Qt::AspectRatioMode::KeepAspectRatio);
}
}
signals:
void pixelChanged(float, float);
protected:
void resizeEvent(QResizeEvent *event) override {
updateImageView();
}
void wheelEvent(QWheelEvent* event) override {
fitMode = false;
qreal zoomFactor = 1;
if (event->angleDelta().y() > 0)
zoomFactor = ziv_wheelZoomFactor;
else if (event->angleDelta().y() < 0)
zoomFactor = 1 / ziv_wheelZoomFactor;
scale(zoomFactor, zoomFactor);
}
void mousePressEvent(QMouseEvent* event) override {
fitMode = false;
setDragMode(QGraphicsView::ScrollHandDrag);
QGraphicsView::mousePressEvent(event);
}
void mouseReleaseEvent(QMouseEvent* event) override {
QGraphicsView::mouseReleaseEvent(event);
setDragMode(QGraphicsView::NoDrag);
}
void mouseMoveEvent(QMouseEvent* event) override {
if(auto *item = qgraphicsitem_cast<QGraphicsPixmapItem *>(itemAt(event->pos()))){
QPointF p = item->mapFromScene(mapToScene(event->pos()));
emit(pixelChanged(p.x(), p.y()));
}
QGraphicsView::mouseMoveEvent(event);
}
};

class ZenoImagePanel : public QWidget {
Q_OBJECT
Expand Down
Loading