Repository: spyder-ide/spyder

Issue:
Error when trying to print source code - AttributeError: 'QPrintDialog' object has no attribute 'addEnabledOption'
## Description

I want to print a source code file.  When I go to File->Print in Spyder I see the error message that I presume is linked to this in some manner.

**What steps will reproduce the problem?**

It doesn't seem reproducible in that I've now closed and re-opened the file and the print function works as expected.  To get this issue I opened the source file, and then I highlight items near the top of the file like "from utilities import *" and right-clicked on utilities to choose go to definition, as I wanted to also view those files.  I then went to File->Print and this error was generated.  I can't supply any of the files for commericial-in-confidence reasons.

**What is the expected output? What do you see instead?**
I expected to see a file dialog, instead I got the error below.

**Please provide any additional information below**

  File "C:\Users\fra256\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyder\plugins\editor.py", line 1932, in print_file
    printDialog.addEnabledOption(QAbstractPrintDialog.PrintSelection)
AttributeError: 'QPrintDialog' object has no attribute 'addEnabledOption'


## Version and main components

* Spyder Version: 3.2.0 
* Python Version: 3.6.0
* Qt Versions: 5.6.2, PyQt5 5.6 on Windows

## Dependencies
```
pyflakes >=0.6.0 :  1.5.0 (OK)
pycodestyle >=2.3:  2.3.1 (OK)
pygments >=2.0   :  2.1.3 (OK)
pandas >=0.13.1  :  0.19.2 (OK)
numpy >=1.7      :  1.11.3 (OK)
sphinx >=0.6.6   :  1.5.1 (OK)
rope >=0.9.4     :  0.9.4-1 (OK)
jedi >=0.9.0     :  0.9.0 (OK)
nbconvert >=4.0  :  4.2.0 (OK)
sympy >=0.7.3    :  1.0 (OK)
cython >=0.21    :  0.25.2 (OK)
qtconsole >=4.2.0:  4.2.1 (OK)
IPython >=4.0    :  5.1.0 (OK)
pylint >=0.25    :  1.6.4 (OK)

```



Discussion:
Thanks for reporting. We'll fix this error in Spyder 3.2.1

@dalthviz, please take a look at this problem.

Resolving patch:
diff --git a/spyder/plugins/editor.py b/spyder/plugins/editor.py
--- a/spyder/plugins/editor.py
+++ b/spyder/plugins/editor.py
@@ -1929,7 +1929,7 @@ def print_file(self):
                           header_font=self.get_plugin_font('printer_header'))
         printDialog = QPrintDialog(printer, editor)
         if editor.has_selected_text():
-            printDialog.addEnabledOption(QAbstractPrintDialog.PrintSelection)
+            printDialog.setOption(QAbstractPrintDialog.PrintSelection, True)
         self.redirect_stdio.emit(False)
         answer = printDialog.exec_()
         self.redirect_stdio.emit(True)


Test patch:
diff --git a/spyder/app/tests/test_mainwindow.py b/spyder/app/tests/test_mainwindow.py
--- a/spyder/app/tests/test_mainwindow.py
+++ b/spyder/app/tests/test_mainwindow.py
@@ -20,6 +20,7 @@
 import pytest
 from qtpy import PYQT5, PYQT_VERSION
 from qtpy.QtCore import Qt, QTimer
+from qtpy.QtPrintSupport import QAbstractPrintDialog, QPrintDialog
 from qtpy.QtTest import QTest
 from qtpy.QtWidgets import QApplication, QFileDialog, QLineEdit
 
@@ -72,6 +73,15 @@ def open_file_in_editor(main_window, fname, directory=None):
             QTest.keyClick(w, Qt.Key_Enter)
 
 
+def print_file(main_window, result, selection_option=False):
+    """Check if the print dialog has the correct option"""
+    top_level_widgets = QApplication.topLevelWidgets()
+    for w in top_level_widgets:
+        if isinstance(w, QPrintDialog):
+            result.append(selection_option == w.testOption(QAbstractPrintDialog.PrintSelection))
+            QTest.keyClick(w, Qt.Key_Escape)
+
+
 def reset_run_code(qtbot, shell, code_editor, nsb):
     """Reset state after a run code test"""
     with qtbot.waitSignal(shell.executed):
@@ -165,6 +175,33 @@ def test_calltip(main_window, qtbot):
     main_window.editor.close_file()
 
 
+@flaky(max_runs=3)
+@pytest.mark.skipif(os.name == 'nt',
+                    reason="QtWarningMsg: QPrintDialog: Cannot be used on non-native printer")
+def test_print_file(main_window, qtbot, tmpdir):
+    """Test print funtionality."""
+    # ---- Load test file ----
+    test_file = osp.join(LOCATION, 'script.py')
+    editor = main_window.editor
+    editor.load(test_file)
+    code_editor = editor.get_focus_widget()
+
+    # --- Test without selecting text ---
+    # Set a timer to manipulate the print dialog while it's running
+    result = []
+    QTimer.singleShot(2000, lambda: print_file(main_window, result))
+    editor.print_file()
+    assert True in result
+
+    # --- Test selecting text --
+    # Select all the text and set a timer to manipulate the print dialog while it's running
+    result = []
+    code_editor.selectAll()
+    QTimer.singleShot(2000, lambda: print_file(main_window, result, selection_option=True))
+    editor.print_file()
+    assert True in result
+
+
 @flaky(max_runs=3)
 def test_runconfig_workdir(main_window, qtbot, tmpdir):
     """Test runconfig workdir options."""