some improvements

This commit is contained in:
ingvar1995 2016-03-29 17:11:30 +03:00
parent 3b5e869055
commit ce7f6e03f5
5 changed files with 39 additions and 22 deletions

View File

@ -1,9 +1,11 @@
# coding=utf-8 # coding=utf-8
from sqlite3 import connect from sqlite3 import connect
from settings import Settings import profile as pr
from os import chdir from os import chdir
PAGE_SIZE = 42
MESSAGE_OWNER = { MESSAGE_OWNER = {
'ME': 0, 'ME': 0,
'FRIEND': 1 'FRIEND': 1
@ -13,7 +15,7 @@ MESSAGE_OWNER = {
class History(object): class History(object):
def __init__(self, name): def __init__(self, name):
self._name = name self._name = name
chdir(Settings.get_default_path()) chdir(pr.ProfileHelper.get_path())
db = connect(name + '.hstr') db = connect(name + '.hstr')
cursor = db.cursor() cursor = db.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS friends(' cursor.execute('CREATE TABLE IF NOT EXISTS friends('
@ -22,7 +24,7 @@ class History(object):
db.close() db.close()
def export(self, directory): def export(self, directory):
path = Settings.get_default_path() + self._name + '.hstr' path = pr.ProfileHelper.get_path() + self._name + '.hstr'
new_path = directory + self._name + '.hstr' new_path = directory + self._name + '.hstr'
with open(path, 'rb') as fin: with open(path, 'rb') as fin:
data = fin.read() data = fin.read()
@ -31,7 +33,7 @@ class History(object):
print 'History exported to: {}'.format(new_path) print 'History exported to: {}'.format(new_path)
def add_friend_to_db(self, tox_id): def add_friend_to_db(self, tox_id):
chdir(Settings.get_default_path()) chdir(pr.ProfileHelper.get_path())
db = connect(self._name + '.hstr') db = connect(self._name + '.hstr')
try: try:
cursor = db.cursor() cursor = db.cursor()
@ -51,7 +53,7 @@ class History(object):
db.close() db.close()
def delete_friend_from_db(self, tox_id): def delete_friend_from_db(self, tox_id):
chdir(Settings.get_default_path()) chdir(pr.ProfileHelper.get_path())
db = connect(self._name + '.hstr') db = connect(self._name + '.hstr')
try: try:
cursor = db.cursor() cursor = db.cursor()
@ -65,7 +67,7 @@ class History(object):
db.close() db.close()
def friend_exists_in_db(self, tox_id): def friend_exists_in_db(self, tox_id):
chdir(Settings.get_default_path()) chdir(pr.ProfileHelper.get_path())
db = connect(self._name + '.hstr') db = connect(self._name + '.hstr')
cursor = db.cursor() cursor = db.cursor()
cursor.execute('SELECT 0 FROM friends WHERE tox_id=?', (tox_id, )) cursor.execute('SELECT 0 FROM friends WHERE tox_id=?', (tox_id, ))
@ -74,7 +76,7 @@ class History(object):
return result is not None return result is not None
def save_messages_to_db(self, tox_id, messages_iter): def save_messages_to_db(self, tox_id, messages_iter):
chdir(Settings.get_default_path()) chdir(pr.ProfileHelper.get_path())
db = connect(self._name + '.hstr') db = connect(self._name + '.hstr')
try: try:
cursor = db.cursor() cursor = db.cursor()
@ -88,7 +90,7 @@ class History(object):
db.close() db.close()
def delete_messages(self, tox_id): def delete_messages(self, tox_id):
chdir(Settings.get_default_path()) chdir(pr.ProfileHelper.get_path())
db = connect(self._name + '.hstr') db = connect(self._name + '.hstr')
try: try:
cursor = db.cursor() cursor = db.cursor()
@ -105,7 +107,7 @@ class History(object):
class MessageGetter(object): class MessageGetter(object):
def __init__(self, name, tox_id): def __init__(self, name, tox_id):
chdir(Settings.get_default_path()) chdir(pr.ProfileHelper.get_path())
self._db = connect(name + '.hstr') self._db = connect(name + '.hstr')
self._cursor = self._db.cursor() self._cursor = self._db.cursor()
self._cursor.execute('SELECT message, owner, unix_time, message_type FROM id' + tox_id + self._cursor.execute('SELECT message, owner, unix_time, message_type FROM id' + tox_id +

View File

@ -60,9 +60,9 @@ class MessageItem(QtGui.QWidget):
self.message.setStyleSheet("QTextEdit { color: #4169E1; }") self.message.setStyleSheet("QTextEdit { color: #4169E1; }")
else: else:
if text[0] == '>': if text[0] == '>':
self.message.setStyleSheet("QPlainTextEdit { color: green; }") self.message.setStyleSheet("QTextEdit { color: green; }")
if text[-1] == '<': if text[-1] == '<':
self.message.setStyleSheet("QPlainTextEdit { color: red; }") self.message.setStyleSheet("QTextEdit { color: red; }")
class ContactItem(QtGui.QWidget): class ContactItem(QtGui.QWidget):

View File

@ -13,6 +13,10 @@ class MessageArea(QtGui.QPlainTextEdit):
def keyPressEvent(self, event): def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Return: if event.key() == QtCore.Qt.Key_Return:
modifiers = event.modifiers()
if modifiers & QtCore.Qt.ControlModifier or modifiers & QtCore.Qt.ShiftModifier:
self.appendPlainText('')
else:
self.parent.send_message() self.parent.send_message()
else: else:
super(self.__class__, self).keyPressEvent(event) super(self.__class__, self).keyPressEvent(event)
@ -384,7 +388,7 @@ class MainWindow(QtGui.QMainWindow):
class ScreenShotWindow(QtGui.QWidget): class ScreenShotWindow(QtGui.QWidget):
# TODO: make window semi-transparent # TODO: make selected area transparent
def __init__(self): def __init__(self):
super(ScreenShotWindow, self).__init__() super(ScreenShotWindow, self).__init__()
self.setMouseTracking(True) self.setMouseTracking(True)
@ -405,6 +409,7 @@ class ScreenShotWindow(QtGui.QWidget):
self.rubberband.setGeometry( self.rubberband.setGeometry(
QtCore.QRect(self.origin, event.pos()).normalized()) QtCore.QRect(self.origin, event.pos()).normalized())
QtGui.QWidget.mouseMoveEvent(self, event) QtGui.QWidget.mouseMoveEvent(self, event)
self.repaint()
def mouseReleaseEvent(self, event): def mouseReleaseEvent(self, event):
if self.rubberband.isVisible(): if self.rubberband.isVisible():
@ -425,3 +430,4 @@ class ScreenShotWindow(QtGui.QWidget):

View File

@ -1,5 +1,4 @@
PAGE_SIZE = 42
MESSAGE_TYPE = { MESSAGE_TYPE = {
'TEXT': 0, 'TEXT': 0,

View File

@ -227,7 +227,8 @@ class Friend(Contact):
data.reverse() data.reverse()
else: else:
return [] return []
self._corr = map(lambda tupl: TextMessage(*tupl), data) + self._corr data = map(lambda tupl: TextMessage(*tupl), data)
self._corr = data + self._corr
self._history_loaded = True self._history_loaded = True
return data return data
@ -258,7 +259,7 @@ class Friend(Contact):
""" """
if hasattr(self, '_message_getter'): if hasattr(self, '_message_getter'):
del self._message_getter del self._message_getter
self._corr = [] self._corr = filter(lambda x: x.get_type() > 1, self._corr)
self._unsaved_messages = 0 self._unsaved_messages = 0
def update_transfer_data(self, file_number, status): def update_transfer_data(self, file_number, status):
@ -595,12 +596,18 @@ class Profile(Contact, Singleton):
return return
data.reverse() data.reverse()
for message in data: for message in data:
if message.get_type() <= 1:
data = message.get_data() data = message.get_data()
self.create_message_item(data[0], self.create_message_item(data[0],
convert_time(data[2]), convert_time(data[2]),
friend.name if data[1] else self._name, friend.name if data[1] else self._name,
data[3], data[3],
False) False)
elif message.get_type() == 2:
item = self.create_file_transfer_item(message, False)
if message.get_status() in (2, 4):
ft = self._file_transfers[(message.get_friend_number(), message.get_file_number())]
ft.set_state_changed_handler(item.update)
def export_history(self, directory): def export_history(self, directory):
self._history.export(directory) self._history.export(directory)
@ -811,6 +818,7 @@ class Profile(Contact, Singleton):
file_number) file_number)
if friend_number == self.get_active_number(): if friend_number == self.get_active_number():
self.create_file_transfer_item(tm) self.create_file_transfer_item(tm)
self._messages.scrollToBottom()
else: else:
friend.set_messages(True) friend.set_messages(True)
friend.append_message(tm) friend.append_message(tm)
@ -867,6 +875,7 @@ class Profile(Contact, Singleton):
item = self.create_file_transfer_item(tm) item = self.create_file_transfer_item(tm)
friend.append_message(tm) friend.append_message(tm)
st.set_state_changed_handler(item.update) st.set_state_changed_handler(item.update)
self._messages.scrollToBottom()
def send_file(self, path): def send_file(self, path):
""" """
@ -886,6 +895,7 @@ class Profile(Contact, Singleton):
item = self.create_file_transfer_item(tm) item = self.create_file_transfer_item(tm)
st.set_state_changed_handler(item.update) st.set_state_changed_handler(item.update)
self._friends[self._active_friend].append_message(tm) self._friends[self._active_friend].append_message(tm)
self._messages.scrollToBottom()
def incoming_chunk(self, friend_number, file_number, position, data): def incoming_chunk(self, friend_number, file_number, position, data):
if (friend_number, file_number) in self._file_transfers: if (friend_number, file_number) in self._file_transfers: