Skip to content

Commit a397c5a

Browse files
authored
Merge pull request #7129 from janezd/nomogram-report-save-clipboard
Nomogram: Fix reporting, saving image, copying to clipboard
2 parents 6d8d85e + ba09a78 commit a397c5a

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

Orange/widgets/visualize/ownomogram.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
from AnyQt.QtCore import Qt, QRectF, QSize, QPropertyAnimation, QObject, \
1919
pyqtProperty
2020

21+
from orangewidget.io import ClipboardFormat
22+
from orangewidget.utils import saveplot
23+
2124
from Orange.data import Table, Domain, DiscreteVariable, ContinuousVariable, \
2225
Variable
2326
from Orange.statistics.util import nanmin, nanmax, nanmean, unique
@@ -674,6 +677,10 @@ class Outputs:
674677
sort_index = Setting(SortBy.ABSOLUTE)
675678
cont_feature_dim_index = Setting(0)
676679

680+
# This is defined so that base widget shows the button for saving graph
681+
# and connects the shortcut for copying to clipboard. The value itself
682+
# is not used because send_report, save_graph and copy_to_clipboard are
683+
# overridden.
677684
graph_name = "scene" # QGraphicsScene
678685

679686
class Error(OWWidget.Error):
@@ -1302,8 +1309,26 @@ def clear_scene(self):
13021309
self.dot_animator.clear()
13031310
self.scene.clear()
13041311

1312+
def get_nomogram_view(self):
1313+
view = QGraphicsView(self.scene, self)
1314+
scene_rect = self.scene.itemsBoundingRect()
1315+
view.setSceneRect(scene_rect)
1316+
view.resize(scene_rect.size().toSize())
1317+
return view
1318+
1319+
def copy_to_clipboard(self):
1320+
ClipboardFormat.write_image(None, self.get_nomogram_view())
1321+
1322+
def save_graph(self):
1323+
saveplot.save_plot(self.get_nomogram_view(), self.graph_writers)
1324+
13051325
def send_report(self):
1306-
self.report_plot()
1326+
# self.report_plot(name="", plot=self.get_nomogram_view())
1327+
# would work, but the resulting nomogram is too small
1328+
# The drawback of the below is that the space between top_view and view
1329+
self.report_plot(name="", plot=self.top_view)
1330+
self.report_plot(name="", plot=self.view)
1331+
self.report_plot(name="", plot=self.bottom_view)
13071332

13081333
@staticmethod
13091334
def reconstruct_domain(classifier: Model, preprocessed: Domain) -> Domain:

Orange/widgets/visualize/tests/test_ownomogram.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,15 @@ def __call__(self, table):
350350
lr = LogisticRegressionLearner()(data)
351351
self.send_signal(self.widget.Inputs.classifier, lr)
352352

353+
def test_report_copy_save_graph(self):
354+
# Test that reporting, copying to clipboard, and saving graph don't crash
355+
self.send_signal(self.widget.Inputs.classifier, self.nb_cls)
356+
self.widget.send_report()
357+
self.widget.copy_to_clipboard()
358+
with patch("orangewidget.utils.filedialogs.open_filename_dialog_save") as m:
359+
m.return_value = (None, None, None)
360+
self.widget.save_graph()
361+
353362

354363
if __name__ == "__main__":
355364
unittest.main()

0 commit comments

Comments
 (0)