Skip to content

Commit

Permalink
Merge remote branch 'origin/feature/7950_pyqt_v2_api_for_qstring_qvar…
Browse files Browse the repository at this point in the history
…iant'
  • Loading branch information
Michael Reuter committed Oct 4, 2013
2 parents 1b8a91e + 3da9095 commit a7e6cdd
Show file tree
Hide file tree
Showing 23 changed files with 223 additions and 232 deletions.
3 changes: 3 additions & 0 deletions Code/Mantid/MantidPlot/src/PythonScripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ bool PythonScripting::start()
return false;
}

// Our use of the IPython console requires that we use the v2 api for these PyQt types
// This has to be set before the very first import of PyQt (which happens in init_qti)
PyRun_SimpleString("import sip\nsip.setapi('QString',2)\nsip.setapi('QVariant',2)");
//Embedded qti module needs sip definitions initializing before it can be used
init_qti();

Expand Down
70 changes: 29 additions & 41 deletions Code/Mantid/scripts/Interface/reduction_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""
import sys, os
import traceback
from PyQt4 import QtGui, QtCore, uic
import math

# Check whether Mantid is available
Expand All @@ -15,8 +14,13 @@
from mantid.api import AlgorithmFactory
CLUSTER_ENABLED = "SubmitRemoteJob" in AlgorithmFactory.getRegisteredAlgorithms(True)
except:
import sip
sip.setapi('QString',2)
sip.setapi('QVariant',2)
pass

from PyQt4 import QtGui, QtCore, uic

REDUCTION_WARNING = False
WARNING_MESSAGE = ""

Expand Down Expand Up @@ -56,7 +60,7 @@ def __init__(self, instrument=None, instrument_list=None):

# Name handle for the instrument
if instrument is None:
instrument = unicode(settings.value("instrument_name", QtCore.QVariant('')).toString())
instrument = unicode(settings.value("instrument_name", ''))
if instrument_list is not None and instrument not in instrument_list:
instrument = None

Expand All @@ -70,11 +74,13 @@ def __init__(self, instrument=None, instrument_list=None):
self._interface = None

# Recent files
self._recent_files = settings.value("recent_files", QtCore.QVariant([])).toStringList()
self._recent_files = settings.value("recent_files", [])
if self._recent_files is None: # An empty list saved to QSettings comes back as 'None'
self._recent_files = []

# Folder to open files in
self._last_directory = unicode(settings.value("last_directory", QtCore.QVariant('.')).toString())
self._last_export_directory = unicode(settings.value("last_export_directory", QtCore.QVariant('.')).toString())
self._last_directory = unicode(settings.value("last_directory", '.'))
self._last_export_directory = unicode(settings.value("last_export_directory", '.'))

# Current file name
self._filename = None
Expand Down Expand Up @@ -272,7 +278,7 @@ def _update_file_menu(self):
self.file_menu.addSeparator()
for i, fname in enumerate(recent_files):
action = QtGui.QAction("&%d %s" % (i+1, QtCore.QFileInfo(fname).fileName()), self)
action.setData(QtCore.QVariant(fname))
action.setData(fname)
self.connect(action, QtCore.SIGNAL("triggered()"), self.open_file)
self.file_menu.addAction(action)

Expand Down Expand Up @@ -352,8 +358,8 @@ def __init__(self, compute_resources=None):
# Fill out the defaults
dialog = ClusterDialog(self._compute_resources)
if self.general_settings.cluster_user is not None:
dialog.username_edit.setText(QtCore.QString(str(self.general_settings.cluster_user)))
dialog.pass_edit.setText(QtCore.QString(str(self.general_settings.cluster_pass)))
dialog.username_edit.setText(str(self.general_settings.cluster_user))
dialog.pass_edit.setText(str(self.general_settings.cluster_pass))

dialog.nodes_box.setValue(int(self._number_of_nodes))
dialog.cores_box.setValue(int(self._cores_per_node))
Expand Down Expand Up @@ -402,29 +408,11 @@ def closeEvent(self, event):
else:
settings = QtCore.QSettings()

if self._instrument is not None:
instrument = QtCore.QVariant(QtCore.QString(self._instrument))
else:
instrument = QtCore.QVariant()
settings.setValue("instrument_name", instrument)

if self._filename is not None:
filename = QtCore.QVariant(QtCore.QString(self._filename))
else:
filename = QtCore.QVariant()
settings.setValue("last_file", filename)

if self._recent_files is not []:
recent_files = QtCore.QVariant(self._recent_files)
else:
recent_files = QtCore.QVariant()
settings.setValue("recent_files", recent_files)

