QTfrontend/ui/page/pagevideos.cpp
changeset 7447 01111960a48d
parent 7392 bc3306c59a08
child 7507 3032a5739fe1
equal deleted inserted replaced
7442:9bb6abdb5675 7447:01111960a48d
    37 #include <QVBoxLayout>
    37 #include <QVBoxLayout>
    38 #include <QHBoxLayout>
    38 #include <QHBoxLayout>
    39 #include <QFileSystemWatcher>
    39 #include <QFileSystemWatcher>
    40 #include <QDateTime>
    40 #include <QDateTime>
    41 #include <QRegExp>
    41 #include <QRegExp>
       
    42 #include <QNetworkAccessManager>
       
    43 #include <QNetworkRequest>
       
    44 #include <QNetworkReply>
    42 
    45 
    43 #include "hwconsts.h"
    46 #include "hwconsts.h"
    44 #include "pagevideos.h"
    47 #include "pagevideos.h"
    45 #include "igbox.h"
    48 #include "igbox.h"
    46 #include "libav_iteraction.h"
    49 #include "libav_iteraction.h"
    47 #include "gameuiconfig.h"
    50 #include "gameuiconfig.h"
    48 #include "recorder.h"
    51 #include "recorder.h"
    49 #include "ask_quit.h"
    52 #include "ask_quit.h"
    50 
    53 #include "upload_video.h"
    51 const QSize ThumbnailSize(350, 350*3/5);
    54 
       
    55 static const QSize ThumbnailSize(350, 350*3/5);
    52 
    56 
    53 // columns in table with list of video files
    57 // columns in table with list of video files
    54 enum VideosColumns
    58 enum VideosColumns
    55 {
    59 {
    56     vcName,
    60     vcName,
    68         VideoItem(const QString& name);
    72         VideoItem(const QString& name);
    69         ~VideoItem();
    73         ~VideoItem();
    70 
    74 
    71         QString name;
    75         QString name;
    72         QString desc; // description
    76         QString desc; // description
       
    77         QString uploadReply;
    73         HWRecorder * pRecorder; // non NULL if file is being encoded
    78         HWRecorder * pRecorder; // non NULL if file is being encoded
       
    79         QNetworkReply * pUploading; // non NULL if file is being uploaded
    74         bool seen; // used when updating directory
    80         bool seen; // used when updating directory
    75         float lastSizeUpdate;
    81         float lastSizeUpdate;
    76         float progress;
    82         float progress;
    77 
    83 
    78         bool ready()
    84         bool ready()
    85 VideoItem::VideoItem(const QString& name)
    91 VideoItem::VideoItem(const QString& name)
    86     : QTableWidgetItem(name, UserType)
    92     : QTableWidgetItem(name, UserType)
    87 {
    93 {
    88     this->name = name;
    94     this->name = name;
    89     pRecorder = NULL;
    95     pRecorder = NULL;
       
    96     pUploading = NULL;
    90     lastSizeUpdate = 0;
    97     lastSizeUpdate = 0;
    91     progress = 0;
    98     progress = 0;
    92 }
    99 }
    93 
   100 
    94 VideoItem::~VideoItem()
   101 VideoItem::~VideoItem()
   215 
   222 
   216         filesTable = new QTableWidget(pTableGroup);
   223         filesTable = new QTableWidget(pTableGroup);
   217         filesTable->setColumnCount(vcNumColumns);
   224         filesTable->setColumnCount(vcNumColumns);
   218         filesTable->setHorizontalHeaderLabels(columns);
   225         filesTable->setHorizontalHeaderLabels(columns);
   219         filesTable->setSelectionBehavior(QAbstractItemView::SelectRows);
   226         filesTable->setSelectionBehavior(QAbstractItemView::SelectRows);
       
   227         filesTable->setSelectionMode(QAbstractItemView::SingleSelection);
   220         filesTable->setEditTriggers(QAbstractItemView::SelectedClicked);
   228         filesTable->setEditTriggers(QAbstractItemView::SelectedClicked);
   221         filesTable->verticalHeader()->hide();
   229         filesTable->verticalHeader()->hide();
   222         filesTable->setMinimumWidth(400);
   230         filesTable->setMinimumWidth(400);
   223 
   231 
   224         QHeaderView * header = filesTable->horizontalHeader();
   232         QHeaderView * header = filesTable->horizontalHeader();
   270         btnPlay->setEnabled(false);
   278         btnPlay->setEnabled(false);
   271         pBottomDescLayout->addWidget(btnPlay);
   279         pBottomDescLayout->addWidget(btnPlay);
   272         btnDelete = new QPushButton(QPushButton::tr("Delete"), pDescGroup);
   280         btnDelete = new QPushButton(QPushButton::tr("Delete"), pDescGroup);
   273         btnDelete->setEnabled(false);
   281         btnDelete->setEnabled(false);
   274         pBottomDescLayout->addWidget(btnDelete);
   282         pBottomDescLayout->addWidget(btnDelete);
       
   283         btnToYouTube = new QPushButton(QPushButton::tr("Upload to YouTube"), pDescGroup);
       
   284         btnToYouTube->setEnabled(false);
       
   285         pBottomDescLayout->addWidget(btnToYouTube);
   275 
   286 
   276         pDescLayout->addStretch(1);
   287         pDescLayout->addStretch(1);
   277         pDescLayout->addLayout(pTopDescLayout, 0);
   288         pDescLayout->addLayout(pTopDescLayout, 0);
   278         pDescLayout->addStretch(1);
   289         pDescLayout->addStretch(1);
   279         pDescLayout->addLayout(pBottomDescLayout, 0);
   290         pDescLayout->addLayout(pBottomDescLayout, 0);
   298     connect(filesTable, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(cellDoubleClicked(int, int)));
   309     connect(filesTable, SIGNAL(cellDoubleClicked(int, int)), this, SLOT(cellDoubleClicked(int, int)));
   299     connect(filesTable, SIGNAL(cellChanged(int,int)), this, SLOT(cellChanged(int, int)));
   310     connect(filesTable, SIGNAL(cellChanged(int,int)), this, SLOT(cellChanged(int, int)));
   300     connect(filesTable, SIGNAL(currentCellChanged(int,int,int,int)), this, SLOT(currentCellChanged(int,int,int,int)));
   311     connect(filesTable, SIGNAL(currentCellChanged(int,int,int,int)), this, SLOT(currentCellChanged(int,int,int,int)));
   301     connect(btnPlay,   SIGNAL(clicked()), this, SLOT(playSelectedFile()));
   312     connect(btnPlay,   SIGNAL(clicked()), this, SLOT(playSelectedFile()));
   302     connect(btnDelete, SIGNAL(clicked()), this, SLOT(deleteSelectedFiles()));
   313     connect(btnDelete, SIGNAL(clicked()), this, SLOT(deleteSelectedFiles()));
       
   314     connect(btnToYouTube, SIGNAL(clicked()), this, SLOT(uploadToYouTube()));
   303     connect(btnOpenDir, SIGNAL(clicked()), this, SLOT(openVideosDirectory()));
   315     connect(btnOpenDir, SIGNAL(clicked()), this, SLOT(openVideosDirectory()));
   304  }
   316  }
   305 
   317 
   306 PageVideos::PageVideos(QWidget* parent) : AbstractPage(parent),
   318 PageVideos::PageVideos(QWidget* parent) : AbstractPage(parent),
   307     config(0)
   319     config(0), netManager(0)
   308 {
   320 {
   309     nameChangedFromCode = false;
   321     nameChangedFromCode = false;
   310     numRecorders = 0;
   322     numRecorders = 0;
       
   323     numUploads = 0;
   311     initPage();
   324     initPage();
   312 }
   325 }
   313 
   326 
   314 void PageVideos::init(GameUIConfig * config)
   327 void PageVideos::init(GameUIConfig * config)
   315 {
   328 {
   519     filesTable->setCellWidget(row, vcProgress, progressBar);
   532     filesTable->setCellWidget(row, vcProgress, progressBar);
   520 
   533 
   521     numRecorders++;
   534     numRecorders++;
   522 }
   535 }
   523 
   536 
   524 void PageVideos::updateProgress(float value)
   537 void PageVideos::setProgress(int row, VideoItem* item, float value)
   525 {
   538 {
   526     HWRecorder * pRecorder = (HWRecorder*)sender();
       
   527     VideoItem * item = (VideoItem*)pRecorder->item;
       
   528     int row = filesTable->row(item);
       
   529 
       
   530     // update file size every percent
       
   531     if (value - item->lastSizeUpdate > 0.01)
       
   532     {
       
   533         updateSize(row);
       
   534         item->lastSizeUpdate = value;
       
   535     }
       
   536 
       
   537     // update progress bar
       
   538     QProgressBar * progressBar = (QProgressBar*)filesTable->cellWidget(row, vcProgress);
   539     QProgressBar * progressBar = (QProgressBar*)filesTable->cellWidget(row, vcProgress);
   539     progressBar->setValue(value*10000);
   540     progressBar->setValue(value*10000);
   540     progressBar->setFormat(QString("%1%").arg(value*100, 0, 'f', 2));
   541     progressBar->setFormat(QString("%1%").arg(value*100, 0, 'f', 2));
   541     item->progress = value;
   542     item->progress = value;
       
   543 }
       
   544 
       
   545 void PageVideos::updateProgress(float value)
       
   546 {
       
   547     HWRecorder * pRecorder = (HWRecorder*)sender();
       
   548     VideoItem * item = (VideoItem*)pRecorder->item;
       
   549     int row = filesTable->row(item);
       
   550 
       
   551     // update file size every percent
       
   552     if (value - item->lastSizeUpdate > 0.01)
       
   553     {
       
   554         updateSize(row);
       
   555         item->lastSizeUpdate = value;
       
   556     }
       
   557 
       
   558     setProgress(row, item, value);
   542 }
   559 }
   543 
   560 
   544 void PageVideos::encodingFinished(bool success)
   561 void PageVideos::encodingFinished(bool success)
   545 {
   562 {
   546     numRecorders--;
   563     numRecorders--;
   672     {
   689     {
   673         labelDesc->clear();
   690         labelDesc->clear();
   674         clearThumbnail();
   691         clearThumbnail();
   675         btnPlay->setEnabled(false);
   692         btnPlay->setEnabled(false);
   676         btnDelete->setEnabled(false);
   693         btnDelete->setEnabled(false);
       
   694         btnToYouTube->setEnabled(false);
   677         return;
   695         return;
   678     }
   696     }
   679 
   697 
   680     btnPlay->setEnabled(item->ready());
   698     btnPlay->setEnabled(item->ready());
   681     btnDelete->setEnabled(true);
   699     btnDelete->setEnabled(true);
       
   700     btnToYouTube->setEnabled(item->ready());
   682 
   701 
   683     QString desc = item->name + "\n\n";
   702     QString desc = item->name + "\n\n";
   684     QString thumbName = "";
   703     QString thumbName = "";
   685 
   704 
   686     if (item->ready())
   705     if (item->ready())
   699         {
   718         {
   700             QString prefix = desc.mid(prefixBegin + 7, prefixEnd - (prefixBegin + 7));
   719             QString prefix = desc.mid(prefixBegin + 7, prefixEnd - (prefixBegin + 7));
   701             desc.remove(prefixBegin, prefixEnd + 7 - prefixBegin);
   720             desc.remove(prefixBegin, prefixEnd + 7 - prefixBegin);
   702             thumbName = prefix;
   721             thumbName = prefix;
   703         }
   722         }
       
   723 
       
   724         desc += item->uploadReply;
   704     }
   725     }
   705     else
   726     else
   706         desc += tr("(in progress...)");
   727         desc += tr("(in progress...)");
   707 
   728 
   708     if (thumbName.isEmpty())
   729     if (thumbName.isEmpty())
   821 }
   842 }
   822 
   843 
   823 bool PageVideos::tryQuit(HWForm * form)
   844 bool PageVideos::tryQuit(HWForm * form)
   824 {
   845 {
   825     bool quit = true;
   846     bool quit = true;
   826     if (numRecorders != 0)
   847     if (numRecorders != 0 || numUploads != 0)
   827     {
   848     {
   828         // ask user what to do - abort or wait
   849         // ask user what to do - abort or wait
   829         HWAskQuitDialog * askd = new HWAskQuitDialog(this, form);
   850         HWAskQuitDialog * askd = new HWAskQuitDialog(this, form);
       
   851         askd->deleteLater();
   830         quit = askd->exec();
   852         quit = askd->exec();
   831         delete askd;
       
   832     }
   853     }
   833     if (quit)
   854     if (quit)
   834         clearTemp();
   855         clearTemp();
   835     return quit;
   856     return quit;
   836 }
   857 }
   837 
   858 
   838 // returns multi-line string with list of videos in progress
   859 // returns multi-line string with list of videos in progress
       
   860 /* it will look like this:
       
   861 foo.avi (15.21% - encoding)
       
   862 bar.avi (18.21% - uploading)
       
   863 */
   839 QString PageVideos::getVideosInProgress()
   864 QString PageVideos::getVideosInProgress()
   840 {
   865 {
   841     QString list = "";
   866     QString list = "";
   842     int count = filesTable->rowCount();
   867     int count = filesTable->rowCount();
   843     for (int i = 0; i < count; i++)
   868     for (int i = 0; i < count; i++)
   844     {
   869     {
   845         VideoItem * item = nameItem(i);
   870         VideoItem * item = nameItem(i);
       
   871         QString process;
       
   872         if (!item->ready())
       
   873             process = tr("encoding");
       
   874         else if (item->pUploading)
       
   875             process = tr("uploading");
       
   876         else
       
   877             continue;
   846         float progress = 100*item->progress;
   878         float progress = 100*item->progress;
   847         if (progress > 99.99)
   879         if (progress > 99.99)
   848             progress = 99.99; // displaying 100% may be confusing
   880             progress = 99.99; // displaying 100% may be confusing
   849         if (!item->ready())
   881         list += item->name + " (" + QString::number(progress, 'f', 2) + "% - " + process + ")\n";
   850             list += item->name + " (" + QString::number(progress, 'f', 2) + "%)\n";
       
   851     }
   882     }
   852     return list;
   883     return list;
   853 }
   884 }
   854 
   885 
   855 void PageVideos::startEncoding(const QByteArray & record)
   886 void PageVideos::startEncoding(const QByteArray & record)
   878             pRecorder->EncodeVideo(demo);
   909             pRecorder->EncodeVideo(demo);
   879         }
   910         }
   880         addRecorder(pRecorder);
   911         addRecorder(pRecorder);
   881     }
   912     }
   882 }
   913 }
       
   914 
       
   915 void PageVideos::uploadProgress(qint64 bytesSent, qint64 bytesTotal)
       
   916 {
       
   917     QNetworkReply* reply = (QNetworkReply*)sender();
       
   918 
       
   919     VideoItem * item = NULL;
       
   920     int row;
       
   921     int count = filesTable->rowCount();
       
   922     // find corresponding item (maybe there is a better wat to implement this?)
       
   923     for (int i = 0; i < count; i++)
       
   924     {
       
   925         item = nameItem(i);
       
   926         if (item->pUploading == reply)
       
   927         {
       
   928             row = i;
       
   929             break;
       
   930         }
       
   931     }
       
   932     Q_ASSERT(item);
       
   933 
       
   934     setProgress(row, item, bytesSent*1.0/bytesTotal);
       
   935 }
       
   936 
       
   937 void PageVideos::uploadFinished()
       
   938 {
       
   939     QNetworkReply* reply = (QNetworkReply*)sender();
       
   940 
       
   941     VideoItem * item = NULL;
       
   942     int row;
       
   943     int count = filesTable->rowCount();
       
   944     for (int i = 0; i < count; i++)
       
   945     {
       
   946         item = nameItem(i);
       
   947         if (item->pUploading == reply)
       
   948         {
       
   949             row = i;
       
   950             break;
       
   951         }
       
   952     }
       
   953     Q_ASSERT(item);
       
   954 
       
   955     item->pUploading = NULL;
       
   956     QByteArray answer = reply->readAll();
       
   957     item->uploadReply = QString::fromUtf8(answer.data());
       
   958    // QMessageBox::information(this,"",item->uploadReply,0);
       
   959     filesTable->setCellWidget(row, vcProgress, NULL); // remove progress bar
       
   960     numUploads--;
       
   961 }
       
   962 
       
   963 void PageVideos::uploadToYouTube()
       
   964 {
       
   965     int row = filesTable->currentRow();
       
   966     VideoItem * item = nameItem(row);
       
   967 
       
   968     if (!netManager)
       
   969         netManager = new QNetworkAccessManager(this);
       
   970 
       
   971     HWUploadVideoDialog* dlg = new HWUploadVideoDialog(this, item->name, netManager);
       
   972     dlg->deleteLater();
       
   973     if (!dlg->exec())
       
   974         return;
       
   975 
       
   976     QNetworkRequest request(QUrl(dlg->location));
       
   977     request.setRawHeader("Content-Type", "application/octet-stream");
       
   978 
       
   979     QFile * file = new QFile(item->path(), this);
       
   980     if (!file->open(QIODevice::ReadOnly))
       
   981         return;
       
   982 
       
   983     // add progress bar
       
   984     QProgressBar * progressBar = new QProgressBar(filesTable);
       
   985     progressBar->setMinimum(0);
       
   986     progressBar->setMaximum(10000);
       
   987     progressBar->setValue(0);
       
   988     // make it different from encoding progress-bar
       
   989     progressBar->setStyleSheet("* {color: #00ccff; selection-background-color: #00ccff;}" );
       
   990     filesTable->setCellWidget(row, vcProgress, progressBar);
       
   991 
       
   992     QNetworkReply* reply = netManager->put(request, file);
       
   993     item->pUploading = reply;
       
   994     connect(reply, SIGNAL(uploadProgress(qint64, qint64)), this, SLOT(uploadProgress(qint64, qint64)));
       
   995     connect(reply, SIGNAL(finished()), this, SLOT(uploadFinished()));
       
   996     numUploads++;
       
   997 }