qt/LedOK/program/emultiwin.cpp

318 lines
13 KiB
C++
Raw Normal View History

2022-08-25 18:37:24 +08:00
#include "emultiwin.h"
#include "tools.h"
#include "base/extendedgroupbox.h"
#include "ebase.h"
#include "etext.h"
#include "ephoto.h"
#include "evideo.h"
#include "egif.h"
#include "edclock.h"
#include "eaclock.h"
#include "etimer.h"
#include "eenviron.h"
#include "eweb.h"
#include <QJsonArray>
#include <QBoxLayout>
#include <QMenu>
#include <QMessageBox>
#include <QGraphicsScene>
EMultiWin::EMultiWin(PageListItem *pageItem) : mPageItem(pageItem) {
mType = EBase::Window;
}
EMultiWin::EMultiWin(const QJsonObject &json, PageListItem *pageItem) : mPageItem(pageItem) {
mType = EBase::Window;
setBaseAttr(json);
2023-04-19 14:42:06 +08:00
auto elements = json["elements"].toArray();
2022-08-25 18:37:24 +08:00
index = json["index"].toInt();
2023-04-19 14:42:06 +08:00
foreach(QJsonValue element, elements) {
2022-08-25 18:37:24 +08:00
QString type = element["elementType"].toString();
EBase *inner = nullptr;
if(type=="Text") inner = new EText(element.toObject(), this);
else if(type=="Photo") inner = EPhoto::create(element.toObject(), pageItem, this);
else if(type=="Gif") inner = EGif::create(element.toObject(), pageItem, this);
else if(type=="Movie") inner = EVideo::create(element.toObject(), pageItem, this);
2023-04-18 14:14:46 +08:00
else if(type=="DClock") inner = new EDClock(element.toObject(), this);
else if(type=="AClock") inner = new EAClock(element.toObject(), this);
2022-08-25 18:37:24 +08:00
else if(type=="Temp") inner = new EEnviron(element.toObject(), this);
else if(type=="Web") inner = new EWeb(element.toObject(), this);
else if(type=="Timer") inner = new ETimer(element.toObject(), this);
2023-05-09 15:08:39 +08:00
if(inner==0) continue;
2022-08-25 18:37:24 +08:00
inner->setPos(0, 0);
inner->setFlag(QGraphicsItem::ItemStacksBehindParent);
if(index != inners.size()) inner->freeFiles();
inners.append(inner);
}
if(inners.isEmpty()) return;
if(index < 0 || index >= inners.size()) index = inners.size()-1;
setCur(inners[index]);
}
EMultiWin::~EMultiWin() {
foreach(auto inner, inners) delete inner;
}
void EMultiWin::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
if(inners.isEmpty()) {
QTextOption opt(Qt::AlignCenter);
painter->save();
painter->fillRect(rect(),QColor(0, 0, 0));
2023-04-27 15:06:24 +08:00
painter->setFont(qfont("Arial", 12));
2022-08-25 18:37:24 +08:00
painter->setPen(QColor(100, 100, 100));
painter->drawText(rect(), tr("Please add media on the right"), opt);
painter->restore();
}
EBase::paint(painter, option, widget);
}
bool EMultiWin::save(const QString &pageDir) {
foreach(auto inner, inners) inner->save(pageDir);
return true;
}
QJsonObject EMultiWin::attrJson() const{
QJsonArray eles;
foreach(auto inner, inners) eles.append(inner->attrJson());
QJsonObject oRoot;
addBaseAttr(oRoot);
oRoot["elementType"] = "Window";
oRoot["index"] = index;
oRoot["elements"] = eles;
return oRoot;
}
void EMultiWin::setCur(EBase *cur) {
cur->setSize(mWidth, mHeight);
connect(this, &EMultiWin::sizeChanged, cur, [cur] {
if(cur->mMultiWin!=nullptr) cur->setSize(cur->mMultiWin->mWidth, cur->mMultiWin->mHeight);
});
cur->setParentItem(this);
cur->loadFiles();
}
class MListWidget : public QListWidget {
public:
QSize minimumSizeHint() const override {
return sizeHint();
};
QSize sizeHint() const override {
auto size = QListWidget::sizeHint();
auto minH = minimumHeight();
if(minH > 0) size.setHeight(minH+0xfff);
return size;
};
};
QWidget* EMultiWin::attrWgt() {
auto wgtAttr = new QWidget();
auto vBox = new QVBoxLayout(wgtAttr);
vBox->setContentsMargins(3, 0, 3, 0);
vBox->setSpacing(3);
addBaseAttrWgt(vBox);
auto groupBox = new ExtendedGroupBox(tr("Media List"));
groupBox->setFlat(true);
groupBox->setCheckable(true);
auto vBoxGroup = new QVBoxLayout(groupBox);
vBoxGroup->setContentsMargins(0,0,0,0);
vBoxGroup->setSpacing(2);
auto hBox = new QHBoxLayout();
auto btnAdd = new QPushButton();
2023-04-18 14:14:46 +08:00
btnAdd->setIcon(QIcon(":/res/program/Add.png"));
2022-08-25 18:37:24 +08:00
btnAdd->setProperty("style","multiTool");
auto menu = new QMenu();
2023-04-18 14:14:46 +08:00
menu->addAction(QIcon(":/res/program/Text.png"), tr("Text"))->setData(EBase::Text);
menu->addAction(QIcon(":/res/program/Photo.png"), tr("Photo"))->setData(EBase::Photo);
menu->addAction(QIcon(":/res/program/Movie.png"), tr("Video"))->setData(EBase::Video);
menu->addAction(QIcon(":/res/program/Gif.png"), tr("Gif"))->setData(EBase::Gif);
menu->addAction(QIcon(":/res/program/DClock.png"), tr("DClock"))->setData(EBase::DClock);
menu->addAction(QIcon(":/res/program/AClock.png"), tr("AClock"))->setData(EBase::AClock);
2022-08-25 18:37:24 +08:00
auto listWgt = new MListWidget();
2023-05-09 15:08:39 +08:00
connect(menu, &QMenu::triggered, this, [=](QAction *act) {
2022-08-25 18:37:24 +08:00
int type = act->data().toInt();
int order = listWgt->count();
2023-05-09 15:08:39 +08:00
EBase *ele = 0;
QListWidgetItem *item = 0;
2022-08-25 18:37:24 +08:00
if(type==EBase::Photo) {
2023-05-11 11:47:00 +08:00
auto files = QFileDialog::getOpenFileNames(wgtAttr, tr("Select File"), gFileHome, EPhoto::filters());
2022-08-25 18:37:24 +08:00
for(int i=0; i<files.count(); i++) {
auto ePhoto = EPhoto::create(files[i], mPageItem, this);
2023-05-09 15:08:39 +08:00
if(ePhoto==0) continue;
2022-08-25 18:37:24 +08:00
ePhoto->setSize(mWidth, mHeight);
ePhoto->setZValue(order++);
ePhoto->setFlag(QGraphicsItem::ItemStacksBehindParent);
inners.append(ePhoto);
2023-04-18 14:14:46 +08:00
auto item = new QListWidgetItem(QIcon(":/res/program/Photo.png"), tr("Photo")+" "+ePhoto->mName);
2023-05-09 15:08:39 +08:00
item->setData(Qt::UserRole, QVariant::fromValue((void*) ePhoto));
2022-08-25 18:37:24 +08:00
listWgt->addItem(item);
if(i==files.count()-1) {
listWgt->setCurrentItem(item);
2022-09-13 23:16:36 +08:00
gFileHome = ePhoto->mDir;
2022-08-25 18:37:24 +08:00
}
}
} else if(type==EBase::Gif) {
2023-05-11 11:47:00 +08:00
auto files = QFileDialog::getOpenFileNames(wgtAttr, tr("Select File"), gFileHome, EGif::filters());
2022-08-25 18:37:24 +08:00
for(int i=0; i<files.count(); i++) {
auto eGif = EGif::create(files[i], mPageItem, this);
2023-05-09 15:08:39 +08:00
if(eGif==0) continue;
2022-08-25 18:37:24 +08:00
eGif->setSize(mWidth, mHeight);
eGif->setZValue(order++);
eGif->setFlag(QGraphicsItem::ItemStacksBehindParent);
inners.append(eGif);
2023-04-18 14:14:46 +08:00
auto item = new QListWidgetItem(QIcon(":/res/program/Gif.png"), tr("Gif")+" "+eGif->mName);
2023-05-09 15:08:39 +08:00
item->setData(Qt::UserRole, QVariant::fromValue((void*) eGif));
2022-08-25 18:37:24 +08:00
listWgt->addItem(item);
if(i == files.count()-1) {
listWgt->setCurrentItem(item);
2022-09-13 23:16:36 +08:00
gFileHome = eGif->mDir;
2022-08-25 18:37:24 +08:00
}
}
} else if(type==EBase::Video) {
2023-05-11 11:47:00 +08:00
auto file = QFileDialog::getOpenFileName(wgtAttr, tr("Select File"), gFileHome, EVideo::filters());
2022-08-25 18:37:24 +08:00
if(! file.isEmpty()) {
auto eVideo = EVideo::create(file, mPageItem, this);
2023-05-09 15:08:39 +08:00
if(eVideo==0) return;
2023-04-18 14:14:46 +08:00
item = new QListWidgetItem(QIcon(":/res/program/Movie.png"), tr("Video")+" "+eVideo->mRawName);
2022-09-13 23:16:36 +08:00
gFileHome = eVideo->mRawDir;
2022-08-25 18:37:24 +08:00
ele = eVideo;
}
} else if(type==EBase::Text) {
ele = new EText(this);
2023-04-18 14:14:46 +08:00
item = new QListWidgetItem(QIcon(":/res/program/Text.png"), tr("Text"));
2022-08-25 18:37:24 +08:00
} else if(type==EBase::DClock) {
2023-04-18 14:14:46 +08:00
ele = new EDClock(this);
item = new QListWidgetItem(QIcon(":/res/program/DClock.png"), tr("DClock"));
2022-08-25 18:37:24 +08:00
} else if(type==EBase::AClock) {
2023-04-18 14:14:46 +08:00
ele = new EAClock(this);
item = new QListWidgetItem(QIcon(":/res/program/AClock.png"), tr("AClock"));
2022-08-25 18:37:24 +08:00
} else if(type==EBase::Environ) {
ele = new EEnviron(this);
2023-04-18 14:14:46 +08:00
item = new QListWidgetItem(QIcon(":/res/program/Temp.png"), tr("Environment"));
2022-08-25 18:37:24 +08:00
} else if(type==EBase::Timer) {
ele = new ETimer(this);
2023-04-18 14:14:46 +08:00
item = new QListWidgetItem(QIcon(":/res/program/Timer.png"), tr("Timer"));
2022-08-25 18:37:24 +08:00
}
2023-05-09 15:08:39 +08:00
if(ele) {
2022-08-25 18:37:24 +08:00
ele->setSize(mWidth, mHeight);
ele->setZValue(order);
ele->setFlag(QGraphicsItem::ItemStacksBehindParent);
inners.append(ele);
2023-05-09 15:08:39 +08:00
item->setData(Qt::UserRole, QVariant::fromValue((void*)ele));
2022-08-25 18:37:24 +08:00
listWgt->addItem(item);
listWgt->setCurrentItem(item);
}
});
btnAdd->setMenu(menu);
hBox->addWidget(btnAdd);
auto btnDel = new QPushButton();
2023-04-18 14:14:46 +08:00
btnDel->setIcon(QIcon(":/res/program/Delete.png"));
2022-08-25 18:37:24 +08:00
btnDel->setProperty("style","multiTool");
connect(btnDel, &QPushButton::clicked, this, [this, listWgt] {
auto row = listWgt->currentRow();
if(row < 0) return;
auto item = listWgt->takeItem(row);
if(listWgt->count() > 0) listWgt->setCurrentRow(0);
auto ele = static_cast<EBase*>(item->data(Qt::UserRole).value<void*>());
delete item;
inners.removeOne(ele);
delete ele;
int n = listWgt->count();
for(int i=0; i<n; i++) static_cast<EBase*>(listWgt->item(i)->data(Qt::UserRole).value<void*>())->setZValue(i);
});
hBox->addWidget(btnDel);
auto btnClean = new QPushButton();
2023-04-18 14:14:46 +08:00
btnClean->setIcon(QIcon(":/res/program/Clean.png"));
2022-08-25 18:37:24 +08:00
btnClean->setProperty("style","multiTool");
connect(btnClean, &QPushButton::clicked, this, [this, listWgt] {
listWgt->clear();
foreach(auto inner, inners) delete inner;
inners.clear();
});
hBox->addWidget(btnClean);
auto btnGoUp = new QPushButton();
2023-04-18 14:14:46 +08:00
btnGoUp->setIcon(QIcon(":/res/program/GoUp.png"));
2022-08-25 18:37:24 +08:00
btnGoUp->setProperty("style","multiTool");
connect(btnGoUp, &QPushButton::clicked, this, [this, listWgt] {
int row = listWgt->currentRow();
if(row < 1) return;
listWgt->insertItem(row-1, listWgt->takeItem(row));
listWgt->setCurrentRow(row-1);
int n = listWgt->count();
for(int i=0; i<n; i++) static_cast<EBase*>(listWgt->item(i)->data(Qt::UserRole).value<void*>())->setZValue(i);
inners.swapItemsAt(row, row-1);
});
hBox->addWidget(btnGoUp);
auto btnGoDown = new QPushButton();
2023-04-18 14:14:46 +08:00
btnGoDown->setIcon(QIcon(":/res/program/GoDown.png"));
2022-08-25 18:37:24 +08:00
btnGoDown->setProperty("style","multiTool");
connect(btnGoDown, &QPushButton::clicked, this, [this, listWgt] {
int row = listWgt->currentRow();
if(row < 0 || row > listWgt->count() - 2) return;
listWgt->insertItem(row+1, listWgt->takeItem(row));
listWgt->setCurrentRow(row+1);
int n = listWgt->count();
for(int i=0; i<n; i++) static_cast<EBase*>(listWgt->item(i)->data(Qt::UserRole).value<void*>())->setZValue(i);
inners.swapItemsAt(row, row+1);
});
hBox->addWidget(btnGoDown);
vBoxGroup->addLayout(hBox);
listWgt->setMinimumHeight(120);
listWgt->setIconSize(QSize(20, 20));
foreach(auto inner, inners) {
2023-05-09 15:08:39 +08:00
QListWidgetItem *item = 0;
2022-08-25 18:37:24 +08:00
int type = inner->type();
2023-04-18 14:14:46 +08:00
if(type==EBase::Text) item = new QListWidgetItem(QIcon(":/res/program/Text.png"), tr("Text"));
else if(type==EBase::Photo) item = new QListWidgetItem(QIcon(":/res/program/Photo.png"), tr("Photo")+" "+static_cast<EPhoto*>(inner)->mName);
else if(type==EBase::Video) item = new QListWidgetItem(QIcon(":/res/program/Movie.png"), tr("Video")+" "+static_cast<EVideo*>(inner)->mRawName);
else if(type==EBase::Gif) item = new QListWidgetItem(QIcon(":/res/program/Gif.png"), tr("Gif")+" "+static_cast<EGif*>(inner)->mName);
else if(type==EBase::DClock) item = new QListWidgetItem(QIcon(":/res/program/DClock.png"), tr("DClock"));
else if(type==EBase::AClock) item = new QListWidgetItem(QIcon(":/res/program/AClock.png"), tr("AClock"));
else if(type==EBase::Environ) item = new QListWidgetItem(QIcon(":/res/program/Temp.png"), tr("Environment"));
else if(type==EBase::Timer) item = new QListWidgetItem(QIcon(":/res/program/Timer.png"), tr("Timer"));
2022-08-25 18:37:24 +08:00
if(item != nullptr) {
2023-05-09 15:08:39 +08:00
item->setData(Qt::UserRole, QVariant::fromValue((void*) inner));
2022-08-25 18:37:24 +08:00
listWgt->addItem(item);
}
}
2023-05-09 15:08:39 +08:00
connect(listWgt, &QListWidget::currentItemChanged, this, [=](QListWidgetItem *current, QListWidgetItem *previous) {
if(previous) {
2022-08-25 18:37:24 +08:00
auto eLast = static_cast<EBase*>(previous->data(Qt::UserRole).value<void*>());
2023-05-09 15:08:39 +08:00
if(eLast) {
if(scene()) scene()->removeItem(eLast);
2022-08-25 18:37:24 +08:00
disconnect(this, &EMultiWin::sizeChanged, eLast, 0);
eLast->freeFiles();
}
auto itemLast = vBox->itemAt(vBox->count()-1);
2023-05-09 15:08:39 +08:00
if(itemLast && itemLast->widget() != groupBox) {
2022-08-25 18:37:24 +08:00
vBox->takeAt(vBox->count()-1);
2023-05-09 15:08:39 +08:00
if(itemLast->widget()) delete itemLast->widget();
2022-08-25 18:37:24 +08:00
delete itemLast;
}
}
2023-05-09 15:08:39 +08:00
EBase *eCur = 0;
if(current) {
eCur = (EBase*) current->data(Qt::UserRole).value<void*>();
2022-08-25 18:37:24 +08:00
auto attr = eCur->attrWgt();
2023-05-09 15:08:39 +08:00
if(attr) vBox->addWidget(attr);
2022-08-25 18:37:24 +08:00
}
index = listWgt->currentRow();
2023-05-09 15:08:39 +08:00
if(eCur) setCur(eCur);
2022-08-25 18:37:24 +08:00
});
vBoxGroup->addWidget(listWgt);
vBox->addWidget(groupBox);
if(index > -1) listWgt->setCurrentRow(index);
return wgtAttr;
}