예제 #1
0
void MainWindow::UpdateCurrentItemIcon(const QImage &image) {
    QPixmap pixmap = QPixmap::fromImage(image);
    QPainter painter(&pixmap);

    static const QImage link_h(":/sockets/linkH.png");
    static const QImage link_v(":/sockets/linkV.png");
    ItemSocket prev = { 255, '-' };
    size_t i = 0;
    for (auto &socket : current_item_->text_sockets()) {
        bool link = socket.group == prev.group;
        QImage socket_image(":/sockets/" + QString(socket.attr) + ".png");
        if (current_item_->w() == 1) {
            painter.drawImage(0, PIXELS_PER_SLOT * i, socket_image);
            if (link)
                painter.drawImage(16, PIXELS_PER_SLOT * i - 19, link_v);
        } else /* w == 2 */ {
            int row = i / 2;
            int column = i % 2;
            if (row % 2 == 1)
                column = 1 - column;
            painter.drawImage(PIXELS_PER_SLOT * column, PIXELS_PER_SLOT * row, socket_image);
            if (link) {
                if (i == 1 || i == 3 || i == 5) {
                    // horizontal link
                    painter.drawImage(
                        PIXELS_PER_SLOT - LINKH_WIDTH / 2,
                        row * PIXELS_PER_SLOT + PIXELS_PER_SLOT / 2 - LINKH_HEIGHT / 2,
                        link_h
                    );
                } else if (i == 2) {
                    painter.drawImage(
                        PIXELS_PER_SLOT * 1.5 - LINKV_WIDTH / 2,
                        row * PIXELS_PER_SLOT - LINKV_HEIGHT / 2,
                        link_v
                    );
                } else if (i == 4) {
                    painter.drawImage(
                        PIXELS_PER_SLOT / 2 - LINKV_WIDTH / 2,
                        row * PIXELS_PER_SLOT - LINKV_HEIGHT / 2,
                        link_v
                    );
                } else {
                    QLOG_ERROR() << "No idea how to draw link for" << current_item_->PrettyName().c_str();
                }
            }
        }

        prev = socket;
        ++i;
    }

    ui->imageLabel->setPixmap(pixmap);
}
예제 #2
0
void GenerateItemIcon(const Item &item, const QImage &image, Ui::MainWindow *ui) {
    int height = item.h();
    int width = item.w();
    int socket_rows = 0;
    int socket_columns = 0;
    // this will ensure we have enough room to draw the slots
    QPixmap pixmap(width * PIXELS_PER_SLOT, height * PIXELS_PER_SLOT);
    pixmap.fill(Qt::transparent);
    QPainter painter(&pixmap);

    static const QImage link_h(":/sockets/linkH.png");
    static const QImage link_v(":/sockets/linkV.png");
    ItemSocket prev = { 255, '-' };
    size_t i = 0;

    auto &sockets = item.text_sockets();
    if (sockets.size() == 0) {
        // Do nothing
    } else if (sockets.size() == 1) {
        auto &socket = sockets.front();
        QImage socket_image(":/sockets/" + QString(socket.attr) + ".png");
        painter.drawImage(0, PIXELS_PER_SLOT * i, socket_image);
        socket_rows = 1;
        socket_columns = 1;
    } else {
        for (auto &socket : sockets) {
            bool link = socket.group == prev.group;
            QImage socket_image(":/sockets/" + QString(socket.attr) + ".png");
            if (width == 1) {
                painter.drawImage(0, PIXELS_PER_SLOT * i, socket_image);
                if (link)
                    painter.drawImage(16, PIXELS_PER_SLOT * i - 19, link_v);
                socket_columns = 1;
                socket_rows = i + 1;
            } else /* w == 2 */ {
                int row = i / 2;
                int column = i % 2;
                if (row % 2 == 1)
                    column = 1 - column;
                socket_columns = qMax(column + 1, socket_columns);
                socket_rows = qMax(row + 1, socket_rows);
                painter.drawImage(PIXELS_PER_SLOT * column, PIXELS_PER_SLOT * row, socket_image);
                if (link) {
                    if (i == 1 || i == 3 || i == 5) {
                        // horizontal link
                        painter.drawImage(
                            PIXELS_PER_SLOT - LINKH_WIDTH / 2,
                            row * PIXELS_PER_SLOT + PIXELS_PER_SLOT / 2 - LINKH_HEIGHT / 2,
                            link_h
                        );
                    } else if (i == 2) {
                        painter.drawImage(
                            PIXELS_PER_SLOT * 1.5 - LINKV_WIDTH / 2,
                            row * PIXELS_PER_SLOT - LINKV_HEIGHT / 2,
                            link_v
                        );
                    } else if (i == 4) {
                        painter.drawImage(
                            PIXELS_PER_SLOT / 2 - LINKV_WIDTH / 2,
                            row * PIXELS_PER_SLOT - LINKV_HEIGHT / 2,
                            link_v
                        );
                    } else {
                        QLOG_ERROR() << "No idea how to draw link for" << item.PrettyName().c_str();
                    }
                }
            }

            prev = socket;
            ++i;
        }
    }

    QPixmap cropped = pixmap.copy(0, 0, PIXELS_PER_SLOT * socket_columns,
                                        PIXELS_PER_SLOT * socket_rows);

    QPixmap base(image.width(), image.height());
    base.fill(Qt::transparent);
    QPainter overlay(&base);
    overlay.drawImage(0, 0, image);

    overlay.drawPixmap((int)(0.5*(image.width() - cropped.width())),
                       (int)(0.5*(image.height() - cropped.height())), cropped);

    ui->imageLabel->setPixmap(base);
}