File: ImageLoaderModel.cpp

package info (click to toggle)
qnodeeditor 2.1.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 2,144 kB
  • sloc: cpp: 8,823; makefile: 3
file content (56 lines) | stat: -rw-r--r-- 1,709 bytes parent folder | download
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
#include "ImageLoaderModel.hpp"

#include <QtCore/QDir>
#include <QtCore/QEvent>
#include <QtWidgets/QFileDialog>
ImageLoaderModel::ImageLoaderModel() : _label(new QLabel("Double click to load image"))
{
    _label->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
    QFont f = _label->font();
    f.setBold(true);
    f.setItalic(true);
    _label->setFont(f);
    _label->setFixedSize(200, 200);
    _label->installEventFilter(this);
}
unsigned int ImageLoaderModel::nPorts(PortType portType) const
{
    unsigned int result = 1;
    switch (portType)
    {
        case PortType::In: result = 0; break;
        case PortType::Out: result = 1;
        default: break;
    }
    return result;
}
bool ImageLoaderModel::eventFilter(QObject *object, QEvent *event)
{
    if (object == _label)
    {
        int w = _label->width();
        int h = _label->height();
        if (event->type() == QEvent::MouseButtonPress)
        {
            QString fileName = QFileDialog::getOpenFileName(nullptr, tr("Open Image"), QDir::homePath(), tr("Image Files (*.png *.jpg *.bmp)"));
            _pixmap = QPixmap(fileName);
            _label->setPixmap(_pixmap.scaled(w, h, Qt::KeepAspectRatio));
            Q_EMIT dataUpdated(0);
            return true;
        }
        else if (event->type() == QEvent::Resize)
        {
            if (!_pixmap.isNull())
                _label->setPixmap(_pixmap.scaled(w, h, Qt::KeepAspectRatio));
        }
    }
    return false;
}
std::shared_ptr<NodeDataType> ImageLoaderModel::dataType(PortType, PortIndex) const
{
    return PixmapData().type();
}
std::shared_ptr<NodeData> ImageLoaderModel::outData(PortIndex)
{
    return std::make_shared<PixmapData>(_pixmap);
}