Skip to content

Commit c6d5f3f

Browse files
authored
Merge pull request #2259 from jerneju/paintdata-save-labels
[FIX] Paint Data: Save and load labels
2 parents 50ce4ca + 63fc139 commit c6d5f3f

File tree

1 file changed

+36
-57
lines changed

1 file changed

+36
-57
lines changed

Orange/widgets/data/owpaintdata.py

Lines changed: 36 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import unicodedata
55
import itertools
66
from functools import partial
7+
from collections import namedtuple
78

89
import numpy as np
910

@@ -71,7 +72,6 @@ def stack_on_condition(a, b, condition):
7172
# Data manipulation operators
7273
# ###########################
7374

74-
from collections import namedtuple
7575
if sys.version_info < (3, 4):
7676
# use singledispatch backports from pypi
7777
from singledispatch import singledispatch
@@ -273,8 +273,7 @@ def mousePressEvent(self, event):
273273
event.accept()
274274
self.editingFinished.emit()
275275
return True
276-
else:
277-
return super().mousePressEvent(event)
276+
return super().mousePressEvent(event)
278277

279278

280279
class PenTool(DataTool):
@@ -286,22 +285,19 @@ def mousePressEvent(self, event):
286285
self.editingStarted.emit()
287286
self.__handleEvent(event)
288287
return True
289-
else:
290-
return super().mousePressEvent()
288+
return super().mousePressEvent()
291289

292290
def mouseMoveEvent(self, event):
293291
if event.buttons() & Qt.LeftButton:
294292
self.__handleEvent(event)
295293
return True
296-
else:
297-
return super().mouseMoveEvent()
294+
return super().mouseMoveEvent()
298295

299296
def mouseReleaseEvent(self, event):
300297
if event.button() == Qt.LeftButton:
301298
self.editingFinished.emit()
302299
return True
303-
else:
304-
return super().mouseReleaseEvent()
300+
return super().mouseReleaseEvent()
305301

306302
def __handleEvent(self, event):
307303
pos = self.mapToPlot(event.pos())
@@ -328,23 +324,20 @@ def mousePressEvent(self, event):
328324
self.__pos = self.mapToPlot(event.pos())
329325
self.__timer.start()
330326
return True
331-
else:
332-
return super().mousePressEvent(event)
327+
return super().mousePressEvent(event)
333328

334329
def mouseMoveEvent(self, event):
335330
if event.buttons() & Qt.LeftButton:
336331
self.__pos = self.mapToPlot(event.pos())
337332
return True
338-
else:
339-
return super().mouseMoveEvent(event)
333+
return super().mouseMoveEvent(event)
340334

341335
def mouseReleaseEvent(self, event):
342336
if event.button() == Qt.LeftButton:
343337
self.__timer.stop()
344338
self.editingFinished.emit()
345339
return True
346-
else:
347-
return super().mouseReleaseEvent(event)
340+
return super().mouseReleaseEvent(event)
348341

