smileys update, inline images improvements

This commit is contained in:
ingvar1995 2016-06-20 20:16:38 +03:00
parent 65930716f9
commit 3cb961957c
3 changed files with 74 additions and 29 deletions

View file

@ -5,7 +5,7 @@ except ImportError:
from PyQt4 import QtCore, QtGui from PyQt4 import QtCore, QtGui
import profile import profile
from file_transfers import TOX_FILE_TRANSFER_STATE, PAUSED_FILE_TRANSFERS, DO_NOT_SHOW_ACCEPT_BUTTON, ACTIVE_FILE_TRANSFERS, SHOW_PROGRESS_BAR from file_transfers import TOX_FILE_TRANSFER_STATE, PAUSED_FILE_TRANSFERS, DO_NOT_SHOW_ACCEPT_BUTTON, ACTIVE_FILE_TRANSFERS, SHOW_PROGRESS_BAR
from util import curr_directory, convert_time from util import curr_directory, convert_time, curr_time
from widgets import DataLabel, create_menu from widgets import DataLabel, create_menu
import cgi import cgi
import smileys import smileys
@ -408,32 +408,67 @@ class UnsentFileItem(FileTransferItem):
TOX_FILE_TRANSFER_STATE['PAUSED_BY_FRIEND'], width, parent) TOX_FILE_TRANSFER_STATE['PAUSED_BY_FRIEND'], width, parent)
self._time = time self._time = time
self.pb.setVisible(False) self.pb.setVisible(False)
movie = QtGui.QMovie(curr_directory() + '/images/spinner.gif')
self.time.setMovie(movie)
movie.start()
def cancel_transfer(self, *args): def cancel_transfer(self, *args):
pr = profile.Profile.get_instance() pr = profile.Profile.get_instance()
pr.cancel_not_started_transfer(self._time) pr.cancel_not_started_transfer(self._time)
class InlineImageItem(QtGui.QWidget): class InlineImageItem(QtGui.QScrollArea):
def __init__(self, data, width, parent=None): def __init__(self, data, width, elem):
QtGui.QWidget.__init__(self, parent) QtGui.QScrollArea.__init__(self)
self.resize(QtCore.QSize(width, 500)) self.setFocusPolicy(QtCore.Qt.NoFocus)
self._elem = elem
self._image_label = QtGui.QLabel(self) self._image_label = QtGui.QLabel(self)
self._image_label.raise_() self._image_label.raise_()
self._image_label.setAutoFillBackground(True) self.setWidget(self._image_label)
self._image_label.setScaledContents(False) self._image_label.setScaledContents(False)
self.pixmap = QtGui.QPixmap() self._pixmap = QtGui.QPixmap()
self.pixmap.loadFromData(QtCore.QByteArray(data), "PNG") self._pixmap.loadFromData(QtCore.QByteArray(data), "PNG")
max_size = width - 40 self._max_size = width - 30
if self.pixmap.width() <= max_size: self._resize_needed = not (self._pixmap.width() <= self._max_size)
self._image_label.setPixmap(self.pixmap) self._full_size = not self._resize_needed
self.resize(QtCore.QSize(max_size, self.pixmap.height())) if not self._resize_needed:
self._image_label.setPixmap(self._pixmap)
self.resize(QtCore.QSize(self._max_size + 5, self._pixmap.height()))
self._image_label.setGeometry(5, 0, self._max_size + 5, self._pixmap.height())
else: else:
pixmap = self.pixmap.scaled(max_size, max_size, QtCore.Qt.KeepAspectRatio) pixmap = self._pixmap.scaled(self._max_size, self._max_size, QtCore.Qt.KeepAspectRatio)
self._image_label.setPixmap(pixmap) self._image_label.setPixmap(pixmap)
self.resize(QtCore.QSize(max_size, pixmap.height())) self.resize(QtCore.QSize(self._max_size, pixmap.height()))
self._image_label.setGeometry(5, 0, self._max_size + 5, pixmap.height())
self._elem.setSizeHint(QtCore.QSize(self.width(), self.height()))
def mouseReleaseEvent(self, event):
if event.button() == QtCore.Qt.LeftButton and self._resize_needed:
if self._full_size:
pixmap = self._pixmap.scaled(self._max_size, self._max_size, QtCore.Qt.KeepAspectRatio)
self._image_label.setPixmap(pixmap)
self.resize(QtCore.QSize(self._max_size, pixmap.height()))
self._image_label.setGeometry(5, 0, self._max_size + 5, pixmap.height())
else:
self._image_label.setPixmap(self._pixmap)
self.resize(QtCore.QSize(self._max_size, self._pixmap.height() + 15))
self._image_label.setGeometry(5, 0, self._pixmap.width(), self._pixmap.height() + 15)
self._full_size = not self._full_size
self._elem.setSizeHint(QtCore.QSize(self.width(), self.height()))
elif event.button() == QtCore.Qt.RightButton:
directory = QtGui.QFileDialog.getExistingDirectory(self,
QtGui.QApplication.translate("MainWindow",
'Choose folder', None,
QtGui.QApplication.UnicodeUTF8),
curr_directory(),
QtGui.QFileDialog.ShowDirsOnly)
if directory:
fl = QtCore.QFile(directory + '/toxygen_inline_' + curr_time().replace(':', '_') + '.png')
self._pixmap.save(fl, 'PNG')
return False
def mark_as_sent(self): def mark_as_sent(self):
return False return False

View file

