98 lines
3.0 KiB
C++

#include "NameCreater.h"
#include <QtWidgets/QApplication>
#include "QGraphicsScene"
#include "QGraphicsView"
#include "QGraphicsPixmapItem"
#include "QGraphicsTextItem"
#define BG_PATH ":/NameCreater/resource/background.jpg"
#define FONT_NAME QStringLiteral("µØ±d¶êÅé Std W8")
#define FONT_SIZE 76
static QPointF BOY_POS[] = {
QPointF(235, 400), QPointF(366, 1420),
QPointF(555, 400), QPointF(692, 1420),
QPointF(878, 400), QPointF(1014, 1420),
QPointF(1200, 400), QPointF(1337, 1420),
QPointF(1527, 400), QPointF(1663, 1420),
};
static int BOY_POS_COUNT = sizeof(BOY_POS) / sizeof(BOY_POS[0]);
static QPointF GIRL_POS[] = {
QPointF(1854, 400), QPointF(1987, 1420),
QPointF(2176, 400), QPointF(2314, 1420),
QPointF(2498, 400), QPointF(2635, 1420),
QPointF(2821, 400), QPointF(2958, 1420),
QPointF(3147, 400), QPointF(3285, 1420),
};
static int GIRL_POS_COUNT = sizeof(GIRL_POS) / sizeof(GIRL_POS[0]);
void saveToImage(const QString& filename , QGraphicsScene *scene) {
scene->clearSelection(); // Selections would also render to the file
scene->setSceneRect(scene->itemsBoundingRect()); // Re-shrink the scene to it's bounding contents
QImage image(scene->sceneRect().size().toSize(), QImage::Format_ARGB32); // Create the image with the exact size of the shrunk scene
image.fill(Qt::transparent); // Start all pixels transparent
int dpm = 300 / 0.0254; // ~300 DPI
image.setDotsPerMeterX(dpm);
image.setDotsPerMeterY(dpm);
QPainter painter(&image);
scene->render(&painter);
image.save(filename);
}
int main(int argc, char *argv[])
{
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication a(argc, argv);
QFont font(FONT_NAME, FONT_SIZE);
QColor font_color(0, 0, 0);
QGraphicsScene scene;
QGraphicsView view(&scene);
QImage image(BG_PATH);
QGraphicsPixmapItem* bgItem = new QGraphicsPixmapItem(QPixmap::fromImage(image));
scene.addItem(bgItem);
for (int i = 0; i < BOY_POS_COUNT; i++) {
QGraphicsTextItem* textItem = new QGraphicsTextItem(QStringLiteral("Ĭ\n´é\n¶£"));
textItem->setFont(font);
textItem->setDefaultTextColor(font_color);
textItem->setPos(BOY_POS[i]);
int xscale, yscale;
xscale = yscale = (i % 2 == 1) ? -1 : 1;
textItem->setTransform(QTransform::fromScale(xscale, yscale));
scene.addItem(textItem);
}
for (int i = 0; i < GIRL_POS_COUNT; i++) {
QGraphicsTextItem* textItem = new QGraphicsTextItem(QStringLiteral("Ĭ\n¤_\n®¦"));
textItem->setFont(font);
textItem->setDefaultTextColor(font_color);
textItem->setPos(GIRL_POS[i]);
int xscale, yscale;
xscale = yscale = (i % 2 == 1) ? -1 : 1;
textItem->setTransform(QTransform::fromScale(xscale, yscale));
scene.addItem(textItem);
}
view.show();
saveToImage("test.jpg",&scene);
return a.exec();
}