|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- from PySide2 import QtWidgets as qt
- from PySide2 import QtCore as qtc
- from PySide2.QtGui import QColor
- import configparser, sys, os
- from datetime import datetime as dt
-
- set_debug = False
-
- class TextLine:
- def __init__(self, username, color, textarea, timestamp, logfile):
- self.username = username
- self.color = "#000000" if color == "" else color
- self.qcolor = QColor(int(self.color[1:3], 16), int(self.color[3:5], 16), int(self.color[5:7], 16))
- self.label = qt.QLabel("<span style='color: {}'>&{}:</span>".format(self.color, self.username))
- self.message = qt.QPlainTextEdit()
- self.message.keyPressEvent = self.isItEnter
- self.message.setMaximumSize(600, 40)
- self.textarea = textarea
- self.label.setBuddy(self.message)
- self.label
- self.timestamp = timestamp
- self.logfile = logfile
-
- def isItEnter(self, event):
- if event.key() == qtc.Qt.Key_Enter or event.key() == qtc.Qt.Key_Return:
- mod = qt.QApplication.keyboardModifiers()
- if mod == qtc.Qt.ShiftModifier:
- self.message.insertPlainText("\n")
- else:
- sentText = self.message.toPlainText()
- sentText = sentText.replace("\n", "\t\n")
- if self.timestamp:
- t = dt.now()
- o = "[{}] {}: {}".format(t.strftime("%H:%M:%S"), self.username, sentText)
- else:
- o = "{}: {}".format(self.username, sentText)
- self.textarea.setTextColor(self.qcolor)
- self.textarea.append(o)
- if self.logfile != "":
- with open(self.logfile, "a", encoding="utf8") as file:
- file.write(o + "\n")
- self.message.clear()
- else:
- qt.QPlainTextEdit.keyPressEvent(self.message, event)
-
- def toString(self):
- print(self.username)
-
- def debug(message):
- if set_debug:
- print(message)
-
- def main():
- debug("Loading config...")
- cfg = config()
-
- timestamp = True if int(cfg["timestamp"]) == 1 else False
-
- debug("Setting up...")
- app = qt.QApplication([])
- layout = qt.QVBoxLayout()
-
- debug("Making text area...")
- text_area = qt.QTextEdit()
- text_area.setFocusPolicy(qtc.Qt.NoFocus)
- layout.addWidget(text_area)
-
- debug("Getting users...")
- users = [x.strip() for x in cfg["users"].split(",")]
- colors = [x.strip() for x in cfg["colors"].split(",")]
- while len(colors) < len(users):
- colors.append("#000000")
- debug("Users are {}".format(users))
- inputs = []
- for i in range(len(users)):
- ipt = TextLine(users[i], colors[i], text_area, timestamp, cfg["logfile"])
- inputs.append(ipt)
- for ipt in inputs:
- layout.addWidget(ipt.label)
- layout.addWidget(ipt.message)
-
- debug("Starting window...")
- window = qt.QWidget()
- window.setLayout(layout)
- window.resize(int(cfg["sizew"]), int(cfg["sizeh"]))
- window.show()
-
- debug("Starting interactivity...")
- app.exec_()
-
- def config():
- config = configparser.ConfigParser()
- if not os.path.isfile('config.ini'):
- print("config.ini is missing!")
- sys.exit(0)
- config.read('config.ini')
- return config["DEFAULT"]
-
- if __name__ == "__main__":
- main()
|