forked from ahrm/sioyek
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_widget.cpp
More file actions
12155 lines (10204 loc) · 429 KB
/
Copy pathmain_widget.cpp
File metadata and controls
12155 lines (10204 loc) · 429 KB
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
995
996
997
998
999
1000
// deduplicate database code
// make sure jsons exported by previous sioyek versions can be imported
// maybe: use a better method to handle deletion of canceled download portals
// change find_closest_*_index and argminf to use the fact that the list is sorted and speed up the search (not important if there are not a ridiculous amount of highlight/bookmarks)
// do the todo for link clicks when the document is zoomed in (focus on x too)
// fix the issue where executing non-existant command blocks the python api
// handle mobile text selection case where the character is not in the current page
// make sure database migrations goes smoothly. Test with database files from previous sioyek versions.
// portals are not correctly saved in an updated database
// touch epub controls
// better tablet button handling, the current method is setting dependent
// name of command in statusbar is not correct when key is overloaded
// smartviewcandidates are not filled when right clicking on a link?
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <set>
#include <unordered_map>
#include <optional>
#include <memory>
#include <cctype>
#include <cstdlib>
#include <qpainterpath.h>
#include <qabstractitemmodel.h>
#include <qapplication.h>
#include <qboxlayout.h>
#include <qdatetime.h>
#include <qfile.h>
#include <qdrag.h>
#include <qmenu.h>
#include <QThread>
#ifndef SIOYEK_QT6
#include <qdesktopwidget.h>
#endif
#include <QKeyEvent>
#include <qlabel.h>
#include <qlineedit.h>
#include <qlistview.h>
#include <qopenglfunctions.h>
#include <qpushbutton.h>
#include <qsortfilterproxymodel.h>
#include <qstringlistmodel.h>
#include <qtextedit.h>
#include <qtimer.h>
#include <qtreeview.h>
#include <qwindow.h>
#include <qstandardpaths.h>
#include <qprocess.h>
#include <qguiapplication.h>
#include <qmimedata.h>
#include <qscreen.h>
#include <QGestureEvent>
#include <qjsonarray.h>
#include <qjsonobject.h>
#include <qlocalsocket.h>
#include <qbytearray.h>
#include <qscrollbar.h>
#include <qtexttospeech.h>
#include <qwidget.h>
#include <qjsengine.h>
#include <qqmlengine.h>
#include <qtextdocumentfragment.h>
#include <qmenubar.h>
#include <qstylehints.h>
#include <mupdf/fitz.h>
#include "input.h"
#include "database.h"
#include "book.h"
#include "utils.h"
#include "ui.h"
#include "pdf_renderer.h"
#include "document.h"
#include "document_view.h"
#include "pdf_view_opengl_widget.h"
#include "config.h"
#include "utf8.h"
#include "synctex/synctex_parser.h"
#include "path.h"
#include "touchui/TouchMarkSelector.h"
#include "checksum.h"
#include "touchui/TouchSettings.h"
#include "main_widget.h"
#ifdef SIOYEK_ANDROID
#include <QtCore/private/qandroidextras_p.h>
#endif
extern "C" {
#include <fzf/fzf.h>
}
#ifdef Q_OS_MACOS
extern "C" void changeTitlebarColor(WId, double, double, double, double);
extern "C" void hideWindowTitleBar(WId);
#endif
extern int next_window_id;
extern bool VERBOSE;
extern bool SHOULD_USE_MULTIPLE_MONITORS;
extern bool MULTILINE_MENUS;
extern bool SORT_BOOKMARKS_BY_LOCATION;
extern bool SORT_HIGHLIGHTS_BY_LOCATION;
extern bool FLAT_TABLE_OF_CONTENTS;
extern bool HOVER_OVERVIEW;
extern bool WHEEL_ZOOM_ON_CURSOR;
extern float MOVE_SCREEN_PERCENTAGE;
extern std::wstring LIBGEN_ADDRESS;
extern std::wstring INVERSE_SEARCH_COMMAND;
extern std::wstring PAPER_SEARCH_URL;
extern std::wstring PAPER_SEARCH_URL_PATH;
extern std::wstring PAPER_SEARCH_TILE_PATH;
extern std::wstring PAPER_SEARCH_CONTRIB_PATH;
extern bool FUZZY_SEARCHING;
extern bool AUTO_RENAME_DOWNLOADED_PAPERS;
extern bool SHOW_STATUSBAR_ONLY_WHEN_MOUSE_OVER;
extern bool HIGHLIGHT_LINK_DESTINATION;
extern float TEXT_SELECTION_MINIMUM_DISTANCE;
extern float VISUAL_MARK_NEXT_PAGE_FRACTION;
extern float VISUAL_MARK_NEXT_PAGE_THRESHOLD;
extern float SMALL_PIXMAP_SCALE;
extern std::wstring EXECUTE_COMMANDS[26];
extern float HIGHLIGHT_COLORS[26 * 3];
extern int STATUS_BAR_FONT_SIZE;
extern Path default_config_path;
extern Path default_keys_path;
extern std::vector<Path> user_config_paths;
extern std::vector<Path> user_keys_paths;
extern Path database_file_path;
extern Path tutorial_path;
extern Path last_opened_file_address_path;
extern Path auto_config_path;
extern std::wstring ITEM_LIST_PREFIX;
extern std::wstring SEARCH_URLS[26];
extern std::wstring MIDDLE_CLICK_SEARCH_ENGINE;
extern std::wstring SHIFT_MIDDLE_CLICK_SEARCH_ENGINE;
extern float DISPLAY_RESOLUTION_SCALE;
extern float STATUS_BAR_COLOR[3];
extern float STATUS_BAR_TEXT_COLOR[3];
extern int MAIN_WINDOW_SIZE[2];
extern int HELPER_WINDOW_SIZE[2];
extern int MAIN_WINDOW_MOVE[2];
extern int HELPER_WINDOW_MOVE[2];
extern float TOUCHPAD_SENSITIVITY;
extern int SINGLE_MAIN_WINDOW_SIZE[2];
extern int SINGLE_MAIN_WINDOW_MOVE[2];
extern float OVERVIEW_SIZE[2];
extern float OVERVIEW_OFFSET[2];
extern bool IGNORE_WHITESPACE_IN_PRESENTATION_MODE;
extern std::vector<MainWidget*> windows;
extern bool SHOW_DOC_PATH;
extern bool SINGLE_CLICK_SELECTS_WORDS;
extern std::wstring SHIFT_CLICK_COMMAND;
extern std::wstring CONTROL_CLICK_COMMAND;
extern std::wstring SHIFT_RIGHT_CLICK_COMMAND;
extern std::wstring CONTROL_RIGHT_CLICK_COMMAND;
extern std::wstring ALT_CLICK_COMMAND;
extern std::wstring ALT_RIGHT_CLICK_COMMAND;
extern Path local_database_file_path;
extern Path global_database_file_path;
extern Path downloaded_papers_path;
extern std::map<std::wstring, std::wstring> ADDITIONAL_COMMANDS;
extern std::map<std::wstring, std::wstring> ADDITIONAL_MACROS;
extern bool HIGHLIGHT_MIDDLE_CLICK;
extern std::wstring STARTUP_COMMANDS;
extern float CUSTOM_BACKGROUND_COLOR[3];
extern float CUSTOM_TEXT_COLOR[3];
extern float HYPERDRIVE_SPEED_FACTOR;
extern float SMOOTH_SCROLL_SPEED;
extern float SMOOTH_SCROLL_DRAG;
extern bool SUPER_FAST_SEARCH;
extern bool INCREMENTAL_SEARCH;
extern bool SHOW_CLOSEST_BOOKMARK_IN_STATUSBAR;
extern bool SHOW_CLOSE_PORTAL_IN_STATUSBAR;
extern bool CASE_SENSITIVE_SEARCH;
extern bool SMARTCASE_SEARCH;
extern bool SHOW_DOCUMENT_NAME_IN_STATUSBAR;
extern bool SHOULD_HIGHLIGHT_LINKS;
extern float SCROLL_VIEW_SENSITIVITY;
extern std::wstring STATUS_BAR_FORMAT;
extern std::wstring RIGHT_STATUS_BAR_FORMAT;
extern bool INVERTED_HORIZONTAL_SCROLLING;
extern bool TOC_JUMP_ALIGN_TOP;
extern bool AUTOCENTER_VISUAL_SCROLL;
extern bool ALPHABETIC_LINK_TAGS;
extern bool VIMTEX_WSL_FIX;
extern float RULER_AUTO_MOVE_SENSITIVITY;
extern float TTS_RATE;
extern std::wstring HOLD_MIDDLE_CLICK_COMMAND;
extern std::wstring RIGHT_CLICK_SCROLL_UP_COMMAND;
extern std::wstring RIGHT_CLICK_SCROLL_DOWN_COMMAND;
extern std::wstring LEFT_CLICK_SCROLL_UP_COMMAND;
extern std::wstring LEFT_CLICK_SCROLL_DOWN_COMMAND;
extern float FREETEXT_BOOKMARK_FONT_SIZE;
extern std::wstring BOOK_SCAN_PATH;
extern bool USE_RULER_TO_HIGHLIGHT_SYNCTEX_LINE;
extern std::wstring VOLUME_DOWN_COMMAND;
extern std::wstring VOLUME_UP_COMMAND;
extern int DOCUMENTATION_FONT_SIZE;
extern ScratchPad global_scratchpad;
extern int NUM_CACHED_PAGES;
extern bool IGNORE_SCROLL_EVENTS;
extern bool DONT_FOCUS_IF_SYNCTEX_RECT_IS_VISIBLE;
extern bool USE_SYSTEM_THEME;
extern bool USE_CUSTOM_COLOR_FOR_DARK_SYSTEM_THEME;
extern bool ALLOW_MAIN_VIEW_SCROLL_WHILE_IN_OVERVIEW;
extern bool SAME_WIDTH;
extern bool KEYBOARD_SELECT_INCLUSIVE;
extern bool SHOW_COMMAND_HINTS;
extern bool RESTORE_ALL_WINDOWS_ON_STARTUP;
extern bool SIMPLIFY_FREEHAND_DRAWINGS;
extern bool SHOW_RIGHT_CLICK_CONTEXT_MENU;
extern std::wstring CONTEXT_MENU_ITEMS;
extern std::wstring CONTEXT_MENU_ITEMS_FOR_SELECTED_TEXT;
extern std::wstring CONTEXT_MENU_ITEMS_FOR_LINKS;
extern std::wstring CONTEXT_MENU_ITEMS_FOR_HIGHLIGHTS;
extern std::wstring CONTEXT_MENU_ITEMS_FOR_BOOKMARKS;
extern std::wstring CONTEXT_MENU_ITEMS_FOR_OVERVIEW;
extern bool RIGHT_CLICK_CONTEXT_MENU;
extern float SMOOTH_MOVE_MAX_VELOCITY;
extern float PERSISTANCE_PERIOD;
extern bool FORCE_CUSTOM_LINE_ALGORITHM;
extern std::wstring RIGHT_CLICK_COMMAND;
extern std::wstring MIDDLE_CLICK_COMMAND;
extern int MAX_TAB_COUNT;
extern std::wstring RESIZE_COMMAND;
extern std::wstring BACK_RECT_TAP_COMMAND;
extern std::wstring BACK_RECT_HOLD_COMMAND;
extern std::wstring FORWARD_RECT_TAP_COMMAND;
extern std::wstring FORWARD_RECT_HOLD_COMMAND;
extern std::wstring TOP_CENTER_TAP_COMMAND;
extern std::wstring TOP_CENTER_HOLD_COMMAND;
extern std::wstring VISUAL_MARK_NEXT_TAP_COMMAND;
extern std::wstring VISUAL_MARK_NEXT_HOLD_COMMAND;
extern std::wstring VISUAL_MARK_PREV_TAP_COMMAND;
extern std::wstring VISUAL_MARK_PREV_HOLD_COMMAND;
extern bool AUTOMATICALLY_DOWNLOAD_MATCHING_PAPER_NAME;
extern std::wstring TABLET_PEN_CLICK_COMMAND;
extern std::wstring TABLET_PEN_DOUBLE_CLICK_COMMAND;
extern bool ALLOW_HORIZONTAL_DRAG_WHEN_DOCUMENT_IS_SMALL;
extern float PAGE_SPACE_X;
extern float PAGE_SPACE_Y;
extern std::wstring MIDDLE_LEFT_RECT_TAP_COMMAND;
extern std::wstring MIDDLE_LEFT_RECT_HOLD_COMMAND;
extern std::wstring MIDDLE_RIGHT_RECT_TAP_COMMAND;
extern std::wstring MIDDLE_RIGHT_RECT_HOLD_COMMAND;
extern std::wstring PERIODIC_COMMANDS;
extern UIRect PORTRAIT_EDIT_PORTAL_UI_RECT;
extern UIRect LANDSCAPE_EDIT_PORTAL_UI_RECT;
extern UIRect PORTRAIT_BACK_UI_RECT;
extern UIRect PORTRAIT_FORWARD_UI_RECT;
extern UIRect LANDSCAPE_BACK_UI_RECT;
extern UIRect LANDSCAPE_FORWARD_UI_RECT;
extern UIRect PORTRAIT_VISUAL_MARK_PREV;
extern UIRect PORTRAIT_VISUAL_MARK_NEXT;
extern UIRect LANDSCAPE_VISUAL_MARK_PREV;
extern UIRect LANDSCAPE_VISUAL_MARK_NEXT;
extern UIRect PORTRAIT_MIDDLE_LEFT_UI_RECT;
extern UIRect PORTRAIT_MIDDLE_RIGHT_UI_RECT;
extern UIRect LANDSCAPE_MIDDLE_LEFT_UI_RECT;
extern UIRect LANDSCAPE_MIDDLE_RIGHT_UI_RECT;
extern bool PAPER_DOWNLOAD_CREATE_PORTAL;
extern bool ALIGN_LINK_DEST_TO_TOP;
extern bool USE_KEYBOARD_POINT_SELECTION;
extern bool TOUCH_MODE;
const int MAX_SCROLLBAR = 10000;
extern int RELOAD_INTERVAL_MILISECONDS;
const unsigned int INTERVAL_TIME = 200;
#ifdef Q_OS_MACOS
extern float MACOS_TITLEBAR_COLOR[3];
extern bool MACOS_HIDE_TITLEBAR;
#endif
MainWidget* get_window_with_window_id(int window_id) {
for (auto window : windows) {
if (window->get_window_id() == window_id) return window;
}
return nullptr;
}
bool MainWidget::main_document_view_has_document()
{
return (main_document_view != nullptr) && (doc() != nullptr);
}
template<typename T>
void set_filtered_select_menu(MainWidget* main_widget, bool fuzzy, bool multiline, std::vector<std::vector<std::wstring>> columns,
std::vector<T> values,
int selected_index,
std::function<void(T*)> on_select,
std::function<void(T*)> on_delete,
std::function<void(T*)> on_edit = nullptr
) {
if (columns.size() > 1) {
if (TOUCH_MODE) {
// we will set the parent of model to be the widget in the constructor, and it will delete
// the model when it is freed, so this is not a memory leak
QStandardItemModel* model = create_table_model(columns);
TouchFilteredSelectWidget<T>* widget = new TouchFilteredSelectWidget<T>(fuzzy, model, values, selected_index,
[&, main_widget, on_select = std::move(on_select)](T* val) {
if (val) {
on_select(val);
}
main_widget->pop_current_widget();
},
[&, on_delete = std::move(on_delete)](T* val) {
if (val) {
on_delete(val);
}
}, main_widget);
widget->set_filter_column_index(-1);
main_widget->set_current_widget(widget);
}
else {
auto w = new FilteredSelectTableWindowClass<T>(
fuzzy,
multiline,
columns,
values,
selected_index,
[on_select = std::move(on_select)](T* val) {
if (val) {
on_select(val);
}
},
main_widget,
[on_delete = std::move(on_delete)](T* val) {
if (val && on_delete) {
on_delete(val);
}
});
w->set_filter_column_index(-1);
if (on_edit) {
w->set_on_edit_function(on_edit);
}
main_widget->set_current_widget(w);
}
}
else {
if (TOUCH_MODE) {
// when will this be released?
TouchFilteredSelectWidget<T>* widget = new TouchFilteredSelectWidget<T>(fuzzy, columns[0], values, selected_index,
[&, main_widget, on_select = std::move(on_select)](T* val) {
if (val) {
on_select(val);
}
main_widget->pop_current_widget();
},
[&, on_delete = std::move(on_delete)](T* val) {
if (val) {
on_delete(val);
}
}, main_widget);
main_widget->set_current_widget(widget);
}
else {
main_widget->set_current_widget(new FilteredSelectWindowClass<T>(
fuzzy,
columns[0],
values,
[on_select = std::move(on_select)](T* val) {
if (val) {
on_select(val);
}
},
main_widget,
[on_delete = std::move(on_delete)](T* val) {
if (val) {
on_delete(val);
}
},
selected_index));
}
}
}
class SelectionIndicator : public QWidget {
private:
bool is_dragging = false;
bool is_begin;
MainWidget* main_widget;
QIcon begin_icon;
QIcon end_icon;
QPoint last_press_window_pos;
QPoint last_press_widget_pos;
DocumentPos docpos;
bool docpos_needs_recompute = false;
public:
SelectionIndicator(QWidget* parent, bool begin, MainWidget* w, AbsoluteDocumentPos pos) : QWidget(parent), is_begin(begin), main_widget(w) {
docpos = pos.to_document(w->doc());
//begin_icon = QIcon(":/icons/arrow-begin.svg");
//end_icon = QIcon(":/icons/arrow-end.svg");
begin_icon = QIcon(":/icons/selection-begin.svg");
end_icon = QIcon(":/icons/selection-end.svg");
}
void update_pos() {
WindowPos wp = docpos.to_window(main_widget->main_document_view);
if (is_begin) {
move(wp.x - width(), wp.y - height());
}
else {
move(wp.x, wp.y);
}
}
void mousePressEvent(QMouseEvent* mevent) {
is_dragging = true;
last_press_window_pos = mapToParent(mevent->pos());
last_press_widget_pos = pos();
docpos_needs_recompute = true;
}
void mouseMoveEvent(QMouseEvent* mouse_event) {
if (is_dragging) {
QPoint mouse_pos = mapToParent(mouse_event->pos());
QPoint diff = mouse_pos - last_press_window_pos;
QPoint new_widget_pos = last_press_widget_pos + diff;
move(new_widget_pos);
docpos_needs_recompute = true;
main_widget->update_mobile_selection();
}
}
void mouseReleaseEvent(QMouseEvent* mevent) {
is_dragging = false;
}
DocumentPos get_docpos() {
if (!docpos_needs_recompute) return docpos;
if (is_begin) {
docpos = WindowPos{ x() + width(), y() + height() }.to_document(main_widget->main_document_view);
}
else {
docpos = WindowPos{x(), y()}.to_document(main_widget->main_document_view);
}
return docpos;
}
void paintEvent(QPaintEvent* event) {
QPainter painter(this);
(is_begin ? &begin_icon : &end_icon)->paint(&painter, rect());
}
};
void MainWidget::resizeEvent(QResizeEvent* resize_event) {
QWidget::resizeEvent(resize_event);
main_window_width = size().width();
main_window_height = size().height();
if (scratchpad) {
scratchpad->on_view_size_change(resize_event->size().width(), resize_event->size().height());
invalidate_render();
}
if (main_document_view->get_is_auto_resize_mode()) {
main_document_view->fit_to_page_width();
main_document_view->set_offset_y(main_window_height / 2 / main_document_view->get_zoom_level());
}
int status_bar_height = get_status_bar_height();
if (text_command_line_edit_container != nullptr) {
text_command_line_edit_container->move(0, 0);
text_command_line_edit_container->resize(main_window_width, status_bar_height);
}
if (status_label != nullptr) {
status_label->move(0, main_window_height - status_bar_height);
status_label->resize(main_window_width, status_bar_height);
if (should_show_status_label()) {
status_label->show();
}
}
update_command_hints_position();
if ((main_document_view->get_document() != nullptr) && (main_document_view->get_zoom_level() == 0)) {
main_document_view->fit_to_page_width();
update_current_history_index();
}
if ((current_widget_stack.size() > 0)) {
for (auto w : current_widget_stack) {
QCoreApplication::postEvent(w, resize_event->clone());
}
}
if (text_selection_buttons_) {
QCoreApplication::postEvent(get_text_selection_buttons(), resize_event->clone());
}
if (search_buttons_) {
QCoreApplication::postEvent(get_search_buttons(), resize_event->clone());
}
if (highlight_buttons_) {
QCoreApplication::postEvent(get_highlight_buttons(), resize_event->clone());
}
if (draw_controls_) {
QCoreApplication::postEvent(get_draw_controls(), resize_event->clone());
}
if (RESIZE_COMMAND.size() > 0) {
execute_macro_if_enabled(RESIZE_COMMAND);
}
}
void MainWidget::set_overview_position(int page, float offset, std::optional<std::string> overview_type) {
if (page >= 0) {
auto overview_state = OverviewState{ DocumentPos{ page, 0, offset }.to_absolute(doc()).y, 0, -1, nullptr };
overview_state.overview_type = overview_type;
set_overview_page(overview_state);
invalidate_render();
}
}
void MainWidget::set_overview_link(PdfLink link) {
auto [page, offset_x, offset_y] = parse_uri(mupdf_context, doc()->doc, link.uri);
if (page >= 1) {
AbsoluteRect source_absolute_rect = DocumentRect(link.rects[0], link.source_page).to_absolute(doc());
std::wstring source_text = doc()->get_pdf_link_text(link);
SmartViewCandidate current_candidate;
current_candidate.source_rect = source_absolute_rect;
current_candidate.target_pos = DocumentPos{ page - 1, 0, offset_y };
current_candidate.source_text = source_text;
smart_view_candidates = { current_candidate };
index_into_candidates = 0;
set_overview_position(page - 1, offset_y, "link");
}
}
void MainWidget::handle_selection_mouse_edge_scrolling(QMouseEvent* mouse_event){
bool are_we_above_the_window = mapFromGlobal(mouse_event->globalPos()).y() < 30;
bool are_we_below_the_window = mapFromGlobal(mouse_event->globalPos()).y() > main_window_height - 30;
if (are_we_above_the_window){
validation_interval_timer->setInterval(0);
set_fixed_velocity(SMOOTH_MOVE_MAX_VELOCITY, 0);
if (!is_mouse_edge_scrolling){
last_speed_update_time = QTime::currentTime();
}
is_mouse_edge_scrolling = true;
validate_render();
}
else if (are_we_below_the_window){
validation_interval_timer->setInterval(0);
set_fixed_velocity(-SMOOTH_MOVE_MAX_VELOCITY, 0);
if (!is_mouse_edge_scrolling){
last_speed_update_time = QTime::currentTime();
}
is_mouse_edge_scrolling = true;
validate_render();
}
else if (is_mouse_edge_scrolling){
set_fixed_velocity(0, 0);
is_mouse_edge_scrolling = false;
}
}
void MainWidget::mouseMoveEvent(QMouseEvent* mouse_event) {
if (is_pinching){
// no need to handle move events when a pinch to zoom is in progress
return;
}
if (is_rotated()) {
// we don't handle mouse events while document is rotated becausae proper handling
// would increase the code complexity too much to be worth it
return;
}
if (!TOUCH_MODE && is_selecting){
handle_selection_mouse_edge_scrolling(mouse_event);
}
if (should_draw(false) && is_drawing) {
handle_drawing_move(mouse_event->pos(), -1.0f);
validate_render();
return;
}
if (SHOW_STATUSBAR_ONLY_WHEN_MOUSE_OVER) {
if (should_show_status_label(false)) {
if (!status_label->isVisible()) {
status_label->show();
}
}
else {
if (status_label->isVisible()) {
status_label->hide();
}
}
}
WindowPos mpos(mouse_event->pos());
AbsoluteDocumentPos abs_mpos = get_window_abspos(mpos);
NormalizedWindowPos normal_mpos = mpos.to_window_normalized(main_document_view);
if (freehand_drawing_move_data) {
// update temp drawings of opengl widget
//AbsoluteDocumentPos mouse_abspos = WindowPos(mouse_event->pos()).to_absolute(main_document_view);
AbsoluteDocumentPos mouse_abspos = get_window_abspos(WindowPos(mouse_event->pos()));
opengl_widget->moving_drawings.clear();
opengl_widget->moving_pixmaps.clear();
move_selected_drawings(mouse_abspos, opengl_widget->moving_drawings, opengl_widget->moving_pixmaps);
validate_render();
return;
}
if (rect_select_mode) {
if (rect_select_begin.has_value()) {
rect_select_end = abs_mpos;
AbsoluteRect selected_rect(rect_select_begin.value(), rect_select_end.value());
opengl_widget->set_selected_rectangle(selected_rect);
validate_render();
}
return;
}
if (TOUCH_MODE) {
if (selection_begin_indicator) {
selection_begin_indicator->update_pos();
selection_end_indicator->update_pos();
}
update_highlight_buttons_position();
if (is_pressed) {
update_position_buffer();
}
if (was_last_mouse_down_in_ruler_next_rect || was_last_mouse_down_in_ruler_prev_rect) {
WindowPos current_window_pos = { mouse_event->pos().x(), mouse_event->pos().y() };
int distance = current_window_pos.manhattan(ruler_moving_last_window_pos);
ruler_moving_last_window_pos = current_window_pos;
ruler_moving_distance_traveled += distance;
int num_next = ruler_moving_distance_traveled / static_cast<int>(std::max(RULER_AUTO_MOVE_SENSITIVITY, 1.0f));
if (num_next > 0) {
ruler_moving_distance_traveled = 0;
}
for (int i = 0; i < num_next; i++) {
if (was_last_mouse_down_in_ruler_next_rect) {
move_visual_mark_next();
}
else {
move_visual_mark_prev();
}
invalidate_render();
}
return;
}
}
if (bookmark_move_data) {
handle_bookmark_move();
validate_render();
}
if (portal_move_data) {
handle_portal_move();
validate_render();
}
// if the mouse has moved too much when pressing middle mouse button, we assume that the user wants to drag
// instead of smart jump
if (QGuiApplication::mouseButtons() & Qt::MouseButton::MiddleButton) {
if ((!bookmark_move_data.has_value()) && (!portal_move_data.has_value())) {
if ((mpos.manhattan(last_mouse_down_window_pos)) > 50) {
is_dragging = true;
}
}
}
std::optional<PdfLink> link = {};
if (overview_resize_data) {
// if we are resizing overview page, set the selected side of the overview window to the mosue position
fvec2 offset_diff = normal_mpos - overview_resize_data->original_normal_mouse_pos;
opengl_widget->set_overview_side_pos(
overview_resize_data->side_index,
overview_resize_data->original_rect,
offset_diff);
validate_render();
return;
}
if (overview_move_data) {
fvec2 offset_diff = normal_mpos - overview_move_data->original_normal_mouse_pos;
fvec2 new_offsets = overview_move_data.value().original_offsets + offset_diff;
opengl_widget->set_overview_offsets(new_offsets);
validate_render();
return;
}
if (overview_touch_move_data && opengl_widget->get_overview_page()) {
// in touch mode, instead of moving the overview itself, we move the document inside the overview
NormalizedWindowPos current_pos = normal_mpos;
AbsoluteDocumentPos current_overview_absolute_pos = opengl_widget->window_pos_to_overview_pos(current_pos).to_absolute(doc());
/* current_pos = overview_touch_move_data->original_mouse_normalized_pos; */
AbsoluteDocumentPos original_absolute_pos = opengl_widget->window_pos_to_overview_pos(
overview_touch_move_data->original_mouse_normalized_pos).to_absolute(doc());
float absdiff_y = current_overview_absolute_pos.y - original_absolute_pos.y;
float absdiff_x = current_overview_absolute_pos.x - original_absolute_pos.x;
float new_absolute_y = overview_touch_move_data->overview_original_pos_absolute.y - absdiff_y;
float new_absolute_x = overview_touch_move_data->overview_original_pos_absolute.x + absdiff_x;
OverviewState new_overview_state;
new_overview_state.absolute_offset_y = new_absolute_y;
new_overview_state.absolute_offset_x = new_absolute_x;
new_overview_state.doc = opengl_widget->get_overview_page()->doc;
new_overview_state.zoom_level = opengl_widget->get_overview_page()->zoom_level;
set_overview_page(new_overview_state);
validate_render();
}
if (!is_scratchpad_mode()){
if (opengl_widget->is_window_point_in_overview(normal_mpos)) {
link = doc()->get_link_in_pos(opengl_widget->window_pos_to_overview_pos(normal_mpos));
if (link) {
setCursor(Qt::PointingHandCursor);
}
else {
setCursor(Qt::ArrowCursor);
}
return;
}
if (main_document_view && (link = main_document_view->get_link_in_pos(mpos))) {
// show hand cursor when hovering over links
setCursor(Qt::PointingHandCursor);
// if hover_overview config is set, we show an overview of links while hovering over them
if (HOVER_OVERVIEW) {
set_overview_link(link.value());
}
}
else {
setCursor(Qt::ArrowCursor);
if (HOVER_OVERVIEW) {
set_overview_page({});
// invalidate_render();
}
}
}
if (should_drag()) {
fvec2 diff_doc = (mpos - last_mouse_down_window_pos) / dv()->get_zoom_level();
diff_doc[1] = -diff_doc[1]; // higher document y positions correspond to lower window positions
if (horizontal_scroll_locked) {
diff_doc.values[0] = 0;
}
if (!ALLOW_HORIZONTAL_DRAG_WHEN_DOCUMENT_IS_SMALL) {
float current_page_width = doc()->get_page_width(get_current_page_number());
if (dv()->is_two_page_mode()) {
current_page_width += current_page_width + PAGE_SPACE_X;
}
if ((current_page_width > 0) && ((dv()->get_zoom_level() * current_page_width) < width())) {
diff_doc.values[0] = 0;
}
}
//dv()->set_pos(last_mouse_down_document_offset + diff_doc);
dv()->set_virtual_pos(last_mouse_down_document_virtual_offset + diff_doc);
validate_render();
}
if (is_selecting) {
update_text_selection(abs_mpos);
}
}
void MainWidget::update_text_selection(AbsoluteDocumentPos abs_mpos) {
// When selecting, we occasionally update selected text
//todo: maybe have a timer event that handles this periodically
int msecs_since_last_text_select = last_text_select_time.msecsTo(QTime::currentTime());
if (msecs_since_last_text_select > 16 || msecs_since_last_text_select < 0) {
selection_begin = last_mouse_down;
selection_end = abs_mpos;
//fz_point selection_begin = { last_mouse_down.x(), last_mouse_down.y()};
//fz_point selection_end = { document_x, document_y };
if (selection_mode == SelectionMode::Line) {
main_document_view->get_line_selection(selection_begin,
selection_end,
main_document_view->selected_character_rects,
selected_text);
}
else {
main_document_view->get_text_selection(selection_begin,
selection_end,
selection_mode == SelectionMode::Word ,
main_document_view->selected_character_rects,
selected_text);
}
selected_text_is_dirty = false;
validate_render();
last_text_select_time = QTime::currentTime();
}
}
void MainWidget::persist(bool persist_drawings) {
main_document_view->persist(persist_drawings);
if (RESTORE_ALL_WINDOWS_ON_STARTUP) {
std::ofstream last_path_file(last_opened_file_address_path.get_path_utf8());
for (auto window : windows) {
if (window->main_document_view->get_document()) {
window->main_document_view->persist(persist_drawings);
QByteArray geom = window->saveGeometry();
std::string geom_hex = geom.toHex().constData();
last_path_file << "[window] " << geom_hex << "\n";
std::string tabs_str = utf8_encode(window->get_current_tabs_file_names());
last_path_file << tabs_str;
if (!tabs_str.empty() && tabs_str.back() != '\n') {
last_path_file << "\n";
}
}
}
last_path_file.close();
}
else {
// write the address of the current document in a file so that the next time
// we launch the application, we open this document
if (main_document_view->get_document()) {
std::ofstream last_path_file(last_opened_file_address_path.get_path_utf8());
//std::string encoded_file_name_str = utf8_encode(main_document_view->get_document()->get_path());
std::string encoded_file_name_str = utf8_encode(get_current_tabs_file_names());
last_path_file << encoded_file_name_str.c_str() << std::endl;
last_path_file.close();
}
}
}
void MainWidget::closeEvent(QCloseEvent* close_event) {
handle_close_event();
}
MainWidget::MainWidget(MainWidget* other) : MainWidget(other->mupdf_context, other->db_manager, other->document_manager, other->config_manager, other->command_manager, other->input_handler, other->checksummer, other->should_quit) {
}
MainWidget::MainWidget(fz_context* mupdf_context,
DatabaseManager* db_manager,
DocumentManager* document_manager,
ConfigManager* config_manager,
CommandManager* command_manager,
InputHandler* input_handler,
CachedChecksummer* checksummer,
bool* should_quit_ptr,
QWidget* parent) :
#ifdef SIOYEK_ANDROID
QQuickWidget(parent),
#else
QMainWindow(parent),
#endif
mupdf_context(mupdf_context),
db_manager(db_manager),
document_manager(document_manager),
config_manager(config_manager),
input_handler(input_handler),
checksummer(checksummer),
should_quit(should_quit_ptr),
command_manager(command_manager)
{
//main_widget->quickWindow()->setGraphicsApi(QSGRendererInterface::OpenGL);
//quickWindow()->setGraphicsApi(QSGRendererInterface::OpenGL);
window_id = next_window_id;
next_window_id++;
setMouseTracking(true);
setAcceptDrops(true);
setAttribute(Qt::WA_DeleteOnClose);
setAttribute(Qt::WA_AcceptTouchEvents);
central_widget = new QWidget(this);
central_widget->setMouseTracking(true);
inverse_search_command = INVERSE_SEARCH_COMMAND;
pdf_renderer = new PdfRenderer(4, should_quit_ptr, mupdf_context);
pdf_renderer->set_num_cached_pages(NUM_CACHED_PAGES);
pdf_renderer->start_threads();
//scratchpad = new ScratchPad();
scratchpad = &global_scratchpad;
main_document_view = new DocumentView(db_manager, document_manager, checksummer);
opengl_widget = new PdfViewOpenGLWidget(main_document_view, pdf_renderer, config_manager, false, this);
QFont label_font = QFont(get_status_font_face_name());
label_font.setStyleHint(QFont::TypeWriter);
status_label = new QWidget(this);
status_label->setFont(label_font);
status_label->setStyleSheet(get_status_stylesheet());
status_label_left = new QLabel();
status_label_left->setStyleSheet(get_status_stylesheet());
status_label_left->setFont(label_font);
status_label_right = new QLabel();
status_label_right->setStyleSheet(get_status_stylesheet());
status_label_right->setFont(label_font);
QHBoxLayout* status_label_layout = new QHBoxLayout();
status_label_layout->setContentsMargins(0, 0, 0, 0);
status_label_layout->addWidget(status_label_left, 1);
status_label_layout->addWidget(status_label_right);
status_label->setLayout(status_label_layout);
opengl_widget->stackUnder(status_label);
// automatically open the helper window in second monitor
int num_screens = QGuiApplication::screens().size();
text_command_line_edit_container = new QWidget(this);
text_command_line_edit_container->setStyleSheet(get_status_stylesheet());
QHBoxLayout* text_command_line_edit_container_layout = new QHBoxLayout();
text_command_line_edit_label = new QLabel(this);
text_command_line_edit = new MyLineEdit(this);
command_hints_label = new QLabel(this);
text_command_line_edit_label->setFont(label_font);
text_command_line_edit->setFont(label_font);
command_hints_label->setFont(label_font);
text_command_line_edit_label->setStyleSheet(get_status_stylesheet());
text_command_line_edit->setStyleSheet(get_status_stylesheet());
command_hints_label->setStyleSheet(get_status_stylesheet());
command_hints_label->setWordWrap(true);
command_hints_label->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
command_hints_label->setMargin(8);
command_hints_label->setAttribute(Qt::WA_TransparentForMouseEvents);
command_hints_label->hide();
text_command_line_edit_container_layout->addWidget(text_command_line_edit_label);
text_command_line_edit_container_layout->addWidget(text_command_line_edit);
text_command_line_edit_container_layout->setContentsMargins(10, 0, 10, 0);
text_command_line_edit_container->setLayout(text_command_line_edit_container_layout);
text_command_line_edit_container->hide();
//QObject::connect(dynamic_cast<MyLineEdit*>(text_command_line_edit), &MyLineEdit::next_suggestion, this, &MainWidget::on_next_text_suggestion);
//QObject::connect(dynamic_cast<MyLineEdit*>(text_command_line_edit), &MyLineEdit::prev_suggestion, this, &MainWidget::on_prev_text_suggestion);