Advertisement
AntonPaly4

MyGuiWidget

Aug 19th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.55 KB | None | 0 0
  1. from PyQt5 import QtGui, QtCore, QtWidgets
  2. from os.path import abspath, join
  3.  
  4. STRING_HIGH = 24
  5. CONTACT_BACKGROUND_COLOR = 'light green'
  6. NON_CONTACT_BACKGROUND_COLOR = 'yellow'
  7. NO_COLOR = 'white'
  8.  
  9.  
  10. class MyListWidget(QtWidgets.QListWidget):
  11.     del_contact_signal = QtCore.pyqtSignal(str)
  12.     add_contact_signal = QtCore.pyqtSignal(str)
  13.     remove_and_ignore_signal = QtCore.pyqtSignal(str)
  14.  
  15.     def __init__(self, parent=None):
  16.         super().__init__(parent)
  17.  
  18.     def mousePressEvent(self, event):
  19.         if event.button() == QtCore.Qt.RightButton:
  20.             if event.y() < self.count() * STRING_HIGH:
  21.                 self.setCurrentRow(event.y() // STRING_HIGH)
  22.                 if self.currentItem().in_contacts:
  23.                     self.context_menu_for_contacts(event)
  24.                 else:
  25.                     self.context_menu_for_non_contacts(event)
  26.         super().mousePressEvent(event)
  27.  
  28.     def context_menu_for_contacts(self, event):
  29.         menu = QtWidgets.QMenu(self)
  30.         menu.addAction('delete', self.context_del_contact)
  31.         menu.exec(event.globalPos())
  32.  
  33.     def context_menu_for_non_contacts(self, event):
  34.         menu = QtWidgets.QMenu(self)
  35.         menu.addAction('add contact', self.context_add_contact)
  36.         menu.addSeparator()
  37.         menu.addAction('remove and ignore', self.context_remove_and_ignore)
  38.         menu.exec(event.globalPos())
  39.  
  40.     def context_add_contact(self):
  41.         name = self.currentItem().text()
  42.         self.add_contact_signal.emit(name)
  43.  
  44.     def contact_added(self, contact_item):
  45.         contact = self.currentItem() if not contact_item else contact_item
  46.         if contact.background() == QtGui.QColor(NON_CONTACT_BACKGROUND_COLOR):
  47.             contact.setBackground(QtGui.QColor(CONTACT_BACKGROUND_COLOR))
  48.  
  49.     def context_del_contact(self):
  50.         name = self.currentItem().text()
  51.         self.del_contact_signal.emit(name)
  52.  
  53.     def context_remove_and_ignore(self):
  54.         name = self.currentItem().text()
  55.         self.takeItem(self.currentRow())
  56.         self.remove_and_ignore_signal.emit(name)
  57.  
  58.     def new_message_icon(self, item_):
  59.         item_.messages_count += 1
  60.         if item_.messages_count <= 10:
  61.             icon_file = '{}.png'.format(item_.messages_count)
  62.             icon = join(abspath('.'), 'ui', 'numbers_icons', icon_file)
  63.         else:
  64.             icon = join(abspath('.'), 'ui', 'numbers_icons', '11')
  65.         item_.setIcon(QtGui.QIcon(icon))
  66.  
  67.     def new_message(self, name):
  68.         item = self.findItems(name, QtCore.Qt.MatchExactly)
  69.         if not item:
  70.             new_contact = self.addItem(name)
  71.             new_contact.setBackground(QtGui.QColor(NON_CONTACT_BACKGROUND_COLOR))
  72.             self.new_message_icon(new_contact)
  73.         else:
  74.             contact = item[0]
  75.             if contact.in_contacts:
  76.                 contact.setBackground(QtGui.QColor(CONTACT_BACKGROUND_COLOR))
  77.             else:
  78.                 contact.setBackground(QtGui.QColor(NON_CONTACT_BACKGROUND_COLOR))
  79.             self.new_message_icon(contact)
  80.  
  81.     def item_double_clicked(self):
  82.         self.currentItem().messages_count = 0
  83.         self.currentItem().setBackground(QtGui.QColor(NO_COLOR))
  84.         self.currentItem().setIcon(QtGui.QIcon(''))
  85.  
  86.     def add_contact(self, contact_name):
  87.         contact = self.findItems(contact_name, QtCore.Qt.MatchExactly)
  88.         if contact:
  89.             new_contact = contact[0]
  90.             self.contact_added(new_contact)
  91.         else:
  92.             new_contact = self.addItem(contact_name)
  93.         new_contact.in_contacts = True
  94.  
  95.     def add_contacts(self, contacts_list):
  96.         for contact in contacts_list:
  97.             self.add_contact(contact)
  98.  
  99.     def addItem(self, *__args):
  100.         item = MyListItem(*__args)
  101.         super().addItem(item)
  102.         return item
  103.  
  104.  
  105. class MyListItem(QtWidgets.QListWidgetItem):
  106.     def __init__(self, parent=None):
  107.         super().__init__(parent)
  108.         self.messages_count = 0
  109.         self.in_contacts = False
  110.         self.setSizeHint(QtCore.QSize(-1, STRING_HIGH))
  111.  
  112.  
  113. class MyTextEditWidget(QtWidgets.QTextEdit):
  114.     send_message_signal = QtCore.pyqtSignal()
  115.  
  116.     def __init__(self, parent=None):
  117.         super().__init__(parent)
  118.         self.send_message_shortcut_id = self.grabShortcut(QtGui.QKeySequence(QtCore.Qt.CTRL + QtCore.Qt.Key_Return))
  119.  
  120.     def event(self, event_):
  121.         if event_.type() == QtCore.QEvent.Shortcut:
  122.             if event_.shortcutId() == self.send_message_shortcut_id:
  123.                 self.send_message_signal.emit()
  124.         return super().event(event_)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement