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
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 util import curr_directory, convert_time
from util import curr_directory, convert_time, curr_time
from widgets import DataLabel, create_menu
import cgi
import smileys
@ -408,32 +408,67 @@ class UnsentFileItem(FileTransferItem):
TOX_FILE_TRANSFER_STATE['PAUSED_BY_FRIEND'], width, parent)
self._time = time
self.pb.setVisible(False)
movie = QtGui.QMovie(curr_directory() + '/images/spinner.gif')
self.time.setMovie(movie)
movie.start()
def cancel_transfer(self, *args):
pr = profile.Profile.get_instance()
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)
self.resize(QtCore.QSize(width, 500))
QtGui.QScrollArea.__init__(self)
self.setFocusPolicy(QtCore.Qt.NoFocus)
self._elem = elem
self._image_label = QtGui.QLabel(self)
self._image_label.raise_()
self._image_label.setAutoFillBackground(True)
self.setWidget(self._image_label)
self._image_label.setScaledContents(False)
self.pixmap = QtGui.QPixmap()
self.pixmap.loadFromData(QtCore.QByteArray(data), "PNG")
max_size = width - 40
if self.pixmap.width() <= max_size:
self._image_label.setPixmap(self.pixmap)
self.resize(QtCore.QSize(max_size, self.pixmap.height()))
self._pixmap = QtGui.QPixmap()
self._pixmap.loadFromData(QtCore.QByteArray(data), "PNG")
self._max_size = width - 30
self._resize_needed = not (self._pixmap.width() <= self._max_size)
self._full_size = not self._resize_needed
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:
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.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):
return False

View File

@ -540,8 +540,8 @@ class Profile(contact.Contact, Singleton):
self._messages.setItemWidget(elem, item)
def create_inline_item(self, data, append=True):
item = InlineImageItem(data, self._messages.width())
elem = QtGui.QListWidgetItem()
item = InlineImageItem(data, self._messages.width(), elem)
elem.setSizeHint(QtCore.QSize(self._messages.width(), item.height()))
if append:
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,
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)]
if by_friend:
tr.state = TOX_FILE_TRANSFER_STATE['RUNNING']
@ -944,6 +948,7 @@ class Profile(contact.Contact, Singleton):
self.update()
return
elif friend.status is None and is_resend:
print 'Error in sending'
raise RuntimeError()
st = SendTransfer(path, self._tox, friend_number)
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():
count = self._messages.count()
if count + i + 1 >= 0:
item = InlineImageItem(transfer.get_data(), self._messages.width())
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.setItemWidget(elem, item)
else:
@ -1007,9 +1012,9 @@ class Profile(contact.Contact, Singleton):
if friend_number == self.get_active_number():
count = self._messages.count()
if count + i + 1 >= 0:
item = InlineImageItem(transfer.get_data(), self._messages.width())
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.setItemWidget(elem, item)
else:

View File

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