Skip to content

Commit

Permalink
(#402 #14 #15) Drag & Drop / Amount values custom painting bug
Browse files Browse the repository at this point in the history
This commit fixes the issue with painting amount values by _not_ calling
the base class paint method in instances where a value painter is present.
This removes the need to suppress the table model from hiding that display
information.  The usage of customized ItemFlags which facilitated that
suppression of the model was removed.  Fixes #402 issue with drag and drop
because now the model provides the DisplayRole data again.
  • Loading branch information
brownnrl committed Dec 11, 2014
1 parent e85139d commit 313b504
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 25 deletions.
7 changes: 1 addition & 6 deletions qt/controller/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from PyQt4.QtGui import QFontMetrics

from qtlib.table import Table as TableBase, ItemFlags
from qtlib.table import Table as TableBase

from ..support.completable_edit import DescriptionEdit, PayeeEdit, AccountEdit
from ..support.column_view import AmountPainter
Expand Down Expand Up @@ -79,10 +79,5 @@ def _updateFontSize(self):
# in the custom drawing for the amount field.
self.view.resize(self.view.sizeHint())

def _getFlags(self, row, column):
has_painter = ItemFlags.ItemHasValuePainter if column.painter else 0
return TableBase._getFlags(self, row, column) | has_painter

def appPrefsChanged(self):
self._updateFontSize()

20 changes: 12 additions & 8 deletions qt/support/item_delegate.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,8 @@ def paint(self, painter, option, index):
option.decorationSize = QSize(decorationWidth, decorationHeight)
option.features |= QStyleOptionViewItemV4.HasDecoration
self._prepare_paint_options(option, index)
QStyledItemDelegate.paint(self, painter, option, index)

xOffset = 0
for dec in decorations:
pixmap = dec.pixmap
x = option.rect.right() - pixmap.width() - xOffset
y = option.rect.center().y() - (pixmap.height() // 2)
rect = QRect(x, y, pixmap.width(), pixmap.height())
painter.drawPixmap(rect, pixmap)
xOffset += pixmap.width()
# First added for #15, the painting of custom amount information. This can
# be used as a pattern for painting any column of information.
value_painter = self._get_value_painter(index)
Expand All @@ -137,6 +130,17 @@ def paint(self, painter, option, index):
rect = QRect(rect.left(), rect.top(), rect.width() - xOffset, rect.height())
value_option.rect = rect
value_painter.paint(painter, value_option, index)
else:
QStyledItemDelegate.paint(self, painter, option, index)

for dec in decorations:
pixmap = dec.pixmap
x = option.rect.right() - pixmap.width() - xOffset
y = option.rect.center().y() - (pixmap.height() // 2)
rect = QRect(x, y, pixmap.width(), pixmap.height())
painter.drawPixmap(rect, pixmap)
xOffset += pixmap.width()


def setModelData(self, editor, model, index):
# This call below is to give a chance to the editor to tweak its content a little bit before
Expand Down
12 changes: 1 addition & 11 deletions qtlib/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
from .column import Columns


class ItemFlags(Qt.ItemFlags):
ItemHasValuePainter = 128


class Table(QAbstractTableModel):
# Flags you want when index.isValid() is False. In those cases, _getFlags() is never called.
INVALID_INDEX_FLAGS = Qt.ItemIsEnabled
Expand Down Expand Up @@ -58,13 +54,7 @@ def _updateViewSelection(self):
# Virtual
def _getData(self, row, column, role):

# See moneyguru #14, #15 added to do custom painting of amounts
# but can be used as a pattern to paint custom values of other model attribute data.
has_custom_painter = self._getFlags(row, column) & ItemFlags.ItemHasValuePainter

if role == Qt.DisplayRole and has_custom_painter:
return None
elif role in (Qt.DisplayRole, Qt.EditRole):
if role in (Qt.DisplayRole, Qt.EditRole):
attrname = column.name
return row.get_cell_value(attrname)
elif role == Qt.TextAlignmentRole:
Expand Down

0 comments on commit 313b504

Please sign in to comment.