#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)) { ui->setupUi(this); 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); ui->replaySlider->setMediaObject(_media); ui->pauseButton->setVisible(false); connect(_media, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(handleMediaStateChange(Phonon::State))); } 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); if (_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::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::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; } break; case Phonon::PausedState: ui->playButton->setVisible(true); ui->pauseButton->setVisible(false); break; default: ui->playButton->setVisible(true); ui->pauseButton->setVisible(false); } }