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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
/*
* scribiu -- read notebooks and voice memos from Livescribe pens
* Copyright (C) 2015 Javier S. Pedro <javier@javispedro.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <QtCore/QDebug>
#include <QtCore/QTimer>
#include <QtCore/QSettings>
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QMessageBox>
#include <QtSvg/QSvgGenerator>
#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);
ui->notebookTree->setModel(_notebooks);
ui->notebookTree->header()->setSectionResizeMode(0, QHeaderView::Stretch);
ui->notebookTree->header()->setSectionResizeMode(1, QHeaderView::Fixed);
ui->notebookTree->header()->setSectionResizeMode(2, QHeaderView::Fixed);
ui->notebookTree->expandAll();
ui->paperReplayView->setModel(_replayModel);
ui->paperReplayView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
ui->paperReplayView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);
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->pane2Stack->setCurrentWidget(ui->notebookView);
ui->notebookView->setNotebook(QString());
}
void MainWindow::openNotebook(const QString &pen, const QString ¬ebook)
{
if (_curPenName == pen && _curNotebookName == notebook) return;
closeNotebook();
_curPenName = pen;
_curNotebookName = notebook;
Smartpen::PenTime userTime = _notebooks->penUserTime(pen);
if (_curNotebookName == PAPER_REPLAY) {
QString replayDir = _notebooks->paperReplayDirectory(_curPenName);
_replay->open(replayDir, PAPER_REPLAY_GUID, userTime);
_replayModel->refresh();
ui->pane2Stack->setCurrentWidget(ui->paperReplayView);
} else {
QString nbDir = _notebooks->notebookDirectory(_curPenName, _curNotebookName);
qDebug() << "Opening notebook" << _curPenName << _curNotebookName << nbDir;
ui->notebookView->setPenUserTime(userTime);
ui->notebookView->setPaperReplay(_notebooks->paperReplayDirectory(_curPenName));
ui->notebookView->setNotebook(nbDir);
ui->pane2Stack->setCurrentWidget(ui->notebookView);
}
}
void MainWindow::exportCurrentPageAsPng(const QString &file)
{
qDebug() << "Exporting current page" << ui->notebookView->curPage() << "to" << file;
QImage image = ui->notebookView->exportPageAsImage(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::exportCurrentPageAsTXYP(const QString &file, bool relativeTime)
{
QFile f(file);
if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) {
QMessageBox::warning(this, tr("Export page"),
tr("Could not export current page to '%s'").arg(file));
return;
}
ui->notebookView->exportPageAsTXYP(&f, ui->notebookView->curPage(), relativeTime);
f.close();
}
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;
}
// Get column 0, which corresponds to notebook name
QModelIndex nb = _notebooks->index(index.row(), 0, parent);
openNotebook(_notebooks->data(parent, NotebookModel::FileNameRole).toString(),
_notebooks->data(nb, NotebookModel::FileNameRole).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) {
qDebug() << "requesting media " << filePath;
_media->setCurrentSource(QUrl::fromLocalFile(filePath));
}
qDebug() << "requesting media seek to" << time << "/" << _media->totalTime();
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) {
qDebug() << "requesting (pending) media seek to" << _pendingSeek << "/" << _media->totalTime();
_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()
{
QSettings settings;
if (_curNotebookName == PAPER_REPLAY) {
settings.beginGroup("export");
settings.beginGroup("audio");
QString dir = settings.value("dir").toString();
QStringList filters;
filters << tr("Current audio as AAC (*.aac)");
QString filter;
QString fileName = QFileDialog::getSaveFileName(this, tr("Export page"), dir,
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;
default:
Q_UNREACHABLE();
}
QFileInfo file(fileName);
settings.setValue("dir", file.absolutePath());
} else if (!_curNotebookName.isEmpty()) {
settings.beginGroup("export");
settings.beginGroup("page");
QString dir = settings.value("dir").toString();
QStringList filters;
filters << tr("Current page as PNG image (*.png)")
<< tr("Current page as SVG image (*.svg)")
<< tr("Current page as TXYP (*.txyp)")
<< tr("Current audio as AAC (*.aac)");
int filterIndex = settings.value("filetype").toInt();
QString filter = filters.value(filterIndex);
QString fileName = QFileDialog::getSaveFileName(this, tr("Export page"), dir,
filters.join(";;"), &filter);
if (fileName.isEmpty()) return;
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(".txyp", Qt::CaseInsensitive)) {
fileName.append(".txyp");
}
exportCurrentPageAsTXYP(fileName, settings.value("txyp_relative_t", true).toBool());
break;
case 3:
if (!fileName.endsWith(".aac", Qt::CaseInsensitive)) {
fileName.append(".aac");
}
exportCurrentPaperReplayAsAac(fileName);
break;
default:
Q_UNREACHABLE();
}
QFileInfo file(fileName);
settings.setValue("dir", file.absolutePath());
settings.setValue("filetype", filterIndex);
} 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);
}
}
|