-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchQuerygeneratorWithUi_ChatGPT_v8.py
More file actions
184 lines (152 loc) · 7.22 KB
/
Copy pathSearchQuerygeneratorWithUi_ChatGPT_v8.py
File metadata and controls
184 lines (152 loc) · 7.22 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
import sys
import random
from PyQt6.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem, QVBoxLayout, QWidget, QCheckBox, QTextEdit, QHeaderView, QPushButton, QHBoxLayout, QComboBox, QFileDialog
from PyQt6.QtGui import QColor, QBrush, QFont
import pandas as pd
HIGHLIGHT_COLOR = QColor(255, 255, 0)
NORMAL_TEXT_COLOR = QColor(0, 0, 0)
NORMAL_BG_COLOR = QColor(255, 255, 255)
class SearchInterface(QMainWindow):
def __init__(self):
super().__init__()
self.data = None # DataFrame to store the loaded Excel data
self.selected_items = set()
self.logic = "AND" # Default logic
self.init_ui()
self.load_excel_file()
def load_excel_file(self):
try:
if self.data is None: # Check if data is not already loaded
self.data = pd.read_excel("data/Literaturrecherche.xlsx", sheet_name="data") # Change this to your excel file or load it via the button
self.init_table()
except Exception as e:
print(f"Error loading Excel file: {e}")
self.data = None
def init_ui(self):
self.setWindowTitle("Search Query Generator")
self.resize(800, 600) # Set the initial size of the window (width, height)
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
layout = QVBoxLayout()
self.table_widget = QTableWidget()
self.table_widget.setColumnCount(2) # Two columns: Checkbox and Query
layout.addWidget(self.table_widget)
# Create a QHBoxLayout for the buttons and logic selection
button_layout = QHBoxLayout()
# Add the Load Excel button
load_excel_button = QPushButton("Load Excel")
load_excel_button.clicked.connect(self.open_file_dialog)
button_layout.addWidget(load_excel_button)
# Add the Copy button
copy_button = QPushButton("Copy")
copy_button.clicked.connect(self.copy_to_clipboard)
button_layout.addWidget(copy_button)
# Add the Clear All button
clear_all_button = QPushButton("Clear All")
clear_all_button.clicked.connect(self.clear_all_checkboxes)
button_layout.addWidget(clear_all_button)
# Add the Randomize button
randomize_button = QPushButton("Randomize")
randomize_button.clicked.connect(self.randomize_checkboxes)
button_layout.addWidget(randomize_button)
# Add the logic selection ComboBox
self.logic_combobox = QComboBox()
self.logic_combobox.addItems(["AND", "OR"])
self.logic_combobox.currentIndexChanged.connect(self.update_query)
button_layout.addWidget(self.logic_combobox)
layout.addLayout(button_layout)
# Add the QTextEdit
self.query_result_text_edit = QTextEdit()
# Set maximum height for the QTextEdit widget
self.query_result_text_edit.setMaximumHeight(50)
layout.addWidget(self.query_result_text_edit)
self.central_widget.setLayout(layout)
self.show()
def open_file_dialog(self):
options = QFileDialog.Options()
file_name, _ = QFileDialog.getOpenFileName(self, "Open Excel File", "", "Excel Files (*.xlsx);;All Files (*)", options=options)
if file_name:
try:
self.data = pd.read_excel(file_name, sheet_name="data")
self.init_table()
except Exception as e:
print(f"Error loading Excel file: {e}")
self.data = None
def init_table(self):
if self.data is not None:
# Clear any existing data in the table
self.table_widget.clearContents()
self.table_widget.setRowCount(0)
for i, row in self.data.iterrows():
self.table_widget.insertRow(i)
# Checkbox column
checkbox = QCheckBox()
checkbox.setChecked(False)
checkbox.stateChanged.connect(self.update_query)
self.table_widget.setCellWidget(i, 0, checkbox)
# Query column
query_item = QTableWidgetItem(str(row["Query"]))
self.table_widget.setItem(i, 1, query_item)
# Set header labels and resize mode
self.table_widget.setHorizontalHeaderLabels(["Query?", "Query"])
self.table_widget.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch)
def update_query(self):
self.logic = self.logic_combobox.currentText()
self.selected_items = set()
for i in range(self.table_widget.rowCount()):
checkbox = self.table_widget.cellWidget(i, 0)
query_item = self.table_widget.item(i, 1)
if checkbox.isChecked():
self.selected_items.add(query_item.text())
query_item.setBackground(HIGHLIGHT_COLOR)
query_item.setForeground(QBrush(NORMAL_TEXT_COLOR)) # Set text color to black for better contrast
font = QFont()
font.setBold(True)
query_item.setFont(font)
else:
query_item.setBackground(NORMAL_BG_COLOR) # Reset background color
query_item.setForeground(QBrush(NORMAL_TEXT_COLOR)) # Reset text color
font = QFont()
font.setBold(False)
query_item.setFont(font)
if self.logic == "AND":
query = " AND ".join(self.selected_items)
else:
query = " OR ".join(self.selected_items)
self.query_result_text_edit.setText(query)
def copy_to_clipboard(self):
clipboard = QApplication.clipboard()
clipboard.setText(self.query_result_text_edit.toPlainText())
def clear_all_checkboxes(self):
for i in range(self.table_widget.rowCount()):
checkbox = self.table_widget.cellWidget(i, 0)
checkbox.setChecked(False)
query_item = self.table_widget.item(i, 1)
query_item.setBackground(NORMAL_BG_COLOR) # Reset background color
query_item.setForeground(QBrush(NORMAL_TEXT_COLOR)) # Reset text color
font = QFont()
font.setBold(False)
query_item.setFont(font)
def randomize_checkboxes(self):
num_to_tick = random.randint(1, 4)
rows = random.sample(range(self.table_widget.rowCount()), num_to_tick)
for i in range(self.table_widget.rowCount()):
checkbox = self.table_widget.cellWidget(i, 0)
checkbox.setChecked(i in rows)
query_item = self.table_widget.item(i, 1)
if i in rows:
query_item.setBackground(HIGHLIGHT_COLOR)
query_item.setForeground(QBrush(NORMAL_TEXT_COLOR)) # Set text color to black for better contrast
font = QFont()
font.setBold(True)
query_item.setFont(font)
else:
query_item.setBackground(NORMAL_BG_COLOR) # Reset background color
query_item.setForeground(QBrush(NORMAL_TEXT_COLOR)) # Reset text color
font = QFont()
font.setBold(False)
query_item.setFont(font)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = SearchInterface()
sys.exit(app.exec_())