-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui.py
123 lines (108 loc) · 4.66 KB
/
gui.py
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
import os
import sys
import configparser
from GroupWidget import GroupWidget
from ResultWidget import ResultWidget
from pdf_search import Searcher
from pdf_parser import load_db
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog, QMessageBox, \
QHBoxLayout, QGroupBox, QVBoxLayout, QLineEdit, QLabel, QScrollArea
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'SR PDF Searcher'
self.left = 100
self.top = 100
self.width = 640
self.height = 640
self.config = configparser.ConfigParser()
self.config_file = 'config.ini'
self.read_config()
self.searcher = Searcher()
self.horizontal_groupbox = None
self.horizontal_recommendations = None
self.recommendations = None
self.content_area = None
self.scroll_area = None
self.utility_label = None
self.line_edit = None
self.init_ui()
def init_ui(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.create_layout()
window_layout = QVBoxLayout()
window_layout.addWidget(self.horizontal_groupbox, 1)
window_layout.addWidget(self.scroll_area, 100)
window_layout.setAlignment(self.scroll_area, Qt.AlignTop)
self.setLayout(window_layout)
self.show()
def create_layout(self):
self.horizontal_groupbox = QGroupBox("Enter the search query")
layout = QVBoxLayout()
self.line_edit = QLineEdit(self)
layout.addWidget(self.line_edit)
self.line_edit.editingFinished.connect(self.search)
self.recommendations = QLabel("Other recommendations: [Currently in development]")
layout.addWidget(self.recommendations)
self.horizontal_groupbox.setLayout(layout)
self.scroll_area = QScrollArea()
self.scroll_area.setWidgetResizable(True)
self.scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
self.scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.scroll_area.setMinimumSize(600, 400) # TODO: Replace by proper scaling
self.content_area = ResultWidget(self.scroll_area)
self.scroll_area.setWidget(self.content_area)
def search(self):
results = [] # [ (term, [title, page, content], count ]
extended_matches = self.searcher.search(self.line_edit.text())
if len(extended_matches) == 0:
return
terms = [m[0] for m in extended_matches]
self.recommendations.setText(" ".join(terms[1:]))
term, matches = extended_matches[0]
hits = []
search = [(match["title"], match["page"], match.highlights("content")) for match in matches]
for title, page, content in search:
hits.append((title, page, content.replace("\n", " ")))
self.update_results(sorted(hits))
def update_results(self, hits):
self.content_area.clear()
books = {}
for title, page, content in hits:
if not title in books:
books[title] = []
books[title].append((page, content))
for b in books.keys():
group_widget = GroupWidget(title="{}\t{} Matches".format(b, len(books[b])))
content_layout = QVBoxLayout(group_widget)
for t in sorted(books[b]):
if len(t[1]) < 20:
continue
label = QLabel("\tPage: {}\n{} ".format(*t))
label.setWordWrap(True)
content_layout.addWidget(label)
if content_layout.count() > 0:
group_widget.set_content_layout(content_layout)
self.content_area.add(group_widget)
def read_config(self):
if not os.path.exists(self.config_file):
self.config['GENERAL'] = {}
self.config['GENERAL']['RulebookLocation'] = self.open_pdfdialog()
with open(self.config_file, 'w') as confout:
self.config.write(confout)
self.config.read(self.config_file)
def open_pdfdialog(self):
message = "Not set up yet. Select directory containing PDF files to process. This may take up to 5 minutes"
QMessageBox.question(self, "Process rulebooks", message, QMessageBox.Ok)
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
rulebook_location = QFileDialog.getExistingDirectory(self, options=options)
if rulebook_location:
load_db(rulebook_location)
return rulebook_location
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())