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
from sqlite3 import connect
from settings import Settings
import profile as pr
from os import chdir
PAGE_SIZE = 42
MESSAGE_OWNER = {
'ME': 0,
'FRIEND': 1
@ -13,7 +15,7 @@ MESSAGE_OWNER = {
class History(object):
def __init__(self, name):
self._name = name
chdir(Settings.get_default_path())
chdir(pr.ProfileHelper.get_path())
db = connect(name + '.hstr')
cursor = db.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS friends('
@ -22,7 +24,7 @@ class History(object):
db.close()
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'
with open(path, 'rb') as fin:
data = fin.read()
@ -31,7 +33,7 @@ class History(object):
print 'History exported to: {}'.format(new_path)
def add_friend_to_db(self, tox_id):
chdir(Settings.get_default_path())
chdir(pr.ProfileHelper.get_path())
db = connect(self._name + '.hstr')
try:
cursor = db.cursor()
@ -51,7 +53,7 @@ class History(object):
db.close()
def delete_friend_from_db(self, tox_id):
chdir(Settings.get_default_path())
chdir(pr.ProfileHelper.get_path())
db = connect(self._name + '.hstr')
try:
cursor = db.cursor()
@ -65,7 +67,7 @@ class History(object):
db.close()
def friend_exists_in_db(self, tox_id):
chdir(Settings.get_default_path())
chdir(pr.ProfileHelper.get_path())
db = connect(self._name + '.hstr')
cursor = db.cursor()
cursor.execute('SELECT 0 FROM friends WHERE tox_id=?', (tox_id, ))
@ -74,7 +76,7 @@ class History(object):
return result is not None
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')
try:
cursor = db.cursor()
@ -88,7 +90,7 @@ class History(object):
db.close()
def delete_messages(self, tox_id):
chdir(Settings.get_default_path())
chdir(pr.ProfileHelper.get_path())
db = connect(self._name + '.hstr')
try:
cursor = db.cursor()
@ -105,7 +107,7 @@ class History(object):
class MessageGetter(object):
def __init__(self, name, tox_id):
chdir(Settings.get_default_path())
chdir(pr.ProfileHelper.get_path())
self._db = connect(name + '.hstr')
self._cursor = self._db.cursor()
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; }")
else:
if text[0] == '>':
self.message.setStyleSheet("QPlainTextEdit { color: green; }")
self.message.setStyleSheet("QTextEdit { color: green; }")
if text[-1] == '<':
self.message.setStyleSheet("QPlainTextEdit { color: red; }")
self.message.setStyleSheet("QTextEdit { color: red; }")
class ContactItem(QtGui.QWidget):

View file

@ -13,6 +13,10 @@ class MessageArea(QtGui.QPlainTextEdit):
def keyPressEvent(self, event):
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()
else:
super(self.__class__, self).keyPressEvent(event)
@ -384,7 +388,7 @@ class MainWindow(QtGui.QMainWindow):
class ScreenShotWindow(QtGui.QWidget):
# TODO: make window semi-transparent
# TODO: make selected area transparent
def __init__(self):
super(ScreenShotWindow, self).__init__()
self.setMouseTracking(True)
@ -405,6 +409,7 @@ class ScreenShotWindow(QtGui.QWidget):
self.rubberband.setGeometry(
QtCore.QRect(self.origin, event.pos()).normalized())
QtGui.QWidget.mouseMoveEvent(self, event)
self.repaint()
def mouseReleaseEvent(self, event):
if self.rubberband.isVisible():
@ -425,3 +430,4 @@ class ScreenShotWindow(QtGui.QWidget):

View file

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

View file

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