last_dir = QtCore.QVariant(QtCore.QString(self._last_directory))
settings.setValue("last_directory", last_dir)

last_export_dir = QtCore.QVariant(QtCore.QString(self._last_export_directory))
settings.setValue("last_export_directory", last_export_dir)
settings.setValue("instrument_name", self._instrument)
settings.setValue("last_file", self._filename)
settings.setValue("recent_files", self._recent_files)
settings.setValue("last_directory", str(self._last_directory))
settings.setValue("last_export_directory", str(self._last_export_directory))

# General settings
self.general_settings.to_settings(settings)
Expand Down Expand Up @@ -483,7 +471,7 @@ def open_file(self, file_path=None):
if file_path is None:
action = self.sender()
if isinstance(action, QtGui.QAction):
file_path = unicode(action.data().toString())
file_path = unicode(action.data())

# Check whether the file describes the current instrument
try:
Expand Down Expand Up @@ -512,12 +500,12 @@ def open_file(self, file_path=None):
self._filename = file_path
self._update_file_menu()
self._set_window_title()

if file_path in self._recent_files:
self._recent_files.removeAll(file_path)
self._recent_files.prepend(file_path)
while self._recent_files.count() > 10:
self._recent_files.takeLast()
self._recent_files.remove(file_path)
self._recent_files.insert(0,file_path)
while len(self._recent_files) > 10:
self._recent_files.pop()

def _new(self, *argv):
"""
Expand Down Expand Up @@ -578,10 +566,10 @@ def _save_as(self):
if not fname.endswith('.xml'):
fname += ".xml"
if fname in self._recent_files:
self._recent_files.removeAll(fname)
self._recent_files.prepend(fname)
while self._recent_files.count() > 10:
self._recent_files.takeLast()
self._recent_files.remove(fname)
self._recent_files.insert(0,fname)
while len(self._recent_files) > 10:
self._recent_files.pop()
self._last_directory = str(QtCore.QFileInfo(fname_qstr).path())
self._filename = fname
self._save()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from scripter import BaseScriptElement
from mantid.api import AnalysisDataService

# Check whether we are running in MantidPlot
IS_IN_MANTIDPLOT = False
try:
import mantidplot
IS_IN_MANTIDPLOT = True
from mantid.api import AnalysisDataService
except:
pass

Expand Down Expand Up @@ -34,4 +34,4 @@ def update(self):
if len(iq_plots)>0:
mantidplot.plotSpectrum(iq_plots, 0, True)
except:
raise RuntimeError, "Could not plot resulting output\n %s" % sys.exc_value
raise RuntimeError, "Could not plot resulting output\n %s" % sys.exc_value
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,20 @@ def to_settings(self, settings):
Write the current settings to a QSettings object
@param settings: QSettings object
"""
last_dir = QtCore.QVariant(QtCore.QString(self.data_path))
settings.setValue("general_data_path", last_dir)
debug_mode = QtCore.QVariant(self.debug)
settings.setValue("debug_mode", debug_mode)
advanced_mode = QtCore.QVariant(self.advanced)
settings.setValue("advanced_mode", advanced_mode)
instr_name = QtCore.QVariant(QtCore.QString(self.instrument_name))
settings.setValue("instrument_name", instr_name)
facility_name = QtCore.QVariant(QtCore.QString(self.facility_name))
settings.setValue("facility_name", facility_name)
settings.setValue("general_data_path", str(self.data_path))
settings.setValue("debug_mode", self.debug)
settings.setValue("advanced_mode", self.advanced)
settings.setValue("instrument_name", str(self.instrument_name))
settings.setValue("facility_name", str(self.facility_name))

def from_settings(self, settings):
"""
Get the settings from a QSettings object
@param settings: QSettings object
"""
self.data_path = unicode(settings.value("general_data_path", QtCore.QVariant('.')).toString())
self.debug = settings.value("debug_mode", QtCore.QVariant('false')).toBool()
self.advanced = settings.value("advanced_mode", QtCore.QVariant('true')).toBool()
self.instrument_name = unicode(settings.value("instrument_name", QtCore.QVariant('')).toString())
self.facility_name = unicode(settings.value("facility_name", QtCore.QVariant('')).toString())

