def __init__(self, mainW): super(editLibrary, self).__init__() self.ui = Ui_editLibrary() self.ui.setupUi(self) self.mainW = mainW self.d = None self.selectedTemplate = None self.library = None self.ui.tableWidget.setSortingEnabled(True) self.ui.returnButton.clicked.connect(self.returnToMain) self.ui.selectTemplateButton.clicked.connect(self.selectTemplateWindow) self.ui.addButton.clicked.connect(self.newEntryWindow)
class editLibrary(QWidget): # To Do: # 1. When creating a new entry check to make sure an entry with that name does not already exists, # as the name is the unique identifier. # # 2. Allow a user to edit an attribute for an individual field. # # 3. Allow a user to delete an entry. def __init__(self, mainW): super(editLibrary, self).__init__() self.ui = Ui_editLibrary() self.ui.setupUi(self) self.mainW = mainW self.d = None self.selectedTemplate = None self.library = None self.ui.tableWidget.setSortingEnabled(True) self.ui.returnButton.clicked.connect(self.returnToMain) self.ui.selectTemplateButton.clicked.connect(self.selectTemplateWindow) self.ui.addButton.clicked.connect(self.newEntryWindow) def returnToMain(self): self.close() self.mainW.show() def selectTemplateWindow(self): self.d = selectTemplate(self) self.d.show() def newEntryWindow(self): if self.selectedTemplate is not None: self.d = newEntry(self, cleanse(self.selectedTemplate), self.selectedTemplate, self.library) self.d.show() def changeLibrary(self, library): self.ui.currentLIbrary.setText(library) self.selectedTemplate = library self.importTemplate() def importTemplate(self): fields = {} field_list = [] table_columns = [] true_columns = {} # From the template file determine what fields will be on the table. with open('templates/' + self.selectedTemplate + '.csv', 'r') as file: count = 1 for line in file: line = line.rstrip('\n') if count < 3: count += 1 else: if line.endswith("(text),"): fields[line[:-8]] = line[-6:-2] field_list.append(line[:-8]) else: fields[line[:-11]] = line[-9:-2] field_list.append(line[:-11]) count += 1 # Set the initial row and column count. self.ui.tableWidget.setColumnCount(len(fields)) self.ui.tableWidget.setRowCount(0) self.ui.tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents) # Set the headers for the table to the fields of the template. i = 0 for item in field_list: self.ui.tableWidget.setHorizontalHeaderItem(i, QTableWidgetItem(item)) i += 1 # Connect to the database to read the library. conn = sqlite3.connect('libraries/' + self.selectedTemplate + '.db') table_name = cleanse(self.selectedTemplate) c = conn.cursor() # If the table does not exist then create it. c.execute('''SELECT name FROM sqlite_master WHERE type='table' AND name=?''', (cleanse(self.selectedTemplate),)) if c.fetchone() is None: count = 0 for key in fields: if count == 0: if fields[key] == "real": query = "CREATE TABLE " + table_name + " (" + cleanse(key) + " INTEGER)" else: query = "CREATE TABLE " + table_name + " (" + cleanse(key) + " TEXT)" c.execute(query) count += 1 else: query = "ALTER TABLE " + table_name + " ADD COLUMN " + \ cleanse(key) + " " + fields[key] c.execute(query) count += 1 # Fill a list with the names of the table columns. c.execute('''PRAGMA TABLE_INFO(''' + table_name + ''')''') for item in c: table_columns.append(item[1]) for item in field_list: true_columns[cleanse(item)] = item # For each row of the table add it it to the table widget. row_count = 0 for row in c.execute('''SELECT * FROM ''' + table_name): i = 0 self.ui.tableWidget.setRowCount(row_count + 1) for item in row: if fields[true_columns[table_columns[i]]] == "integer": newItem = NumericTableWidgetItem() newItem.setData(Qt.EditRole, QVariant(item)) else: newItem = QTableWidgetItem() newItem.setData(Qt.EditRole, QVariant(item)) newItem.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled) self.ui.tableWidget.setItem(row_count, getColumn(field_list, true_columns[table_columns[i]]), newItem) i += 1 row_count += 1 conn.commit() conn.close() self.library = LibraryDef(fields, field_list, table_columns, true_columns) def refresh(self, newEntry): self.ui.tableWidget.setSortingEnabled(False) row_count = self.ui.tableWidget.rowCount() + 1 self.ui.tableWidget.setRowCount(row_count) i = 0 for field in self.library.field_list: if self.library.field_types[field] == "integer": newItem = NumericTableWidgetItem() newItem.setData(Qt.EditRole, QVariant(int(newEntry[field]))) else: newItem = QTableWidgetItem() newItem.setData(Qt.EditRole, QVariant(str(newEntry[field]))) self.ui.tableWidget.setItem(row_count-1, i, newItem) i += 1 self.ui.tableWidget.setSortingEnabled(True)
class editLibrary(QWidget): # To Do: # 1. When creating a new entry check to make sure an entry with that name does not already exists, # as the name is the unique identifier. # # 2. Allow a user to edit an attribute for an individual field. # # 3. Allow a user to delete an entry. def __init__(self, mainW): super(editLibrary, self).__init__() self.ui = Ui_editLibrary() self.ui.setupUi(self) self.mainW = mainW self.d = None self.selectedTemplate = None self.library = None self.ui.tableWidget.setSortingEnabled(True) self.ui.returnButton.clicked.connect(self.returnToMain) self.ui.selectTemplateButton.clicked.connect(self.selectTemplateWindow) self.ui.addButton.clicked.connect(self.newEntryWindow) def returnToMain(self): self.close() self.mainW.show() def selectTemplateWindow(self): self.d = selectTemplate(self) self.d.show() def newEntryWindow(self): if self.selectedTemplate is not None: self.d = newEntry(self, cleanse(self.selectedTemplate), self.selectedTemplate, self.library) self.d.show() def changeLibrary(self, library): self.ui.currentLIbrary.setText(library) self.selectedTemplate = library self.importTemplate() def importTemplate(self): fields = {} field_list = [] table_columns = [] true_columns = {} # From the template file determine what fields will be on the table. with open('templates/' + self.selectedTemplate + '.csv', 'r') as file: count = 1 for line in file: line = line.rstrip('\n') if count < 3: count += 1 else: if line.endswith("(text),"): fields[line[:-8]] = line[-6:-2] field_list.append(line[:-8]) else: fields[line[:-11]] = line[-9:-2] field_list.append(line[:-11]) count += 1 # Set the initial row and column count. self.ui.tableWidget.setColumnCount(len(fields)) self.ui.tableWidget.setRowCount(0) self.ui.tableWidget.horizontalHeader().setSectionResizeMode( QHeaderView.ResizeToContents) # Set the headers for the table to the fields of the template. i = 0 for item in field_list: self.ui.tableWidget.setHorizontalHeaderItem( i, QTableWidgetItem(item)) i += 1 # Connect to the database to read the library. conn = sqlite3.connect('libraries/' + self.selectedTemplate + '.db') table_name = cleanse(self.selectedTemplate) c = conn.cursor() # If the table does not exist then create it. c.execute( '''SELECT name FROM sqlite_master WHERE type='table' AND name=?''', (cleanse(self.selectedTemplate), )) if c.fetchone() is None: count = 0 for key in fields: if count == 0: if fields[key] == "real": query = "CREATE TABLE " + table_name + " (" + cleanse( key) + " INTEGER)" else: query = "CREATE TABLE " + table_name + " (" + cleanse( key) + " TEXT)" c.execute(query) count += 1 else: query = "ALTER TABLE " + table_name + " ADD COLUMN " + \ cleanse(key) + " " + fields[key] c.execute(query) count += 1 # Fill a list with the names of the table columns. c.execute('''PRAGMA TABLE_INFO(''' + table_name + ''')''') for item in c: table_columns.append(item[1]) for item in field_list: true_columns[cleanse(item)] = item # For each row of the table add it it to the table widget. row_count = 0 for row in c.execute('''SELECT * FROM ''' + table_name): i = 0 self.ui.tableWidget.setRowCount(row_count + 1) for item in row: if fields[true_columns[table_columns[i]]] == "integer": newItem = NumericTableWidgetItem() newItem.setData(Qt.EditRole, QVariant(item)) else: newItem = QTableWidgetItem() newItem.setData(Qt.EditRole, QVariant(item)) newItem.setFlags(Qt.ItemIsSelectable | Qt.ItemIsEnabled) self.ui.tableWidget.setItem( row_count, getColumn(field_list, true_columns[table_columns[i]]), newItem) i += 1 row_count += 1 conn.commit() conn.close() self.library = LibraryDef(fields, field_list, table_columns, true_columns) def refresh(self, newEntry): self.ui.tableWidget.setSortingEnabled(False) row_count = self.ui.tableWidget.rowCount() + 1 self.ui.tableWidget.setRowCount(row_count) i = 0 for field in self.library.field_list: if self.library.field_types[field] == "integer": newItem = NumericTableWidgetItem() newItem.setData(Qt.EditRole, QVariant(int(newEntry[field]))) else: newItem = QTableWidgetItem() newItem.setData(Qt.EditRole, QVariant(str(newEntry[field]))) self.ui.tableWidget.setItem(row_count - 1, i, newItem) i += 1 self.ui.tableWidget.setSortingEnabled(True)