2022-08-25 18:37:24 +08:00
|
|
|
#include "ephoto.h"
|
|
|
|
#include "cfg.h"
|
|
|
|
#include "tools.h"
|
2023-10-23 14:58:29 +08:00
|
|
|
#include "globaldefine.h"
|
2022-08-25 18:37:24 +08:00
|
|
|
#include <QComboBox>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QImageReader>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QSpinBox>
|
|
|
|
|
|
|
|
EPhoto *EPhoto::create(const QString &file, PageListItem *pageItem, EBase *multiWin) {
|
|
|
|
QImageReader reader(file);
|
|
|
|
QImage img = reader.read();
|
|
|
|
if(img.isNull()) {
|
2023-05-11 11:47:00 +08:00
|
|
|
QMessageBox::critical(pageItem->listWidget(), "Image Error", Tools::readErrStr(reader.error())+": "+reader.errorString()+"\n"+file);
|
|
|
|
return 0;
|
2022-08-25 18:37:24 +08:00
|
|
|
}
|
|
|
|
QFileInfo info(file);
|
|
|
|
return new EPhoto(img, info.absolutePath(), info.fileName(), pageItem, multiWin);
|
|
|
|
}
|
2023-10-23 14:58:29 +08:00
|
|
|
EPhoto *EPhoto::create(const JObj &json, PageListItem *pageItem, EBase *multiWin) {
|
2022-08-25 18:37:24 +08:00
|
|
|
auto widget = json["widget"];
|
|
|
|
auto dir = widget["path"].toString();
|
|
|
|
auto name = widget["file"].toString();
|
|
|
|
if(! QFileInfo(dir).isDir()) dir = pageItem->mPageDir;
|
|
|
|
QString file = dir + "/" + name;
|
|
|
|
QFileInfo fileInfo(file);
|
|
|
|
if(! fileInfo.isFile()) {
|
|
|
|
QString file2 = dir + "/" + widget["yuanshi_file"].toString();
|
|
|
|
if(QFileInfo(file2).isFile()) QFile::rename(file2, file);
|
|
|
|
else if(QFileInfo(file2 = widget["computer_pic_file"].toString()).isFile()) QFile::copy(file2, file);
|
|
|
|
else if(QFileInfo(file2 = dir + "/card_"+name).isFile()) QFile::rename(file2, file);
|
|
|
|
else return nullptr;
|
|
|
|
}
|
|
|
|
auto img = QImage(file);
|
|
|
|
if(img.isNull()) return nullptr;
|
|
|
|
auto ins = new EPhoto(img, dir, name, pageItem, multiWin);
|
|
|
|
ins->setBaseAttr(json);
|
|
|
|
auto play = json["play"];
|
|
|
|
ins->mDuration = play["playDuration"].toInt();
|
|
|
|
ins->mEnterStyle = play["enterStyle"].toInt();
|
|
|
|
ins->mEnterDuration = play["enterDuration"].toInt();
|
|
|
|
return ins;
|
|
|
|
}
|
|
|
|
|
|
|
|
EPhoto::EPhoto(const QImage &img, const QString &dir, const QString &name, PageListItem *pageItem, EBase *multiWin) : EBase(multiWin), img(img), mDir(dir), mName(name), mPageItem(pageItem) {
|
|
|
|
mType = EBase::Photo;
|
|
|
|
scaleImgIfNeed();
|
|
|
|
}
|
2023-10-23 14:58:29 +08:00
|
|
|
JObj EPhoto::attrJson() const {
|
|
|
|
JObj oRoot;
|
2022-08-25 18:37:24 +08:00
|
|
|
addBaseAttr(oRoot);
|
|
|
|
oRoot["elementType"] = "Photo";
|
2023-10-23 14:58:29 +08:00
|
|
|
oRoot["widget"] = JObj{
|
2022-08-25 18:37:24 +08:00
|
|
|
{"path", mDir},
|
|
|
|
{"file", mName}
|
|
|
|
};
|
2023-10-23 14:58:29 +08:00
|
|
|
oRoot["play"] = JObj{
|
2022-08-25 18:37:24 +08:00
|
|
|
{"playDuration", mDuration},
|
|
|
|
{"playTimes", 1},
|
|
|
|
{"enterStyle", mEnterStyle},
|
|
|
|
{"enterDuration", mEnterDuration}
|
|
|
|
};
|
|
|
|
return oRoot;
|
|
|
|
}
|
|
|
|
void EPhoto::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
|
|
|
|
painter->drawImage(innerRect(), img);
|
|
|
|
EBase::paint(painter, option, widget);
|
|
|
|
}
|
|
|
|
void EPhoto::freeFiles() {
|
|
|
|
if(img.isNull()) return;
|
|
|
|
img = QImage();
|
|
|
|
}
|
|
|
|
void EPhoto::loadFiles() {
|
|
|
|
if(! img.isNull()) return;
|
2022-10-27 15:07:45 +08:00
|
|
|
if(! QFileInfo::exists(mDir + "/" + mName)) return;
|
|
|
|
img = QImage(mDir + "/" + mName);
|
2022-08-25 18:37:24 +08:00
|
|
|
}
|
|
|
|
bool EPhoto::save(const QString &pageDir) {
|
|
|
|
QString newName = mName;
|
|
|
|
QString newFile = pageDir + "/" + newName;
|
|
|
|
if(QFileInfo::exists(newFile)) {
|
|
|
|
newFile = Tools::addSufix(newFile);
|
|
|
|
newName = QFileInfo(newFile).fileName();
|
|
|
|
}
|
|
|
|
QString oldFile = mDir + PAGEDEL_SUFFIX + "/" + mName;
|
|
|
|
if(QFileInfo::exists(oldFile)) QFile(oldFile).copy(newFile);
|
|
|
|
else if(pageDir!=mDir && QFileInfo::exists(oldFile = mDir + "/" + mName)) QFile(oldFile).copy(newFile);
|
|
|
|
else if(QFileInfo::exists(pageDir + "/" + mName)) newName = mName;
|
|
|
|
mDir = pageDir;
|
|
|
|
mName = newName;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget* EPhoto::attrWgt() {
|
|
|
|
auto wgtAttr = new QWidget();
|
|
|
|
auto vBox = new QVBoxLayout(wgtAttr);
|
|
|
|
vBox->setContentsMargins(6, 0, 6, 0);
|
2023-08-01 11:42:41 +08:00
|
|
|
if(mMultiWin) vBox->setSpacing(3);
|
2022-08-25 18:37:24 +08:00
|
|
|
|
|
|
|
addBaseAttrWgt(vBox);
|
|
|
|
|
|
|
|
auto hBox = new QHBoxLayout();
|
|
|
|
hBox->addWidget(new QLabel(tr("Basic Properties")));
|
|
|
|
|
|
|
|
auto line = new QFrame();
|
|
|
|
line->setFrameShape(QFrame::HLine);
|
|
|
|
line->setFrameShadow(QFrame::Sunken);
|
|
|
|
hBox->addWidget(line, 1);
|
|
|
|
|
|
|
|
vBox->addLayout(hBox);
|
|
|
|
|
|
|
|
hBox = new QHBoxLayout();
|
|
|
|
hBox->addSpacing(6);
|
|
|
|
hBox->addWidget(new QLabel(tr("File")));
|
|
|
|
|
|
|
|
auto fdFile = new QLineEdit(mName);
|
|
|
|
fdFile->setReadOnly(true);
|
|
|
|
hBox->addWidget(fdFile);
|
|
|
|
|
|
|
|
auto bnSelectFile = new QPushButton("...");
|
|
|
|
bnSelectFile->setFixedWidth(30);
|
|
|
|
bnSelectFile->setObjectName("bnSelectFile");
|
2023-05-11 11:47:00 +08:00
|
|
|
connect(bnSelectFile, &QPushButton::clicked, wgtAttr, [=] {
|
2022-10-27 15:07:45 +08:00
|
|
|
QString home = mDir.startsWith(gProgItem->mProgDir) ? gFileHome : mDir;
|
2023-05-11 11:47:00 +08:00
|
|
|
QString file = QFileDialog::getOpenFileName(wgtAttr, tr("Select File"), home, EPhoto::filters());
|
2022-09-13 23:16:36 +08:00
|
|
|
if(file.isEmpty()) return;
|
2022-08-25 18:37:24 +08:00
|
|
|
QImageReader reader(file);
|
|
|
|
QImage aimg = reader.read();
|
|
|
|
if(aimg.isNull()) {
|
2023-05-11 11:47:00 +08:00
|
|
|
QMessageBox::critical(wgtAttr, tr("Image Read Error"), Tools::readErrStr(reader.error())+": "+reader.errorString()+"\n"+file);
|
2022-08-25 18:37:24 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
img = aimg;
|
|
|
|
QFileInfo info(file);
|
|
|
|
mDir = info.absolutePath();
|
2022-09-13 23:16:36 +08:00
|
|
|
gFileHome = mDir;
|
2022-08-25 18:37:24 +08:00
|
|
|
mName = info.fileName();
|
|
|
|
scaleImgIfNeed();
|
|
|
|
fdFile->setText(mName);
|
|
|
|
});
|
|
|
|
hBox->addWidget(bnSelectFile);
|
|
|
|
|
|
|
|
vBox->addLayout(hBox);
|
|
|
|
|
|
|
|
hBox = new QHBoxLayout();
|
|
|
|
hBox->addWidget(new QLabel(tr("Play Properties")));
|
|
|
|
|
|
|
|
line = new QFrame();
|
|
|
|
line->setFrameShape(QFrame::HLine);
|
|
|
|
line->setFrameShadow(QFrame::Sunken);
|
|
|
|
hBox->addWidget(line, 1);
|
|
|
|
|
|
|
|
vBox->addLayout(hBox);
|
|
|
|
|
|
|
|
hBox = new QHBoxLayout();
|
|
|
|
hBox->addSpacing(6);
|
|
|
|
hBox->addWidget(new QLabel(tr("Play Duration")));
|
|
|
|
|
|
|
|
auto fdDuration = new QSpinBox();
|
|
|
|
fdDuration->setRange(1, 99999);
|
|
|
|
fdDuration->setValue(mDuration);
|
|
|
|
hBox->addWidget(fdDuration);
|
|
|
|
|
|
|
|
hBox->addWidget(new QLabel(tr("s")));
|
|
|
|
hBox->addStretch();
|
|
|
|
|
|
|
|
vBox->addLayout(hBox);
|
|
|
|
|
|
|
|
hBox = new QHBoxLayout();
|
|
|
|
hBox->addSpacing(6);
|
|
|
|
hBox->addWidget(new QLabel(tr("Enter Style")));
|
|
|
|
|
|
|
|
auto wEnterStyle = new QComboBox();
|
|
|
|
wEnterStyle->addItem(tr("None"));
|
2023-10-23 14:58:29 +08:00
|
|
|
wEnterStyle->addItem(tr("Alpha In"));
|
2022-08-25 18:37:24 +08:00
|
|
|
wEnterStyle->addItem(tr("Moving to left"));
|
|
|
|
wEnterStyle->addItem(tr("Moving to right"));
|
|
|
|
wEnterStyle->addItem(tr("Moving to top"));
|
|
|
|
wEnterStyle->addItem(tr("Move to bottom"));
|
2023-10-23 14:58:29 +08:00
|
|
|
wEnterStyle->addItem(tr("Zoom In"));
|
|
|
|
wEnterStyle->addItem(tr("Zoom In to left_bottom"));
|
|
|
|
wEnterStyle->addItem(tr("Zoom In to left_top"));
|
|
|
|
wEnterStyle->addItem(tr("Zoom In to right_top"));
|
|
|
|
wEnterStyle->addItem(tr("Zoom In to right bottom"));
|
2022-08-25 18:37:24 +08:00
|
|
|
wEnterStyle->addItem(tr("Rotate to right"));
|
|
|
|
wEnterStyle->addItem(tr("Rotate to left"));
|
|
|
|
wEnterStyle->setCurrentIndex(mEnterStyle);
|
|
|
|
connect(wEnterStyle, (void(QComboBox::*)(int))&QComboBox::currentIndexChanged, this, [this](int value) {
|
|
|
|
mEnterStyle = value;
|
|
|
|
});
|
|
|
|
hBox->addWidget(wEnterStyle);
|
|
|
|
hBox->addStretch();
|
|
|
|
|
|
|
|
vBox->addLayout(hBox);
|
|
|
|
|
|
|
|
hBox = new QHBoxLayout();
|
|
|
|
hBox->addSpacing(6);
|
|
|
|
hBox->addWidget(new QLabel(tr("Enter Duration")));
|
|
|
|
|
|
|
|
auto fdEnterDuration = new QSpinBox();
|
|
|
|
fdEnterDuration->setRange(1, 99999);
|
|
|
|
fdEnterDuration->setValue(mEnterDuration);
|
|
|
|
connect(fdDuration, (void(QSpinBox::*)(int))&QSpinBox::valueChanged, this, [this, fdEnterDuration](int value){
|
|
|
|
mDuration = value;
|
|
|
|
if(mEnterDuration > value) {
|
|
|
|
mEnterDuration = value;
|
|
|
|
fdEnterDuration->setValue(value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
connect(fdEnterDuration, (void(QSpinBox::*)(int))&QSpinBox::valueChanged, this, [this, fdDuration](int value){
|
|
|
|
mEnterDuration = value;
|
|
|
|
if(mDuration < value) {
|
|
|
|
mDuration = value;
|
|
|
|
fdDuration->setValue(value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
hBox->addWidget(fdEnterDuration);
|
|
|
|
|
|
|
|
hBox->addWidget(new QLabel(tr("s")));
|
|
|
|
hBox->addStretch();
|
|
|
|
|
|
|
|
vBox->addLayout(hBox);
|
|
|
|
vBox->addStretch();
|
|
|
|
return wgtAttr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EPhoto::scaleImgIfNeed() {
|
|
|
|
if(img.width() > 4096 || img.height() > 4096) {
|
|
|
|
if(img.width() > img.height()) img = img.scaledToWidth(3840, Qt::SmoothTransformation);
|
|
|
|
else img = img.scaledToHeight(3840, Qt::SmoothTransformation);
|
|
|
|
if(mName.endsWith(".jpg", Qt::CaseInsensitive)) mName = mName.chopped(3)+"png";
|
|
|
|
else if(mName.endsWith(".jpeg", Qt::CaseInsensitive)) mName = mName.chopped(4)+"png";
|
|
|
|
mDir = mPageItem->mPageDir;
|
|
|
|
QString save = mDir + "/" + mName;
|
|
|
|
if(QFileInfo::exists(save)) {
|
|
|
|
save = Tools::addSufix(save);
|
|
|
|
mName = QFileInfo(save).fileName();
|
|
|
|
}
|
|
|
|
img.save(save);
|
|
|
|
}
|
|
|
|
}
|