self.data_path = unicode(settings.value("general_data_path", '.'))
self.debug = bool(settings.value("debug_mode", False))
self.advanced = bool(settings.value("advanced_mode", True))
self.instrument_name = unicode(settings.value("instrument_name", ''))
self.facility_name = unicode(settings.value("facility_name", ''))

Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ def data_browse_dialog(self, data_type=None, title=None, multi=False):
qflist = QtGui.QFileDialog.getOpenFileNames(self, title,
self._settings.data_path,
data_type)
if qflist.count()>0:
if len(qflist)>0:
flist = []
for i in range(qflist.count()):
for i in range(len(qflist)):
flist.append(str(QtCore.QFileInfo(qflist[i]).filePath()))
# Store the location of the loaded file
self._settings.data_path = str(QtCore.QFileInfo(qflist[i]).path())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def initialize_content(self):
def _button_pressed(self):
_tmp_str = self._alternate_text
self._alternate_text = self._content.line_edit.text()
self._content.line_edit.setText(QtCore.QString(_tmp_str))
self._content.line_edit.setText(_tmp_str)

def set_state(self, state):
"""
Expand All @@ -63,4 +63,4 @@ def get_state(self):
m = ExampleState()
m.text = self._content.line_edit.text()
m.alternate_text = self._alternate_text
return m
return m
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ def set_state(self, state):
self._content.grouping_file_edit.setText(state.grouping_file)
self._content.absunits_detvan_edit.setText(state.absunits_detector_vanadium)
self._content.ei_edit.setText(state.incident_energy)
self._content.emin_edit.setText(QtCore.QString(str(state.emin)))
self._content.emax_edit.setText(QtCore.QString(str(state.emax)))
self._content.van_mass_edit.setText(QtCore.QString(str(state.vanadium_mass)))
self._content.sample_mass_edit.setText(QtCore.QString(str(state.sample_mass)))
self._content.sample_rmm_edit.setText(QtCore.QString(str(state.sample_rmm)))
self._content.median_test_high_edit.setText(QtCore.QString(str(state.absunits_median_test_high)))
self._content.median_test_low_edit.setText(QtCore.QString(str(state.absunits_median_test_low)))
self._content.median_test_out_high_edit.setText(QtCore.QString(str(state.absunits_median_test_out_high)))
self._content.median_test_out_low_edit.setText(QtCore.QString(str(state.absunits_median_test_out_low)))
self._content.errorbar_crit_edit.setText(QtCore.QString(str(state.absunits_errorbar_criterion)))
self._content.emin_edit.setText(str(state.emin))
self._content.emax_edit.setText(str(state.emax))
self._content.van_mass_edit.setText(str(state.vanadium_mass))
self._content.sample_mass_edit.setText(str(state.sample_mass))
self._content.sample_rmm_edit.setText(str(state.sample_rmm))
self._content.median_test_high_edit.setText(str(state.absunits_median_test_high))
self._content.median_test_low_edit.setText(str(state.absunits_median_test_low))
self._content.median_test_out_high_edit.setText(str(state.absunits_median_test_out_high))
self._content.median_test_out_low_edit.setText(str(state.absunits_median_test_out_low))
self._content.errorbar_crit_edit.setText(str(state.absunits_errorbar_criterion))

def get_state(self):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,21 @@ def set_state(self, state):
Populate the UI elements with the data from the given state.
@param state: DiagnoseDetectorsScript object
"""
self._content.high_counts_edit.setText(QtCore.QString("%1.e" % state.high_counts))
self._content.low_counts_edit.setText(QtCore.QString(str(state.low_counts)))
self._content.median_test_high_edit.setText(QtCore.QString(str(state.median_test_high)))
self._content.median_test_low_edit.setText(QtCore.QString(str(state.median_test_low)))
self._content.median_test_out_high_edit.setText(QtCore.QString(str(state.median_test_out_high)))
self._content.median_test_out_low_edit.setText(QtCore.QString(str(state.median_test_out_low)))
self._content.errorbar_crit_edit.setText(QtCore.QString(str(state.errorbar_criterion)))
self._content.high_counts_edit.setText(str("%1.e" % state.high_counts))
self._content.low_counts_edit.setText(str(state.low_counts))
self._content.median_test_high_edit.setText(str(state.median_test_high))
self._content.median_test_low_edit.setText(str(state.median_test_low))
self._content.median_test_out_high_edit.setText(str(state.median_test_out_high))
self._content.median_test_out_low_edit.setText(str(state.median_test_out_low))
self._content.errorbar_crit_edit.setText(str(state.errorbar_criterion))
self._content.det_van2_edit.setText(state.det_van2)
self._content.ratio_var_crit_edit.setText(QtCore.QString(str(state.detvan_ratio_var)))
self._content.ratio_var_crit_edit.setText(str(state.detvan_ratio_var))
self._content.background_check_gb.setChecked(state.background_check)
self._content.sambkg_median_test_high_edit.setText(QtCore.QString(str(state.sambkg_median_test_high)))
self._content.sambkg_median_test_low_edit.setText(QtCore.QString(str(state.sambkg_median_test_low)))
self._content.sambkg_errorbar_crit_edit.setText(QtCore.QString(str(state.sambkg_errorbar_criterion)))
self._content.tof_start_edit.setText(QtCore.QString(str(state.tof_start)))
self._content.tof_end_edit.setText(QtCore.QString(str(state.tof_end)))
self._content.sambkg_median_test_high_edit.setText(str(state.sambkg_median_test_high))
self._content.sambkg_median_test_low_edit.setText(str(state.sambkg_median_test_low))
self._content.sambkg_errorbar_crit_edit.setText(str(state.sambkg_errorbar_criterion))
self._content.tof_start_edit.setText(str(state.tof_start))
self._content.tof_end_edit.setText(str(state.tof_end))
self._content.reject_zero_bg_cb.setChecked(state.reject_zero_bkg)
self._content.psd_bleed_gb.setChecked(state.psd_bleed)
self._content.max_framerate_edit.setText(str(state.max_framerate))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def initialize_content(self):

