File emblems working with QStyledItemDelegate

This commit is contained in:
BasioMeusPuga
2017-11-10 18:11:24 +05:30
parent 081406efc5
commit 0f6af1f641
3 changed files with 47 additions and 6 deletions

View File

@@ -41,7 +41,7 @@ import mainwindow
import database
import book_parser
from widgets import LibraryToolBar, BookToolBar, Tab
from widgets import LibraryToolBar, BookToolBar, Tab, LibraryDelegate
from subclasses import Settings, Library
@@ -96,6 +96,7 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
self.listView.setGridSize(QtCore.QSize(175, 240))
self.listView.verticalScrollBar().setSingleStep(7)
self.listView.doubleClicked.connect(self.list_doubleclick)
self.listView.setItemDelegate(LibraryDelegate())
self.reload_listview()
# Keyboard shortcuts

View File

@@ -36,6 +36,13 @@ class Library:
book_year = i[3]
book_cover = i[8]
book_tags = i[6]
book_path = i[4]
book_progress = None # TODO
# Leave at None for an untouched book
# 'completed' for a completed book
# whatever else is here can be used
# to remember position
all_metadata = {
'book_title': i[1],
'book_author': i[2],
@@ -55,6 +62,21 @@ class Library:
if book_tags:
search_workaround += book_tags
# Generate book state for passing onto the QStyledItemDelegate
def generate_book_state(book_path, book_progress):
if not os.path.exists(book_path):
return 'deleted'
if book_progress:
if book_progress == 'completed':
return 'completed'
else:
return 'inprogress'
else:
return None
book_state = generate_book_state(book_path, book_progress)
# Generate image pixmap and then pass it to the widget
# as a QIcon
# Additional data can be set using an incrementing
@@ -72,6 +94,7 @@ class Library:
item.setData(book_year, QtCore.Qt.UserRole + 2)
item.setData(all_metadata, QtCore.Qt.UserRole + 3)
item.setData(search_workaround, QtCore.Qt.UserRole + 4)
item.setData(book_state, QtCore.Qt.UserRole + 5)
item.setIcon(QtGui.QIcon(img_pixmap))
self.parent_window.viewModel.appendRow(item)

View File

@@ -24,7 +24,7 @@ class BookToolBar(QtWidgets.QToolBar):
self.fullscreenButton = QtWidgets.QAction(
QtGui.QIcon.fromTheme('view-fullscreen'), 'Fullscreen', self)
self.fontButton = QtWidgets.QAction(
QtGui.QIcon.fromTheme('gtk-select-font'), 'Format view', self)
QtGui.QIcon.fromTheme('gtk-select-font'), 'Font settings', self)
self.settingsButton = QtWidgets.QAction(
QtGui.QIcon.fromTheme('settings'), 'Settings', self)
@@ -165,7 +165,8 @@ class LibraryToolBar(QtWidgets.QToolBar):
super(LibraryToolBar, self).__init__(parent)
spacer = QtWidgets.QWidget()
spacer.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
spacer.setSizePolicy(
QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
self.setMovable(False)
self.setIconSize(QtCore.QSize(22, 22))
@@ -238,8 +239,24 @@ class Tab(QtWidgets.QWidget):
self.textEdit.setText(book_path)
class BookSettingsDock(QtWidgets.QDockWidget):
class LibraryDelegate(QtWidgets.QStyledItemDelegate):
def __init__(self, parent=None):
super(BookSettingsDock, self).__init__(parent)
super(LibraryDelegate, self).__init__(parent)
print(dir(self))
def paint(self, painter, option, index):
QtWidgets.QStyledItemDelegate.paint(self, painter, option, index)
option = option.__class__(option)
book_state = index.data(QtCore.Qt.UserRole + 5)
if book_state:
if book_state == 'deleted':
read_icon = QtGui.QIcon.fromTheme('vcs-conflicting').pixmap(36)
if book_state == 'completed':
read_icon = QtGui.QIcon.fromTheme('vcs-normal').pixmap(36)
if book_state == 'inprogress':
read_icon = QtGui.QIcon.fromTheme('vcs-locally-modified').pixmap(36)
else:
return
x_draw = option.rect.bottomRight().x() - 30
y_draw = option.rect.bottomRight().y() - 35
painter.drawPixmap(x_draw, y_draw, read_icon)