/* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. */ #include #include #include #include #include #include #include #include #include #include #include #include #include "filepropertiesdialog.h" #include "smtreemodel.h" #include "smtreeitem.h" #include "smtreeview.h" #include "helper.h" FilePropertiesDialog::FilePropertiesDialog(const QString &file, QWidget *parent, Qt::WindowFlags f) : SmDialog(parent, f), mFile(file){ mStack = new QStackedWidget; mTab = new QTabWidget; QString lText = QString(tr("Properties - %1")).arg(mFile); QLabel *l = new QLabel(lText); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(l); QWidget *formatWidget = new QWidget; mFormatView = new SmTreeView; mFormatModel = new SmTreeModel(QStringList() << tr("Key") << tr("Value"), this); mFormatView->setModel(mFormatModel); QHBoxLayout *formatLayout = new QHBoxLayout; formatLayout->addWidget(mFormatView); formatWidget->setLayout(formatLayout); mTab->addTab(formatWidget, tr("Format")); QWidget *streamWidget = new QWidget; mStreamView = new SmTreeView; mStreamModel = new SmTreeModel(QStringList() << tr("Key") << tr("Value"), this); mStreamView->setModel(mStreamModel); QHBoxLayout *streamLayout = new QHBoxLayout; streamLayout->addWidget(mStreamView); streamWidget->setLayout(streamLayout); mTab->addTab(streamWidget, tr("Streams")); mStack->addWidget(mTab); QTabWidget *mPicsTab = new QTabWidget; QWidget *picWidget = new QWidget; mPicView = new SmTreeView; mPicModel = new SmTreeModel(QStringList() << tr("Key") << tr("Value"), this); mPicView->setModel(mPicModel); QHBoxLayout *picLayout = new QHBoxLayout; picLayout->addWidget(mPicView); picWidget->setLayout(picLayout); mPicsTab->addTab(picWidget, tr("Picture data")); QWidget *archivedTab = new QWidget; mArchivedAs = new QLabel; QHBoxLayout *archivedL = new QHBoxLayout; archivedL->addWidget(mArchivedAs); archivedTab->setLayout(archivedL); mPicsTab->addTab(archivedTab, tr("Archive")); mStack->addWidget(mPicsTab); mainLayout->addWidget(mStack); mOk = new QPushButton(tr("Ok")); connect(mOk, SIGNAL(clicked()), this, SLOT(accept())); QHBoxLayout *buttonLayout = new QHBoxLayout; buttonLayout->addStretch(); buttonLayout->addWidget(mOk); buttonLayout->addStretch(); mainLayout->addLayout(buttonLayout); setLayout(mainLayout); QString mimeType = Helper::mimeType(mFile); if(mimeType.startsWith("video")){ mStack->setCurrentIndex(0); movieData(); }else if(mimeType.startsWith("image")){ mStack->setCurrentIndex(1); pictureData(); } } void FilePropertiesDialog::movieData(){ QJsonDocument jsDoc = Helper::streamData(mFile); if(jsDoc.isNull()){ return; } QJsonObject obj = jsDoc.object(); SmTreeItem *formatItem = new SmTreeItem(2); movieDataRecursive(obj.value("format"), formatItem); mFormatModel->setRoot(formatItem); mFormatView->expandAll(); SmTreeItem *streamItem = new SmTreeItem(2); QJsonArray streamA = obj.value("streams").toArray(); for(int i = 0; i < streamA.size(); ++i){ QString itemName = QString(tr("Stream %1 [%2]")).arg(QString::number(i + 1)).arg(streamA.at(i).toObject().value("codec_type").toString()); SmTreeItem *item = new SmTreeItem(QList() << itemName << QVariant(), streamItem); streamItem->appendChild(item); movieDataRecursive(streamA.at(i), item); } mStreamModel->setRoot(streamItem); mStreamView->expandAll(); } void FilePropertiesDialog::movieDataRecursive(QJsonValue start, SmTreeItem *appendTo){ QJsonObject sObject = start.toObject(); QJsonObject::const_iterator it; for(it = sObject.constBegin(); it != sObject.constEnd(); ++it){ QList data = QList() << it.key() << it.value().toVariant(); if(it.key() == "duration"){ qint64 seconds = it.value().toString().toDouble(); Helper::Duration dur(seconds); data[1] = dur.toString(); } if(it.key() == "bit_rate"){ qint64 br = it.value().toString().toInt() / 1000; data[1] = QString("%1 kb/s").arg(QString::number(br)); } SmTreeItem *newItem = new SmTreeItem(data, appendTo); appendTo->appendChild(newItem); movieDataRecursive(*it, newItem); } } void FilePropertiesDialog::pictureData(){ // the sheer numbers... QImage img = QImage(mFile); if(img.isNull()){ return; } SmTreeItem *rootItem = new SmTreeItem(2); appendItem(QList() << tr("width") << img.width(), rootItem); appendItem(QList() << tr("height") << img.height(), rootItem); appendItem(QList() << tr("depth") << img.depth(), rootItem); appendItem(QList() << tr("alpha channel") << (img.hasAlphaChannel() ? tr("yes") : tr("no")), rootItem); appendItem(QList() << tr("qt format") << img.format(), rootItem); appendItem(QList() << tr("byte count") << img.byteCount(), rootItem); mPicModel->setRoot(rootItem); //now let's see if the file is already in the archive QString md5sum = Helper::md5Sum(mFile); QSqlDatabase db = QSqlDatabase::database("treedb"); db.open(); QSqlQuery p(db); p.prepare("SELECT tdescription_name FROM pics, pics_mappings, mapping_parents, mapping_description WHERE pics.cmd5sum = :md5 AND pics.ipicsid = pics_mappings.ipics_id AND pics_mappings.imappings_parents_id = mapping_parents.imapping_parents_id AND mapping_parents.idescription_id = mapping_description.idescription_id ORDER BY tdescription_name"); p.bindValue(":md5", md5sum); p.exec(); QStringList archived; while(p.next()){ archived << p.value(0).toString(); } if(archived.empty()){ mArchivedAs->setText(tr("
File not found in Archive
")); }else{ QString text = QString(tr("

Archived as:

    ")); foreach(QString a, archived){ text.append(QString(tr("
  • %1
  • ")).arg(a)); } text.append("
"); mArchivedAs->setText(text); } } void FilePropertiesDialog::appendItem(const QList &data, SmTreeItem *parent){ SmTreeItem *item = new SmTreeItem(data, parent); parent->appendChild(item); }