@ -540,8 +540,8 @@ class Profile(contact.Contact, Singleton):
self._messages.setItemWidget(elem, item) self._messages.setItemWidget(elem, item)
def create_inline_item(self, data, append=True): def create_inline_item(self, data, append=True):
item = InlineImageItem(data, self._messages.width())
elem = QtGui.QListWidgetItem() elem = QtGui.QListWidgetItem()
item = InlineImageItem(data, self._messages.width(), elem)
elem.setSizeHint(QtCore.QSize(self._messages.width(), item.height())) elem.setSizeHint(QtCore.QSize(self._messages.width(), item.height()))
if append: if append:
self._messages.addItem(elem) self._messages.addItem(elem)
@ -866,6 +866,10 @@ class Profile(contact.Contact, Singleton):
""" """
self.get_friend_by_number(friend_number).update_transfer_data(file_number, self.get_friend_by_number(friend_number).update_transfer_data(file_number,
TOX_FILE_TRANSFER_STATE['RUNNING']) TOX_FILE_TRANSFER_STATE['RUNNING'])
# if (friend_number, file_number) not in self._file_transfers:
# print self._file_transfers
# print (friend_number, file_number)
# return
tr = self._file_transfers[(friend_number, file_number)] tr = self._file_transfers[(friend_number, file_number)]
if by_friend: if by_friend:
tr.state = TOX_FILE_TRANSFER_STATE['RUNNING'] tr.state = TOX_FILE_TRANSFER_STATE['RUNNING']
@ -944,6 +948,7 @@ class Profile(contact.Contact, Singleton):
self.update() self.update()
return return
elif friend.status is None and is_resend: elif friend.status is None and is_resend:
print 'Error in sending'
raise RuntimeError() raise RuntimeError()
st = SendTransfer(path, self._tox, friend_number) st = SendTransfer(path, self._tox, friend_number)
self._file_transfers[(friend_number, st.get_file_number())] = st self._file_transfers[(friend_number, st.get_file_number())] = st
@ -978,9 +983,9 @@ class Profile(contact.Contact, Singleton):
if friend_number == self.get_active_number(): if friend_number == self.get_active_number():
count = self._messages.count() count = self._messages.count()
if count + i + 1 >= 0: if count + i + 1 >= 0:
item = InlineImageItem(transfer.get_data(), self._messages.width())
elem = QtGui.QListWidgetItem() elem = QtGui.QListWidgetItem()
elem.setSizeHint(QtCore.QSize(600, item.height())) item = InlineImageItem(transfer.get_data(), self._messages.width(), elem)
elem.setSizeHint(QtCore.QSize(self._messages.width(), item.height()))
self._messages.insertItem(count + i + 1, elem) self._messages.insertItem(count + i + 1, elem)
self._messages.setItemWidget(elem, item) self._messages.setItemWidget(elem, item)
else: else:
@ -1007,9 +1012,9 @@ class Profile(contact.Contact, Singleton):
if friend_number == self.get_active_number(): if friend_number == self.get_active_number():
count = self._messages.count() count = self._messages.count()
if count + i + 1 >= 0: if count + i + 1 >= 0:
item = InlineImageItem(transfer.get_data(), self._messages.width())
elem = QtGui.QListWidgetItem() elem = QtGui.QListWidgetItem()
elem.setSizeHint(QtCore.QSize(600, item.height())) item = InlineImageItem(transfer.get_data(), self._messages.width(), elem)
elem.setSizeHint(QtCore.QSize(self._messages.width(), item.height()))
self._messages.insertItem(count + i + 1, elem) self._messages.insertItem(count + i + 1, elem)
self._messages.setItemWidget(elem, item) self._messages.setItemWidget(elem, item)
else: else:

View file

@ -1,6 +1,7 @@
import util import util
import json import json
import os import os
from collections import OrderedDict
try: try:
from PySide import QtCore from PySide import QtCore
except ImportError: except ImportError:
@ -16,7 +17,7 @@ class SmileyLoader(util.Singleton):
self._settings = settings self._settings = settings
self._curr_pack = None # current pack name self._curr_pack = None # current pack name
self._smileys = {} # smileys dict. key - smiley (str), value - path to image (str) self._smileys = {} # smileys dict. key - smiley (str), value - path to image (str)
self._set = {} # smileys dict without duplicates self._list = [] # smileys list without duplicates
self.load_pack() self.load_pack()
def load_pack(self): def load_pack(self):
@ -30,16 +31,20 @@ class SmileyLoader(util.Singleton):
try: try:
with open(path) as fl: with open(path) as fl:
self._smileys = json.loads(fl.read()) self._smileys = json.loads(fl.read())
print 'Smiley pack', pack_name, 'loaded' fl.seek(0)
self._set = {} tmp = json.loads(fl.read(), object_pairs_hook=OrderedDict)
for key, value in self._smileys.items(): print 'Smiley pack {} loaded'.format(pack_name)
keys, values, self._list = [], [], []
for key, value in tmp.items():
value = self.get_smileys_path() + value value = self.get_smileys_path() + value
if value not in self._set.values(): if value not in values:
self._set[key] = value keys.append(key)
except: values.append(value)
self._list = zip(keys, values)
except Exception as ex:
self._smileys = {} self._smileys = {}
self._set = {} self._list = []
print 'Smiley pack', pack_name, 'was not loaded' print 'Smiley pack {} was not loaded. Error: {}'.format(pack_name, ex)
def get_smileys_path(self): def get_smileys_path(self):
return util.curr_directory() + '/smileys/' + self._curr_pack + '/' return util.curr_directory() + '/smileys/' + self._curr_pack + '/'
@ -49,7 +54,7 @@ class SmileyLoader(util.Singleton):
return [x[1] for x in os.walk(d)][0] return [x[1] for x in os.walk(d)][0]
def get_smileys(self): def get_smileys(self):
return list(self._set.items()) return self._list[:]
def add_smileys_to_text(self, text, edit): def add_smileys_to_text(self, text, edit):
""" """