/* * scribiu -- read notebooks and voice memos from Livescribe pens * Copyright (C) 2015 Javier S. Pedro * * 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 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #include #include #include #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), _notebooks(new NotebookModel(this)), _manager(new SmartpenManager(this)), _media(new Phonon::MediaObject(this)), _mediaOutput(new Phonon::AudioOutput(this)), _replay(new PaperReplay(this)), _replayModel(new PaperReplayModel(_replay, this)), _statusLabel(new QLabel) { ui->setupUi(this); #if QT_VERSION < QT_VERSION_CHECK(5, 1, 0) // Some tricks for DPI support const qreal scale = logicalDpiX() / 96.0; if (scale > 1.1) { ui->notebookTree->header()->setDefaultSectionSize(ui->notebookTree->header()->defaultSectionSize() * scale); ui->notebookTree->setIconSize(QSize(16, 16) * scale); ui->prevButton->setMaximumSize(ui->prevButton->maximumSize() * scale); ui->nextButton->setMaximumSize(ui->nextButton->maximumSize() * scale); ui->exportButton->setMaximumSize(ui->exportButton->maximumSize() * scale); ui->playButton->setMaximumSize(ui->playButton->maximumSize() * scale); ui->pauseButton->setMaximumSize(ui->pauseButton->maximumSize() * scale); ui->playButton->setMaximumSize(ui->prevButton->maximumSize() * scale); ui->pageEdit->setMaximumWidth(ui->pageEdit->maximumWidth() * scale); } #endif ui->notebookTree->setModel(_notebooks); ui->notebookTree->header()->setResizeMode(0, QHeaderView::Stretch); ui->notebookTree->header()->setResizeMode(1, QHeaderView::Fixed); ui->notebookTree->header()->setResizeMode(2, QHeaderView::Fixed); ui->notebookTree->expandAll(); ui->notebookView->setVisible(false); ui->paperReplayView->setModel(_replayModel); ui->paperReplayView->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch); 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); ui->statusBar->addWidget(_statusLabel, 1); connect(_notebooks, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(handleNotebookRowsInserted(QModelIndex,int,int))); connect(_manager, SIGNAL(pensBeingSynchronizedChanged()), this, SLOT(handlePensBeingSynchronizedChanged())); connect(_manager, SIGNAL(syncComplete(QString)), this, SLOT(handlePenSyncComplete(QString))); connect(_manager, SIGNAL(syncFailed(QString)), this, SLOT(handlePenSyncFailed(QString))); 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))); QSettings settings; settings.beginGroup("mainwindow"); restoreGeometry(settings.value("geometry").toByteArray()); restoreState(settings.value("state").toByteArray()); ui->splitter->restoreState(settings.value("splitter").toByteArray()); settings.endGroup(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::closeNotebook() { _curPenName.clear(); _curNotebookName.clear(); _replay->close(); ui->notebookView->setNotebook(QString()); ui->notebookView->setVisible(false); ui->paperReplayView->setVisible(false); } void MainWindow::openNotebook(const QString &pen, const QString ¬ebook) { if (_curPenName == pen && _curNotebookName == notebook) return; closeNotebook(); _curPenName = pen; _curNotebookName = notebook; if (_curNotebookName == PAPER_REPLAY) { QString replayDir = _notebooks->paperReplayDirectory(_curPenName); _replay->open(replayDir, 0); _replayModel->refresh(); ui->paperReplayView->setVisible(true); } else { QString nbDir = _notebooks->notebookDirectory(_curPenName, _curNotebookName); qDebug() << "Opening notebook" << _curPenName << _curNotebookName << nbDir; ui->notebookView->setPaperReplay(_notebooks->paperReplayDirectory(_curPenName)); ui->notebookView->setNotebook(nbDir); ui->notebookView->setVisible(true); } } 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()) { closeNotebook(); return; } QModelIndex parent = index.parent(); if (!parent.isValid()) { closeNotebook(); return; } QModelIndex child = parent.child(index.row(), 0); openNotebook(_notebooks->data(parent, Qt::DisplayRole).toString(), _notebooks->data(child, Qt::DisplayRole).toString()); } void MainWindow::handleNotebookRowsInserted(const QModelIndex &index, int start, int end) { Q_UNUSED(index); Q_UNUSED(start); Q_UNUSED(end); QTimer::singleShot(200, ui->notebookTree, SLOT(expandAll())); } void MainWindow::handleCurPageChanged() { ui->pageEdit->setText(QString::number(ui->notebookView->curPage() + 1)); } void MainWindow::handlePaperReplaySelected(const QModelIndex &index) { QString file = _replayModel->sessionFilename(index); handlePaperReplayRequested(file, 0); } void MainWindow::handlePaperReplayRequested(const QString &file, qint64 time) { QFileInfo finfo(file); if (!finfo.exists()) { qWarning() << "Cannot open paper replay media file:" << finfo.canonicalFilePath(); } QString filePath = finfo.canonicalFilePath(); if (_media->currentSource().fileName() != filePath) { _media->setCurrentSource(filePath); } switch (_media->state()) { case Phonon::PlayingState: case Phonon::BufferingState: case Phonon::PausedState: _pendingSeek = 0; _media->seek(time); break; default: _pendingSeek = time; break; } _media->play(); } void MainWindow::handlePaperReplayPlay() { _media->play(); } void MainWindow::handlePaperReplayPause() { _media->pause(); } void MainWindow::handleMediaStateChange(Phonon::State state) { switch (state) { case Phonon::PlayingState: ui->playButton->setVisible(false); ui->pauseButton->setVisible(true); if (_pendingSeek) { _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::handlePensBeingSynchronizedChanged() { QStringList pens = _manager->pensBeingSynchronized(); if (pens.isEmpty()) { _statusLabel->setText(QString()); } else { _statusLabel->setText(tr("Synchronizing %1...").arg(pens.join(", "))); } } void MainWindow::handlePenSyncComplete(const QString &penName) { ui->statusBar->showMessage(tr("Completed synchronization with %1").arg(penName), 10000); } void MainWindow::handlePenSyncFailed(const QString &penName) { ui->statusBar->showMessage(tr("Failed synchronization with %1").arg(penName), 10000); } 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")); } } void MainWindow::handleAbout() { QMessageBox::about(this, tr("About Scribiu"), tr("Read notebooks and audio notes from your Livescribe Echo pen")); } void MainWindow::closeEvent(QCloseEvent *event) { QSettings settings; Q_UNUSED(event); settings.beginGroup("mainwindow"); settings.setValue("geometry", saveGeometry()); settings.setValue("state", saveState()); settings.setValue("splitter", ui->splitter->saveState()); settings.endGroup(); } 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); } }