-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvaswidget.cpp
executable file
·994 lines (845 loc) · 34.7 KB
/
canvaswidget.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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
/* This file is part of Misli.
Misli 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.
Misli 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 Misli. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QInputDialog>
#include <QMessageBox>
#include <QAction>
#include "canvaswidget.h"
#include "misli_desktop/misliwindow.h"
#include "notessearch.h"
#include "ui_misliwindow.h"
CanvasWidget::CanvasWidget(MisliWindow *misliWindow_, NoteFile *nf) :
detailsMenu(tr("Details"),this)
{
hide();
//Random
misliWindow = misliWindow_;
linkOnControlPointDrag = nullptr;
cpChangeNote = nullptr;
PushLeft = false;
timedOutMove = false;
moveOn = false;
noteResizeOn = false;
userIsDraggingStuff = false;
contextMenu = new QMenu(this);
infoLabel = new QLabel(this);
infoLabel->setFont(QFont("Helvetica", 15));
//Setting the timer for the move_func
move_func_timeout = new QTimer(this);
move_func_timeout->setSingleShot(1);
connect(move_func_timeout, SIGNAL(timeout()), this, SLOT(startMove()));
setMouseTracking(1);
setFocusPolicy(Qt::ClickFocus);
setAcceptDrops(true);
//Prepare per tag filter actions for the context menu
per_tag_filter_menu.setTitle("Filter per tag (hacky)");
for(auto tag: misliWindow->misliLibrary()->filter_menu_tags){
QAction *action = new QAction(tag);
action->setCheckable(true);
action->setChecked(true);
per_tag_filter_menu.addAction(action);
connect(action, SIGNAL(triggered()), this, SLOT(update));
}
// Set notefile
setNoteFile(nf);
}
CanvasWidget::~CanvasWidget()
{
delete contextMenu;
delete infoLabel;
delete move_func_timeout;
}
QPointF CanvasWidget::project(QPointF point)
{
return QPointF(projectX(point.x()),projectY(point.y()));
}
QLineF CanvasWidget::project(QLineF line)
{
return QLineF(project(line.p1()),project(line.p2()));
}
QRectF CanvasWidget::project(QRectF rect)
{
return QRectF(project(rect.topLeft()),project(rect.bottomRight()));
}
void CanvasWidget::project(double realX, double realY, double &screenX, double &screenY)
{
screenX = projectX(realX);
screenY = projectY(realY);
}
void CanvasWidget::unproject(double screenX, double screenY, double &realX, double &realY)
{
realX = unprojectX(screenX);
realY = unprojectY(screenY);
}
QPointF CanvasWidget::unproject(QPointF point)
{
return QPointF(unprojectX(point.x()),unprojectY(point.y()));
}
double CanvasWidget::projectX(double realX)
{
realX -= noteFile()->eyeX;
realX = realX/noteFile()->eyeZ; //The heigher we look from - the smaller the note
realX = realX*1000; //More readable note sizes (and not to break compatability, this section was changed)
return realX + double(width())/2;
}
double CanvasWidget::projectY(double realY)
{
realY -= noteFile()->eyeY;
realY = realY/noteFile()->eyeZ;
realY = realY*1000;
return realY + double(height())/2;
}
double CanvasWidget::unprojectX(double screenX)
{
screenX -= double(width())/2;
screenX = screenX/1000;
screenX = screenX*noteFile()->eyeZ;
return screenX + noteFile()->eyeX;
}
double CanvasWidget::unprojectY(double screenY)
{
screenY -= double(height())/2;
screenY = screenY/1000;
screenY = screenY*noteFile()->eyeZ;
return screenY + noteFile()->eyeY;
}
double CanvasWidget::heightScaleFactor()
{
return 1000/noteFile()->eyeZ;
}
void CanvasWidget::paintEvent(QPaintEvent*)
{
QTime paintStartTime = QTime::currentTime();
if(noteFile()==nullptr) return;
//Init the painter
QPainter painter;
QPen pen;
pen.setCosmetic(true);
if(!painter.begin((this))){
qDebug()<<"[Canvas::Canvas]Failed to initialize painter.";
}
painter.setPen(pen);
painter.setRenderHint(QPainter::Antialiasing); //better antialiasing
painter.setRenderHint(QPainter::TextAntialiasing);
painter.fillRect(0,0,width(),height(),QColor(255,255,255,255)); //clear background
//==========Check for some special conditions================
if(linkingIsOn){ //If we're making a link
infoLabel->setText(tr("Click on a note to link to it."));
infoLabel->show();
}else if(noteFile()==misliWindow->helpNoteFile){
infoLabel->setText(tr("This is the help note file."));
infoLabel->show();
painter.fillRect(0,0,width(),height(),QColor(255,255,240,255)); //clear background
}else if(moveOn){
infoLabel->setText(tr("Moving note"));
infoLabel->show();
}else if(userIsDraggingStuff){
if(draggedStuffIsValid){
painter.fillRect(0,0,width(),height(),QColor(240,255,240,255)); //greenish background
}else{
painter.fillRect(0,0,width(),height(),QColor(255,240,240,255)); //redish background
}
}else{
infoLabel->hide();
}
//Setup the transformation matrix
QTransform viewpointTransform;
viewpointTransform.scale(heightScaleFactor(),heightScaleFactor());
viewpointTransform.translate(width()/heightScaleFactor()/2 - noteFile()->eyeX,
height()/heightScaleFactor()/2 - noteFile()->eyeY);
painter.setTransform(viewpointTransform);
QRectF windowFrame;
windowFrame.setSize( QSizeF(width()/heightScaleFactor(), height()/heightScaleFactor()) );
windowFrame.moveCenter(QPointF(noteFile()->eyeX,noteFile()->eyeY));
// Allowed tags
QStringList allowedTags;
for(auto action: per_tag_filter_menu.actions()){
if(!action->isChecked()){
allowedTags.append(action->text());
}
}
//=============Start painting===========================
int displayed_notes = 0;
for(Note* nt: noteFile()->notes){
QColor circleColor = nt->backgroundColor();
circleColor.setAlpha(60);//same as BG but transparent
//Draw the note (only if it's on screen (to avoid lag) )
if(!nt->rect().intersects(windowFrame)){
continue;
}
bool hideNote = false;
for(auto tag: nt->tags){
if(allowedTags.indexOf(tag) != -1){
hideNote = true;
}
}
if(hideNote) continue;
displayed_notes++;
//Check the validity of the address string
if(nt->type == NoteType::redirecting){
if(misliWindow->misliLibrary()->noteFileByName(nt->addressString) == nullptr &&
nt->textForShortening!=tr("Missing note file") )
{
nt->textForShortening = tr("Missing note file");
}
}
//Handle autoSize requests
if(nt->requestAutoSize){
nt->requestAutoSize = false;
nt->autoSize(painter);
noteFile()->save();
}
//Draw the actual note
nt->drawNote(painter);
//Wash out some notes to visualize tags if tags view is activated
if(misliWindow->ui->actionToggle_tags_view->isChecked()){
if(!nt->tags.contains(misliWindow->ui->tagTextLineEdit->text())){
painter.fillRect(nt->rect(), QBrush(QColor(255,255,255,128), Qt::SolidPattern));
}
}
//Draw additional lines to help alignment
if( (moveOn | noteResizeOn) && nt->isSelected_m){
double x = nt->rect().x();
double y = nt->rect().y();
double rectX = nt->rect().right(); //coordinates of the rectangle encapsulating the note
double rectY = nt->rect().bottom();
QLineF leftLine(x,y-ALIGNMENT_LINE_LENGTH,x,rectY+ALIGNMENT_LINE_LENGTH);
QLineF rightLine(rectX,y-ALIGNMENT_LINE_LENGTH,rectX,rectY+ALIGNMENT_LINE_LENGTH);
QLineF bottomLine(x-ALIGNMENT_LINE_LENGTH,rectY,rectX+ALIGNMENT_LINE_LENGTH,rectY);
QLineF topLine(x-ALIGNMENT_LINE_LENGTH,y,rectX+ALIGNMENT_LINE_LENGTH,y);
//Set the color
pen.setColor(nt->textColor());
painter.setPen( pen );
painter.drawLine(leftLine);
painter.drawLine(rightLine);
painter.drawLine(bottomLine);
painter.drawLine(topLine);
}
//If it's selected - draw the resize circle
if(nt->isSelected_m){
painter.setBrush(circleColor);
painter.drawEllipse(nt->rect().bottomRight(),RESIZE_CIRCLE_RADIUS,RESIZE_CIRCLE_RADIUS);
if(linkingIsOn){
Link ln(0);
ln.line.setPoints(nt->rect().center(), unproject(mousePos()));
nt->drawLink(painter, ln);
}
}
} //next note
//Draw all the links. It's not much compute time and when a note goes offscreen its links should still be visible
for(Note* nt: noteFile()->notes){
//Draw links
for(Link &ln: nt->outlinks){
nt->drawLink(painter, ln);
} //next link
}
//Show the shadows of stuff that's about to be pasted when ctrl is pressed
if( misliWindow->misliDesktopGUI->queryKeyboardModifiers() & Qt::ControlModifier ){
ctrlUpdateHack = false;
NoteFile *clipboardNF = misliWindow->clipboardNoteFile;
double x,y;
x = mapFromGlobal(cursor().pos()).x(); //get mouse screen coords
y = mapFromGlobal(cursor().pos()).y();
unproject(x,y,x,y); //translate them to canvas coords
clipboardNF->makeCoordsRelativeTo(-x,-y);
for(Note *nt: clipboardNF->notes){
pen.setColor(nt->textColor());
painter.setPen(pen);
painter.setBrush(Qt::NoBrush);
painter.drawRect(nt->rect());
}
clipboardNF->makeCoordsRelativeTo(x,y);
}
//If there's no note on the screen - show the JumpToNearestNoteButton
if( (displayed_notes==0) && !noteFile()->notes.isEmpty() ) {
misliWindow->ui->jumpToNearestNotePushButton->show();
}else{
misliWindow->ui->jumpToNearestNotePushButton->hide();
}
qDebug() << "Painter latency: " << QTime::currentTime().msecsTo( paintStartTime);
}
void CanvasWidget::mouseDoubleClickEvent(QMouseEvent *event)
{
if(noteFile()==nullptr) return;
//Don't accept doubleClick on modifiers, because it ruin a selection
if( (event->button()==Qt::LeftButton) &&
!(misliWindow->misliDesktopGUI->keyboardModifiers()==Qt::ControlModifier) &&
!(misliWindow->misliDesktopGUI->keyboardModifiers()==Qt::ShiftModifier) ){
doubleClick();
}
}
void CanvasWidget::wheelEvent(QWheelEvent *event)
{
if(noteFile()==nullptr) return;
int numDegrees = event->delta() / 8;
int numSteps = numDegrees / 15;
noteFile()->eyeZ -= MOVE_SPEED * numSteps;
noteFile()->eyeZ = std::max(1.0, std::min(noteFile()->eyeZ, 1000.0));
update();
//glutPostRedisplay(); artefact, thank you for siteseeing
}
void CanvasWidget::mouseMoveEvent(QMouseEvent *event)
{
if(noteFile() == nullptr) return;
if(PushLeft){
if(moveOn){
QPointF realPos( unproject(event->pos()) ), realPosOnPush( unproject(QPointF(XonPush, YonPush))), deltaRealPos, newPos;
deltaRealPos = realPos - realPosOnPush;
for(Note *nt: noteFile()->notes){
if(nt->isSelected()){
newPos = nt->posBeforeMove + deltaRealPos;
QRectF newRect(nt->rect());
newRect.moveTopLeft(newPos);
nt->setRect( newRect );
for(Link &ln: nt->outlinks){
ln.usesControlPoint = false;
ln.controlPointIsSet = false;
}
noteFile()->arrangeLinksGeometry();
}
}
}else if(noteResizeOn){
for(Note *nt:noteFile()->notes){
if(nt->isSelected_m){
double d_x, d_y, realX, realY;
unproject(event->x(),event->y(),realX,realY);
d_x = realX - resizeX;
d_y = realY - resizeY;
QRectF newRect( nt->rect());
newRect.setSize( QSizeF(d_x, d_y) );
nt->setRect( newRect );
noteFile()->arrangeLinksGeometry();
}
}
}else if(linkOnControlPointDrag!=nullptr){ //We're changing a control point
linkOnControlPointDrag->controlPoint = unproject(mousePos());
linkOnControlPointDrag->isSelected = true;
linkOnControlPointDrag->controlPointIsChanged = true;
noteFile()->arrangeLinksGeometry(cpChangeNote);
update();
}else{ //Else we're moving about the canvas (respectively - changing the camera position)
double xrel=event->x()-XonPush,yrel=event->y()-YonPush;
noteFile()->eyeX = EyeXOnPush - xrel*noteFile()->eyeZ/1000; // eyez/1000 is the transform factor practically
noteFile()->eyeY = EyeYOnPush - yrel*noteFile()->eyeZ/1000;
update();
}
}else if(linkingIsOn){
update();
}
if(misliWindow->misliDesktopGUI->keyboardModifiers() == Qt::ControlModifier){
ctrlUpdateHack = true;
update(); //while showing the shadows of the notes for pasting
}
}
void CanvasWidget::dragEnterEvent(QDragEnterEvent *event)
{
//Pretty much if there's items of the types we accept - we acknowledge the drop
//but if they are invalid - we indicate that. Before the drop - with a redish canvas.
//And on it - with a specific message
draggedStuffIsValid = mimeDataIsCompatible(event->mimeData());
if(event->mimeData()->hasUrls() |
event->mimeData()->hasImage() |
event->mimeData()->hasText())
{
userIsDraggingStuff = true;
event->acceptProposedAction();
update();
}
}
void CanvasWidget::dragLeaveEvent(QDragLeaveEvent *)
{
userIsDraggingStuff = false;
update();
}
void CanvasWidget::dropEvent(QDropEvent *event)
{
pasteMimeData(event->mimeData());
userIsDraggingStuff = false;
update();
}
void CanvasWidget::doubleClick()
{
Note *nt = getNoteUnderMouse(mousePos().x(),mousePos().y());
if(nt!=nullptr){ //if we've clicked on a note
if(nt->type == NoteType::redirecting){ //if it's a valid redirecting note
if(misliWindow->misliLibrary()->noteFileByName(nt->addressString)!=nullptr)
setNoteFile(misliWindow->misliLibrary()->noteFileByName(nt->addressString));
}else if(nt->type == NoteType::textFile){
QDesktopServices::openUrl(QUrl("file://"+nt->addressString, QUrl::TolerantMode));
}else if(nt->type == NoteType::systemCall){
QProcess process;
//Run the command and get the output
process.setWorkingDirectory(library()->folderPath);
process.start(nt->addressString);
process.waitForFinished(-1);
QByteArray out = process.readAllStandardOutput();
QByteArray err = process.readAllStandardError();
QString cwd = QDir::currentPath();
if(!out.isEmpty()){
//Put the feedback in a note below the command
Note *newNote = new Note(noteFile()->getNewId(), QString(out + err));
newNote->setRect(QRectF(nt->rect().x(), nt->rect().bottom() + 1, 1, 1));
newNote->timeMade = QDateTime::currentDateTime();
newNote->timeModified = QDateTime::currentDateTime();
newNote->textColor_m = nt->textColor();
newNote->backgroundColor_m = nt->backgroundColor();
newNote->requestAutoSize = true;
noteFile()->addNote(newNote);
}
}else if(nt->type==NoteType::webPage){
QDesktopServices::openUrl(QUrl(nt->addressString, QUrl::TolerantMode));
}else{ //Edit that note
noteFile()->clearNoteSelection();
nt->setSelected(true);
misliWindow->edit_w->editNote();
}
}
else {//if not on note
misliWindow->edit_w->newNote();
}
}
void CanvasWidget::handleMousePress(Qt::MouseButton button)
{
if(noteFile()==nullptr) return;
Note *noteUnderMouse, *noteForResize;
Link *linkUnderMouse;
bool ctrl_is_pressed,shift_is_pressed;
int x = mousePos().x();
int y = mousePos().y();
update();//don't put at the end , return get's called earlier in some cases
if(misliWindow->misliDesktopGUI->keyboardModifiers()==Qt::ControlModifier){
ctrl_is_pressed=true;
}else ctrl_is_pressed=false;
if(misliWindow->misliDesktopGUI->keyboardModifiers()==Qt::ShiftModifier){
shift_is_pressed=true;
}else shift_is_pressed=false;
if (button==Qt::LeftButton) { //on left button
//----Save of global variables for other functions-----
XonPush = x;
YonPush = y;
PushLeft = true;
EyeXOnPush = noteFile()->eyeX;
EyeYOnPush = noteFile()->eyeY;
timedOutMove = false;
if (linkingIsOn) { // ------------LINKING ON KEY----------
noteUnderMouse = getNoteUnderMouse(x,y);
linkingIsOn = false;
if(noteUnderMouse!=nullptr){
if( noteFile()->linkSelectedNotesTo(noteUnderMouse)>0 ){
noteFile()->save();
}
}
update();
return;
}
// -----------RESIZE---------------
noteForResize=getNoteClickedForResize(x,y);
if( !(ctrl_is_pressed | shift_is_pressed) ) { //if neither ctrl or shift are pressed resize only the note under mouse
noteFile()->clearNoteSelection();
}
if (noteForResize!=nullptr){
noteForResize->setSelected(true);
noteResizeOn=true;
resizeX=noteForResize->rect().x();
resizeY=noteForResize->rect().y();
QCursor::setPos( mapToGlobal( project(noteForResize->rect().bottomRight()).toPoint() ) );
return;
}
//---------DRAG LINK CONTROL POINT---------------
linkOnControlPointDrag = getControlPointUnderMouse(mousePos().x(),mousePos().y());
if(linkOnControlPointDrag!=nullptr){
linkOnControlPointDrag->isSelected = true;
if(!linkOnControlPointDrag->usesControlPoint){
linkOnControlPointDrag->controlPoint = unproject(mousePos());
}
}
update();
//----------SELECT------------
//Clear selection if there are no modifiers
if( !(ctrl_is_pressed | shift_is_pressed) ) {
noteFile()->clearNoteSelection();
noteFile()->clearLinkSelection();
}
//Select links
linkUnderMouse = getLinkUnderMouse(x,y);
if (linkUnderMouse!=nullptr){
linkUnderMouse->isSelected = true;
}
//Select notes
noteUnderMouse = getNoteUnderMouse(x,y);
if (noteUnderMouse!=nullptr){
if(noteUnderMouse->isSelected()){
noteUnderMouse->setSelected(false);
}else {
if(noteUnderMouse->type == NoteType::textFile){
noteUnderMouse->checkTextForFileDefinition();
}
noteUnderMouse->setSelected(true);
}
distanceToPrimeNoteX = x-noteUnderMouse->rect().x();
distanceToPrimeNoteY = y-noteUnderMouse->rect().y();
if(shift_is_pressed){
//Select all notes this one links to
noteUnderMouse->setSelected(true);
for(Link ln: noteUnderMouse->outlinks){
noteFile()->getNoteById(ln.id)->setSelected(true);
}
}
timedOutMove = true;
move_func_timeout->start(MOVE_FUNC_TIMEOUT);
}
}else if(button==Qt::RightButton){
noteUnderMouse = getNoteUnderMouse(x,y);
contextMenu->clear();
if(noteUnderMouse!=nullptr){
noteUnderMouse->setSelected(true);
contextMenu->addAction(misliWindow->ui->actionEdit_note);
contextMenu->addAction(misliWindow->ui->actionDelete_selected);
contextMenu->addAction(misliWindow->ui->actionMake_link);
contextMenu->addSeparator();
detailsMenu.clear();
detailsMenu.addAction("Date created:"+noteUnderMouse->timeMade.toString("d.M.yyyy H:m:s"));
detailsMenu.addAction("Date modified:"+noteUnderMouse->timeModified.toString("d.M.yyyy H:m:s"));
contextMenu->addMenu(&detailsMenu);
}else{
contextMenu->addAction(misliWindow->ui->actionNew_note);
}
contextMenu->addMenu(misliWindow->ui->menuSwitch_to_another_note_file);
contextMenu->addSeparator();
contextMenu->addAction(misliWindow->ui->actionCopy);
contextMenu->addAction(misliWindow->ui->actionPaste);
contextMenu->addAction(misliWindow->ui->actionCreate_note_from_the_clipboard_text);
contextMenu->addMenu(&per_tag_filter_menu);
contextMenu->popup(cursor().pos());
}
}
void CanvasWidget::handleMouseRelease(Qt::MouseButton button)
{
if(noteFile()==nullptr) return;
if(button==Qt::LeftButton){
PushLeft = false;
if(moveOn){
moveOn = false;
noteFile()->save(); //saving the new positions
noteFile()->arrangeLinksGeometry();
}else if(noteResizeOn){
noteResizeOn = false;
noteFile()->save(); //save the new size
noteFile()->arrangeLinksGeometry();
}else if(linkOnControlPointDrag!=nullptr){
linkOnControlPointDrag = nullptr;
noteFile()->save(); //save the new controlPoint position
update();
}
}else if(button==Qt::MiddleButton){
Note *nt = getNoteUnderMouse(mousePos().x(), mousePos().y());
if( nt == nullptr ){
return;
}
if(nt->type == NoteType::redirecting){
NoteFile *nfUnderMouse = misliWindow->misliLibrary()->noteFileByName(nt->addressString);
if(nfUnderMouse != nullptr){
misliWindow->openNoteFileInNewTab(nfUnderMouse);
}
}
}
}
Note *CanvasWidget::getNoteUnderMouse(int mouseX, int mouseY)
{
for(Note *nt: noteFile()->notes){
if( project(nt->rect()).contains(QPointF(mouseX,mouseY))){
return nt;
}
}
return nullptr;
}
Note *CanvasWidget::getNoteClickedForResize(int mouseX , int mouseY)
{
for(Note *nt: noteFile()->notes){
if(QLineF(unproject(QPointF(mouseX,mouseY)),nt->rect().bottomRight()).length()<=RESIZE_CIRCLE_RADIUS){
//if(dottodot(unprojectX(mouseX),unprojectY(mouseY),nt->rect().right(),nt->rect().bottom())<=RESIZE_CIRCLE_RADIUS){
return nt;
}
}
return nullptr;
}
Link *CanvasWidget::getLinkUnderMouse(int mouseX,int mouseY) //returns one link (not necesserily the top one) onder the mouse
{
QRectF mouseSelectionRect(0,0,CLICK_RADIUS,CLICK_RADIUS); //not perfect, but works
mouseSelectionRect.moveCenter(unproject(QPointF(mouseX,mouseY)));
for(Note *nt: noteFile()->notes){
for(Link &ln: nt->outlinks){
if(ln.path.translated(ln.line.p1()).intersects(mouseSelectionRect)){
return &ln;
}
}
}
return nullptr;
}
Link *CanvasWidget::getControlPointUnderMouse(int x, int y)
{
for(Note *nt: noteFile()->notes){
for(Link &ln: nt->outlinks){
if(ln.usesControlPoint){
if(QLineF(unproject(QPointF(x,y)),ln.controlPoint).length()<=RESIZE_CIRCLE_RADIUS){
cpChangeNote = nt;
return &ln;
}
}else{
if(QLineF(unproject(QPointF(x,y)),ln.middleOfTheLine()).length()<=RESIZE_CIRCLE_RADIUS){
cpChangeNote = nt;
return &ln;
}
}
}
}
cpChangeNote = nullptr;
return nullptr;
}
void CanvasWidget::startMove(){ //if the mouse hasn't moved and time_out_move is not off the move flag is set to true
qreal x = mousePos().x();
qreal y = mousePos().y();
qreal dist = QLineF(XonPush, YonPush, x, y).length();
if( (timedOutMove && PushLeft ) && ( dist<MOVE_FUNC_TOLERANCE ) ){
getNoteUnderMouse(x, y)->setSelected(true); //to pickup a selected note with control pressed (not to deselect it)
//Store all the coordinates before the move
for(Note *nt: currentNoteFile->notes){
if(nt->isSelected()) nt->posBeforeMove = nt->rect().topLeft();
}
moveOn = true;
update();
}
timedOutMove = false;
}
QString CanvasWidget::copySelectedNotes(NoteFile *sourceNotefile, NoteFile *targetNoteFile)
{
QString clipboardText;
//Copy selected notes (only the info that would be in the file)
for(Note *nt_in_source: sourceNotefile->notes){
if(nt_in_source->isSelected()){
Note *nt_in_target = targetNoteFile->cloneNote(nt_in_source);
//Add the links
nt_in_target->outlinks = nt_in_source->outlinks;
//Add the note text to the regular clipboard
clipboardText += nt_in_source->text_m;
clipboardText += "\n\n"; //leave space before the next (gets trimmed in the end)
}
}
return clipboardText;
}
void CanvasWidget::paste()
{
NoteFile *clipboardNF = misliWindow->clipboardNoteFile;
int old_id;
clipboardNF->makeAllIDsNegative();
//Make coordinates relative to the mouse
double x = mousePos().x(); //get mouse screen coords
double y = mousePos().y();
unproject(x,y,x,y); //translate them to canvas coords
clipboardNF->makeCoordsRelativeTo(-x,-y);
//Copy the notes over to the target
clipboardNF->selectAllNotes();
copySelectedNotes(clipboardNF, noteFile());
//return clipboard notes' coordinates to 0
clipboardNF->makeCoordsRelativeTo(x,y);
//Replace the negative id-s with real ones
for(Note *nt: noteFile()->notes){
if(nt->id<0){ //only for the pasted notes (with negative id-s)
old_id = nt->id;
nt->id = noteFile()->getNewId();
//Now check ALL of the links in the NF for that id and change them
for(Note *nt2: noteFile()->notes){
for(Link &ln: nt2->outlinks){
if(ln.id==old_id){ ln.id=nt->id ; } //if it has the old id - set it up with the new one
}
}
}
}
noteFile()->arrangeLinksGeometry();
clipboardNF->makeAllIDsNegative(); //restore ids to positive in the clipboard
noteFile()->save();
}
void CanvasWidget::jumpToNearestNote()
{
Note *nearest_note = nullptr;
double best_result = 0, result,x_unprojected, y_unprojected;
unproject(mousePos().x(),mousePos().y(),x_unprojected,y_unprojected);
int iter=0;
for(Note *nt: noteFile()->notes){
result = QLineF(unproject(mousePos()), nt->rect().topLeft()).length();
if( (result<best_result) | (iter==0) ){
nearest_note = nt;
best_result = result;
}
iter++;
}
if(iter>0){
centerEyeOnNote(nearest_note);
}
}
NoteFile* CanvasWidget::noteFile()
{
return currentNoteFile;
}
void CanvasWidget::setNoteFile(NoteFile *newNoteFile) //This function has to not care what happens to the last NF
//Else we need no know the misliDir of the last one
{
if( newNoteFile == currentNoteFile ){
return;
}
disconnect(visualChangeConnection);
if(newNoteFile == nullptr){
if(!library()->noteFiles().isEmpty()){ // If the library is NOT empty
if(library()->defaultNoteFile() != nullptr){
setNoteFile(library()->defaultNoteFile());
return;
}else{
setNoteFile(library()->noteFiles()[0]);
return;
}
}else{ //If the library IS empty
misliWindow->ui->jumpToNearestNotePushButton->hide();
misliWindow->ui->makeNoteFilePushButton->show();
misliWindow->ui->menuSwitch_to_another_note_file->setEnabled(false);
misliWindow->updateNoteFilesListMenu();
misliWindow->updateTitle();
hide();
}
}else{ //If the newNoteFile is NOT NULL
visualChangeConnection = connect(newNoteFile, SIGNAL(visualChange()), this, SLOT(update()));
misliWindow->ui->makeNoteFilePushButton->hide();
misliWindow->ui->menuSwitch_to_another_note_file->setEnabled(true);
show();
}
lastNoteFile = currentNoteFile;
currentNoteFile = newNoteFile;
misliWindow->updateNoteFilesListMenu();
misliWindow->updateTitle();
int myIndex = misliWindow->ui->tabWidget->indexOf(this);
misliWindow->ui->tabWidget->setTabText(myIndex, noteFile()->name());
update();
}
void CanvasWidget::centerEyeOnNote(Note *nt)
{
noteFile()->eyeX = nt->rect().center().x();
noteFile()->eyeY = nt->rect().center().y();
update();
}
bool CanvasWidget::mimeDataIsCompatible(const QMimeData *mimeData)
{
if(mimeData->hasUrls()){
QList<QUrl> urls = mimeData->urls();
//If there's no valid URLs - draggedStuffIsValid is left false
for(QUrl url: urls){
if(url.isLocalFile()){
if(url.fileName().endsWith(".txt")){
return true;
}else if(!QImage(url.toLocalFile()).isNull()){
return true;
}else{
return false;
}
}else{
return true;
}
}
return false;
}else if(mimeData->hasImage()){
if(QImage(qvariant_cast<QImage>(mimeData->imageData())).isNull()){
return false;
}else{
return true;
}
}else if(mimeData->hasText()){
return true;
}else{
return false;
}
}
void CanvasWidget::pasteMimeData(const QMimeData *mimeData)
{
//We'll use the note input mechanism to make the new notes from the dropped items
misliWindow->edit_w->x_on_new_note=mousePos().x(); //cursor position relative to the gl widget
misliWindow->edit_w->y_on_new_note=mousePos().y();
misliWindow->edit_w->edited_note=nullptr;
if(mimeData->hasImage()){
QString imageName = QInputDialog::getText(this,tr("Give a name to the image"),tr("Enter a name for the image"));
QImage img = qvariant_cast<QImage>(mimeData->imageData());
QDir misliDir(misliWindow->misliLibrary()->folderPath);
if(imageName.isEmpty()){
QMessageBox::warning(this, "Warning", tr("Can't use an empty name."));
return;
}else if(misliDir.entryList().contains(imageName+".png")){
QMessageBox::warning(this, "Warning", tr("File name is taken."));
return;
}
imageName = misliDir.filePath(imageName+".png");
if(!img.save(imageName)){
QMessageBox::warning(this, "Warning", tr("Could not save the image (maybe a file with this name already exists? )"));
}else{
misliWindow->edit_w->setTextEditText( "define_picture_note:"+imageName );
misliWindow->edit_w->inputDone();
}
}else if(mimeData->hasText()){
QUrl url(mimeData->text().trimmed(),QUrl::StrictMode);
if(url.isValid()){
if(url.toString().isEmpty()){
qDebug()<<"[Canvas::dropEvent]Empty URL dropped";
}else if(url.isLocalFile()){
if(url.fileName().endsWith(".txt")){
misliWindow->edit_w->setTextEditText( "define_text_file_note:"+misliWindow->edit_w->maybeToRelativePath(url.toLocalFile()) );
misliWindow->edit_w->inputDone();
}else if(!QImage(url.toLocalFile()).isNull()){
misliWindow->edit_w->setTextEditText( "define_picture_note:"+misliWindow->edit_w->maybeToRelativePath(url.toLocalFile()) );
misliWindow->edit_w->inputDone();
}else{
QMessageBox::warning(this, "Warning", tr("Unsupported file dropped:")+url.toLocalFile());
}
}else{
QString pageName = QInputDialog::getText(this,tr("Give a name to the web page"),tr("Enter a name for the web page"));
misliWindow->edit_w->setTextEditText( "define_web_page_note:\nurl="+url.toString()+"\nname="+pageName );
misliWindow->edit_w->inputDone();
}
}else{
misliWindow->edit_w->setTextEditText( mimeData->text() );
misliWindow->edit_w->inputDone();
}
}
}
QPointF CanvasWidget::mousePos()
{
return mapFromGlobal(cursor().pos());
}
//void CanvasWidget::setCurrentDir(Library * newDir)
//{
// if(currentDir_m == newDir && newDir != nullptr) return;
// disconnect(nfChangedConnecton);
// currentDir_m = newDir;
// if(newDir != nullptr){
// nfChangedConnecton = connect(newDir,&Library::noteFilesChanged,[&] {
// setNoteFile(currentNoteFile);
// });
// misliWindow->ui->addMisliDirPushButton->hide();
// misliWindow->ui->makeNoteFilePushButton->setEnabled(true);
// setNoteFile(currentNoteFile);
// misliWindow->notes_search->findByText(misliWindow->ui->searchLineEdit->text());
// }else{
// if(misliWindow->misliInstance()->misliDirs().isEmpty()){//If there are no dirs
// misliWindow->misliInstance()->addDir(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
// return;
// }else{ //Else just switch to the last
// setCurrentDir(misliWindow->misliInstance()->misliDirs().last());
// return;
// }
// }
// misliWindow->updateDirListMenu();
//}