Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 36 additions & 57 deletions Orange/widgets/data/owpaintdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import unicodedata
import itertools
from functools import partial
from collections import namedtuple

import numpy as np

Expand Down Expand Up @@ -71,7 +72,6 @@ def stack_on_condition(a, b, condition):
# Data manipulation operators
# ###########################

from collections import namedtuple
if sys.version_info < (3, 4):
# use singledispatch backports from pypi
from singledispatch import singledispatch
Expand Down Expand Up @@ -273,8 +273,7 @@ def mousePressEvent(self, event):
event.accept()
self.editingFinished.emit()
return True
else:
return super().mousePressEvent(event)
return super().mousePressEvent(event)


class PenTool(DataTool):
Expand All @@ -286,22 +285,19 @@ def mousePressEvent(self, event):
self.editingStarted.emit()
self.__handleEvent(event)
return True
else:
return super().mousePressEvent()
return super().mousePressEvent()

def mouseMoveEvent(self, event):
if event.buttons() & Qt.LeftButton:
self.__handleEvent(event)
return True
else:
return super().mouseMoveEvent()
return super().mouseMoveEvent()

def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.editingFinished.emit()
return True
else:
return super().mouseReleaseEvent()
return super().mouseReleaseEvent()

def __handleEvent(self, event):
pos = self.mapToPlot(event.pos())
Expand All @@ -328,23 +324,20 @@ def mousePressEvent(self, event):
self.__pos = self.mapToPlot(event.pos())
self.__timer.start()
return True
else:
return super().mousePressEvent(event)
return super().mousePressEvent(event)

def mouseMoveEvent(self, event):
if event.buttons() & Qt.LeftButton:
self.__pos = self.mapToPlot(event.pos())
return True
else:
return super().mouseMoveEvent(event)
return super().mouseMoveEvent(event)

def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.__timer.stop()
self.editingFinished.emit()
return True
else:
return super().mouseReleaseEvent(event)
return super().mouseReleaseEvent(event)

