aboutsummaryrefslogtreecommitdiff
path: root/mainwindow.cc
blob: e269c619b818d1adcb7cb2c85da349a1b4781280 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <QtCore/QDebug>
#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))
{
	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();
	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();
	ui->notebookView->setNotebook(QString());
}

void MainWindow::openNotebook(const QString &pen, const QString &notebook)
{
	if (_curPenName == pen && _curNotebookName == notebook) return;

	_curPenName = pen;
	_curNotebookName = notebook;

	QString nbDir = _notebooks->notebookDirectory(_curPenName, _curNotebookName);

	qDebug() << "Opening notebook" << _curPenName << _curNotebookName << nbDir;

	ui->notebookView->setPaperReplay(_notebooks->paperReplayDirectory(_curPenName));
	ui->notebookView->setNotebook(nbDir);
}

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::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)
{
	qDebug() << "Media state change:" << 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);
	}
}