208 lines
7.9 KiB
C++
208 lines
7.9 KiB
C++
#include "mediapanel.h"
|
|
#include "main.h"
|
|
#include <QApplication>
|
|
#include <QMessageBox>
|
|
#include <QStandardPaths>
|
|
#include <QProgressBar>
|
|
#include <QFileDialog>
|
|
#include <QLineEdit>
|
|
#include <QProcess>
|
|
#include <QInputDialog>
|
|
#include <QDropEvent>
|
|
#include <QMediaPlayer>
|
|
#include <QVideoWidget>
|
|
#include <QVideoSink>
|
|
#include <QVideoFrame>
|
|
|
|
MediaPanel::MediaPanel(QWidget *parent) : QWidget(parent) {
|
|
auto vBox = new VBox(this);
|
|
vBox->setContentsMargins(0, 0, 0, 0);
|
|
vBox->setSpacing(0);
|
|
auto hBox = new HBox(vBox);
|
|
hBox->setContentsMargins(6,6,6,6);
|
|
hBox->setSpacing(6);
|
|
|
|
bnNew = new QPushButton(translate("","New"));
|
|
bnNew->setMinimumSize(75, 30);
|
|
bnNew->setProperty("ssType", "progManageTool");
|
|
hBox->addWidget(bnNew);
|
|
connect(bnNew, &QPushButton::clicked, this, [this] {
|
|
auto file = QFileDialog::getOpenFileName(this, 0, gFileHome);
|
|
if(file.isEmpty()) return;
|
|
QFileInfo info(file);
|
|
auto dir = info.absolutePath();
|
|
auto name = info.fileName();
|
|
gFileHome = dir;
|
|
auto suffix = info.suffix().toLower();
|
|
MediaItem *item = 0;
|
|
if(suffix.startsWith("mp")) {
|
|
auto player = new QMediaPlayer;
|
|
player->setSource(QUrl::fromLocalFile(file));
|
|
item = new MediaItem(file, tree);
|
|
item->setText("type", "Video");
|
|
auto videoWgt = new QVideoWidget;
|
|
player->setVideoOutput(videoWgt);
|
|
auto videoSink = videoWgt->videoSink();
|
|
connect(videoSink, &QVideoSink::videoFrameChanged, player, [=](const QVideoFrame &frame) {
|
|
disconnect(videoSink, &QVideoSink::videoFrameChanged, player, 0);
|
|
player->stop();
|
|
player->deleteLater();
|
|
videoWgt->deleteLater();
|
|
item->setText("size", QString("%1*%2").arg(frame.width()).arg(frame.height()));
|
|
item->setText("dur", QTime::fromMSecsSinceStartOfDay(player->duration()).toString("hh:mm:ss.zzz"));
|
|
item->profile = frame.toImage().scaledToHeight(60, Qt::SmoothTransformation);
|
|
auto edProfile = new QLabel;
|
|
edProfile->setPixmap(QPixmap::fromImage(item->profile));
|
|
edProfile->setScaledContents(true);
|
|
edProfile->setMaximumHeight(24);
|
|
item->setCellWidget("profile", edProfile);
|
|
});
|
|
player->play();
|
|
} else if(suffix == "png" || suffix.startsWith("jp") || suffix == "gif") {
|
|
QImageReader reader(file);
|
|
reader.setAutoTransform(true);
|
|
auto img = reader.read();
|
|
if(img.isNull()) {
|
|
QMessageBox::critical(this, "Image Read Error", QString::number(reader.error())+" "+reader.errorString());
|
|
return;
|
|
}
|
|
item = new MediaItem(file, tree);
|
|
item->setText("type", "Image");
|
|
item->setText("dur", "00:10:00.000");
|
|
item->setText("size", QString("%1*%2").arg(img.width()).arg(img.height()));
|
|
item->profile = img.scaledToHeight(60, Qt::SmoothTransformation);
|
|
auto edProfile = new QLabel;
|
|
edProfile->setPixmap(QPixmap::fromImage(item->profile));
|
|
edProfile->setScaledContents(true);
|
|
edProfile->setMaximumHeight(24);
|
|
item->setCellWidget("profile", edProfile);
|
|
}
|
|
if(item) {
|
|
item->setText("name", name);
|
|
}
|
|
});
|
|
|
|
bnDelete = new QPushButton(tr("Delete"));
|
|
bnDelete->setMinimumSize(75, 30);
|
|
bnDelete->setProperty("ssType", "progManageTool");
|
|
hBox->addWidget(bnDelete);
|
|
connect(bnDelete, &QPushButton::clicked, this, [=] {
|
|
// for(int i=0; i<tree->topLevelItemCount(); i++) if(tree->item(i)->checkState("check") == Qt::Checked) {
|
|
// auto item = (MediaItem*) tree->topLevelItem(i--);
|
|
// item->del();
|
|
// delete item;
|
|
// }
|
|
});
|
|
|
|
bnRename = new QPushButton(tr("Rename"));
|
|
bnRename->setMinimumSize(60, 30);
|
|
bnRename->setProperty("ssType", "progManageTool");
|
|
hBox->addWidget(bnRename);
|
|
connect(bnRename, &QPushButton::clicked, this, [=] {
|
|
// int cnt = tree->topLevelItemCount();
|
|
// QString progName;
|
|
// for(int i=0; i<cnt; i++) if(tree->item(i)->checkState("check") == Qt::Checked) {
|
|
// progName = ((MediaItem*) tree->topLevelItem(i))->mName;
|
|
// break;
|
|
// }
|
|
// if(progName.isEmpty()) return;
|
|
// bool ok = false;
|
|
// auto newName = QInputDialog::getText(this, "Rename '"+progName+"'", "New Name", QLineEdit::Normal, "", &ok);
|
|
// if(! ok) return;
|
|
// if(newName.isEmpty()) {
|
|
// QMessageBox::warning(this, "Warning", "New Name is Empty");
|
|
// return;
|
|
// }
|
|
// auto progsDir = programsDir();
|
|
// auto res = QDir(progsDir).rename(progName, newName);
|
|
// if(res) addProFiles();
|
|
// else QMessageBox::warning(this, "Error", "Rename Failed");
|
|
});
|
|
|
|
hBox->addStretch();
|
|
|
|
auto fdSearch = new QLineEdit;
|
|
fdSearch->setMinimumWidth(100);
|
|
auto search = new QAction;
|
|
search->setIcon(QIcon(":/res/program/bnSearch.png"));
|
|
fdSearch->addAction(search, QLineEdit::LeadingPosition);
|
|
fdSearch->setClearButtonEnabled(true);
|
|
//fdSearch->setStyleSheet("border: 1px solid #888;");
|
|
connect(fdSearch, &QLineEdit::textChanged, this, [this](const QString &text) {
|
|
auto cnt = tree->topLevelItemCount();
|
|
for(int i=0; i<cnt; i++) {
|
|
auto item = tree->item(i);
|
|
item->setHidden(! (text.isEmpty() || item->text("name").contains(text) || item->text("resolution").contains(text)));
|
|
}
|
|
});
|
|
hBox->addWidget(fdSearch);
|
|
|
|
tree = new MediaTree;
|
|
tree->addCol("#", "", 20);
|
|
tree->addCol("profile", "", 42);
|
|
tree->addCol("name", "", 140);
|
|
tree->addCol("type", "", 45);
|
|
tree->addCol("size", "", 60);
|
|
tree->addCol("dur", "", 80);
|
|
tree->setDefs()->setHeaderAlignC();
|
|
tree->minRowHeight = 26;
|
|
tree->setSortingEnabled(true);
|
|
tree->setDragEnabled(true);
|
|
tree->setAcceptDrops(true);
|
|
tree->setDropIndicatorShown(true);
|
|
vBox->addWidget(tree);
|
|
|
|
auto dataDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
|
if(mProgsDir.isEmpty()) return;
|
|
tree->clear();
|
|
// for(auto &progName : progNames) {
|
|
// auto file = mProgsDir + "/" + progName + "/pro.json";
|
|
// QFile qFile(file);
|
|
// if(! qFile.exists()) continue;
|
|
// if(! qFile.open(QIODevice::ReadOnly)) continue;
|
|
// auto data = qFile.readAll();
|
|
// qFile.close();
|
|
// QString error;
|
|
// auto json = JFrom(data, &error);
|
|
// if(! error.isEmpty()) continue;
|
|
// auto item = new MediaItem(tree);
|
|
// item->dir = dir;
|
|
// item->setText("name", name);
|
|
// item->setText("resolution", QString("%1 x %2").arg(mWidth).arg(mHeight));
|
|
// }
|
|
|
|
QSettings settings;
|
|
tree->sortByColumn(settings.value("MediaSortColumn").toInt(), (Qt::SortOrder)settings.value("MediaSortOrder").toInt());
|
|
|
|
transUi();
|
|
}
|
|
|
|
void MediaPanel::changeEvent(QEvent *event) {
|
|
QWidget::changeEvent(event);
|
|
if(event->type() == QEvent::LanguageChange) transUi();
|
|
}
|
|
void MediaPanel::transUi() {
|
|
tree->headerItem()->setText("name"**tree, tr("Name"));
|
|
tree->headerItem()->setText("type"**tree, tr("Type"));
|
|
tree->headerItem()->setText("size"**tree, tr("Size"));
|
|
tree->headerItem()->setText("dur"**tree, tr("Duration"));
|
|
bnNew->setText(tr("New"));
|
|
bnDelete->setText(tr("Delete"));
|
|
bnRename->setText(tr("Rename"));
|
|
}
|
|
|
|
MediaItem::MediaItem(const QString &file, TreeWidget *tree) : TreeWidgetItem(tree), file(file) {
|
|
|
|
}
|
|
|
|
void MediaTree::dropEvent(QDropEvent *event) {
|
|
if(MediaTree::OnItem==dropIndicatorPosition()) {
|
|
event->ignore();
|
|
return;
|
|
}
|
|
TreeWidget::dropEvent(event);
|
|
qDebug()<<" event"<<event;
|
|
qDebug()<<" source"<<event->source();
|
|
qDebug()<<" mimeData"<<event->mimeData();
|
|
}
|