def __timout(self):
self.issueCommand.emit(
Expand All @@ -355,8 +348,7 @@ def __timout(self):
def random_state(rstate):
if isinstance(rstate, np.random.RandomState):
return rstate
else:
return np.random.RandomState(rstate)
return np.random.RandomState(rstate)


def create_data(x, y, radius, size, rstate):
Expand Down Expand Up @@ -384,23 +376,20 @@ def mousePressEvent(self, event):
self._pos = self.mapToPlot(event.pos())
self.__timer.start()
return True
else:
return super().mousePressEvent(event)
return super().mousePressEvent(event)

def mouseMoveEvent(self, event):
if event.buttons() & Qt.LeftButton:
self._pos = self.mapToPlot(event.pos())
return True
else:
return super().mouseMoveEvent(event)
return super().mouseMoveEvent(event)

def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.__timer.stop()
self.editingFinished.emit()
return True
else:
return super().mouseReleaseEvent(event)
return super().mouseReleaseEvent(event)

def __timeout(self):
self.issueCommand.emit(
Expand All @@ -427,23 +416,20 @@ def mousePressEvent(self, event):
self._pos = self.mapToPlot(event.pos())
self.__timer.start()
return True
else:
return super().mousePressEvent(event)
return super().mousePressEvent(event)

def mouseMoveEvent(self, event):
if event.buttons() & Qt.LeftButton:
self._pos = self.mapToPlot(event.pos())
return True
else:
return super().mouseMoveEvent(event)
return super().mouseMoveEvent(event)

def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self.__timer.stop()
self.editingFinished.emit()
return True
else:
return super().mouseReleaseEvent(event)
return super().mouseReleaseEvent(event)

def _do(self):
self.issueCommand.emit(
Expand Down Expand Up @@ -507,17 +493,15 @@ def mousePressEvent(self, event):
self.setSelectionRect(QRectF(pos, pos))
event.accept()
return True
else:
return super().mousePressEvent(event)
return super().mousePressEvent(event)

def mouseMoveEvent(self, event):
if event.buttons() & Qt.LeftButton:
pos = self.mapToPlot(event.pos())
self.setSelectionRect(QRectF(self._start_pos, pos).normalized())
event.accept()
return True
else:
return super().mouseMoveEvent(event)
return super().mouseMoveEvent(event)

def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
Expand All @@ -528,8 +512,7 @@ def mouseReleaseEvent(self, event):
self._item.setCursor(Qt.OpenHandCursor)
self._mouse_dragging = False
return True
else:
return super().mouseReleaseEvent(event)
return super().mouseReleaseEvent(event)

def activate(self):
if self._item is None:
Expand Down Expand Up @@ -647,8 +630,7 @@ def indices_eq(ind1, ind2):
if isinstance(ind1, tuple) and isinstance(ind2, tuple):
if len(ind1) != len(ind2):
return False
else:
return all(indices_eq(i1, i2) for i1, i2 in zip(ind1, ind2))
return all(indices_eq(i1, i2) for i1, i2 in zip(ind1, ind2))
elif isinstance(ind1, slice) and isinstance(ind2, slice):
return ind1 == ind2
elif ind1 is ... and ind2 is ...:
Expand All @@ -658,8 +640,7 @@ def indices_eq(ind1, ind2):

if ind1.shape != ind2.shape or ind1.dtype != ind2.dtype:
return False
else:
return (ind1 == ind2).all()
return (ind1 == ind2).all()


def merge_cmd(composit):
Expand Down Expand Up @@ -739,24 +720,23 @@ def data(self, index, role=Qt.DisplayRole):
role == Qt.DecorationRole and \
0 <= index.row() < self.colors.number_of_colors:
return gui.createAttributePixmap("", self.colors[index.row()])
else:
return super().data(index, role)
return super().data(index, role)


def _i(name, icon_path="icons/paintdata",
widg_path=os.path.dirname(os.path.abspath(__file__))):
def _icon(name, icon_path="icons/paintdata",
widg_path=os.path.dirname(os.path.abspath(__file__))):
return os.path.join(widg_path, icon_path, name)


class OWPaintData(OWWidget):
TOOLS = [
("Brush", "Create multiple instances", AirBrushTool, _i("brush.svg")),
("Put", "Put individual instances", PutInstanceTool, _i("put.svg")),
("Brush", "Create multiple instances", AirBrushTool, _icon("brush.svg")),
("Put", "Put individual instances", PutInstanceTool, _icon("put.svg")),
("Select", "Select and move instances", SelectTool,
_i("select-transparent_42px.png")),
("Jitter", "Jitter instances", JitterTool, _i("jitter.svg")),
("Magnet", "Attract multiple instances", MagnetTool, _i("magnet.svg")),
("Clear", "Clear the plot", ClearTool, _i("../../../icons/Dlg_clear.png"))
_icon("select-transparent_42px.png")),
("Jitter", "Jitter instances", JitterTool, _icon("jitter.svg")),
("Magnet", "Attract multiple instances", MagnetTool, _icon("magnet.svg")),
("Clear", "Clear the plot", ClearTool, _icon("../../../icons/Dlg_clear.png"))
]

name = "Paint Data"
Expand All @@ -778,6 +758,7 @@ class OWPaintData(OWWidget):
density = Setting(7)
#: current data array (shape=(N, 3)) as presented on the output
data = Setting(None, schema_only=True)
labels = Setting(["C1", "C2"], schema_only=True)

graph_name = "plot"

Expand All @@ -803,14 +784,13 @@ def __init__(self):
#: a copy of this array (as seen when the `invalidate` method is
#: called
self.__buffer = None
self.labels = ["C1", "C2"]

self.undo_stack = QUndoStack(self)

self.class_model = ColoredListModel(
self.labels, self,
flags=Qt.ItemIsSelectable | Qt.ItemIsEnabled |
Qt.ItemIsEditable)
Qt.ItemIsEditable)

self.class_model.dataChanged.connect(self._class_value_changed)
self.class_model.rowsInserted.connect(self._class_count_changed)
Expand Down Expand Up @@ -1118,8 +1098,7 @@ def selected_class_label(self):
rows = self.classValuesView.selectedIndexes()
if rows:
return rows[0].row()
else:
return None
return None

def set_current_tool(self, tool):
prev_tool = self.current_tool.__class__
Expand Down Expand Up @@ -1172,14 +1151,14 @@ def _add_command(self, cmd):
name = "Name"

if (not self.hasAttr2 and
isinstance(cmd, (Move, MoveSelection, Jitter, Magnet))):
isinstance(cmd, (Move, MoveSelection, Jitter, Magnet))):
# tool only supported if both x and y are enabled
return

if isinstance(cmd, Append):
cls = self.selected_class_label()
points = np.array([(p.x(), p.y() if self.hasAttr2 else 0, cls)
for p in cmd.points])
for p in cmd.points])
self.undo_stack.push(UndoCommand(Append(points), self, text=name))
elif isinstance(cmd, Move):
self.undo_stack.push(UndoCommand(cmd, self, text=name))
Expand Down Expand Up @@ -1298,7 +1277,7 @@ def send_report(self):
self.report_items("Painted data", settings)
self.report_plot()

def test():
def main():
from AnyQt.QtWidgets import QApplication
import gc
import sip
Expand All @@ -1317,4 +1296,4 @@ def test():


if __name__ == "__main__":
sys.exit(test())
sys.exit(main())