aboutsummaryrefslogtreecommitdiff
path: root/mainwindow.cc
blob: dd687a2704c06462aed71d42983c2595730b1980 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#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)),
    _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 &notebook)
{
	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);
	}
}