add saving system with sqlite

This commit is contained in:
2022-10-29 20:06:20 +02:00
parent 4c54265f89
commit 8ea4dc281e
9 changed files with 309 additions and 33 deletions

View File

@ -8,6 +8,8 @@ class Stroke {
double _miny = double.infinity;
double _maxy = double.negativeInfinity;
final int id;
@override
String toString() {
return 'Stroke{points: $points}';
@ -17,18 +19,22 @@ class Stroke {
points.add(point);
// update bounding rect
if (point.point.dx < _minx) _minx = point.point.dx;
if (point.point.dx > _maxx) _maxx = point.point.dx;
if (point.point.dy < _miny) _miny = point.point.dy;
if (point.point.dy > _maxy) _maxy = point.point.dy;
_updateBoundingRect(point);
}
void _updateBoundingRect(Point p) {
if (p.point.dx < _minx) _minx = p.point.dx;
if (p.point.dx > _maxx) _maxx = p.point.dx;
if (p.point.dy < _miny) _miny = p.point.dy;
if (p.point.dy > _maxy) _maxy = p.point.dy;
}
Rect getBoundingRect() {
return Rect.fromPoints(Offset(_minx, _miny), Offset(_maxx, _maxy));
}
Stroke.fromPoints(this.points);
Stroke();
Stroke.fromPoints(this.points, this.id);
Stroke(this.id);
}
class Point {

View File

@ -2,6 +2,7 @@ import 'dart:math';
import 'dart:ui';
import 'package:fluentui_system_icons/fluentui_system_icons.dart';
import 'package:flutter/material.dart';
import 'package:notes/savesystem/note_file.dart';
import 'my_painter.dart';
import 'paint_controller.dart';
import 'screen_document_mapping.dart';
@ -11,7 +12,10 @@ import '../tool_bar.dart';
/// Handles input events and draws canvas element
class DrawingPage extends StatefulWidget {
const DrawingPage({Key? key}) : super(key: key);
// path to the .dbnote file
final String filePath;
const DrawingPage({Key? key, required this.filePath}) : super(key: key);
@override
State<DrawingPage> createState() => _DrawingPageState();
@ -22,18 +26,29 @@ class _DrawingPageState extends State<DrawingPage> {
double basezoom = 1.0;
Offset offset = const Offset(.0, .0);
PaintController controller = PaintController();
late PaintController controller;
late NoteFile noteFile = NoteFile(widget.filePath);
@override
void initState() {
super.initState();
controller = PaintController(noteFile);
noteFile.init().then((value) => controller.loadStrokesFromFile());
// todo might be weird behaviour if used with short side
final screenWidth =
(window.physicalSize.longestSide / window.devicePixelRatio);
_calcNewPageOffset(const Offset(.0, .0), screenWidth - 45);
}
@override
void dispose() {
super.dispose();
noteFile.close();
}
@override
Widget build(BuildContext context) {
return Scaffold(

View File

@ -2,7 +2,9 @@ import 'dart:math';
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:notes/savesystem/line_loader.dart';
import '../savesystem/note_file.dart';
import 'document_types.dart';
import 'my_painter.dart';
@ -13,6 +15,10 @@ class PaintController extends ChangeNotifier {
List<Stroke> strokes = [];
final bool _allowDrawWithFinger = false;
PaintController(this.file);
final NoteFile file;
void changePen(Pen pen) {
activePen = pen;
notifyListeners();
@ -40,7 +46,8 @@ class PaintController extends ChangeNotifier {
return thickness;
}
void pointDownEvent(Offset offset, PointerDeviceKind pointer, double tilt) {
void pointDownEvent(
Offset offset, PointerDeviceKind pointer, double tilt) async {
if (_allowDrawWithFinger || pointer != PointerDeviceKind.touch) {
// todo line drawn on edge where line left page
if (!a4Page.contains(offset)) return;
@ -48,8 +55,12 @@ class PaintController extends ChangeNotifier {
// todo handle other pens
if (activePen != Pen.pen) return;
strokes
.add(Stroke.fromPoints([Point(offset, _calcTiltedWidth(3.0, tilt))]));
int strokeid = strokes.isNotEmpty ? strokes.last.id + 1 : 0;
strokes.add(Stroke.fromPoints(
[Point(offset, _calcTiltedWidth(3.0, tilt))],
strokes.isNotEmpty ? strokes.last.id + 1 : 0));
file.addStroke(strokeid);
notifyListeners();
}
}
@ -58,10 +69,11 @@ class PaintController extends ChangeNotifier {
if (activePen == Pen.eraser) return;
if (_allowDrawWithFinger || pointer != PointerDeviceKind.touch) {
if (strokes.last.points.length <= 1) {
final lastStroke = strokes.last;
if (lastStroke.points.length <= 1) {
// if the line consists only of one point (point) add endpoint as the same to allow drawing a line
// todo maybe solve this in custompainter in future
strokes.last.points.add(strokes.last.points.last);
lastStroke.points.add(lastStroke.points.last);
file.addPoint(lastStroke.id, lastStroke.points.last);
notifyListeners();
}
}
@ -83,6 +95,7 @@ class PaintController extends ChangeNotifier {
// check if eraser hit an point within its range
for (final pt in stroke.points) {
if (eraserrect.contains(pt.point)) {
file.removeStroke(stroke.id);
strokes.remove(stroke);
notifyListeners();
return;
@ -101,7 +114,9 @@ class PaintController extends ChangeNotifier {
pts.last, pts[pts.length - 2], newWidth);
}
strokes.last.addPoint(Point(offset, newWidth));
Point p = Point(offset, newWidth);
strokes.last.addPoint(p);
file.addPoint(strokes.last.id, p);
break;
case Pen.highlighter:
// TODO: Handle this case.
@ -113,4 +128,9 @@ class PaintController extends ChangeNotifier {
notifyListeners();
}
}
Future<void> loadStrokesFromFile() async {
strokes = await file.loadStrokes();
notifyListeners();
}
}