#include "ephoto.h" #include "main.h" #include #include #include #include #include #include EPhoto *EPhoto::create(const JObj &json, PageListItem *pageItem, EBase *multiWin) { auto widget = json["widget"]; auto name = (widget.isNull() ? json["name"] : widget["file"]).toString(); auto file = pageItem->mPageDir + "/" + name; QImageReader reader(file); reader.setAutoTransform(true); auto img = reader.read(); if(img.isNull()) return 0; auto ins = new EPhoto(reader, img, pageItem->mPageDir, name, json, multiWin); ins->setBaseAttr(json); return ins; } EPhoto::EPhoto(QImageReader &reader, const QImage &img, const QString &dir, const QString &name, const JObj &json, EBase *multiWin) : EBase(multiWin), mDir(dir), mName(name) { mType = EBase::Image; auto cnt = reader.imageCount(); if(cnt<=1) this->img = img; else { frames.push_back({QPixmap::fromImage(img), reader.nextImageDelay()}); while((int)frames.size() < cnt) { reader.jumpToNextImage(); frames.push_back({QPixmap::fromImage(reader.read()), reader.nextImageDelay()}); } } direct = json["direct"].toString(); speed = json["speed"].toInt(); tailSpacing = json["tailSpacing"].toInt(); } JObj EPhoto::attrJson() const { JObj json; addBaseAttr(json); json["elementType"] = frames.empty() ? "Image" : "Gif"; json["name"] = mName; json["direct"] = direct; json["speed"] = speed; json["tailSpacing"] = tailSpacing; return json; } void EPhoto::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { if(frames.empty()) painter->drawImage(innerRect(), img); else { painter->drawPixmap(innerRect(), frames[fi].img, QRectF()); if(timer==0) { auto delay = frames[fi].dur; if(delay) { timer = new SyncTimer(delay); connect(timer, &SyncTimer::timeout, this, &EPhoto::sltNext, Qt::BlockingQueuedConnection); timer->start(); } } } EBase::paint(painter, option, widget); } void EPhoto::sltNext() { if(isVisible() && ! frames.empty()) { if(++fi==frames.size()) fi = 0; timer->inter = frames[fi].dur; update(); } else if(timer) { timer->stop(); timer = 0; } } bool EPhoto::save(const QString &pageDir) { auto oldFile = mDir + PAGEDEL_SUFFIX + "/" + mName; auto newName = mName; auto newFile = pageDir + "/" + newName; if(QFileInfo::exists(newFile)) { newFile = AddSufix(newFile); newName = QFileInfo(newFile).fileName(); } 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; else return false; mName = newName; mDir = pageDir; return true; } QWidget* EPhoto::attrWgt() { auto wgtAttr = new QWidget; auto vBox = new VBox(wgtAttr); vBox->setContentsMargins(6, 0, 6, 0); if(mMultiWin) vBox->setSpacing(3); addBaseAttrWgt(vBox); auto hBox = new HBox(vBox); hBox->addLabel(translate("","Basic Properties")); auto line = new QFrame(); line->setFrameShape(QFrame::HLine); line->setFrameShadow(QFrame::Sunken); hBox->addWidget(line, 1); hBox = new HBox(vBox); 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"); auto wgtScroll = new QWidget; connect(bnSelectFile, &QPushButton::clicked, wgtAttr, [=] { auto home = mDir.startsWith(gProgItem->mProgDir) ? gFileHome : mDir; auto file = QFileDialog::getOpenFileName(wgtAttr, translate("","Select File"), home, EPhoto::filters()); if(file.isEmpty()) return; QImageReader reader(file); reader.setAutoTransform(true); auto aimg = reader.read(); if(aimg.isNull()) { QMessageBox::critical(wgtAttr, tr("Image Read Error"), ImgErrStr(reader.error())+": "+reader.errorString()+"\n"+file); return; } QFileInfo info(file); gFileHome = mDir = info.absolutePath(); fdFile->setText(mName = info.fileName()); frames.clear(); fi = 0; auto cnt = reader.imageCount(); if(cnt<=1) { img = aimg; wgtScroll->show(); } else { img = QImage(); frames.push_back({QPixmap::fromImage(img), reader.nextImageDelay()}); while((int)frames.size() < cnt) { reader.jumpToNextImage(); frames.push_back({QPixmap::fromImage(reader.read()), reader.nextImageDelay()}); } wgtScroll->hide(); } }); hBox->addWidget(bnSelectFile); vBox->addWidget(wgtScroll); wgtScroll->setVisible(frames.empty()); hBox = new HBox(wgtScroll); hBox->setContentsMargins(0,0,0,0); auto label = hBox->addLabel(tr("Scroll")+": "+tr("Direction")); label->setMinimumWidth(80); label->setAlignment(Qt::AlignRight|Qt::AlignVCenter); auto edDirect = new QComboBox; edDirect->addItem(tr("No"), ""); edDirect->addItem(tr("Right -> Left"), "left"); edDirect->addItem(tr("Bottom -> Top"), "top"); edDirect->addItem(tr("Left -> Right"), "right"); edDirect->addItem(tr("Top -> Bottom"), "bottom"); SetCurData(edDirect, direct); connect(edDirect, (void(QComboBox::*)(int))&QComboBox::currentIndexChanged, this, [=] { direct = edDirect->currentData().toString(); }); hBox->addWidget(edDirect); label = hBox->addLabel(tr("Speed")); label->setMinimumWidth(80); label->setAlignment(Qt::AlignRight|Qt::AlignVCenter); auto fd = new QComboBox; fd->setEditable(true); fd->setMinimumWidth(50); fd->addItem("600"); fd->addItem("540"); fd->addItem("480"); fd->addItem("420"); fd->addItem("360"); fd->addItem("300"); fd->addItem("240"); fd->addItem("180"); fd->addItem("120"); fd->addItem("60"); fd->addItem("30"); fd->setMaxVisibleItems(fd->count()); auto text = QString::number(speed); if(SetCurText(fd, text)==-1) { SetCurText(fd, "60"); fd->setCurrentText(text); } connect(fd, &QComboBox::editTextChanged, this, [=](const QString &text) { bool ok; auto speed = text.toInt(&ok); if(ok) this->speed = speed; }); hBox->addWidget(fd); hBox->addLabel("px/s"); hBox->addStretch(); // hBox = new HBox(vBox); // label = hBox->addLabel(tr("Tail Spacing")); // label->setMinimumWidth(80); // label->setAlignment(Qt::AlignRight|Qt::AlignVCenter); // auto edTailSpacing = new QSpinBox; // edTailSpacing->setRange(0, 9999); // edTailSpacing->setValue(tailSpacing); // connect(edTailSpacing, &QSpinBox::valueChanged, this, [this](int value) { // tailSpacing = value; // }); // hBox->addWidget(edTailSpacing); // hBox->addStretch(); // hBox = new HBox(vBox); // hBox->addWidget(new QLabel(tr("Play Properties"))); // line = new QFrame(); // line->setFrameShape(QFrame::HLine); // line->setFrameShadow(QFrame::Sunken); // hBox->addWidget(line, 1); // hBox = new HBox(vBox); // hBox->setSpacing(2); // hBox->addSpacing(6); // hBox->addWidget(new QLabel(tr("Entry Effect"))); // auto fdEntryEff = new QComboBox; // fdEntryEff->addItem(tr("None"), ""); // fdEntryEff->addItem(tr("Move to left"), "moving_left"); // fdEntryEff->addItem(tr("Move to top"), "moving_top"); // fdEntryEff->addItem(tr("Move to right"), "moving_right"); // fdEntryEff->addItem(tr("Move to bottom"), "moving_bottom"); // fdEntryEff->addItem(tr("Alpha in"), "alpha_in"); // fdEntryEff->addItem(tr("Zoom in"), "zoom_in"); // fdEntryEff->addItem(tr("Zoom in from left-top"), "zoom_in_left_top"); // fdEntryEff->addItem(tr("Zoom in from right-top"), "zoom_in_right_top"); // fdEntryEff->addItem(tr("Zoom in from right-bottom"), "zoom_in_right_bottom"); // fdEntryEff->addItem(tr("Zoom in from left-bottom"), "zoom_in_left_bottom"); // fdEntryEff->addItem(tr("Rotate to right"), "rotate_right"); // fdEntryEff->addItem(tr("Rotate to left"), "rotate_left"); // fdEntryEff->addItem(tr("Random"), "Random"); // SetCurData(fdEntryEff, mEntryEffect); // connect(fdEntryEff, (void(QComboBox::*)(int))&QComboBox::currentIndexChanged, this, [=] { // mEntryEffect = fdEntryEff->currentData().toString(); // }); // hBox->addWidget(fdEntryEff); // hBox->addStretch(); // hBox->addLabel(tr("Dur")); // auto fdEntryDur = new QSpinBox; // fdEntryDur->setRange(1, 99); // fdEntryDur->setValue(mEntryDur); // connect(fdEntryDur, &QSpinBox::valueChanged, this, [=](int value) { // mEntryDur = value; // if(mDuration < value) { // mDuration = value; // fdDuration->setValue(value); // } // }); // hBox->addWidget(fdEntryDur); // hBox->addLabel(tr("s")); // hBox = new HBox(vBox); // hBox->setSpacing(2); // hBox->addSpacing(6); // hBox->addWidget(new QLabel(tr("Exit Effect"))); // auto fdExitEff = new QComboBox; // fdExitEff->addItem(tr("None"), ""); // fdExitEff->addItem(tr("Move to left"), "moving_left"); // fdExitEff->addItem(tr("Move to top"), "moving_top"); // fdExitEff->addItem(tr("Move to right"), "moving_right"); // fdExitEff->addItem(tr("Move to bottom"), "moving_bottom"); // fdExitEff->addItem(tr("Alpha out"), "alpha_out"); // fdExitEff->addItem(tr("Zoom out"), "zoom_out"); // fdExitEff->addItem(tr("Zoom out to left-top"), "zoom_out_left_top"); // fdExitEff->addItem(tr("Zoom out to right-top"), "zoom_out_right_top"); // fdExitEff->addItem(tr("Zoom out to right-bottom"), "zoom_out_right_bottom"); // fdExitEff->addItem(tr("Zoom out to left-bottom"), "zoom_out_left_bottom"); // fdExitEff->addItem(tr("Rotate to right"), "rotate_right"); // fdExitEff->addItem(tr("Rotate to left"), "rotate_left"); // fdExitEff->addItem(tr("Random"), "Random"); // SetCurData(fdExitEff, mExitEffect); // connect(fdExitEff, (void(QComboBox::*)(int))&QComboBox::currentIndexChanged, this, [=] { // mExitEffect = fdExitEff->currentData().toString(); // }); // hBox->addWidget(fdExitEff); // hBox->addStretch(); // hBox->addLabel(tr("Dur")); // auto fdExitDur = new QSpinBox; // fdExitDur->setRange(1, 99); // fdExitDur->setValue(mExitDur); // connect(fdExitDur, &QSpinBox::valueChanged, this, [=](int value) { // mExitDur = value; // if(mDuration < value) { // mDuration = value; // fdDuration->setValue(value); // } // }); // hBox->addWidget(fdExitDur); // hBox->addLabel(tr("s")); vBox->addStretch(); return wgtAttr; }