-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1173 lines (855 loc) · 33.8 KB
/
main.py
File metadata and controls
1173 lines (855 loc) · 33.8 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
import sys
import os
import json
import uuid
import copy
import requests
from bs4 import BeautifulSoup
from pyzbar.pyzbar import decode
import cv2
import isbnlib
from PySide6.QtWidgets import (
QApplication,
QMainWindow,
QDialog,
QMessageBox,
QWidget,
QVBoxLayout,
QHBoxLayout,
QLabel,
QLineEdit,
QTextEdit,
QPushButton,
QCheckBox,
QTableView,
QAbstractItemView,
QHeaderView
)
from PySide6.QtCore import (
Qt,
QAbstractTableModel,
QModelIndex,
QObject,
QTimer,
QThread,
QMutex,
QRunnable,
QThreadPool,
Signal,
Slot,
)
from PySide6.QtGui import (
QImage,
QPixmap,
)
class CameraWorker(QThread):
frame = Signal(QImage)
isbn = Signal(str)
error = Signal(str)
finished = Signal()
number = Signal(int)
status = Signal(str) # Status updates
def __init__(self):
super().__init__()
self.is_running = False
self.camera = None
self.is_camera_active = False
@Slot()
def run(self):
self.is_running = True
self.status.emit("Starting camera...")
try:
self.camera = cv2.VideoCapture(0, cv2.CAP_DSHOW)
if not self.camera.isOpened():
self.error.emit("Camera could not be opened")
return
self.is_camera_active = True
self.status.emit("Camera started successfully")
fps = int(self.camera.get(5))
print(f"Frame Rate : {fps} frames per second")
while self.camera.isOpened():
if self.is_running == False:
return
ret, frame = self.camera.read()
if not ret:
self.error.emit("Failed to read frame")
break
isbn_barcode = None
decoded_barcodes = decode(frame)
for decoded_barcode in decoded_barcodes:
if decoded_barcode:
if decoded_barcode.type == "EAN13":
isbn_barcode = decoded_barcode
if isbn_barcode:
isbn_code = isbn_barcode.data.decode("utf-8")
self.isbn.emit(isbn_code)
(x, y, w, h) = isbn_barcode.rect
pt1_rect = (x, y)
pt2_rect = (x + w, y + h)
cv2.rectangle(
img=frame,
pt1=pt1_rect,
pt2=pt2_rect,
thickness=2,
color=(0, 0, 255),
lineType=cv2.LINE_8
)
h, w, ch = frame.shape
bytes_per_line = ch * w
q_img = QImage(frame.data, w, h, bytes_per_line, QImage.Format.Format_BGR888)
self.frame.emit(q_img)
CameraWorker.msleep(30)
except Exception as e:
self.error.emit(f"Camera error: {str(e)}")
finally:
self.stop_camera()
self.status.emit("Camera stopped")
self.finished.emit()
def stop_camera(self):
if self.camera and self.is_camera_active:
self.is_running = False
self.camera.release()
self.is_camera_active = False
print("Camera stopped")
def start_camera(self):
if not self.is_running:
self.start()
def is_camera_running(self):
return self.is_running and self.is_camera_active
class ScraperWorkerSignals(QObject):
finished = Signal()
error = Signal(str)
result = Signal(dict)
class ScraperWorker(QRunnable):
def __init__(self, _isbn:str="1692492780"):
super().__init__()
self._isbn = _isbn
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'Accept-Language': 'en-US,en;q=0.9,tr;q=0.8',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
# 'Referer': 'https://www.google.com/'
}
self.signals = (
ScraperWorkerSignals()
)
@Slot()
def run(self):
_isbn10 = ""
self._isbn = isbnlib.canonical(self._isbn)
if isbnlib.is_isbn13(self._isbn):
_isbn10 = isbnlib.to_isbn10(self._isbn)
elif isbnlib.is_isbn10(self._isbn):
_isbn10 = self._isbn
else:
print("[ERROR] - You should enter a valid ISBN.")
return
try:
book_amazon_url = f"https://www.amazon.com/dp/{_isbn10}"
page = requests.get(url=book_amazon_url, headers=self.headers)
soup = BeautifulSoup(page.text, "html.parser")
title = ""
author = ""
publisher = ""
publication_date = ""
isbn10 = ""
isbn13 = ""
page_count = ""
language = ""
description = ""
if soup.find(id="productTitle"):
title = soup.find(id="productTitle").text.strip()
# +++++++++ Translator
if soup.find(class_="author").a:
author = soup.find(class_="author").a.text # Author
for i in soup.find(id="detailBullets_feature_div").find("ul").find_all(class_="a-list-item"):
if ("Publisher" in i.find_all("span")[0].text):
publisher = i.find_all("span")[1].text
elif ("Publication date" in i.find_all("span")[0].text):
publication_date = i.find_all("span")[1].text
elif ("ISBN-10" in i.find_all("span")[0].text):
isbn10 = i.find_all("span")[1].text
elif ("ISBN-13" in i.find_all("span")[0].text):
isbn13 = i.find_all("span")[1].text
elif ("Print length" in i.find_all("span")[0].text):
page_count = i.find_all("span")[1].text
elif ("Language" in i.find_all("span")[0].text):
language = i.find_all("span")[1].text
isDescription = True if soup.find(id="bookDescription_feature_div") else False
description = soup.find(id="bookDescription_feature_div").text.strip().replace(" Read more", "") if isDescription else "" # Description
# price = soup.find(class_="slot-price").text.strip().replace("from ", "") # Price
# print(title, author, isbn10, isbn13, language, description, price)
except Exception as err:
self.signals.error.emit(err)
else:
self.signals.finished.emit()
self.signals.result.emit({
"title": title,
"author": author,
"publisher": publisher,
"publication_date": publication_date,
"isbn10": isbn10,
"isbn13": isbn13,
"page_count": page_count,
"language": language,
"description": description,
})
class BookModel(QAbstractTableModel):
def __init__(self, books=None):
super().__init__()
self.books = books or []
self.headers = ["Title", "Author", "Publisher", "ISBN-13"]
def data(self, index, role):
if not index.isValid():
return None
if role == Qt.ItemDataRole.DisplayRole:
return self.books[index.row()][index.column()]
def rowCount(self, index):
return len(self.books)
def columnCount(self, index):
# return len(self.books[0])
return len(self.headers)
def headerData(self, section, orientation, role):
if role == Qt.ItemDataRole.DisplayRole:
if orientation == Qt.Orientation.Horizontal:
return self.headers[section]
elif orientation == Qt.Orientation.Vertical:
return str(section+1)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.db = BasicDB(collection_name="books", root_dir=os.path.abspath(__file__))
self.books = self.db.find()
self.books_list = self.extract_values_from_docs(self.books)
self.scraped_book = None
self.setup_ui()
self.label_camera = QLabel()
self.model = BookModel(self.books_list)
self.table_view.setModel(self.model)
# Camera worker initialization
self.camera_worker = CameraWorker()
self.camera_worker.status.connect(self.handle_camera_status)
self.camera_worker.error.connect(self.handle_camera_error)
## QThreadPool
self.threadpool = QThreadPool()
print(f"Multithreading with maximum {self.threadpool.maxThreadCount()} threads")
# ////////////////////////
# Signals
self.button_add.clicked.connect(self.add_book)
self.button_edit.clicked.connect(self.edit_book)
self.button_delete.clicked.connect(self.delete_book)
self.table_view.pressed.connect(lambda: self.button_edit.setDisabled(False))
self.table_view.pressed.connect(lambda: self.button_delete.setDisabled(False))
self.table_view.doubleClicked.connect(self.show_book_details_dialog)
self.lineedit_search.textChanged.connect(self.search_book)
QApplication.instance().aboutToQuit.connect(self.camera_worker.stop_camera)
def update_frame(self, q_img):
if self.label_camera:
self.label_camera.setPixmap(QPixmap.fromImage(q_img).scaled(
self.label_camera.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation
))
def handle_camera_error(self, error_msg):
print(f"Camera error: {error_msg}")
# You can show a message box or update UI here
def handle_camera_status(self, status_msg):
print(f"Camera status: {status_msg}")
# You can update UI status here
def handle_search_text_changed(self, search_text):
QTimer.singleShot(300, lambda: self.search_book(search_text=search_text))
def search_book(self, search_text):
if len(search_text) == 0:
self._update_model()
return
books = self.db.find()
search_text = search_text.lower()
def linear_search(search_text):
found_books = []
for book in books:
for key, value in book.items():
if search_text and search_text in value.lower() and key != "_id":
found_books.append(book)
break
return found_books
self._update_model(linear_search(search_text))
def add_book(self):
self.show_form_dialog()
def edit_book(self):
indexes = self.table_view.selectedIndexes()
if indexes:
row = indexes[0].row()
selected_book = self.books[row]
self.show_form_dialog(existing_book=selected_book)
def delete_book(self):
indexes = self.table_view.selectedIndexes()
if indexes:
row = indexes[0].row()
selected_book = self.books_list[row]
reply = QMessageBox.question(
self,
"Holocron - Delete Book",
f"Are you sure you want to delete {selected_book[0]}?",
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No
)
if reply == QMessageBox.Yes:
_id = selected_book[-1]
self.db.find_by_id_and_delete(_id)
# print(f"[INFO] - '{selected_book[0]}' was deleted.")
self._update_model()
# ////////////////////////////////////////////////////////////
# SCRAPER WORKER ////////////////////////////////////////////
def start_scraper_worker(self, _isbn:str, callback=None):
## Defining ScraperWorker for scraping
scraper_worker = ScraperWorker(_isbn)
if callback:
scraper_worker.signals.result.connect(callback)
scraper_worker.signals.result.connect(self.scaper_worker_output)
scraper_worker.signals.error.connect(self.scraper_worker_error)
scraper_worker.signals.finished.connect(self.scaper_worker_complete)
self.threadpool.start(scraper_worker)
def scaper_worker_output(self, s):
print("RESULT", s)
def scaper_worker_complete(self):
print("THREAD COMPLETE")
def scraper_worker_error(self, t):
print("ERROR: ", t)
# ////////////////////////////////////////////////////////////
def show_form_dialog(self, existing_book:list=None):
self.keep_dialog_open_state = False
dialog = QDialog(self)
# Start camera if not already running
if not self.camera_worker.is_camera_running() and not existing_book:
self.camera_worker.frame.connect(self.update_frame)
self.camera_worker.isbn.connect(lambda isbn: self.lineedit_scraping_input.setText(isbn))
self.camera_worker.start_camera()
dialog.finished.connect(self.camera_worker.stop_camera)
dialog.destroyed.connect(self.camera_worker.stop_camera)
dialog_x , dialog_y = self.get_available_coordinates()
dialog.setWindowTitle("Holocron - Add Contact")
dialog.setModal(True)
dialog.setFixedWidth(600)
dialog.resize(600, 300)
# dialog.setGeometry(dialog_x, dialog_y, 600, 500)
layout = QVBoxLayout()
layout.setSpacing(15)
#///////////////////////////////////////////////////
# SETUP DIALOG UI
## Scraping
layout_scraping_input = QHBoxLayout()
layout_scraping_input.setSpacing(10)
layout_scraping_input.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.label_camera.setFixedSize(200, 200)
layout_scraping_input.addWidget(self.label_camera)
self.lineedit_scraping_input = QLineEdit()
self.lineedit_scraping_input.setPlaceholderText("Enter ISBN-10 or ISBN-13...")
# self.lineedit_scraping_input.setFocusPolicy(Qt.FocusPolicy.NoFocus)
self.lineedit_scraping_input.clearFocus()
layout_scraping_input.addWidget(self.lineedit_scraping_input)
button_scraping_input = QPushButton("Scrape Book")
button_scraping_input.setStyleSheet("padding: 5px 10px;")
button_scraping_input.setFocusPolicy(Qt.FocusPolicy.NoFocus)
layout_scraping_input.addWidget(button_scraping_input)
if not existing_book:
layout.addLayout(layout_scraping_input)
## Add Form: Title
layout_add_title = QHBoxLayout()
label_add_title = QLabel("Title*")
label_add_title.setStyleSheet("font-size: 12px;")
label_add_title.setFixedWidth(120)
layout_add_title.addWidget(label_add_title)
lineedit_add_title = QLineEdit()
if existing_book:
lineedit_add_title.setText(existing_book["title"])
lineedit_add_title.setPlaceholderText("e.g. 1984")
lineedit_add_title.setStyleSheet("padding: 2px 0; font-size: 12px;")
layout_add_title.addWidget(lineedit_add_title)
## Add Form: Authors
layout_add_authors = QHBoxLayout()
label_add_authors = QLabel("Authors*")
label_add_authors.setStyleSheet("font-size: 12px;")
label_add_authors.setFixedWidth(120)
layout_add_authors.addWidget(label_add_authors)
lineedit_add_authors = QLineEdit()
if existing_book:
lineedit_add_authors.setText(existing_book["authors"])
lineedit_add_authors.setPlaceholderText("e.g. George Orwell")
lineedit_add_authors.setStyleSheet("padding: 2px 0; font-size: 12px;")
layout_add_authors.addWidget(lineedit_add_authors)
## Add Form: Publisher
layout_add_publisher = QHBoxLayout()
label_add_publisher = QLabel("Publisher")
label_add_publisher.setStyleSheet("font-size: 12px;")
label_add_publisher.setFixedWidth(120)
layout_add_publisher.addWidget(label_add_publisher)
lineedit_add_publisher = QLineEdit()
if existing_book:
lineedit_add_publisher.setText(existing_book["publisher"])
lineedit_add_publisher.setPlaceholderText("e.g. Secker & Warburg")
lineedit_add_publisher.setStyleSheet("padding: 2px 0; font-size: 12px;")
layout_add_publisher.addWidget(lineedit_add_publisher)
## Add Form: Publication Date
layout_add_publication_date = QHBoxLayout()
label_add_publication_date = QLabel("Publication Date")
label_add_publication_date.setStyleSheet("font-size: 12px;")
label_add_publication_date.setFixedWidth(120)
layout_add_publication_date.addWidget(label_add_publication_date)
lineedit_add_publication_date = QLineEdit()
if existing_book:
lineedit_add_publication_date.setText(existing_book["publicationDate"])
lineedit_add_publication_date.setPlaceholderText("e.g. 1949")
lineedit_add_publication_date.setStyleSheet("padding: 2px 0; font-size: 12px;")
layout_add_publication_date.addWidget(lineedit_add_publication_date)
## Add Form: ISBN-10
layout_add_isbn10 = QHBoxLayout()
label_add_isbn10 = QLabel("ISBN-10")
label_add_isbn10.setStyleSheet("font-size: 12px;")
label_add_isbn10.setFixedWidth(120)
layout_add_isbn10.addWidget(label_add_isbn10)
lineedit_add_isbn10 = QLineEdit()
if existing_book:
lineedit_add_isbn10.setText(existing_book["isbn10"])
lineedit_add_isbn10.setPlaceholderText("e.g. 6052090493")
lineedit_add_isbn10.setStyleSheet("padding: 2px 0; font-size: 12px;")
layout_add_isbn10.addWidget(lineedit_add_isbn10)
## Add Form: ISBN-13
layout_add_isbn13 = QHBoxLayout()
label_add_isbn13 = QLabel("ISBN-13*")
label_add_isbn13.setStyleSheet("font-size: 12px;")
label_add_isbn13.setFixedWidth(120)
layout_add_isbn13.addWidget(label_add_isbn13)
lineedit_add_isbn13 = QLineEdit()
if existing_book:
lineedit_add_isbn13.setText(existing_book["isbn13"])
lineedit_add_isbn13.setPlaceholderText("e.g. 978-0451524935")
lineedit_add_isbn13.setStyleSheet("padding: 2px 0; font-size: 12px;")
layout_add_isbn13.addWidget(lineedit_add_isbn13)
## Add Form: Page Count
layout_add_page_count = QHBoxLayout()
label_add_page_count = QLabel("Page Count")
label_add_page_count.setStyleSheet("font-size: 12px;")
label_add_page_count.setFixedWidth(120)
layout_add_page_count.addWidget(label_add_page_count)
lineedit_add_page_count = QLineEdit()
if existing_book:
lineedit_add_page_count.setText(existing_book["pageCount"])
lineedit_add_page_count.setPlaceholderText("e.g. 328")
lineedit_add_page_count.setStyleSheet("padding: 2px 0; font-size: 12px;")
layout_add_page_count.addWidget(lineedit_add_page_count)
## Add Form: Language
layout_add_language = QHBoxLayout()
label_add_language = QLabel("Language")
label_add_language.setStyleSheet("font-size: 12px;")
label_add_language.setFixedWidth(120)
layout_add_language.addWidget(label_add_language)
lineedit_add_language = QLineEdit()
if existing_book:
lineedit_add_language.setText(existing_book["language"])
lineedit_add_language.setPlaceholderText("e.g. English")
lineedit_add_language.setStyleSheet("padding: 2px 0; font-size: 12px;")
layout_add_language.addWidget(lineedit_add_language)
## Add Form: Genre
layout_add_genres = QHBoxLayout()
label_add_genres = QLabel("Genres")
label_add_genres.setStyleSheet("font-size: 12px;")
label_add_genres.setFixedWidth(120)
layout_add_genres.addWidget(label_add_genres)
lineedit_add_genres = QLineEdit()
if existing_book:
lineedit_add_genres.setText(existing_book["genres"])
lineedit_add_genres.setPlaceholderText("e.g. Dystopian, Political Fiction, Science Fiction")
lineedit_add_genres.setStyleSheet("padding: 2px 0; font-size: 12px;")
layout_add_genres.addWidget(lineedit_add_genres)
## Add Form: Description
layout_add_description = QHBoxLayout()
label_add_description = QLabel("Description")
label_add_description.setStyleSheet("font-size: 12px;")
label_add_description.setFixedWidth(120)
label_add_description.setAlignment(Qt.AlignmentFlag.AlignTop)
layout_add_description.addWidget(label_add_description)
textedit_add_description = QTextEdit()
if existing_book:
textedit_add_description.setPlainText(existing_book["description"])
textedit_add_description.setPlaceholderText("e.g. Dystopian, Political Fiction, Science Fiction")
textedit_add_description.setStyleSheet("padding: 2px 0; font-size: 12px;")
textedit_add_description.setFixedHeight(100)
layout_add_description.addWidget(textedit_add_description)
layout_add_form = QVBoxLayout()
layout_add_form.setSpacing(10)
layout.addLayout(layout_add_form)
layout_add_form.addLayout(layout_add_title)
layout_add_form.addLayout(layout_add_authors)
layout_add_form.addLayout(layout_add_publisher)
layout_add_form.addLayout(layout_add_publication_date)
layout_add_form.addLayout(layout_add_isbn10)
layout_add_form.addLayout(layout_add_isbn13)
layout_add_form.addLayout(layout_add_page_count)
layout_add_form.addLayout(layout_add_language)
layout_add_form.addLayout(layout_add_genres)
layout_add_form.addLayout(layout_add_description)
## Keep window open?
layout_keep_dialog_open = QHBoxLayout()
layout_keep_dialog_open.addStretch()
if not existing_book:
layout_add_form.addLayout(layout_keep_dialog_open)
label_keep_dialog_open = QLabel("Keep window open?")
layout_keep_dialog_open.addWidget(label_keep_dialog_open)
checkbox_keep_dialog_open = QCheckBox("No")
checkbox_keep_dialog_open.setChecked(self.keep_dialog_open_state)
layout_keep_dialog_open.addWidget(checkbox_keep_dialog_open)
## Form Buttons
layout_add_form_buttons_container = QHBoxLayout()
layout_add_form_buttons_container.setSpacing(10)
layout.addLayout(layout_add_form_buttons_container)
button_form_save_book = QPushButton("Save Changes") if existing_book else QPushButton("Save Book")
button_form_save_book.setStyleSheet("padding: 5px 0;")
button_form_save_book.setFocusPolicy(Qt.FocusPolicy.NoFocus)
layout_add_form_buttons_container.addWidget(button_form_save_book)
button_form_cancel = QPushButton("Cancel")
button_form_cancel.setStyleSheet("padding: 5px 0;")
button_form_cancel.setFocusPolicy(Qt.FocusPolicy.NoFocus)
layout_add_form_buttons_container.addWidget(button_form_cancel)
layout.addStretch()
dialog.setLayout(layout)
#/////////////////////////////////////////////////////////
def save_book():
title = lineedit_add_title.text().strip()
authors = lineedit_add_authors.text().strip()
publisher = lineedit_add_publisher.text().strip()
publication_date = lineedit_add_publication_date.text().strip()
isbn10 = lineedit_add_isbn10.text().strip()
isbn13 = lineedit_add_isbn13.text().strip()
page_count = lineedit_add_page_count.text().strip()
language = lineedit_add_language.text().strip()
genres = lineedit_add_genres.text().strip()
description = textedit_add_description.toPlainText().strip()
if all([title, authors, isbn13]):
book_data ={
"title": title,
"authors": authors,
"publisher": publisher,
"publicationDate": publication_date,
"isbn10": isbn10,
"isbn13": isbn13,
"pageCount": page_count,
"language": language,
"genres": genres,
"description": description
}
if existing_book:
updated_book = self.db.find_by_id_and_update(existing_book["_id"], book_data)
if updated_book:
self._update_model()
dialog.close()
else:
new_book = self.db.create(book_data)
if new_book:
self._update_model()
if not self.keep_dialog_open_state:
dialog.close()
self.lineedit_scraping_input.clear()
title = lineedit_add_title.clear()
authors = lineedit_add_authors.clear()
publisher = lineedit_add_publisher.clear()
publication_date = lineedit_add_publication_date.clear()
isbn10 = lineedit_add_isbn10.clear()
isbn13 = lineedit_add_isbn13.clear()
page_count = lineedit_add_page_count.clear()
language = lineedit_add_language.clear()
genres = lineedit_add_genres.clear()
description = textedit_add_description.clear()
else:
QMessageBox.warning(
dialog,
"Add Contact",
f"Please fill in the required fields",
QMessageBox.Ok,
QMessageBox.Ok
)
def update_keep_dialog_open_state(check_state):
if check_state == 2: # Qt.CheckState.Checked
self.keep_dialog_open_state = True
elif check_state == 0:
self.keep_dialog_open_state = False
def fill_scraped_book(scraped_book):
if scraped_book:
lineedit_add_title.setText(scraped_book["title"])
lineedit_add_authors.setText(scraped_book["author"])
lineedit_add_publisher.setText(scraped_book["publisher"])
lineedit_add_publication_date.setText(scraped_book["publication_date"])
lineedit_add_isbn10.setText(scraped_book["isbn10"])
lineedit_add_isbn13.setText(scraped_book["isbn13"])
lineedit_add_page_count.setText(scraped_book["page_count"])
lineedit_add_language.setText(scraped_book["language"])
# lineedit_add_genres.setText()
textedit_add_description.setText(scraped_book["description"])
#/////////////////////////////////////////////////////////
## Signals
button_scraping_input.clicked.connect(
lambda: self.start_scraper_worker(self.lineedit_scraping_input.text().strip(), fill_scraped_book)
)
button_form_save_book.clicked.connect(save_book)
button_form_cancel.clicked.connect(dialog.close)
checkbox_keep_dialog_open.stateChanged.connect(update_keep_dialog_open_state)
#/////////////////////////////////////////////////////////
dialog.show()
def show_book_details_dialog(self, index:QModelIndex):
row = index.row()
selected_books_id = self.books_list[row][-1]
book_in_detail = self.db.find_by_id(selected_books_id)
dialog = QDialog(self)
dialog_x, dialog_y = self.get_available_coordinates()
dialog.setGeometry(dialog_x, dialog_y, 500, 400)
dialog.setFixedWidth(500)
dialog.setModal(True)
dialog.setWindowTitle(f"{book_in_detail['title']} - Details")
# Main layout
layout = QVBoxLayout()
layout.setSpacing(5)
dialog.setLayout(layout)
# Title section
title_label = QLabel(book_in_detail['title'])
title_label.setStyleSheet("font-size: 24px; font-weight: bold; margin-bottom: 20px;")
title_label.setWordWrap(True)
layout.addWidget(title_label)
# Author section
if book_in_detail.get('authors'):
author_label = QLabel(f"<b>Author:</b> {book_in_detail['authors']}")
author_label.setStyleSheet("font-size: 14px; margin: 5px 0;")
author_label.setWordWrap(True)
layout.addWidget(author_label)
# Publisher section
if book_in_detail.get('publisher'):
publisher_label = QLabel(f"<b>Publisher:</b> {book_in_detail['publisher']}")
publisher_label.setStyleSheet("font-size: 14px; margin: 5px 0;")
publisher_label.setWordWrap(True)
layout.addWidget(publisher_label)
# Publication date section
if book_in_detail.get('publicationDate'):
pub_date_label = QLabel(f"<b>Publication Date:</b> {book_in_detail['publicationDate']}")
pub_date_label.setStyleSheet("font-size: 14px; margin: 5px 0;")
layout.addWidget(pub_date_label)
# ISBN section
isbn_layout = QHBoxLayout()
if book_in_detail.get('isbn10'):
isbn10_label = QLabel(f"<b>ISBN-10:</b> {book_in_detail['isbn10']}")
isbn10_label.setStyleSheet("font-size: 14px; margin: 5px 0;")
isbn_layout.addWidget(isbn10_label)
if book_in_detail.get('isbn13'):
isbn13_label = QLabel(f"<b>ISBN-13:</b> {book_in_detail['isbn13']}")
isbn13_label.setStyleSheet("font-size: 14px; margin: 5px 0;")
isbn_layout.addWidget(isbn13_label)
if isbn_layout.count() > 0:
layout.addLayout(isbn_layout)
# Page count section
if book_in_detail.get('pageCount'):
page_count_label = QLabel(f"<b>Pages:</b> {book_in_detail['pageCount']}")
page_count_label.setStyleSheet("font-size: 14px; margin: 5px 0;")
layout.addWidget(page_count_label)
# Language section
if book_in_detail.get('language'):
language_label = QLabel(f"<b>Language:</b> {book_in_detail['language']}")
language_label.setStyleSheet("font-size: 14px; margin: 5px 0;")
layout.addWidget(language_label)
# Genres section
if book_in_detail.get('genres'):
genres_label = QLabel(f"<b>Genres:</b> {book_in_detail['genres']}")
genres_label.setStyleSheet("font-size: 14px; margin: 5px 0;")
genres_label.setWordWrap(True)
layout.addWidget(genres_label)
# Description section
if book_in_detail.get('description'):
description_label = QLabel("<b>Description:</b>")
description_label.setStyleSheet("font-size: 14px; font-weight: bold; margin: 10px 0 5px 0;")
layout.addWidget(description_label)
description_text = QTextEdit()
description_text.setPlainText(book_in_detail['description'])
description_text.setReadOnly(True)
description_text.setStyleSheet("""
QTextEdit {
border: 1px solid transparent;
border-radius: 5px;
font-size: 13px;
line-height: 1.4;
}
""")
description_text.setFixedHeight(150)
layout.addWidget(description_text)
# Add some spacing
layout.addStretch()
# Buttons section
button_layout = QHBoxLayout()
button_layout.setSpacing(10)
edit_button = QPushButton("Edit Book")
edit_button.setStyleSheet("padding: 8px 0;")
button_layout.addWidget(edit_button)
close_button = QPushButton("Close")
close_button.setStyleSheet("padding: 8px 0;")
button_layout.addWidget(close_button)
layout.addLayout(button_layout)
# Connect signals
edit_button.clicked.connect(lambda: self.edit_book_from_details(book_in_detail, dialog))
close_button.clicked.connect(dialog.close)
dialog.show()
def edit_book_from_details(self, book_data, dialog=None):
if dialog:
dialog.close()
self.show_form_dialog(existing_book=book_data)
def get_available_coordinates(self):
geo = self.geometry()
windowsize = { "x": geo.x(), "y": geo.y(), "width": geo.width(), "height": geo.height()}
screensize = { "width": self.screen().size().toTuple()[0], "hieght": self.screen().size().toTuple()[1]}
dialog_width = 400
dialog_x = None
dialog_y = windowsize["y"] + 0
distance_between_windows = - 200
if (screensize["width"] - (windowsize["x"] + windowsize["width"]) < (dialog_width + distance_between_windows)):
dialog_x = windowsize["x"] - (dialog_width + distance_between_windows)
else:
dialog_x = windowsize["x"] + windowsize["width"] + distance_between_windows
return (dialog_x, dialog_y)
def extract_values_from_docs(self, documents):
result = []
for doc in documents:
# result.append([value for key, value in doc.items() if key in ["title", "authors", "publisher", "isbn13"]])
result.append([doc["title"], doc["authors"], doc["publisher"], doc["isbn13"], doc["_id"]])
return result
def _update_model(self, books:dict = None):
self.books = books if books != None else self.db.find()
self.books_list = self.extract_values_from_docs(self.books)
self.model.books = self.books_list
self.model.layoutChanged.emit()
def setup_ui(self):
self.resize(800, 600)
self.setWindowTitle("Holocron: Library Manager")
central_widget = QWidget()
self.setCentralWidget(central_widget)
layout = QVBoxLayout()
central_widget.setLayout(layout)
# ///////////////////////////////////
layout_search = QHBoxLayout()
layout.addLayout(layout_search)
self.lineedit_search = QLineEdit()
self.lineedit_search.setPlaceholderText("Search a book...")
self.lineedit_search.setFixedSize(400, 30)
layout_search.addWidget(self.lineedit_search)
layout_table = QVBoxLayout()
layout.addLayout(layout_table)
label_table_title = QLabel("Books")
label_table_title.setStyleSheet("font-size: 20px; font-weight: 600;")
layout_table.addWidget(label_table_title)
self.table_view = QTableView()
self.table_view.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeMode.Stretch)
self.table_view.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
self.table_view.setSelectionBehavior(QAbstractItemView.SelectionBehavior.SelectRows)
layout_table.addWidget(self.table_view)
# //////////////////////////////////
layout_buttons_container = QHBoxLayout()
layout.addLayout(layout_buttons_container)
self.button_add = QPushButton("Add Book")