2017-03-13 15:18:52 +03:00
|
|
|
|
#!/usr/bin/python3
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QFrame,
|
|
|
|
|
QSplitter, QStyleFactory, QApplication)
|
|
|
|
|
from PyQt5.QtCore import Qt
|
2017-03-17 17:17:17 +03:00
|
|
|
|
#from PyQt5.QtWidgets import QMainWindow, QTextEdit, QAction, QApplication, QListView, QListWidget, QTableWidget , QTableView, QTableWidgetItem, QHeaderView, QPushButton, QMenu
|
2017-03-13 15:18:52 +03:00
|
|
|
|
from PyQt5.QtGui import QIcon
|
2017-03-17 17:17:17 +03:00
|
|
|
|
from PyQt5.QtWidgets import *
|
2017-03-20 12:38:14 +03:00
|
|
|
|
#from PyQt5 import QtGui
|
2017-03-14 14:09:31 +03:00
|
|
|
|
from PyQt5.QtSql import *
|
2017-03-17 17:17:17 +03:00
|
|
|
|
|
2017-03-15 15:58:26 +03:00
|
|
|
|
#import pymysql
|
2017-03-13 15:18:52 +03:00
|
|
|
|
import dm
|
|
|
|
|
|
2017-03-15 15:58:26 +03:00
|
|
|
|
# conn = pymysql.connect(
|
|
|
|
|
# db='ats',
|
|
|
|
|
# user='dba',
|
|
|
|
|
# passwd='AlsprofilinE',
|
|
|
|
|
# host='kis',
|
|
|
|
|
# charset='utf8')
|
|
|
|
|
# c = conn.cursor()
|
2017-03-14 14:09:31 +03:00
|
|
|
|
|
2017-03-13 15:18:52 +03:00
|
|
|
|
class MainWin(QMainWindow):
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
|
|
self.initUI()
|
2017-03-14 14:09:31 +03:00
|
|
|
|
#заглушка
|
2017-03-20 12:38:14 +03:00
|
|
|
|
def addNewRecord(self):
|
|
|
|
|
editForm = EditForm()
|
|
|
|
|
editForm.setParent(self)
|
|
|
|
|
editForm.show()
|
|
|
|
|
|
2017-03-17 17:17:17 +03:00
|
|
|
|
#tablesDataWidget.setCellWidget(0, 0, edit)
|
|
|
|
|
#callEdit = tablesDataWidget.cellWidget(0, 0)
|
|
|
|
|
#callEdit.text()
|
2017-03-13 15:18:52 +03:00
|
|
|
|
|
|
|
|
|
def initUI(self):
|
|
|
|
|
|
|
|
|
|
textEdit = QTextEdit()
|
|
|
|
|
splitter = WorkArea()
|
|
|
|
|
self.setCentralWidget(splitter)
|
|
|
|
|
#self.setCentralWidget(textEdit)
|
|
|
|
|
|
2017-03-14 14:09:31 +03:00
|
|
|
|
newAction = QAction(QIcon('img/new.gif'), 'Добавить', self)
|
|
|
|
|
newAction.setShortcut('Ins')
|
|
|
|
|
newAction.setStatusTip('Добавить')
|
2017-03-20 12:38:14 +03:00
|
|
|
|
newAction.triggered.connect(self.addNewRecord)
|
2017-03-14 14:09:31 +03:00
|
|
|
|
|
|
|
|
|
deleteAction = QAction(QIcon('img/delete.gif'), 'Удалить', self)
|
|
|
|
|
deleteAction.setShortcut('Del')
|
|
|
|
|
deleteAction.setStatusTip('Удалить')
|
2017-03-20 12:38:14 +03:00
|
|
|
|
#deleteAction.triggered.connect(self.qqqq)
|
2017-03-14 14:09:31 +03:00
|
|
|
|
|
|
|
|
|
exitAction = QAction(QIcon('img/exit.gif'), 'Выход', self)
|
2017-03-13 15:18:52 +03:00
|
|
|
|
exitAction.setShortcut('Ctrl+Q')
|
|
|
|
|
exitAction.setStatusTip('Выход')
|
|
|
|
|
exitAction.triggered.connect(self.close)
|
|
|
|
|
|
2017-03-14 14:09:31 +03:00
|
|
|
|
cutAction = QAction(QIcon('img/cut.gif'), 'Вырезать', self)
|
|
|
|
|
cutAction.setShortcut('Ctrl+X')
|
|
|
|
|
cutAction.setStatusTip('Вырезать')
|
|
|
|
|
#cutAction.triggered.connect(self.close)
|
|
|
|
|
|
|
|
|
|
copyAction = QAction(QIcon('img/copy.gif'), 'Копировать', self)
|
|
|
|
|
copyAction.setShortcut('Ctrl+С')
|
|
|
|
|
copyAction.setStatusTip('Копировать')
|
|
|
|
|
#copyAction.triggered.connect(self.close)
|
|
|
|
|
|
|
|
|
|
pasteAction = QAction(QIcon('img/paste.gif'), 'Вставить', self)
|
|
|
|
|
pasteAction.setShortcut('Ctrl+V')
|
|
|
|
|
pasteAction.setStatusTip('Вставить')
|
|
|
|
|
#pasteAction.triggered.connect(self.close)
|
|
|
|
|
|
|
|
|
|
findAction = QAction(QIcon('img/find.gif'), 'Копировать', self)
|
|
|
|
|
findAction.setShortcut('Ctrl+F')
|
|
|
|
|
findAction.setStatusTip('Искать')
|
|
|
|
|
# findAction.triggered.connect(self.close)
|
|
|
|
|
|
|
|
|
|
printAction = QAction(QIcon('img/print.gif'), 'Печатать', self)
|
|
|
|
|
printAction.setShortcut('Ctrl+P')
|
|
|
|
|
printAction.setStatusTip('Печатать')
|
|
|
|
|
# printAction.triggered.connect(self.close)
|
|
|
|
|
|
|
|
|
|
|
2017-03-13 15:18:52 +03:00
|
|
|
|
self.statusBar()
|
|
|
|
|
|
|
|
|
|
menubar = self.menuBar()
|
|
|
|
|
fileMenu = menubar.addMenu('&Файл')
|
2017-03-14 14:09:31 +03:00
|
|
|
|
fileMenu.addAction(newAction)
|
|
|
|
|
fileMenu.addAction(deleteAction)
|
|
|
|
|
fileMenu.addAction(printAction)
|
2017-03-13 15:18:52 +03:00
|
|
|
|
fileMenu.addAction(exitAction)
|
|
|
|
|
|
|
|
|
|
editMenu = menubar.addMenu('&Редактирование')
|
2017-03-14 14:09:31 +03:00
|
|
|
|
editMenu.addAction(copyAction)
|
|
|
|
|
editMenu.addAction(cutAction)
|
|
|
|
|
editMenu.addAction(pasteAction)
|
|
|
|
|
editMenu.addAction(printAction)
|
|
|
|
|
|
2017-03-13 15:18:52 +03:00
|
|
|
|
helpMenu = menubar.addMenu('&Помощь')
|
|
|
|
|
|
2017-03-14 14:09:31 +03:00
|
|
|
|
#toolbar = self.addToolBar('Редактирование')
|
|
|
|
|
|
|
|
|
|
toolbar = self.addToolBar('Панель инструментов')
|
|
|
|
|
toolbar.addAction(newAction)
|
|
|
|
|
toolbar.addAction(deleteAction)
|
|
|
|
|
toolbar.addAction(copyAction)
|
|
|
|
|
toolbar.addAction(cutAction)
|
|
|
|
|
toolbar.addAction(pasteAction)
|
|
|
|
|
toolbar.addAction(findAction)
|
|
|
|
|
toolbar.addAction(printAction)
|
|
|
|
|
#toolbar.addAction(exitAction)
|
2017-03-13 15:18:52 +03:00
|
|
|
|
|
|
|
|
|
self.setGeometry(300, 300, 850, 650)
|
|
|
|
|
self.setWindowTitle('Ацкый быдлокод')
|
|
|
|
|
self.show()
|
|
|
|
|
|
2017-03-14 14:09:31 +03:00
|
|
|
|
class MyTable(QTableWidget):
|
2017-03-14 17:01:30 +03:00
|
|
|
|
def __init__(self, tbl, thestruct, *args):
|
2017-03-14 14:09:31 +03:00
|
|
|
|
QTableWidget.__init__(self, *args)
|
|
|
|
|
self.data = thestruct
|
|
|
|
|
self.setmydata()
|
2017-03-14 17:01:30 +03:00
|
|
|
|
self.tbl = tbl
|
2017-03-16 16:10:47 +03:00
|
|
|
|
self.resizeColumnsToContents()
|
|
|
|
|
self.horizontalHeader().setSortIndicatorShown(True)
|
|
|
|
|
self.horizontalHeader().setStretchLastSection(True)
|
|
|
|
|
self.horizontalHeader().setCascadingSectionResizes(True)
|
|
|
|
|
# равномерное изменение ширины столбцов
|
|
|
|
|
#self.horizontalHeader().setSectionResizeMode(1)
|
|
|
|
|
# изменение ширины столбцов по размеру текста
|
2017-03-20 12:38:14 +03:00
|
|
|
|
#self.horizontalHeader().setSectionResizeMode(3)
|
2017-03-16 16:10:47 +03:00
|
|
|
|
self.horizontalHeader().setStyleSheet("color: blue;")
|
2017-03-20 12:38:14 +03:00
|
|
|
|
self.setWordWrap(True)
|
|
|
|
|
self.setSortingEnabled(True)
|
2017-03-17 17:17:17 +03:00
|
|
|
|
|
2017-03-14 17:01:30 +03:00
|
|
|
|
#print(self.tbl)
|
2017-03-14 14:09:31 +03:00
|
|
|
|
n = 0
|
2017-03-14 17:01:30 +03:00
|
|
|
|
for item in dm.getTablesStructure(self.tbl):
|
|
|
|
|
# установка заголовков столбцов таблицы
|
|
|
|
|
self.setHorizontalHeaderItem(n, QTableWidgetItem(item))
|
|
|
|
|
n = n+1
|
2017-03-14 14:09:31 +03:00
|
|
|
|
|
2017-03-17 17:17:17 +03:00
|
|
|
|
# обработка нажатия мышой на таблице
|
|
|
|
|
self.doubleClicked.connect(self.on_click)
|
|
|
|
|
# self.doubleClicked.connect(self.contextMenuEvent(self.cursor()))
|
|
|
|
|
|
|
|
|
|
# добавлем кнопку в ячейку
|
|
|
|
|
#self.clicked.connect(self.addButtonToCell())
|
|
|
|
|
def addButtonToCell(self):
|
|
|
|
|
modelIndex = self.currentIndex()
|
|
|
|
|
col = modelIndex.column()
|
|
|
|
|
row = modelIndex.row()
|
|
|
|
|
button = QPushButton('...', self)
|
|
|
|
|
self.setCellWidget(row, col + 1, button)
|
|
|
|
|
# показ контекстного меню
|
|
|
|
|
def contextMenuEvent(self, event):
|
|
|
|
|
Rmenu = QMenu(self)
|
2017-03-20 12:38:14 +03:00
|
|
|
|
addRecord = Rmenu.addAction("Добавить запись")
|
2017-03-17 17:17:17 +03:00
|
|
|
|
delRecord = Rmenu.addAction("Удалить запись")
|
|
|
|
|
relations = Rmenu.addAction("Показать связанные документы")
|
|
|
|
|
action = Rmenu.exec_(self.mapToGlobal(event.pos()))
|
|
|
|
|
if action == delRecord:
|
|
|
|
|
row = self.rowAt(event.pos().y())
|
|
|
|
|
col = self.columnAt(event.pos().x())
|
|
|
|
|
print("row:%d, col:%d" % (row, col))
|
|
|
|
|
item = self.item(self.currentIndex().row(), self.currentIndex().column())
|
|
|
|
|
print(item.text())
|
|
|
|
|
|
|
|
|
|
def on_click(self):
|
|
|
|
|
print(self.cursor().pos().x())
|
|
|
|
|
#self.contextMenuEvent(self.cursor())
|
|
|
|
|
for currentQTableWidgetItem in self.selectedItems():
|
|
|
|
|
print(currentQTableWidgetItem.row(), currentQTableWidgetItem.column(), currentQTableWidgetItem.text())
|
|
|
|
|
|
2017-03-14 17:01:30 +03:00
|
|
|
|
def setmydata(self):
|
|
|
|
|
n = 0
|
2017-03-14 14:09:31 +03:00
|
|
|
|
for key in self.data:
|
|
|
|
|
m = 0
|
|
|
|
|
for item in key:
|
2017-03-17 17:17:17 +03:00
|
|
|
|
# преобразуем все типы в строку
|
|
|
|
|
newitem = QTableWidgetItem(str(item))
|
2017-03-14 14:09:31 +03:00
|
|
|
|
self.setItem(m, n, newitem)
|
|
|
|
|
n += 1
|
|
|
|
|
m += 1
|
|
|
|
|
|
2017-03-13 15:18:52 +03:00
|
|
|
|
|
|
|
|
|
class WorkArea(QWidget):
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.initUI()
|
|
|
|
|
|
|
|
|
|
def initUI(self):
|
|
|
|
|
hbox = QHBoxLayout(self)
|
2017-03-13 16:50:33 +03:00
|
|
|
|
# список таблиц
|
|
|
|
|
listTablesWidget = QListWidget()
|
|
|
|
|
listTablesWidget.setFrameShape(QFrame.StyledPanel)
|
2017-03-16 16:10:47 +03:00
|
|
|
|
listTablesWidget.setFixedWidth(200)
|
2017-03-13 16:50:33 +03:00
|
|
|
|
# таблица для вывода данных
|
|
|
|
|
tablesDataWidget = QTableView()
|
|
|
|
|
tablesDataWidget.setFrameShape(QFrame.StyledPanel)
|
2017-03-13 15:18:52 +03:00
|
|
|
|
|
|
|
|
|
splitter1 = QSplitter(Qt.Horizontal)
|
2017-03-13 16:50:33 +03:00
|
|
|
|
splitter1.addWidget(listTablesWidget)
|
|
|
|
|
splitter1.addWidget(tablesDataWidget)
|
2017-03-13 15:18:52 +03:00
|
|
|
|
|
|
|
|
|
splitter2 = QSplitter(Qt.Vertical)
|
|
|
|
|
splitter2.addWidget(splitter1)
|
2017-03-16 16:10:47 +03:00
|
|
|
|
#splitter1.setSizes([100, 300])
|
2017-03-13 15:18:52 +03:00
|
|
|
|
hbox.addWidget(splitter2)
|
2017-03-16 16:10:47 +03:00
|
|
|
|
#splitter1.setStretchFactor(1, 0)
|
|
|
|
|
#splitter1.sizeHint()
|
2017-03-13 15:18:52 +03:00
|
|
|
|
|
|
|
|
|
self.setLayout(hbox)
|
|
|
|
|
|
2017-03-13 16:50:33 +03:00
|
|
|
|
# выводим список таблиц в левом поле
|
2017-03-16 16:10:47 +03:00
|
|
|
|
tblList = dm.initDBstructure()
|
|
|
|
|
for i in tblList:
|
|
|
|
|
listTablesWidget.addItem(i[1])
|
2017-03-13 16:50:33 +03:00
|
|
|
|
|
|
|
|
|
# заполняем правый список данными из выбранной таблицы
|
2017-03-16 16:10:47 +03:00
|
|
|
|
def addData(tbl_descr):
|
|
|
|
|
# выбираем название таблицы по описанию
|
|
|
|
|
for i in tblList:
|
|
|
|
|
if i[1] == tbl_descr:
|
|
|
|
|
tbl_name = i[0]
|
|
|
|
|
|
2017-03-14 14:09:31 +03:00
|
|
|
|
# удаляем предыдущий виджет таблицы
|
|
|
|
|
item = splitter1.widget(1)
|
|
|
|
|
if item is not None:
|
|
|
|
|
item.deleteLater()
|
2017-03-15 15:58:26 +03:00
|
|
|
|
|
|
|
|
|
data = dm.selectData(tbl_name)
|
2017-03-14 14:09:31 +03:00
|
|
|
|
# проверка на наличие записей в таблице
|
|
|
|
|
if data:
|
|
|
|
|
# количество строк
|
|
|
|
|
rows = len(data)
|
|
|
|
|
# КОЛИЧЕСТВО КОЛОНОК
|
|
|
|
|
cols = len(data[0])
|
2017-03-17 17:17:17 +03:00
|
|
|
|
tablesDataWidget = MyTable(tbl_name, data, rows, cols)
|
|
|
|
|
tablesDataWidget.setFrameShape(QFrame.StyledPanel)
|
|
|
|
|
splitter1.addWidget(tablesDataWidget)
|
|
|
|
|
else:
|
|
|
|
|
# если данных в БД нет выводим пустую табличку
|
|
|
|
|
tablesDataWidget = QTableView()
|
|
|
|
|
tablesDataWidget.setFrameShape(QFrame.StyledPanel)
|
|
|
|
|
splitter1.addWidget(tablesDataWidget)
|
2017-03-13 16:50:33 +03:00
|
|
|
|
# обработка нажатия мышой на списке
|
|
|
|
|
#listTablesWidget.itemClicked.connect(lambda: dm.selectData(listTablesWidget.currentItem))
|
2017-03-14 14:09:31 +03:00
|
|
|
|
listTablesWidget.itemClicked.connect(lambda: addData(listTablesWidget.model().data(listTablesWidget.currentIndex())))
|
2017-03-13 15:18:52 +03:00
|
|
|
|
|
|
|
|
|
def onChanged(self, text):
|
|
|
|
|
self.lbl.setText(text)
|
|
|
|
|
self.lbl.adjustSize()
|
|
|
|
|
|
2017-03-20 12:38:14 +03:00
|
|
|
|
# Форма для добавления и редактирования
|
|
|
|
|
class EditForm(QWidget):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
QWidget.__init__(self)
|
|
|
|
|
self.initUI()
|
|
|
|
|
#self.setWindowFlags(Qt.SubWindow)
|
|
|
|
|
#print(self.parent)
|
|
|
|
|
#self.parent = parent
|
|
|
|
|
def initUI(self):
|
|
|
|
|
title = QLabel('Title')
|
|
|
|
|
author = QLabel('Author')
|
|
|
|
|
review = QLabel('Review')
|
|
|
|
|
titleEdit = QLineEdit()
|
|
|
|
|
authorEdit = QLineEdit()
|
|
|
|
|
reviewEdit = QTextEdit()
|
|
|
|
|
grid = QGridLayout()
|
|
|
|
|
grid.setSpacing(10)
|
|
|
|
|
grid.addWidget(title, 1, 0)
|
|
|
|
|
grid.addWidget(titleEdit, 1, 1)
|
|
|
|
|
grid.addWidget(author, 2, 0)
|
|
|
|
|
grid.addWidget(authorEdit, 2, 1)
|
|
|
|
|
grid.addWidget(review, 3, 0)
|
|
|
|
|
grid.addWidget(reviewEdit, 3, 1, 5, 1)
|
|
|
|
|
|
|
|
|
|
self.setLayout(grid)
|
|
|
|
|
|
|
|
|
|
self.setGeometry(300, 300, 350, 300)
|
|
|
|
|
self.setWindowTitle('Review')
|
|
|
|
|
#self.setParent(parent)
|
|
|
|
|
#self.show()
|
|
|
|
|
|
|
|
|
|
|
2017-03-13 15:18:52 +03:00
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
|
|
app = QApplication(sys.argv)
|
|
|
|
|
ex = MainWin()
|
|
|
|
|
sys.exit(app.exec_())
|
|
|
|
|
|