From 2910de560ead3ff65db26292fc27e427a5cf9b5e Mon Sep 17 00:00:00 2001 From: Javier Date: Sun, 14 Jun 2015 05:18:53 +0200 Subject: add export feature and desktop file --- mainwindow.cc | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) (limited to 'mainwindow.cc') diff --git a/mainwindow.cc b/mainwindow.cc index dd687a2..0fdf38d 100644 --- a/mainwindow.cc +++ b/mainwindow.cc @@ -1,4 +1,7 @@ #include +#include +#include +#include #include "mainwindow.h" #include "ui_mainwindow.h" @@ -24,10 +27,15 @@ MainWindow::MainWindow(QWidget *parent) : ui->paperReplayView->horizontalHeader()->setResizeMode(1, QHeaderView::Fixed); ui->paperReplayView->setVisible(false); Phonon::createPath(_media, _mediaOutput); + _media->setTickInterval(500); ui->replaySlider->setMediaObject(_media); ui->pauseButton->setVisible(false); connect(_media, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(handleMediaStateChange(Phonon::State))); + connect(_media, SIGNAL(totalTimeChanged(qint64)), + this, SLOT(handleMediaTotalTimeChanged(qint64))); + connect(_media, SIGNAL(tick(qint64)), + this, SLOT(handleMediaTick(qint64))); } MainWindow::~MainWindow() @@ -73,6 +81,46 @@ void MainWindow::openNotebook(const QString &pen, const QString ¬ebook) } } +void MainWindow::exportCurrentPageAsPng(const QString &file) +{ + qDebug() << "Exporting current page" << ui->notebookView->curPage() << "to" << file; + QImage image = ui->notebookView->exportPage(ui->notebookView->curPage()); + if (!image.save(file, "PNG")) { + QMessageBox::warning(this, tr("Export page"), + tr("Could not export current page to '%s'").arg(file)); + } +} + +void MainWindow::exportCurrentPageAsSvg(const QString &file) +{ + QSvgGenerator svg; + svg.setFileName(file); + svg.setSize(ui->notebookView->getCurPageSize()); + svg.setViewBox(ui->notebookView->getCurPageTrim()); + svg.setTitle(_curNotebookName + " p." + QString::number(ui->notebookView->curPage())); + svg.setDescription("Page " + QString::number(ui->notebookView->curPage()) + " of " + _curNotebookName + " from " + _curPenName); + qDebug() << "Exporting current page" << ui->notebookView->curPage() << "to" << file; + QPainter painter; + painter.begin(&svg); + ui->notebookView->renderPage(&painter, ui->notebookView->curPage()); + painter.end(); +} + +void MainWindow::exportCurrentPaperReplayAsAac(const QString &file) +{ + QString src = _media->currentSource().fileName(); + if (src.isEmpty()) { + QMessageBox::warning(this, tr("Export audio"), + tr("No audio file is selected")); + return; + } + qDebug() << "Exporting current audio" << src << "to" << file; + if (!QFile::copy(src, file)) { + QMessageBox::warning(this, tr("Export audio"), + tr("Could not export current audio to '%s'").arg(file)); + } +} + void MainWindow::handleNotebookSelected(const QModelIndex &index) { if (!index.isValid()) { @@ -151,13 +199,100 @@ void MainWindow::handleMediaStateChange(Phonon::State state) _media->seek(_pendingSeek); _pendingSeek = 0; } + ui->mediaPosLabel->setText(formatDuration(_media->currentTime())); + ui->mediaLenLabel->setText("/ " + formatDuration(_media->totalTime())); break; case Phonon::PausedState: ui->playButton->setVisible(true); ui->pauseButton->setVisible(false); + ui->mediaPosLabel->setText(formatDuration(_media->currentTime())); + ui->mediaLenLabel->setText("/ " + formatDuration(_media->totalTime())); break; default: ui->playButton->setVisible(true); ui->pauseButton->setVisible(false); + ui->mediaPosLabel->setText(QString()); + ui->mediaLenLabel->setText(QString()); + break; + } +} + +void MainWindow::handleMediaTotalTimeChanged(qint64 time) +{ + ui->mediaLenLabel->setText("/ " + formatDuration(time)); +} + +void MainWindow::handleMediaTick(qint64 time) +{ + ui->mediaPosLabel->setText(formatDuration(time)); +} + +void MainWindow::handleExport() +{ + if (_curNotebookName == PAPER_REPLAY) { + QStringList filters; + filters << tr("Current audio as AAC (*.aac)"); + QString filter; + QString fileName = QFileDialog::getSaveFileName(this, tr("Export page"), QString(), + filters.join(";;"), &filter); + if (fileName.isEmpty()) return; + int filterIndex = filters.indexOf(filter); + switch (filterIndex) { + case 0: + if (!fileName.endsWith(".aac", Qt::CaseInsensitive)) { + fileName.append(".aac"); + } + exportCurrentPaperReplayAsAac(fileName); + break; + } + } else if (!_curNotebookName.isEmpty()) { + QStringList filters; + filters << tr("Current page as PNG image (*.png)") + << tr("Current page as SVG image (*.svg)") + << tr("Current audio as AAC (*.aac)"); + QString filter; + QString fileName = QFileDialog::getSaveFileName(this, tr("Export page"), QString(), + filters.join(";;"), &filter); + if (fileName.isEmpty()) return; + int filterIndex = filters.indexOf(filter); + switch (filterIndex) { + case 0: + if (!fileName.endsWith(".png", Qt::CaseInsensitive)) { + fileName.append(".png"); + } + exportCurrentPageAsPng(fileName); + break; + case 1: + if (!fileName.endsWith(".svg", Qt::CaseInsensitive)) { + fileName.append(".svg"); + } + exportCurrentPageAsSvg(fileName); + break; + case 2: + if (!fileName.endsWith(".aac", Qt::CaseInsensitive)) { + fileName.append(".aac"); + } + exportCurrentPaperReplayAsAac(fileName); + break; + } + } else { + QMessageBox::warning(this, tr("Export page"), + tr("Open a notebook or audio in order to export")); + } +} + +QString MainWindow::formatDuration(qint64 time) +{ + int secs = time / 1000; + int mins = secs / 60; + secs %= 60; + int hours = mins / 60; + mins %= 60; + + const QChar fill('0'); + if (hours) { + return QString("%1:%2:%3").arg(hours).arg(mins, 2, 10, fill).arg(secs, 2, 10, fill); + } else { + return QString("%2:%3").arg(mins).arg(secs, 2, 10, fill); } } -- cgit v1.2.3