File indexing completed on 2024-04-21 03:49:48

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2013 Illya Kovalevskyy <illya.kovalevskyy@gmail.com>
0004 //
0005 
0006 #include "MovieCaptureDialog.h"
0007 #include "ui_MovieCaptureDialog.h"
0008 
0009 #include <QFileDialog>
0010 #include <QMessageBox>
0011 
0012 #include "MarbleWidget.h"
0013 #include "MovieCapture.h"
0014 
0015 namespace Marble {
0016 
0017 MovieCaptureDialog::MovieCaptureDialog(MarbleWidget *widget, QWidget *parent) :
0018     QDialog(parent),
0019     ui(new Ui::MovieCaptureDialog),
0020     m_recorder(new MovieCapture(widget, parent))
0021 {
0022     ui->setupUi(this);
0023     m_recorder->setSnapshotMethod(MovieCapture::TimeDriven);
0024     QPushButton *startButton = ui->buttonBox->addButton(tr("&Start", "Start recording a movie"), QDialogButtonBox::ActionRole);
0025 
0026     connect(ui->fpsSlider, SIGNAL(valueChanged(int)),
0027             ui->fpsSpin, SLOT(setValue(int)));
0028 
0029     connect(ui->fpsSpin, SIGNAL(valueChanged(int)),
0030             ui->fpsSlider, SLOT(setValue(int)));
0031 
0032     connect(ui->fpsSlider, SIGNAL(valueChanged(int)),
0033             m_recorder, SLOT(setFps(int)));
0034 
0035     connect(ui->buttonBox, SIGNAL(rejected()),
0036             this, SLOT(close()));
0037 
0038     connect(startButton, SIGNAL(clicked()),
0039             this, SLOT(startRecording()));
0040 
0041     connect(ui->openButton, SIGNAL(clicked()),
0042             this, SLOT(loadDestinationFile()));
0043 }
0044 
0045 MovieCaptureDialog::~MovieCaptureDialog()
0046 {
0047     delete ui;
0048 }
0049 
0050 void MovieCaptureDialog::loadDestinationFile()
0051 {
0052     const QVector<MovieFormat> formats = m_recorder->availableFormats();
0053     if( formats.isEmpty() ) {
0054         QMessageBox::warning( this, tr( "Codecs are unavailable" ), tr( "Supported codecs are not found." ) );
0055         return;
0056     }
0057     QString filter = formats.first().name() + QLatin1String(" (*.") + formats.first().extension() + QLatin1Char(')');
0058     for( int i = 1; i < formats.size(); i++ )
0059     {
0060         filter += QLatin1String(";;") + formats.at(i).name() + QLatin1String(" (*.") + formats.at(i).extension() + QLatin1Char(')');
0061     }
0062     const QString defaultFileName =
0063             ui->destinationEdit->text().isEmpty() ? "" : ui->destinationEdit->text();
0064 
0065     const QString destination =
0066             QFileDialog::getSaveFileName(this, tr("Save video file"), defaultFileName,
0067                                          filter );
0068 
0069     if (destination.isEmpty()) {
0070         return;
0071     }
0072 
0073     bool supported = false;
0074     for(const MovieFormat &format: formats) {
0075         if (destination.endsWith(QLatin1Char('.') + format.extension())) {
0076             supported = true;
0077             break;
0078         }
0079     }
0080 
0081     if (!supported) {
0082         QString formatsExtensions = QLatin1Char('.') + formats.at(0).extension();
0083         for( int i = 1; i < formats.size(); ++i )
0084         {
0085             formatsExtensions += QLatin1String(", .") + formats.at(i).extension();
0086         }
0087         QMessageBox::warning(this, tr("Filename is not valid"),
0088                              tr("This file format is not supported. "
0089                                 "Please, use %1 instead").arg( formatsExtensions ) );
0090         return;
0091     }
0092 
0093     ui->destinationEdit->setText(destination);
0094     m_recorder->setFilename(destination);
0095 }
0096 
0097 void MovieCaptureDialog::startRecording()
0098 {
0099     const QString path = ui->destinationEdit->text();
0100 
0101     if (path.isEmpty()) {
0102         QMessageBox::warning(this, tr("Missing filename"),
0103                              tr("Destination video file is not set. "
0104                                 "I don't know where to save recorded "
0105                                 "video. Please, specify one."));
0106         return;
0107     }
0108 
0109     hide();
0110 
0111     m_recorder->setFps(ui->fpsSlider->value());
0112     if (m_recorder->startRecording())
0113         emit started();
0114 }
0115 
0116 void MovieCaptureDialog::stopRecording()
0117 {
0118     m_recorder->stopRecording();
0119 }
0120 
0121 } // namespace Marble
0122 
0123 #include "moc_MovieCaptureDialog.cpp"