From 617f8120432b0f4827cef050af2b8da142474225 Mon Sep 17 00:00:00 2001 From: fasiondog Date: Fri, 23 Apr 2021 01:40:51 +0800 Subject: [PATCH] hkuadmin continue --- hikyuu/admin/HikyuuAdmin.py | 8 +- hikyuu/admin/data/BaseModel.py | 5 + hikyuu/admin/data/LocalDatabase.py | 20 +++ hikyuu/admin/data/__init__.py | 4 + hikyuu/admin/data/model.py | 17 +++ hikyuu/admin/dialog/HkuEditSessionDialog.py | 11 ++ .../admin/dialog/Ui_HkuEditSessionDialog.py | 83 +++++++----- .../admin/dialog/Ui_HkuEditSessionDialog.ui | 127 +++++++++++------- hikyuu/admin/widget/HkuSessionViewWidget.py | 4 +- sub_setup.py | 3 +- 10 files changed, 195 insertions(+), 87 deletions(-) create mode 100644 hikyuu/admin/data/BaseModel.py create mode 100644 hikyuu/admin/data/LocalDatabase.py create mode 100644 hikyuu/admin/data/__init__.py create mode 100644 hikyuu/admin/data/model.py diff --git a/hikyuu/admin/HikyuuAdmin.py b/hikyuu/admin/HikyuuAdmin.py index 01817462..e3468b56 100644 --- a/hikyuu/admin/HikyuuAdmin.py +++ b/hikyuu/admin/HikyuuAdmin.py @@ -24,14 +24,16 @@ import logging import sys +sys.path.append('.') from PyQt5 import QtCore, QtGui, QtWidgets import qdarkstyle from UiConfig import UiConfig -from admin.widget.HkuSessionViewWidget import HkuSessionViewWidget +from widget.HkuSessionViewWidget import HkuSessionViewWidget from dialog import * from widget import * +from data import * _translate = QtCore.QCoreApplication.translate @@ -94,6 +96,8 @@ class MyMainWindow(QtWidgets.QMainWindow): self.statusBar().showMessage(_translate('MainWindow', 'Running')) QtCore.QMetaObject.connectSlotsByName(self) + self.db = LocalDatabase() + def closeEvent(self, event): self.ui_config.save(self) event.accept() @@ -185,6 +189,8 @@ class MyMainWindow(QtWidgets.QMainWindow): self.ui_config.set('main_window', 'style', style_name) def actionEditSession(self): + item = self.server_view_dock.tree.currentItem() + print(item.text(0) if item else 'None') edit_session_dialog = HkuEditSessionDialog() if edit_session_dialog.exec(): print("ok") diff --git a/hikyuu/admin/data/BaseModel.py b/hikyuu/admin/data/BaseModel.py new file mode 100644 index 00000000..3cec31e3 --- /dev/null +++ b/hikyuu/admin/data/BaseModel.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- + +from sqlalchemy.ext.declarative import declarative_base + +BaseModel = declarative_base() diff --git a/hikyuu/admin/data/LocalDatabase.py b/hikyuu/admin/data/LocalDatabase.py new file mode 100644 index 00000000..60562ad2 --- /dev/null +++ b/hikyuu/admin/data/LocalDatabase.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- + +import os +import sqlalchemy +from .BaseModel import BaseModel +from .model import SessionModel + + +class LocalDatabase: + def __init__(self): + self.db_name = "%s/.hikyuu/admin/hkuadmin.db" % os.path.expanduser('~') + self.engine = sqlalchemy.create_engine( + "sqlite:///%s?check_same_thread=false" % self.db_name, echo=False, future=True + ) + BaseModel.metadata.create_all(self.engine) + self.session = sqlalchemy.orm.sessionmaker(bind=self.engine)() + + def __del__(self): + self.session.close() + self.engine.dispose() diff --git a/hikyuu/admin/data/__init__.py b/hikyuu/admin/data/__init__.py new file mode 100644 index 00000000..58cfe13d --- /dev/null +++ b/hikyuu/admin/data/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- + +from .model import * +from .LocalDatabase import LocalDatabase diff --git a/hikyuu/admin/data/model.py b/hikyuu/admin/data/model.py new file mode 100644 index 00000000..1ffbf389 --- /dev/null +++ b/hikyuu/admin/data/model.py @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -*- + +import os +from sqlalchemy import Column, Integer, String +from .BaseModel import BaseModel + + +class SessionModel(BaseModel): + __tablename__ = 'session' + + id = Column(Integer, primary_key=True, autoincrement=True) + name = Column(String(64)) + host = Column(String(39)) + port = Column(Integer) + user = Column(String(64)) + password = Column(String(64)) + remark = Column(String(256)) diff --git a/hikyuu/admin/dialog/HkuEditSessionDialog.py b/hikyuu/admin/dialog/HkuEditSessionDialog.py index 858b501d..8a4ba0c7 100644 --- a/hikyuu/admin/dialog/HkuEditSessionDialog.py +++ b/hikyuu/admin/dialog/HkuEditSessionDialog.py @@ -13,6 +13,7 @@ class HkuEditSessionDialog(QtWidgets.QDialog, Ui_HkuEditSessionDialog): super(HkuEditSessionDialog, self).__init__(parent) self.setWindowIcon(QtGui.QIcon(":/icon/server.png")) self.setupUi(self) + self.remark_textEdit.textChanged.connect(self.limitRemarkMaxLength) self.count = 1 def accept(self): @@ -27,6 +28,16 @@ class HkuEditSessionDialog(QtWidgets.QDialog, Ui_HkuEditSessionDialog): def on_test_pushButton_clicked(self): print("jjjj") + def limitRemarkMaxLength(self): + text = self.remark_textEdit.toPlainText() + print(len(text)) + max_length = 3 + if len(text) > max_length: + self.remark_textEdit.setText(text[:max_length]) + cursor = self.remark_textEdit.textCursor() + cursor.movePosition(QtGui.QTextCursor.End) + self.remark_textEdit.setTextCursor(cursor) + if __name__ == "__main__": import sys diff --git a/hikyuu/admin/dialog/Ui_HkuEditSessionDialog.py b/hikyuu/admin/dialog/Ui_HkuEditSessionDialog.py index 981f9f2a..cea10ae1 100644 --- a/hikyuu/admin/dialog/Ui_HkuEditSessionDialog.py +++ b/hikyuu/admin/dialog/Ui_HkuEditSessionDialog.py @@ -14,55 +14,63 @@ from PyQt5 import QtCore, QtGui, QtWidgets class Ui_HkuEditSessionDialog(object): def setupUi(self, HkuEditSessionDialog): HkuEditSessionDialog.setObjectName("HkuEditSessionDialog") - HkuEditSessionDialog.resize(562, 390) + HkuEditSessionDialog.resize(562, 401) self.layoutWidget = QtWidgets.QWidget(HkuEditSessionDialog) - self.layoutWidget.setGeometry(QtCore.QRect(30, 20, 501, 298)) + self.layoutWidget.setGeometry(QtCore.QRect(30, 30, 501, 298)) self.layoutWidget.setObjectName("layoutWidget") self.gridLayout = QtWidgets.QGridLayout(self.layoutWidget) self.gridLayout.setContentsMargins(0, 0, 0, 0) self.gridLayout.setObjectName("gridLayout") + self.password_lineEdit = QtWidgets.QLineEdit(self.layoutWidget) + self.password_lineEdit.setMaxLength(64) + self.password_lineEdit.setEchoMode(QtWidgets.QLineEdit.Password) + self.password_lineEdit.setPlaceholderText("") + self.password_lineEdit.setObjectName("password_lineEdit") + self.gridLayout.addWidget(self.password_lineEdit, 4, 1, 1, 2) + self.user_label = QtWidgets.QLabel(self.layoutWidget) + self.user_label.setObjectName("user_label") + self.gridLayout.addWidget(self.user_label, 3, 0, 1, 1) + self.host_lineEdit = QtWidgets.QLineEdit(self.layoutWidget) + self.host_lineEdit.setMaxLength(39) + self.host_lineEdit.setObjectName("host_lineEdit") + self.gridLayout.addWidget(self.host_lineEdit, 1, 1, 1, 2) + self.user_lineEdit = QtWidgets.QLineEdit(self.layoutWidget) + self.user_lineEdit.setMaxLength(64) + self.user_lineEdit.setObjectName("user_lineEdit") + self.gridLayout.addWidget(self.user_lineEdit, 3, 1, 1, 2) + self.password_label = QtWidgets.QLabel(self.layoutWidget) + self.password_label.setObjectName("password_label") + self.gridLayout.addWidget(self.password_label, 4, 0, 1, 1) self.host_label = QtWidgets.QLabel(self.layoutWidget) self.host_label.setObjectName("host_label") - self.gridLayout.addWidget(self.host_label, 0, 0, 1, 1) - self.ip_lineEdit = QtWidgets.QLineEdit(self.layoutWidget) - self.ip_lineEdit.setMaxLength(100) - self.ip_lineEdit.setObjectName("ip_lineEdit") - self.gridLayout.addWidget(self.ip_lineEdit, 0, 1, 1, 2) + self.gridLayout.addWidget(self.host_label, 1, 0, 1, 1) self.port_label = QtWidgets.QLabel(self.layoutWidget) self.port_label.setObjectName("port_label") - self.gridLayout.addWidget(self.port_label, 1, 0, 1, 1) + self.gridLayout.addWidget(self.port_label, 2, 0, 1, 1) + spacerItem = QtWidgets.QSpacerItem(218, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) + self.gridLayout.addItem(spacerItem, 2, 2, 1, 1) + self.label = QtWidgets.QLabel(self.layoutWidget) + self.label.setObjectName("label") + self.gridLayout.addWidget(self.label, 0, 0, 1, 1) self.port_spinBox = QtWidgets.QSpinBox(self.layoutWidget) self.port_spinBox.setMaximum(65535) self.port_spinBox.setProperty("value", 9001) self.port_spinBox.setObjectName("port_spinBox") - self.gridLayout.addWidget(self.port_spinBox, 1, 1, 1, 1) - spacerItem = QtWidgets.QSpacerItem(218, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum) - self.gridLayout.addItem(spacerItem, 1, 2, 1, 1) - self.user_label = QtWidgets.QLabel(self.layoutWidget) - self.user_label.setObjectName("user_label") - self.gridLayout.addWidget(self.user_label, 2, 0, 1, 1) - self.user_lineEdit = QtWidgets.QLineEdit(self.layoutWidget) - self.user_lineEdit.setMaxLength(100) - self.user_lineEdit.setObjectName("user_lineEdit") - self.gridLayout.addWidget(self.user_lineEdit, 2, 1, 1, 2) - self.password_label = QtWidgets.QLabel(self.layoutWidget) - self.password_label.setObjectName("password_label") - self.gridLayout.addWidget(self.password_label, 3, 0, 1, 1) - self.password_lineEdit = QtWidgets.QLineEdit(self.layoutWidget) - self.password_lineEdit.setMaxLength(100) - self.password_lineEdit.setEchoMode(QtWidgets.QLineEdit.Password) - self.password_lineEdit.setPlaceholderText("") - self.password_lineEdit.setObjectName("password_lineEdit") - self.gridLayout.addWidget(self.password_lineEdit, 3, 1, 1, 2) + self.gridLayout.addWidget(self.port_spinBox, 2, 1, 1, 1) + self.remark_textEdit = QtWidgets.QTextEdit(self.layoutWidget) + self.remark_textEdit.setAcceptRichText(False) + self.remark_textEdit.setObjectName("remark_textEdit") + self.gridLayout.addWidget(self.remark_textEdit, 5, 1, 1, 2) self.remark_label = QtWidgets.QLabel(self.layoutWidget) self.remark_label.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop) self.remark_label.setObjectName("remark_label") - self.gridLayout.addWidget(self.remark_label, 4, 0, 1, 1) - self.remark_textEdit = QtWidgets.QTextEdit(self.layoutWidget) - self.remark_textEdit.setObjectName("remark_textEdit") - self.gridLayout.addWidget(self.remark_textEdit, 4, 1, 1, 2) + self.gridLayout.addWidget(self.remark_label, 5, 0, 1, 1) + self.name_lineEdit = QtWidgets.QLineEdit(self.layoutWidget) + self.name_lineEdit.setMaxLength(64) + self.name_lineEdit.setObjectName("name_lineEdit") + self.gridLayout.addWidget(self.name_lineEdit, 0, 1, 1, 2) self.layoutWidget1 = QtWidgets.QWidget(HkuEditSessionDialog) - self.layoutWidget1.setGeometry(QtCore.QRect(237, 340, 291, 25)) + self.layoutWidget1.setGeometry(QtCore.QRect(237, 350, 291, 25)) self.layoutWidget1.setObjectName("layoutWidget1") self.horizontalLayout = QtWidgets.QHBoxLayout(self.layoutWidget1) self.horizontalLayout.setContentsMargins(0, 0, 0, 0) @@ -82,13 +90,20 @@ class Ui_HkuEditSessionDialog(object): self.buttonBox.accepted.connect(HkuEditSessionDialog.accept) self.buttonBox.rejected.connect(HkuEditSessionDialog.reject) QtCore.QMetaObject.connectSlotsByName(HkuEditSessionDialog) + HkuEditSessionDialog.setTabOrder(self.name_lineEdit, self.host_lineEdit) + HkuEditSessionDialog.setTabOrder(self.host_lineEdit, self.port_spinBox) + HkuEditSessionDialog.setTabOrder(self.port_spinBox, self.user_lineEdit) + HkuEditSessionDialog.setTabOrder(self.user_lineEdit, self.password_lineEdit) + HkuEditSessionDialog.setTabOrder(self.password_lineEdit, self.remark_textEdit) + HkuEditSessionDialog.setTabOrder(self.remark_textEdit, self.test_pushButton) def retranslateUi(self, HkuEditSessionDialog): _translate = QtCore.QCoreApplication.translate HkuEditSessionDialog.setWindowTitle(_translate("HkuEditSessionDialog", "Dialog")) - self.host_label.setText(_translate("HkuEditSessionDialog", "Host/ip:")) - self.port_label.setText(_translate("HkuEditSessionDialog", "Port:")) self.user_label.setText(_translate("HkuEditSessionDialog", "User:")) self.password_label.setText(_translate("HkuEditSessionDialog", "Password:")) + self.host_label.setText(_translate("HkuEditSessionDialog", "Host/ip:")) + self.port_label.setText(_translate("HkuEditSessionDialog", "Port:")) + self.label.setText(_translate("HkuEditSessionDialog", "Name:")) self.remark_label.setText(_translate("HkuEditSessionDialog", "Remark:")) self.test_pushButton.setText(_translate("HkuEditSessionDialog", "Test connect")) diff --git a/hikyuu/admin/dialog/Ui_HkuEditSessionDialog.ui b/hikyuu/admin/dialog/Ui_HkuEditSessionDialog.ui index 87770e78..ebf475dd 100644 --- a/hikyuu/admin/dialog/Ui_HkuEditSessionDialog.ui +++ b/hikyuu/admin/dialog/Ui_HkuEditSessionDialog.ui @@ -7,7 +7,7 @@ 0 0 562 - 390 + 401 @@ -17,44 +17,68 @@ 30 - 20 + 30 501 298 - + + + + 64 + + + QLineEdit::Password + + + + + + + + + + User: + + + + + + + 39 + + + + + + + 64 + + + + + + + Password: + + + + Host/ip: - - - - 100 - - - - + Port: - - - - 65535 - - - 9001 - - - - + Qt::Horizontal @@ -67,41 +91,31 @@ - - + + - User: + Name: - - - - 100 + + + + 65535 + + + 9001 - - - - Password: + + + + false - - - - 100 - - - QLineEdit::Password - - - - - - - + Remark: @@ -111,8 +125,12 @@ - - + + + + 64 + + @@ -120,7 +138,7 @@ 237 - 340 + 350 291 25 @@ -159,6 +177,15 @@ + + name_lineEdit + host_lineEdit + port_spinBox + user_lineEdit + password_lineEdit + remark_textEdit + test_pushButton + diff --git a/hikyuu/admin/widget/HkuSessionViewWidget.py b/hikyuu/admin/widget/HkuSessionViewWidget.py index 8ac2a156..20a25d96 100644 --- a/hikyuu/admin/widget/HkuSessionViewWidget.py +++ b/hikyuu/admin/widget/HkuSessionViewWidget.py @@ -30,8 +30,10 @@ class HkuSessionViewWidget(QtWidgets.QDockWidget): self.server_menu.addAction(self.parent().action_dict['action_file_session']) self.tree.customContextMenuRequested.connect(self.on_tree_customContextMenuRequested) - def on_tree_customContextMenuRequested(self): + def on_tree_customContextMenuRequested(self, pos): print("on_tree_customContextMenuRequested") + item = self.tree.itemAt(pos) + print(item.parent) self.server_menu.exec(QtGui.QCursor.pos()) def addSession(self): diff --git a/sub_setup.py b/sub_setup.py index ebe16489..84303420 100644 --- a/sub_setup.py +++ b/sub_setup.py @@ -145,6 +145,7 @@ setup( 'qdarkstyle', 'flatbuffers', 'pynng', - 'requests' + 'requests', + 'sqlalchemy' ], ) \ No newline at end of file