· 5 years ago · Oct 31, 2020, 02:18 AM
1import csv
2import os
3import sys
4from datetime import date
5
6import pandas as pd
7from PyQt5 import QtCore, QtWidgets
8from PyQt5.QtCore import QAbstractTableModel, Qt
9from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton, QLabel, QGridLayout,
10 QGroupBox, QDialog, QVBoxLayout, QTableView, QLineEdit)
11
12
13class PandasTable(QAbstractTableModel):
14 def __init__(self, df):
15 QAbstractTableModel.__init__(self)
16 self._df = df
17
18 def rowCount(self, parent=None):
19 return self._df.shape[0]
20
21 def columnCount(self, parent=None):
22 return self._df.shape[1]
23
24 def data(self, index, role=Qt.DisplayRole):
25 if index.isValid():
26 if role == Qt.DisplayRole:
27 return str(self._df.iloc[index.row(), index.column()])
28
29 def headerData(self, col, orientation, role):
30 if orientation == Qt.Horizontal and role == Qt.DisplayRole:
31 return self._df.columns[col]
32 return None
33
34
35class Window2(QDialog):
36
37 def __init__(self):
38 super().__init__()
39
40 self.mac_file_path = os.path.abspath(
41 os.path.join(sys.executable + "/prodotti.csv", '..', "..", "..",
42 "prodotti.csv"))
43 print(self.mac_file_path)
44 self.df = None
45 self.title = "Inserisci Prodotto"
46 self.top = 100
47 self.left = 100
48 self.width = 680
49 self.height = 500
50 self.initUI()
51
52 def initUI(self):
53 self.setWindowTitle(self.title)
54 self.setGeometry(self.left, self.top, self.width, self.height)
55
56 self.createGridLayout()
57
58 windowLayout = QVBoxLayout()
59 windowLayout.addWidget(self.horizontalGroupBox)
60 self.setLayout(windowLayout)
61
62 self.show()
63
64 def load_df(self):
65 if not self.df:
66 self.df = pd.read_csv(self.mac_file_path)
67
68 def append_to_db(self, s_list):
69 df = pd.read_csv(self.mac_file_path)
70 if not self.mac_file_path:
71 index_list = ["CodiceProdotto", "Nome", "Descrizione", "QtTotale", "QtaMagazzino", "QtaNegozio", "Taglia",
72 "Colore", "Hex", "Data"]
73 to_add_series = pd.Series(s_list, index=index_list)
74 df.append(to_add_series, ignore_index=True)
75 df.to_csv(self.mac_file_path)
76 print("initialization dir:" + self.mac_file_path)
77
78 def check_existence(self, text=None):
79 pass
80
81 def createGridLayout(self):
82 self.horizontalGroupBox = QGroupBox("Inserisci un prodotto")
83 layout = QGridLayout()
84 layout.setColumnStretch(1, 8)
85 layout.setColumnStretch(2, 8)
86
87 layout = QtWidgets.QGridLayout()
88 codprod = QtWidgets.QLabel("Codice prodotto")
89 layout.addWidget(codprod, 0, 0)
90 self.codiceprod = QtWidgets.QLineEdit()
91 layout.addWidget(self.codiceprod, 0, 1)
92 nome = QtWidgets.QLabel("Nome")
93 layout.addWidget(nome, 1, 0)
94
95 self.nomeprod = QtWidgets.QLineEdit()
96 layout.addWidget(self.nomeprod, 1, 1)
97 descrizione = QtWidgets.QLabel("Descrizione")
98 layout.addWidget(descrizione, 2, 0)
99 self.descprod = QtWidgets.QTextEdit()
100 layout.addWidget(self.descprod, 2, 1)
101 qtatot = QtWidgets.QLabel("QtaTotale")
102 layout.addWidget(qtatot, 3, 0)
103 self.qtatotale = QtWidgets.QSpinBox()
104 layout.addWidget(self.qtatotale, 3, 1)
105 qtaneg = QtWidgets.QLabel("QtaNegozio")
106 layout.addWidget(qtaneg, 3, 2)
107 self.qtanegozio = QtWidgets.QSpinBox()
108 layout.addWidget(self.qtanegozio, 3, 3)
109 qtamag = QtWidgets.QLabel("QtaMagazzino")
110 layout.addWidget(qtamag, 3, 4)
111 self.qtamagazzino = QtWidgets.QSpinBox()
112 layout.addWidget(self.qtamagazzino, 3, 5)
113 taglia = QtWidgets.QLabel("Taglia")
114 layout.addWidget(taglia, 4, 0)
115 self.tag = QtWidgets.QComboBox()
116 self.tag.addItem("XS")
117 self.tag.addItem("S")
118 self.tag.addItem("M")
119 self.tag.addItem("L")
120 self.tag.addItem("XL")
121 self.tag.addItem("XXL")
122
123 layout.addWidget(self.tag, 4, 1)
124
125 colore = QtWidgets.QLabel("Colore")
126 layout.addWidget(colore, 4, 2)
127 self.col = QtWidgets.QComboBox()
128 self.col.addItem("Bianco", "#ffffff")
129 self.col.addItem("Rosso", "#ff0000")
130 self.col.addItem("Verde", "#00ff00")
131 self.col.addItem("Blu", "#0000ff")
132
133 layout.addWidget(self.col, 4, 3)
134
135 self.buttonOK = QtWidgets.QPushButton("SALVA", self)
136 layout.addWidget(self.buttonOK, 5, 2)
137
138 self.setLayout(layout)
139
140 self.buttonOK.clicked.connect(self.buttonOK_clicked)
141
142 ## listener for input change here
143 if self.codiceprod.text() is None:
144 self.codiceprod.textChanged.connect(lambda what=None: self.check_existence(None))
145 else:
146 self.codiceprod.textChanged.connect(lambda what=self.codiceprod.text(): self.check_existence(what))
147 self.load_df()
148
149 @QtCore.pyqtSlot()
150 def buttonOK_clicked(self):
151 # printing the form information
152 codiceprodotto = self.codiceprod.text()
153 nomeprodotto = self.nomeprod.text()
154 descprodotto = self.descprod.toPlainText()
155 qtatotprodotto = self.qtatotale.text()
156 qtanegprodotto = self.qtanegozio.text()
157 qtamagprodotto = self.qtamagazzino.text()
158 tagliaprodotto = self.tag.currentText()
159 coloreprodotto = self.col.currentText()
160 hexprodotto = self.col.currentData()
161 dataprodotto = date.today()
162
163 data_list = [codiceprodotto, nomeprodotto, descprodotto, qtatotprodotto, qtamagprodotto, qtanegprodotto,
164 tagliaprodotto,
165 coloreprodotto, hexprodotto, dataprodotto]
166 self.append_to_db(data_list)
167 print(hexprodotto)
168
169 try:
170 with open(self.mac_file_path, 'a', newline='') as file:
171 writer = csv.writer(file)
172 writer.writerow([codiceprodotto, nomeprodotto, descprodotto,
173 qtatotprodotto, qtanegprodotto, qtamagprodotto, tagliaprodotto, coloreprodotto,
174 hexprodotto, dataprodotto])
175 file.close()
176 except FileNotFoundError as e:
177 with open(self.mac_file_path, 'w', newline='') as file:
178 writer = csv.writer(file)
179 writer.writerow([codiceprodotto, nomeprodotto, descprodotto,
180 qtatotprodotto, qtanegprodotto, qtamagprodotto, tagliaprodotto, coloreprodotto,
181 hexprodotto, dataprodotto])
182 file.close()
183
184 # closing the window
185 self.close()
186
187
188class Window3(QMainWindow): # <===
189 def __init__(self):
190 super().__init__()
191 self.mac_file_path = os.path.abspath(
192 os.path.join(sys.executable + "/prodotti.csv", "..", '..', '..', "prodotti.csv"))
193
194 self.setWindowTitle("Cerca prodotti")
195 self.title = "Cerca Prodotto"
196 self.top = 200
197 self.left = 200
198 self.width = 1120
199 self.height = 900
200 self.df = self.load_csv()
201
202 self.pandas_table = self.create_table()
203 self.pandas_table.setGeometry(30, 30, 1020, 850)
204 self.pandas_table.setBaseSize(1020, 850)
205 self.line_edit = QLineEdit()
206 self.line_edit.setBaseSize(200, 30)
207 self.line_edit.setGeometry(30, 30, 300, 30)
208 self.line_edit.adjustSize()
209 self.label = QLabel()
210 self.label.setText("Inserire un ID valido per modificare i valori NEGOZIO e TOTALE")
211 self.label_instructions = QLabel()
212 self.label_instructions.setText("- Il valore TOTALE viene calcolato come la somma di NEGOZIO e MAGAZZINO")
213 self.wid = QtWidgets.QWidget()
214 self.setCentralWidget(self.wid)
215 self.layout = QtWidgets.QVBoxLayout()
216 self.layout.addWidget(self.label)
217 self.layout.addWidget(self.label_instructions)
218 self.layout.addWidget(self.line_edit)
219 self.layout.addWidget(self.pandas_table)
220 self.wid.setLayout(self.layout)
221
222 if self.line_edit.text() is None:
223 self.line_edit.textChanged.connect(lambda what=None: self.check_existence(None))
224 else:
225 self.line_edit.textChanged.connect(lambda what=self.line_edit.text(): self.check_existence(what))
226
227 def update_widget(self):
228 self.pandas_table = self.create_table()
229 self.pandas_table.setGeometry(30, 30, 1020, 850)
230 self.pandas_table.setBaseSize(1020, 850)
231 self.line_edit = QLineEdit()
232 self.line_edit.setBaseSize(200, 30)
233 self.line_edit.setGeometry(30, 30, 300, 30)
234 self.line_edit.adjustSize()
235 self.label = QLabel()
236 self.label.setText("Inserire un ID valido per modificare i valori NEGOZIO e TOTALE")
237 self.label_instructions = QLabel()
238 self.label_instructions.setText("- Il valore TOTALE viene calcolato come la somma di NEGOZIO e MAGAZZINO")
239 self.wid = QtWidgets.QWidget()
240 self.setCentralWidget(self.wid)
241 self.layout = QtWidgets.QVBoxLayout()
242 self.layout.addWidget(self.label)
243 self.layout.addWidget(self.label_instructions)
244 self.layout.addWidget(self.line_edit)
245 self.layout.addWidget(self.pandas_table)
246 self.wid.setLayout(self.layout)
247
248 if self.line_edit.text() is None:
249 self.line_edit.textChanged.connect(lambda what=None: self.check_existence(None))
250 else:
251 self.line_edit.textChanged.connect(lambda what=self.line_edit.text(): self.check_existence(what))
252
253 def create_table(self):
254 # create the view
255 tv = QTableView()
256
257 # set the table model
258 tablemodel = PandasTable(self.load_csv())
259 tv.setModel(tablemodel)
260 tv.setBaseSize(995, 800)
261 # hide grid
262 tv.setShowGrid(False)
263
264 vh = tv.verticalHeader()
265 vh.setVisible(True)
266
267 # set horizontal header properties
268 hh = tv.horizontalHeader()
269 hh.setStretchLastSection(True)
270
271 # set column width to fit contents
272 # tv.resizeColumnsToContents()
273
274 # set row height
275 tv.resizeRowsToContents()
276
277 # enable sorting
278 tv.setSortingEnabled(False)
279
280 return tv
281
282 def check_existence(self, text=None):
283 if text is None:
284 print("no adds")
285 else:
286 if self.df is None:
287 self.load_df()
288 else:
289 if self.df.loc[self.df.CodiceProdotto == text].any().any():
290 self.df.loc[self.df.CodiceProdotto == text, 'QtaNegozio'] = self.df.loc[
291 self.df.CodiceProdotto == text, 'QtaNegozio'] - 1
292 if int(self.df.loc[self.df.CodiceProdotto == text, 'QtaNegozio']) < 0:
293 self.df.loc[self.df.CodiceProdotto == text, 'QtaNegozio'] = 0
294 self.df.loc[self.df.CodiceProdotto == text, 'QtTotale'] = self.df.loc[
295 self.df.CodiceProdotto == text, 'QtTotale'] - 1
296 if int(self.df.loc[self.df.CodiceProdotto == text, 'QtTotale']) != (
297 int(self.df.loc[self.df.CodiceProdotto == text, 'QtaNegozio']) + int(
298 self.df.loc[self.df.CodiceProdotto == text, 'QtaMagazzino'])):
299 self.df.loc[self.df.CodiceProdotto == text, 'QtTotale'] = int(
300 self.df.loc[self.df.CodiceProdotto == text, 'QtaNegozio']) + int(
301 self.df.loc[self.df.CodiceProdotto == text, 'QtaMagazzino'])
302
303 pd.set_option('display.max_columns', None)
304 print(self.df)
305 self.df.to_csv(self.mac_file_path, index=False)
306 self.update_widget()
307 else:
308 pass
309
310 def load_csv(self):
311 try:
312 df = pd.read_csv(self.mac_file_path)
313 return df
314 except FileNotFoundError as e:
315 df = self.create_db()
316 return df
317
318 def create_db(self):
319 if not os.path.isfile(self.mac_file_path):
320 index_list = ["CodiceProdotto", "Nome", "Descrizione", "QtTotale", "QtaMagazzino", "QtaNegozio", "Taglia",
321 "Colore", "Hex", "Data"]
322 df = pd.DataFrame(columns=index_list)
323 df.to_csv(self.mac_file_path, index=False)
324 print("initialization dir:" + self.mac_file_path)
325 return df
326 else:
327 print("csv exists", self.mac_file_path)
328
329 @QtCore.pyqtSlot()
330 def on_pushButtonLoad_clicked(self):
331 self.df = self.load_csv()
332 # self.loadCsv()
333
334
335class Window(QMainWindow):
336 def __init__(self):
337 super().__init__()
338
339 self.title = "Home"
340 self.top = 100
341 self.left = 100
342 self.width = 680
343 self.height = 500
344
345 self.mac_file_path = os.path.abspath(
346 os.path.join(sys.executable + "/prodotti.csv", "..", '..', '..', "prodotti.csv"))
347
348 self.pushButton = QPushButton("Inserisci", self)
349 styles = """background-color: orange;
350border-style: outset;
351border-width: 2px;
352border-radius: 15px;
353border-color: #777777;
354padding: 4px;
355color: #777777;"""
356 self.pushButton.setStyleSheet(styles)
357 self.pushButton.move(160, 200)
358 self.pushButton.setToolTip("<h3>Inserisci</h3>")
359 self.pushButton.clicked.connect(self.window2) # <===
360 self.pushButton2 = QPushButton("Cerca", self)
361 self.pushButton2.move(425, 200)
362 self.pushButton2.setStyleSheet(styles)
363 self.pushButton2.setToolTip("<h3>Cerca</h3>")
364
365 self.pushButton2.clicked.connect(self.window3) # <===
366
367 self.create_db()
368 self.main_window()
369
370 def create_db(self):
371 if not os.path.isfile(self.mac_file_path):
372 index_list = ["CodiceProdotto", "Nome", "Descrizione", "QtTotale", "QtaMagazzino", "QtaNegozio", "Taglia",
373 "Colore", "Hex", "Data"]
374 df = pd.DataFrame(columns=index_list)
375 df.to_csv(self.mac_file_path, index=False)
376 print("initialization dir:" + self.mac_file_path)
377 else:
378 print("csv exists", self.mac_file_path)
379
380 def main_window(self):
381 self.setWindowTitle(self.title)
382 self.image_path = os.path.abspath(
383 os.path.join(sys.executable + "/barneys.jpeg", '..', '..', "barneys.jpeg"))
384 self.setStyleSheet("background-image: url(" + self.image_path + ");")
385
386 self.setGeometry(self.top, self.left, self.width, self.height)
387 self.show()
388
389 def window2(self): # <===
390 self.w = Window2()
391 self.w.setWindowTitle(self.w.title)
392 self.w.setGeometry(self.w.top, self.w.left, self.w.width, self.w.height)
393 self.w.show()
394
395 def window3(self): # <===
396 self.w = Window3()
397 self.w.setWindowTitle(self.w.title)
398 self.w.setGeometry(self.w.top, self.w.left, self.w.width, self.w.height)
399 self.w.show()
400
401
402if __name__ == "__main__":
403 app = QApplication(sys.argv)
404 window = Window()
405 sys.exit(app.exec())