This repository has been archived by the owner on Apr 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
view.py
194 lines (134 loc) · 6.6 KB
/
view.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/python
# coding: utf-8
from PyQt5 import QtGui, QtWidgets, QtCore
class ViewPerso(QtWidgets.QTableView):
"""New class to modify the view. Basically reimplements some methods.
Generates a personnal table view, which will be used for each tab in the
main window"""
def __init__(self, parent=None):
super(ViewPerso, self).__init__(parent)
self.parent = parent
self.defineSlots()
self.name_search = None
self.base_query = None
self.topic_entries = None
self.author_entries = None
self.radio_states = None
self.articles = {}
self.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
# Scroll per "pixel". Gives a better impression when scrolling
self.setVerticalScrollMode(QtWidgets.QAbstractItemView.ScrollPerPixel)
def defineSlots(self):
# Double-click opens in the browser
self.doubleClicked.connect(self.parent.openInBrowser)
# http://www.diotavelli.net/PyQtWiki/Handling%20context%20menus
# Personal right-click
self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.parent.popup)
self.clicked.connect(self.parent.displayInfos)
self.clicked.connect(self.parent.markOneRead)
def mousePressEvent(self, e):
"""Adding some actions to the normal mousePressEvent.
Determine if the user clicked in the right bottom corner.
If yes, the user clicked on the 'like star', so the liked
state is toggled from the parent toggleLike method"""
# Constant to set the size of the zone in the bottom right
# corner, where the user can click to toggle liked state
DIMENSION = self.parent.styles.ICON_SIZE_SMALL
# Attribute to shunt parent.markOneRead if the user is clicking
# on the read/unread icon
self.toread_icon = False
# Call the parent class function
super(ViewPerso, self).mousePressEvent(e)
# Get the current cell as a QRect
rect = self.visualRect(self.selectionModel().currentIndex())
# Get the coordinates of the mouse click
x, y = e.x(), e.y()
# area_y: to check if the click is in the bottom part of the post
# area_*_x: to check if the click is on the to-read icon,
# or the like icon
area_like_x, area_wait_x, area_y = False, False, False
# If the click was on the right bottom corner, start the real buisness
if y <= rect.y() + rect.height() and y >= rect.y() + rect.height() - DIMENSION:
area_y = True
if x <= rect.x() + rect.width() and x >= rect.x() + rect.width() - DIMENSION:
area_like_x = True
if x >= rect.x() + rect.width() - 2 * DIMENSION and x <= rect.x() + rect.width() - DIMENSION:
area_wait_x = True
if area_like_x and area_y:
self.parent.toggleLike()
# Emit a clicked signal, otherwise the user can like an article
# while the article is not marked as read
self.clicked.emit(self.selectionModel().currentIndex())
# Icon to-read
elif area_wait_x and area_y:
self.toread_icon = True
self.parent.toggleWait()
def updateHeight(self):
self.verticalHeader().setDefaultSectionSize(self.height() * 0.17)
def resizeCells(self, new_size):
"""Slot triggered by the parent when the central splitter is moved.
Allows to resize the columns of the table to optimize space.
new_size is the width of the central splitter of the parent"""
# The thumbnail's size is set to 30 % of the view's width
# The rest is filled with the stretchable title
self.setColumnWidth(8, new_size * 0.3)
def initUI(self):
# Turn off the horizontal scrollBar. Not necessary, and ugly
self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
# Header style
self.hideColumn(0) # Hide id
self.hideColumn(1) # Hide percentage match
self.hideColumn(2) # Hide doi
self.hideColumn(4) # Hide date
self.hideColumn(5) # Hide journals
self.hideColumn(6) # Hide authors
self.hideColumn(7) # Hide abstracts
self.hideColumn(9) # Hide like
self.hideColumn(10) # Hide urls
self.hideColumn(11) # Hide new
self.hideColumn(12) # Hide topic_simple
self.hideColumn(13) # Hide author_simple
# Stretch the title into the remaining space
self.horizontalHeader().setSectionResizeMode(3,
QtWidgets.QHeaderView.Stretch)
# Move the graphical abstract to first
self.horizontalHeader().moveSection(8, 0)
self.verticalHeader().setVisible(False)
self.horizontalHeader().setVisible(False)
# No cell editing
self.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
def keyboardSearch(self, search):
"""Reimplementation to deactivate the keyboard search"""
pass
def keyPressEvent(self, e):
"""Reimplementation to propagate the event to the parent
widget. Also, defines some special behaviors"""
# super(ViewPerso, self).keyPressEvent(e)
key = e.key()
# To avoid a bug: the user scrolls the articles w/ the keyboard,
# put an article in the toread list, and then continues scrolling.
# The posts are not marked as read anymore
if key == QtCore.Qt.Key_Down or key == QtCore.Qt.Key_Up:
self.toread_icon = False
# Browsing with up and down keys. Verifications made for
# when the selection is completely at the top or the bottom
if key == QtCore.Qt.Key_Down:
current_index = self.selectionModel().currentIndex()
if not current_index.isValid():
self.selectRow(0)
current_index = self.selectionModel().currentIndex()
else:
new_index = current_index.sibling(current_index.row() + 1, current_index.column())
if new_index.isValid():
current_index = new_index
self.setCurrentIndex(current_index)
self.clicked.emit(current_index)
if key == QtCore.Qt.Key_Up:
current_index = self.selectionModel().currentIndex()
new_index = current_index.sibling(current_index.row() - 1, current_index.column())
if new_index.isValid():
current_index = new_index
self.setCurrentIndex(current_index)
self.clicked.emit(current_index)
e.ignore()