-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmainwidget.cpp
258 lines (216 loc) · 7.76 KB
/
mainwidget.cpp
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
/*
* scribble: Scribbling Application for Onyx Boox M92
*
* Copyright (C) 2012 peter-x
*
* 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 "mainwidget.h"
#include "filebrowser.h"
#include "fileio.h"
#include "onyx/screen/screen_proxy.h"
#include "onyx/screen/screen_update_watcher.h"
#include "onyx/ui/toolbar.h"
#include "onyx/ui/status_bar.h"
#include "onyx/ui/onyx_dialog.h"
#include "onyx/ui/thumbnail_view.h"
MainWidget::MainWidget(QWidget *parent) :
QWidget(parent, Qt::FramelessWindowHint), touchActive(true), currentFile("")
{
document = new ScribbleDocument(this);
scribbleArea = new ScribbleArea(this, document);
pressure_of_last_point_ = 0;
ui::OnyxToolBar *toolbar = new ui::OnyxToolBar(this);
/* TODO use action groups */
QAction *left = new QAction(QIcon(":/images/left_arrow.png"),
"previous page", this);
connect(left, SIGNAL(triggered()), document, SLOT(previousPage()));
toolbar->addAction(left);
QAction *pen = new QAction(QIcon(":images/sketch_mode_sketch.png"),
"pen", this);
connect(pen, SIGNAL(triggered()), document, SLOT(usePen()));
toolbar->addAction(pen);
QAction *eraser = new QAction(QIcon(":images/sketch_mode_erase.png"),
"eraser", this);
connect(eraser, SIGNAL(triggered()), document, SLOT(useEraser()));
toolbar->addAction(eraser);
/*
QAction *thin = new QAction(QIcon(":images/sketch_shape_1.png"),
"thin", this);
connect(thin, SIGNAL(triggered()), this, SLOT(sizeThin()));
toolbar->addAction(thin);
QAction *medium = new QAction(QIcon(":images/sketch_shape_3.png"),
"thin", this);
connect(medium, SIGNAL(triggered()), this, SLOT(sizeMedium()));
toolbar->addAction(medium);
QAction *thick = new QAction(QIcon(":images/sketch_shape_5.png"),
"thin", this);
connect(thick, SIGNAL(triggered()), this, SLOT(sizeThick()));
toolbar->addAction(thick);
*/
/* TODO icon at least looks like "open" */
QAction *open = new QAction(QIcon(":/images/config.png"),
"open document", this);
connect(open, SIGNAL(triggered()), SLOT(open()));
toolbar->addAction(open);
/*
QAction *save = new QAction(QIcon(":/images/save_document.png"),
"save as", this);
connect(save, SIGNAL(triggered()), SLOT(saveAs()));
toolbar->addAction(save);
*/
QAction *right = new QAction(QIcon(":/images/right_arrow.png"),
"next page", this);
connect(right, SIGNAL(triggered()), document, SLOT(nextPage()));
toolbar->addAction(right);
/* TODO need UI methods for:
* layer access
* loading and saving
* undo
* set page and layer
* remove page, insert page
* set background
* some settings
*/
statusBar = new ui::StatusBar(this, ui::MENU | ui::PROGRESS |
ui::BATTERY | ui::SCREEN_REFRESH |
ui::CLOCK);
QVBoxLayout *layout = new QVBoxLayout;
layout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(toolbar);
layout->addWidget(scribbleArea);
layout->addWidget(statusBar);
setLayout(layout);
onyx::screen::watcher().addWatcher(this);
asyncWriter = new AsyncWriter(this);
connect(document, SIGNAL(pageOrLayerNumberChanged(int,int,int,int)), SLOT(updateProgressBar(int,int,int,int)));
connect(scribbleArea, SIGNAL(resized(QSize)), document, SLOT(setViewSize(QSize)));
connect(statusBar, SIGNAL(progressClicked(int,int)), SLOT(setPage(int,int)));
connect(&touchListener, SIGNAL(touchData(TouchData &)), this, SLOT(touchEventDataReceived(TouchData &)));
QTimer *save_timer = new QTimer(this);
connect(save_timer, SIGNAL(timeout()), SLOT(saveAsynchronously()));
/* save every 5 seconds */
save_timer->start(5000);
}
void MainWidget::loadFile(const QFile &file)
{
save();
/* TODO error message */
QByteArray data = FileIO::readGZFileLocked(file);
if (document->loadXournalFile(data)) {
currentFile.setFileName(file.fileName());
}
}
void MainWidget::saveFile(const QFile &file)
{
FileIO::writeGZFileLocked(file, document->toXournalXMLFormat());
currentFile.setFileName(file.fileName());
}
void MainWidget::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Escape:
save();
qApp->exit();
case Qt::Key_Right:
case Qt::Key_PageDown:
document->nextPage();
break;
case Qt::Key_Left:
case Qt::Key_PageUp:
document->previousPage();
break;
default:
QWidget::keyPressEvent(event);
}
}
void MainWidget::touchEventDataReceived(const TouchData &data)
{
if (!touchActive) return;
const OnyxTouchPoint &touch_point = data.points[0];
QPoint pos = scribbleArea->mapFromGlobal(QPoint(touch_point.x, touch_point.y));
document->touchEventDataReceived(pos, data.points[0].pressure);
}
void MainWidget::mousePressEvent(QMouseEvent *ev)
{
TouchData data;
data.points[0].x = ev->globalX();
data.points[0].y = ev->globalY();
data.points[0].pressure = ev->buttons() & Qt::LeftButton;
touchEventDataReceived(data);
}
void MainWidget::mouseMoveEvent(QMouseEvent *ev)
{
TouchData data;
data.points[0].x = ev->globalX();
data.points[0].y = ev->globalY();
data.points[0].pressure = ev->buttons() & Qt::LeftButton;
touchEventDataReceived(data);
}
void MainWidget::mouseReleaseEvent(QMouseEvent *ev)
{
TouchData data;
data.points[0].x = ev->globalX();
data.points[0].y = ev->globalY();
data.points[0].pressure = 0;
touchEventDataReceived(data);
}
void MainWidget::open()
{
touchActive = false;
FileBrowser fileBrowser(this);
/* TODO save last path */
QString path = fileBrowser.showLoadFile(currentFile.fileName());
if (path.isEmpty()) {
touchActive = true;
return;
}
loadFile(QFile(path));
touchActive = true;
}
void MainWidget::save()
{
if (!currentFile.fileName().isEmpty()) {
asyncWriter->stopWriting();
/* save timeout cannot occur now since this is the same thread */
saveFile(currentFile);
}
}
void MainWidget::saveAs()
{
QString file = QFileDialog::getSaveFileName(this, "Save Scribble File",
currentFile.fileName(),
"Xournal Files (*.xoj);;All Files (*)");
if (!file.isEmpty()) {
saveFile(QFile(file));
}
/* TODO set current filename */
}
void MainWidget::updateProgressBar(int currentPage, int maxPages, int currentLayer, int maxLayers)
{
statusBar->setProgress(currentPage + 1, maxPages);
}
void MainWidget::setPage(int percentage, int page)
{
document->setCurrentPage(page - 1);
}
void MainWidget::saveAsynchronously()
{
if (!currentFile.fileName().isEmpty() && document->hasChangedSinceLastSave()) {
document->setSaved();
asyncWriter->writeData(document->getPagesCopy(), currentFile);
}
}