def _check_and_set_lineedit_content(self, lineedit, content):
lineedit.setText(content)
util.set_valid(lineedit, not lineedit.text().isEmpty())
util.set_valid(lineedit, not lineedit.text() == '')

def _connect_validated_lineedit(self, ui_ctrl):
call_back = partial(self._validate_edit, ctrl=ui_ctrl)
Expand All @@ -64,7 +64,7 @@ def _connect_validated_lineedit(self, ui_ctrl):

def _validate_edit(self, ctrl=None):
is_valid = True
if ctrl.text().isEmpty():
if not ctrl.text():
is_valid = False
util.set_valid(ctrl, is_valid)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def _handle_tzero_guess(self, is_enabled):

def _check_and_set_lineedit_content(self, lineedit, content):
lineedit.setText(content)
util.set_valid(lineedit, not lineedit.text().isEmpty())
util.set_valid(lineedit, not lineedit.text() == '')

def _connect_validated_lineedit(self, ui_ctrl):
call_back = partial(self._validate_edit, ctrl=ui_ctrl)
Expand All @@ -122,7 +122,7 @@ def _validate_edit(self, ctrl=None):
if not ctrl.isValid():
is_valid = False
else:
if ctrl.text().isEmpty():
if not ctrl.text():
is_valid = False
util.set_valid(ctrl, is_valid)

Expand Down Expand Up @@ -171,9 +171,9 @@ def set_state(self, state):
self._check_and_set_lineedit_content(self._content.ei_guess_edit,
state.incident_energy_guess)
self._content.use_ei_guess_chkbox.setChecked(state.use_ei_guess)
self._content.tzero_guess_edit.setText(QtCore.QString(str(state.tzero_guess)))
self._content.monitor1_specid_edit.setText(QtCore.QString(str(state.monitor1_specid)))
self._content.monitor2_specid_edit.setText(QtCore.QString(str(state.monitor2_specid)))
self._content.tzero_guess_edit.setText(str(state.tzero_guess))
self._content.monitor1_specid_edit.setText(str(state.monitor1_specid))
self._content.monitor2_specid_edit.setText(str(state.monitor2_specid))
self._content.et_range_box.setChecked(state.rebin_et)
self._content.etr_low_edit.setText(state.et_range_low)
self._content.etr_width_edit.setText(state.et_range_width)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def initialize_content(self):
self._content.log_binning_radio.hide()

def set_state(self, state):
self._content.output_text_edit.setText(QtCore.QString(state.log_text))
self._content.output_text_edit.setText(state.log_text)

def get_state(self):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,7 @@ def _add_data(self):
while i < self._summary.angle_list.count():

current_item = self._summary.angle_list.item(i)
state = current_item.data(QtCore.Qt.UserRole).toPyObject()
state = current_item.data(QtCore.Qt.UserRole)

_q_min = self._summary.q_min_edit.text()
state.q_min = float(_q_min)
Expand Down Expand Up @@ -1293,7 +1293,7 @@ def _angle_changed(self):
self._summary.remove_btn.setEnabled(False)
current_item = self._summary.angle_list.currentItem()
if current_item is not None:
state = current_item.data(QtCore.Qt.UserRole).toPyObject()
state = current_item.data(QtCore.Qt.UserRole)
self.set_editing_state(state)
self._reset_warnings()
self._summary.angle_list.setEnabled(True)
Expand Down

0 comments on commit a7e6cdd

Please sign in to comment.