toxygen/src/mainscreen_widgets.py

313 lines
12 KiB
Python
Raw Normal View History

2016-06-13 21:32:45 +00:00
try:
from PySide import QtCore, QtGui
except ImportError:
from PyQt4 import QtCore, QtGui
2016-06-14 18:47:03 +00:00
from widgets import RubberBand, create_menu, QRightClickButton
2016-06-13 21:32:45 +00:00
from profile import Profile
2016-06-14 18:47:03 +00:00
import smileys
import util
2016-06-13 21:32:45 +00:00
class MessageArea(QtGui.QPlainTextEdit):
2016-06-15 18:02:59 +00:00
"""User types messages here"""
2016-06-13 21:32:45 +00:00
def __init__(self, parent, form):
super(MessageArea, self).__init__(parent)
self.parent = form
self.setAcceptDrops(True)
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(lambda: self.parent.profile.send_typing(False))
def keyPressEvent(self, event):
if event.matches(QtGui.QKeySequence.Paste):
self.pasteEvent()
elif event.key() in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter):
modifiers = event.modifiers()
if modifiers & QtCore.Qt.ControlModifier or modifiers & QtCore.Qt.ShiftModifier:
self.insertPlainText('\n')
else:
if self.timer.isActive():
self.timer.stop()
self.parent.profile.send_typing(False)
self.parent.send_message()
elif event.key() == QtCore.Qt.Key_Up and not self.toPlainText():
self.appendPlainText(Profile.get_instance().get_last_message())
else:
self.parent.profile.send_typing(True)
if self.timer.isActive():
self.timer.stop()
self.timer.start(5000)
super(MessageArea, self).keyPressEvent(event)
def contextMenuEvent(self, event):
menu = create_menu(self.createStandardContextMenu())
menu.exec_(event.globalPos())
del menu
def dragEnterEvent(self, e):
e.accept()
def dragMoveEvent(self, e):
e.accept()
def dropEvent(self, e):
if e.mimeData().hasFormat('text/plain'):
e.accept()
self.pasteEvent(e.mimeData().text())
else:
e.ignore()
def pasteEvent(self, text=None):
text = text or QtGui.QApplication.clipboard().text()
if text.startswith('file://'):
self.parent.profile.send_file(text[7:])
else:
self.insertPlainText(text)
class ScreenShotWindow(QtGui.QWidget):
def __init__(self, parent):
super(ScreenShotWindow, self).__init__()
self.parent = parent
self.setMouseTracking(True)
self.setWindowFlags(self.windowFlags() | QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)
self.showFullScreen()
self.setWindowOpacity(0.5)
self.rubberband = RubberBand()
def closeEvent(self, *args):
if self.parent.isHidden():
self.parent.show()
def mousePressEvent(self, event):
self.origin = event.pos()
self.rubberband.setGeometry(QtCore.QRect(self.origin, QtCore.QSize()))
self.rubberband.show()
QtGui.QWidget.mousePressEvent(self, event)
def mouseMoveEvent(self, event):
if self.rubberband.isVisible():
self.rubberband.setGeometry(QtCore.QRect(self.origin, event.pos()).normalized())
left = QtGui.QRegion(QtCore.QRect(0, 0, self.rubberband.x(), self.height()))
right = QtGui.QRegion(QtCore.QRect(self.rubberband.x() + self.rubberband.width(), 0, self.width(), self.height()))
top = QtGui.QRegion(0, 0, self.width(), self.rubberband.y())
bottom = QtGui.QRegion(0, self.rubberband.y() + self.rubberband.height(), self.width(), self.height())
self.setMask(left + right + top + bottom)
def mouseReleaseEvent(self, event):
if self.rubberband.isVisible():
self.rubberband.hide()
rect = self.rubberband.geometry()
if rect.width() and rect.height():
p = QtGui.QPixmap.grabWindow(QtGui.QApplication.desktop().winId(),
rect.x() + 4,
rect.y() + 4,
rect.width() - 8,
rect.height() - 8)
byte_array = QtCore.QByteArray()
buffer = QtCore.QBuffer(byte_array)
buffer.open(QtCore.QIODevice.WriteOnly)
p.save(buffer, 'PNG')
Profile.get_instance().send_screenshot(str(byte_array.data()))
self.close()
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
self.rubberband.setHidden(True)
self.close()
else:
super(ScreenShotWindow, self).keyPressEvent(event)
2016-06-14 18:47:03 +00:00
class SmileyWindow(QtGui.QWidget):
2016-06-15 18:02:59 +00:00
"""
Smiley selection window
"""
2016-06-14 18:47:03 +00:00
def __init__(self, parent):
super(SmileyWindow, self).__init__()
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
inst = smileys.SmileyLoader.get_instance()
self.data = inst.get_smileys()
count = len(self.data)
2016-06-15 18:02:59 +00:00
self.page_size = int(pow(count / 8, 0.5) + 1) * 8 # smileys per page
2016-06-14 18:47:03 +00:00
if count % self.page_size == 0:
self.page_count = count / self.page_size
else:
self.page_count = int(count / float(self.page_size) + 0.5)
self.page = 0
self.radio = []
self.parent = parent
2016-06-15 18:02:59 +00:00
for i in range(self.page_count): # buttons with smileys
2016-06-14 18:47:03 +00:00
elem = QtGui.QRadioButton(self)
elem.setGeometry(QtCore.QRect(i * 20 + 5, 180, 20, 20))
elem.clicked.connect(lambda i=i: self.checked(i))
self.radio.append(elem)
width = max(self.page_count * 20 + 30, (self.page_size + 5) * 8 // 10)
2016-06-14 18:47:03 +00:00
self.setMaximumSize(width, 200)
self.setMinimumSize(width, 200)
self.buttons = []
2016-06-15 18:02:59 +00:00
for i in range(self.page_size): # pages - radio buttons
2016-06-14 18:47:03 +00:00
b = QtGui.QPushButton(self)
b.setGeometry(QtCore.QRect((i // 8) * 20 + 5, (i % 8) * 20, 20, 20))
2016-06-14 18:47:03 +00:00
b.clicked.connect(lambda i=i: self.clicked(i))
self.buttons.append(b)
self.checked(0)
2016-06-15 18:02:59 +00:00
def checked(self, pos): # new page opened
2016-06-14 18:47:03 +00:00
self.radio[self.page].setChecked(False)
self.radio[pos].setChecked(True)
self.page = pos
start = self.page * self.page_size
for i in range(self.page_size):
try:
self.buttons[i].setVisible(True)
pixmap = QtGui.QPixmap(self.data[start + i][1])
icon = QtGui.QIcon(pixmap)
self.buttons[i].setIcon(icon)
except:
self.buttons[i].setVisible(False)
2016-06-15 18:02:59 +00:00
def clicked(self, pos): # smiley selected
2016-06-14 18:47:03 +00:00
pos += self.page * self.page_size
smiley = self.data[pos][0]
self.parent.messageEdit.insertPlainText(smiley)
self.close()
2016-06-15 18:02:59 +00:00
def leaveEvent(self, event):
self.close()
2016-06-14 18:47:03 +00:00
class MenuButton(QtGui.QPushButton):
def __init__(self, parent, enter):
super(MenuButton, self).__init__(parent)
self.enter = enter
def enterEvent(self, event):
self.enter()
super(MenuButton, self).enterEvent(event)
class DropdownMenu(QtGui.QWidget):
def __init__(self, parent):
super(DropdownMenu, self).__init__(parent)
self.installEventFilter(self)
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setMaximumSize(150, 100)
self.setMinimumSize(150, 100)
self.screenshotButton = QRightClickButton(self)
self.screenshotButton.setGeometry(QtCore.QRect(0, 50, 50, 50))
self.screenshotButton.setObjectName("screenshotButton")
self.fileTransferButton = QtGui.QPushButton(self)
self.fileTransferButton.setGeometry(QtCore.QRect(50, 50, 50, 50))
self.fileTransferButton.setObjectName("fileTransferButton")
self.audioMessageButton = QtGui.QPushButton(self)
self.audioMessageButton.setGeometry(QtCore.QRect(100, 50, 50, 50))
self.smileyButton = QtGui.QPushButton(self)
self.smileyButton.setGeometry(QtCore.QRect(0, 0, 50, 50))
self.videoMessageButton = QtGui.QPushButton(self)
self.videoMessageButton.setGeometry(QtCore.QRect(100, 0, 50, 50))
self.stickerButton = QtGui.QPushButton(self)
self.stickerButton.setGeometry(QtCore.QRect(50, 0, 50, 50))
pixmap = QtGui.QPixmap(util.curr_directory() + '/images/file.png')
icon = QtGui.QIcon(pixmap)
self.fileTransferButton.setIcon(icon)
self.fileTransferButton.setIconSize(QtCore.QSize(40, 40))
pixmap = QtGui.QPixmap(util.curr_directory() + '/images/screenshot.png')
icon = QtGui.QIcon(pixmap)
self.screenshotButton.setIcon(icon)
self.screenshotButton.setIconSize(QtCore.QSize(40, 50))
pixmap = QtGui.QPixmap(util.curr_directory() + '/images/audio_message.png')
icon = QtGui.QIcon(pixmap)
self.audioMessageButton.setIcon(icon)
self.audioMessageButton.setIconSize(QtCore.QSize(40, 40))
pixmap = QtGui.QPixmap(util.curr_directory() + '/images/smiley.png')
icon = QtGui.QIcon(pixmap)
self.smileyButton.setIcon(icon)
self.smileyButton.setIconSize(QtCore.QSize(40, 40))
pixmap = QtGui.QPixmap(util.curr_directory() + '/images/video_message.png')
icon = QtGui.QIcon(pixmap)
self.videoMessageButton.setIcon(icon)
self.videoMessageButton.setIconSize(QtCore.QSize(45, 45))
pixmap = QtGui.QPixmap(util.curr_directory() + '/images/sticker.png')
icon = QtGui.QIcon(pixmap)
self.stickerButton.setIcon(icon)
self.stickerButton.setIconSize(QtCore.QSize(45, 45))
self.screenshotButton.setToolTip(QtGui.QApplication.translate("MenuWindow", "Send screenshot", None, QtGui.QApplication.UnicodeUTF8))
self.fileTransferButton.setToolTip(QtGui.QApplication.translate("MenuWindow", "Send file", None, QtGui.QApplication.UnicodeUTF8))
self.audioMessageButton.setToolTip(QtGui.QApplication.translate("MenuWindow", "Send audio message", None, QtGui.QApplication.UnicodeUTF8))
self.videoMessageButton.setToolTip(QtGui.QApplication.translate("MenuWindow", "Send video message", None, QtGui.QApplication.UnicodeUTF8))
self.smileyButton.setToolTip(QtGui.QApplication.translate("MenuWindow", "Add smiley", None, QtGui.QApplication.UnicodeUTF8))
self.stickerButton.setToolTip(QtGui.QApplication.translate("MenuWindow", "Send sticker", None, QtGui.QApplication.UnicodeUTF8))
self.fileTransferButton.clicked.connect(parent.send_file)
self.screenshotButton.clicked.connect(parent.send_screenshot)
self.connect(self.screenshotButton, QtCore.SIGNAL("rightClicked()"), lambda: parent.send_screenshot(True))
self.smileyButton.clicked.connect(parent.send_smiley)
2016-06-14 20:55:41 +00:00
self.stickerButton.clicked.connect(parent.send_sticker)
2016-06-14 18:47:03 +00:00
def leaveEvent(self, event):
self.close()
def eventFilter(self, object, event):
if event.type() == QtCore.QEvent.WindowDeactivate:
self.close()
return False
2016-06-14 20:55:41 +00:00
class StickerItem(QtGui.QWidget):
def __init__(self, fl):
super(StickerItem, self).__init__()
self._image_label = QtGui.QLabel(self)
self.path = fl
self.pixmap = QtGui.QPixmap()
self.pixmap.load(fl)
2016-06-15 18:02:59 +00:00
if self.pixmap.width() > 150:
self.pixmap = self.pixmap.scaled(150, 200, QtCore.Qt.KeepAspectRatio)
2016-06-14 20:55:41 +00:00
self.setFixedSize(150, self.pixmap.height())
self._image_label.setPixmap(self.pixmap)
class StickerWindow(QtGui.QWidget):
2016-06-15 18:02:59 +00:00
"""Sticker selection window"""
2016-06-14 20:55:41 +00:00
def __init__(self, parent):
super(StickerWindow, self).__init__()
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setMaximumSize(250, 200)
self.setMinimumSize(250, 200)
2016-06-14 20:55:41 +00:00
self.list = QtGui.QListWidget(self)
self.list.setGeometry(QtCore.QRect(0, 0, 250, 200))
2016-06-14 20:55:41 +00:00
self.arr = smileys.sticker_loader()
for sticker in self.arr:
item = StickerItem(sticker)
elem = QtGui.QListWidgetItem()
elem.setSizeHint(QtCore.QSize(250, item.height()))
2016-06-14 20:55:41 +00:00
self.list.addItem(elem)
self.list.setItemWidget(elem, item)
self.list.setVerticalScrollMode(QtGui.QAbstractItemView.ScrollPerPixel)
self.list.setSpacing(3)
2016-06-14 20:55:41 +00:00
self.list.clicked.connect(self.click)
self.parent = parent
def click(self, index):
num = index.row()
self.parent.profile.send_sticker(self.arr[num])
self.close()
2016-06-15 18:02:59 +00:00
def leaveEvent(self, event):
self.close()
2016-06-14 20:55:41 +00:00