349342
def __timout(self):
350343
self.issueCommand.emit(
@@ -355,8 +348,7 @@ def __timout(self):
355348
def random_state(rstate):
356349
if isinstance(rstate, np.random.RandomState):
357350
return rstate
358-
else:
359-
return np.random.RandomState(rstate)
351+
return np.random.RandomState(rstate)
360352

361353

362354
def create_data(x, y, radius, size, rstate):
@@ -384,23 +376,20 @@ def mousePressEvent(self, event):
384376
self._pos = self.mapToPlot(event.pos())
385377
self.__timer.start()
386378
return True
387-
else:
388-
return super().mousePressEvent(event)
379+
return super().mousePressEvent(event)
389380

390381
def mouseMoveEvent(self, event):
391382
if event.buttons() & Qt.LeftButton:
392383
self._pos = self.mapToPlot(event.pos())
393384
return True
394-
else:
395-
return super().mouseMoveEvent(event)
385+
return super().mouseMoveEvent(event)
396386

397387
def mouseReleaseEvent(self, event):
398388
if event.button() == Qt.LeftButton:
399389
self.__timer.stop()
400390
self.editingFinished.emit()
401391
return True
402-
else:
403-
return super().mouseReleaseEvent(event)
392+
return super().mouseReleaseEvent(event)
404393

405394
def __timeout(self):
406395
self.issueCommand.emit(
@@ -427,23 +416,20 @@ def mousePressEvent(self, event):
427416
self._pos = self.mapToPlot(event.pos())
428417
self.__timer.start()
429418
return True
430-
else:
431-
return super().mousePressEvent(event)
419+
return super().mousePressEvent(event)
432420

433421
def mouseMoveEvent(self, event):
434422
if event.buttons() & Qt.LeftButton:
435423
self._pos = self.mapToPlot(event.pos())
436424
return True
437-
else:
438-
return super().mouseMoveEvent(event)
425+
return super().mouseMoveEvent(event)
439426

440427
def mouseReleaseEvent(self, event):
441428
if event.button() == Qt.LeftButton:
442429
self.__timer.stop()
443430
self.editingFinished.emit()
444431
return True
445-
else:
446-
return super().mouseReleaseEvent(event)
432+
return super().mouseReleaseEvent(event)
447433

448434
def _do(self):
449435
self.issueCommand.emit(
@@ -507,17 +493,15 @@ def mousePressEvent(self, event):
507493
self.setSelectionRect(QRectF(pos, pos))
508494
event.accept()
509495
return True
510-
else:
511-
return super().mousePressEvent(event)
496+
return super().mousePressEvent(event)
512497

513498
def mouseMoveEvent(self, event):
514499
if event.buttons() & Qt.LeftButton:
515500
pos = self.mapToPlot(event.pos())
516501
self.setSelectionRect(QRectF(self._start_pos, pos).normalized())
517502
event.accept()
518503
return True
519-
else:
520-
return super().mouseMoveEvent(event)
504+
return super().mouseMoveEvent(event)
521505

522506
def mouseReleaseEvent(self, event):
523507
if event.button() == Qt.LeftButton:
@@ -528,8 +512,7 @@ def mouseReleaseEvent(self, event):
528512
self._item.setCursor(Qt.OpenHandCursor)
529513
self._mouse_dragging = False
530514
return True
531-
else:
532-
return super().mouseReleaseEvent(event)
515+
return super().mouseReleaseEvent(event)
533516

534517
def activate(self):
535518
if self._item is None:
@@ -647,8 +630,7 @@ def indices_eq(ind1, ind2):
647630
if isinstance(ind1, tuple) and isinstance(ind2, tuple):
648631
if len(ind1) != len(ind2):
649632
return False
650-
else:
651-
return all(indices_eq(i1, i2) for i1, i2 in zip(ind1, ind2))
633+
return all(indices_eq(i1, i2) for i1, i2 in zip(ind1, ind2))
652634
elif isinstance(ind1, slice) and isinstance(ind2, slice):
653635
return ind1 == ind2
654636
elif ind1 is ... and ind2 is ...:
@@ -658,8 +640,7 @@ def indices_eq(ind1, ind2):
658640

659641
if ind1.shape != ind2.shape or ind1.dtype != ind2.dtype:
660642
return False
661-
else:
662-
return (ind1 == ind2).all()
643+
return (ind1 == ind2).all()
663644

664645

665646
def merge_cmd(composit):
@@ -739,24 +720,23 @@ def data(self, index, role=Qt.DisplayRole):
739720
role == Qt.DecorationRole and \
740721
0 <= index.row() < self.colors.number_of_colors:
741722
return gui.createAttributePixmap("", self.colors[index.row()])
742-
else:
743-
return super().data(index, role)
723+
return super().data(index, role)
744724

745725

746-
def _i(name, icon_path="icons/paintdata",
747-
widg_path=os.path.dirname(os.path.abspath(__file__))):
726+
def _icon(name, icon_path="icons/paintdata",
727+
widg_path=os.path.dirname(os.path.abspath(__file__))):
748728
return os.path.join(widg_path, icon_path, name)
749729

750730

751731
class OWPaintData(OWWidget):
752732
TOOLS = [
753-
("Brush", "Create multiple instances", AirBrushTool, _i("brush.svg")),
754-
("Put", "Put individual instances", PutInstanceTool, _i("put.svg")),
733+
("Brush", "Create multiple instances", AirBrushTool, _icon("brush.svg")),
734+
("Put", "Put individual instances", PutInstanceTool, _icon("put.svg")),
755735
("Select", "Select and move instances", SelectTool,
756-
_i("select-transparent_42px.png")),
757-
("Jitter", "Jitter instances", JitterTool, _i("jitter.svg")),
758-
("Magnet", "Attract multiple instances", MagnetTool, _i("magnet.svg")),
759-
("Clear", "Clear the plot", ClearTool, _i("../../../icons/Dlg_clear.png"))
736+
_icon("select-transparent_42px.png")),
737+
("Jitter", "Jitter instances", JitterTool, _icon("jitter.svg")),
738+
("Magnet", "Attract multiple instances", MagnetTool, _icon("magnet.svg")),
739+
("Clear", "Clear the plot", ClearTool, _icon("../../../icons/Dlg_clear.png"))
760740
]
761741

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

782763
graph_name = "plot"
783764

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

808788
self.undo_stack = QUndoStack(self)
809789

810790
self.class_model = ColoredListModel(
811791
self.labels, self,
812792
flags=Qt.ItemIsSelectable | Qt.ItemIsEnabled |
813-
Qt.ItemIsEditable)
793+
Qt.ItemIsEditable)
814794

815795
self.class_model.dataChanged.connect(self._class_value_changed)
816796
self.class_model.rowsInserted.connect(self._class_count_changed)
@@ -1118,8 +1098,7 @@ def selected_class_label(self):
11181098
rows = self.classValuesView.selectedIndexes()
11191099
if rows:
11201100
return rows[0].row()
1121-
else:
1122-
return None
1101+
return None
11231102

11241103
def set_current_tool(self, tool):
11251104
prev_tool = self.current_tool.__class__
@@ -1172,14 +1151,14 @@ def _add_command(self, cmd):
11721151
name = "Name"
11731152

11741153
if (not self.hasAttr2 and
1175-
isinstance(cmd, (Move, MoveSelection, Jitter, Magnet))):
1154+
isinstance(cmd, (Move, MoveSelection, Jitter, Magnet))):
11761155
# tool only supported if both x and y are enabled
11771156
return
11781157

11791158
if isinstance(cmd, Append):
11801159
cls = self.selected_class_label()
11811160
points = np.array([(p.x(), p.y() if self.hasAttr2 else 0, cls)
1182-
for p in cmd.points])
1161+
for p in cmd.points])
11831162
self.undo_stack.push(UndoCommand(Append(points), self, text=name))
11841163
elif isinstance(cmd, Move):
11851164
self.undo_stack.push(UndoCommand(cmd, self, text=name))
@@ -1298,7 +1277,7 @@ def send_report(self):
12981277
self.report_items("Painted data", settings)
12991278
self.report_plot()
13001279

1301-
def test():
1280+
def main():
13021281
from AnyQt.QtWidgets import QApplication
13031282
import gc
13041283
import sip
@@ -1317,4 +1296,4 @@ def test():
13171296

13181297

13191298
if __name__ == "__main__":
1320-
sys.exit(test())
1299+
sys.exit(main())

0 commit comments

Comments
 (0)