diff --git a/.flake8 b/.flake8 new file mode 100644 index 00000000..0556c925 --- /dev/null +++ b/.flake8 @@ -0,0 +1,49 @@ +[flake8] +exclude = .git,.tox,__pycache__,doc,old,build,dist +show_source = True +statistics = True +verbose = 2 +select = + E101, + E112, + E122, + E125, + E133, + E223, + E224, + E242, + E273, + E274, + E901, + E902, + W191, + W601, + W602, + W603, + W604, + E124, + E231, + E211, + E261, + E271, + E272, + E304, + F401, + F402, + F403, + F404, + E501, + E502, + E702, + E703, + E711, + E712, + E721, + F811, + F812, + F821, + F822, + F823, + F831, + F841, + W292 diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md new file mode 100644 index 00000000..85ea5b79 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -0,0 +1,44 @@ +--- +name: Bug report +about: Create a report to help us improve +title: '' +labels: '' +assignees: '' + +--- + + + + +### Short description + + +### Code to reproduce + +```python +import pyqtgraph as pg +import numpy as np +``` + +### Expected behavior + + +### Real behavior + + +``` +An error occurred? +Post the full traceback inside these 'code fences'! +``` + +### Tested environment(s) + + * PyQtGraph version: + * Qt Python binding: + * Python version: + * NumPy version: + * Operating system: + * Installation method: + +### Additional context diff --git a/.mailmap b/.mailmap deleted file mode 100644 index 025cf940..00000000 --- a/.mailmap +++ /dev/null @@ -1,12 +0,0 @@ -Luke Campagnola Luke Campagnola <> -Luke Campagnola Luke Campagnola -Megan Kratz meganbkratz@gmail.com <> -Megan Kratz Megan Kratz -Megan Kratz Megan Kratz -Megan Kratz Megan Kratz -Megan Kratz Megan Kratz -Megan Kratz Megan Kratz -Megan Kratz Megan Kratz -Ingo Breßler Ingo Breßler -Ingo Breßler Ingo B. - diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..c2f8f9a8 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,11 @@ +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + sha: master + hooks: + - id: check-added-large-files + args: ['--maxkb=100'] + - id: check-case-conflict + - id: end-of-file-fixer + - id: fix-encoding-pragma + - id: mixed-line-ending + args: [--fix=lf] diff --git a/.readthedocs.yml b/.readthedocs.yml new file mode 100644 index 00000000..795d359a --- /dev/null +++ b/.readthedocs.yml @@ -0,0 +1,12 @@ +# Read the Docs configuration file +# https://docs.readthedocs.io/en/stable/config-file/v2.html + +version: 2 + +python: + version: 3 + install: + - requirements: doc/requirements.txt + +sphinx: + fail_on_warning: true diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 0da455d8..00000000 --- a/.travis.yml +++ /dev/null @@ -1,189 +0,0 @@ -language: python -sudo: false -# Credit: Original .travis.yml lifted from VisPy - -# Here we use anaconda for 2.6 and 3.3, since it provides the simplest -# interface for running different versions of Python. We could also use -# it for 2.7, but the Ubuntu system has installable 2.7 Qt4-GL, which -# allows for more complete testing. -notifications: - email: false - -env: - # Enable python 2 and python 3 builds - # Note that the python 2.6 support ended. - - PYTHON=2.7 QT=pyqt4 TEST=extra - - PYTHON=2.7 QT=pyside TEST=standard - - PYTHON=3.5 QT=pyqt5 TEST=standard - # - PYTHON=3.4 QT=pyside TEST=standard # pyside isn't available for 3.4 with conda - #- PYTHON=3.2 QT=pyqt5 TEST=standard - -services: - - xvfb - -before_install: - - if [ ${TRAVIS_PYTHON_VERSION:0:1} == "2" ]; then wget http://repo.continuum.io/miniconda/Miniconda-3.5.5-Linux-x86_64.sh -O miniconda.sh; else wget http://repo.continuum.io/miniconda/Miniconda3-3.5.5-Linux-x86_64.sh -O miniconda.sh; fi - - chmod +x miniconda.sh - - ./miniconda.sh -b -p /home/travis/mc - - export PATH=/home/travis/mc/bin:$PATH - - # not sure what is if block is for - - if [ "${TRAVIS_PULL_REQUEST}" != "false" ]; then - GIT_TARGET_EXTRA="+refs/heads/${TRAVIS_BRANCH}"; - GIT_SOURCE_EXTRA="+refs/pull/${TRAVIS_PULL_REQUEST}/merge"; - else - GIT_TARGET_EXTRA=""; - GIT_SOURCE_EXTRA=""; - fi; - - # to aid in debugging - - echo ${TRAVIS_BRANCH} - - echo ${TRAVIS_REPO_SLUG} - - echo ${GIT_TARGET_EXTRA} - - echo ${GIT_SOURCE_EXTRA} - -install: - - export GIT_FULL_HASH=`git rev-parse HEAD` - - conda update conda --yes - - conda create -n test_env python=${PYTHON} --yes - - source activate test_env - - conda install numpy scipy pyopengl pytest flake8 six coverage --yes - - echo ${QT} - - echo ${TEST} - - echo ${PYTHON} - - - if [ "${QT}" == "pyqt5" ]; then - conda install pyqt --yes; - fi; - - if [ "${QT}" == "pyqt4" ]; then - conda install pyqt=4 --yes; - fi; - - if [ "${QT}" == "pyside" ]; then - conda install pyside --yes; - fi; - - pip install pytest-xdist # multi-thread py.test - - pip install pytest-cov # add coverage stats - - pip install pytest-faulthandler # activate faulthandler - - # Debugging helpers - - uname -a - - cat /etc/issue - - if [ "${PYTHON}" == "2.7" ]; then - python --version; - else - python3 --version; - fi; - -before_script: - # We need to create a (fake) display on Travis, let's use a funny resolution - - export DISPLAY=:99.0 - - /sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_99.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :99 -screen 0 1400x900x24 -ac +extension GLX +render - - # Make sure everyone uses the correct python (this is handled by conda) - - which python - - python --version - - pwd - - ls - # Help color output from each test - - RESET='\033[0m'; - RED='\033[00;31m'; - GREEN='\033[00;32m'; - YELLOW='\033[00;33m'; - BLUE='\033[00;34m'; - PURPLE='\033[00;35m'; - CYAN='\033[00;36m'; - WHITE='\033[00;37m'; - start_test() { - echo -e "${BLUE}======== Starting $1 ========${RESET}"; - }; - check_output() { - ret=$?; - if [ $ret == 0 ]; then - echo -e "${GREEN}>>>>>> $1 passed <<<<<<${RESET}"; - else - echo -e "${RED}>>>>>> $1 FAILED <<<<<<${RESET}"; - fi; - return $ret; - }; - - - if [ "${TEST}" == "extra" ]; then - start_test "repo size check"; - mkdir ~/repo-clone && cd ~/repo-clone && - git init && git remote add -t ${TRAVIS_BRANCH} origin git://github.com/${TRAVIS_REPO_SLUG}.git && - git fetch origin ${GIT_TARGET_EXTRA} && - git checkout -qf FETCH_HEAD && - git tag travis-merge-target && - git gc --aggressive && - TARGET_SIZE=`du -s . | sed -e "s/\t.*//"` && - git pull origin ${GIT_SOURCE_EXTRA} && - git gc --aggressive && - MERGE_SIZE=`du -s . | sed -e "s/\t.*//"` && - if [ "${MERGE_SIZE}" != "${TARGET_SIZE}" ]; then - SIZE_DIFF=`expr \( ${MERGE_SIZE} - ${TARGET_SIZE} \)`; - else - SIZE_DIFF=0; - fi; - fi; - -script: - - - source activate test_env - - # Check system info - - python -c "import pyqtgraph as pg; pg.systemInfo()" - - # Run unit tests - - start_test "unit tests"; - PYTHONPATH=. py.test --cov pyqtgraph -sv; - check_output "unit tests"; - - echo "test script finished. Current directory:" - - pwd - - # check line endings - - if [ "${TEST}" == "extra" ]; then - start_test "line ending check"; - ! find ./ -name "*.py" | xargs file | grep CRLF && - ! find ./ -name "*.rst" | xargs file | grep CRLF; - check_output "line ending check"; - fi; - - # Check repo size does not expand too much - - if [ "${TEST}" == "extra" ]; then - start_test "repo size check"; - echo -e "Estimated content size difference = ${SIZE_DIFF} kB" && - test ${SIZE_DIFF} -lt 100; - check_output "repo size check"; - fi; - - # Check for style issues - - if [ "${TEST}" == "extra" ]; then - start_test "style check"; - cd ~/repo-clone && - git reset -q travis-merge-target && - python setup.py style && - check_output "style check"; - fi; - - # Check install works - - start_test "install test"; - python setup.py --quiet install; - check_output "install test"; - - # Check double-install fails - # Note the bash -c is because travis strips off the ! otherwise. - - start_test "double install test"; - bash -c "! python setup.py --quiet install"; - check_output "double install test"; - - # Check we can import pg - - start_test "import test"; - echo "import sys; print(sys.path)" | python && - cd /; echo "import pyqtgraph.examples" | python; - check_output "import test"; - -after_success: - - cd /home/travis/build/pyqtgraph/pyqtgraph - - pip install codecov --upgrade # add coverage integration service - - codecov - - pip install coveralls --upgrade # add another coverage integration service - - coveralls diff --git a/CHANGELOG b/CHANGELOG index 2cb16918..efc3ee3b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,9 @@ -pyqtgraph-0.11.0 (in development) +pyqtgraph-0.11.0 + + NOTICE: This is the _last_ feature release to support Python 2 and Qt 4 (PyQt4 or pyside 1) New Features: + - #101: GridItem formatting options - #410: SpinBox custom formatting options - #415: ROI.getArrayRegion supports nearest-neighbor interpolation (especially handy for label images) - #428: DataTreeWidget: @@ -56,10 +59,24 @@ pyqtgraph-0.11.0 (in development) - #683: Allow data filter entries to be updated after they are created - #685: Add option to set enum default values in DataFilterWidget - #710: Adds ability to rotate/scale ROIs by mouse drag on the ROI itself (using alt/shift modifiers) + - #813,814,817: Performance improvements + - #837: Added options for field variables in ColorMapWidget + - #840, 932: Improve clipping behavior + - #841: Set color of tick-labels separately + - #922: Curve fill for fill-patches + - #996: Allow the update of LegendItem + - #1023: Add bookkeeping exporter parameters + - #1072: HDF5Exporter handling of ragged curves with tests + - #1124: Syntax highlighting for examples. + - #1154: Date axis item + - #393: NEW show/hide gradient ticks NEW link gradientEditor to others + - #1211: Add support for running pyside2-uic binary to dynamically compile ui files API / behavior changes: - Deprecated graphicsWindow classes; these have been unnecessary for many years because widgets can be placed into a new window just by calling show(). + - #158: Make DockArea compatible with Qt Designer + - #406: Applying alpha mask on numpy.nan data values - #566: ArrowItem's `angle` option now rotates the arrow without affecting its coordinate system. The result is visually the same, but children of ArrowItem are no longer rotated (this allows screen-aligned text to be attached more easily). @@ -76,8 +93,50 @@ pyqtgraph-0.11.0 (in development) - #589: Remove SpiralROI (this was unintentionally added in the first case) - #593: Override qAbort on slot exceptions for PyQt>=5.5 - #657: When a floating Dock window is closed, the dock is now returned home + - #771: Suppress RuntimeWarning for arrays containing zeros in logscale + - #942: If the visible GraphicsView is garbage collected, a warning is issued. + - #958: Nicer Legend + - #963: Last image in image-stack can now be selected with the z-slider + - #992: Added a setter for GlGridItem.color. + - #999: Make outline around fillLevel optional. + - #1014: Enable various arguments as color in colormap. + - #1044: Raise AttributeError in __getattr__ in graphicsWindows (deprecated) + - #1055: Remove global for CONFIG_OPTIONS in setConfigOption + - #1066: Add RemoteGraphicsView to __init__.py + - #1069: Allow actions to display title instead of name + - #1074: Validate min/max text inputs in ViewBoxMenu + - #1076: Reset currentRow and currentCol on GraphicsLayout.clear() + - #1079: Improve performance of updateData PlotCurveItem + - #1082: Allow MetaArray.__array__ to accept an optional dtype arg + - #841: set color of tick-labels separately + - #1111: Add name label to GradientEditorItem + - #1145: Pass showAxRect keyword arguments to setRange + - #1184: improve SymbolAtlas.getSymbolCoords performance + - #1198: improve SymbolAtlas.getSymbolCoords and ScatterPlotItem.plot performance + - #1197: Disable remove ROI menu action in handle context menu + - #1188: Added support for plot curve to handle both fill and connect args + - #801: Remove use of GraphicsScene._addressCache in translateGraphicsItem + - Deprecates registerObject meethod of GraphicsScene + - Deprecates regstar argument to GraphicsScene.__init__ + - #1166: pg.mkQApp: Pass non-empty string array to QApplication() as default + - #1199: Pass non-empty sys.argv to QApplication + - #1090: dump ExportDialog.exporterParameters + - #1173: GraphicsLayout: Always call layout.activate() after adding items + - #1097: pretty-print log-scale axes labels + - #755: Check lastDownsample in viewTransformChanged + - #1216: Add cache for mapRectFromView + - #444: Fix duplicate menus in GradientEditorItem + - #151: Optionally provide custom PlotItem to PlotWidget + - #1093: Fix aspectRatio and zoom range issues when zooming + - #390: moved some functionality from method 'export' to new method + - #468: Patch/window handling + - #392: new method 'getAxpectRatio' with code taken from 'setAspectLocked' + - #1206: Added context menu option to parametertree + - #1228: Minor improvements to LegendItem Bugfixes: + - #88: Fixed image scatterplot export + - #356: Fix some NumPy warnings - #408: Fix `cleanup` when the running qt application is not a QApplication - #410: SpinBox fixes - fixed bug with exponents disappearing after edit @@ -93,7 +152,7 @@ pyqtgraph-0.11.0 (in development) - fixed spinbox height too small for font size - ROI subclass getArrayRegion methods are a bit more consistent (still need work) - #424: Fix crash when running pyqtgraph with python -OO - - #429: fix fft premature slicing away of 0 freq bin + - #429: Fix fft premature slicing away of 0 freq bin - #458: Fixed image export problems with new numpy API - #478: Fixed PySide image memory leak - #475: Fixed unicode error when exporting to SVG with non-ascii symbols @@ -126,17 +185,18 @@ pyqtgraph-0.11.0 (in development) - #592,595: Fix InvisibleRootItem issues introduced in #518 - #596: Fix polyline click causing lines to bedrawn to the wrong node - #598: Better ParameterTree support for dark themes + - #599: Prevent invalid list access in GraphicsScene - #623: Fix PyQt5 / ScatterPlot issue with custom symbols - #626: Fix OpenGL texture state leaking to wrong items - #627: Fix ConsoleWidget stack handling on python 3.5 - #633: Fix OpenGL cylinder geometry - #637: Fix TypeError in isosurface - #641,642: Fix SVG export on Qt5 / high-DPI displays - - #645: scatterplotwidget behaves nicely when data contains infs + - #645: ScatterPlotWidget behaves nicely when data contains infs - #653: ScatterPlotItem: Fix a GC memory leak due to numpy issue 6581 - #648: fix color ignored in GLGridItem - - #671: fixed SVG export failing if the first value of a plot is nan - - #674: fixed parallelizer leaking file handles + - #671: Fixed SVG export failing if the first value of a plot is nan + - #674: Fixed parallelizer leaking file handles - #675: Gracefully handle case where image data has size==0 - #679: Fix overflow in Point.length() - #682: Fix: mkQApp returned None if a QApplication was already created elsewhere @@ -153,14 +213,103 @@ pyqtgraph-0.11.0 (in development) it was causing auto range to be disabled. - #723: Fix axis ticks when using self.scale - #739: Fix handling of 2-axis mouse wheel events + - #742: Fix Metaarray in python 3 - #758: Fix remote graphicsview "ValueError: mmap length is greater than file size" on OSX. + - #763: Fix OverflowError when using Auto Downsampling. + - #767: Fix Image display for images with the same value everywhere. + - #770: Fix GLVieWidget.setCameraPosition ignoring first parameter. + - #782: Fix missing FileForwarder thread termination. + - #787: Fix encoding errors in checkOpenGLVersion. + - #793: Fix wrong default scaling in makeARGB + - #815: Fixed mirroring of x-axis with "invert Axis" submenu. + - #824: Fix several issues related with mouse movement and GraphicsView. + - #832: Fix Permission error in tests due to unclosed filehandle. + - #836: Fix tickSpacing bug that lead to axis not being drawn. + - #861: Fix crash of PlotWidget if empty ErrorBarItem is added. + - #868: Fix segfault on repeated closing of matplotlib exporter. + - #875,876,887,934,947,980: Fix deprecation warnings. + - #886: Fix flowchart saving on python3. + - #888: Fix TreeWidget.topLevelItems in python3. + - #924: Fix QWheelEvent in RemoteGraphicsView with pyqt5. + - #935: Fix PlotItem.addLine with 'pos' and 'angle' parameter. + - #949: Fix multiline parameters (such as arrays) reading from config files. + - #951: Fix event firing from scale handler. + - #952: Fix RotateFree handle dragging + - #953: Fix HistogramLUTWidget with background parameter + - #968: Fix Si units in AxisItem leading to an incorrect unit. + - #970: Always update transform when setting angle of a TextItem + - #971: Fix a segfault stemming from incorrect signal disconnection. + - #972: Correctly include SI units for log AxisItems + - #974: Fix recursion error when instancing CtrlNode. + - #987: Fix visibility reset when PlotItems are removed. + - #998: Fix QtProcess proxy being unable to handle numpy arrays with dtype uint8. + - #1010: Fix matplotlib/CSV export. + - #1012: Fix circular texture centering + - #1015: Iterators are now converted to NumPy arrays. + - #1016: Fix synchronisation of multiple ImageViews with time axis. + - #1017: Fix duplicate paint calls emitted by Items on ViewBox. + - #1019: Fix disappearing GLGridItems when PlotItems are removed and readded. + - #1024: Prevent element-wise string comparison + - #1031: Reset ParentItem to None on removing from PlotItem/ViewBox + - #1044: Fix PlotCurveItem.paintGL + - #1048: Fix bounding box for InfiniteLine + - #1062: Fix flowchart context menu redundant menu + - #1062: Fix a typo + - #1073: Fix Python3 compatibility + - #1083: Fix SVG export of scatter plots + - #1085: Fix ofset when drawing symbol + - #1101: Fix small oversight in LegendItem + - #1113: Correctly call hasFaceIndexedData function + - #1139: Bug fix in LegendItem for `setPen`, `setBrush` etc (Call update instead of paint) + - #1110: fix for makeARGB error after #955 + - #1063: Fix: AttributeError in ViewBox.setEnableMenu + - #1151: ImageExporter py2-pyside fix with test + - #1133: compatibility-fix for py2/pyside + - #1152: Nanmask fix in makeARGB + - #1159: Fix: Update axes after data is set + - #1156: SVGExporter: Correct image pixelation + - #1169: Replace default list arg with None + - #770: Do not ignore pos argument of setCameraPosition + - #1180: Fix: AxisItem tickFont is defined in two places while only one is used + - #1168: GroupParameterItem: Did not pass changed options to ParameterItem + - #1174: Fixed a possible race condition with linked views + - #809: Fix selection of FlowchartWidget input/output nodes + - #1071: Fix py3 execution in flowchart + - #1212: Fix PixelVectors cache + - #1161: Correctly import numpy where needed + - #1218: Fix ParameterTree.clear() + - #1175: Fix: Parameter tree ignores user-set 'expanded' state + - #1219: Encode csv export header as unicode + - #507: Fix Dock close event QLabel still running with no parent + - #1222: py3 fix for ScatterPlotWidget.setSelectedFields + - #1203: Image axis order bugfix + - #1225: ParameterTree: Fix custom context menu + Maintenance: - Lots of new unit tests - Lots of code cleanup + - A lot of work on CI pipelines, test coverage and test passing (see e.g. #903,911) - #546: Add check for EINTR during example testing to avoid sporadic test failures on travis - #624: TravisCI no longer running python 2.6 tests - #695: "dev0" added to version string + - #865,873,877 (and more): Implement Azure CI pipelines, fix Travis CI + - #991: Use Azure Pipelines to do style checks, Add .pre-commit-config.yaml + - #1042: Close windows at the end of test functions + - #1046: Establish minimum numpy version, remove legacy workarounds + - #1067: Make scipy dependency optional + - #1114: doc: Fix small mistake in introduction + - #1131: Update CI/tox and Enable More Tests + - #1142: Miscellaneous doc fixups + - #1179: DateAxisItem: AxisItem unlinking tests and doc fixed + - #1201: Get readthedocs working + - #1214: Pin PyVirtualDisplay Version + - #1215: Skip test when on qt 5.9 + - #1221: Identify pyqt5 5.15 ci issue + - #1223: Remove workaround for memory leak in QImage + - #1217: Get docs version and copyright year dynamically + - #1229: Wrap text in tables in docs + - #1231: Update readme for 0.11 release pyqtgraph-0.10.0 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3ca5e0bf..461e9b14 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,6 +1,6 @@ # Contributing to PyQtGraph -Contributions to pyqtgraph are welcome! +Contributions to pyqtgraph are welcome! Please use the following guidelines when preparing changes: @@ -9,15 +9,20 @@ Please use the following guidelines when preparing changes: * The preferred method for submitting changes is by github pull request against the "develop" branch. * Pull requests should include only a focused and related set of changes. Mixed features and unrelated changes may be rejected. * For major changes, it is recommended to discuss your plans on the mailing list or in a github issue before putting in too much effort. - * Along these lines, please note that `pyqtgraph.opengl` will be deprecated soon and replaced with VisPy. +* The following deprecations are being considered by the maintainers + * `pyqtgraph.opengl` may be deprecated and replaced with `VisPy` functionality + * After v0.11, pyqtgraph will adopt [NEP-29](https://numpy.org/neps/nep-0029-deprecation_policy.html) which will effectively mean that python2 support will be deprecated + * Qt4 will be deprecated shortly, as well as Qt5<5.9 (and potentially <5.12) ## Documentation -* Writing proper documentation and unit tests is highly encouraged. PyQtGraph uses nose / pytest style testing, so tests should usually be included in a tests/ directory adjacent to the relevant code. +* Writing proper documentation and unit tests is highly encouraged. PyQtGraph uses pytest style testing, so tests should usually be included in a tests/ directory adjacent to the relevant code. * Documentation is generated with sphinx; please check that docstring changes compile correctly ## Style guidelines +### Rules + * PyQtGraph prefers PEP8 for most style issues, but this is not enforced rigorously as long as the code is clean and readable. * Use `python setup.py style` to see whether your code follows the mandatory style guidelines checked by flake8. * Exception 1: All variable names should use camelCase rather than underscore_separation. This is done for consistency with Qt @@ -33,9 +38,15 @@ Please use the following guidelines when preparing changes: ============== ======================================================== ``` - QObject subclasses that implement new signals should also describe + QObject subclasses that implement new signals should also describe these in a similar table. - + +### Pre-Commit + +PyQtGraph developers are highly encouraged to (but not required) to use [`pre-commit`](https://pre-commit.com/). `pre-commit` does a number of checks when attempting to commit the code to ensure it conforms to various standards, such as `flake8`, utf-8 encoding pragma, line-ending fixers, and so on. If any of the checks fail, the commit will be rejected, and you will have the opportunity to make the necessary fixes before adding and committing a file again. This ensures that every commit made conforms to (most) of the styling standards that the library enforces; and you will most likely pass the code style checks by the CI. + +To make use of `pre-commit`, have it available in your `$PATH` and run `pre-commit install` from the root directory of PyQtGraph. + ## Testing Setting up a test environment ### Dependencies @@ -45,9 +56,10 @@ Please use the following guidelines when preparing changes: * pytest * pytest-cov * pytest-xdist -* pytest-faulthandler * Optional: pytest-xvfb +If you have `pytest<5` (used in python2), you may also want to install `pytest-faulthandler==1.6` plugin to output extra debugging information in case of test failures. This isn't necessary with `pytest>=5` + ### Tox As PyQtGraph supports a wide array of Qt-bindings, and python versions, we make use of `tox` to test against most of the configurations in our test matrix. As some of the qt-bindings are only installable via `conda`, `conda` needs to be in your `PATH`, and we utilize the `tox-conda` plugin. @@ -57,13 +69,4 @@ As PyQtGraph supports a wide array of Qt-bindings, and python versions, we make ### Continous Integration -For our Continuous Integration, we utilize Azure Pipelines. On each OS, we test the following 6 configurations - -* Python2.7 with PyQt4 -* Python2.7 with PySide -* Python3.6 with PyQt5-5.9 -* Python3.6 with PySide2-5.9 -* Python3.7 with PyQt5-5.12 -* Python3.7 with PySide2-5.12 - -More information on coverage and test failures can be found on the respective tabs of the [build results page](https://dev.azure.com/pyqtgraph/pyqtgraph/_build?definitionId=1) +For our Continuous Integration, we utilize Azure Pipelines. Tested configurations are visible on [README](README.md). More information on coverage and test failures can be found on the respective tabs of the [build results page](https://dev.azure.com/pyqtgraph/pyqtgraph/_build?definitionId=1) diff --git a/README.md b/README.md index e5b3a9c7..07787663 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ [![Build Status](https://pyqtgraph.visualstudio.com/pyqtgraph/_apis/build/status/pyqtgraph.pyqtgraph?branchName=develop)](https://pyqtgraph.visualstudio.com/pyqtgraph/_build/latest?definitionId=17&branchName=develop) - +[![Documentation Status](https://readthedocs.org/projects/pyqtgraph/badge/?version=latest)](https://pyqtgraph.readthedocs.io/en/latest/?badge=latest) PyQtGraph ========= A pure-Python graphics library for PyQt/PySide/PyQt5/PySide2 -Copyright 2019 Luke Campagnola, University of North Carolina at Chapel Hill +Copyright 2020 Luke Campagnola, University of North Carolina at Chapel Hill @@ -19,29 +19,31 @@ heavy leverage of numpy for number crunching, Qt's GraphicsView framework for Requirements ------------ -* PyQt 4.8+, PySide, PyQt5, or PySide2 -* python 2.7, or 3.x +* Python 2.7, or 3.x * Required - * `numpy`, `scipy` + * PyQt 4.8+, PySide, PyQt5, or PySide2 + * `numpy` * Optional + * `scipy` for image processing * `pyopengl` for 3D graphics - * `pyqtgraph.opengl` will be depreciated in a future version and replaced with `VisPy` * `hdf5` for large hdf5 binary format support -* Known to run on Windows, Linux, and macOS. Qt Bindings Test Matrix ----------------------- -Below is a table of the configurations we test and have confidence pyqtgraph will work with. All current operating major operating systems (Windows, macOS, Linux) are tested against this configuration. We recommend using the Qt 5.12 or 5.9 (either PyQt5 or PySide2) bindings. +The following table represents the python environments we test in our CI system. Our CI system uses Ubuntu 18.04, Windows Server 2019, and macOS 10.15 base images. -| Python Version | PyQt4 | PySide | PyQt5-5.6 | PySide2-5.6 | PyQt5-5.9 | PySide2-5.9 | PyQt5-5.12 | PySide2 5.12 | -| :-------------- | :----------------: | :----------------: | :----------------: | :----------------: | :----------------: | :----------------: | :----------------: | :----------------: | -| 2.7 | :white_check_mark: | :white_check_mark: | :x: | :x: | :white_check_mark: | :white_check_mark: | :x: | :white_check_mark: | -| 3.5 | :x: | :x: | :white_check_mark: | :x: | :x: | :x: | :white_check_mark: | :white_check_mark: | -| 3.6 | :x: | :x: | :x: | :x: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | -| 3.7 | :x: | :x: | :x: | :x: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | +| Qt-Bindings | Python 2.7 | Python 3.6 | Python 3.7 | Python 3.8 | +| :------------- | :----------------: | :----------------: | :----------------: | :----------------: | +| PyQt-4 | :white_check_mark: | :x: | :x: | :x: | +| PySide1 | :white_check_mark: | :x: | :x: | :x: | +| PyQt5-5.9 | :x: | :white_check_mark: | :x: | :x: | +| PySide2-5.13 | :x: | :x: | :white_check_mark: | :x: | +| PyQt5-Latest | :x: | :x: | :x: | :white_check_mark: | +| PySide2-Latest | :x: | :x: | :x: | :white_check_mark: | -* pyqtgraph has had some incompatabilities with PySide2-5.6, and we recommend you avoid those bindings if possible. +* pyqtgraph has had some incompatibilities with PySide2 versions 5.6-5.11, and we recommend you avoid those versions if possible +* on macOS with Python 2.7 and Qt4 bindings (PyQt4 or PySide) the openGL related visualizations do not work reliably Support ------- @@ -54,18 +56,17 @@ Installation Methods * From PyPI: * Last released version: `pip install pyqtgraph` - * Latest development version: `pip install git+https://github.com/pyqtgraph/pyqtgraph@develop` + * Latest development version: `pip install git+https://github.com/pyqtgraph/pyqtgraph@master` * From conda - * Last released version: `conda install pyqtgraph` + * Last released version: `conda install -c conda-forge pyqtgraph` * To install system-wide from source distribution: `python setup.py install` * Many linux package repositories have release versions. * To use with a specific project, simply copy the pyqtgraph subdirectory anywhere that is importable from your project. -* For installation packages, see the website (pyqtgraph.org) Documentation ------------- -The easiest way to learn pyqtgraph is to browse through the examples; run `python -m pyqtgraph.examples` for a menu. +The official documentation lives at https://pyqtgraph.readthedocs.io -The official documentation lives at http://pyqtgraph.org/documentation +The easiest way to learn pyqtgraph is to browse through the examples; run `python -m pyqtgraph.examples` to launch the examples application. diff --git a/azure-pipelines.yml b/azure-pipelines.yml index b91f515a..eb379119 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -1,7 +1,3 @@ -############################################################################################ -# This config was rectrieved in no small part from https://github.com/slaclab/pydm -############################################################################################ - trigger: branches: include: @@ -20,19 +16,84 @@ pr: variables: OFFICIAL_REPO: 'pyqtgraph/pyqtgraph' + DEFAULT_MERGE_BRANCH: 'develop' + disable.coverage.autogenerate: 'true' -jobs: +stages: +- stage: "pre_test" + jobs: + - job: check_diff_size + pool: + vmImage: 'Ubuntu 18.04' + steps: + - bash: | + git config --global advice.detachedHead false + mkdir ~/repo-clone && cd ~/repo-clone + git init + + git remote add -t $(Build.SourceBranchName) origin $(Build.Repository.Uri) + git remote add -t ${DEFAULT_MERGE_BRANCH} upstream https://github.com/${OFFICIAL_REPO}.git + + git fetch origin $(Build.SourceBranchName) + git fetch upstream ${DEFAULT_MERGE_BRANCH} + + git checkout $(Build.SourceBranchName) + MERGE_SIZE=`du -s . | sed -e "s/\t.*//"` + echo -e "Merge Size ${MERGE_SIZE}" + + git checkout ${DEFAULT_MERGE_BRANCH} + TARGET_SIZE=`du -s . | sed -e "s/\t.*//"` + echo -e "Target Size ${TARGET_SIZE}" + + if [ "${MERGE_SIZE}" != "${TARGET_SIZE}" ]; then + SIZE_DIFF=`expr \( ${MERGE_SIZE} - ${TARGET_SIZE} \)`; + else + SIZE_DIFF=0; + fi; + echo -e "Estimated content size difference = ${SIZE_DIFF} kB" && + test ${SIZE_DIFF} -lt 100; + displayName: 'Diff Size Check' + continueOnError: true + + - job: "style_check" + pool: + vmImage: "Ubuntu 18.04" + steps: + - task: UsePythonVersion@0 + inputs: + versionSpec: 3.7 + - bash: | + pip install flake8 + python setup.py style + displayName: 'flake8 check' + continueOnError: true + + - job: "build_wheel" + pool: + vmImage: 'Ubuntu 18.04' + steps: + - task: UsePythonVersion@0 + inputs: + versionSpec: 3.8 + - script: | + python -m pip install setuptools wheel + python setup.py bdist_wheel --universal + displayName: "Build Python Wheel" + continueOnError: false + - publish: dist + artifact: wheel + +- stage: "test" + jobs: - template: azure-test-template.yml parameters: - name: Linux - vmImage: 'Ubuntu 16.04' - + name: linux + vmImage: 'Ubuntu 18.04' - template: azure-test-template.yml parameters: - name: Windows - vmImage: 'vs2017-win2016' - + name: windows + vmImage: 'windows-2019' - template: azure-test-template.yml parameters: - name: MacOS - vmImage: 'macOS-10.13' + name: macOS + vmImage: 'macOS-10.15' diff --git a/azure-test-template.yml b/azure-test-template.yml index 496ec10b..e1d4e177 100644 --- a/azure-test-template.yml +++ b/azure-test-template.yml @@ -18,24 +18,30 @@ jobs: python.version: '2.7' qt.bindings: "pyside" install.method: "conda" - Python36-PyQt-5.9: + Python36-PyQt5-5.9: python.version: "3.6" qt.bindings: "pyqt" install.method: "conda" - Python36-PySide2-5.9: - python.version: "3.6" + Python37-PySide2-5.13: + python.version: "3.7" qt.bindings: "pyside2" install.method: "conda" - Python37-PyQt-5.12: - python.version: '3.7' + Python38-PyQt5-Latest: + python.version: '3.8' qt.bindings: "PyQt5" install.method: "pip" - Python37-PySide2-5.12: - python.version: "3.7" + Python38-PySide2-Latest: + python.version: '3.8' qt.bindings: "PySide2" - install.method: "pip" + install.method: "pip" steps: + - task: DownloadPipelineArtifact@2 + inputs: + source: 'current' + artifact: wheel + path: 'dist' + - task: ScreenResolutionUtility@1 inputs: displaySettings: 'specific' @@ -43,6 +49,11 @@ jobs: height: '1080' condition: eq(variables['agent.os'], 'Windows_NT' ) + - task: UsePythonVersion@0 + inputs: + versionSpec: $(python.version) + condition: eq(variables['install.method'], 'pip') + - script: | curl -LJO https://github.com/pal1000/mesa-dist-win/releases/download/19.1.0/mesa3d-19.1.0-release-msvc.exe 7z x mesa3d-19.1.0-release-msvc.exe @@ -60,81 +71,81 @@ jobs: displayName: "Install Windows-Mesa OpenGL DLL" condition: eq(variables['agent.os'], 'Windows_NT') - - task: UsePythonVersion@0 - inputs: - versionSpec: $(python.version) - condition: eq(variables['install.method'], 'pip') - - bash: | if [ $(agent.os) == 'Linux' ] then - echo '##vso[task.prependpath]/usr/share/miniconda/bin' + echo "##vso[task.prependpath]$CONDA/bin" elif [ $(agent.os) == 'Darwin' ] then - echo '##vso[task.prependpath]$CONDA/bin' - sudo install -d -m 0777 /usr/local/miniconda/envs + sudo chown -R $USER $CONDA + echo "##vso[task.prependpath]$CONDA/bin" elif [ $(agent.os) == 'Windows_NT' ] then - echo "##vso[task.prependpath]$env:CONDA\Scripts" + echo "##vso[task.prependpath]$CONDA/Scripts" else echo 'Just what OS are you using?' fi - displayName: 'Add Conda to $PATH' + displayName: 'Add Conda To $PATH' condition: eq(variables['install.method'], 'conda' ) - - - task: CondaEnvironment@0 - displayName: 'Create Conda Environment' - condition: eq(variables['install.method'], 'conda') - inputs: - environmentName: 'test-environment-$(python.version)' - packageSpecs: 'python=$(python.version)' + continueOnError: false - bash: | if [ $(install.method) == "conda" ] then + conda update --all --yes --quiet + conda create --name test-environment-$(python.version) python=$(python.version) --yes --quiet source activate test-environment-$(python.version) - conda install -c conda-forge $(qt.bindings) numpy scipy pyopengl pytest flake8 six coverage --yes --quiet + conda config --env --set always_yes true + if [ $(python.version) == '2.7' ] + then + conda config --set restore_free_channel true + fi + if [ $(qt.bindings) == "pyside2" ] || ([ $(qt.bindings) == 'pyside' ] && [ $(agent.os) == 'Darwin' ]) + then + conda config --prepend channels conda-forge + fi + conda info + conda install $(qt.bindings) numpy scipy pyopengl h5py six --yes --quiet else - pip install $(qt.bindings) numpy scipy pyopengl pytest flake8 six coverage + pip install $(qt.bindings) numpy scipy pyopengl h5py six + fi + pip install pytest pytest-cov coverage pytest-xdist + if [ $(python.version) == "2.7" ] + then + pip install pytest-faulthandler==1.6.0 + export PYTEST_ADDOPTS="--faulthandler-timeout=15" + else + pip install pytest pytest-cov coverage fi - pip install pytest-xdist pytest-cov pytest-faulthandler displayName: "Install Dependencies" - + - bash: | if [ $(install.method) == "conda" ] then source activate test-environment-$(python.version) fi - pip install setuptools wheel - python setup.py bdist_wheel - pip install dist/*.whl - displayName: 'Build Wheel and Install' - - - task: CopyFiles@2 - inputs: - contents: 'dist/**' - targetFolder: $(Build.ArtifactStagingDirectory) - cleanTargetFolder: true - displayName: "Copy Binary Wheel Distribution To Artifacts" - - - task: PublishBuildArtifacts@1 - displayName: 'Publish Binary Wheel' - condition: always() - inputs: - pathtoPublish: $(Build.ArtifactStagingDirectory)/dist - artifactName: Distributions + python -m pip install --no-index --find-links=dist pyqtgraph + displayName: 'Install Wheel' - bash: | - sudo apt-get install -y libxkbcommon-x11-0 # herbstluftwm + sudo apt-get install -y libxkbcommon-x11-dev + # workaround for QTBUG-84489 + sudo apt-get install -y libxcb-xfixes0 libxcb-icccm4 libxcb-image0 libxcb-keysyms1 libxcb-randr0 libxcb-render-util0 libxcb-xinerama0 if [ $(install.method) == "conda" ] then source activate test-environment-$(python.version) fi - pip install pytest-xvfb + if [ $(python.version) == "2.7" ] + then + pip install PyVirtualDisplay==0.2.5 pytest-xvfb==1.2.0 + else + pip install pytest-xvfb + fi displayName: "Virtual Display Setup" condition: eq(variables['agent.os'], 'Linux' ) - bash: | + export QT_DEBUG_PLUGINS=1 if [ $(install.method) == "conda" ] then source activate test-environment-$(python.version) @@ -167,9 +178,10 @@ jobs: mkdir -p "$SCREENSHOT_DIR" # echo "If Screenshots are generated, they may be downloaded from:" # echo "https://dev.azure.com/pyqtgraph/pyqtgraph/_apis/build/builds/$(Build.BuildId)/artifacts?artifactName=Screenshots&api-version=5.0" - pytest . -sv \ + pytest . -v \ + -n 1 \ --junitxml=junit/test-results.xml \ - -n 1 --cov pyqtgraph --cov-report=xml --cov-report=html + --cov pyqtgraph --cov-report=xml --cov-report=html displayName: 'Unit tests' env: AZURE: 1 @@ -193,4 +205,4 @@ jobs: inputs: codeCoverageTool: Cobertura summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml' - reportDirectory: '$(System.DefaultWorkingDirectory)/**/htmlcov' \ No newline at end of file + reportDirectory: '$(System.DefaultWorkingDirectory)/**/htmlcov' diff --git a/doc/requirements.txt b/doc/requirements.txt new file mode 100644 index 00000000..60d1d1e7 --- /dev/null +++ b/doc/requirements.txt @@ -0,0 +1,5 @@ +pyside2 +numpy +pyopengl +sphinx +sphinx_rtd_theme diff --git a/doc/source/_static/custom.css b/doc/source/_static/custom.css new file mode 100644 index 00000000..2ad3413b --- /dev/null +++ b/doc/source/_static/custom.css @@ -0,0 +1,14 @@ +/* Customizations to the theme */ + +/* override table width restrictions */ +/* https://github.com/readthedocs/sphinx_rtd_theme/issues/117 */ +@media screen and (min-width: 768px) { + .wy-table-responsive table td, .wy-table-responsive table th { + white-space: normal !important; + } + + .wy-table-responsive { + overflow: visible !important; + max-width: 100%; + } +} diff --git a/doc/source/apireference.rst b/doc/source/apireference.rst index c4dc64aa..c52c8df1 100644 --- a/doc/source/apireference.rst +++ b/doc/source/apireference.rst @@ -13,5 +13,7 @@ Contents: 3dgraphics/index colormap parametertree/index + dockarea graphicsscene/index flowchart/index + graphicswindow diff --git a/doc/source/conf.py b/doc/source/conf.py index 3ec48f75..4038cba4 100644 --- a/doc/source/conf.py +++ b/doc/source/conf.py @@ -11,7 +11,10 @@ # All configuration values have a default; values that are commented out # serve to show the default. -import sys, os +import time +import sys +import os +from datetime import datetime # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the @@ -19,6 +22,7 @@ import sys, os path = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(path, '..', '..')) sys.path.insert(0, os.path.join(path, '..', 'extensions')) +import pyqtgraph # -- General configuration ----------------------------------------------------- @@ -43,16 +47,19 @@ master_doc = 'index' # General information about the project. project = 'pyqtgraph' -copyright = '2011, Luke Campagnola' +now = datetime.utcfromtimestamp( + int(os.environ.get('SOURCE_DATE_EPOCH', time.time())) +) +copyright = '2011 - {}, Luke Campagnola'.format(now.year) # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. -version = '0.10.0' +version = pyqtgraph.__version__ # The full version, including alpha/beta/rc tags. -release = '0.10.0' +release = version # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. @@ -88,12 +95,18 @@ pygments_style = 'sphinx' # A list of ignored prefixes for module index sorting. #modindex_common_prefix = [] +autodoc_inherit_docstrings = False +autodoc_mock_imports = [ + "scipy", + "h5py", + "matplotlib", +] # -- Options for HTML output --------------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. -html_theme = 'default' +html_theme = 'sphinx_rtd_theme' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the @@ -124,6 +137,10 @@ html_theme = 'default' # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['_static'] +# add the theme customizations +def setup(app): + app.add_stylesheet("custom.css") + # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. #html_last_updated_fmt = '%b %d, %Y' @@ -216,4 +233,3 @@ man_pages = [ ('index', 'pyqtgraph', 'pyqtgraph Documentation', ['Luke Campagnola'], 1) ] - diff --git a/doc/source/dockarea.rst b/doc/source/dockarea.rst new file mode 100644 index 00000000..384581d3 --- /dev/null +++ b/doc/source/dockarea.rst @@ -0,0 +1,11 @@ +Dock Area Module +================ + +.. automodule:: pyqtgraph.dockarea + :members: + +.. autoclass:: pyqtgraph.dockarea.DockArea + :members: + +.. autoclass:: pyqtgraph.dockarea.Dock + :members: diff --git a/doc/source/exporting.rst b/doc/source/exporting.rst index ccd017d7..0bb1c82a 100644 --- a/doc/source/exporting.rst +++ b/doc/source/exporting.rst @@ -30,8 +30,13 @@ Export Formats for export. * Printer - Exports to the operating system's printing service. This exporter is provided for completeness, but is not well supported due to problems with Qt's printing system. +* HDF5 - Exports data from a :class:`~pyqtgraph.PlotItem` to a HDF5 file if + h5py_ is installed. This exporter supports :class:`~pyqtgraph.PlotItem` + objects containing multiple curves, stacking the data into a single HDF5 + dataset based on the ``columnMode`` parameter. If data items aren't the same + size, each one is given its own dataset. - +.. _h5py: https://www.h5py.org/ Exporting from the API ---------------------- diff --git a/doc/source/graphicsItems/bargraphitem.rst b/doc/source/graphicsItems/bargraphitem.rst new file mode 100644 index 00000000..4959385b --- /dev/null +++ b/doc/source/graphicsItems/bargraphitem.rst @@ -0,0 +1,8 @@ +BarGraphItem +============ + +.. autoclass:: pyqtgraph.BarGraphItem + :members: + + .. automethod:: pyqtgraph.BarGraphItem.__init__ + diff --git a/doc/source/graphicsItems/dateaxisitem.rst b/doc/source/graphicsItems/dateaxisitem.rst new file mode 100644 index 00000000..9da36c6f --- /dev/null +++ b/doc/source/graphicsItems/dateaxisitem.rst @@ -0,0 +1,8 @@ +DateAxisItem +============ + +.. autoclass:: pyqtgraph.DateAxisItem + :members: + + .. automethod:: pyqtgraph.DateAxisItem.__init__ + diff --git a/doc/source/graphicsItems/index.rst b/doc/source/graphicsItems/index.rst index 7042d27e..390d8f17 100644 --- a/doc/source/graphicsItems/index.rst +++ b/doc/source/graphicsItems/index.rst @@ -24,6 +24,7 @@ Contents: axisitem textitem errorbaritem + bargraphitem arrowitem fillbetweenitem curvepoint @@ -42,4 +43,4 @@ Contents: graphicsitem uigraphicsitem graphicswidgetanchor - + dateaxisitem diff --git a/doc/source/graphicsItems/make b/doc/source/graphicsItems/make index 293db0d6..9d9f9954 100644 --- a/doc/source/graphicsItems/make +++ b/doc/source/graphicsItems/make @@ -2,6 +2,7 @@ files = """ArrowItem AxisItem ButtonItem CurvePoint +DateAxisItem GradientEditorItem GradientLegend GraphicsLayout diff --git a/doc/source/graphicswindow.rst b/doc/source/graphicswindow.rst index 3d5641c3..0602ae7e 100644 --- a/doc/source/graphicswindow.rst +++ b/doc/source/graphicswindow.rst @@ -1,8 +1,16 @@ -Basic display widgets -===================== +Deprecated Window Classes +========================= - - GraphicsWindow - - GraphicsView - - GraphicsLayoutItem - - ViewBox +.. automodule:: pyqtgraph.graphicsWindows +.. autoclass:: pyqtgraph.GraphicsWindow + :members: + +.. autoclass:: pyqtgraph.TabWindow + :members: + +.. autoclass:: pyqtgraph.PlotWindow + :members: + +.. autoclass:: pyqtgraph.ImageWindow + :members: diff --git a/doc/source/installation.rst b/doc/source/installation.rst index e3e1f1fc..fd9f5288 100644 --- a/doc/source/installation.rst +++ b/doc/source/installation.rst @@ -1,55 +1,70 @@ Installation ============ -There are many different ways to install pyqtgraph, depending on your needs: - -* The most common way to install pyqtgraph is with pip:: - - $ pip install pyqtgraph - - Some users may need to call ``pip3`` instead. This method should work on - all platforms. -* To get access to the very latest features and bugfixes you have three choice:: - - 1. Clone pyqtgraph from github:: - - $ git clone https://github.com/pyqtgraph/pyqtgraph - - Now you can install pyqtgraph from the source:: - - $ python setup.py install - - 2. Directly install from GitHub repo:: - - $ pip install git+git://github.com/pyqtgraph/pyqtgraph.git@develop - - You can change to ``develop`` of the above command to the branch - name or the commit you prefer. - - 3. - You can simply place the pyqtgraph folder someplace importable, such as - inside the root of another project. PyQtGraph does not need to be "built" or - compiled in any way. - -* Packages for pyqtgraph are also available in a few other forms: - - * **Anaconda**: ``conda install pyqtgraph`` - * **Debian, Ubuntu, and similar Linux:** Use ``apt install python-pyqtgraph`` or - download the .deb file linked at the top of the pyqtgraph web page. - * **Arch Linux:** has packages (thanks windel). (https://aur.archlinux.org/packages.php?ID=62577) - * **Windows:** Download and run the .exe installer file linked at the top of the pyqtgraph web page. - - -Requirements -============ - PyQtGraph depends on: - + * Python 2.7 or Python 3.x * A Qt library such as PyQt4, PyQt5, PySide, or PySide2 * numpy -The easiest way to meet these dependencies is with ``pip`` or with a scientific python -distribution like Anaconda. +The easiest way to meet these dependencies is with ``pip`` or with a scientific +python distribution like Anaconda. -.. _pyqtgraph: http://www.pyqtgraph.org/ +There are many different ways to install pyqtgraph, depending on your needs: + +pip +--- + +The most common way to install pyqtgraph is with pip:: + + $ pip install pyqtgraph + +Some users may need to call ``pip3`` instead. This method should work on all +platforms. + +conda +----- + +pyqtgraph is on the default Anaconda channel:: + + $ conda install pyqtgraph + +It is also available in the conda-forge channel:: + + $ conda install -c conda-forge pyqtgraph + +From Source +----------- + +To get access to the very latest features and bugfixes you have three choices: + +1. Clone pyqtgraph from github:: + + $ git clone https://github.com/pyqtgraph/pyqtgraph + $ cd pyqtgraph + + Now you can install pyqtgraph from the source:: + + $ pip install . + +2. Directly install from GitHub repo:: + + $ pip install git+git://github.com/pyqtgraph/pyqtgraph.git@develop + + You can change ``develop`` of the above command to the branch name or the + commit you prefer. + +3. You can simply place the pyqtgraph folder someplace importable, such as + inside the root of another project. PyQtGraph does not need to be "built" or + compiled in any way. + +Other Packages +-------------- + +Packages for pyqtgraph are also available in a few other forms: + +* **Debian, Ubuntu, and similar Linux:** Use ``apt install python-pyqtgraph`` or + download the .deb file linked at the top of the pyqtgraph web page. +* **Arch Linux:** https://www.archlinux.org/packages/community/any/python-pyqtgraph/ +* **Windows:** Download and run the .exe installer file linked at the top of the + pyqtgraph web page: http://pyqtgraph.org diff --git a/doc/source/introduction.rst b/doc/source/introduction.rst index 70161173..741acd30 100644 --- a/doc/source/introduction.rst +++ b/doc/source/introduction.rst @@ -73,7 +73,7 @@ How does it compare to... such as image interaction, volumetric rendering, parameter trees, flowcharts, etc. -* pyqwt5: About as fast as pyqwt5, but not quite as complete for plotting +* pyqwt5: About as fast as pyqtgraph, but not quite as complete for plotting functionality. Image handling in pyqtgraph is much more complete (again, no ROI widgets in qwt). Also, pyqtgraph is written in pure python, so it is more portable than pyqwt, which often lags behind pyqt in development (I diff --git a/doc/source/mouse_interaction.rst b/doc/source/mouse_interaction.rst index 3aea2527..c1bec45d 100644 --- a/doc/source/mouse_interaction.rst +++ b/doc/source/mouse_interaction.rst @@ -10,7 +10,7 @@ Most applications that use pyqtgraph's data visualization will generate widgets In pyqtgraph, most 2D visualizations follow the following mouse interaction: * **Left button:** Interacts with items in the scene (select/move objects, etc). If there are no movable objects under the mouse cursor, then dragging with the left button will pan the scene instead. -* **Right button drag:** Scales the scene. Dragging left/right scales horizontally; dragging up/down scales vertically (although some scenes will have their x/y scales locked together). If there are x/y axes fisible in the scene, then right-dragging over the axis will _only_ affect that axis. +* **Right button drag:** Scales the scene. Dragging left/right scales horizontally; dragging up/down scales vertically (although some scenes will have their x/y scales locked together). If there are x/y axes visible in the scene, then right-dragging over the axis will _only_ affect that axis. * **Right button click:** Clicking the right button in most cases will show a context menu with a variety of options depending on the object(s) under the mouse cursor. * **Middle button (or wheel) drag:** Dragging the mouse with the wheel pressed down will always pan the scene (this is useful in instances where panning with the left button is prevented by other objects in the scene). * **Wheel spin:** Zooms the scene in and out. diff --git a/doc/source/plotting.rst b/doc/source/plotting.rst index 8a99663a..956f5b97 100644 --- a/doc/source/plotting.rst +++ b/doc/source/plotting.rst @@ -41,7 +41,7 @@ There are several classes invloved in displaying plot data. Most of these classe * :class:`AxisItem ` - Displays axis values, ticks, and labels. Most commonly used with PlotItem. * Container Classes (subclasses of QWidget; may be embedded in PyQt GUIs) * :class:`PlotWidget ` - A subclass of GraphicsView with a single PlotItem displayed. Most of the methods provided by PlotItem are also available through PlotWidget. - * :class:`GraphicsLayoutWidget ` - QWidget subclass displaying a single GraphicsLayoutItem. Most of the methods provided by GraphicsLayoutItem are also available through GraphicsLayoutWidget. + * :class:`GraphicsLayoutWidget ` - QWidget subclass displaying a single :class:`~pyqtgraph.GraphicsLayout`. Most of the methods provided by :class:`~pyqtgraph.GraphicsLayout` are also available through GraphicsLayoutWidget. .. image:: images/plottingClasses.png @@ -69,5 +69,3 @@ Create/show a plot widget, display three data curves:: for i in range(3): plotWidget.plot(x, y[i], pen=(i,3)) ## setting pen=(i,3) automaticaly creates three different-colored pens - - diff --git a/doc/source/widgets/dockarea.rst b/doc/source/widgets/dockarea.rst deleted file mode 100644 index 09a6acca..00000000 --- a/doc/source/widgets/dockarea.rst +++ /dev/null @@ -1,5 +0,0 @@ -dockarea module -=============== - -.. automodule:: pyqtgraph.dockarea - :members: diff --git a/doc/source/widgets/index.rst b/doc/source/widgets/index.rst index 9cfbc0c4..e5acb7f0 100644 --- a/doc/source/widgets/index.rst +++ b/doc/source/widgets/index.rst @@ -12,11 +12,9 @@ Contents: plotwidget imageview - dockarea spinbox gradientwidget histogramlutwidget - parametertree consolewidget colormapwidget scatterplotwidget diff --git a/doc/source/widgets/parametertree.rst b/doc/source/widgets/parametertree.rst deleted file mode 100644 index 565b930b..00000000 --- a/doc/source/widgets/parametertree.rst +++ /dev/null @@ -1,5 +0,0 @@ -parametertree module -==================== - -.. automodule:: pyqtgraph.parametertree - :members: diff --git a/examples/Arrow.py b/examples/Arrow.py index d5ea2a74..2a707fec 100644 --- a/examples/Arrow.py +++ b/examples/Arrow.py @@ -30,7 +30,7 @@ p2 = cw.addPlot(row=1, col=0) ## variety of arrow shapes a1 = pg.ArrowItem(angle=-160, tipAngle=60, headLen=40, tailLen=40, tailWidth=20, pen={'color': 'w', 'width': 3}) a2 = pg.ArrowItem(angle=-120, tipAngle=30, baseAngle=20, headLen=40, tailLen=40, tailWidth=8, pen=None, brush='y') -a3 = pg.ArrowItem(angle=-60, tipAngle=30, baseAngle=20, headLen=40, tailLen=None, brush=None) +a3 = pg.ArrowItem(angle=-60, baseAngle=20, headLen=40, headWidth=20, tailLen=None, brush=None) a4 = pg.ArrowItem(angle=-20, tipAngle=30, baseAngle=-30, headLen=40, tailLen=None) a2.setPos(10,0) a3.setPos(20,0) diff --git a/examples/DateAxisItem.py b/examples/DateAxisItem.py new file mode 100644 index 00000000..7bbaafff --- /dev/null +++ b/examples/DateAxisItem.py @@ -0,0 +1,33 @@ +""" +Demonstrates the usage of DateAxisItem to display properly-formatted +timestamps on x-axis which automatically adapt to current zoom level. + +""" +import initExample ## Add path to library (just for examples; you do not need this) + +import time +from datetime import datetime, timedelta + +import numpy as np +import pyqtgraph as pg +from pyqtgraph.Qt import QtGui + +app = QtGui.QApplication([]) + +# Create a plot with a date-time axis +w = pg.PlotWidget(axisItems = {'bottom': pg.DateAxisItem()}) +w.showGrid(x=True, y=True) + +# Plot sin(1/x^2) with timestamps in the last 100 years +now = time.time() +x = np.linspace(2*np.pi, 1000*2*np.pi, 8301) +w.plot(now-(2*np.pi/x)**2*100*np.pi*1e7, np.sin(x), symbol='o') + +w.setWindowTitle('pyqtgraph example: DateAxisItem') +w.show() + +## Start Qt event loop unless running in interactive mode or using pyside. +if __name__ == '__main__': + import sys + if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): + app.exec_() diff --git a/examples/DateAxisItem_QtDesigner.py b/examples/DateAxisItem_QtDesigner.py new file mode 100644 index 00000000..f6f17489 --- /dev/null +++ b/examples/DateAxisItem_QtDesigner.py @@ -0,0 +1,48 @@ +""" +Demonstrates the usage of DateAxisItem in a layout created with Qt Designer. + +The spotlight here is on the 'setAxisItems' method, without which +one would have to subclass plotWidget in order to attach a dateaxis to it. + +""" +import initExample ## Add path to library (just for examples; you do not need this) + +import sys +import time + +import numpy as np +from PyQt5 import QtWidgets, QtCore, uic +import pyqtgraph as pg + +pg.setConfigOption('background', 'w') +pg.setConfigOption('foreground', 'k') + +BLUE = pg.mkPen('#1f77b4') + +Design, _ = uic.loadUiType('DateAxisItem_QtDesigner.ui') + +class ExampleApp(QtWidgets.QMainWindow, Design): + def __init__(self): + super().__init__() + self.setupUi(self) + now = time.time() + # Plot random values with timestamps in the last 6 months + timestamps = np.linspace(now - 6*30*24*3600, now, 100) + self.curve = self.plotWidget.plot(x=timestamps, y=np.random.rand(100), + symbol='o', symbolSize=5, pen=BLUE) + # 'o' circle 't' triangle 'd' diamond '+' plus 's' square + self.plotWidget.setAxisItems({'bottom': pg.DateAxisItem()}) + self.plotWidget.showGrid(x=True, y=True) + +app = QtWidgets.QApplication(sys.argv) +app.setStyle(QtWidgets.QStyleFactory.create('Fusion')) +app.setPalette(QtWidgets.QApplication.style().standardPalette()) +window = ExampleApp() +window.setWindowTitle('pyqtgraph example: DateAxisItem_QtDesigner') +window.show() + +## Start Qt event loop unless running in interactive mode or using pyside. +if __name__ == '__main__': + import sys + if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): + app.exec_() diff --git a/examples/DateAxisItem_QtDesigner.ui b/examples/DateAxisItem_QtDesigner.ui new file mode 100644 index 00000000..91f77ba9 --- /dev/null +++ b/examples/DateAxisItem_QtDesigner.ui @@ -0,0 +1,44 @@ + + + MainWindow + + + + 0 + 0 + 536 + 381 + + + + MainWindow + + + + + + + + + + + + 0 + 0 + 536 + 18 + + + + + + + + PlotWidget + QGraphicsView +
pyqtgraph
+
+
+ + +
diff --git a/examples/Legend.py b/examples/Legend.py index f7841151..9239f1ae 100644 --- a/examples/Legend.py +++ b/examples/Legend.py @@ -7,17 +7,37 @@ import initExample ## Add path to library (just for examples; you do not need th import pyqtgraph as pg from pyqtgraph.Qt import QtCore, QtGui +import numpy as np -plt = pg.plot() -plt.setWindowTitle('pyqtgraph example: Legend') -plt.addLegend() -#l = pg.LegendItem((100,60), offset=(70,30)) # args are (size, offset) -#l.setParentItem(plt.graphicsItem()) # Note we do NOT call plt.addItem in this case +win = pg.plot() +win.setWindowTitle('pyqtgraph example: BarGraphItem') -c1 = plt.plot([1,3,2,4], pen='r', symbol='o', symbolPen='r', symbolBrush=0.5, name='red plot') -c2 = plt.plot([2,1,4,3], pen='g', fillLevel=0, fillBrush=(255,255,255,30), name='green plot') -#l.addItem(c1, 'red plot') -#l.addItem(c2, 'green plot') +# # option1: only for .plot(), following c1,c2 for example----------------------- +# win.addLegend(frame=False, rowCount=1, colCount=2) + +# bar graph +x = np.arange(10) +y = np.sin(x+2) * 3 +bg1 = pg.BarGraphItem(x=x, height=y, width=0.3, brush='b', pen='w', name='bar') +win.addItem(bg1) + +# curve +c1 = win.plot([np.random.randint(0,8) for i in range(10)], pen='r', symbol='t', symbolPen='r', symbolBrush='g', name='curve1') +c2 = win.plot([2,1,4,3,1,3,2,4,3,2], pen='g', fillLevel=0, fillBrush=(255,255,255,30), name='curve2') + +# scatter plot +s1 = pg.ScatterPlotItem(size=10, pen=pg.mkPen(None), brush=pg.mkBrush(255, 255, 255, 120), name='scatter') +spots = [{'pos': [i, np.random.randint(-3, 3)], 'data': 1} for i in range(10)] +s1.addPoints(spots) +win.addItem(s1) + +# # option2: generic method------------------------------------------------ +legend = pg.LegendItem((80,60), offset=(70,20)) +legend.setParentItem(win.graphicsItem()) +legend.addItem(bg1, 'bar') +legend.addItem(c1, 'curve1') +legend.addItem(c2, 'curve2') +legend.addItem(s1, 'scatter') ## Start Qt event loop unless running in interactive mode or using pyside. diff --git a/examples/MultiPlotWidget.py b/examples/MultiPlotWidget.py index 5ab4b21d..67cb83ee 100644 --- a/examples/MultiPlotWidget.py +++ b/examples/MultiPlotWidget.py @@ -3,8 +3,7 @@ ## Add path to library (just for examples; you do not need this) import initExample - -from scipy import random +import numpy as np from numpy import linspace from pyqtgraph.Qt import QtGui, QtCore import pyqtgraph as pg @@ -22,7 +21,7 @@ pw = MultiPlotWidget() mw.setCentralWidget(pw) mw.show() -data = random.normal(size=(3, 1000)) * np.array([[0.1], [1e-5], [1]]) +data = np.random.normal(size=(3, 1000)) * np.array([[0.1], [1e-5], [1]]) ma = MetaArray(data, info=[ {'name': 'Signal', 'cols': [ {'name': 'Col1', 'units': 'V'}, diff --git a/examples/PlotWidget.py b/examples/PlotWidget.py index e52a893d..38bbc73c 100644 --- a/examples/PlotWidget.py +++ b/examples/PlotWidget.py @@ -13,7 +13,7 @@ import numpy as np import pyqtgraph as pg #QtGui.QApplication.setGraphicsSystem('raster') -app = QtGui.QApplication([]) +app = pg.mkQApp() mw = QtGui.QMainWindow() mw.setWindowTitle('pyqtgraph example: PlotWidget') mw.resize(800,800) diff --git a/examples/ScatterPlot.py b/examples/ScatterPlot.py index 93f184f2..ea86bd19 100644 --- a/examples/ScatterPlot.py +++ b/examples/ScatterPlot.py @@ -83,7 +83,7 @@ random_str = lambda : (''.join([chr(np.random.randint(ord('A'),ord('z'))) for i s2 = pg.ScatterPlotItem(size=10, pen=pg.mkPen('w'), pxMode=True) pos = np.random.normal(size=(2,n), scale=1e-5) -spots = [{'pos': pos[:,i], 'data': 1, 'brush':pg.intColor(i, n), 'symbol': i%5, 'size': 5+i/10.} for i in range(n)] +spots = [{'pos': pos[:,i], 'data': 1, 'brush':pg.intColor(i, n), 'symbol': i%10, 'size': 5+i/10.} for i in range(n)] s2.addPoints(spots) spots = [{'pos': pos[:,i], 'data': 1, 'brush':pg.intColor(i, n), 'symbol': label[1], 'size': label[2]*(5+i/10.)} for (i, label) in [(i, createLabel(*random_str())) for i in range(n)]] s2.addPoints(spots) @@ -120,4 +120,3 @@ if __name__ == '__main__': import sys if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'): QtGui.QApplication.instance().exec_() - diff --git a/examples/ScatterPlotSpeedTestTemplate.ui b/examples/ScatterPlotSpeedTestTemplate.ui index 6b87e85d..5cdccf0f 100644 --- a/examples/ScatterPlotSpeedTestTemplate.ui +++ b/examples/ScatterPlotSpeedTestTemplate.ui @@ -11,7 +11,7 @@ - Form + PyQtGraph diff --git a/examples/ScatterPlotSpeedTestTemplate_pyqt.py b/examples/ScatterPlotSpeedTestTemplate_pyqt.py index 22136690..896525eb 100644 --- a/examples/ScatterPlotSpeedTestTemplate_pyqt.py +++ b/examples/ScatterPlotSpeedTestTemplate_pyqt.py @@ -41,7 +41,7 @@ class Ui_Form(object): QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): - Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8)) + Form.setWindowTitle(QtGui.QApplication.translate("Form", "PyQtGraph", None, QtGui.QApplication.UnicodeUTF8)) self.pixelModeCheck.setText(QtGui.QApplication.translate("Form", "pixel mode", None, QtGui.QApplication.UnicodeUTF8)) self.label.setText(QtGui.QApplication.translate("Form", "Size", None, QtGui.QApplication.UnicodeUTF8)) self.randCheck.setText(QtGui.QApplication.translate("Form", "Randomize", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/examples/ScatterPlotSpeedTestTemplate_pyside.py b/examples/ScatterPlotSpeedTestTemplate_pyside.py index 690b0990..798ebccd 100644 --- a/examples/ScatterPlotSpeedTestTemplate_pyside.py +++ b/examples/ScatterPlotSpeedTestTemplate_pyside.py @@ -36,7 +36,7 @@ class Ui_Form(object): QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): - Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8)) + Form.setWindowTitle(QtGui.QApplication.translate("Form", "PyQtGraph", None, QtGui.QApplication.UnicodeUTF8)) self.pixelModeCheck.setText(QtGui.QApplication.translate("Form", "pixel mode", None, QtGui.QApplication.UnicodeUTF8)) self.label.setText(QtGui.QApplication.translate("Form", "Size", None, QtGui.QApplication.UnicodeUTF8)) self.randCheck.setText(QtGui.QApplication.translate("Form", "Randomize", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/examples/Symbols.py b/examples/Symbols.py index 417df35e..a0c57f75 100755 --- a/examples/Symbols.py +++ b/examples/Symbols.py @@ -29,6 +29,10 @@ plot.plot([7, 8, 9, 10, 11], pen=(217,83,25), symbolBrush=(217,83,25), symbolPen plot.plot([8, 9, 10, 11, 12], pen=(237,177,32), symbolBrush=(237,177,32), symbolPen='w', symbol='star', symbolSize=14, name="symbol='star'") plot.plot([9, 10, 11, 12, 13], pen=(126,47,142), symbolBrush=(126,47,142), symbolPen='w', symbol='+', symbolSize=14, name="symbol='+'") plot.plot([10, 11, 12, 13, 14], pen=(119,172,48), symbolBrush=(119,172,48), symbolPen='w', symbol='d', symbolSize=14, name="symbol='d'") +plot.plot([11, 12, 13, 14, 15], pen=(253, 216, 53), symbolBrush=(253, 216, 53), symbolPen='w', symbol='arrow_down', symbolSize=22, name="symbol='arrow_down'") +plot.plot([12, 13, 14, 15, 16], pen=(189, 189, 189), symbolBrush=(189, 189, 189), symbolPen='w', symbol='arrow_left', symbolSize=22, name="symbol='arrow_left'") +plot.plot([13, 14, 15, 16, 17], pen=(187, 26, 95), symbolBrush=(187, 26, 95), symbolPen='w', symbol='arrow_up', symbolSize=22, name="symbol='arrow_up'") +plot.plot([14, 15, 16, 17, 18], pen=(248, 187, 208), symbolBrush=(248, 187, 208), symbolPen='w', symbol='arrow_right', symbolSize=22, name="symbol='arrow_right'") plot.setXRange(-2, 4) ## Start Qt event loop unless running in interactive mode or using pyside. diff --git a/examples/VideoSpeedTest.py b/examples/VideoSpeedTest.py index f123ccc3..7131f9d1 100644 --- a/examples/VideoSpeedTest.py +++ b/examples/VideoSpeedTest.py @@ -103,6 +103,7 @@ def mkData(): dt = np.float loc = 1.0 scale = 0.1 + mx = 1.0 if ui.rgbCheck.isChecked(): data = np.random.normal(size=(frames,width,height,3), loc=loc, scale=scale) diff --git a/examples/__main__.py b/examples/__main__.py index ffc38ff7..df390cb9 100644 --- a/examples/__main__.py +++ b/examples/__main__.py @@ -10,6 +10,7 @@ from pyqtgraph.python2_3 import basestring from pyqtgraph.Qt import QtGui, QT_LIB from .utils import buildFileList, path, examples +from .syntax import PythonHighlighter if QT_LIB == 'PySide': @@ -21,6 +22,16 @@ elif QT_LIB == 'PyQt5': else: from .exampleLoaderTemplate_pyqt import Ui_Form +class App(QtGui.QApplication): + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.paletteChanged.connect(self.onPaletteChange) + self.onPaletteChange(self.palette()) + + def onPaletteChange(self, palette): + self.dark_mode = palette.base().color().name().lower() != "#ffffff" + class ExampleLoader(QtGui.QMainWindow): def __init__(self): QtGui.QMainWindow.__init__(self) @@ -33,6 +44,9 @@ class ExampleLoader(QtGui.QMainWindow): self.codeBtn = QtGui.QPushButton('Run Edited Code') self.codeLayout = QtGui.QGridLayout() self.ui.codeView.setLayout(self.codeLayout) + self.hl = PythonHighlighter(self.ui.codeView.document()) + app = QtGui.QApplication.instance() + app.paletteChanged.connect(self.updateTheme) self.codeLayout.addItem(QtGui.QSpacerItem(100,100,QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding), 0, 0) self.codeLayout.addWidget(self.codeBtn, 1, 1) self.codeBtn.hide() @@ -51,6 +65,28 @@ class ExampleLoader(QtGui.QMainWindow): self.ui.codeView.textChanged.connect(self.codeEdited) self.codeBtn.clicked.connect(self.runEditedCode) + def simulate_black_mode(self): + """ + used to simulate MacOS "black mode" on other platforms + intended for debug only, as it manage only the QPlainTextEdit + """ + # first, a dark background + c = QtGui.QColor('#171717') + p = self.ui.codeView.palette() + p.setColor(QtGui.QPalette.Active, QtGui.QPalette.Base, c) + p.setColor(QtGui.QPalette.Inactive, QtGui.QPalette.Base, c) + self.ui.codeView.setPalette(p) + # then, a light font + f = QtGui.QTextCharFormat() + f.setForeground(QtGui.QColor('white')) + self.ui.codeView.setCurrentCharFormat(f) + # finally, override application automatic detection + app = QtGui.QApplication.instance() + app.dark_mode = True + + def updateTheme(self): + self.hl = PythonHighlighter(self.ui.codeView.document()) + def populateTree(self, root, examples): for key, val in examples.items(): item = QtGui.QTreeWidgetItem([key]) @@ -115,7 +151,7 @@ class ExampleLoader(QtGui.QMainWindow): self.loadFile(edited=True) def run(): - app = QtGui.QApplication([]) + app = App([]) loader = ExampleLoader() app.exec_() diff --git a/examples/customPlot.py b/examples/customPlot.py index b523fd17..c5e05f91 100644 --- a/examples/customPlot.py +++ b/examples/customPlot.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """ -This example demonstrates the creation of a plot with a customized -AxisItem and ViewBox. +This example demonstrates the creation of a plot with +DateAxisItem and a customized ViewBox. """ @@ -12,40 +12,6 @@ from pyqtgraph.Qt import QtCore, QtGui import numpy as np import time -class DateAxis(pg.AxisItem): - def tickStrings(self, values, scale, spacing): - strns = [] - rng = max(values)-min(values) - #if rng < 120: - # return pg.AxisItem.tickStrings(self, values, scale, spacing) - if rng < 3600*24: - string = '%H:%M:%S' - label1 = '%b %d -' - label2 = ' %b %d, %Y' - elif rng >= 3600*24 and rng < 3600*24*30: - string = '%d' - label1 = '%b - ' - label2 = '%b, %Y' - elif rng >= 3600*24*30 and rng < 3600*24*30*24: - string = '%b' - label1 = '%Y -' - label2 = ' %Y' - elif rng >=3600*24*30*24: - string = '%Y' - label1 = '' - label2 = '' - for x in values: - try: - strns.append(time.strftime(string, time.localtime(x))) - except ValueError: ## Windows can't handle dates before 1970 - strns.append('') - try: - label = time.strftime(label1, time.localtime(min(values)))+time.strftime(label2, time.localtime(max(values))) - except ValueError: - label = '' - #self.setLabel(text=label) - return strns - class CustomViewBox(pg.ViewBox): def __init__(self, *args, **kwds): pg.ViewBox.__init__(self, *args, **kwds) @@ -65,10 +31,10 @@ class CustomViewBox(pg.ViewBox): app = pg.mkQApp() -axis = DateAxis(orientation='bottom') +axis = pg.DateAxisItem(orientation='bottom') vb = CustomViewBox() -pw = pg.PlotWidget(viewBox=vb, axisItems={'bottom': axis}, enableMenu=False, title="PlotItem with custom axis and ViewBox
Menu disabled, mouse behavior changed: left-drag to zoom, right-click to reset zoom") +pw = pg.PlotWidget(viewBox=vb, axisItems={'bottom': axis}, enableMenu=False, title="PlotItem with DateAxisItem and custom ViewBox
Menu disabled, mouse behavior changed: left-drag to zoom, right-click to reset zoom") dates = np.arange(8) * (3600*24*356) pw.plot(x=dates, y=[1,6,2,4,3,5,6,8], symbol='o') pw.show() diff --git a/examples/designerExample.ui b/examples/designerExample.ui index 41d06089..0f1695af 100644 --- a/examples/designerExample.ui +++ b/examples/designerExample.ui @@ -11,7 +11,7 @@ - Form + PyQtGraph diff --git a/examples/exampleLoaderTemplate.ui b/examples/exampleLoaderTemplate.ui index f12459ba..c26dbddf 100644 --- a/examples/exampleLoaderTemplate.ui +++ b/examples/exampleLoaderTemplate.ui @@ -11,7 +11,7 @@ - Form + PyQtGraph diff --git a/examples/exampleLoaderTemplate_pyqt.py b/examples/exampleLoaderTemplate_pyqt.py index 732a3ea1..f5521a8f 100644 --- a/examples/exampleLoaderTemplate_pyqt.py +++ b/examples/exampleLoaderTemplate_pyqt.py @@ -89,7 +89,7 @@ class Ui_Form(object): QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): - Form.setWindowTitle(_translate("Form", "Form", None)) + Form.setWindowTitle(_translate("Form", "PyQtGraph", None)) self.graphicsSystemCombo.setItemText(0, _translate("Form", "default", None)) self.graphicsSystemCombo.setItemText(1, _translate("Form", "native", None)) self.graphicsSystemCombo.setItemText(2, _translate("Form", "raster", None)) diff --git a/examples/exampleLoaderTemplate_pyqt5.py b/examples/exampleLoaderTemplate_pyqt5.py index 14ded4d9..090447c2 100644 --- a/examples/exampleLoaderTemplate_pyqt5.py +++ b/examples/exampleLoaderTemplate_pyqt5.py @@ -78,7 +78,7 @@ class Ui_Form(object): def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate - Form.setWindowTitle(_translate("Form", "Form")) + Form.setWindowTitle(_translate("Form", "PyQtGraph")) self.graphicsSystemCombo.setItemText(0, _translate("Form", "default")) self.graphicsSystemCombo.setItemText(1, _translate("Form", "native")) self.graphicsSystemCombo.setItemText(2, _translate("Form", "raster")) diff --git a/examples/exampleLoaderTemplate_pyside.py b/examples/exampleLoaderTemplate_pyside.py index 62296827..d1705d23 100644 --- a/examples/exampleLoaderTemplate_pyside.py +++ b/examples/exampleLoaderTemplate_pyside.py @@ -78,7 +78,7 @@ class Ui_Form(object): QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): - Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8)) + Form.setWindowTitle(QtGui.QApplication.translate("Form", "PyQtGraph", None, QtGui.QApplication.UnicodeUTF8)) self.graphicsSystemCombo.setItemText(0, QtGui.QApplication.translate("Form", "default", None, QtGui.QApplication.UnicodeUTF8)) self.graphicsSystemCombo.setItemText(1, QtGui.QApplication.translate("Form", "native", None, QtGui.QApplication.UnicodeUTF8)) self.graphicsSystemCombo.setItemText(2, QtGui.QApplication.translate("Form", "raster", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/examples/histogram.py b/examples/histogram.py index a25f0947..85fbe3f0 100644 --- a/examples/histogram.py +++ b/examples/histogram.py @@ -22,7 +22,7 @@ y,x = np.histogram(vals, bins=np.linspace(-3, 8, 40)) ## Using stepMode=True causes the plot to draw two lines for each sample. ## notice that len(x) == len(y)+1 -plt1.plot(x, y, stepMode=True, fillLevel=0, brush=(0,0,255,150)) +plt1.plot(x, y, stepMode=True, fillLevel=0, fillOutline=True, brush=(0,0,255,150)) ## Now draw all points as a nicely-spaced scatter plot y = pg.pseudoScatter(vals, spacing=0.15) diff --git a/examples/parametertree.py b/examples/parametertree.py index 8d8a7352..acfeac4d 100644 --- a/examples/parametertree.py +++ b/examples/parametertree.py @@ -96,6 +96,16 @@ params = [ {'name': 'Renamable', 'type': 'float', 'value': 1.2e6, 'siPrefix': True, 'suffix': 'Hz', 'renamable': True}, {'name': 'Removable', 'type': 'float', 'value': 1.2e6, 'siPrefix': True, 'suffix': 'Hz', 'removable': True}, ]}, + {'name': 'Custom context menu', 'type': 'group', 'children': [ + {'name': 'List contextMenu', 'type': 'float', 'value': 0, 'context': [ + 'menu1', + 'menu2' + ]}, + {'name': 'Dict contextMenu', 'type': 'float', 'value': 0, 'context': { + 'changeName': 'Title', + 'internal': 'What the user sees', + }}, + ]}, ComplexParameter(name='Custom parameter group (reciprocal values)'), ScalableGroup(name="Expandable Parameter Group", children=[ {'name': 'ScalableParam 1', 'type': 'str', 'value': "default param 1"}, diff --git a/examples/syntax.py b/examples/syntax.py new file mode 100644 index 00000000..95417827 --- /dev/null +++ b/examples/syntax.py @@ -0,0 +1,250 @@ +# based on https://github.com/art1415926535/PyQt5-syntax-highlighting + +from pyqtgraph.Qt import QtCore, QtGui + +QRegExp = QtCore.QRegExp + +QFont = QtGui.QFont +QColor = QtGui.QColor +QTextCharFormat = QtGui.QTextCharFormat +QSyntaxHighlighter = QtGui.QSyntaxHighlighter + + +def format(color, style=''): + """ + Return a QTextCharFormat with the given attributes. + """ + _color = QColor() + if type(color) is not str: + _color.setRgb(color[0], color[1], color[2]) + else: + _color.setNamedColor(color) + + _format = QTextCharFormat() + _format.setForeground(_color) + if 'bold' in style: + _format.setFontWeight(QFont.Bold) + if 'italic' in style: + _format.setFontItalic(True) + + return _format + + +class LightThemeColors: + + Red = "#B71C1C" + Pink = "#FCE4EC" + Purple = "#4A148C" + DeepPurple = "#311B92" + Indigo = "#1A237E" + Blue = "#0D47A1" + LightBlue = "#01579B" + Cyan = "#006064" + Teal = "#004D40" + Green = "#1B5E20" + LightGreen = "#33691E" + Lime = "#827717" + Yellow = "#F57F17" + Amber = "#FF6F00" + Orange = "#E65100" + DeepOrange = "#BF360C" + Brown = "#3E2723" + Grey = "#212121" + BlueGrey = "#263238" + + +class DarkThemeColors: + + Red = "#F44336" + Pink = "#F48FB1" + Purple = "#CE93D8" + DeepPurple = "#B39DDB" + Indigo = "#9FA8DA" + Blue = "#90CAF9" + LightBlue = "#81D4FA" + Cyan = "#80DEEA" + Teal = "#80CBC4" + Green = "#A5D6A7" + LightGreen = "#C5E1A5" + Lime = "#E6EE9C" + Yellow = "#FFF59D" + Amber = "#FFE082" + Orange = "#FFCC80" + DeepOrange = "#FFAB91" + Brown = "#BCAAA4" + Grey = "#EEEEEE" + BlueGrey = "#B0BEC5" + + +LIGHT_STYLES = { + 'keyword': format(LightThemeColors.Blue, 'bold'), + 'operator': format(LightThemeColors.Red, 'bold'), + 'brace': format(LightThemeColors.Purple), + 'defclass': format(LightThemeColors.Indigo, 'bold'), + 'string': format(LightThemeColors.Amber), + 'string2': format(LightThemeColors.DeepPurple), + 'comment': format(LightThemeColors.Green, 'italic'), + 'self': format(LightThemeColors.Blue, 'bold'), + 'numbers': format(LightThemeColors.Teal), +} + + +DARK_STYLES = { + 'keyword': format(DarkThemeColors.Blue, 'bold'), + 'operator': format(DarkThemeColors.Red, 'bold'), + 'brace': format(DarkThemeColors.Purple), + 'defclass': format(DarkThemeColors.Indigo, 'bold'), + 'string': format(DarkThemeColors.Amber), + 'string2': format(DarkThemeColors.DeepPurple), + 'comment': format(DarkThemeColors.Green, 'italic'), + 'self': format(DarkThemeColors.Blue, 'bold'), + 'numbers': format(DarkThemeColors.Teal), +} + + +class PythonHighlighter(QSyntaxHighlighter): + """Syntax highlighter for the Python language. + """ + # Python keywords + keywords = [ + 'and', 'assert', 'break', 'class', 'continue', 'def', + 'del', 'elif', 'else', 'except', 'exec', 'finally', + 'for', 'from', 'global', 'if', 'import', 'in', + 'is', 'lambda', 'not', 'or', 'pass', 'print', + 'raise', 'return', 'try', 'while', 'yield', + 'None', 'True', 'False', 'async', 'await', + ] + + # Python operators + operators = [ + '=', + # Comparison + '==', '!=', '<', '<=', '>', '>=', + # Arithmetic + '\+', '-', '\*', '/', '//', '\%', '\*\*', + # In-place + '\+=', '-=', '\*=', '/=', '\%=', + # Bitwise + '\^', '\|', '\&', '\~', '>>', '<<', + ] + + # Python braces + braces = [ + '\{', '\}', '\(', '\)', '\[', '\]', + ] + + def __init__(self, document): + QSyntaxHighlighter.__init__(self, document) + + # Multi-line strings (expression, flag, style) + # FIXME: The triple-quotes in these two lines will mess up the + # syntax highlighting from this point onward + self.tri_single = (QRegExp("'''"), 1, 'string2') + self.tri_double = (QRegExp('"""'), 2, 'string2') + + rules = [] + + # Keyword, operator, and brace rules + rules += [(r'\b%s\b' % w, 0, 'keyword') + for w in PythonHighlighter.keywords] + rules += [(r'%s' % o, 0, 'operator') + for o in PythonHighlighter.operators] + rules += [(r'%s' % b, 0, 'brace') + for b in PythonHighlighter.braces] + + # All other rules + rules += [ + + # 'self' + (r'\bself\b', 0, 'self'), + + # 'def' followed by an identifier + (r'\bdef\b\s*(\w+)', 1, 'defclass'), + # 'class' followed by an identifier + (r'\bclass\b\s*(\w+)', 1, 'defclass'), + + # Numeric literals + (r'\b[+-]?[0-9]+[lL]?\b', 0, 'numbers'), + (r'\b[+-]?0[xX][0-9A-Fa-f]+[lL]?\b', 0, 'numbers'), + (r'\b[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?\b', 0, 'numbers'), + + # Double-quoted string, possibly containing escape sequences + (r'"[^"\\]*(\\.[^"\\]*)*"', 0, 'string'), + # Single-quoted string, possibly containing escape sequences + (r"'[^'\\]*(\\.[^'\\]*)*'", 0, 'string'), + + # From '#' until a newline + (r'#[^\n]*', 0, 'comment'), + + ] + + # Build a QRegExp for each pattern + self.rules = [(QRegExp(pat), index, fmt) + for (pat, index, fmt) in rules] + + @property + def styles(self): + app = QtGui.QApplication.instance() + return DARK_STYLES if app.dark_mode else LIGHT_STYLES + + def highlightBlock(self, text): + """Apply syntax highlighting to the given block of text. + """ + # Do other syntax formatting + for expression, nth, format in self.rules: + index = expression.indexIn(text, 0) + format = self.styles[format] + + while index >= 0: + # We actually want the index of the nth match + index = expression.pos(nth) + length = len(expression.cap(nth)) + self.setFormat(index, length, format) + index = expression.indexIn(text, index + length) + + self.setCurrentBlockState(0) + + # Do multi-line strings + in_multiline = self.match_multiline(text, *self.tri_single) + if not in_multiline: + in_multiline = self.match_multiline(text, *self.tri_double) + + def match_multiline(self, text, delimiter, in_state, style): + """Do highlighting of multi-line strings. ``delimiter`` should be a + ``QRegExp`` for triple-single-quotes or triple-double-quotes, and + ``in_state`` should be a unique integer to represent the corresponding + state changes when inside those strings. Returns True if we're still + inside a multi-line string when this function is finished. + """ + # If inside triple-single quotes, start at 0 + if self.previousBlockState() == in_state: + start = 0 + add = 0 + # Otherwise, look for the delimiter on this line + else: + start = delimiter.indexIn(text) + # Move past this match + add = delimiter.matchedLength() + + # As long as there's a delimiter match on this line... + while start >= 0: + # Look for the ending delimiter + end = delimiter.indexIn(text, start + add) + # Ending delimiter on this line? + if end >= add: + length = end - start + add + delimiter.matchedLength() + self.setCurrentBlockState(0) + # No; multi-line string + else: + self.setCurrentBlockState(in_state) + length = len(text) - start + add + # Apply formatting + self.setFormat(start, length, self.styles[style]) + # Look for the next match + start = delimiter.indexIn(text, start + length) + + # Return True if still inside a multi-line string, False otherwise + if self.currentBlockState() == in_state: + return True + else: + return False diff --git a/examples/test_examples.py b/examples/test_examples.py index 0856b4ff..a9fecca2 100644 --- a/examples/test_examples.py +++ b/examples/test_examples.py @@ -1,11 +1,11 @@ +# -*- coding: utf-8 -*- from __future__ import print_function, division, absolute_import -from pyqtgraph import Qt from . import utils from collections import namedtuple +from pyqtgraph import Qt import errno import importlib import itertools -import pkgutil import pytest import os, sys import subprocess @@ -41,7 +41,12 @@ if os.getenv('TRAVIS') is not None: files = sorted(set(utils.buildFileList(utils.examples))) -frontends = {Qt.PYQT4: False, Qt.PYQT5: False, Qt.PYSIDE: False, Qt.PYSIDE2: False} +frontends = { + Qt.PYQT4: False, + Qt.PYQT5: False, + Qt.PYSIDE: False, + Qt.PYSIDE2: False +} # sort out which of the front ends are available for frontend in frontends.keys(): try: @@ -50,41 +55,142 @@ for frontend in frontends.keys(): except ImportError: pass -installedFrontends = sorted([frontend for frontend, isPresent in frontends.items() if isPresent]) +installedFrontends = sorted([ + frontend for frontend, isPresent in frontends.items() if isPresent +]) exceptionCondition = namedtuple("exceptionCondition", ["condition", "reason"]) -conditionalExampleTests = { - "hdf5.py": exceptionCondition(False, reason="Example requires user interaction and is not suitable for testing"), - "RemoteSpeedTest.py": exceptionCondition(False, reason="Test is being problematic on CI machines"), - "optics_demos.py": exceptionCondition(not frontends[Qt.PYSIDE], reason="Test fails due to PySide bug: https://bugreports.qt.io/browse/PYSIDE-671") +conditionalExamples = { + "hdf5.py": exceptionCondition( + False, + reason="Example requires user interaction" + ), + "RemoteSpeedTest.py": exceptionCondition( + False, + reason="Test is being problematic on CI machines" + ), + "optics_demos.py": exceptionCondition( + not frontends[Qt.PYSIDE], + reason=( + "Test fails due to PySide bug: ", + "https://bugreports.qt.io/browse/PYSIDE-671" + ) + ), + 'GLVolumeItem.py': exceptionCondition( + not(sys.platform == "darwin" and + sys.version_info[0] == 2 and + (frontends[Qt.PYQT4] or frontends[Qt.PYSIDE])), + reason=( + "glClear does not work on macOS + Python2.7 + Qt4: ", + "https://github.com/pyqtgraph/pyqtgraph/issues/939" + ) + ), + 'GLIsosurface.py': exceptionCondition( + not(sys.platform == "darwin" and + sys.version_info[0] == 2 and + (frontends[Qt.PYQT4] or frontends[Qt.PYSIDE])), + reason=( + "glClear does not work on macOS + Python2.7 + Qt4: ", + "https://github.com/pyqtgraph/pyqtgraph/issues/939" + ) + ), + 'GLSurfacePlot.py': exceptionCondition( + not(sys.platform == "darwin" and + sys.version_info[0] == 2 and + (frontends[Qt.PYQT4] or frontends[Qt.PYSIDE])), + reason=( + "glClear does not work on macOS + Python2.7 + Qt4: ", + "https://github.com/pyqtgraph/pyqtgraph/issues/939" + ) + ), + 'GLScatterPlotItem.py': exceptionCondition( + not(sys.platform == "darwin" and + sys.version_info[0] == 2 and + (frontends[Qt.PYQT4] or frontends[Qt.PYSIDE])), + reason=( + "glClear does not work on macOS + Python2.7 + Qt4: ", + "https://github.com/pyqtgraph/pyqtgraph/issues/939" + ) + ), + 'GLshaders.py': exceptionCondition( + not(sys.platform == "darwin" and + sys.version_info[0] == 2 and + (frontends[Qt.PYQT4] or frontends[Qt.PYSIDE])), + reason=( + "glClear does not work on macOS + Python2.7 + Qt4: ", + "https://github.com/pyqtgraph/pyqtgraph/issues/939" + ) + ), + 'GLLinePlotItem.py': exceptionCondition( + not(sys.platform == "darwin" and + sys.version_info[0] == 2 and + (frontends[Qt.PYQT4] or frontends[Qt.PYSIDE])), + reason=( + "glClear does not work on macOS + Python2.7 + Qt4: ", + "https://github.com/pyqtgraph/pyqtgraph/issues/939" + ) + ), + 'GLMeshItem.py': exceptionCondition( + not(sys.platform == "darwin" and + sys.version_info[0] == 2 and + (frontends[Qt.PYQT4] or frontends[Qt.PYSIDE])), + reason=( + "glClear does not work on macOS + Python2.7 + Qt4: ", + "https://github.com/pyqtgraph/pyqtgraph/issues/939" + ) + ), + 'GLImageItem.py': exceptionCondition( + not(sys.platform == "darwin" and + sys.version_info[0] == 2 and + (frontends[Qt.PYQT4] or frontends[Qt.PYSIDE])), + reason=( + "glClear does not work on macOS + Python2.7 + Qt4: ", + "https://github.com/pyqtgraph/pyqtgraph/issues/939" + ) + ) } - +@pytest.mark.skipif( + Qt.QT_LIB == "PySide2" + and tuple(map(int, Qt.PySide2.__version__.split("."))) >= (5, 14) + and tuple(map(int, Qt.PySide2.__version__.split("."))) < (5, 14, 2, 2), + reason="new PySide2 doesn't have loadUi functionality" +) @pytest.mark.parametrize( - "frontend, f", - [ - pytest.param( - frontend, + "frontend, f", + [ + pytest.param( + frontend, f, - marks=pytest.mark.skipif(conditionalExampleTests[f[1]].condition is False, - reason=conditionalExampleTests[f[1]].reason) if f[1] in conditionalExampleTests.keys() else (), - ) - for frontend, f, in itertools.product(installedFrontends, files) - ], - ids = [" {} - {} ".format(f[1], frontend) for frontend, f in itertools.product(installedFrontends, files)] + marks=pytest.mark.skipif( + conditionalExamples[f[1]].condition is False, + reason=conditionalExamples[f[1]].reason + ) if f[1] in conditionalExamples.keys() else (), + ) + for frontend, f, in itertools.product(installedFrontends, files) + ], + ids = [ + " {} - {} ".format(f[1], frontend) + for frontend, f in itertools.product( + installedFrontends, + files + ) + ] ) def testExamples(frontend, f, graphicsSystem=None): # runExampleFile(f[0], f[1], sys.executable, frontend) name, file = f global path - fn = os.path.join(path,file) + fn = os.path.join(path, file) os.chdir(path) sys.stdout.write("{} ".format(name)) sys.stdout.flush() import1 = "import %s" % frontend if frontend != '' else '' import2 = os.path.splitext(os.path.split(fn)[1])[0] - graphicsSystem = '' if graphicsSystem is None else "pg.QtGui.QApplication.setGraphicsSystem('%s')" % graphicsSystem + graphicsSystem = ( + '' if graphicsSystem is None else "pg.QtGui.QApplication.setGraphicsSystem('%s')" % graphicsSystem + ) code = """ try: %s @@ -116,7 +222,7 @@ except: stderr=subprocess.PIPE, stdout=subprocess.PIPE) process.stdin.write(code.encode('UTF-8')) - process.stdin.close() ##? + process.stdin.close() output = '' fail = False while True: @@ -139,10 +245,14 @@ except: process.kill() #res = process.communicate() res = (process.stdout.read(), process.stderr.read()) - if fail or 'exception' in res[1].decode().lower() or 'error' in res[1].decode().lower(): + if (fail or + 'exception' in res[1].decode().lower() or + 'error' in res[1].decode().lower()): print(res[0].decode()) print(res[1].decode()) - pytest.fail("{}\n{}\nFailed {} Example Test Located in {} ".format(res[0].decode(), res[1].decode(), name, file), pytrace=False) + pytest.fail("{}\n{}\nFailed {} Example Test Located in {} " + .format(res[0].decode(), res[1].decode(), name, file), + pytrace=False) if __name__ == "__main__": pytest.cmdline.main() diff --git a/examples/utils.py b/examples/utils.py index 494b686b..041d17d7 100644 --- a/examples/utils.py +++ b/examples/utils.py @@ -14,6 +14,7 @@ examples = OrderedDict([ ('Crosshair / Mouse interaction', 'crosshair.py'), ('Data Slicing', 'DataSlicing.py'), ('Plot Customization', 'customPlot.py'), + ('Timestamps on x axis', 'DateAxisItem.py'), ('Image Analysis', 'imageAnalysis.py'), ('ViewBox Features', 'ViewBoxFeatures.py'), ('Dock widgets', 'dockarea.py'), diff --git a/pyqtgraph/GraphicsScene/GraphicsScene.py b/pyqtgraph/GraphicsScene/GraphicsScene.py index 01b6b808..b67e44ef 100644 --- a/pyqtgraph/GraphicsScene/GraphicsScene.py +++ b/pyqtgraph/GraphicsScene/GraphicsScene.py @@ -1,6 +1,8 @@ +# -*- coding: utf-8 -*- import weakref +import warnings + from ..Qt import QtCore, QtGui -from ..python2_3 import sortList, cmp from ..Point import Point from .. import functions as fn from .. import ptime as ptime @@ -88,15 +90,11 @@ class GraphicsScene(QtGui.QGraphicsScene): @classmethod def registerObject(cls, obj): - """ - Workaround for PyQt bug in qgraphicsscene.items() - All subclasses of QGraphicsObject must register themselves with this function. - (otherwise, mouse interaction with those objects will likely fail) - """ - if HAVE_SIP and isinstance(obj, sip.wrapper): - cls._addressCache[sip.unwrapinstance(sip.cast(obj, QtGui.QGraphicsItem))] = obj - - + warnings.warn( + "'registerObject' is deprecated and does nothing.", + DeprecationWarning, stacklevel=2 + ) + def __init__(self, clickRadius=2, moveDistance=5, parent=None): QtGui.QGraphicsScene.__init__(self, parent) self.setClickRadius(clickRadius) @@ -183,12 +181,14 @@ class GraphicsScene(QtGui.QGraphicsScene): if int(ev.buttons() & btn) == 0: continue if int(btn) not in self.dragButtons: ## see if we've dragged far enough yet - cev = [e for e in self.clickEvents if int(e.button()) == int(btn)][0] - dist = Point(ev.scenePos() - cev.scenePos()).length() - if dist == 0 or (dist < self._moveDistance and now - cev.time() < self.minDragTime): - continue - init = init or (len(self.dragButtons) == 0) ## If this is the first button to be dragged, then init=True - self.dragButtons.append(int(btn)) + cev = [e for e in self.clickEvents if int(e.button()) == int(btn)] + if cev: + cev = cev[0] + dist = Point(ev.scenePos() - cev.scenePos()).length() + if dist == 0 or (dist < self._moveDistance and now - cev.time() < self.minDragTime): + continue + init = init or (len(self.dragButtons) == 0) ## If this is the first button to be dragged, then init=True + self.dragButtons.append(int(btn)) ## If we have dragged buttons, deliver a drag event if len(self.dragButtons) > 0: @@ -208,10 +208,11 @@ class GraphicsScene(QtGui.QGraphicsScene): self.dragButtons.remove(ev.button()) else: cev = [e for e in self.clickEvents if int(e.button()) == int(ev.button())] - if self.sendClickEvent(cev[0]): - #print "sent click event" - ev.accept() - self.clickEvents.remove(cev[0]) + if cev: + if self.sendClickEvent(cev[0]): + #print "sent click event" + ev.accept() + self.clickEvents.remove(cev[0]) if int(ev.buttons()) == 0: self.dragItem = None @@ -365,46 +366,15 @@ class GraphicsScene(QtGui.QGraphicsScene): return ev.isAccepted() def items(self, *args): - #print 'args:', args items = QtGui.QGraphicsScene.items(self, *args) - ## PyQt bug: items() returns a list of QGraphicsItem instances. If the item is subclassed from QGraphicsObject, - ## then the object returned will be different than the actual item that was originally added to the scene - items2 = list(map(self.translateGraphicsItem, items)) - #if HAVE_SIP and isinstance(self, sip.wrapper): - #items2 = [] - #for i in items: - #addr = sip.unwrapinstance(sip.cast(i, QtGui.QGraphicsItem)) - #i2 = GraphicsScene._addressCache.get(addr, i) - ##print i, "==>", i2 - #items2.append(i2) - #print 'items:', items - return items2 + return self.translateGraphicsItems(items) def selectedItems(self, *args): items = QtGui.QGraphicsScene.selectedItems(self, *args) - ## PyQt bug: items() returns a list of QGraphicsItem instances. If the item is subclassed from QGraphicsObject, - ## then the object returned will be different than the actual item that was originally added to the scene - #if HAVE_SIP and isinstance(self, sip.wrapper): - #items2 = [] - #for i in items: - #addr = sip.unwrapinstance(sip.cast(i, QtGui.QGraphicsItem)) - #i2 = GraphicsScene._addressCache.get(addr, i) - ##print i, "==>", i2 - #items2.append(i2) - items2 = list(map(self.translateGraphicsItem, items)) - - #print 'items:', items - return items2 + return self.translateGraphicsItems(items) def itemAt(self, *args): item = QtGui.QGraphicsScene.itemAt(self, *args) - - ## PyQt bug: items() returns a list of QGraphicsItem instances. If the item is subclassed from QGraphicsObject, - ## then the object returned will be different than the actual item that was originally added to the scene - #if HAVE_SIP and isinstance(self, sip.wrapper): - #addr = sip.unwrapinstance(sip.cast(item, QtGui.QGraphicsItem)) - #item = GraphicsScene._addressCache.get(addr, item) - #return item return self.translateGraphicsItem(item) def itemsNearEvent(self, event, selMode=QtCore.Qt.IntersectsItemShape, sortOrder=QtCore.Qt.DescendingOrder, hoverable=False): @@ -451,7 +421,7 @@ class GraphicsScene(QtGui.QGraphicsScene): return 0 return item.zValue() + absZValue(item.parentItem()) - sortList(items2, lambda a,b: cmp(absZValue(b), absZValue(a))) + items2.sort(key=absZValue, reverse=True) return items2 @@ -551,15 +521,16 @@ class GraphicsScene(QtGui.QGraphicsScene): @staticmethod def translateGraphicsItem(item): - ## for fixing pyqt bugs where the wrong item is returned + # This function is intended as a workaround for a problem with older + # versions of PyQt (< 4.9?), where methods returning 'QGraphicsItem *' + # lose the type of the QGraphicsObject subclasses and instead return + # generic QGraphicsItem wrappers. if HAVE_SIP and isinstance(item, sip.wrapper): - addr = sip.unwrapinstance(sip.cast(item, QtGui.QGraphicsItem)) - item = GraphicsScene._addressCache.get(addr, item) + obj = item.toGraphicsObject() + if obj is not None: + item = obj return item @staticmethod def translateGraphicsItems(items): return list(map(GraphicsScene.translateGraphicsItem, items)) - - - diff --git a/pyqtgraph/GraphicsScene/exportDialog.py b/pyqtgraph/GraphicsScene/exportDialog.py index 8085c5bf..61f2233d 100644 --- a/pyqtgraph/GraphicsScene/exportDialog.py +++ b/pyqtgraph/GraphicsScene/exportDialog.py @@ -22,7 +22,7 @@ class ExportDialog(QtGui.QWidget): self.shown = False self.currentExporter = None self.scene = scene - + self.selectBox = QtGui.QGraphicsRectItem() self.selectBox.setPen(fn.mkPen('y', width=3, style=QtCore.Qt.DashLine)) self.selectBox.hide() @@ -121,7 +121,9 @@ class ExportDialog(QtGui.QWidget): return expClass = self.exporterClasses[str(item.text())] exp = expClass(item=self.ui.itemTree.currentItem().gitem) + params = exp.parameters() + if params is None: self.ui.paramTree.clear() else: diff --git a/pyqtgraph/PlotData.py b/pyqtgraph/PlotData.py index e5faadda..f2760508 100644 --- a/pyqtgraph/PlotData.py +++ b/pyqtgraph/PlotData.py @@ -1,3 +1,4 @@ +import numpy as np class PlotData(object): @@ -50,7 +51,3 @@ class PlotData(object): mn = np.min(self[field]) self.minVals[field] = mn return mn - - - - \ No newline at end of file diff --git a/pyqtgraph/Point.py b/pyqtgraph/Point.py index 3fb43cac..3b4dacf3 100644 --- a/pyqtgraph/Point.py +++ b/pyqtgraph/Point.py @@ -2,7 +2,7 @@ """ Point.py - Extension of QPointF which adds a few missing methods. Copyright 2010 Luke Campagnola -Distributed under MIT/X11 license. See license.txt for more infomation. +Distributed under MIT/X11 license. See license.txt for more information. """ from .Qt import QtCore diff --git a/pyqtgraph/Qt.py b/pyqtgraph/Qt.py index 0941c3c7..6035d7ac 100644 --- a/pyqtgraph/Qt.py +++ b/pyqtgraph/Qt.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """ This module exists to smooth out some of the differences between PySide and PyQt4: @@ -9,7 +10,7 @@ This module exists to smooth out some of the differences between PySide and PyQt """ -import os, sys, re, time +import os, sys, re, time, subprocess, warnings from .python2_3 import asUnicode @@ -104,24 +105,38 @@ def _loadUiType(uiFile): if QT_LIB == "PYSIDE": import pysideuic else: - import pyside2uic as pysideuic - import xml.etree.ElementTree as xml + try: + import pyside2uic as pysideuic + except ImportError: + # later vserions of pyside2 have dropped pysideuic; use the uic binary instead. + pysideuic = None + # get class names from ui file + import xml.etree.ElementTree as xml parsed = xml.parse(uiFile) widget_class = parsed.find('widget').get('class') form_class = parsed.find('class').text - - with open(uiFile, 'r') as f: + + # convert ui file to python code + if pysideuic is None: + pyside2version = tuple(map(int, PySide2.__version__.split("."))) + if pyside2version >= (5, 14) and pyside2version < (5, 14, 2, 2): + warnings.warn('For UI compilation, it is recommended to upgrade to PySide >= 5.15') + uipy = subprocess.check_output(['pyside2-uic', uiFile]) + else: o = _StringIO() - frame = {} + with open(uiFile, 'r') as f: + pysideuic.compileUi(f, o, indent=0) + uipy = o.getvalue() - pysideuic.compileUi(f, o, indent=0) - pyc = compile(o.getvalue(), '', 'exec') - exec(pyc, frame) + # exceute python code + pyc = compile(uipy, '', 'exec') + frame = {} + exec(pyc, frame) - #Fetch the base_class and form class based on their type in the xml from designer - form_class = frame['Ui_%s'%form_class] - base_class = eval('QtGui.%s'%widget_class) + # fetch the base_class and form class based on their type in the xml from designer + form_class = frame['Ui_%s'%form_class] + base_class = eval('QtGui.%s'%widget_class) return form_class, base_class @@ -329,9 +344,19 @@ if m is not None and list(map(int, m.groups())) < versionReq: QAPP = None -def mkQApp(): - global QAPP +def mkQApp(name=None): + """ + Creates new QApplication or returns current instance if existing. + + ============== ======================================================== + **Arguments:** + name (str) Application name, passed to Qt + ============== ======================================================== + """ + global QAPP QAPP = QtGui.QApplication.instance() if QAPP is None: - QAPP = QtGui.QApplication([]) + QAPP = QtGui.QApplication(sys.argv or ["pyqtgraph"]) + if name is not None: + QAPP.setApplicationName(name) return QAPP diff --git a/pyqtgraph/SignalProxy.py b/pyqtgraph/SignalProxy.py index 7463dfc3..46b44887 100644 --- a/pyqtgraph/SignalProxy.py +++ b/pyqtgraph/SignalProxy.py @@ -81,7 +81,7 @@ class SignalProxy(QtCore.QObject): except: pass try: - self.sigDelayed.disconnect(self.slot()) + self.sigDelayed.disconnect(self.slot) except: pass diff --git a/pyqtgraph/Vector.py b/pyqtgraph/Vector.py index 0c980a61..f2166c45 100644 --- a/pyqtgraph/Vector.py +++ b/pyqtgraph/Vector.py @@ -2,7 +2,7 @@ """ Vector.py - Extension of QVector3D which adds a few missing methods. Copyright 2010 Luke Campagnola -Distributed under MIT/X11 license. See license.txt for more infomation. +Distributed under MIT/X11 license. See license.txt for more information. """ from .Qt import QtGui, QtCore, QT_LIB diff --git a/pyqtgraph/WidgetGroup.py b/pyqtgraph/WidgetGroup.py index 09c30854..2792aa98 100644 --- a/pyqtgraph/WidgetGroup.py +++ b/pyqtgraph/WidgetGroup.py @@ -2,7 +2,7 @@ """ WidgetGroup.py - WidgetGroup class for easily managing lots of Qt widgets Copyright 2010 Luke Campagnola -Distributed under MIT/X11 license. See license.txt for more infomation. +Distributed under MIT/X11 license. See license.txt for more information. This class addresses the problem of having to save and restore the state of a large group of widgets. diff --git a/pyqtgraph/__init__.py b/pyqtgraph/__init__.py index b1aa98aa..bc36e891 100644 --- a/pyqtgraph/__init__.py +++ b/pyqtgraph/__init__.py @@ -4,7 +4,7 @@ PyQtGraph - Scientific Graphics and GUI Library for Python www.pyqtgraph.org """ -__version__ = '0.11.0.dev0' +__version__ = '0.11.0' ### import all the goodies and add some helper functions for easy CLI use @@ -29,9 +29,6 @@ if sys.version_info[0] < 2 or (sys.version_info[0] == 2 and sys.version_info[1] ## helpers for 2/3 compatibility from . import python2_3 -## install workarounds for numpy bugs -from . import numpy_fix - ## in general openGL is poorly supported with Qt+GraphicsView. ## we only enable it where the performance benefit is critical. ## Note this only applies to 2D graphics; 3D graphics always use OpenGL. @@ -67,7 +64,6 @@ CONFIG_OPTIONS = { def setConfigOption(opt, value): - global CONFIG_OPTIONS if opt not in CONFIG_OPTIONS: raise KeyError('Unknown configuration option "%s"' % opt) if opt == 'imageAxisOrder' and value not in ('row-major', 'col-major'): @@ -99,7 +95,8 @@ def systemInfo(): if __version__ is None: ## this code was probably checked out from bzr; look up the last-revision file lastRevFile = os.path.join(os.path.dirname(__file__), '..', '.bzr', 'branch', 'last-revision') if os.path.exists(lastRevFile): - rev = open(lastRevFile, 'r').read().strip() + with open(lastRevFile, 'r') as fd: + rev = fd.read().strip() print("pyqtgraph: %s; %s" % (__version__, rev)) print("config:") @@ -222,6 +219,7 @@ from .graphicsItems.ViewBox import * from .graphicsItems.ArrowItem import * from .graphicsItems.ImageItem import * from .graphicsItems.AxisItem import * +from .graphicsItems.DateAxisItem import * from .graphicsItems.LabelItem import * from .graphicsItems.CurvePoint import * from .graphicsItems.GraphicsWidgetAnchor import * @@ -264,6 +262,7 @@ from .widgets.LayoutWidget import * from .widgets.TableWidget import * from .widgets.ProgressDialog import * from .widgets.GroupBox import GroupBox +from .widgets.RemoteGraphicsView import RemoteGraphicsView from .imageview import * from .WidgetGroup import * @@ -414,12 +413,20 @@ def plot(*args, **kargs): dataArgs[k] = kargs[k] w = PlotWindow(**pwArgs) + w.sigClosed.connect(_plotWindowClosed) if len(args) > 0 or len(dataArgs) > 0: w.plot(*args, **dataArgs) plots.append(w) w.show() return w - + +def _plotWindowClosed(w): + w.close() + try: + plots.remove(w) + except ValueError: + pass + def image(*args, **kargs): """ Create and return an :class:`ImageWindow ` @@ -430,11 +437,19 @@ def image(*args, **kargs): """ mkQApp() w = ImageWindow(*args, **kargs) + w.sigClosed.connect(_imageWindowClosed) images.append(w) w.show() return w show = image ## for backward compatibility +def _imageWindowClosed(w): + w.close() + try: + images.remove(w) + except ValueError: + pass + def dbg(*args, **kwds): """ Create a console window and begin watching for exceptions. diff --git a/pyqtgraph/canvas/Canvas.py b/pyqtgraph/canvas/Canvas.py index 72d70d7e..2ec13b19 100644 --- a/pyqtgraph/canvas/Canvas.py +++ b/pyqtgraph/canvas/Canvas.py @@ -1,8 +1,4 @@ # -*- coding: utf-8 -*- -if __name__ == '__main__': - import sys, os - md = os.path.dirname(os.path.abspath(__file__)) - sys.path = [os.path.dirname(md), os.path.join(md, '..', '..', '..')] + sys.path from ..Qt import QtGui, QtCore, QT_LIB from ..graphicsItems.ROI import ROI diff --git a/pyqtgraph/canvas/CanvasTemplate.ui b/pyqtgraph/canvas/CanvasTemplate.ui index bfdacf38..15fdf7a9 100644 --- a/pyqtgraph/canvas/CanvasTemplate.ui +++ b/pyqtgraph/canvas/CanvasTemplate.ui @@ -11,7 +11,7 @@ - Form + PyQtGraph diff --git a/pyqtgraph/canvas/CanvasTemplate_pyqt.py b/pyqtgraph/canvas/CanvasTemplate_pyqt.py index 3569c8e7..823265aa 100644 --- a/pyqtgraph/canvas/CanvasTemplate_pyqt.py +++ b/pyqtgraph/canvas/CanvasTemplate_pyqt.py @@ -91,7 +91,7 @@ class Ui_Form(object): QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): - Form.setWindowTitle(_translate("Form", "Form", None)) + Form.setWindowTitle(_translate("Form", "PyQtGraph", None)) self.autoRangeBtn.setText(_translate("Form", "Auto Range", None)) self.redirectCheck.setToolTip(_translate("Form", "Check to display all local items in a remote canvas.", None)) self.redirectCheck.setText(_translate("Form", "Redirect", None)) diff --git a/pyqtgraph/canvas/CanvasTemplate_pyqt5.py b/pyqtgraph/canvas/CanvasTemplate_pyqt5.py index 03310d39..83afc772 100644 --- a/pyqtgraph/canvas/CanvasTemplate_pyqt5.py +++ b/pyqtgraph/canvas/CanvasTemplate_pyqt5.py @@ -79,7 +79,7 @@ class Ui_Form(object): def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate - Form.setWindowTitle(_translate("Form", "Form")) + Form.setWindowTitle(_translate("Form", "PyQtGraph")) self.autoRangeBtn.setText(_translate("Form", "Auto Range")) self.redirectCheck.setToolTip(_translate("Form", "Check to display all local items in a remote canvas.")) self.redirectCheck.setText(_translate("Form", "Redirect")) diff --git a/pyqtgraph/canvas/CanvasTemplate_pyside.py b/pyqtgraph/canvas/CanvasTemplate_pyside.py index 570d5bd1..c728efac 100644 --- a/pyqtgraph/canvas/CanvasTemplate_pyside.py +++ b/pyqtgraph/canvas/CanvasTemplate_pyside.py @@ -80,7 +80,7 @@ class Ui_Form(object): QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): - Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8)) + Form.setWindowTitle(QtGui.QApplication.translate("Form", "PyQtGraph", None, QtGui.QApplication.UnicodeUTF8)) self.autoRangeBtn.setText(QtGui.QApplication.translate("Form", "Auto Range", None, QtGui.QApplication.UnicodeUTF8)) self.redirectCheck.setToolTip(QtGui.QApplication.translate("Form", "Check to display all local items in a remote canvas.", None, QtGui.QApplication.UnicodeUTF8)) self.redirectCheck.setText(QtGui.QApplication.translate("Form", "Redirect", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/pyqtgraph/canvas/TransformGuiTemplate.ui b/pyqtgraph/canvas/TransformGuiTemplate.ui index d8312388..c63979e0 100644 --- a/pyqtgraph/canvas/TransformGuiTemplate.ui +++ b/pyqtgraph/canvas/TransformGuiTemplate.ui @@ -17,7 +17,7 @@ - Form + PyQtGraph diff --git a/pyqtgraph/canvas/TransformGuiTemplate_pyqt.py b/pyqtgraph/canvas/TransformGuiTemplate_pyqt.py index c6cf82e4..7cbb3652 100644 --- a/pyqtgraph/canvas/TransformGuiTemplate_pyqt.py +++ b/pyqtgraph/canvas/TransformGuiTemplate_pyqt.py @@ -59,7 +59,7 @@ class Ui_Form(object): QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): - Form.setWindowTitle(_translate("Form", "Form", None)) + Form.setWindowTitle(_translate("Form", "PyQtGraph", None)) self.translateLabel.setText(_translate("Form", "Translate:", None)) self.rotateLabel.setText(_translate("Form", "Rotate:", None)) self.scaleLabel.setText(_translate("Form", "Scale:", None)) diff --git a/pyqtgraph/canvas/TransformGuiTemplate_pyqt5.py b/pyqtgraph/canvas/TransformGuiTemplate_pyqt5.py index 6b1f239b..2af0499a 100644 --- a/pyqtgraph/canvas/TransformGuiTemplate_pyqt5.py +++ b/pyqtgraph/canvas/TransformGuiTemplate_pyqt5.py @@ -46,7 +46,7 @@ class Ui_Form(object): def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate - Form.setWindowTitle(_translate("Form", "Form")) + Form.setWindowTitle(_translate("Form", "PyQtGraph")) self.translateLabel.setText(_translate("Form", "Translate:")) self.rotateLabel.setText(_translate("Form", "Rotate:")) self.scaleLabel.setText(_translate("Form", "Scale:")) diff --git a/pyqtgraph/canvas/TransformGuiTemplate_pyside.py b/pyqtgraph/canvas/TransformGuiTemplate_pyside.py index e430b61a..76620342 100644 --- a/pyqtgraph/canvas/TransformGuiTemplate_pyside.py +++ b/pyqtgraph/canvas/TransformGuiTemplate_pyside.py @@ -46,7 +46,7 @@ class Ui_Form(object): QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): - Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8)) + Form.setWindowTitle(QtGui.QApplication.translate("Form", "PyQtGraph", None, QtGui.QApplication.UnicodeUTF8)) self.translateLabel.setText(QtGui.QApplication.translate("Form", "Translate:", None, QtGui.QApplication.UnicodeUTF8)) self.rotateLabel.setText(QtGui.QApplication.translate("Form", "Rotate:", None, QtGui.QApplication.UnicodeUTF8)) self.scaleLabel.setText(QtGui.QApplication.translate("Form", "Scale:", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/pyqtgraph/colormap.py b/pyqtgraph/colormap.py index 585d7ea1..eb423634 100644 --- a/pyqtgraph/colormap.py +++ b/pyqtgraph/colormap.py @@ -1,6 +1,7 @@ import numpy as np from .Qt import QtGui, QtCore from .python2_3 import basestring +from .functions import mkColor class ColorMap(object): @@ -56,9 +57,9 @@ class ColorMap(object): =============== ============================================================== **Arguments:** pos Array of positions where each color is defined - color Array of RGBA colors. - Integer data types are interpreted as 0-255; float data types - are interpreted as 0.0-1.0 + color Array of colors. + Values are interpreted via + :func:`mkColor() `. mode Array of color modes (ColorMap.RGB, HSV_POS, or HSV_NEG) indicating the color space that should be used when interpolating between stops. Note that the last mode value is @@ -68,7 +69,11 @@ class ColorMap(object): self.pos = np.array(pos) order = np.argsort(self.pos) self.pos = self.pos[order] - self.color = np.array(color)[order] + self.color = np.apply_along_axis( + func1d = lambda x: mkColor(x).getRgb(), + axis = -1, + arr = color, + )[order] if mode is None: mode = np.ones(len(pos)) self.mode = mode @@ -225,7 +230,7 @@ class ColorMap(object): x = np.linspace(start, stop, nPts) table = self.map(x, mode) - if not alpha: + if not alpha and mode != self.QCOLOR: return table[:,:3] else: return table diff --git a/pyqtgraph/configfile.py b/pyqtgraph/configfile.py index 275a4fdb..6ae8a0c5 100644 --- a/pyqtgraph/configfile.py +++ b/pyqtgraph/configfile.py @@ -2,7 +2,7 @@ """ configfile.py - Human-readable text configuration file library Copyright 2010 Luke Campagnola -Distributed under MIT/X11 license. See license.txt for more infomation. +Distributed under MIT/X11 license. See license.txt for more information. Used for reading and writing dictionary objects to a python-like configuration file format. Data structures may be nested and contain any data type as long @@ -39,10 +39,10 @@ class ParseError(Exception): def writeConfigFile(data, fname): s = genString(data) - fd = open(fname, 'w') - fd.write(s) - fd.close() - + with open(fname, 'w') as fd: + fd.write(s) + + def readConfigFile(fname): #cwd = os.getcwd() global GLOBAL_PATH @@ -55,9 +55,8 @@ def readConfigFile(fname): try: #os.chdir(newDir) ## bad. - fd = open(fname) - s = asUnicode(fd.read()) - fd.close() + with open(fname) as fd: + s = asUnicode(fd.read()) s = s.replace("\r\n", "\n") s = s.replace("\r", "\n") data = parseString(s)[1] @@ -73,9 +72,8 @@ def readConfigFile(fname): def appendConfigFile(data, fname): s = genString(data) - fd = open(fname, 'a') - fd.write(s) - fd.close() + with open(fname, 'a') as fd: + fd.write(s) def genString(data, indent=''): @@ -194,8 +192,6 @@ def measureIndent(s): if __name__ == '__main__': import tempfile - fn = tempfile.mktemp() - tf = open(fn, 'w') cf = """ key: 'value' key2: ##comment @@ -205,8 +201,9 @@ key2: ##comment key22: [1,2,3] key23: 234 #comment """ - tf.write(cf) - tf.close() + fn = tempfile.mktemp() + with open(fn, 'w') as tf: + tf.write(cf) print("=== Test:===") num = 1 for line in cf.split('\n'): diff --git a/pyqtgraph/console/Console.py b/pyqtgraph/console/Console.py index 477beb77..aac32d63 100644 --- a/pyqtgraph/console/Console.py +++ b/pyqtgraph/console/Console.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- import sys, re, os, time, traceback, subprocess import pickle @@ -98,12 +99,14 @@ class ConsoleWidget(QtGui.QWidget): def loadHistory(self): """Return the list of previously-invoked command strings (or None).""" if self.historyFile is not None: - return pickle.load(open(self.historyFile, 'rb')) + with open(self.historyFile, 'rb') as pf: + return pickle.load(pf) def saveHistory(self, history): """Store the list of previously-invoked command strings.""" if self.historyFile is not None: - pickle.dump(open(self.historyFile, 'wb'), history) + with open(self.historyFile, 'wb') as pf: + pickle.dump(pf, history) def runCmd(self, cmd): self.stdout = sys.stdout diff --git a/pyqtgraph/debug.py b/pyqtgraph/debug.py index 3ddcae37..bc6d6895 100644 --- a/pyqtgraph/debug.py +++ b/pyqtgraph/debug.py @@ -2,7 +2,7 @@ """ debug.py - Functions to aid in debugging Copyright 2010 Luke Campagnola -Distributed under MIT/X11 license. See license.txt for more infomation. +Distributed under MIT/X11 license. See license.txt for more information. """ from __future__ import print_function diff --git a/pyqtgraph/dockarea/Dock.py b/pyqtgraph/dockarea/Dock.py index ddeb0c4a..083756dc 100644 --- a/pyqtgraph/dockarea/Dock.py +++ b/pyqtgraph/dockarea/Dock.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- from ..Qt import QtCore, QtGui from .DockDrop import * @@ -5,10 +6,10 @@ from ..widgets.VerticalLabel import VerticalLabel from ..python2_3 import asUnicode class Dock(QtGui.QWidget, DockDrop): - + sigStretchChanged = QtCore.Signal() sigClosed = QtCore.Signal(object) - + def __init__(self, name, area=None, size=(10, 10), widget=None, hideTitle=False, autoOrientation=True, closable=False): QtGui.QWidget.__init__(self) DockDrop.__init__(self) @@ -68,9 +69,9 @@ class Dock(QtGui.QWidget, DockDrop): }""" self.setAutoFillBackground(False) self.widgetArea.setStyleSheet(self.hStyle) - + self.setStretch(*size) - + if widget is not None: self.addWidget(widget) @@ -82,34 +83,22 @@ class Dock(QtGui.QWidget, DockDrop): return ['dock'] else: return name == 'dock' - + def setStretch(self, x=None, y=None): """ Set the 'target' size for this Dock. The actual size will be determined by comparing this Dock's stretch value to the rest of the docks it shares space with. """ - #print "setStretch", self, x, y - #self._stretch = (x, y) if x is None: x = 0 if y is None: y = 0 - #policy = self.sizePolicy() - #policy.setHorizontalStretch(x) - #policy.setVerticalStretch(y) - #self.setSizePolicy(policy) self._stretch = (x, y) self.sigStretchChanged.emit() - #print "setStretch", self, x, y, self.stretch() def stretch(self): - #policy = self.sizePolicy() - #return policy.horizontalStretch(), policy.verticalStretch() return self._stretch - - #def stretch(self): - #return self._stretch def hideTitleBar(self): """ @@ -121,7 +110,7 @@ class Dock(QtGui.QWidget, DockDrop): if 'center' in self.allowedAreas: self.allowedAreas.remove('center') self.updateStyle() - + def showTitleBar(self): """ Show the title bar for this Dock. @@ -142,7 +131,7 @@ class Dock(QtGui.QWidget, DockDrop): Sets the text displayed in title bar for this Dock. """ self.label.setText(text) - + def setOrientation(self, o='auto', force=False): """ Sets the orientation of the title bar for this Dock. @@ -150,7 +139,12 @@ class Dock(QtGui.QWidget, DockDrop): By default ('auto'), the orientation is determined based on the aspect ratio of the Dock. """ - #print self.name(), "setOrientation", o, force + # setOrientation may be called before the container is set in some cases + # (via resizeEvent), so there's no need to do anything here until called + # again by containerChanged + if self.container() is None: + return + if o == 'auto' and self.autoOrient: if self.container().type() == 'tab': o = 'horizontal' @@ -162,22 +156,19 @@ class Dock(QtGui.QWidget, DockDrop): self.orientation = o self.label.setOrientation(o) self.updateStyle() - + def updateStyle(self): ## updates orientation and appearance of title bar - #print self.name(), "update style:", self.orientation, self.moveLabel, self.label.isVisible() if self.labelHidden: self.widgetArea.setStyleSheet(self.nStyle) elif self.orientation == 'vertical': self.label.setOrientation('vertical') if self.moveLabel: - #print self.name(), "reclaim label" self.topLayout.addWidget(self.label, 1, 0) self.widgetArea.setStyleSheet(self.vStyle) else: self.label.setOrientation('horizontal') if self.moveLabel: - #print self.name(), "reclaim label" self.topLayout.addWidget(self.label, 0, 1) self.widgetArea.setStyleSheet(self.hStyle) @@ -203,13 +194,12 @@ class Dock(QtGui.QWidget, DockDrop): def startDrag(self): self.drag = QtGui.QDrag(self) mime = QtCore.QMimeData() - #mime.setPlainText("asd") self.drag.setMimeData(mime) self.widgetArea.setStyleSheet(self.dragStyle) self.update() action = self.drag.exec_() self.updateStyle() - + def float(self): self.area.floatDock(self) @@ -220,7 +210,6 @@ class Dock(QtGui.QWidget, DockDrop): if self._container is not None: # ask old container to close itself if it is no longer needed self._container.apoptose() - #print self.name(), "container changed" self._container = c if c is None: self.area = None @@ -241,6 +230,7 @@ class Dock(QtGui.QWidget, DockDrop): def close(self): """Remove this dock from the DockArea it lives inside.""" self.setParent(None) + QtGui.QLabel.close(self.label) self.label.setParent(None) self._container.apoptose() self._container = None @@ -265,10 +255,10 @@ class Dock(QtGui.QWidget, DockDrop): class DockLabel(VerticalLabel): - + sigClicked = QtCore.Signal(object, object) sigCloseClicked = QtCore.Signal() - + def __init__(self, text, dock, showCloseButton): self.dim = False self.fixedWidth = False @@ -295,7 +285,7 @@ class DockLabel(VerticalLabel): fg = '#fff' bg = '#66c' border = '#55B' - + if self.orientation == 'vertical': self.vStyle = """DockLabel { background-color : %s; @@ -329,7 +319,7 @@ class DockLabel(VerticalLabel): if self.dim != d: self.dim = d self.updateStyle() - + def setOrientation(self, o): VerticalLabel.setOrientation(self, o) self.updateStyle() @@ -339,12 +329,12 @@ class DockLabel(VerticalLabel): self.pressPos = ev.pos() self.startedDrag = False ev.accept() - + def mouseMoveEvent(self, ev): if not self.startedDrag and (ev.pos() - self.pressPos).manhattanLength() > QtGui.QApplication.startDragDistance(): self.dock.startDrag() ev.accept() - + def mouseReleaseEvent(self, ev): ev.accept() if not self.startedDrag: @@ -353,7 +343,7 @@ class DockLabel(VerticalLabel): def mouseDoubleClickEvent(self, ev): if ev.button() == QtCore.Qt.LeftButton: self.dock.float() - + def resizeEvent (self, ev): if self.closeButton: if self.orientation == 'vertical': diff --git a/pyqtgraph/dockarea/DockArea.py b/pyqtgraph/dockarea/DockArea.py index b7b0659e..ff3f22ab 100644 --- a/pyqtgraph/dockarea/DockArea.py +++ b/pyqtgraph/dockarea/DockArea.py @@ -9,9 +9,9 @@ from ..python2_3 import basestring class DockArea(Container, QtGui.QWidget, DockDrop): - def __init__(self, temporary=False, home=None): + def __init__(self, parent=None, temporary=False, home=None): Container.__init__(self, self) - QtGui.QWidget.__init__(self) + QtGui.QWidget.__init__(self, parent=parent) DockDrop.__init__(self, allowedAreas=['left', 'right', 'top', 'bottom']) self.layout = QtGui.QVBoxLayout() self.layout.setContentsMargins(0,0,0,0) diff --git a/pyqtgraph/dockarea/tests/test_dock.py b/pyqtgraph/dockarea/tests/test_dock.py index 949f3f0e..3fb47075 100644 --- a/pyqtgraph/dockarea/tests/test_dock.py +++ b/pyqtgraph/dockarea/tests/test_dock.py @@ -14,3 +14,15 @@ def test_dock(): assert dock.name() == name # no surprises in return type. assert type(dock.name()) == type(name) + +def test_closable_dock(): + name = "Test close dock" + dock = da.Dock(name=name, closable=True) + + assert dock.label.closeButton != None + +def test_hide_title_dock(): + name = "Test hide title dock" + dock = da.Dock(name=name, hideTitle=True) + + assert dock.labelHidden == True diff --git a/pyqtgraph/exporters/CSVExporter.py b/pyqtgraph/exporters/CSVExporter.py index b87f0182..33c6ec69 100644 --- a/pyqtgraph/exporters/CSVExporter.py +++ b/pyqtgraph/exporters/CSVExporter.py @@ -1,7 +1,9 @@ +# -*- coding: utf-8 -*- from ..Qt import QtGui, QtCore from .Exporter import Exporter from ..parametertree import Parameter from .. import PlotItem +from ..python2_3 import asUnicode __all__ = ['CSVExporter'] @@ -29,7 +31,6 @@ class CSVExporter(Exporter): self.fileSaveDialog(filter=["*.csv", "*.tsv"]) return - fd = open(fileName, 'w') data = [] header = [] @@ -55,28 +56,29 @@ class CSVExporter(Exporter): sep = ',' else: sep = '\t' - - fd.write(sep.join(header) + '\n') - i = 0 - numFormat = '%%0.%dg' % self.params['precision'] - numRows = max([len(d[0]) for d in data]) - for i in range(numRows): - for j, d in enumerate(data): - # write x value if this is the first column, or if we want x - # for all rows - if appendAllX or j == 0: - if d is not None and i < len(d[0]): - fd.write(numFormat % d[0][i] + sep) + + with open(fileName, 'w') as fd: + fd.write(sep.join(map(asUnicode, header)) + '\n') + i = 0 + numFormat = '%%0.%dg' % self.params['precision'] + numRows = max([len(d[0]) for d in data]) + for i in range(numRows): + for j, d in enumerate(data): + # write x value if this is the first column, or if we want + # x for all rows + if appendAllX or j == 0: + if d is not None and i < len(d[0]): + fd.write(numFormat % d[0][i] + sep) + else: + fd.write(' %s' % sep) + + # write y value + if d is not None and i < len(d[1]): + fd.write(numFormat % d[1][i] + sep) else: fd.write(' %s' % sep) - - # write y value - if d is not None and i < len(d[1]): - fd.write(numFormat % d[1][i] + sep) - else: - fd.write(' %s' % sep) - fd.write('\n') - fd.close() + fd.write('\n') + CSVExporter.register() diff --git a/pyqtgraph/exporters/HDF5Exporter.py b/pyqtgraph/exporters/HDF5Exporter.py index 584a9f71..2a2ac19c 100644 --- a/pyqtgraph/exporters/HDF5Exporter.py +++ b/pyqtgraph/exporters/HDF5Exporter.py @@ -44,20 +44,27 @@ class HDF5Exporter(Exporter): data = [] appendAllX = self.params['columnMode'] == '(x,y) per plot' - #print dir(self.item.curves[0]) - tlen = 0 - for i, c in enumerate(self.item.curves): - d = c.getData() - if i > 0 and len(d[0]) != tlen: - raise ValueError ("HDF5 Export requires all curves in plot to have same length") - if appendAllX or i == 0: - data.append(d[0]) - tlen = len(d[0]) - data.append(d[1]) + # Check if the arrays are ragged + len_first = len(self.item.curves[0].getData()[0]) if self.item.curves[0] else None + ragged = any(len(i.getData()[0]) != len_first for i in self.item.curves) + if ragged: + dgroup = fd.create_group(dsname) + for i, c in enumerate(self.item.curves): + d = c.getData() + fdata = numpy.array([d[0], d[1]]).astype('double') + cname = c.name() if c.name() is not None else str(i) + dset = dgroup.create_dataset(cname, data=fdata) + else: + for i, c in enumerate(self.item.curves): + d = c.getData() + if appendAllX or i == 0: + data.append(d[0]) + data.append(d[1]) + + fdata = numpy.array(data).astype('double') + dset = fd.create_dataset(dsname, data=fdata) - fdata = numpy.array(data).astype('double') - dset = fd.create_dataset(dsname, data=fdata) fd.close() if HAVE_HDF5: diff --git a/pyqtgraph/exporters/ImageExporter.py b/pyqtgraph/exporters/ImageExporter.py index a43a3d88..cacddee1 100644 --- a/pyqtgraph/exporters/ImageExporter.py +++ b/pyqtgraph/exporters/ImageExporter.py @@ -44,31 +44,33 @@ class ImageExporter(Exporter): def parameters(self): return self.params - + + @staticmethod + def getSupportedImageFormats(): + filter = ["*."+f.data().decode('utf-8') for f in QtGui.QImageWriter.supportedImageFormats()] + preferred = ['*.png', '*.tif', '*.jpg'] + for p in preferred[::-1]: + if p in filter: + filter.remove(p) + filter.insert(0, p) + return filter + def export(self, fileName=None, toBytes=False, copy=False): if fileName is None and not toBytes and not copy: - if QT_LIB in ['PySide', 'PySide2']: - filter = ["*."+str(f) for f in QtGui.QImageWriter.supportedImageFormats()] - else: - filter = ["*."+bytes(f).decode('utf-8') for f in QtGui.QImageWriter.supportedImageFormats()] - preferred = ['*.png', '*.tif', '*.jpg'] - for p in preferred[::-1]: - if p in filter: - filter.remove(p) - filter.insert(0, p) + filter = self.getSupportedImageFormats() self.fileSaveDialog(filter=filter) return - - targetRect = QtCore.QRect(0, 0, self.params['width'], self.params['height']) - sourceRect = self.getSourceRect() - - - #self.png = QtGui.QImage(targetRect.size(), QtGui.QImage.Format_ARGB32) - #self.png.fill(pyqtgraph.mkColor(self.params['background'])) - w, h = self.params['width'], self.params['height'] + + w = int(self.params['width']) + h = int(self.params['height']) if w == 0 or h == 0: - raise Exception("Cannot export image with size=0 (requested export size is %dx%d)" % (w,h)) - bg = np.empty((self.params['height'], self.params['width'], 4), dtype=np.ubyte) + raise Exception("Cannot export image with size=0 (requested " + "export size is %dx%d)" % (w, h)) + + targetRect = QtCore.QRect(0, 0, w, h) + sourceRect = self.getSourceRect() + + bg = np.empty((h, w, 4), dtype=np.ubyte) color = self.params['background'] bg[:,:,0] = color.blue() bg[:,:,1] = color.green() @@ -105,7 +107,7 @@ class ImageExporter(Exporter): elif toBytes: return self.png else: - self.png.save(fileName) + return self.png.save(fileName) ImageExporter.register() diff --git a/pyqtgraph/exporters/SVGExporter.py b/pyqtgraph/exporters/SVGExporter.py index b0e9b1c0..6f0035bb 100644 --- a/pyqtgraph/exporters/SVGExporter.py +++ b/pyqtgraph/exporters/SVGExporter.py @@ -69,6 +69,13 @@ xmlHeader = """\ pyqtgraph SVG export Generated with Qt and pyqtgraph + """ def generateSvg(item, options={}): diff --git a/pyqtgraph/exporters/tests/test_hdf5.py b/pyqtgraph/exporters/tests/test_hdf5.py new file mode 100644 index 00000000..69bb8ae7 --- /dev/null +++ b/pyqtgraph/exporters/tests/test_hdf5.py @@ -0,0 +1,71 @@ +# -*- coding: utf-8 -*- +import pytest +import pyqtgraph as pg +from pyqtgraph.exporters import HDF5Exporter +import numpy as np +from numpy.testing import assert_equal +import h5py +import os + + +@pytest.fixture +def tmp_h5(tmp_path): + yield tmp_path / "data.h5" + + +@pytest.mark.parametrize("combine", [False, True]) +def test_HDF5Exporter(tmp_h5, combine): + # Basic test of functionality: multiple curves with shared x array. Tests + # both options for stacking the data (columnMode). + x = np.linspace(0, 1, 100) + y1 = np.sin(x) + y2 = np.cos(x) + + plt = pg.plot() + plt.plot(x=x, y=y1) + plt.plot(x=x, y=y2) + + ex = HDF5Exporter(plt.plotItem) + + if combine: + ex.parameters()['columnMode'] = '(x,y,y,y) for all plots' + + ex.export(fileName=tmp_h5) + + with h5py.File(tmp_h5, 'r') as f: + # should be a single dataset with the name of the exporter + dset = f[ex.parameters()['Name']] + assert isinstance(dset, h5py.Dataset) + + if combine: + assert_equal(np.array([x, y1, y2]), dset) + else: + assert_equal(np.array([x, y1, x, y2]), dset) + + +def test_HDF5Exporter_unequal_lengths(tmp_h5): + # Test export with multiple curves of different size. The exporter should + # detect this and create multiple hdf5 datasets under a group. + x1 = np.linspace(0, 1, 10) + y1 = np.sin(x1) + x2 = np.linspace(0, 1, 100) + y2 = np.cos(x2) + + plt = pg.plot() + plt.plot(x=x1, y=y1, name='plot0') + plt.plot(x=x2, y=y2) + + ex = HDF5Exporter(plt.plotItem) + ex.export(fileName=tmp_h5) + + with h5py.File(tmp_h5, 'r') as f: + # should be a group with the name of the exporter + group = f[ex.parameters()['Name']] + assert isinstance(group, h5py.Group) + + # should be a dataset under the group with the name of the PlotItem + assert_equal(np.array([x1, y1]), group['plot0']) + + # should be a dataset under the group with a default name that's the + # index of the curve in the PlotItem + assert_equal(np.array([x2, y2]), group['1']) diff --git a/pyqtgraph/exporters/tests/test_image.py b/pyqtgraph/exporters/tests/test_image.py new file mode 100644 index 00000000..6f52eceb --- /dev/null +++ b/pyqtgraph/exporters/tests/test_image.py @@ -0,0 +1,13 @@ +# -*- coding: utf-8 -*- +import pyqtgraph as pg +from pyqtgraph.exporters import ImageExporter + +app = pg.mkQApp() + + +def test_ImageExporter_filename_dialog(): + """Tests ImageExporter code path that opens a file dialog. Regression test + for pull request 1133.""" + p = pg.plot() + exp = ImageExporter(p.getPlotItem()) + exp.export() diff --git a/pyqtgraph/exporters/tests/test_svg.py b/pyqtgraph/exporters/tests/test_svg.py index 2261f7df..62946368 100644 --- a/pyqtgraph/exporters/tests/test_svg.py +++ b/pyqtgraph/exporters/tests/test_svg.py @@ -28,6 +28,7 @@ def test_plotscene(): ex.export(fileName=tempfilename) # clean up after the test is done os.unlink(tempfilename) + w.close() def test_simple(): tempfilename = tempfile.NamedTemporaryFile(suffix='.svg').name diff --git a/pyqtgraph/flowchart/Flowchart.py b/pyqtgraph/flowchart/Flowchart.py index 5aeeac38..2c7b9d59 100644 --- a/pyqtgraph/flowchart/Flowchart.py +++ b/pyqtgraph/flowchart/Flowchart.py @@ -508,7 +508,7 @@ class Flowchart(Node): self.sigStateChanged.emit() def loadFile(self, fileName=None, startDir=None): - """Load a flowchart (*.fc) file. + """Load a flowchart (``*.fc``) file. """ if fileName is None: if startDir is None: @@ -763,6 +763,9 @@ class FlowchartCtrlWidget(QtGui.QWidget): item = self.items[node] self.ui.ctrlList.setCurrentItem(item) + def clearSelection(self): + self.ui.ctrlList.selectionModel().clearSelection() + class FlowchartWidget(dockarea.DockArea): """Includes the actual graphical flowchart and debugging interface""" @@ -834,9 +837,9 @@ class FlowchartWidget(dockarea.DockArea): def buildMenu(self, pos=None): def buildSubMenu(node, rootMenu, subMenus, pos=None): for section, node in node.items(): - menu = QtGui.QMenu(section) - rootMenu.addMenu(menu) - if isinstance(node, OrderedDict): + if isinstance(node, OrderedDict): + menu = QtGui.QMenu(section) + rootMenu.addMenu(menu) buildSubMenu(node, menu, subMenus, pos=pos) subMenus.append(menu) else: @@ -890,7 +893,10 @@ class FlowchartWidget(dockarea.DockArea): item = items[0] if hasattr(item, 'node') and isinstance(item.node, Node): n = item.node - self.ctrl.select(n) + if n in self.ctrl.items: + self.ctrl.select(n) + else: + self.ctrl.clearSelection() data = {'outputs': n.outputValues(), 'inputs': n.inputValues()} self.selNameLabel.setText(n.name()) if hasattr(n, 'nodeName'): @@ -938,4 +944,3 @@ class FlowchartWidget(dockarea.DockArea): class FlowchartNode(Node): pass - diff --git a/pyqtgraph/flowchart/FlowchartCtrlTemplate.ui b/pyqtgraph/flowchart/FlowchartCtrlTemplate.ui index 0361ad3e..6a9a203a 100644 --- a/pyqtgraph/flowchart/FlowchartCtrlTemplate.ui +++ b/pyqtgraph/flowchart/FlowchartCtrlTemplate.ui @@ -11,7 +11,7 @@ - Form + PyQtGraph diff --git a/pyqtgraph/flowchart/FlowchartCtrlTemplate_pyqt.py b/pyqtgraph/flowchart/FlowchartCtrlTemplate_pyqt.py index 8afd43f8..3d8bcf56 100644 --- a/pyqtgraph/flowchart/FlowchartCtrlTemplate_pyqt.py +++ b/pyqtgraph/flowchart/FlowchartCtrlTemplate_pyqt.py @@ -69,7 +69,7 @@ class Ui_Form(object): QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): - Form.setWindowTitle(_translate("Form", "Form", None)) + Form.setWindowTitle(_translate("Form", "PyQtGraph", None)) self.loadBtn.setText(_translate("Form", "Load..", None)) self.saveBtn.setText(_translate("Form", "Save", None)) self.saveAsBtn.setText(_translate("Form", "As..", None)) diff --git a/pyqtgraph/flowchart/FlowchartCtrlTemplate_pyqt5.py b/pyqtgraph/flowchart/FlowchartCtrlTemplate_pyqt5.py index b661918d..958f2aaf 100644 --- a/pyqtgraph/flowchart/FlowchartCtrlTemplate_pyqt5.py +++ b/pyqtgraph/flowchart/FlowchartCtrlTemplate_pyqt5.py @@ -56,7 +56,7 @@ class Ui_Form(object): def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate - Form.setWindowTitle(_translate("Form", "Form")) + Form.setWindowTitle(_translate("Form", "PyQtGraph")) self.loadBtn.setText(_translate("Form", "Load..")) self.saveBtn.setText(_translate("Form", "Save")) self.saveAsBtn.setText(_translate("Form", "As..")) diff --git a/pyqtgraph/flowchart/FlowchartCtrlTemplate_pyside.py b/pyqtgraph/flowchart/FlowchartCtrlTemplate_pyside.py index b722000e..2db10f6a 100644 --- a/pyqtgraph/flowchart/FlowchartCtrlTemplate_pyside.py +++ b/pyqtgraph/flowchart/FlowchartCtrlTemplate_pyside.py @@ -55,7 +55,7 @@ class Ui_Form(object): QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): - Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8)) + Form.setWindowTitle(QtGui.QApplication.translate("Form", "PyQtGraph", None, QtGui.QApplication.UnicodeUTF8)) self.loadBtn.setText(QtGui.QApplication.translate("Form", "Load..", None, QtGui.QApplication.UnicodeUTF8)) self.saveBtn.setText(QtGui.QApplication.translate("Form", "Save", None, QtGui.QApplication.UnicodeUTF8)) self.saveAsBtn.setText(QtGui.QApplication.translate("Form", "As..", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/pyqtgraph/flowchart/FlowchartTemplate.ui b/pyqtgraph/flowchart/FlowchartTemplate.ui index 8b0c19da..22934b91 100644 --- a/pyqtgraph/flowchart/FlowchartTemplate.ui +++ b/pyqtgraph/flowchart/FlowchartTemplate.ui @@ -11,7 +11,7 @@ - Form + PyQtGraph diff --git a/pyqtgraph/flowchart/FlowchartTemplate_pyqt.py b/pyqtgraph/flowchart/FlowchartTemplate_pyqt.py index 06b10bfe..e6084eee 100644 --- a/pyqtgraph/flowchart/FlowchartTemplate_pyqt.py +++ b/pyqtgraph/flowchart/FlowchartTemplate_pyqt.py @@ -62,7 +62,7 @@ class Ui_Form(object): QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): - Form.setWindowTitle(_translate("Form", "Form", None)) + Form.setWindowTitle(_translate("Form", "PyQtGraph", None)) from ..flowchart.FlowchartGraphicsView import FlowchartGraphicsView from ..widgets.DataTreeWidget import DataTreeWidget diff --git a/pyqtgraph/flowchart/FlowchartTemplate_pyqt5.py b/pyqtgraph/flowchart/FlowchartTemplate_pyqt5.py index ba754305..448a00ff 100644 --- a/pyqtgraph/flowchart/FlowchartTemplate_pyqt5.py +++ b/pyqtgraph/flowchart/FlowchartTemplate_pyqt5.py @@ -49,7 +49,7 @@ class Ui_Form(object): def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate - Form.setWindowTitle(_translate("Form", "Form")) + Form.setWindowTitle(_translate("Form", "PyQtGraph")) from ..widgets.DataTreeWidget import DataTreeWidget from ..flowchart.FlowchartGraphicsView import FlowchartGraphicsView diff --git a/pyqtgraph/flowchart/FlowchartTemplate_pyside.py b/pyqtgraph/flowchart/FlowchartTemplate_pyside.py index 2c693c60..47f97f85 100644 --- a/pyqtgraph/flowchart/FlowchartTemplate_pyside.py +++ b/pyqtgraph/flowchart/FlowchartTemplate_pyside.py @@ -48,7 +48,7 @@ class Ui_Form(object): QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): - Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8)) + Form.setWindowTitle(QtGui.QApplication.translate("Form", "PyQtGraph", None, QtGui.QApplication.UnicodeUTF8)) from ..flowchart.FlowchartGraphicsView import FlowchartGraphicsView from ..widgets.DataTreeWidget import DataTreeWidget diff --git a/pyqtgraph/flowchart/library/Data.py b/pyqtgraph/flowchart/library/Data.py index 18f1c948..b133b159 100644 --- a/pyqtgraph/flowchart/library/Data.py +++ b/pyqtgraph/flowchart/library/Data.py @@ -2,6 +2,7 @@ from ..Node import Node from ...Qt import QtGui, QtCore import numpy as np +import sys from .common import * from ...SRTTransform import SRTTransform from ...Point import Point @@ -238,7 +239,12 @@ class EvalNode(Node): fn = "def fn(**args):\n" run = "\noutput=fn(**args)\n" text = fn + "\n".join([" "+l for l in str(self.text.toPlainText()).split('\n')]) + run - exec(text) + if sys.version_info.major == 2: + exec(text) + elif sys.version_info.major == 3: + ldict = locals() + exec(text, globals(), ldict) + output = ldict['output'] except: print("Error processing node: %s" % self.name()) raise diff --git a/pyqtgraph/flowchart/library/common.py b/pyqtgraph/flowchart/library/common.py index 8b3376c3..1f5613c9 100644 --- a/pyqtgraph/flowchart/library/common.py +++ b/pyqtgraph/flowchart/library/common.py @@ -91,14 +91,15 @@ class CtrlNode(Node): sigStateChanged = QtCore.Signal(object) def __init__(self, name, ui=None, terminals=None): + if terminals is None: + terminals = {'In': {'io': 'in'}, 'Out': {'io': 'out', 'bypass': 'In'}} + Node.__init__(self, name=name, terminals=terminals) + if ui is None: if hasattr(self, 'uiTemplate'): ui = self.uiTemplate else: ui = [] - if terminals is None: - terminals = {'In': {'io': 'in'}, 'Out': {'io': 'out', 'bypass': 'In'}} - Node.__init__(self, name=name, terminals=terminals) self.ui, self.stateGroup, self.ctrls = generateUi(ui) self.stateGroup.sigChanged.connect(self.changed) diff --git a/pyqtgraph/functions.py b/pyqtgraph/functions.py index 6f67cfff..75318bbc 100644 --- a/pyqtgraph/functions.py +++ b/pyqtgraph/functions.py @@ -2,7 +2,7 @@ """ functions.py - Miscellaneous functions with no other home Copyright 2010 Luke Campagnola -Distributed under MIT/X11 license. See license.txt for more infomation. +Distributed under MIT/X11 license. See license.txt for more information. """ from __future__ import division @@ -11,6 +11,7 @@ import numpy as np import decimal, re import ctypes import sys, struct +from .pgcollections import OrderedDict from .python2_3 import asUnicode, basestring from .Qt import QtGui, QtCore, QT_LIB from . import getConfigOption, setConfigOptions @@ -76,9 +77,10 @@ def siScale(x, minVal=1e-25, allowUnicode=True): pref = SI_PREFIXES[m+8] else: pref = SI_PREFIXES_ASCII[m+8] - p = .001**m + m1 = -3*m + p = 10.**m1 - return (p, pref) + return (p, pref) def siFormat(x, precision=3, suffix='', space=True, error=None, minVal=1e-25, allowUnicode=True): @@ -387,14 +389,15 @@ def glColor(*args, **kargs): -def makeArrowPath(headLen=20, tipAngle=20, tailLen=20, tailWidth=3, baseAngle=0): +def makeArrowPath(headLen=20, headWidth=None, tipAngle=20, tailLen=20, tailWidth=3, baseAngle=0): """ Construct a path outlining an arrow with the given dimensions. The arrow points in the -x direction with tip positioned at 0,0. - If *tipAngle* is supplied (in degrees), it overrides *headWidth*. + If *headWidth* is supplied, it overrides *tipAngle* (in degrees). If *tailLen* is None, no tail will be drawn. """ - headWidth = headLen * np.tan(tipAngle * 0.5 * np.pi/180.) + if headWidth is None: + headWidth = headLen * np.tan(tipAngle * 0.5 * np.pi/180.) path = QtGui.QPainterPath() path.moveTo(0,0) path.lineTo(headLen, -headWidth) @@ -424,6 +427,8 @@ def eq(a, b): 3. When comparing arrays, returns False if the array shapes are not the same. 4. When comparing arrays of the same shape, returns True only if all elements are equal (whereas the == operator would return a boolean array). + 5. Collections (dict, list, etc.) must have the same type to be considered equal. One + consequence is that comparing a dict to an OrderedDict will always return False. """ if a is b: return True @@ -440,6 +445,28 @@ def eq(a, b): if aIsArr and bIsArr and (a.shape != b.shape or a.dtype != b.dtype): return False + # Recursively handle common containers + if isinstance(a, dict) and isinstance(b, dict): + if type(a) != type(b) or len(a) != len(b): + return False + if set(a.keys()) != set(b.keys()): + return False + for k, v in a.items(): + if not eq(v, b[k]): + return False + if isinstance(a, OrderedDict) or sys.version_info >= (3, 7): + for a_item, b_item in zip(a.items(), b.items()): + if not eq(a_item, b_item): + return False + return True + if isinstance(a, (list, tuple)) and isinstance(b, (list, tuple)): + if type(a) != type(b) or len(a) != len(b): + return False + for v1,v2 in zip(a, b): + if not eq(v1, v2): + return False + return True + # Test for equivalence. # If the test raises a recognized exception, then return Falase try: @@ -909,10 +936,12 @@ def solveBilinearTransform(points1, points2): return matrix def rescaleData(data, scale, offset, dtype=None, clip=None): - """Return data rescaled and optionally cast to a new dtype:: - + """Return data rescaled and optionally cast to a new dtype. + + The scaling operation is:: + data => (data-offset) * scale - + """ if dtype is None: dtype = data.dtype @@ -1035,7 +1064,6 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False): ============== ================================================================================== """ profile = debug.Profiler() - if data.ndim not in (2, 3): raise TypeError("data must be 2D or 3D") if data.ndim == 3 and data.shape[2] > 4: @@ -1074,7 +1102,7 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False): # Decide on maximum scaled value if scale is None: if lut is not None: - scale = lut.shape[0] - 1 + scale = lut.shape[0] else: scale = 255. @@ -1083,7 +1111,14 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False): dtype = np.ubyte else: dtype = np.min_scalar_type(lut.shape[0]-1) - + + # awkward, but fastest numpy native nan evaluation + # + nanMask = None + if data.dtype.kind == 'f' and np.isnan(data.min()): + nanMask = np.isnan(data) + if data.ndim > 2: + nanMask = np.any(nanMask, axis=-1) # Apply levels if given if levels is not None: if isinstance(levels, np.ndarray) and levels.ndim == 2: @@ -1105,11 +1140,11 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False): if minVal != 0 or maxVal != scale: if minVal == maxVal: maxVal = np.nextafter(maxVal, 2*maxVal) - data = rescaleData(data, scale/(maxVal-minVal), minVal, dtype=dtype) - + rng = maxVal-minVal + rng = 1 if rng == 0 else rng + data = rescaleData(data, scale/rng, minVal, dtype=dtype) profile() - # apply LUT if given if lut is not None: data = applyLookupTable(data, lut) @@ -1152,7 +1187,12 @@ def makeARGB(data, lut=None, levels=None, scale=None, useRGBA=False): imgData[..., 3] = 255 else: alpha = True - + + # apply nan mask through alpha channel + if nanMask is not None: + alpha = True + imgData[nanMask, 3] = 0 + profile() return imgData, alpha @@ -1223,30 +1263,10 @@ def makeQImage(imgData, alpha=None, copy=True, transpose=True): if QT_LIB in ['PySide', 'PySide2']: ch = ctypes.c_char.from_buffer(imgData, 0) - - # Bug in PySide + Python 3 causes refcount for image data to be improperly - # incremented, which leads to leaked memory. As a workaround, we manually - # reset the reference count after creating the QImage. - # See: https://bugreports.qt.io/browse/PYSIDE-140 - - # Get initial reference count (PyObject struct has ob_refcnt as first element) - rcount = ctypes.c_long.from_address(id(ch)).value img = QtGui.QImage(ch, imgData.shape[1], imgData.shape[0], imgFormat) - if sys.version[0] == '3': - # Reset refcount only on python 3. Technically this would have no effect - # on python 2, but this is a nasty hack, and checking for version here - # helps to mitigate possible unforseen consequences. - ctypes.c_long.from_address(id(ch)).value = rcount else: - #addr = ctypes.addressof(ctypes.c_char.from_buffer(imgData, 0)) ## PyQt API for QImage changed between 4.9.3 and 4.9.6 (I don't know exactly which version it was) ## So we first attempt the 4.9.6 API, then fall back to 4.9.3 - #addr = ctypes.c_char.from_buffer(imgData, 0) - #try: - #img = QtGui.QImage(addr, imgData.shape[1], imgData.shape[0], imgFormat) - #except TypeError: - #addr = ctypes.addressof(addr) - #img = QtGui.QImage(addr, imgData.shape[1], imgData.shape[0], imgFormat) try: img = QtGui.QImage(imgData.ctypes.data, imgData.shape[1], imgData.shape[0], imgFormat) except: @@ -1259,16 +1279,6 @@ def makeQImage(imgData, alpha=None, copy=True, transpose=True): img.data = imgData return img - #try: - #buf = imgData.data - #except AttributeError: ## happens when image data is non-contiguous - #buf = imgData.data - - #profiler() - #qimage = QtGui.QImage(buf, imgData.shape[1], imgData.shape[0], imgFormat) - #profiler() - #qimage.data = imgData - #return qimage def imageToArray(img, copy=False, transpose=True): """ @@ -2312,14 +2322,62 @@ def invertQTransform(tr): raise Exception("Transform is not invertible.") return inv[0] + +def pseudoScatter(data, spacing=None, shuffle=True, bidir=False, method='exact'): + """Return an array of position values needed to make beeswarm or column scatter plots. -def pseudoScatter(data, spacing=None, shuffle=True, bidir=False): - """ - Used for examining the distribution of values in a set. Produces scattering as in beeswarm or column scatter plots. + Used for examining the distribution of values in an array. - Given a list of x-values, construct a set of y-values such that an x,y scatter-plot + Given an array of x-values, construct an array of y-values such that an x,y scatter-plot will not have overlapping points (it will look similar to a histogram). """ + if method == 'exact': + return _pseudoScatterExact(data, spacing=spacing, shuffle=shuffle, bidir=bidir) + elif method == 'histogram': + return _pseudoScatterHistogram(data, spacing=spacing, shuffle=shuffle, bidir=bidir) + + +def _pseudoScatterHistogram(data, spacing=None, shuffle=True, bidir=False): + """Works by binning points into a histogram and spreading them out to fill the bin. + + Faster method, but can produce blocky results. + """ + inds = np.arange(len(data)) + if shuffle: + np.random.shuffle(inds) + + data = data[inds] + + if spacing is None: + spacing = 2.*np.std(data)/len(data)**0.5 + + yvals = np.empty(len(data)) + + dmin = data.min() + dmax = data.max() + nbins = int((dmax-dmin) / spacing) + 1 + bins = np.linspace(dmin, dmax, nbins) + dx = bins[1] - bins[0] + dbins = ((data - bins[0]) / dx).astype(int) + binCounts = {} + + for i,j in enumerate(dbins): + c = binCounts.get(j, -1) + 1 + binCounts[j] = c + yvals[i] = c + + if bidir is True: + for i in range(nbins): + yvals[dbins==i] -= binCounts.get(i, 0) * 0.5 + + return yvals[np.argsort(inds)] ## un-shuffle values before returning + + +def _pseudoScatterExact(data, spacing=None, shuffle=True, bidir=False): + """Works by stacking points up one at a time, searching for the lowest position available at each point. + + This method produces nice, smooth results but can be prohibitively slow for large datasets. + """ inds = np.arange(len(data)) if shuffle: np.random.shuffle(inds) @@ -2469,6 +2527,3 @@ class SignalBlock(object): def __exit__(self, *args): if self.reconnect: self.signal.connect(self.slot) - - - diff --git a/pyqtgraph/graphicsItems/ArrowItem.py b/pyqtgraph/graphicsItems/ArrowItem.py index 897cbc50..b272b7fc 100644 --- a/pyqtgraph/graphicsItems/ArrowItem.py +++ b/pyqtgraph/graphicsItems/ArrowItem.py @@ -28,6 +28,7 @@ class ArrowItem(QtGui.QGraphicsPathItem): 'angle': -150, ## If the angle is 0, the arrow points left 'pos': (0,0), 'headLen': 20, + 'headWidth': None, 'tipAngle': 25, 'baseAngle': 0, 'tailLen': None, @@ -52,10 +53,10 @@ class ArrowItem(QtGui.QGraphicsPathItem): 0; arrow pointing to the left. headLen Length of the arrow head, from tip to base. default=20 - headWidth Width of the arrow head at its base. + headWidth Width of the arrow head at its base. If + headWidth is specified, it overrides tipAngle. tipAngle Angle of the tip of the arrow in degrees. Smaller - values make a 'sharper' arrow. If tipAngle is - specified, ot overrides headWidth. default=25 + values make a 'sharper' arrow. default=25 baseAngle Angle of the base of the arrow head. Default is 0, which means that the base of the arrow head is perpendicular to the arrow tail. @@ -70,7 +71,7 @@ class ArrowItem(QtGui.QGraphicsPathItem): """ self.opts.update(opts) - opt = dict([(k,self.opts[k]) for k in ['headLen', 'tipAngle', 'baseAngle', 'tailLen', 'tailWidth']]) + opt = dict([(k,self.opts[k]) for k in ['headLen', 'headWidth', 'tipAngle', 'baseAngle', 'tailLen', 'tailWidth']]) tr = QtGui.QTransform() tr.rotate(self.opts['angle']) self.path = tr.map(fn.makeArrowPath(**opt)) diff --git a/pyqtgraph/graphicsItems/AxisItem.py b/pyqtgraph/graphicsItems/AxisItem.py index b34052ae..39b572d3 100644 --- a/pyqtgraph/graphicsItems/AxisItem.py +++ b/pyqtgraph/graphicsItems/AxisItem.py @@ -1,8 +1,10 @@ +# -*- coding: utf-8 -*- from ..Qt import QtGui, QtCore from ..python2_3 import asUnicode import numpy as np from ..Point import Point from .. import debug as debug +import sys import weakref from .. import functions as fn from .. import getConfigOption @@ -17,7 +19,7 @@ class AxisItem(GraphicsWidget): If maxTickLength is negative, ticks point into the plot. """ - def __init__(self, orientation, pen=None, linkView=None, parent=None, maxTickLength=-5, showValues=True, text='', units='', unitPrefix='', **args): + def __init__(self, orientation, pen=None, textPen=None, linkView=None, parent=None, maxTickLength=-5, showValues=True, text='', units='', unitPrefix='', **args): """ ============== =============================================================== **Arguments:** @@ -28,13 +30,14 @@ class AxisItem(GraphicsWidget): to be linked to the visible range of a ViewBox. showValues (bool) Whether to display values adjacent to ticks pen (QPen) Pen used when drawing ticks. + textPen (QPen) Pen used when drawing tick labels. text The text (excluding units) to display on the label for this axis. units The units for this axis. Units should generally be given without any scaling prefix (eg, 'V' instead of 'mV'). The scaling prefix will be automatically prepended based on the range of data displayed. - **args All extra keyword arguments become CSS style options for + args All extra keyword arguments become CSS style options for the tag which will surround the axis label and units. ============== =============================================================== """ @@ -80,7 +83,6 @@ class AxisItem(GraphicsWidget): self.labelUnitPrefix = unitPrefix self.labelStyle = args self.logMode = False - self.tickFont = None self._tickLevels = None ## used to override the automatic ticking system with explicit ticks self._tickSpacing = None # used to override default tickSpacing method @@ -97,6 +99,11 @@ class AxisItem(GraphicsWidget): else: self.setPen(pen) + if textPen is None: + self.setTextPen() + else: + self.setTextPen(pen) + self._linkedView = None if linkView is not None: self.linkToView(linkView) @@ -198,7 +205,11 @@ class AxisItem(GraphicsWidget): self.update() def setTickFont(self, font): - self.tickFont = font + """ + (QFont or None) Determines the font used for tick values. + Use None for the default font. + """ + self.style['tickFont'] = font self.picture = None self.prepareGeometryChange() ## Need to re-allocate space depending on font size? @@ -249,7 +260,7 @@ class AxisItem(GraphicsWidget): without any scaling prefix (eg, 'V' instead of 'mV'). The scaling prefix will be automatically prepended based on the range of data displayed. - **args All extra keyword arguments become CSS style options for + args All extra keyword arguments become CSS style options for the tag which will surround the axis label and units. ============== ============================================================= @@ -405,6 +416,25 @@ class AxisItem(GraphicsWidget): self.setLabel() self.update() + def textPen(self): + if self._textPen is None: + return fn.mkPen(getConfigOption('foreground')) + return fn.mkPen(self._textPen) + + def setTextPen(self, *args, **kwargs): + """ + Set the pen used for drawing text. + If no arguments are given, the default foreground color will be used. + """ + self.picture = None + if args or kwargs: + self._textPen = fn.mkPen(*args, **kwargs) + else: + self._textPen = fn.mkPen(getConfigOption('foreground')) + self.labelStyle['color'] = '#' + fn.colorStr(self._textPen.color())[:6] + self.setLabel() + self.update() + def setScale(self, scale=None): """ Set the value scaling for this axis. @@ -444,15 +474,19 @@ class AxisItem(GraphicsWidget): def updateAutoSIPrefix(self): if self.label.isVisible(): - (scale, prefix) = fn.siScale(max(abs(self.range[0]*self.scale), abs(self.range[1]*self.scale))) + if self.logMode: + _range = 10**np.array(self.range) + else: + _range = self.range + (scale, prefix) = fn.siScale(max(abs(_range[0]*self.scale), abs(_range[1]*self.scale))) if self.labelUnits == '' and prefix in ['k', 'm']: ## If we are not showing units, wait until 1e6 before scaling. scale = 1.0 prefix = '' + self.autoSIPrefixScale = scale self.setLabel(unitPrefix=prefix) else: - scale = 1.0 + self.autoSIPrefixScale = 1.0 - self.autoSIPrefixScale = scale self.picture = None self.update() @@ -477,20 +511,29 @@ class AxisItem(GraphicsWidget): def linkToView(self, view): """Link this axis to a ViewBox, causing its displayed range to match the visible range of the view.""" - oldView = self.linkedView() + self.unlinkFromView() + self._linkedView = weakref.ref(view) + if self.orientation in ['right', 'left']: + view.sigYRangeChanged.connect(self.linkedViewChanged) + else: + view.sigXRangeChanged.connect(self.linkedViewChanged) + + view.sigResized.connect(self.linkedViewChanged) + + def unlinkFromView(self): + """Unlink this axis from a ViewBox.""" + oldView = self.linkedView() + self._linkedView = None if self.orientation in ['right', 'left']: if oldView is not None: oldView.sigYRangeChanged.disconnect(self.linkedViewChanged) - view.sigYRangeChanged.connect(self.linkedViewChanged) else: if oldView is not None: oldView.sigXRangeChanged.disconnect(self.linkedViewChanged) - view.sigXRangeChanged.connect(self.linkedViewChanged) if oldView is not None: oldView.sigResized.disconnect(self.linkedViewChanged) - view.sigResized.connect(self.linkedViewChanged) def linkedViewChanged(self, view, newRange=None): if self.orientation in ['right', 'left']: @@ -533,6 +576,8 @@ class AxisItem(GraphicsWidget): try: picture = QtGui.QPicture() painter = QtGui.QPainter(picture) + if self.style["tickFont"]: + painter.setFont(self.style["tickFont"]) specs = self.generateDrawSpecs(painter) profiler('generate specs') if specs is not None: @@ -771,7 +816,37 @@ class AxisItem(GraphicsWidget): return strings def logTickStrings(self, values, scale, spacing): - return ["%0.1g"%x for x in 10 ** np.array(values).astype(float)] + estrings = ["%0.1g"%x for x in 10 ** np.array(values).astype(float) * np.array(scale)] + + if sys.version_info < (3, 0): + # python 2 does not support unicode strings like that + return estrings + else: # python 3+ + convdict = {"0": "⁰", + "1": "¹", + "2": "²", + "3": "³", + "4": "⁴", + "5": "⁵", + "6": "⁶", + "7": "⁷", + "8": "⁸", + "9": "⁹", + } + dstrings = [] + for e in estrings: + if e.count("e"): + v, p = e.split("e") + sign = "⁻" if p[0] == "-" else "" + pot = "".join([convdict[pp] for pp in p[1:].lstrip("0")]) + if v == "1": + v = "" + else: + v = v + "·" + dstrings.append(v + "10" + sign + pot) + else: + dstrings.append(e) + return dstrings def generateDrawSpecs(self, p): """ @@ -780,8 +855,8 @@ class AxisItem(GraphicsWidget): interpreted by drawPicture(). """ profiler = debug.Profiler() - - #bounds = self.boundingRect() + if self.style['tickFont'] is not None: + p.setFont(self.style['tickFont']) bounds = self.mapRectFromParent(self.geometry()) linkedView = self.linkedView() @@ -892,7 +967,7 @@ class AxisItem(GraphicsWidget): p2[axis] += tickLength*tickDir tickPen = self.pen() color = tickPen.color() - color.setAlpha(lineAlpha) + color.setAlpha(int(lineAlpha)) tickPen.setColor(color) tickSpecs.append((tickPen, Point(p1), Point(p2))) profiler('compute ticks') @@ -1044,13 +1119,13 @@ class AxisItem(GraphicsWidget): p.drawLine(p1, p2) profiler('draw ticks') - ## Draw all text - if self.tickFont is not None: - p.setFont(self.tickFont) - p.setPen(self.pen()) + # Draw all text + if self.style['tickFont'] is not None: + p.setFont(self.style['tickFont']) + p.setPen(self.textPen()) for rect, flags, text in textSpecs: - p.drawText(rect, flags, text) - #p.drawRect(rect) + p.drawText(rect, int(flags), text) + profiler('draw text') def show(self): @@ -1068,23 +1143,26 @@ class AxisItem(GraphicsWidget): self._updateHeight() def wheelEvent(self, ev): - if self.linkedView() is None: + lv = self.linkedView() + if lv is None: return if self.orientation in ['left', 'right']: - self.linkedView().wheelEvent(ev, axis=1) + lv.wheelEvent(ev, axis=1) else: - self.linkedView().wheelEvent(ev, axis=0) + lv.wheelEvent(ev, axis=0) ev.accept() def mouseDragEvent(self, event): - if self.linkedView() is None: + lv = self.linkedView() + if lv is None: return if self.orientation in ['left', 'right']: - return self.linkedView().mouseDragEvent(event, axis=1) + return lv.mouseDragEvent(event, axis=1) else: - return self.linkedView().mouseDragEvent(event, axis=0) + return lv.mouseDragEvent(event, axis=0) def mouseClickEvent(self, event): - if self.linkedView() is None: + lv = self.linkedView() + if lv is None: return - return self.linkedView().mouseClickEvent(event) + return lv.mouseClickEvent(event) diff --git a/pyqtgraph/graphicsItems/BarGraphItem.py b/pyqtgraph/graphicsItems/BarGraphItem.py index 657222ba..4e820cb8 100644 --- a/pyqtgraph/graphicsItems/BarGraphItem.py +++ b/pyqtgraph/graphicsItems/BarGraphItem.py @@ -40,6 +40,7 @@ class BarGraphItem(GraphicsObject): y0=None, x1=None, y1=None, + name=None, height=None, width=None, pen=None, @@ -166,3 +167,15 @@ class BarGraphItem(GraphicsObject): if self.picture is None: self.drawPicture() return self._shape + + def implements(self, interface=None): + ints = ['plotData'] + if interface is None: + return ints + return interface in ints + + def name(self): + return self.opts.get('name', None) + + def getData(self): + return self.opts.get('x'), self.opts.get('height') diff --git a/pyqtgraph/graphicsItems/DateAxisItem.py b/pyqtgraph/graphicsItems/DateAxisItem.py new file mode 100644 index 00000000..846abb90 --- /dev/null +++ b/pyqtgraph/graphicsItems/DateAxisItem.py @@ -0,0 +1,318 @@ +import sys +import numpy as np +import time +from datetime import datetime, timedelta + +from .AxisItem import AxisItem +from ..pgcollections import OrderedDict + +__all__ = ['DateAxisItem'] + +MS_SPACING = 1/1000.0 +SECOND_SPACING = 1 +MINUTE_SPACING = 60 +HOUR_SPACING = 3600 +DAY_SPACING = 24 * HOUR_SPACING +WEEK_SPACING = 7 * DAY_SPACING +MONTH_SPACING = 30 * DAY_SPACING +YEAR_SPACING = 365 * DAY_SPACING + +if sys.platform == 'win32': + _epoch = datetime.utcfromtimestamp(0) + def utcfromtimestamp(timestamp): + return _epoch + timedelta(seconds=timestamp) +else: + utcfromtimestamp = datetime.utcfromtimestamp + +MIN_REGULAR_TIMESTAMP = (datetime(1, 1, 1) - datetime(1970,1,1)).total_seconds() +MAX_REGULAR_TIMESTAMP = (datetime(9999, 1, 1) - datetime(1970,1,1)).total_seconds() +SEC_PER_YEAR = 365.25*24*3600 + +def makeMSStepper(stepSize): + def stepper(val, n): + if val < MIN_REGULAR_TIMESTAMP or val > MAX_REGULAR_TIMESTAMP: + return np.inf + + val *= 1000 + f = stepSize * 1000 + return (val // (n*f) + 1) * (n*f) / 1000.0 + return stepper + +def makeSStepper(stepSize): + def stepper(val, n): + if val < MIN_REGULAR_TIMESTAMP or val > MAX_REGULAR_TIMESTAMP: + return np.inf + + return (val // (n*stepSize) + 1) * (n*stepSize) + return stepper + +def makeMStepper(stepSize): + def stepper(val, n): + if val < MIN_REGULAR_TIMESTAMP or val > MAX_REGULAR_TIMESTAMP: + return np.inf + + d = utcfromtimestamp(val) + base0m = (d.month + n*stepSize - 1) + d = datetime(d.year + base0m // 12, base0m % 12 + 1, 1) + return (d - datetime(1970, 1, 1)).total_seconds() + return stepper + +def makeYStepper(stepSize): + def stepper(val, n): + if val < MIN_REGULAR_TIMESTAMP or val > MAX_REGULAR_TIMESTAMP: + return np.inf + + d = utcfromtimestamp(val) + next_year = (d.year // (n*stepSize) + 1) * (n*stepSize) + if next_year > 9999: + return np.inf + next_date = datetime(next_year, 1, 1) + return (next_date - datetime(1970, 1, 1)).total_seconds() + return stepper + +class TickSpec: + """ Specifies the properties for a set of date ticks and computes ticks + within a given utc timestamp range """ + def __init__(self, spacing, stepper, format, autoSkip=None): + """ + ============= ========================================================== + Arguments + spacing approximate (average) tick spacing + stepper a stepper function that takes a utc time stamp and a step + steps number n to compute the start of the next unit. You + can use the make_X_stepper functions to create common + steppers. + format a strftime compatible format string which will be used to + convert tick locations to date/time strings + autoSkip list of step size multipliers to be applied when the tick + density becomes too high. The tick spec automatically + applies additional powers of 10 (10, 100, ...) to the list + if necessary. Set to None to switch autoSkip off + ============= ========================================================== + + """ + self.spacing = spacing + self.step = stepper + self.format = format + self.autoSkip = autoSkip + + def makeTicks(self, minVal, maxVal, minSpc): + ticks = [] + n = self.skipFactor(minSpc) + x = self.step(minVal, n) + while x <= maxVal: + ticks.append(x) + x = self.step(x, n) + return (np.array(ticks), n) + + def skipFactor(self, minSpc): + if self.autoSkip is None or minSpc < self.spacing: + return 1 + factors = np.array(self.autoSkip, dtype=np.float) + while True: + for f in factors: + spc = self.spacing * f + if spc > minSpc: + return int(f) + factors *= 10 + + +class ZoomLevel: + """ Generates the ticks which appear in a specific zoom level """ + def __init__(self, tickSpecs, exampleText): + """ + ============= ========================================================== + tickSpecs a list of one or more TickSpec objects with decreasing + coarseness + ============= ========================================================== + + """ + self.tickSpecs = tickSpecs + self.utcOffset = 0 + self.exampleText = exampleText + + def tickValues(self, minVal, maxVal, minSpc): + # return tick values for this format in the range minVal, maxVal + # the return value is a list of tuples (, [tick positions]) + # minSpc indicates the minimum spacing (in seconds) between two ticks + # to fullfill the maxTicksPerPt constraint of the DateAxisItem at the + # current zoom level. This is used for auto skipping ticks. + allTicks = [] + valueSpecs = [] + # back-project (minVal maxVal) to UTC, compute ticks then offset to + # back to local time again + utcMin = minVal - self.utcOffset + utcMax = maxVal - self.utcOffset + for spec in self.tickSpecs: + ticks, skipFactor = spec.makeTicks(utcMin, utcMax, minSpc) + # reposition tick labels to local time coordinates + ticks += self.utcOffset + # remove any ticks that were present in higher levels + tick_list = [x for x in ticks.tolist() if x not in allTicks] + allTicks.extend(tick_list) + valueSpecs.append((spec.spacing, tick_list)) + # if we're skipping ticks on the current level there's no point in + # producing lower level ticks + if skipFactor > 1: + break + return valueSpecs + + +YEAR_MONTH_ZOOM_LEVEL = ZoomLevel([ + TickSpec(YEAR_SPACING, makeYStepper(1), '%Y', autoSkip=[1, 5, 10, 25]), + TickSpec(MONTH_SPACING, makeMStepper(1), '%b') +], "YYYY") +MONTH_DAY_ZOOM_LEVEL = ZoomLevel([ + TickSpec(MONTH_SPACING, makeMStepper(1), '%b'), + TickSpec(DAY_SPACING, makeSStepper(DAY_SPACING), '%d', autoSkip=[1, 5]) +], "MMM") +DAY_HOUR_ZOOM_LEVEL = ZoomLevel([ + TickSpec(DAY_SPACING, makeSStepper(DAY_SPACING), '%a %d'), + TickSpec(HOUR_SPACING, makeSStepper(HOUR_SPACING), '%H:%M', autoSkip=[1, 6]) +], "MMM 00") +HOUR_MINUTE_ZOOM_LEVEL = ZoomLevel([ + TickSpec(DAY_SPACING, makeSStepper(DAY_SPACING), '%a %d'), + TickSpec(MINUTE_SPACING, makeSStepper(MINUTE_SPACING), '%H:%M', + autoSkip=[1, 5, 15]) +], "MMM 00") +HMS_ZOOM_LEVEL = ZoomLevel([ + TickSpec(SECOND_SPACING, makeSStepper(SECOND_SPACING), '%H:%M:%S', + autoSkip=[1, 5, 15, 30]) +], "99:99:99") +MS_ZOOM_LEVEL = ZoomLevel([ + TickSpec(MINUTE_SPACING, makeSStepper(MINUTE_SPACING), '%H:%M:%S'), + TickSpec(MS_SPACING, makeMSStepper(MS_SPACING), '%S.%f', + autoSkip=[1, 5, 10, 25]) +], "99:99:99") + +class DateAxisItem(AxisItem): + """ + **Bases:** :class:`AxisItem ` + + An AxisItem that displays dates from unix timestamps. + + The display format is adjusted automatically depending on the current time + density (seconds/point) on the axis. For more details on changing this + behaviour, see :func:`setZoomLevelForDensity() `. + + Can be added to an existing plot e.g. via + :func:`setAxisItems({'bottom':axis}) `. + + """ + + def __init__(self, orientation='bottom', utcOffset=time.timezone, **kwargs): + """ + Create a new DateAxisItem. + + For `orientation` and `**kwargs`, see + :func:`AxisItem.__init__ `. + + """ + + super(DateAxisItem, self).__init__(orientation, **kwargs) + # Set the zoom level to use depending on the time density on the axis + self.utcOffset = utcOffset + + self.zoomLevels = OrderedDict([ + (np.inf, YEAR_MONTH_ZOOM_LEVEL), + (5 * 3600*24, MONTH_DAY_ZOOM_LEVEL), + (6 * 3600, DAY_HOUR_ZOOM_LEVEL), + (15 * 60, HOUR_MINUTE_ZOOM_LEVEL), + (30, HMS_ZOOM_LEVEL), + (1, MS_ZOOM_LEVEL), + ]) + + def tickStrings(self, values, scale, spacing): + tickSpecs = self.zoomLevel.tickSpecs + tickSpec = next((s for s in tickSpecs if s.spacing == spacing), None) + try: + dates = [utcfromtimestamp(v - self.utcOffset) for v in values] + except (OverflowError, ValueError, OSError): + # should not normally happen + return ['%g' % ((v-self.utcOffset)//SEC_PER_YEAR + 1970) for v in values] + + formatStrings = [] + for x in dates: + try: + s = x.strftime(tickSpec.format) + if '%f' in tickSpec.format: + # we only support ms precision + s = s[:-3] + elif '%Y' in tickSpec.format: + s = s.lstrip('0') + formatStrings.append(s) + except ValueError: # Windows can't handle dates before 1970 + formatStrings.append('') + return formatStrings + + def tickValues(self, minVal, maxVal, size): + density = (maxVal - minVal) / size + self.setZoomLevelForDensity(density) + values = self.zoomLevel.tickValues(minVal, maxVal, minSpc=self.minSpacing) + return values + + def setZoomLevelForDensity(self, density): + """ + Setting `zoomLevel` and `minSpacing` based on given density of seconds per pixel + + The display format is adjusted automatically depending on the current time + density (seconds/point) on the axis. You can customize the behaviour by + overriding this function or setting a different set of zoom levels + than the default one. The `zoomLevels` variable is a dictionary with the + maximal distance of ticks in seconds which are allowed for each zoom level + before the axis switches to the next coarser level. To customize the zoom level + selection, override this function. + """ + padding = 10 + + # Size in pixels a specific tick label will take + if self.orientation in ['bottom', 'top']: + def sizeOf(text): + return self.fontMetrics.boundingRect(text).width() + padding*self.fontScaleFactor + else: + def sizeOf(text): + return self.fontMetrics.boundingRect(text).height() + padding*self.fontScaleFactor + + # Fallback zoom level: Years/Months + self.zoomLevel = YEAR_MONTH_ZOOM_LEVEL + for maximalSpacing, zoomLevel in self.zoomLevels.items(): + size = sizeOf(zoomLevel.exampleText) + + # Test if zoom level is too fine grained + if maximalSpacing/size < density: + break + + self.zoomLevel = zoomLevel + + # Set up zoomLevel + self.zoomLevel.utcOffset = self.utcOffset + + # Calculate minimal spacing of items on the axis + size = sizeOf(self.zoomLevel.exampleText) + self.minSpacing = density*size + + def linkToView(self, view): + super(DateAxisItem, self).linkToView(view) + + # Set default limits + _min = MIN_REGULAR_TIMESTAMP + _max = MAX_REGULAR_TIMESTAMP + + if self.orientation in ['right', 'left']: + view.setLimits(yMin=_min, yMax=_max) + else: + view.setLimits(xMin=_min, xMax=_max) + + def generateDrawSpecs(self, p): + # Get font metrics from QPainter + # Not happening in "paint", as the QPainter p there is a different one from the one here, + # so changing that font could cause unwanted side effects + if self.style['tickFont'] is not None: + p.setFont(self.style['tickFont']) + + self.fontMetrics = p.fontMetrics() + + # Get font scale factor by current window resolution + self.fontScaleFactor = p.device().logicalDpiX() / 96 + + return super(DateAxisItem, self).generateDrawSpecs(p) diff --git a/pyqtgraph/graphicsItems/GradientEditorItem.py b/pyqtgraph/graphicsItems/GradientEditorItem.py index fc1d638c..942b280b 100644 --- a/pyqtgraph/graphicsItems/GradientEditorItem.py +++ b/pyqtgraph/graphicsItems/GradientEditorItem.py @@ -1,14 +1,14 @@ +# -*- coding: utf-8 -*- +import operator import weakref import numpy as np from ..Qt import QtGui, QtCore -from ..python2_3 import sortList from .. import functions as fn from .GraphicsObject import GraphicsObject from .GraphicsWidget import GraphicsWidget from ..widgets.SpinBox import SpinBox from ..pgcollections import OrderedDict from ..colormap import ColorMap -from ..python2_3 import cmp __all__ = ['TickSliderItem', 'GradientEditorItem'] @@ -352,8 +352,7 @@ class TickSliderItem(GraphicsWidget): def listTicks(self): """Return a sorted list of all the Tick objects on the slider.""" ## public - ticks = list(self.ticks.items()) - sortList(ticks, lambda a,b: cmp(a[1], b[1])) ## see pyqtgraph.python2_3.sortList + ticks = sorted(self.ticks.items(), key=operator.itemgetter(1)) return ticks @@ -439,8 +438,14 @@ class GradientEditorItem(TickSliderItem): label = QtGui.QLabel() label.setPixmap(px) label.setContentsMargins(1, 1, 1, 1) + labelName = QtGui.QLabel(g) + hbox = QtGui.QHBoxLayout() + hbox.addWidget(labelName) + hbox.addWidget(label) + widget = QtGui.QWidget() + widget.setLayout(hbox) act = QtGui.QWidgetAction(self) - act.setDefaultWidget(label) + act.setDefaultWidget(widget) act.triggered.connect(self.contextMenuClicked) act.name = g self.menu.addAction(act) @@ -456,7 +461,20 @@ class GradientEditorItem(TickSliderItem): self.addTick(1, QtGui.QColor(255,0,0), True) self.setColorMode('rgb') self.updateGradient() - + self.linkedGradients = {} + + def showTicks(self, show=True): + for tick in self.ticks.keys(): + if show: + tick.show() + orig = getattr(self, '_allowAdd_backup', None) + if orig: + self.allowAdd = orig + else: + self._allowAdd_backup = self.allowAdd + self.allowAdd = False #block tick creation + tick.hide() + def setOrientation(self, orientation): ## public """ @@ -655,7 +673,7 @@ class GradientEditorItem(TickSliderItem): s = s1 * (1.-f) + s2 * f v = v1 * (1.-f) + v2 * f c = QtGui.QColor() - c.setHsv(h,s,v) + c.setHsv(*map(int, [h,s,v])) if toQColor: return c else: @@ -759,7 +777,9 @@ class GradientEditorItem(TickSliderItem): for t in self.ticks: c = t.color ticks.append((self.ticks[t], (c.red(), c.green(), c.blue(), c.alpha()))) - state = {'mode': self.colorMode, 'ticks': ticks} + state = {'mode': self.colorMode, + 'ticks': ticks, + 'ticksVisible': next(iter(self.ticks)).isVisible()} return state def restoreState(self, state): @@ -784,6 +804,8 @@ class GradientEditorItem(TickSliderItem): for t in state['ticks']: c = QtGui.QColor(*t[1]) self.addTick(t[0], c, finish=False) + self.showTicks( state.get('ticksVisible', + next(iter(self.ticks)).isVisible()) ) self.updateGradient() self.sigGradientChangeFinished.emit(self) @@ -799,6 +821,18 @@ class GradientEditorItem(TickSliderItem): self.updateGradient() self.sigGradientChangeFinished.emit(self) + def linkGradient(self, slaveGradient, connect=True): + if connect: + fn = lambda g, slave=slaveGradient:slave.restoreState( + g.saveState()) + self.linkedGradients[id(slaveGradient)] = fn + self.sigGradientChanged.connect(fn) + self.sigGradientChanged.emit(self) + else: + fn = self.linkedGradients.get(id(slaveGradient), None) + if fn: + self.sigGradientChanged.disconnect(fn) + class Tick(QtGui.QGraphicsWidget): ## NOTE: Making this a subclass of GraphicsObject instead results in ## activating this bug: https://bugreports.qt-project.org/browse/PYSIDE-86 @@ -869,8 +903,8 @@ class Tick(QtGui.QGraphicsWidget): ## NOTE: Making this a subclass of GraphicsO self.view().tickMoveFinished(self) def mouseClickEvent(self, ev): - if ev.button() == QtCore.Qt.RightButton and self.moving: - ev.accept() + ev.accept() + if ev.button() == QtCore.Qt.RightButton and self.moving: self.setPos(self.startPosition) self.view().tickMoved(self, self.startPosition) self.moving = False @@ -878,7 +912,6 @@ class Tick(QtGui.QGraphicsWidget): ## NOTE: Making this a subclass of GraphicsO self.sigMoved.emit(self) else: self.view().tickClicked(self, ev) - ##remove def hoverEvent(self, ev): if (not ev.isExit()) and ev.acceptDrags(QtCore.Qt.LeftButton): @@ -944,4 +977,3 @@ class TickMenu(QtGui.QMenu): # self.fracPosSpin.blockSignals(True) # self.fracPosSpin.setValue(self.sliderItem().tickValue(self.tick())) # self.fracPosSpin.blockSignals(False) - diff --git a/pyqtgraph/graphicsItems/GraphicsItem.py b/pyqtgraph/graphicsItems/GraphicsItem.py index 628b495b..1a522446 100644 --- a/pyqtgraph/graphicsItems/GraphicsItem.py +++ b/pyqtgraph/graphicsItems/GraphicsItem.py @@ -19,8 +19,9 @@ class GraphicsItem(object): The GraphicsView system places a lot of emphasis on the notion that the graphics within the scene should be device independent--you should be able to take the same graphics and display them on screens of different resolutions, printers, export to SVG, etc. This is nice in principle, but causes me a lot of headache in practice. It means that I have to circumvent all the device-independent expectations any time I want to operate in pixel coordinates rather than arbitrary scene coordinates. A lot of the code in GraphicsItem is devoted to this task--keeping track of view widgets and device transforms, computing the size and shape of a pixel in local item coordinates, etc. Note that in item coordinates, a pixel does not have to be square or even rectangular, so just asking how to increase a bounding rect by 2px can be a rather complex task. """ _pixelVectorGlobalCache = LRUCache(100, 70) - - def __init__(self, register=True): + _mapRectFromViewGlobalCache = LRUCache(100, 70) + + def __init__(self, register=None): if not hasattr(self, '_qtBaseClass'): for b in self.__class__.__bases__: if issubclass(b, QtGui.QGraphicsItem): @@ -28,15 +29,18 @@ class GraphicsItem(object): break if not hasattr(self, '_qtBaseClass'): raise Exception('Could not determine Qt base class for GraphicsItem: %s' % str(self)) - + self._pixelVectorCache = [None, None] self._viewWidget = None self._viewBox = None self._connectedView = None self._exportOpts = False ## If False, not currently exporting. Otherwise, contains dict of export options. - if register: - GraphicsScene.registerObject(self) ## workaround for pyqt bug in graphicsscene.items() - + if register is not None and register: + warnings.warn( + "'register' argument is deprecated and does nothing", + DeprecationWarning, stacklevel=2 + ) + def getViewWidget(self): """ Return the view widget for this item. @@ -98,8 +102,9 @@ class GraphicsItem(object): Extends deviceTransform to automatically determine the viewportTransform. """ if self._exportOpts is not False and 'painter' in self._exportOpts: ## currently exporting; device transform may be different. - return self._exportOpts['painter'].deviceTransform() * self.sceneTransform() - + scaler = self._exportOpts.get('resolutionScale', 1.0) + return self.sceneTransform() * QtGui.QTransform(scaler, 0, 0, scaler, 1, 1) + if viewportTransform is None: view = self.getViewWidget() if view is None: @@ -184,24 +189,23 @@ class GraphicsItem(object): ## (such as when looking at unix timestamps), we can get floating-point errors. dt.setMatrix(dt.m11(), dt.m12(), 0, dt.m21(), dt.m22(), 0, 0, 0, 1) + if direction is None: + direction = QtCore.QPointF(1, 0) + elif direction.manhattanLength() == 0: + raise Exception("Cannot compute pixel length for 0-length vector.") + + key = (dt.m11(), dt.m21(), dt.m12(), dt.m22(), direction.x(), direction.y()) + ## check local cache - if direction is None and dt == self._pixelVectorCache[0]: + if key == self._pixelVectorCache[0]: return tuple(map(Point, self._pixelVectorCache[1])) ## return a *copy* - + ## check global cache - #key = (dt.m11(), dt.m21(), dt.m31(), dt.m12(), dt.m22(), dt.m32(), dt.m31(), dt.m32()) - key = (dt.m11(), dt.m21(), dt.m12(), dt.m22()) pv = self._pixelVectorGlobalCache.get(key, None) - if direction is None and pv is not None: - self._pixelVectorCache = [dt, pv] + if pv is not None: + self._pixelVectorCache = [key, pv] return tuple(map(Point,pv)) ## return a *copy* - - if direction is None: - direction = QtCore.QPointF(1, 0) - if direction.manhattanLength() == 0: - raise Exception("Cannot compute pixel length for 0-length vector.") - ## attempt to re-scale direction vector to fit within the precision of the coordinate system ## Here's the problem: we need to map the vector 'direction' from the item to the device, via transform 'dt'. ## In some extreme cases, this mapping can fail unless the length of 'direction' is cleverly chosen. @@ -364,8 +368,21 @@ class GraphicsItem(object): vt = self.viewTransform() if vt is None: return None - vt = fn.invertQTransform(vt) - return vt.mapRect(obj) + + cache = self._mapRectFromViewGlobalCache + k = ( + vt.m11(), vt.m12(), vt.m13(), + vt.m21(), vt.m22(), vt.m23(), + vt.m31(), vt.m32(), vt.m33(), + ) + + try: + inv_vt = cache[k] + except KeyError: + inv_vt = fn.invertQTransform(vt) + cache[k] = inv_vt + + return inv_vt.mapRect(obj) def pos(self): return Point(self._qtBaseClass.pos(self)) diff --git a/pyqtgraph/graphicsItems/GraphicsLayout.py b/pyqtgraph/graphicsItems/GraphicsLayout.py index 6ec38fb5..9c209352 100644 --- a/pyqtgraph/graphicsItems/GraphicsLayout.py +++ b/pyqtgraph/graphicsItems/GraphicsLayout.py @@ -23,6 +23,7 @@ class GraphicsLayout(GraphicsWidget): self.setLayout(self.layout) self.items = {} ## item: [(row, col), (row, col), ...] lists all cells occupied by the item self.rows = {} ## row: {col1: item1, col2: item2, ...} maps cell location to item + self.itemBorders = {} ## {item1: QtGui.QGraphicsRectItem, ...} border rects self.currentRow = 0 self.currentCol = 0 self.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)) @@ -39,8 +40,10 @@ class GraphicsLayout(GraphicsWidget): See :func:`mkPen ` for arguments. """ self.border = fn.mkPen(*args, **kwds) - self.update() - + + for borderRect in self.itemBorders.values(): + borderRect.setPen(self.border) + def nextRow(self): """Advance to next row for automatic item placement""" self.currentRow += 1 @@ -119,8 +122,21 @@ class GraphicsLayout(GraphicsWidget): self.rows[row2] = {} self.rows[row2][col2] = item self.items[item].append((row2, col2)) - + + borderRect = QtGui.QGraphicsRectItem() + + borderRect.setParentItem(self) + borderRect.setZValue(1e3) + borderRect.setPen(fn.mkPen(self.border)) + + self.itemBorders[item] = borderRect + + item.geometryChanged.connect(self._updateItemBorder) + self.layout.addItem(item, row, col, rowspan, colspan) + self.layout.activate() # Update layout, recalculating bounds. + # Allows some PyQtGraph features to also work without Qt event loop. + self.nextColumn() def getItem(self, row, col): @@ -129,15 +145,7 @@ class GraphicsLayout(GraphicsWidget): def boundingRect(self): return self.rect() - - def paint(self, p, *args): - if self.border is None: - return - p.setPen(fn.mkPen(self.border)) - for i in self.items: - r = i.mapRectToParent(i.boundingRect()) - p.drawRect(r) - + def itemIndex(self, item): for i in range(self.layout.count()): if self.layout.itemAt(i).graphicsItem() is item: @@ -150,15 +158,20 @@ class GraphicsLayout(GraphicsWidget): self.layout.removeAt(ind) self.scene().removeItem(item) - for r,c in self.items[item]: + for r, c in self.items[item]: del self.rows[r][c] del self.items[item] + + item.geometryChanged.disconnect(self._updateItemBorder) + del self.itemBorders[item] + self.update() def clear(self): - items = [] for i in list(self.items.keys()): self.removeItem(i) + self.currentRow = 0 + self.currentCol = 0 def setContentsMargins(self, *args): # Wrap calls to layout. This should happen automatically, but there @@ -168,4 +181,14 @@ class GraphicsLayout(GraphicsWidget): def setSpacing(self, *args): self.layout.setSpacing(*args) - \ No newline at end of file + + def _updateItemBorder(self): + if self.border is None: + return + + item = self.sender() + if item is None: + return + + r = item.mapRectToParent(item.boundingRect()) + self.itemBorders[item].setRect(r) diff --git a/pyqtgraph/graphicsItems/GridItem.py b/pyqtgraph/graphicsItems/GridItem.py index 87f90a62..db64cbbf 100644 --- a/pyqtgraph/graphicsItems/GridItem.py +++ b/pyqtgraph/graphicsItems/GridItem.py @@ -3,6 +3,7 @@ from .UIGraphicsItem import * import numpy as np from ..Point import Point from .. import functions as fn +from .. import getConfigOption __all__ = ['GridItem'] class GridItem(UIGraphicsItem): @@ -12,16 +13,75 @@ class GridItem(UIGraphicsItem): Displays a rectangular grid of lines indicating major divisions within a coordinate system. Automatically determines what divisions to use. """ - - def __init__(self): + + def __init__(self, pen='default', textPen='default'): UIGraphicsItem.__init__(self) #QtGui.QGraphicsItem.__init__(self, *args) #self.setFlag(QtGui.QGraphicsItem.ItemClipsToShape) #self.setCacheMode(QtGui.QGraphicsItem.DeviceCoordinateCache) - + + self.opts = {} + + self.setPen(pen) + self.setTextPen(textPen) + self.setTickSpacing(x=[None, None, None], y=[None, None, None]) + + + def setPen(self, *args, **kwargs): + """Set the pen used to draw the grid.""" + if kwargs == {} and (args == () or args == ('default',)): + self.opts['pen'] = fn.mkPen(getConfigOption('foreground')) + else: + self.opts['pen'] = fn.mkPen(*args, **kwargs) + self.picture = None - - + self.update() + + + def setTextPen(self, *args, **kwargs): + """Set the pen used to draw the texts.""" + if kwargs == {} and (args == () or args == ('default',)): + self.opts['textPen'] = fn.mkPen(getConfigOption('foreground')) + else: + if args == (None,): + self.opts['textPen'] = None + else: + self.opts['textPen'] = fn.mkPen(*args, **kargs) + + self.picture = None + self.update() + + + def setTickSpacing(self, x=None, y=None): + """ + Set the grid tick spacing to use. + + Tick spacing for each axis shall be specified as an array of + descending values, one for each tick scale. When the value + is set to None, grid line distance is chosen automatically + for this particular level. + + Example: + Default setting of 3 scales for each axis: + setTickSpacing(x=[None, None, None], y=[None, None, None]) + + Single scale with distance of 1.0 for X axis, Two automatic + scales for Y axis: + setTickSpacing(x=[1.0], y=[None, None]) + + Single scale with distance of 1.0 for X axis, Two scales + for Y axis, one with spacing of 1.0, other one automatic: + setTickSpacing(x=[1.0], y=[1.0, None]) + """ + self.opts['tickSpacing'] = (x or self.opts['tickSpacing'][0], + y or self.opts['tickSpacing'][1]) + + self.grid_depth = max([len(s) for s in self.opts['tickSpacing']]) + + self.picture = None + self.update() + + def viewRangeChanged(self): UIGraphicsItem.viewRangeChanged(self) self.picture = None @@ -48,7 +108,6 @@ class GridItem(UIGraphicsItem): p = QtGui.QPainter() p.begin(self.picture) - dt = fn.invertQTransform(self.viewTransform()) vr = self.getViewWidget().rect() unit = self.pixelWidth(), self.pixelHeight() dim = [vr.width(), vr.height()] @@ -62,10 +121,22 @@ class GridItem(UIGraphicsItem): x = ul[1] ul[1] = br[1] br[1] = x - for i in [2,1,0]: ## Draw three different scales of grid + + lastd = [None, None] + for i in range(self.grid_depth - 1, -1, -1): dist = br-ul nlTarget = 10.**i + d = 10. ** np.floor(np.log10(abs(dist/nlTarget))+0.5) + for ax in range(0,2): + ts = self.opts['tickSpacing'][ax] + try: + if ts[i] is not None: + d[ax] = ts[i] + except IndexError: + pass + lastd[ax] = d[ax] + ul1 = np.floor(ul / d) * d br1 = np.ceil(br / d) * d dist = br1-ul1 @@ -76,12 +147,25 @@ class GridItem(UIGraphicsItem): #print " d", d #print " nl", nl for ax in range(0,2): ## Draw grid for both axes + if i >= len(self.opts['tickSpacing'][ax]): + continue + if d[ax] < lastd[ax]: + continue + ppl = dim[ax] / nl[ax] - c = np.clip(3.*(ppl-3), 0., 30.) - linePen = QtGui.QPen(QtGui.QColor(255, 255, 255, c)) - textPen = QtGui.QPen(QtGui.QColor(255, 255, 255, c*2)) - #linePen.setCosmetic(True) - #linePen.setWidth(1) + c = np.clip(5 * (ppl-3), 0., 50.).astype(int) + + linePen = self.opts['pen'] + lineColor = self.opts['pen'].color() + lineColor.setAlpha(c) + linePen.setColor(lineColor) + + textPen = self.opts['textPen'] + if textPen is not None: + textColor = self.opts['textPen'].color() + textColor.setAlpha(c * 2) + textPen.setColor(textColor) + bx = (ax+1) % 2 for x in range(0, int(nl[ax])): linePen.setCosmetic(False) @@ -102,8 +186,7 @@ class GridItem(UIGraphicsItem): if p1[ax] < min(ul[ax], br[ax]) or p1[ax] > max(ul[ax], br[ax]): continue p.drawLine(QtCore.QPointF(p1[0], p1[1]), QtCore.QPointF(p2[0], p2[1])) - if i < 2: - p.setPen(textPen) + if i < 2 and textPen is not None: if ax == 0: x = p1[0] + unit[0] y = ul[1] + unit[1] * 8. @@ -114,7 +197,13 @@ class GridItem(UIGraphicsItem): tr = self.deviceTransform() #tr.scale(1.5, 1.5) p.setWorldTransform(fn.invertQTransform(tr)) - for t in texts: - x = tr.map(t[0]) + Point(0.5, 0.5) - p.drawText(x, t[1]) + + if textPen is not None and len(texts) > 0: + # if there is at least one text, then c is set + textColor.setAlpha(c * 2) + p.setPen(QtGui.QPen(textColor)) + for t in texts: + x = tr.map(t[0]) + Point(0.5, 0.5) + p.drawText(x, t[1]) + p.end() diff --git a/pyqtgraph/graphicsItems/HistogramLUTItem.py b/pyqtgraph/graphicsItems/HistogramLUTItem.py index 687c2e3f..38f1e5b4 100644 --- a/pyqtgraph/graphicsItems/HistogramLUTItem.py +++ b/pyqtgraph/graphicsItems/HistogramLUTItem.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """ GraphicsWidget displaying an image histogram along with gradient editor. Can be used to adjust the appearance of images. """ @@ -32,22 +33,20 @@ class HistogramLUTItem(GraphicsWidget): - Movable region over histogram to select black/white levels - Gradient editor to define color lookup table for single-channel images - Parameters - ---------- - image : ImageItem or None - If *image* is provided, then the control will be automatically linked to - the image and changes to the control will be immediately reflected in - the image's appearance. - fillHistogram : bool - By default, the histogram is rendered with a fill. - For performance, set *fillHistogram* = False. - rgbHistogram : bool - Sets whether the histogram is computed once over all channels of the - image, or once per channel. - levelMode : 'mono' or 'rgba' - If 'mono', then only a single set of black/whilte level lines is drawn, - and the levels apply to all channels in the image. If 'rgba', then one - set of levels is drawn for each channel. + ================ =========================================================== + image (:class:`~pyqtgraph.ImageItem` or ``None``) If *image* is + provided, then the control will be automatically linked to + the image and changes to the control will be immediately + reflected in the image's appearance. + fillHistogram (bool) By default, the histogram is rendered with a fill. + For performance, set ``fillHistogram=False`` + rgbHistogram (bool) Sets whether the histogram is computed once over all + channels of the image, or once per channel. + levelMode 'mono' or 'rgba'. If 'mono', then only a single set of + black/white level lines is drawn, and the levels apply to + all channels in the image. If 'rgba', then one set of + levels is drawn for each channel. + ================ =========================================================== """ sigLookupTableChanged = QtCore.Signal(object) @@ -143,6 +142,7 @@ class HistogramLUTItem(GraphicsWidget): p1 = self.vb.mapFromViewToItem(self, Point(self.vb.viewRect().center().x(), rgn[0])) p2 = self.vb.mapFromViewToItem(self, Point(self.vb.viewRect().center().x(), rgn[1])) gradRect = self.gradient.mapRectToParent(self.gradient.gradRect.rect()) + p.setRenderHint(QtGui.QPainter.Antialiasing) for pen in [fn.mkPen((0, 0, 0, 100), width=3), pen]: p.setPen(pen) p.drawLine(p1 + Point(0, 5), gradRect.bottomLeft()) diff --git a/pyqtgraph/graphicsItems/ImageItem.py b/pyqtgraph/graphicsItems/ImageItem.py index 1758bb4d..4b3a94cc 100644 --- a/pyqtgraph/graphicsItems/ImageItem.py +++ b/pyqtgraph/graphicsItems/ImageItem.py @@ -51,6 +51,7 @@ class ImageItem(GraphicsObject): self.levels = None ## [min, max] or [[redMin, redMax], ...] self.lut = None self.autoDownsample = False + self._lastDownsample = (1, 1) self.axisOrder = getConfigOption('imageAxisOrder') @@ -488,7 +489,7 @@ class ImageItem(GraphicsObject): step = (step, step) stepData = self.image[::step[0], ::step[1]] - if 'auto' == bins: + if isinstance(bins, str) and bins == 'auto': mn = np.nanmin(stepData) mx = np.nanmax(stepData) if mx == mn: @@ -551,8 +552,19 @@ class ImageItem(GraphicsObject): def viewTransformChanged(self): if self.autoDownsample: - self.qimage = None - self.update() + o = self.mapToDevice(QtCore.QPointF(0,0)) + x = self.mapToDevice(QtCore.QPointF(1,0)) + y = self.mapToDevice(QtCore.QPointF(0,1)) + w = Point(x-o).length() + h = Point(y-o).length() + if w == 0 or h == 0: + self.qimage = None + return + xds = max(1, int(1.0 / w)) + yds = max(1, int(1.0 / h)) + if (xds, yds) != self._lastDownsample: + self.qimage = None + self.update() def mouseDragEvent(self, ev): if ev.button() != QtCore.Qt.LeftButton: diff --git a/pyqtgraph/graphicsItems/InfiniteLine.py b/pyqtgraph/graphicsItems/InfiniteLine.py index 7aeb1620..37d84c7e 100644 --- a/pyqtgraph/graphicsItems/InfiniteLine.py +++ b/pyqtgraph/graphicsItems/InfiniteLine.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- from ..Qt import QtGui, QtCore from ..Point import Point from .GraphicsObject import GraphicsObject @@ -160,7 +161,8 @@ class InfiniteLine(GraphicsObject): ============= ========================================================= **Arguments** marker String indicating the style of marker to add: - '<|', '|>', '>|', '|<', '<|>', '>|<', '^', 'v', 'o' + ``'<|'``, ``'|>'``, ``'>|'``, ``'|<'``, ``'<|>'``, + ``'>|<'``, ``'^'``, ``'v'``, ``'o'`` position Position (0.0-1.0) along the visible extent of the line to place the marker. Default is 0.5. size Size of the marker in pixels. Default is 10.0. @@ -314,8 +316,8 @@ class InfiniteLine(GraphicsObject): length = br.width() left = br.left() + length * self.span[0] right = br.left() + length * self.span[1] - br.setLeft(left - w) - br.setRight(right + w) + br.setLeft(left) + br.setRight(right) br = br.normalized() vs = self.getViewBox().size() diff --git a/pyqtgraph/graphicsItems/LegendItem.py b/pyqtgraph/graphicsItems/LegendItem.py index 200820fc..67604c45 100644 --- a/pyqtgraph/graphicsItems/LegendItem.py +++ b/pyqtgraph/graphicsItems/LegendItem.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- from .GraphicsWidget import GraphicsWidget from .LabelItem import LabelItem from ..Qt import QtGui, QtCore @@ -6,171 +7,310 @@ from ..Point import Point from .ScatterPlotItem import ScatterPlotItem, drawSymbol from .PlotDataItem import PlotDataItem from .GraphicsWidgetAnchor import GraphicsWidgetAnchor +from .BarGraphItem import BarGraphItem __all__ = ['LegendItem'] + class LegendItem(GraphicsWidget, GraphicsWidgetAnchor): """ Displays a legend used for describing the contents of a plot. - LegendItems are most commonly created by calling PlotItem.addLegend(). - Note that this item should not be added directly to a PlotItem. Instead, - Make it a direct descendant of the PlotItem:: + LegendItems are most commonly created by calling :meth:`PlotItem.addLegend + `. + + Note that this item should *not* be added directly to a PlotItem (via + :meth:`PlotItem.addItem `). Instead, make it a + direct descendant of the PlotItem:: legend.setParentItem(plotItem) """ - def __init__(self, size=None, offset=None): + def __init__(self, size=None, offset=None, horSpacing=25, verSpacing=0, pen=None, + brush=None, labelTextColor=None, frame=True, rowCount=1, colCount=1, **kwargs): """ ============== =============================================================== **Arguments:** size Specifies the fixed size (width, height) of the legend. If - this argument is omitted, the legend will autimatically resize + this argument is omitted, the legend will automatically resize to fit its contents. offset Specifies the offset position relative to the legend's parent. Positive values offset from the left or top; negative values offset from the right or bottom. If offset is None, the legend must be anchored manually by calling anchor() or positioned by calling setPos(). + horSpacing Specifies the spacing between the line symbol and the label. + verSpacing Specifies the spacing between individual entries of the legend + vertically. (Can also be negative to have them really close) + pen Pen to use when drawing legend border. Any single argument + accepted by :func:`mkPen ` is allowed. + brush QBrush to use as legend background filling. Any single argument + accepted by :func:`mkBrush ` is allowed. + labelTextColor Pen to use when drawing legend text. Any single argument + accepted by :func:`mkPen ` is allowed. ============== =============================================================== - + """ - - GraphicsWidget.__init__(self) GraphicsWidgetAnchor.__init__(self) self.setFlag(self.ItemIgnoresTransformations) self.layout = QtGui.QGraphicsGridLayout() + self.layout.setVerticalSpacing(verSpacing) + self.layout.setHorizontalSpacing(horSpacing) + self.setLayout(self.layout) self.items = [] self.size = size self.offset = offset + self.frame = frame + self.columnCount = colCount + self.rowCount = rowCount + self.curRow = 0 if size is not None: self.setGeometry(QtCore.QRectF(0, 0, self.size[0], self.size[1])) - + + self.opts = { + 'pen': fn.mkPen(pen), + 'brush': fn.mkBrush(brush), + 'labelTextColor': labelTextColor, + 'offset': offset, + } + + self.opts.update(kwargs) + + def offset(self): + """Get the offset position relative to the parent.""" + return self.opts['offset'] + + def setOffset(self, offset): + """Set the offset position relative to the parent.""" + self.opts['offset'] = offset + + offset = Point(self.opts['offset']) + anchorx = 1 if offset[0] <= 0 else 0 + anchory = 1 if offset[1] <= 0 else 0 + anchor = (anchorx, anchory) + self.anchor(itemPos=anchor, parentPos=anchor, offset=offset) + + def pen(self): + """Get the QPen used to draw the border around the legend.""" + return self.opts['pen'] + + def setPen(self, *args, **kargs): + """Set the pen used to draw a border around the legend. + + Accepts the same arguments as :func:`~pyqtgraph.mkPen`. + """ + pen = fn.mkPen(*args, **kargs) + self.opts['pen'] = pen + + self.update() + + def brush(self): + """Get the QBrush used to draw the legend background.""" + return self.opts['brush'] + + def setBrush(self, *args, **kargs): + """Set the brush used to draw the legend background. + + Accepts the same arguments as :func:`~pyqtgraph.mkBrush`. + """ + brush = fn.mkBrush(*args, **kargs) + if self.opts['brush'] == brush: + return + self.opts['brush'] = brush + + self.update() + + def labelTextColor(self): + """Get the QColor used for the item labels.""" + return self.opts['labelTextColor'] + + def setLabelTextColor(self, *args, **kargs): + """Set the color of the item labels. + + Accepts the same arguments as :func:`~pyqtgraph.mkColor`. + """ + self.opts['labelTextColor'] = fn.mkColor(*args, **kargs) + for sample, label in self.items: + label.setAttr('color', self.opts['labelTextColor']) + + self.update() + def setParentItem(self, p): + """Set the parent.""" ret = GraphicsWidget.setParentItem(self, p) - if self.offset is not None: - offset = Point(self.offset) + if self.opts['offset'] is not None: + offset = Point(self.opts['offset']) anchorx = 1 if offset[0] <= 0 else 0 anchory = 1 if offset[1] <= 0 else 0 anchor = (anchorx, anchory) self.anchor(itemPos=anchor, parentPos=anchor, offset=offset) return ret - + def addItem(self, item, name): """ - Add a new entry to the legend. + Add a new entry to the legend. ============== ======================================================== **Arguments:** - item A PlotDataItem from which the line and point style - of the item will be determined or an instance of - ItemSample (or a subclass), allowing the item display - to be customized. + item A :class:`~pyqtgraph.PlotDataItem` from which the line + and point style of the item will be determined or an + instance of ItemSample (or a subclass), allowing the + item display to be customized. title The title to display for this item. Simple HTML allowed. ============== ======================================================== """ - label = LabelItem(name) + label = LabelItem(name, color=self.opts['labelTextColor'], justify='left') if isinstance(item, ItemSample): sample = item else: - sample = ItemSample(item) - row = self.layout.rowCount() + sample = ItemSample(item) self.items.append((sample, label)) - self.layout.addItem(sample, row, 0) - self.layout.addItem(label, row, 1) + self._addItemToLayout(sample, label) self.updateSize() + + def _addItemToLayout(self, sample, label): + col = self.layout.columnCount() + row = self.layout.rowCount() + if row: + row -= 1 + nCol = self.columnCount*2 + #FIRST ROW FULL + if col == nCol: + for col in range(0,nCol,2): + #FIND RIGHT COLUMN + if not self.layout.itemAt(row, col): + break + if col+2 == nCol: + #MAKE NEW ROW + col = 0 + row += 1 + self.layout.addItem(sample, row, col) + self.layout.addItem(label, row, col+1) + + def setColumnCount(self, columnCount): + ''' + change the orientation of all items of the legend + ''' + if columnCount != self.columnCount: + self.columnCount = columnCount + self.rowCount = int(len(self.items)/columnCount) + for i in range(self.layout.count()-1,-1,-1): + self.layout.removeAt(i) #clear layout + for sample, label in self.items: + self._addItemToLayout(sample, label) + self.updateSize() + def getLabel(self, plotItem): + """ + return the labelItem inside the legend for a given plotItem + the label-text can be changed via labenItem.setText + """ + out = [(it, lab) for it, lab in self.items if it.item==plotItem] + try: return out[0][1] + except IndexError: return None + def removeItem(self, item): """ - Removes one item from the legend. + Removes one item from the legend. ============== ======================================================== **Arguments:** item The item to remove or its name. ============== ======================================================== """ - # Thanks, Ulrich! - # cycle for a match for sample, label in self.items: if sample.item is item or label.text == item: - self.items.remove( (sample, label) ) # remove from itemlist + self.items.remove((sample, label)) # remove from itemlist self.layout.removeItem(sample) # remove from layout sample.close() # remove from drawing self.layout.removeItem(label) label.close() self.updateSize() # redraq box + return # return after first match + + def clear(self): + """Remove all items from the legend.""" + for sample, label in self.items: + self.layout.removeItem(sample) + self.layout.removeItem(label) + + self.items = [] + self.updateSize() def updateSize(self): if self.size is not None: return - height = 0 width = 0 - #print("-------") - for sample, label in self.items: - height += max(sample.height(), label.height()) + 3 - width = max(width, (sample.sizeHint(QtCore.Qt.MinimumSize, sample.size()).width() + - label.sizeHint(QtCore.Qt.MinimumSize, label.size()).width())) - #print(width, height) - #print width, height - self.setGeometry(0, 0, width+25, height) - + for row in range(self.layout.rowCount()): + row_height = 0 + col_witdh = 0 + for col in range(self.layout.columnCount()): + item = self.layout.itemAt(row, col) + if item: + col_witdh += item.width() + 3 + row_height = max(row_height, item.height()) + width = max(width, col_witdh) + height += row_height + self.setGeometry(0, 0, width, height) + return + def boundingRect(self): return QtCore.QRectF(0, 0, self.width(), self.height()) - + def paint(self, p, *args): - p.setPen(fn.mkPen(255,255,255,100)) - p.setBrush(fn.mkBrush(100,100,100,50)) - p.drawRect(self.boundingRect()) + if self.frame: + p.setPen(self.opts['pen']) + p.setBrush(self.opts['brush']) + p.drawRect(self.boundingRect()) def hoverEvent(self, ev): ev.acceptDrags(QtCore.Qt.LeftButton) - + def mouseDragEvent(self, ev): if ev.button() == QtCore.Qt.LeftButton: + ev.accept() dpos = ev.pos() - ev.lastPos() self.autoAnchor(self.pos() + dpos) class ItemSample(GraphicsWidget): """ Class responsible for drawing a single item in a LegendItem (sans label). - + This may be subclassed to draw custom graphics in a Legend. """ ## Todo: make this more generic; let each item decide how it should be represented. def __init__(self, item): GraphicsWidget.__init__(self) self.item = item - + def boundingRect(self): return QtCore.QRectF(0, 0, 20, 20) - + def paint(self, p, *args): - #p.setRenderHint(p.Antialiasing) # only if the data is antialiased. opts = self.item.opts - - if opts.get('fillLevel',None) is not None and opts.get('fillBrush',None) is not None: - p.setBrush(fn.mkBrush(opts['fillBrush'])) - p.setPen(fn.mkPen(None)) - p.drawPolygon(QtGui.QPolygonF([QtCore.QPointF(2,18), QtCore.QPointF(18,2), QtCore.QPointF(18,18)])) - + + if opts.get('antialias'): + p.setRenderHint(p.Antialiasing) + if not isinstance(self.item, ScatterPlotItem): p.setPen(fn.mkPen(opts['pen'])) - p.drawLine(2, 18, 18, 2) - + p.drawLine(0, 11, 20, 11) + + if opts.get('fillLevel', None) is not None and opts.get('fillBrush', None) is not None: + p.setBrush(fn.mkBrush(opts['fillBrush'])) + p.setPen(fn.mkPen(opts['fillBrush'])) + p.drawPolygon(QtGui.QPolygonF([QtCore.QPointF(2, 18), QtCore.QPointF(18, 2), QtCore.QPointF(18, 18)])) + + symbol = opts.get('symbol', None) if symbol is not None: if isinstance(self.item, PlotDataItem): opts = self.item.scatter.opts - - pen = fn.mkPen(opts['pen']) - brush = fn.mkBrush(opts['brush']) - size = opts['size'] - - p.translate(10,10) - path = drawSymbol(p, symbol, size, pen, brush) - - - - + p.translate(10, 10) + drawSymbol(p, symbol, opts['size'], fn.mkPen(opts['pen']), fn.mkBrush(opts['brush'])) + + if isinstance(self.item, BarGraphItem): + p.setBrush(fn.mkBrush(opts['brush'])) + p.drawRect(QtCore.QRectF(2, 2, 18, 18)) diff --git a/pyqtgraph/graphicsItems/LinearRegionItem.py b/pyqtgraph/graphicsItems/LinearRegionItem.py index 9903dac5..56ff5748 100644 --- a/pyqtgraph/graphicsItems/LinearRegionItem.py +++ b/pyqtgraph/graphicsItems/LinearRegionItem.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- from ..Qt import QtGui, QtCore from .GraphicsObject import GraphicsObject from .InfiniteLine import InfiniteLine @@ -54,17 +55,17 @@ class LinearRegionItem(GraphicsObject): False, they are static. bounds Optional [min, max] bounding values for the region span Optional [min, max] giving the range over the view to draw - the region. For example, with a vertical line, use span=(0.5, 1) - to draw only on the top half of the view. + the region. For example, with a vertical line, use + ``span=(0.5, 1)`` to draw only on the top half of the + view. swapMode Sets the behavior of the region when the lines are moved such that - their order reverses: - * "block" means the user cannot drag one line past the other - * "push" causes both lines to be moved if one would cross the other - * "sort" means that lines may trade places, but the output of - getRegion always gives the line positions in ascending order. - * None means that no attempt is made to handle swapped line - positions. - The default is "sort". + their order reverses. "block" means the user cannot drag + one line past the other. "push" causes both lines to be + moved if one would cross the other. "sort" means that + lines may trade places, but the output of getRegion + always gives the line positions in ascending order. None + means that no attempt is made to handle swapped line + positions. The default is "sort". ============== ===================================================================== """ diff --git a/pyqtgraph/graphicsItems/MultiPlotItem.py b/pyqtgraph/graphicsItems/MultiPlotItem.py index be775d4a..065a605e 100644 --- a/pyqtgraph/graphicsItems/MultiPlotItem.py +++ b/pyqtgraph/graphicsItems/MultiPlotItem.py @@ -2,7 +2,7 @@ """ MultiPlotItem.py - Graphics item used for displaying an array of PlotItems Copyright 2010 Luke Campagnola -Distributed under MIT/X11 license. See license.txt for more infomation. +Distributed under MIT/X11 license. See license.txt for more information. """ from numpy import ndarray diff --git a/pyqtgraph/graphicsItems/PlotCurveItem.py b/pyqtgraph/graphicsItems/PlotCurveItem.py index 86062b5b..1b3197b5 100644 --- a/pyqtgraph/graphicsItems/PlotCurveItem.py +++ b/pyqtgraph/graphicsItems/PlotCurveItem.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- from ..Qt import QtGui, QtCore try: from ..Qt import QtOpenGL @@ -28,15 +29,15 @@ class PlotCurveItem(GraphicsObject): - Fill under curve - Mouse interaction - ==================== =============================================== + ===================== =============================================== **Signals:** - sigPlotChanged(self) Emitted when the data being plotted has changed - sigClicked(self) Emitted when the curve is clicked - ==================== =============================================== + sigPlotChanged(self) Emitted when the data being plotted has changed + sigClicked(self, ev) Emitted when the curve is clicked + ===================== =============================================== """ sigPlotChanged = QtCore.Signal(object) - sigClicked = QtCore.Signal(object) + sigClicked = QtCore.Signal(object, object) def __init__(self, *args, **kargs): """ @@ -61,6 +62,7 @@ class PlotCurveItem(GraphicsObject): self.opts = { 'shadowPen': None, 'fillLevel': None, + 'fillOutline': False, 'brush': None, 'stepMode': False, 'name': None, @@ -165,7 +167,7 @@ class PlotCurveItem(GraphicsObject): b = np.percentile(d, [50 * (1 - frac), 50 * (1 + frac)]) ## adjust for fill level - if ax == 1 and self.opts['fillLevel'] is not None: + if ax == 1 and self.opts['fillLevel'] not in [None, 'enclosed']: b = (min(b[0], self.opts['fillLevel']), max(b[1], self.opts['fillLevel'])) ## Add pen width only if it is non-cosmetic. @@ -271,7 +273,7 @@ class PlotCurveItem(GraphicsObject): self.update() def setShadowPen(self, *args, **kargs): - """Set the shadow pen used to draw behind tyhe primary pen. + """Set the shadow pen used to draw behind the primary pen. This pen must have a larger width than the primary pen to be visible. """ @@ -305,6 +307,8 @@ class PlotCurveItem(GraphicsObject): :func:`mkPen ` is allowed. fillLevel (float or None) Fill the area 'under' the curve to *fillLevel* + fillOutline (bool) If True, an outline surrounding the *fillLevel* + area is drawn. brush QBrush to use when filling. Any single argument accepted by :func:`mkBrush ` is allowed. antialias (bool) Whether to use antialiasing when drawing. This @@ -354,18 +358,19 @@ class PlotCurveItem(GraphicsObject): kargs[k] = data if not isinstance(data, np.ndarray) or data.ndim > 1: raise Exception("Plot data must be 1D ndarray.") - if 'complex' in str(data.dtype): + if data.dtype.kind == 'c': raise Exception("Can not plot complex data types.") profiler("data checks") #self.setCacheMode(QtGui.QGraphicsItem.NoCache) ## Disabling and re-enabling the cache works around a bug in Qt 4.6 causing the cached results to display incorrectly ## Test this bug with test_PlotWidget and zoom in on the animated plot + self.yData = kargs['y'].view(np.ndarray) + self.xData = kargs['x'].view(np.ndarray) + self.invalidateBounds() self.prepareGeometryChange() self.informViewBoundsChanged() - self.yData = kargs['y'].view(np.ndarray) - self.xData = kargs['x'].view(np.ndarray) profiler('copy') @@ -396,6 +401,8 @@ class PlotCurveItem(GraphicsObject): self.setFillLevel(kargs['fillLevel']) if kargs.get("brush") is not None: self.setBrush(kargs['brush']) + if kargs.get("fillOutline") is not None: + self.opts['fillOutline'] = kargs['fillOutline'] if kargs.get("antialias") is not None: self.opts['antialias'] = kargs['antialias'] @@ -473,9 +480,10 @@ class PlotCurveItem(GraphicsObject): if x is None: x,y = self.getData() p2 = QtGui.QPainterPath(self.path) - p2.lineTo(x[-1], self.opts['fillLevel']) - p2.lineTo(x[0], self.opts['fillLevel']) - p2.lineTo(x[0], y[0]) + if self.opts['fillLevel'] != 'enclosed': + p2.lineTo(x[-1], self.opts['fillLevel']) + p2.lineTo(x[0], self.opts['fillLevel']) + p2.lineTo(x[0], y[0]) p2.closeSubpath() self.fillPath = p2 @@ -494,8 +502,6 @@ class PlotCurveItem(GraphicsObject): #pen.setColor(c) ##pen.setCosmetic(True) - - # Avoid constructing a shadow pen if it's not used. if self.opts.get('shadowPen') is not None: if isinstance(self.opts.get('shadowPen'), QtGui.QPen): @@ -578,7 +584,7 @@ class PlotCurveItem(GraphicsObject): gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glHint(gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST) - gl.glDrawArrays(gl.GL_LINE_STRIP, 0, pos.size / pos.shape[-1]) + gl.glDrawArrays(gl.GL_LINE_STRIP, 0, int(pos.size / pos.shape[-1])) finally: gl.glDisableClientState(gl.GL_VERTEX_ARRAY) finally: @@ -618,7 +624,7 @@ class PlotCurveItem(GraphicsObject): return if self.mouseShape().contains(ev.pos()): ev.accept() - self.sigClicked.emit(self) + self.sigClicked.emit(self, ev) @@ -646,4 +652,3 @@ class ROIPlotItem(PlotCurveItem): def roiChangedEvent(self): d = self.getRoiData() self.updateData(d, self.xVals) - diff --git a/pyqtgraph/graphicsItems/PlotDataItem.py b/pyqtgraph/graphicsItems/PlotDataItem.py index ab2eef96..58a218c7 100644 --- a/pyqtgraph/graphicsItems/PlotDataItem.py +++ b/pyqtgraph/graphicsItems/PlotDataItem.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- import numpy as np from .. import metaarray as metaarray from ..Qt import QtCore @@ -40,25 +41,30 @@ class PlotDataItem(GraphicsObject): **Data initialization arguments:** (x,y data only) =================================== ====================================== - PlotDataItem(xValues, yValues) x and y values may be any sequence (including ndarray) of real numbers - PlotDataItem(yValues) y values only -- x will be automatically set to range(len(y)) + PlotDataItem(xValues, yValues) x and y values may be any sequence + (including ndarray) of real numbers + PlotDataItem(yValues) y values only -- x will be + automatically set to range(len(y)) PlotDataItem(x=xValues, y=yValues) x and y given by keyword arguments - PlotDataItem(ndarray(Nx2)) numpy array with shape (N, 2) where x=data[:,0] and y=data[:,1] + PlotDataItem(ndarray(Nx2)) numpy array with shape (N, 2) where + ``x=data[:,0]`` and ``y=data[:,1]`` =================================== ====================================== **Data initialization arguments:** (x,y data AND may include spot style) - =========================== ========================================= - PlotDataItem(recarray) numpy array with dtype=[('x', float), ('y', float), ...] - PlotDataItem(list-of-dicts) [{'x': x, 'y': y, ...}, ...] - PlotDataItem(dict-of-lists) {'x': [...], 'y': [...], ...} - PlotDataItem(MetaArray) 1D array of Y values with X sepecified as axis values - OR 2D array with a column 'y' and extra columns as needed. - =========================== ========================================= + ============================ ========================================= + PlotDataItem(recarray) numpy array with ``dtype=[('x', float), + ('y', float), ...]`` + PlotDataItem(list-of-dicts) ``[{'x': x, 'y': y, ...}, ...]`` + PlotDataItem(dict-of-lists) ``{'x': [...], 'y': [...], ...}`` + PlotDataItem(MetaArray) 1D array of Y values with X sepecified as + axis values OR 2D array with a column 'y' + and extra columns as needed. + ============================ ========================================= **Line style keyword arguments:** - ========== ============================================================================== + ============ ============================================================================== connect Specifies how / whether vertexes should be connected. See :func:`arrayToQPath() ` pen Pen to use for drawing line between points. @@ -67,13 +73,14 @@ class PlotDataItem(GraphicsObject): shadowPen Pen for secondary line to draw behind the primary line. disabled by default. May be any single argument accepted by :func:`mkPen() ` fillLevel Fill the area between the curve and fillLevel - fillBrush Fill to use when fillLevel is specified. + fillOutline (bool) If True, an outline surrounding the *fillLevel* area is drawn. + fillBrush Fill to use when fillLevel is specified. May be any single argument accepted by :func:`mkBrush() ` stepMode If True, two orthogonal lines are drawn for each sample as steps. This is commonly used when drawing histograms. - Note that in this case, `len(x) == len(y) + 1` + Note that in this case, ``len(x) == len(y) + 1`` (added in version 0.9.9) - ========== ============================================================================== + ============ ============================================================================== **Point style keyword arguments:** (see :func:`ScatterPlotItem.setData() ` for more information) @@ -154,6 +161,7 @@ class PlotDataItem(GraphicsObject): 'pen': (200,200,200), 'shadowPen': None, 'fillLevel': None, + 'fillOutline': False, 'fillBrush': None, 'stepMode': None, @@ -448,9 +456,9 @@ class PlotDataItem(GraphicsObject): if y is not None and x is None: x = np.arange(len(y)) - if isinstance(x, list): + if not isinstance(x, np.ndarray): x = np.array(x) - if isinstance(y, list): + if not isinstance(y, np.ndarray): y = np.array(y) self.xData = x.view(np.ndarray) ## one last check to make sure there are no MetaArrays getting by @@ -474,7 +482,7 @@ class PlotDataItem(GraphicsObject): def updateItems(self): curveArgs = {} - for k,v in [('pen','pen'), ('shadowPen','shadowPen'), ('fillLevel','fillLevel'), ('fillBrush', 'brush'), ('antialias', 'antialias'), ('connect', 'connect'), ('stepMode', 'stepMode')]: + for k,v in [('pen','pen'), ('shadowPen','shadowPen'), ('fillLevel','fillLevel'), ('fillOutline', 'fillOutline'), ('fillBrush', 'brush'), ('antialias', 'antialias'), ('connect', 'connect'), ('stepMode', 'stepMode')]: curveArgs[v] = self.opts[k] scatterArgs = {} @@ -542,13 +550,26 @@ class PlotDataItem(GraphicsObject): if self.opts['clipToView']: view = self.getViewBox() if view is None or not view.autoRangeEnabled()[0]: - # this option presumes that x-values have uniform spacing + # this option presumes that x-values are in increasing order range = self.viewRect() if range is not None and len(x) > 1: + # clip to visible region extended by downsampling value, assuming + # uniform spacing of x-values, has O(1) performance dx = float(x[-1]-x[0]) / (len(x)-1) - # clip to visible region extended by downsampling value - x0 = np.clip(int((range.left()-x[0])/dx)-1*ds , 0, len(x)-1) - x1 = np.clip(int((range.right()-x[0])/dx)+2*ds , 0, len(x)-1) + x0 = np.clip(int((range.left()-x[0])/dx) - 1*ds, 0, len(x)-1) + x1 = np.clip(int((range.right()-x[0])/dx) + 2*ds, 0, len(x)-1) + + # if data has been clipped too strongly (in case of non-uniform + # spacing of x-values), refine the clipping region as required + # worst case performance: O(log(n)) + # best case performance: O(1) + if x[x0] > range.left(): + x0 = np.searchsorted(x, range.left()) - 1*ds + x0 = np.clip(x0, a_min=0, a_max=len(x)) + if x[x1] < range.right(): + x1 = np.searchsorted(x, range.right()) + 2*ds + x1 = np.clip(x1, a_min=0, a_max=len(x)) + x = x[x0:x1] y = y[x0:x1] diff --git a/pyqtgraph/graphicsItems/PlotItem/PlotItem.py b/pyqtgraph/graphicsItems/PlotItem/PlotItem.py index 9703f286..38a9ba5c 100644 --- a/pyqtgraph/graphicsItems/PlotItem/PlotItem.py +++ b/pyqtgraph/graphicsItems/PlotItem/PlotItem.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- import sys +import warnings import weakref import numpy as np import os @@ -95,7 +96,7 @@ class PlotItem(GraphicsWidget): def __init__(self, parent=None, name=None, labels=None, title=None, viewBox=None, axisItems=None, enableMenu=True, **kargs): """ Create a new PlotItem. All arguments are optional. - Any extra keyword arguments are passed to PlotItem.plot(). + Any extra keyword arguments are passed to :func:`PlotItem.plot() `. ============== ========================================================================================== **Arguments:** @@ -153,20 +154,9 @@ class PlotItem(GraphicsWidget): self.legend = None - ## Create and place axis items - if axisItems is None: - axisItems = {} + # Initialize axis items self.axes = {} - for k, pos in (('top', (1,1)), ('bottom', (3,1)), ('left', (2,0)), ('right', (2,2))): - if k in axisItems: - axis = axisItems[k] - else: - axis = AxisItem(orientation=k, parent=self) - axis.linkToView(self.vb) - self.axes[k] = {'item': axis, 'pos': pos} - self.layout.addItem(axis, *pos) - axis.setZValue(-1000) - axis.setFlag(axis.ItemNegativeZStacksBehindParent) + self.setAxisItems(axisItems) self.titleLabel = LabelItem('', size='11pt', parent=self) self.layout.addItem(self.titleLabel, 0, 1) @@ -254,11 +244,6 @@ class PlotItem(GraphicsWidget): self.ctrl.maxTracesCheck.toggled.connect(self.updateDecimation) self.ctrl.maxTracesSpin.valueChanged.connect(self.updateDecimation) - self.hideAxis('right') - self.hideAxis('top') - self.showAxis('left') - self.showAxis('bottom') - if labels is None: labels = {} for label in list(self.axes.keys()): @@ -300,6 +285,58 @@ class PlotItem(GraphicsWidget): locals()[m] = _create_method(m) del _create_method + + def setAxisItems(self, axisItems=None): + """ + Place axis items as given by `axisItems`. Initializes non-existing axis items. + + ============== ========================================================================================== + **Arguments:** + *axisItems* Optional dictionary instructing the PlotItem to use pre-constructed items + for its axes. The dict keys must be axis names ('left', 'bottom', 'right', 'top') + and the values must be instances of AxisItem (or at least compatible with AxisItem). + ============== ========================================================================================== + """ + + + if axisItems is None: + axisItems = {} + + # Array containing visible axis items + # Also containing potentially hidden axes, but they are not touched so it does not matter + visibleAxes = ['left', 'bottom'] + visibleAxes.append(axisItems.keys()) # Note that it does not matter that this adds + # some values to visibleAxes a second time + + for k, pos in (('top', (1,1)), ('bottom', (3,1)), ('left', (2,0)), ('right', (2,2))): + if k in self.axes: + if k not in axisItems: + continue # Nothing to do here + + # Remove old axis + oldAxis = self.axes[k]['item'] + self.layout.removeItem(oldAxis) + oldAxis.scene().removeItem(oldAxis) + oldAxis.unlinkFromView() + + # Create new axis + if k in axisItems: + axis = axisItems[k] + if axis.scene() is not None: + if axis != self.axes[k]["item"]: + raise RuntimeError("Can't add an axis to multiple plots.") + else: + axis = AxisItem(orientation=k, parent=self) + + # Set up new axis + axis.linkToView(self.vb) + self.axes[k] = {'item': axis, 'pos': pos} + self.layout.addItem(axis, *pos) + axis.setZValue(-1000) + axis.setFlag(axis.ItemNegativeZStacksBehindParent) + + axisVisible = k in visibleAxes + self.showAxis(k, axisVisible) def setLogMode(self, x=None, y=None): """ @@ -478,6 +515,9 @@ class PlotItem(GraphicsWidget): If the item has plot data (PlotDataItem, PlotCurveItem, ScatterPlotItem), it may be included in analysis performed by the PlotItem. """ + if item in self.items: + warnings.warn('Item already added to PlotItem, ignoring.') + return self.items.append(item) vbargs = {} if 'ignoreBounds' in kargs: @@ -563,8 +603,8 @@ class PlotItem(GraphicsWidget): if item in self.dataItems: self.dataItems.remove(item) - if item.scene() is not None: - self.vb.removeItem(item) + self.vb.removeItem(item) + if item in self.curves: self.curves.remove(item) self.updateDecimation() @@ -609,17 +649,20 @@ class PlotItem(GraphicsWidget): return item - def addLegend(self, size=None, offset=(30, 30)): + def addLegend(self, offset=(30, 30), **kwargs): """ - Create a new LegendItem and anchor it over the internal ViewBox. - Plots will be automatically displayed in the legend if they - are created with the 'name' argument. + Create a new :class:`~pyqtgraph.LegendItem` and anchor it over the + internal ViewBox. Plots will be automatically displayed in the legend + if they are created with the 'name' argument. If a LegendItem has already been created using this method, that item will be returned rather than creating a new one. + + Accepts the same arguments as :meth:`~pyqtgraph.LegendItem`. """ + if self.legend is None: - self.legend = LegendItem(size, offset) + self.legend = LegendItem(offset=offset, **kwargs) self.legend.setParentItem(self.vb) return self.legend @@ -677,7 +720,6 @@ class PlotItem(GraphicsWidget): xRange = rect.left(), rect.right() svg = "" - fh = open(fileName, 'w') dx = max(rect.right(),0) - min(rect.left(),0) ymn = min(rect.top(), rect.bottom()) @@ -691,52 +733,68 @@ class PlotItem(GraphicsWidget): sy *= 1000 sy *= -1 - fh.write('\n') - fh.write('\n' % (rect.left()*sx, rect.right()*sx)) - fh.write('\n' % (rect.top()*sy, rect.bottom()*sy)) + with open(fileName, 'w') as fh: + # fh.write('\n' % (rect.left() * sx, + # rect.top() * sx, + # rect.width() * sy, + # rect.height()*sy)) + fh.write('\n') + fh.write('\n' % ( + rect.left() * sx, rect.right() * sx)) + fh.write('\n' % ( + rect.top() * sy, rect.bottom() * sy)) - for item in self.curves: - if isinstance(item, PlotCurveItem): - color = fn.colorStr(item.pen.color()) - opacity = item.pen.color().alpha() / 255. - color = color[:6] - x, y = item.getData() - mask = (x > xRange[0]) * (x < xRange[1]) - mask[:-1] += mask[1:] - m2 = mask.copy() - mask[1:] += m2[:-1] - x = x[mask] - y = y[mask] - - x *= sx - y *= sy - - fh.write('') - - for item in self.dataItems: - if isinstance(item, ScatterPlotItem): - - pRect = item.boundingRect() - vRect = pRect.intersected(rect) - - for point in item.points(): - pos = point.pos() - if not rect.contains(pos): - continue - color = fn.colorStr(point.brush.color()) - opacity = point.brush.color().alpha() / 255. + for item in self.curves: + if isinstance(item, PlotCurveItem): + color = fn.colorStr(item.pen.color()) + opacity = item.pen.color().alpha() / 255. color = color[:6] - x = pos.x() * sx - y = pos.y() * sy - - fh.write('\n' % (x, y, color, opacity)) - - fh.write("\n") - + x, y = item.getData() + mask = (x > xRange[0]) * (x < xRange[1]) + mask[:-1] += mask[1:] + m2 = mask.copy() + mask[1:] += m2[:-1] + x = x[mask] + y = y[mask] + + x *= sx + y *= sy + + # fh.write('\n' % ( + # color, )) + fh.write('') + # fh.write("") + + for item in self.dataItems: + if isinstance(item, ScatterPlotItem): + pRect = item.boundingRect() + vRect = pRect.intersected(rect) + + for point in item.points(): + pos = point.pos() + if not rect.contains(pos): + continue + color = fn.colorStr(point.brush.color()) + opacity = point.brush.color().alpha() / 255. + color = color[:6] + x = pos.x() * sx + y = pos.y() * sy + + fh.write('\n' % ( + x, y, color, opacity)) + + fh.write("\n") + def writeSvg(self, fileName=None): if fileName is None: self._chooseFilenameDialog(handler=self.writeSvg) @@ -766,22 +824,21 @@ class PlotItem(GraphicsWidget): fileName = str(fileName) PlotItem.lastFileDir = os.path.dirname(fileName) - fd = open(fileName, 'w') data = [c.getData() for c in self.curves] - i = 0 - while True: - done = True - for d in data: - if i < len(d[0]): - fd.write('%g,%g,'%(d[0][i], d[1][i])) - done = False - else: - fd.write(' , ,') - fd.write('\n') - if done: - break - i += 1 - fd.close() + with open(fileName, 'w') as fd: + i = 0 + while True: + done = True + for d in data: + if i < len(d[0]): + fd.write('%g,%g,' % (d[0][i], d[1][i])) + done = False + else: + fd.write(' , ,') + fd.write('\n') + if done: + break + i += 1 def saveState(self): state = self.stateGroup.state() @@ -924,15 +981,13 @@ class PlotItem(GraphicsWidget): curves = self.curves[:] split = len(curves) - numCurves - for i in range(len(curves)): - if numCurves == -1 or i >= split: - curves[i].show() - else: + for curve in curves[split:]: + if numCurves != -1: if self.ctrl.forgetTracesCheck.isChecked(): - curves[i].clear() + curve.clear() self.removeItem(curves[i]) else: - curves[i].hide() + curve.hide() def updateAlpha(self, *args): (alpha, auto) = self.alphaState() diff --git a/pyqtgraph/graphicsItems/PlotItem/plotConfigTemplate.ui b/pyqtgraph/graphicsItems/PlotItem/plotConfigTemplate.ui index dffc62d0..12d8033e 100644 --- a/pyqtgraph/graphicsItems/PlotItem/plotConfigTemplate.ui +++ b/pyqtgraph/graphicsItems/PlotItem/plotConfigTemplate.ui @@ -11,7 +11,7 @@ - Form + PyQtGraph diff --git a/pyqtgraph/graphicsItems/PlotItem/plotConfigTemplate_pyqt.py b/pyqtgraph/graphicsItems/PlotItem/plotConfigTemplate_pyqt.py index e09c9978..5ecc0438 100644 --- a/pyqtgraph/graphicsItems/PlotItem/plotConfigTemplate_pyqt.py +++ b/pyqtgraph/graphicsItems/PlotItem/plotConfigTemplate_pyqt.py @@ -148,7 +148,7 @@ class Ui_Form(object): QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): - Form.setWindowTitle(_translate("Form", "Form", None)) + Form.setWindowTitle(_translate("Form", "PyQtGraph", None)) self.averageGroup.setToolTip(_translate("Form", "Display averages of the curves displayed in this plot. The parameter list allows you to choose parameters to average over (if any are available).", None)) self.averageGroup.setTitle(_translate("Form", "Average", None)) self.clipToViewCheck.setToolTip(_translate("Form", "Plot only the portion of each curve that is visible. This assumes X values are uniformly spaced.", None)) diff --git a/pyqtgraph/graphicsItems/PlotItem/plotConfigTemplate_pyqt5.py b/pyqtgraph/graphicsItems/PlotItem/plotConfigTemplate_pyqt5.py index e9fdff24..817221f2 100644 --- a/pyqtgraph/graphicsItems/PlotItem/plotConfigTemplate_pyqt5.py +++ b/pyqtgraph/graphicsItems/PlotItem/plotConfigTemplate_pyqt5.py @@ -135,7 +135,7 @@ class Ui_Form(object): def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate - Form.setWindowTitle(_translate("Form", "Form")) + Form.setWindowTitle(_translate("Form", "PyQtGraph")) self.averageGroup.setToolTip(_translate("Form", "Display averages of the curves displayed in this plot. The parameter list allows you to choose parameters to average over (if any are available).")) self.averageGroup.setTitle(_translate("Form", "Average")) self.clipToViewCheck.setToolTip(_translate("Form", "Plot only the portion of each curve that is visible. This assumes X values are uniformly spaced.")) diff --git a/pyqtgraph/graphicsItems/PlotItem/plotConfigTemplate_pyside.py b/pyqtgraph/graphicsItems/PlotItem/plotConfigTemplate_pyside.py index aff31211..d0fd1edd 100644 --- a/pyqtgraph/graphicsItems/PlotItem/plotConfigTemplate_pyside.py +++ b/pyqtgraph/graphicsItems/PlotItem/plotConfigTemplate_pyside.py @@ -134,7 +134,7 @@ class Ui_Form(object): QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): - Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8)) + Form.setWindowTitle(QtGui.QApplication.translate("Form", "PyQtGraph", None, QtGui.QApplication.UnicodeUTF8)) self.averageGroup.setToolTip(QtGui.QApplication.translate("Form", "Display averages of the curves displayed in this plot. The parameter list allows you to choose parameters to average over (if any are available).", None, QtGui.QApplication.UnicodeUTF8)) self.averageGroup.setTitle(QtGui.QApplication.translate("Form", "Average", None, QtGui.QApplication.UnicodeUTF8)) self.clipToViewCheck.setToolTip(QtGui.QApplication.translate("Form", "Plot only the portion of each curve that is visible. This assumes X values are uniformly spaced.", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/pyqtgraph/graphicsItems/ROI.py b/pyqtgraph/graphicsItems/ROI.py index fafb5592..c4b29669 100644 --- a/pyqtgraph/graphicsItems/ROI.py +++ b/pyqtgraph/graphicsItems/ROI.py @@ -2,7 +2,7 @@ """ ROI.py - Interactive graphics items for GraphicsView (ROI widgets) Copyright 2010 Luke Campagnola -Distributed under MIT/X11 license. See license.txt for more infomation. +Distributed under MIT/X11 license. See license.txt for more information. Implements a series of graphics items which display movable/scalable/rotatable shapes for use as region-of-interest markers. ROI class automatically handles extraction @@ -590,13 +590,13 @@ class ROI(GraphicsObject): ## If a Handle was not supplied, create it now if 'item' not in info or info['item'] is None: h = Handle(self.handleSize, typ=info['type'], pen=self.handlePen, parent=self) - h.setPos(info['pos'] * self.state['size']) info['item'] = h else: h = info['item'] if info['pos'] is None: info['pos'] = h.pos() - + h.setPos(info['pos'] * self.state['size']) + ## connect the handle to this ROI #iid = len(self.handles) h.connectROI(self) @@ -758,6 +758,9 @@ class ROI(GraphicsObject): remAct.triggered.connect(self.removeClicked) self.menu.addAction(remAct) self.menu.remAct = remAct + # ROI menu may be requested when showing the handle context menu, so + # return the menu but disable it if the ROI isn't removable + self.menu.setEnabled(self.contextMenuEnabled()) return self.menu def removeClicked(self): @@ -1270,11 +1273,12 @@ class Handle(UIGraphicsItem): sigClicked = QtCore.Signal(object, object) # self, event sigRemoveRequested = QtCore.Signal(object) # self - def __init__(self, radius, typ=None, pen=(200, 200, 220), parent=None, deletable=False): + def __init__(self, radius, typ=None, pen=(200, 200, 220), parent=None, deletable=False, activePen=(255, 255, 0)): self.rois = [] self.radius = radius self.typ = typ self.pen = fn.mkPen(pen) + self.activePen = fn.mkPen(activePen) self.currentPen = self.pen self.pen.setWidth(0) self.pen.setCosmetic(True) @@ -1318,7 +1322,7 @@ class Handle(UIGraphicsItem): hover=True if hover: - self.currentPen = fn.mkPen(255, 255,0) + self.currentPen = self.activePen else: self.currentPen = self.pen self.update() @@ -1371,15 +1375,19 @@ class Handle(UIGraphicsItem): for r in self.rois: r.stateChangeFinished() self.isMoving = False + self.currentPen = self.pen + self.update() elif ev.isStart(): for r in self.rois: r.handleMoveStarted() self.isMoving = True self.startPos = self.scenePos() self.cursorOffset = self.scenePos() - ev.buttonDownScenePos() + self.currentPen = self.activePen if self.isMoving: ## note: isMoving may become False in mid-drag due to right-click. pos = ev.scenePos() + self.cursorOffset + self.currentPen = self.activePen self.movePoint(pos, ev.modifiers(), finish=False) def movePoint(self, pos, modifiers=QtCore.Qt.KeyboardModifier(), finish=True): @@ -2113,6 +2121,23 @@ class LineSegmentROI(ROI): def listPoints(self): return [p['item'].pos() for p in self.handles] + + def getState(self): + state = ROI.getState(self) + state['points'] = [Point(h.pos()) for h in self.getHandles()] + return state + + def saveState(self): + state = ROI.saveState(self) + state['points'] = [tuple(h.pos()) for h in self.getHandles()] + return state + + def setState(self, state): + ROI.setState(self, state) + p1 = [state['points'][0][0]+state['pos'][0], state['points'][0][1]+state['pos'][1]] + p2 = [state['points'][1][0]+state['pos'][0], state['points'][1][1]+state['pos'][1]] + self.movePoint(self.getHandles()[0], p1, finish=False) + self.movePoint(self.getHandles()[1], p2) def paint(self, p, *args): p.setRenderHint(QtGui.QPainter.Antialiasing) diff --git a/pyqtgraph/graphicsItems/ScatterPlotItem.py b/pyqtgraph/graphicsItems/ScatterPlotItem.py index 67fafd83..63dc61cb 100644 --- a/pyqtgraph/graphicsItems/ScatterPlotItem.py +++ b/pyqtgraph/graphicsItems/ScatterPlotItem.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- from itertools import starmap, repeat try: from itertools import imap @@ -15,11 +16,14 @@ from ..pgcollections import OrderedDict from .. import debug from ..python2_3 import basestring + __all__ = ['ScatterPlotItem', 'SpotItem'] ## Build all symbol paths -Symbols = OrderedDict([(name, QtGui.QPainterPath()) for name in ['o', 's', 't', 't1', 't2', 't3','d', '+', 'x', 'p', 'h', 'star']]) +name_list = ['o', 's', 't', 't1', 't2', 't3', 'd', '+', 'x', 'p', 'h', 'star', + 'arrow_up', 'arrow_right', 'arrow_down', 'arrow_left'] +Symbols = OrderedDict([(name, QtGui.QPainterPath()) for name in name_list]) Symbols['o'].addEllipse(QtCore.QRectF(-0.5, -0.5, 1, 1)) Symbols['s'].addRect(QtCore.QRectF(-0.5, -0.5, 1, 1)) coords = { @@ -40,7 +44,11 @@ coords = { 'star': [(0, -0.5), (-0.1123, -0.1545), (-0.4755, -0.1545), (-0.1816, 0.059), (-0.2939, 0.4045), (0, 0.1910), (0.2939, 0.4045), (0.1816, 0.059), (0.4755, -0.1545), - (0.1123, -0.1545)] + (0.1123, -0.1545)], + 'arrow_down': [ + (-0.125, 0.125), (0, 0), (0.125, 0.125), + (0.05, 0.125), (0.05, 0.5), (-0.05, 0.5), (-0.05, 0.125) + ] } for k, c in coords.items(): Symbols[k].moveTo(*c[0]) @@ -50,7 +58,10 @@ for k, c in coords.items(): tr = QtGui.QTransform() tr.rotate(45) Symbols['x'] = tr.map(Symbols['+']) - +tr.rotate(45) +Symbols['arrow_right'] = tr.map(Symbols['arrow_down']) +Symbols['arrow_up'] = tr.map(Symbols['arrow_right']) +Symbols['arrow_left'] = tr.map(Symbols['arrow_up']) def drawSymbol(painter, symbol, size, pen, brush): if symbol is None: @@ -122,26 +133,39 @@ class SymbolAtlas(object): """ Given a list of spot records, return an object representing the coordinates of that symbol within the atlas """ - sourceRect = np.empty(len(opts), dtype=object) + + sourceRect = [] keyi = None sourceRecti = None - for i, rec in enumerate(opts): - key = (id(rec[3]), rec[2], id(rec[4]), id(rec[5])) # TODO: use string indexes? + symbol_map = self.symbolMap + + symbols = opts['symbol'].tolist() + sizes = opts['size'].tolist() + pens = opts['pen'].tolist() + brushes = opts['brush'].tolist() + + for symbol, size, pen, brush in zip(symbols, sizes, pens, brushes): + + key = id(symbol), size, id(pen), id(brush) if key == keyi: - sourceRect[i] = sourceRecti + sourceRect.append(sourceRecti) else: try: - sourceRect[i] = self.symbolMap[key] + sourceRect.append(symbol_map[key]) except KeyError: newRectSrc = QtCore.QRectF() - newRectSrc.pen = rec['pen'] - newRectSrc.brush = rec['brush'] - newRectSrc.symbol = rec[3] - self.symbolMap[key] = newRectSrc + newRectSrc.pen = pen + newRectSrc.brush = brush + newRectSrc.symbol = symbol + + symbol_map[key] = newRectSrc self.atlasValid = False - sourceRect[i] = newRectSrc + sourceRect.append(newRectSrc) + keyi = key sourceRecti = newRectSrc + + sourceRect = np.array(sourceRect, dtype=object) return sourceRect def buildAtlas(self): @@ -243,8 +267,8 @@ class ScatterPlotItem(GraphicsObject): self.picture = None # QPicture used for rendering when pxmode==False self.fragmentAtlas = SymbolAtlas() - - self.data = np.empty(0, dtype=[('x', float), ('y', float), ('size', float), ('symbol', object), ('pen', object), ('brush', object), ('data', object), ('item', object), ('sourceRect', object), ('targetRect', object), ('width', float)]) + + self.data = np.empty(0, dtype=[('x', float), ('y', float), ('size', float), ('symbol', object), ('pen', object), ('brush', object), ('data', object), ('item', object), ('sourceRect', object), ('targetRect', object), ('width', float), ('visible', bool)]) self.bounds = [None, None] ## caches data bounds self._maxSpotWidth = 0 ## maximum size of the scale-variant portion of all spots self._maxSpotPxWidth = 0 ## maximum size of the scale-invariant portion of all spots @@ -355,6 +379,9 @@ class ScatterPlotItem(GraphicsObject): kargs['y'] = [] numPts = 0 + ## Clear current SpotItems since the data references they contain will no longer be current + self.data['item'][...] = None + ## Extend record array oldData = self.data self.data = np.empty(len(oldData)+numPts, dtype=self.data.dtype) @@ -366,6 +393,7 @@ class ScatterPlotItem(GraphicsObject): newData = self.data[len(oldData):] newData['size'] = -1 ## indicates to use default size + newData['visible'] = True if 'spots' in kargs: spots = kargs['spots'] @@ -525,6 +553,28 @@ class ScatterPlotItem(GraphicsObject): if update: self.updateSpots(dataSet) + + def setPointsVisible(self, visible, update=True, dataSet=None, mask=None): + """Set whether or not each spot is visible. + If a list or array is provided, then the visibility for each spot will be set separately. + Otherwise, the argument will be used for all spots.""" + if dataSet is None: + dataSet = self.data + + if isinstance(visible, np.ndarray) or isinstance(visible, list): + visibilities = visible + if mask is not None: + visibilities = visibilities[mask] + if len(visibilities) != len(dataSet): + raise Exception("Number of visibilities does not match number of points (%d != %d)" % (len(visibilities), len(dataSet))) + dataSet['visible'] = visibilities + else: + dataSet['visible'] = visible + + dataSet['sourceRect'] = None + if update: + self.updateSpots(dataSet) + def setPointData(self, data, dataSet=None, mask=None): if dataSet is None: dataSet = self.data @@ -551,6 +601,7 @@ class ScatterPlotItem(GraphicsObject): self.invalidate() def updateSpots(self, dataSet=None): + if dataSet is None: dataSet = self.data @@ -601,8 +652,6 @@ class ScatterPlotItem(GraphicsObject): recs['brush'][np.equal(recs['brush'], None)] = fn.mkBrush(self.opts['brush']) return recs - - def measureSpotSizes(self, dataSet): for rec in dataSet: ## keep track of the maximum spot size and pixel size @@ -621,7 +670,6 @@ class ScatterPlotItem(GraphicsObject): self._maxSpotPxWidth = max(self._maxSpotPxWidth, pxWidth) self.bounds = [None, None] - def clear(self): """Remove all spots from the scatter plot""" #self.clearItems() @@ -728,6 +776,8 @@ class ScatterPlotItem(GraphicsObject): (pts[0] - w < viewBounds.right()) & (pts[1] + w > viewBounds.top()) & (pts[1] - w < viewBounds.bottom())) ## remove out of view points + + mask &= self.data['visible'] return mask @debug.warnOnException ## raising an exception here causes crash @@ -748,8 +798,10 @@ class ScatterPlotItem(GraphicsObject): if self.opts['pxMode'] is True: p.resetTransform() + data = self.data + # Map point coordinates to device - pts = np.vstack([self.data['x'], self.data['y']]) + pts = np.vstack([data['x'], data['y']]) pts = self.mapPointsToDevice(pts) if pts is None: return @@ -761,27 +813,33 @@ class ScatterPlotItem(GraphicsObject): # Draw symbols from pre-rendered atlas atlas = self.fragmentAtlas.getAtlas() + target_rect = data['targetRect'] + source_rect = data['sourceRect'] + widths = data['width'] + # Update targetRects if necessary - updateMask = viewMask & np.equal(self.data['targetRect'], None) + updateMask = viewMask & np.equal(target_rect, None) if np.any(updateMask): updatePts = pts[:,updateMask] - width = self.data[updateMask]['width']*2 - self.data['targetRect'][updateMask] = list(imap(QtCore.QRectF, updatePts[0,:], updatePts[1,:], width, width)) + width = widths[updateMask] * 2 + target_rect[updateMask] = list(imap(QtCore.QRectF, updatePts[0,:], updatePts[1,:], width, width)) - data = self.data[viewMask] if QT_LIB == 'PyQt4': - p.drawPixmapFragments(data['targetRect'].tolist(), data['sourceRect'].tolist(), atlas) + p.drawPixmapFragments( + target_rect[viewMask].tolist(), + source_rect[viewMask].tolist(), + atlas + ) else: - list(imap(p.drawPixmap, data['targetRect'], repeat(atlas), data['sourceRect'])) + list(imap(p.drawPixmap, target_rect[viewMask].tolist(), repeat(atlas), source_rect[viewMask].tolist())) else: # render each symbol individually p.setRenderHint(p.Antialiasing, aa) - data = self.data[viewMask] pts = pts[:,viewMask] - for i, rec in enumerate(data): + for i, rec in enumerate(data[viewMask]): p.resetTransform() - p.translate(pts[0,i] + rec['width'], pts[1,i] + rec['width']) + p.translate(pts[0,i] + rec['width']/2, pts[1,i] + rec['width']/2) drawSymbol(p, *self.getSpotOpts(rec, scale)) else: if self.picture is None: @@ -945,6 +1003,15 @@ class SpotItem(object): self._data['brush'] = None ## Note this is NOT the same as calling setBrush(None) self.updateItem() + + def isVisible(self): + return self._data['visible'] + + def setVisible(self, visible): + """Set whether or not this spot is visible.""" + self._data['visible'] = visible + self.updateItem() + def setData(self, data): """Set the user-data associated with this spot""" self._data['data'] = data diff --git a/pyqtgraph/graphicsItems/TextItem.py b/pyqtgraph/graphicsItems/TextItem.py index b2587ded..9dc17960 100644 --- a/pyqtgraph/graphicsItems/TextItem.py +++ b/pyqtgraph/graphicsItems/TextItem.py @@ -110,9 +110,16 @@ class TextItem(GraphicsObject): self.updateTextPos() def setAngle(self, angle): + """ + Set the angle of the text in degrees. + + This sets the rotation angle of the text as a whole, measured + counter-clockwise from the x axis of the parent. Note that this rotation + angle does not depend on horizontal/vertical scaling of the parent. + """ self.angle = angle - self.updateTransform() - + self.updateTransform(force=True) + def setAnchor(self, anchor): self.anchor = Point(anchor) self.updateTextPos() @@ -169,7 +176,7 @@ class TextItem(GraphicsObject): p.setRenderHint(p.Antialiasing, True) p.drawPolygon(self.textItem.mapToParent(self.textItem.boundingRect())) - def updateTransform(self): + def updateTransform(self, force=False): # update transform such that this item has the correct orientation # and scaling relative to the scene, but inherits its position from its # parent. @@ -181,7 +188,7 @@ class TextItem(GraphicsObject): else: pt = p.sceneTransform() - if pt == self._lastTransform: + if not force and pt == self._lastTransform: return t = pt.inverted()[0] diff --git a/pyqtgraph/graphicsItems/ViewBox/ViewBox.py b/pyqtgraph/graphicsItems/ViewBox/ViewBox.py index 27fb8268..b08757af 100644 --- a/pyqtgraph/graphicsItems/ViewBox/ViewBox.py +++ b/pyqtgraph/graphicsItems/ViewBox/ViewBox.py @@ -1,9 +1,10 @@ +# -*- coding: utf-8 -*- import weakref import sys from copy import deepcopy import numpy as np from ...Qt import QtGui, QtCore -from ...python2_3 import sortList, basestring, cmp +from ...python2_3 import basestring from ...Point import Point from ... import functions as fn from .. ItemGroup import ItemGroup @@ -14,6 +15,7 @@ from ...Qt import isQObjectAlive __all__ = ['ViewBox'] + class WeakList(object): def __init__(self): @@ -34,10 +36,12 @@ class WeakList(object): yield d i -= 1 + class ChildGroup(ItemGroup): def __init__(self, parent): ItemGroup.__init__(self, parent) + self.setFlag(self.ItemClipsChildrenToShape) # Used as callback to inform ViewBox when items are added/removed from # the group. @@ -64,6 +68,12 @@ class ChildGroup(ItemGroup): listener.itemsChanged() return ret + def shape(self): + return self.mapFromParent(self.parentItem().shape()) + + def boundingRect(self): + return self.mapRectFromParent(self.parentItem().boundingRect()) + class ViewBox(GraphicsWidget): """ @@ -185,6 +195,13 @@ class ViewBox(GraphicsWidget): self.background.setPen(fn.mkPen(None)) self.updateBackground() + self.border = fn.mkPen(border) + + self.borderRect = QtGui.QGraphicsRectItem(self.rect()) + self.borderRect.setParentItem(self) + self.borderRect.setZValue(1e3) + self.borderRect.setPen(self.border) + ## Make scale box that is shown when dragging on the view self.rbScaleBox = QtGui.QGraphicsRectItem(0, 0, 1, 1) self.rbScaleBox.setPen(fn.mkPen((255,255,100), width=1)) @@ -207,7 +224,6 @@ class ViewBox(GraphicsWidget): self.setAspectLocked(lockAspect) - self.border = fn.mkPen(border) if enableMenu: self.menu = ViewBoxMenu(self) else: @@ -217,6 +233,17 @@ class ViewBox(GraphicsWidget): if name is None: self.updateViewLists() + def getAspectRatio(self): + '''return the current aspect ratio''' + rect = self.rect() + vr = self.viewRect() + if rect.height() == 0 or vr.width() == 0 or vr.height() == 0: + currentRatio = 1.0 + else: + currentRatio = (rect.width()/float(rect.height())) / ( + vr.width()/vr.height()) + return currentRatio + def register(self, name): """ Add this ViewBox to the registered list of views. @@ -267,22 +294,9 @@ class ViewBox(GraphicsWidget): #scene.sigPrepareForPaint.connect(self.prepareForPaint) #return ret - def checkSceneChange(self): - # ViewBox needs to receive sigPrepareForPaint from its scene before - # being painted. However, we have no way of being informed when the - # scene has changed in order to make this connection. The usual way - # to do this is via itemChange(), but bugs prevent this approach - # (see above). Instead, we simply check at every paint to see whether - # (the scene has changed. - scene = self.scene() - if scene == self._lastScene: - return - if self._lastScene is not None and hasattr(self._lastScene, 'sigPrepareForPaint'): - self._lastScene.sigPrepareForPaint.disconnect(self.prepareForPaint) - if scene is not None and hasattr(scene, 'sigPrepareForPaint'): - scene.sigPrepareForPaint.connect(self.prepareForPaint) + def update(self, *args, **kwargs): self.prepareForPaint() - self._lastScene = scene + GraphicsWidget.update(self, *args, **kwargs) def prepareForPaint(self): #autoRangeEnabled = (self.state['autoRange'][0] is not False) or (self.state['autoRange'][1] is not False) @@ -319,12 +333,7 @@ class ViewBox(GraphicsWidget): self.state.update(state) - if self.state['enableMenu'] and self.menu is None: - self.menu = ViewBoxMenu(self) - self.updateViewLists() - else: - self.menu = None - + self._applyMenuEnabled() self.updateViewRange() self.sigStateChanged.emit(self) @@ -378,18 +387,21 @@ class ViewBox(GraphicsWidget): def setMenuEnabled(self, enableMenu=True): self.state['enableMenu'] = enableMenu - if enableMenu: - if self.menu is None: - self.menu = ViewBoxMenu(self) - self.updateViewLists() - else: - self.menu.setParent(None) - self.menu = None + self._applyMenuEnabled() self.sigStateChanged.emit(self) def menuEnabled(self): return self.state.get('enableMenu', True) + def _applyMenuEnabled(self): + enableMenu = self.state.get("enableMenu", True) + if enableMenu and self.menu is None: + self.menu = ViewBoxMenu(self) + self.updateViewLists() + elif not enableMenu and self.menu is not None: + self.menu.setParent(None) + self.menu = None + def addItem(self, item, ignoreBounds=False): """ Add a QGraphicsItem to this view. The view will include this item when determining how to set its range @@ -397,10 +409,12 @@ class ViewBox(GraphicsWidget): """ if item.zValue() < self.zValue(): item.setZValue(self.zValue()+1) + scene = self.scene() if scene is not None and scene is not item.scene(): scene.addItem(item) ## Necessary due to Qt bug: https://bugreports.qt-project.org/browse/QTBUG-18616 item.setParentItem(self.childGroup) + if not ignoreBounds: self.addedItems.append(item) self.updateAutoRange() @@ -411,7 +425,12 @@ class ViewBox(GraphicsWidget): self.addedItems.remove(item) except: pass - self.scene().removeItem(item) + + scene = self.scene() + if scene is not None: + scene.removeItem(item) + item.setParentItem(None) + self.updateAutoRange() def clear(self): @@ -422,14 +441,23 @@ class ViewBox(GraphicsWidget): def resizeEvent(self, ev): self._matrixNeedsUpdate = True + self.updateMatrix() + self.linkedXChanged() self.linkedYChanged() + self.updateAutoRange() self.updateViewRange() + self._matrixNeedsUpdate = True + self.updateMatrix() + self.background.setRect(self.rect()) + self.borderRect.setRect(self.rect()) + self.sigStateChanged.emit(self) self.sigResized.emit(self) + self.childGroup.prepareGeometryChange() def viewRange(self): """Return a the view's visible range as a list: [[xmin, xmax], [ymin, ymax]]""" @@ -512,6 +540,18 @@ class ViewBox(GraphicsWidget): # Update axes one at a time changed = [False, False] + + # Disable auto-range for each axis that was requested to be set + if disableAutoRange: + xOff = False if setRequested[0] else None + yOff = False if setRequested[1] else None + self.enableAutoRange(x=xOff, y=yOff) + changed.append(True) + + limits = (self.state['limits']['xLimits'], self.state['limits']['yLimits']) + minRng = [self.state['limits']['xRange'][0], self.state['limits']['yRange'][0]] + maxRng = [self.state['limits']['xRange'][1], self.state['limits']['yRange'][1]] + for ax, range in changes.items(): mn = min(range) mx = max(range) @@ -539,6 +579,39 @@ class ViewBox(GraphicsWidget): mn -= p mx += p + # max range cannot be larger than bounds, if they are given + if limits[ax][0] is not None and limits[ax][1] is not None: + if maxRng[ax] is not None: + maxRng[ax] = min(maxRng[ax], limits[ax][1] - limits[ax][0]) + else: + maxRng[ax] = limits[ax][1] - limits[ax][0] + + # If we have limits, we will have at least a max range as well + if maxRng[ax] is not None or minRng[ax] is not None: + diff = mx - mn + if maxRng[ax] is not None and diff > maxRng[ax]: + delta = maxRng[ax] - diff + elif minRng[ax] is not None and diff < minRng[ax]: + delta = minRng[ax] - diff + else: + delta = 0 + + mn -= delta / 2. + mx += delta / 2. + + # Make sure our requested area is within limits, if any + if limits[ax][0] is not None or limits[ax][1] is not None: + lmn, lmx = limits[ax] + if lmn is not None and mn < lmn: + delta = lmn - mn # Shift the requested view to match our lower limit + mn = lmn + mx += delta + elif lmx is not None and mx > lmx: + delta = lmx - mx + mx = lmx + mn += delta + + # Set target range if self.state['targetRange'][ax] != [mn, mx]: self.state['targetRange'][ax] = [mn, mx] @@ -552,13 +625,6 @@ class ViewBox(GraphicsWidget): lockY = False self.updateViewRange(lockX, lockY) - # Disable auto-range for each axis that was requested to be set - if disableAutoRange: - xOff = False if setRequested[0] else None - yOff = False if setRequested[1] else None - self.enableAutoRange(x=xOff, y=yOff) - changed.append(True) - # If nothing has changed, we are done. if any(changed): # Update target rect for debugging @@ -571,7 +637,7 @@ class ViewBox(GraphicsWidget): self._autoRangeNeedsUpdate = True elif changed[1] and self.state['autoVisibleOnly'][0] and (self.state['autoRange'][1] is not False): self._autoRangeNeedsUpdate = True - + self.sigStateChanged.emit(self) def setYRange(self, min, max, padding=None, update=True): @@ -1054,6 +1120,19 @@ class ViewBox(GraphicsWidget): def xInverted(self): return self.state['xInverted'] + def setBorder(self, *args, **kwds): + """ + Set the pen used to draw border around the view + + If border is None, then no border will be drawn. + + Added in version 0.9.10 + + See :func:`mkPen ` for arguments. + """ + self.border = fn.mkPen(*args, **kwds) + self.borderRect.setPen(self.border) + def setAspectLocked(self, lock=True, ratio=1): """ If the aspect ratio is locked, view scaling must always preserve the aspect ratio. @@ -1066,12 +1145,7 @@ class ViewBox(GraphicsWidget): return self.state['aspectLocked'] = False else: - rect = self.rect() - vr = self.viewRect() - if rect.height() == 0 or vr.width() == 0 or vr.height() == 0: - currentRatio = 1.0 - else: - currentRatio = (rect.width()/float(rect.height())) / (vr.width()/vr.height()) + currentRatio = self.getAspectRatio() if ratio is None: ratio = currentRatio if self.state['aspectLocked'] == ratio: # nothing to change @@ -1206,8 +1280,10 @@ class ViewBox(GraphicsWidget): ## update shape of scale box self.updateScaleBox(ev.buttonDownPos(), ev.pos()) else: - tr = dif*mask - tr = self.mapToView(tr) - self.mapToView(Point(0,0)) + tr = self.childGroup.transform() + tr = fn.invertQTransform(tr) + tr = tr.map(dif*mask) - tr.map(Point(0,0)) + x = tr.x() if mask[0] == 1 else None y = tr.y() if mask[1] == 1 else None @@ -1272,8 +1348,11 @@ class ViewBox(GraphicsWidget): self.rbScaleBox.scale(r.width(), r.height()) self.rbScaleBox.show() - def showAxRect(self, ax): - self.setRange(ax.normalized()) # be sure w, h are correct coordinates + def showAxRect(self, ax, **kwargs): + """Set the visible range to the given rectangle + Passes keyword arguments to setRange + """ + self.setRange(ax.normalized(), **kwargs) # be sure w, h are correct coordinates self.sigRangeChangedManually.emit(self.state['mouseEnabled']) def allChildren(self, item=None): @@ -1409,40 +1488,6 @@ class ViewBox(GraphicsWidget): aspect = self.state['aspectLocked'] # size ratio / view ratio tr = self.targetRect() bounds = self.rect() - if aspect is not False and 0 not in [aspect, tr.height(), bounds.height(), bounds.width()]: - - ## This is the view range aspect ratio we have requested - targetRatio = tr.width() / tr.height() if tr.height() != 0 else 1 - ## This is the view range aspect ratio we need to obey aspect constraint - viewRatio = (bounds.width() / bounds.height() if bounds.height() != 0 else 1) / aspect - viewRatio = 1 if viewRatio == 0 else viewRatio - - # Decide which range to keep unchanged - #print self.name, "aspect:", aspect, "changed:", changed, "auto:", self.state['autoRange'] - if forceX: - ax = 0 - elif forceY: - ax = 1 - else: - # if we are not required to keep a particular axis unchanged, - # then make the entire target range visible - ax = 0 if targetRatio > viewRatio else 1 - - if ax == 0: - ## view range needs to be taller than target - dy = 0.5 * (tr.width() / viewRatio - tr.height()) - if dy != 0: - changed[1] = True - viewRange[1] = [self.state['targetRange'][1][0] - dy, self.state['targetRange'][1][1] + dy] - else: - ## view range needs to be wider than target - dx = 0.5 * (tr.height() * viewRatio - tr.width()) - if dx != 0: - changed[0] = True - viewRange[0] = [self.state['targetRange'][0][0] - dx, self.state['targetRange'][0][1] + dx] - - - # ----------- Make corrections for view limits ----------- limits = (self.state['limits']['xLimits'], self.state['limits']['yLimits']) minRng = [self.state['limits']['xRange'][0], self.state['limits']['yRange'][0]] @@ -1455,43 +1500,58 @@ class ViewBox(GraphicsWidget): # max range cannot be larger than bounds, if they are given if limits[axis][0] is not None and limits[axis][1] is not None: if maxRng[axis] is not None: - maxRng[axis] = min(maxRng[axis], limits[axis][1]-limits[axis][0]) + maxRng[axis] = min(maxRng[axis], limits[axis][1] - limits[axis][0]) else: - maxRng[axis] = limits[axis][1]-limits[axis][0] + maxRng[axis] = limits[axis][1] - limits[axis][0] - #print "\nLimits for axis %d: range=%s min=%s max=%s" % (axis, limits[axis], minRng[axis], maxRng[axis]) - #print "Starting range:", viewRange[axis] + if aspect is not False and 0 not in [aspect, tr.height(), bounds.height(), bounds.width()]: - # Apply xRange, yRange - diff = viewRange[axis][1] - viewRange[axis][0] - if maxRng[axis] is not None and diff > maxRng[axis]: - delta = maxRng[axis] - diff - changed[axis] = True - elif minRng[axis] is not None and diff < minRng[axis]: - delta = minRng[axis] - diff - changed[axis] = True + ## This is the view range aspect ratio we have requested + targetRatio = tr.width() / tr.height() if tr.height() != 0 else 1 + ## This is the view range aspect ratio we need to obey aspect constraint + viewRatio = (bounds.width() / bounds.height() if bounds.height() != 0 else 1) / aspect + viewRatio = 1 if viewRatio == 0 else viewRatio + + # Calculate both the x and y ranges that would be needed to obtain the desired aspect ratio + dy = 0.5 * (tr.width() / viewRatio - tr.height()) + dx = 0.5 * (tr.height() * viewRatio - tr.width()) + + rangeY = [self.state['targetRange'][1][0] - dy, self.state['targetRange'][1][1] + dy] + rangeX = [self.state['targetRange'][0][0] - dx, self.state['targetRange'][0][1] + dx] + + canidateRange = [rangeX, rangeY] + + # Decide which range to try to keep unchanged + #print self.name, "aspect:", aspect, "changed:", changed, "auto:", self.state['autoRange'] + if forceX: + ax = 0 + elif forceY: + ax = 1 else: - delta = 0 + # if we are not required to keep a particular axis unchanged, + # then try to make the entire target range visible + ax = 0 if targetRatio > viewRatio else 1 + target = 0 if ax == 1 else 1 + # See if this choice would cause out-of-range issues + if maxRng is not None or minRng is not None: + diff = canidateRange[target][1] - canidateRange[target][0] + if maxRng[target] is not None and diff > maxRng[target] or \ + minRng[target] is not None and diff < minRng[target]: + # tweak the target range down so we can still pan properly + self.state['targetRange'][ax] = canidateRange[ax] + ax = target # Switch the "fixed" axes - viewRange[axis][0] -= delta/2. - viewRange[axis][1] += delta/2. + if ax == 0: + ## view range needs to be taller than target + if dy != 0: + changed[1] = True + viewRange[1] = rangeY + else: + ## view range needs to be wider than target + if dx != 0: + changed[0] = True + viewRange[0] = rangeX - #print "after applying min/max:", viewRange[axis] - - # Apply xLimits, yLimits - mn, mx = limits[axis] - if mn is not None and viewRange[axis][0] < mn: - delta = mn - viewRange[axis][0] - viewRange[axis][0] += delta - viewRange[axis][1] += delta - changed[axis] = True - elif mx is not None and viewRange[axis][1] > mx: - delta = mx - viewRange[axis][1] - viewRange[axis][0] += delta - viewRange[axis][1] += delta - changed[axis] = True - - #print "after applying edge limits:", viewRange[axis] changed = [(viewRange[i][0] != self.state['viewRange'][i][0]) or (viewRange[i][1] != self.state['viewRange'][i][1]) for i in (0,1)] self.state['viewRange'] = viewRange @@ -1499,7 +1559,7 @@ class ViewBox(GraphicsWidget): if any(changed): self._matrixNeedsUpdate = True self.update() - + # Inform linked views that the range has changed for ax in [0, 1]: if not changed[ax]: @@ -1518,7 +1578,6 @@ class ViewBox(GraphicsWidget): def updateMatrix(self, changed=None): if not self._matrixNeedsUpdate: return - ## Make the childGroup's transform match the requested viewRange. bounds = self.rect() @@ -1547,8 +1606,6 @@ class ViewBox(GraphicsWidget): self.sigTransformChanged.emit(self) ## segfaults here: 1 def paint(self, p, opt, widget): - self.checkSceneChange() - if self.border is not None: bounds = self.shape() p.setPen(self.border) @@ -1575,14 +1632,11 @@ class ViewBox(GraphicsWidget): except RuntimeError: ## this view has already been deleted; it will probably be collected shortly. return - def cmpViews(a, b): - wins = 100 * cmp(a.window() is self.window(), b.window() is self.window()) - alpha = cmp(a.name, b.name) - return wins + alpha + def view_key(view): + return (view.window() is self.window(), view.name) ## make a sorted list of all named views - nv = list(ViewBox.NamedViews.values()) - sortList(nv, cmpViews) ## see pyqtgraph.python2_3.sortList + nv = sorted(ViewBox.NamedViews.values(), key=view_key) if self in nv: nv.remove(self) diff --git a/pyqtgraph/graphicsItems/ViewBox/ViewBoxMenu.py b/pyqtgraph/graphicsItems/ViewBox/ViewBoxMenu.py index 74a861d0..1f44bdd6 100644 --- a/pyqtgraph/graphicsItems/ViewBox/ViewBoxMenu.py +++ b/pyqtgraph/graphicsItems/ViewBox/ViewBoxMenu.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- from ...Qt import QtCore, QtGui, QT_LIB from ...python2_3 import asUnicode from ...WidgetGroup import WidgetGroup @@ -48,8 +49,8 @@ class ViewBoxMenu(QtGui.QMenu): connects = [ (ui.mouseCheck.toggled, 'MouseToggled'), (ui.manualRadio.clicked, 'ManualClicked'), - (ui.minText.editingFinished, 'MinTextChanged'), - (ui.maxText.editingFinished, 'MaxTextChanged'), + (ui.minText.editingFinished, 'RangeTextChanged'), + (ui.maxText.editingFinished, 'RangeTextChanged'), (ui.autoRadio.clicked, 'AutoClicked'), (ui.autoPercentSpin.valueChanged, 'AutoSpinChanged'), (ui.linkCombo.currentIndexChanged, 'LinkComboChanged'), @@ -162,14 +163,10 @@ class ViewBoxMenu(QtGui.QMenu): def xManualClicked(self): self.view().enableAutoRange(ViewBox.XAxis, False) - def xMinTextChanged(self): + def xRangeTextChanged(self): self.ctrl[0].manualRadio.setChecked(True) - self.view().setXRange(float(self.ctrl[0].minText.text()), float(self.ctrl[0].maxText.text()), padding=0) + self.view().setXRange(*self._validateRangeText(0), padding=0) - def xMaxTextChanged(self): - self.ctrl[0].manualRadio.setChecked(True) - self.view().setXRange(float(self.ctrl[0].minText.text()), float(self.ctrl[0].maxText.text()), padding=0) - def xAutoClicked(self): val = self.ctrl[0].autoPercentSpin.value() * 0.01 self.view().enableAutoRange(ViewBox.XAxis, val) @@ -194,13 +191,9 @@ class ViewBoxMenu(QtGui.QMenu): def yManualClicked(self): self.view().enableAutoRange(ViewBox.YAxis, False) - def yMinTextChanged(self): + def yRangeTextChanged(self): self.ctrl[1].manualRadio.setChecked(True) - self.view().setYRange(float(self.ctrl[1].minText.text()), float(self.ctrl[1].maxText.text()), padding=0) - - def yMaxTextChanged(self): - self.ctrl[1].manualRadio.setChecked(True) - self.view().setYRange(float(self.ctrl[1].minText.text()), float(self.ctrl[1].maxText.text()), padding=0) + self.view().setYRange(*self._validateRangeText(1), padding=0) def yAutoClicked(self): val = self.ctrl[1].autoPercentSpin.value() * 0.01 @@ -265,6 +258,20 @@ class ViewBoxMenu(QtGui.QMenu): if changed: c.setCurrentIndex(0) c.currentIndexChanged.emit(c.currentIndex()) + + def _validateRangeText(self, axis): + """Validate range text inputs. Return current value(s) if invalid.""" + inputs = (self.ctrl[axis].minText.text(), + self.ctrl[axis].maxText.text()) + vals = self.view().viewRange()[axis] + for i, text in enumerate(inputs): + try: + vals[i] = float(text) + except ValueError: + # could not convert string to float + pass + return vals + from .ViewBox import ViewBox diff --git a/pyqtgraph/graphicsItems/ViewBox/axisCtrlTemplate.ui b/pyqtgraph/graphicsItems/ViewBox/axisCtrlTemplate.ui index 297fce75..01bdf93e 100644 --- a/pyqtgraph/graphicsItems/ViewBox/axisCtrlTemplate.ui +++ b/pyqtgraph/graphicsItems/ViewBox/axisCtrlTemplate.ui @@ -17,7 +17,7 @@ - Form + PyQtGraph diff --git a/pyqtgraph/graphicsItems/ViewBox/axisCtrlTemplate_pyqt.py b/pyqtgraph/graphicsItems/ViewBox/axisCtrlTemplate_pyqt.py index 5d952741..b54153fc 100644 --- a/pyqtgraph/graphicsItems/ViewBox/axisCtrlTemplate_pyqt.py +++ b/pyqtgraph/graphicsItems/ViewBox/axisCtrlTemplate_pyqt.py @@ -78,7 +78,7 @@ class Ui_Form(object): QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): - Form.setWindowTitle(_translate("Form", "Form", None)) + Form.setWindowTitle(_translate("Form", "PyQtGraph", None)) self.label.setText(_translate("Form", "Link Axis:", None)) self.linkCombo.setToolTip(_translate("Form", "

Links this axis with another view. When linked, both views will display the same data range.

", None)) self.autoPercentSpin.setToolTip(_translate("Form", "

Percent of data to be visible when auto-scaling. It may be useful to decrease this value for data with spiky noise.

", None)) diff --git a/pyqtgraph/graphicsItems/ViewBox/axisCtrlTemplate_pyqt5.py b/pyqtgraph/graphicsItems/ViewBox/axisCtrlTemplate_pyqt5.py index 78da6eea..0a28e7f6 100644 --- a/pyqtgraph/graphicsItems/ViewBox/axisCtrlTemplate_pyqt5.py +++ b/pyqtgraph/graphicsItems/ViewBox/axisCtrlTemplate_pyqt5.py @@ -65,7 +65,7 @@ class Ui_Form(object): def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate - Form.setWindowTitle(_translate("Form", "Form")) + Form.setWindowTitle(_translate("Form", "PyQtGraph")) self.label.setText(_translate("Form", "Link Axis:")) self.linkCombo.setToolTip(_translate("Form", "

Links this axis with another view. When linked, both views will display the same data range.

")) self.autoPercentSpin.setToolTip(_translate("Form", "

Percent of data to be visible when auto-scaling. It may be useful to decrease this value for data with spiky noise.

")) diff --git a/pyqtgraph/graphicsItems/ViewBox/axisCtrlTemplate_pyside.py b/pyqtgraph/graphicsItems/ViewBox/axisCtrlTemplate_pyside.py index 9ddeb5d1..c90206b5 100644 --- a/pyqtgraph/graphicsItems/ViewBox/axisCtrlTemplate_pyside.py +++ b/pyqtgraph/graphicsItems/ViewBox/axisCtrlTemplate_pyside.py @@ -64,7 +64,7 @@ class Ui_Form(object): QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): - Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8)) + Form.setWindowTitle(QtGui.QApplication.translate("Form", "PyQtGraph", None, QtGui.QApplication.UnicodeUTF8)) self.label.setText(QtGui.QApplication.translate("Form", "Link Axis:", None, QtGui.QApplication.UnicodeUTF8)) self.linkCombo.setToolTip(QtGui.QApplication.translate("Form", "

Links this axis with another view. When linked, both views will display the same data range.

", None, QtGui.QApplication.UnicodeUTF8)) self.autoPercentSpin.setToolTip(QtGui.QApplication.translate("Form", "

Percent of data to be visible when auto-scaling. It may be useful to decrease this value for data with spiky noise.

", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/pyqtgraph/graphicsItems/ViewBox/tests/test_ViewBox.py b/pyqtgraph/graphicsItems/ViewBox/tests/test_ViewBox.py index bb705c18..9495bfc3 100644 --- a/pyqtgraph/graphicsItems/ViewBox/tests/test_ViewBox.py +++ b/pyqtgraph/graphicsItems/ViewBox/tests/test_ViewBox.py @@ -63,7 +63,7 @@ def test_ViewBox(): assertMapping(vb, view1, size1) # test tall resize - win.resize(400, 800) + win.resize(200, 400) app.processEvents() w = vb.geometry().width() h = vb.geometry().height() @@ -71,6 +71,16 @@ def test_ViewBox(): size1 = QRectF(0, h, w, -h) assertMapping(vb, view1, size1) + win.close() + + +def test_ViewBox_setMenuEnabled(): + init_viewbox() + vb.setMenuEnabled(True) + assert vb.menu is not None + vb.setMenuEnabled(False) + assert vb.menu is None + skipreason = "Skipping this test until someone has time to fix it." @pytest.mark.skipif(True, reason=skipreason) diff --git a/pyqtgraph/graphicsItems/ViewBox/tests/test_ViewBoxZoom.py b/pyqtgraph/graphicsItems/ViewBox/tests/test_ViewBoxZoom.py new file mode 100644 index 00000000..4bad9ee1 --- /dev/null +++ b/pyqtgraph/graphicsItems/ViewBox/tests/test_ViewBoxZoom.py @@ -0,0 +1,200 @@ +# -*- coding: utf-8 -*- +import pyqtgraph as pg +import pytest + +app = pg.mkQApp() + +def test_zoom_normal(): + vb = pg.ViewBox() + testRange = pg.QtCore.QRect(0, 0, 10, 20) + vb.setRange(testRange, padding=0) + vbViewRange = vb.getState()['viewRange'] + assert vbViewRange == [[testRange.left(), testRange.right()], + [testRange.top(), testRange.bottom()]] + +def test_zoom_limit(): + """Test zooming with X and Y limits set""" + vb = pg.ViewBox() + vb.setLimits(xMin=0, xMax=10, yMin=0, yMax=10) + + # Try zooming within limits. Should return unmodified + testRange = pg.QtCore.QRect(0, 0, 9, 9) + vb.setRange(testRange, padding=0) + vbViewRange = vb.getState()['viewRange'] + assert vbViewRange == [[testRange.left(), testRange.right()], + [testRange.top(), testRange.bottom()]] + + # And outside limits. both view range and targetRange should be set to limits + testRange = pg.QtCore.QRect(-5, -5, 16, 20) + vb.setRange(testRange, padding=0) + + expected = [[0, 10], [0, 10]] + vbState = vb.getState() + + assert vbState['targetRange'] == expected + assert vbState['viewRange'] == expected + +def test_zoom_range_limit(): + """Test zooming with XRange and YRange limits set, but no X and Y limits""" + vb = pg.ViewBox() + vb.setLimits(minXRange=5, maxXRange=10, minYRange=5, maxYRange=10) + + # Try something within limits + testRange = pg.QtCore.QRect(-15, -15, 7, 7) + vb.setRange(testRange, padding=0) + + expected = [[testRange.left(), testRange.right()], + [testRange.top(), testRange.bottom()]] + + vbViewRange = vb.getState()['viewRange'] + assert vbViewRange == expected + + # and outside limits + testRange = pg.QtCore.QRect(-15, -15, 17, 17) + + # Code should center the required width reduction, so move each side by 3 + expected = [[testRange.left() + 3, testRange.right() - 3], + [testRange.top() + 3, testRange.bottom() - 3]] + + vb.setRange(testRange, padding=0) + vbViewRange = vb.getState()['viewRange'] + vbTargetRange = vb.getState()['targetRange'] + + assert vbViewRange == expected + assert vbTargetRange == expected + +def test_zoom_ratio(): + """Test zooming with a fixed aspect ratio set""" + vb = pg.ViewBox(lockAspect=1) + + # Give the viewbox a size of the proper aspect ratio to keep things easy + vb.setFixedHeight(10) + vb.setFixedWidth(10) + + # request a range with a good ratio + testRange = pg.QtCore.QRect(0, 0, 10, 10) + vb.setRange(testRange, padding=0) + expected = [[testRange.left(), testRange.right()], + [testRange.top(), testRange.bottom()]] + + viewRange = vb.getState()['viewRange'] + viewWidth = viewRange[0][1] - viewRange[0][0] + viewHeight = viewRange[1][1] - viewRange[1][0] + + # Assert that the width and height are equal, since we locked the aspect ratio at 1 + assert viewWidth == viewHeight + + # and for good measure, that it is the same as the test range + assert viewRange == expected + + # Now try to set to something with a different aspect ratio + testRange = pg.QtCore.QRect(0, 0, 10, 20) + vb.setRange(testRange, padding=0) + + viewRange = vb.getState()['viewRange'] + viewWidth = viewRange[0][1] - viewRange[0][0] + viewHeight = viewRange[1][1] - viewRange[1][0] + + # Don't really care what we got here, as long as the width and height are the same + assert viewWidth == viewHeight + +def test_zoom_ratio2(): + """Slightly more complicated zoom ratio test, where the view box shape does not match the ratio""" + vb = pg.ViewBox(lockAspect=1) + + # twice as wide as tall + vb.setFixedHeight(10) + vb.setFixedWidth(20) + + # more or less random requested range + testRange = pg.QtCore.QRect(0, 0, 10, 15) + vb.setRange(testRange, padding=0) + + viewRange = vb.getState()['viewRange'] + viewWidth = viewRange[0][1] - viewRange[0][0] + viewHeight = viewRange[1][1] - viewRange[1][0] + + # View width should be twice as wide as the height, + # since the viewbox is twice as wide as it is tall. + assert viewWidth == 2 * viewHeight + +def test_zoom_ratio_with_limits1(): + """Test zoom with both ratio and limits set""" + vb = pg.ViewBox(lockAspect=1) + + # twice as wide as tall + vb.setFixedHeight(10) + vb.setFixedWidth(20) + + # set some limits + vb.setLimits(xMin=-5, xMax=5, yMin=-5, yMax=5) + + # Try to zoom too tall + testRange = pg.QtCore.QRect(0, 0, 6, 10) + vb.setRange(testRange, padding=0) + + viewRange = vb.getState()['viewRange'] + viewWidth = viewRange[0][1] - viewRange[0][0] + viewHeight = viewRange[1][1] - viewRange[1][0] + + # Make sure our view is within limits and the proper aspect ratio + assert viewRange[0][0] >= -5 + assert viewRange[0][1] <= 5 + assert viewRange[1][0] >= -5 + assert viewRange[1][1] <= 5 + assert viewWidth == 2 * viewHeight + +def test_zoom_ratio_with_limits2(): + vb = pg.ViewBox(lockAspect=1) + + # twice as wide as tall + vb.setFixedHeight(10) + vb.setFixedWidth(20) + + # set some limits + vb.setLimits(xMin=-5, xMax=5, yMin=-5, yMax=5) + + # Same thing, but out-of-range the other way + testRange = pg.QtCore.QRect(0, 0, 16, 6) + vb.setRange(testRange, padding=0) + + viewRange = vb.getState()['viewRange'] + viewWidth = viewRange[0][1] - viewRange[0][0] + viewHeight = viewRange[1][1] - viewRange[1][0] + + # Make sure our view is within limits and the proper aspect ratio + assert viewRange[0][0] >= -5 + assert viewRange[0][1] <= 5 + assert viewRange[1][0] >= -5 + assert viewRange[1][1] <= 5 + assert viewWidth == 2 * viewHeight + +def test_zoom_ratio_with_limits_out_of_range(): + vb = pg.ViewBox(lockAspect=1) + + # twice as wide as tall + vb.setFixedHeight(10) + vb.setFixedWidth(20) + + # set some limits + vb.setLimits(xMin=-5, xMax=5, yMin=-5, yMax=5) + + # Request something completely out-of-range and out-of-aspect + testRange = pg.QtCore.QRect(10, 10, 25, 100) + vb.setRange(testRange, padding=0) + + viewRange = vb.getState()['viewRange'] + viewWidth = viewRange[0][1] - viewRange[0][0] + viewHeight = viewRange[1][1] - viewRange[1][0] + + # Make sure our view is within limits and the proper aspect ratio + assert viewRange[0][0] >= -5 + assert viewRange[0][1] <= 5 + assert viewRange[1][0] >= -5 + assert viewRange[1][1] <= 5 + assert viewWidth == 2 * viewHeight + + +if __name__ == "__main__": + setup_module(None) + test_zoom_ratio() diff --git a/pyqtgraph/graphicsItems/tests/test_AxisItem.py b/pyqtgraph/graphicsItems/tests/test_AxisItem.py index f076890d..6e21396d 100644 --- a/pyqtgraph/graphicsItems/tests/test_AxisItem.py +++ b/pyqtgraph/graphicsItems/tests/test_AxisItem.py @@ -28,3 +28,89 @@ def test_AxisItem_stopAxisAtTick(monkeypatch): monkeypatch.setattr(left, "drawPicture", test_left) plot.show() + app.processEvents() + plot.close() + + +def test_AxisItem_viewUnlink(): + plot = pg.PlotWidget() + view = plot.plotItem.getViewBox() + axis = plot.getAxis("bottom") + assert axis.linkedView() == view + axis.unlinkFromView() + assert axis.linkedView() is None + + +class FakeSignal: + + def __init__(self): + self.calls = [] + + def connect(self, *args, **kwargs): + self.calls.append('connect') + + def disconnect(self, *args, **kwargs): + self.calls.append('disconnect') + + +class FakeView: + + def __init__(self): + self.sigYRangeChanged = FakeSignal() + self.sigXRangeChanged = FakeSignal() + self.sigResized = FakeSignal() + + +def test_AxisItem_bottomRelink(): + axis = pg.AxisItem('bottom') + fake_view = FakeView() + axis.linkToView(fake_view) + assert axis.linkedView() == fake_view + assert fake_view.sigYRangeChanged.calls == [] + assert fake_view.sigXRangeChanged.calls == ['connect'] + assert fake_view.sigResized.calls == ['connect'] + axis.unlinkFromView() + assert fake_view.sigYRangeChanged.calls == [] + assert fake_view.sigXRangeChanged.calls == ['connect', 'disconnect'] + assert fake_view.sigResized.calls == ['connect', 'disconnect'] + + +def test_AxisItem_leftRelink(): + axis = pg.AxisItem('left') + fake_view = FakeView() + axis.linkToView(fake_view) + assert axis.linkedView() == fake_view + assert fake_view.sigYRangeChanged.calls == ['connect'] + assert fake_view.sigXRangeChanged.calls == [] + assert fake_view.sigResized.calls == ['connect'] + axis.unlinkFromView() + assert fake_view.sigYRangeChanged.calls == ['connect', 'disconnect'] + assert fake_view.sigXRangeChanged.calls == [] + assert fake_view.sigResized.calls == ['connect', 'disconnect'] + + +def test_AxisItem_tickFont(monkeypatch): + def collides(textSpecs): + fontMetrics = pg.Qt.QtGui.QFontMetrics(font) + for rect, _, text in textSpecs: + br = fontMetrics.tightBoundingRect(text) + if rect.height() < br.height() or rect.width() < br.width(): + return True + return False + + def test_collision(p, axisSpec, tickSpecs, textSpecs): + assert not collides(textSpecs) + + plot = pg.PlotWidget() + bottom = plot.getAxis("bottom") + left = plot.getAxis("left") + font = bottom.linkedView().font() + font.setPointSize(25) + bottom.setStyle(tickFont=font) + left.setStyle(tickFont=font) + monkeypatch.setattr(bottom, "drawPicture", test_collision) + monkeypatch.setattr(left, "drawPicture", test_collision) + + plot.show() + app.processEvents() + plot.close() diff --git a/pyqtgraph/graphicsItems/tests/test_ErrorBarItem.py b/pyqtgraph/graphicsItems/tests/test_ErrorBarItem.py index 4ee25e45..2b922c1e 100644 --- a/pyqtgraph/graphicsItems/tests/test_ErrorBarItem.py +++ b/pyqtgraph/graphicsItems/tests/test_ErrorBarItem.py @@ -35,3 +35,5 @@ def test_ErrorBarItem_defer_data(): r_clear_ebi = plot.viewRect() assert r_clear_ebi == r_no_ebi + + plot.close() diff --git a/pyqtgraph/graphicsItems/tests/test_ImageItem.py b/pyqtgraph/graphicsItems/tests/test_ImageItem.py index ca197c6e..91926fe4 100644 --- a/pyqtgraph/graphicsItems/tests/test_ImageItem.py +++ b/pyqtgraph/graphicsItems/tests/test_ImageItem.py @@ -121,7 +121,7 @@ def test_ImageItem(transpose=False): assertImageApproved(w, 'imageitem/resolution_with_downsampling_y', 'Resolution test with downsampling across y axis.') assert img._lastDownsample == (1, 4) - view.hide() + w.hide() def test_ImageItem_axisorder(): # All image tests pass again using the opposite axis order diff --git a/pyqtgraph/graphicsItems/tests/test_PlotCurveItem.py b/pyqtgraph/graphicsItems/tests/test_PlotCurveItem.py index a3c34b11..6d60d3e1 100644 --- a/pyqtgraph/graphicsItems/tests/test_PlotCurveItem.py +++ b/pyqtgraph/graphicsItems/tests/test_PlotCurveItem.py @@ -27,7 +27,8 @@ def test_PlotCurveItem(): c.setData(data, connect=np.array([1,1,1,0,1,1,0,0,1,0,0,0,1,1,0,0])) assertImageApproved(p, 'plotcurveitem/connectarray', "Plot curve with connection array.") - + + p.close() if __name__ == '__main__': diff --git a/pyqtgraph/graphicsItems/tests/test_PlotDataItem.py b/pyqtgraph/graphicsItems/tests/test_PlotDataItem.py index b506a654..894afc74 100644 --- a/pyqtgraph/graphicsItems/tests/test_PlotDataItem.py +++ b/pyqtgraph/graphicsItems/tests/test_PlotDataItem.py @@ -64,3 +64,27 @@ def test_clear_in_step_mode(): c = pg.PlotDataItem([1,4,2,3], [5,7,6], stepMode=True) w.addItem(c) c.clear() + +def test_clipping(): + y = np.random.normal(size=150) + x = np.exp2(np.linspace(5, 10, 150)) # non-uniform spacing + + w = pg.PlotWidget(autoRange=True, downsample=5) + c = pg.PlotDataItem(x, y) + w.addItem(c) + w.show() + + c.setClipToView(True) + + w.setXRange(200, 600) + + for x_min in range(100, 2**10 - 100, 100): + w.setXRange(x_min, x_min + 100) + + xDisp, _ = c.getData() + vr = c.viewRect() + + assert xDisp[0] <= vr.left() + assert xDisp[-1] >= vr.right() + + w.close() diff --git a/pyqtgraph/graphicsItems/tests/test_ROI.py b/pyqtgraph/graphicsItems/tests/test_ROI.py index 8cc2efd5..10c6009b 100644 --- a/pyqtgraph/graphicsItems/tests/test_ROI.py +++ b/pyqtgraph/graphicsItems/tests/test_ROI.py @@ -1,13 +1,14 @@ +# -*- coding: utf-8 -*- +import sys import numpy as np import pytest import pyqtgraph as pg from pyqtgraph.Qt import QtCore, QtTest from pyqtgraph.tests import assertImageApproved, mouseMove, mouseDrag, mouseClick, TransposedImageItem, resizeWindow - +import pytest app = pg.mkQApp() - def test_getArrayRegion(transpose=False): pr = pg.PolyLineROI([[0, 0], [27, 0], [0, 28]], closed=True) pr.setPos(1, 1) @@ -32,7 +33,6 @@ def test_getArrayRegion(transpose=False): finally: pg.setConfigOptions(imageAxisOrder=origMode) - def test_getArrayRegion_axisorder(): test_getArrayRegion(transpose=True) @@ -133,7 +133,12 @@ def check_getArrayRegion(roi, name, testResize=True, transpose=False): rgn = roi.getArrayRegion(data, img1, axes=(1, 2)) img2.setImage(rgn[0, ..., 0]) app.processEvents() - assertImageApproved(win, name+'/roi_getarrayregion_inverty', 'Simple ROI region selection, view inverted.') + # on windows, one edge of one ROI handle is shifted slightly; letting this slide with pxCount=10 + if pg.Qt.QT_LIB in {'PyQt4', 'PySide'}: + pxCount = 10 + else: + pxCount=-1 + assertImageApproved(win, name+'/roi_getarrayregion_inverty', 'Simple ROI region selection, view inverted.', pxCount=pxCount) roi.setState(initState) img1.resetTransform() @@ -147,6 +152,7 @@ def check_getArrayRegion(roi, name, testResize=True, transpose=False): # allow the roi to be re-used roi.scene().removeItem(roi) + win.hide() def test_PolyLineROI(): rois = [ @@ -228,5 +234,6 @@ def test_PolyLineROI(): r.setState(initState) assertImageApproved(plt, 'roi/polylineroi/'+name+'_setstate', 'Reset ROI to initial state.') assert len(r.getState()['points']) == 3 - - \ No newline at end of file + + plt.hide() + diff --git a/pyqtgraph/graphicsItems/tests/test_TextItem.py b/pyqtgraph/graphicsItems/tests/test_TextItem.py new file mode 100644 index 00000000..6667dfc5 --- /dev/null +++ b/pyqtgraph/graphicsItems/tests/test_TextItem.py @@ -0,0 +1,23 @@ +import pytest +import pyqtgraph as pg + +app = pg.mkQApp() + + +def test_TextItem_setAngle(): + plt = pg.plot() + plt.setXRange(-10, 10) + plt.setYRange(-20, 20) + item = pg.TextItem(text="test") + plt.addItem(item) + + t1 = item.transform() + + item.setAngle(30) + app.processEvents() + + t2 = item.transform() + + assert t1 != t2 + assert not t1.isRotating() + assert t2.isRotating() diff --git a/pyqtgraph/graphicsWindows.py b/pyqtgraph/graphicsWindows.py index b6598685..e915d1a8 100644 --- a/pyqtgraph/graphicsWindows.py +++ b/pyqtgraph/graphicsWindows.py @@ -15,11 +15,10 @@ from .widgets.GraphicsView import GraphicsView class GraphicsWindow(GraphicsLayoutWidget): """ - (deprecated; use GraphicsLayoutWidget instead) + (deprecated; use :class:`~pyqtgraph.GraphicsLayoutWidget` instead) - Convenience subclass of :class:`GraphicsLayoutWidget - `. This class is intended for use from - the interactive python prompt. + Convenience subclass of :class:`~pyqtgraph.GraphicsLayoutWidget`. This class + is intended for use from the interactive python prompt. """ def __init__(self, title=None, size=(800,600), **kargs): mkQApp() @@ -45,15 +44,14 @@ class TabWindow(QtGui.QMainWindow): self.show() def __getattr__(self, attr): - if hasattr(self.cw, attr): - return getattr(self.cw, attr) - else: - raise NameError(attr) + return getattr(self.cw, attr) class PlotWindow(PlotWidget): + sigClosed = QtCore.Signal(object) + """ - (deprecated; use PlotWidget instead) + (deprecated; use :class:`~pyqtgraph.PlotWidget` instead) """ def __init__(self, title=None, **kargs): mkQApp() @@ -66,10 +64,16 @@ class PlotWindow(PlotWidget): self.win.setWindowTitle(title) self.win.show() + def closeEvent(self, event): + PlotWidget.closeEvent(self, event) + self.sigClosed.emit(self) + class ImageWindow(ImageView): + sigClosed = QtCore.Signal(object) + """ - (deprecated; use ImageView instead) + (deprecated; use :class:`~pyqtgraph.ImageView` instead) """ def __init__(self, *args, **kargs): mkQApp() @@ -81,9 +85,12 @@ class ImageWindow(ImageView): ImageView.__init__(self, self.win) if len(args) > 0 or len(kargs) > 0: self.setImage(*args, **kargs) + self.win.setCentralWidget(self) for m in ['resize']: setattr(self, m, getattr(self.win, m)) - #for m in ['setImage', 'autoRange', 'addItem', 'removeItem', 'blackLevel', 'whiteLevel', 'imageItem']: - #setattr(self, m, getattr(self.cw, m)) self.win.show() + + def closeEvent(self, event): + ImageView.closeEvent(self, event) + self.sigClosed.emit(self) diff --git a/pyqtgraph/imageview/ImageView.py b/pyqtgraph/imageview/ImageView.py index 81463b7a..4544f014 100644 --- a/pyqtgraph/imageview/ImageView.py +++ b/pyqtgraph/imageview/ImageView.py @@ -2,7 +2,7 @@ """ ImageView.py - Widget for basic image dispay and analysis Copyright 2010 Luke Campagnola -Distributed under MIT/X11 license. See license.txt for more infomation. +Distributed under MIT/X11 license. See license.txt for more information. Widget used for displaying 2D or 3D data. Features: - float or int (including 16-bit int) image display via ImageItem @@ -131,7 +131,7 @@ class ImageView(QtGui.QWidget): self.scene = self.ui.graphicsView.scene() self.ui.histogram.setLevelMode(levelMode) - self.ignoreTimeLine = False + self.ignorePlaying = False if view is None: self.view = ViewBox() @@ -176,6 +176,7 @@ class ImageView(QtGui.QWidget): self.keysPressed = {} self.playTimer = QtCore.QTimer() self.playRate = 0 + self.fps = 1 # 1 Hz by default self.lastPlayTime = 0 self.normRgn = LinearRegionItem() @@ -368,11 +369,14 @@ class ImageView(QtGui.QWidget): self.image = None self.imageItem.clear() - def play(self, rate): + def play(self, rate=None): """Begin automatically stepping frames forward at the given rate (in fps). This can also be accessed by pressing the spacebar.""" #print "play:", rate + if rate is None: + rate = self.fps self.playRate = rate + if rate == 0: self.playTimer.stop() return @@ -411,11 +415,9 @@ class ImageView(QtGui.QWidget): def close(self): """Closes the widget nicely, making sure to clear the graphics scene and release memory.""" - self.ui.roiPlot.close() - self.ui.graphicsView.close() - self.scene.clear() - del self.image - del self.imageDisp + self.clear() + self.imageDisp = None + self.imageItem.setParent(None) super(ImageView, self).close() self.setParent(None) @@ -423,9 +425,7 @@ class ImageView(QtGui.QWidget): #print ev.key() if ev.key() == QtCore.Qt.Key_Space: if self.playRate == 0: - fps = (self.getProcessedImage().shape[0]-1) / (self.tVals[-1] - self.tVals[0]) - self.play(fps) - #print fps + self.play() else: self.play(0) ev.accept() @@ -498,11 +498,11 @@ class ImageView(QtGui.QWidget): def setCurrentIndex(self, ind): """Set the currently displayed frame index.""" - self.currentIndex = np.clip(ind, 0, self.getProcessedImage().shape[self.axes['t']]-1) - self.updateImage() - self.ignoreTimeLine = True - self.timeLine.setValue(self.tVals[self.currentIndex]) - self.ignoreTimeLine = False + index = np.clip(ind, 0, self.getProcessedImage().shape[self.axes['t']]-1) + self.ignorePlaying = True + # Implicitly call timeLineChanged + self.timeLine.setValue(self.tVals[index]) + self.ignorePlaying = False def jumpFrames(self, n): """Move video frame ahead n frames (may be negative)""" @@ -586,7 +586,7 @@ class ImageView(QtGui.QWidget): # Extract image data from ROI axes = (self.axes['x'], self.axes['y']) - data, coords = self.roi.getArrayRegion(image.view(np.ndarray), self.imageItem, axes, returnMappedCoords=True) + data, coords = self.roi.getArrayRegion(image.view(np.ndarray), self.imageItem, returnMappedCoords=True) if data is None: return @@ -594,7 +594,10 @@ class ImageView(QtGui.QWidget): if self.axes['t'] is None: # Average across y-axis of ROI data = data.mean(axis=axes[1]) - coords = coords[:,:,0] - coords[:,0:1,0] + if axes == (1,0): ## we're in row-major order mode -- there's probably a better way to do this slicing dynamically, but I've not figured it out yet. + coords = coords[:,0,:] - coords[:,0,0:1] + else: #default to old way + coords = coords[:,:,0] - coords[:,0:1,0] xvals = (coords**2).sum(axis=0) ** 0.5 else: # Average data within entire ROI for each frame @@ -696,16 +699,13 @@ class ImageView(QtGui.QWidget): return norm def timeLineChanged(self): - #(ind, time) = self.timeIndex(self.ui.timeSlider) - if self.ignoreTimeLine: - return - self.play(0) + if not self.ignorePlaying: + self.play(0) + (ind, time) = self.timeIndex(self.timeLine) if ind != self.currentIndex: self.currentIndex = ind self.updateImage() - #self.timeLine.setPos(time) - #self.emit(QtCore.SIGNAL('timeChanged'), ind, time) self.sigTimeChanged.emit(ind, time) def updateImage(self, autoHistogramRange=True): @@ -740,7 +740,7 @@ class ImageView(QtGui.QWidget): return (0,0) t = slider.value() - + xv = self.tVals if xv is None: ind = int(t) @@ -748,7 +748,7 @@ class ImageView(QtGui.QWidget): if len(xv) < 2: return (0,0) totTime = xv[-1] + (xv[-1]-xv[-2]) - inds = np.argwhere(xv < t) + inds = np.argwhere(xv <= t) if len(inds) < 1: return (0,t) ind = inds[-1,0] diff --git a/pyqtgraph/imageview/ImageViewTemplate.ui b/pyqtgraph/imageview/ImageViewTemplate.ui index 927bda30..ece77864 100644 --- a/pyqtgraph/imageview/ImageViewTemplate.ui +++ b/pyqtgraph/imageview/ImageViewTemplate.ui @@ -11,7 +11,7 @@
- Form + PyQtGraph diff --git a/pyqtgraph/imageview/ImageViewTemplate_pyqt.py b/pyqtgraph/imageview/ImageViewTemplate_pyqt.py index 8c9d5633..8a34c1d8 100644 --- a/pyqtgraph/imageview/ImageViewTemplate_pyqt.py +++ b/pyqtgraph/imageview/ImageViewTemplate_pyqt.py @@ -146,7 +146,7 @@ class Ui_Form(object): QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): - Form.setWindowTitle(_translate("Form", "Form", None)) + Form.setWindowTitle(_translate("Form", "PyQtGraph", None)) self.roiBtn.setText(_translate("Form", "ROI", None)) self.menuBtn.setText(_translate("Form", "Menu", None)) self.normGroup.setTitle(_translate("Form", "Normalization", None)) diff --git a/pyqtgraph/imageview/ImageViewTemplate_pyqt5.py b/pyqtgraph/imageview/ImageViewTemplate_pyqt5.py index 1d076a9e..87f3f254 100644 --- a/pyqtgraph/imageview/ImageViewTemplate_pyqt5.py +++ b/pyqtgraph/imageview/ImageViewTemplate_pyqt5.py @@ -134,7 +134,7 @@ class Ui_Form(object): def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate - Form.setWindowTitle(_translate("Form", "Form")) + Form.setWindowTitle(_translate("Form", "PyQtGraph")) self.roiBtn.setText(_translate("Form", "ROI")) self.menuBtn.setText(_translate("Form", "Norm")) self.normGroup.setTitle(_translate("Form", "Normalization")) diff --git a/pyqtgraph/imageview/ImageViewTemplate_pyside.py b/pyqtgraph/imageview/ImageViewTemplate_pyside.py index 6d6c9632..9980e2ba 100644 --- a/pyqtgraph/imageview/ImageViewTemplate_pyside.py +++ b/pyqtgraph/imageview/ImageViewTemplate_pyside.py @@ -132,7 +132,7 @@ class Ui_Form(object): QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): - Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8)) + Form.setWindowTitle(QtGui.QApplication.translate("Form", "PyQtGraph", None, QtGui.QApplication.UnicodeUTF8)) self.roiBtn.setText(QtGui.QApplication.translate("Form", "ROI", None, QtGui.QApplication.UnicodeUTF8)) self.menuBtn.setText(QtGui.QApplication.translate("Form", "Menu", None, QtGui.QApplication.UnicodeUTF8)) self.normGroup.setTitle(QtGui.QApplication.translate("Form", "Normalization", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/pyqtgraph/metaarray/MetaArray.py b/pyqtgraph/metaarray/MetaArray.py index 15d374a6..374c9acf 100644 --- a/pyqtgraph/metaarray/MetaArray.py +++ b/pyqtgraph/metaarray/MetaArray.py @@ -2,7 +2,7 @@ """ MetaArray.py - Class encapsulating ndarray with meta data Copyright 2010 Luke Campagnola -Distributed under MIT/X11 license. See license.txt for more infomation. +Distributed under MIT/X11 license. See license.txt for more information. MetaArray is an array class based on numpy.ndarray that allows storage of per-axis meta data such as axis values, names, units, column names, etc. It also enables several @@ -12,10 +12,9 @@ More info at http://www.scipy.org/Cookbook/MetaArray import types, copy, threading, os, re import pickle -from functools import reduce import numpy as np from ..python2_3 import basestring -#import traceback + ## By default, the library will use HDF5 when writing files. ## This can be overridden by setting USE_HDF5 = False @@ -103,7 +102,7 @@ class MetaArray(object): since the actual values are described (name and units) in the column info for the first axis. """ - version = '2' + version = u'2' # Default hdf5 compression to use when writing # 'gzip' is widely available and somewhat slow @@ -358,9 +357,12 @@ class MetaArray(object): else: return np.array(self._data) - def __array__(self): + def __array__(self, dtype=None): ## supports np.array(metaarray_instance) - return self.asarray() + if dtype is None: + return self.asarray() + else: + return self.asarray().astype(dtype) def view(self, typ): ## deprecated; kept for backward compatibility @@ -741,7 +743,7 @@ class MetaArray(object): ## decide which read function to use with open(filename, 'rb') as fd: magic = fd.read(8) - if magic == '\x89HDF\r\n\x1a\n': + if magic == b'\x89HDF\r\n\x1a\n': fd.close() self._readHDF5(filename, **kwargs) self._isHDF = True @@ -766,7 +768,7 @@ class MetaArray(object): """Read meta array from the top of a file. Read lines until a blank line is reached. This function should ideally work for ALL versions of MetaArray. """ - meta = '' + meta = u'' ## Read meta information until the first blank line while True: line = fd.readline().strip() @@ -776,7 +778,7 @@ class MetaArray(object): ret = eval(meta) #print ret return ret - + def _readData1(self, fd, meta, mmap=False, **kwds): ## Read array data from the file descriptor for MetaArray v1 files ## read in axis values for any axis that specifies a length @@ -844,7 +846,7 @@ class MetaArray(object): frames = [] frameShape = list(meta['shape']) frameShape[dynAxis] = 1 - frameSize = reduce(lambda a,b: a*b, frameShape) + frameSize = np.prod(frameShape) n = 0 while True: ## Extract one non-blank line @@ -886,10 +888,8 @@ class MetaArray(object): newSubset = list(subset[:]) newSubset[dynAxis] = slice(dStart, dStop) if dStop > dStart: - #print n, data.shape, " => ", newSubset, data[tuple(newSubset)].shape frames.append(data[tuple(newSubset)].copy()) else: - #data = data[subset].copy() ## what's this for?? frames.append(data) n += inf['numFrames'] @@ -900,12 +900,8 @@ class MetaArray(object): ax['values'] = np.array(xVals, dtype=ax['values_type']) del ax['values_len'] del ax['values_type'] - #subarr = subarr.view(subtype) - #subarr._info = meta['info'] self._info = meta['info'] self._data = subarr - #raise Exception() ## stress-testing - #return subarr def _readHDF5(self, fileName, readAllData=None, writable=False, **kargs): if 'close' in kargs and readAllData is None: ## for backward compatibility @@ -935,6 +931,10 @@ class MetaArray(object): f = h5py.File(fileName, mode) ver = f.attrs['MetaArray'] + try: + ver = ver.decode('utf-8') + except: + pass if ver > MetaArray.version: print("Warning: This file was written with MetaArray version %s, but you are using version %s. (Will attempt to read anyway)" % (str(ver), str(MetaArray.version))) meta = MetaArray.readHDF5Meta(f['info']) @@ -964,11 +964,6 @@ class MetaArray(object): ma = MetaArray._h5py_metaarray.MetaArray(file=fileName) self._data = ma.asarray()._getValue() self._info = ma._info._getValue() - #print MetaArray._hdf5Process - #import inspect - #print MetaArray, id(MetaArray), inspect.getmodule(MetaArray) - - @staticmethod def mapHDF5Array(data, writable=False): @@ -980,9 +975,6 @@ class MetaArray(object): if off is None: raise Exception("This dataset uses chunked storage; it can not be memory-mapped. (store using mappable=True)") return np.memmap(filename=data.file.filename, offset=off, dtype=data.dtype, shape=data.shape, mode=mode) - - - @staticmethod def readHDF5Meta(root, mmap=False): @@ -991,6 +983,8 @@ class MetaArray(object): ## Pull list of values from attributes and child objects for k in root.attrs: val = root.attrs[k] + if isinstance(val, bytes): + val = val.decode() if isinstance(val, basestring): ## strings need to be re-evaluated to their original types try: val = eval(val) @@ -1011,6 +1005,10 @@ class MetaArray(object): data[k] = val typ = root.attrs['_metaType_'] + try: + typ = typ.decode('utf-8') + except: + pass del data['_metaType_'] if typ == 'dict': @@ -1024,7 +1022,6 @@ class MetaArray(object): return d2 else: raise Exception("Don't understand metaType '%s'" % typ) - def write(self, fileName, **opts): """Write this object to a file. The object can be restored by calling MetaArray(file=fileName) @@ -1033,12 +1030,13 @@ class MetaArray(object): appendKeys: a list of keys (other than "values") for metadata to append to on the appendable axis. compression: None, 'gzip' (good compression), 'lzf' (fast compression), etc. chunks: bool or tuple specifying chunk shape - """ - - if USE_HDF5 and HAVE_HDF5: + """ + if USE_HDF5 is False: + return self.writeMa(fileName, **opts) + elif HAVE_HDF5 is True: return self.writeHDF5(fileName, **opts) else: - return self.writeMa(fileName, **opts) + raise Exception("h5py is required for writing .ma hdf5 files, but it could not be imported.") def writeMeta(self, fileName): """Used to re-write meta info to the given file. @@ -1051,7 +1049,6 @@ class MetaArray(object): self.writeHDF5Meta(f, 'info', self._info) f.close() - def writeHDF5(self, fileName, **opts): ## default options for writing datasets comp = self.defaultCompression @@ -1087,8 +1084,7 @@ class MetaArray(object): ## update options if they were passed in for k in dsOpts: if k in opts: - dsOpts[k] = opts[k] - + dsOpts[k] = opts[k] ## If mappable is in options, it disables chunking/compression if opts.get('mappable', False): @@ -1298,7 +1294,7 @@ class MetaArray(object): #frames = [] #frameShape = list(meta['shape']) #frameShape[dynAxis] = 1 - #frameSize = reduce(lambda a,b: a*b, frameShape) + #frameSize = np.prod(frameShape) #n = 0 #while True: ### Extract one non-blank line diff --git a/pyqtgraph/multiprocess/parallelizer.py b/pyqtgraph/multiprocess/parallelizer.py index 989bd4f8..b0f064bd 100644 --- a/pyqtgraph/multiprocess/parallelizer.py +++ b/pyqtgraph/multiprocess/parallelizer.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- import os, sys, time, multiprocessing, re from .processes import ForkedProcess from .remoteproxy import ClosedError @@ -213,14 +214,14 @@ class Parallelize(object): try: cores = {} pid = None - - for line in open('/proc/cpuinfo'): - m = re.match(r'physical id\s+:\s+(\d+)', line) - if m is not None: - pid = m.groups()[0] - m = re.match(r'cpu cores\s+:\s+(\d+)', line) - if m is not None: - cores[pid] = int(m.groups()[0]) + with open('/proc/cpuinfo') as fd: + for line in fd: + m = re.match(r'physical id\s+:\s+(\d+)', line) + if m is not None: + pid = m.groups()[0] + m = re.match(r'cpu cores\s+:\s+(\d+)', line) + if m is not None: + cores[pid] = int(m.groups()[0]) return sum(cores.values()) except: return multiprocessing.cpu_count() diff --git a/pyqtgraph/multiprocess/processes.py b/pyqtgraph/multiprocess/processes.py index d841ea40..6e815edc 100644 --- a/pyqtgraph/multiprocess/processes.py +++ b/pyqtgraph/multiprocess/processes.py @@ -166,6 +166,14 @@ class Process(RemoteEventHandler): raise Exception('Timed out waiting for remote process to end.') time.sleep(0.05) self.conn.close() + + # Close remote polling threads, otherwise they will spin continuously + if hasattr(self, "_stdoutForwarder"): + self._stdoutForwarder.finish.set() + self._stderrForwarder.finish.set() + self._stdoutForwarder.join() + self._stderrForwarder.join() + self.debugMsg('Child process exited. (%d)' % self.proc.returncode) def debugMsg(self, msg, *args): @@ -473,23 +481,24 @@ class FileForwarder(threading.Thread): self.lock = threading.Lock() self.daemon = True self.color = color + self.finish = threading.Event() self.start() def run(self): if self.output == 'stdout' and self.color is not False: - while True: + while not self.finish.is_set(): line = self.input.readline() with self.lock: cprint.cout(self.color, line, -1) elif self.output == 'stderr' and self.color is not False: - while True: + while not self.finish.is_set(): line = self.input.readline() with self.lock: cprint.cerr(self.color, line, -1) else: if isinstance(self.output, str): self.output = getattr(sys, self.output) - while True: + while not self.finish.is_set(): line = self.input.readline() with self.lock: - self.output.write(line) + self.output.write(line.decode('utf8')) diff --git a/pyqtgraph/multiprocess/remoteproxy.py b/pyqtgraph/multiprocess/remoteproxy.py index b1077674..f0d993cb 100644 --- a/pyqtgraph/multiprocess/remoteproxy.py +++ b/pyqtgraph/multiprocess/remoteproxy.py @@ -458,7 +458,7 @@ class RemoteEventHandler(object): ## follow up by sending byte messages if byteData is not None: for obj in byteData: ## Remote process _must_ be prepared to read the same number of byte messages! - self.conn.send_bytes(obj) + self.conn.send_bytes(bytes(obj)) self.debugMsg(' sent %d byte messages', len(byteData)) self.debugMsg(' call sync: %s', callSync) diff --git a/pyqtgraph/numpy_fix.py b/pyqtgraph/numpy_fix.py deleted file mode 100644 index 2fa8ef1f..00000000 --- a/pyqtgraph/numpy_fix.py +++ /dev/null @@ -1,22 +0,0 @@ -try: - import numpy as np - - ## Wrap np.concatenate to catch and avoid a segmentation fault bug - ## (numpy trac issue #2084) - if not hasattr(np, 'concatenate_orig'): - np.concatenate_orig = np.concatenate - def concatenate(vals, *args, **kwds): - """Wrapper around numpy.concatenate (see pyqtgraph/numpy_fix.py)""" - dtypes = [getattr(v, 'dtype', None) for v in vals] - names = [getattr(dt, 'names', None) for dt in dtypes] - if len(dtypes) < 2 or all([n is None for n in names]): - return np.concatenate_orig(vals, *args, **kwds) - if any([dt != dtypes[0] for dt in dtypes[1:]]): - raise TypeError("Cannot concatenate structured arrays of different dtype.") - return np.concatenate_orig(vals, *args, **kwds) - - np.concatenate = concatenate - -except ImportError: - pass - diff --git a/pyqtgraph/opengl/GLViewWidget.py b/pyqtgraph/opengl/GLViewWidget.py index 1a70d735..82d9c3b1 100644 --- a/pyqtgraph/opengl/GLViewWidget.py +++ b/pyqtgraph/opengl/GLViewWidget.py @@ -425,15 +425,35 @@ class GLViewWidget(QtOpenGL.QGLWidget): self.keyTimer.stop() def checkOpenGLVersion(self, msg): - ## Only to be called from within exception handler. - ver = glGetString(GL_VERSION).split()[0] - if int(ver.split('.')[0]) < 2: - from .. import debug - debug.printExc() - raise Exception(msg + " The original exception is printed above; however, pyqtgraph requires OpenGL version 2.0 or greater for many of its 3D features and your OpenGL version is %s. Installing updated display drivers may resolve this issue." % ver) - else: - raise - + """ + Give exception additional context about version support. + + Only to be called from within exception handler. + As this check is only performed on error, + unsupported versions might still work! + """ + + # Check for unsupported version + verString = glGetString(GL_VERSION) + ver = verString.split()[0] + # If not OpenGL ES... + if str(ver.split(b'.')[0]).isdigit(): + verNumber = int(ver.split(b'.')[0]) + # ...and version is supported: + if verNumber >= 2: + # OpenGL version is fine, raise the original exception + raise + + # Print original exception + from .. import debug + debug.printExc() + + # Notify about unsupported version + raise Exception( + msg + "\n" + \ + "pyqtgraph.opengl: Requires >= OpenGL 2.0 (not ES); Found %s" % verString + ) + def readQImage(self): """ Read the current buffer pixels out as a QImage. diff --git a/pyqtgraph/opengl/MeshData.py b/pyqtgraph/opengl/MeshData.py index 5bab4626..95ba88af 100644 --- a/pyqtgraph/opengl/MeshData.py +++ b/pyqtgraph/opengl/MeshData.py @@ -371,7 +371,7 @@ class MeshData(object): #pass def _computeEdges(self): - if not self.hasFaceIndexedData: + if not self.hasFaceIndexedData(): ## generate self._edges from self._faces nf = len(self._faces) edges = np.empty(nf*3, dtype=[('i', np.uint, 2)]) diff --git a/pyqtgraph/opengl/items/GLBarGraphItem.py b/pyqtgraph/opengl/items/GLBarGraphItem.py index b3060dc9..42d05fb7 100644 --- a/pyqtgraph/opengl/items/GLBarGraphItem.py +++ b/pyqtgraph/opengl/items/GLBarGraphItem.py @@ -8,7 +8,7 @@ class GLBarGraphItem(GLMeshItem): pos is (...,3) array of the bar positions (the corner of each bar) size is (...,3) array of the sizes of each bar """ - nCubes = reduce(lambda a,b: a*b, pos.shape[:-1]) + nCubes = np.prod(pos.shape[:-1]) cubeVerts = np.mgrid[0:2,0:2,0:2].reshape(3,8).transpose().reshape(1,8,3) cubeFaces = np.array([ [0,1,2], [3,2,1], @@ -22,8 +22,5 @@ class GLBarGraphItem(GLMeshItem): verts = cubeVerts * size + pos faces = cubeFaces + (np.arange(nCubes) * 8).reshape(nCubes,1,1) md = MeshData(verts.reshape(nCubes*8,3), faces.reshape(nCubes*12,3)) - - GLMeshItem.__init__(self, meshdata=md, shader='shaded', smooth=False) - - \ No newline at end of file + GLMeshItem.__init__(self, meshdata=md, shader='shaded', smooth=False) diff --git a/pyqtgraph/opengl/items/GLGridItem.py b/pyqtgraph/opengl/items/GLGridItem.py index 0da9f61e..9dcff070 100644 --- a/pyqtgraph/opengl/items/GLGridItem.py +++ b/pyqtgraph/opengl/items/GLGridItem.py @@ -3,6 +3,7 @@ import numpy as np from OpenGL.GL import * from .. GLGraphicsItem import GLGraphicsItem from ... import QtGui +from ... import functions as fn __all__ = ['GLGridItem'] @@ -13,7 +14,7 @@ class GLGridItem(GLGraphicsItem): Displays a wire-frame grid. """ - def __init__(self, size=None, color=(1, 1, 1, .3), antialias=True, glOptions='translucent'): + def __init__(self, size=None, color=(255, 255, 255, 76.5), antialias=True, glOptions='translucent'): GLGraphicsItem.__init__(self) self.setGLOptions(glOptions) self.antialias = antialias @@ -21,7 +22,7 @@ class GLGridItem(GLGraphicsItem): size = QtGui.QVector3D(20,20,1) self.setSize(size=size) self.setSpacing(1, 1, 1) - self.color = color + self.setColor(color) def setSize(self, x=None, y=None, z=None, size=None): """ @@ -53,6 +54,14 @@ class GLGridItem(GLGraphicsItem): def spacing(self): return self.__spacing[:] + def setColor(self, color): + """Set the color of the grid. Arguments are the same as those accepted by functions.mkColor()""" + self.__color = fn.Color(color) + self.update() + + def color(self): + return self.__color + def paint(self): self.setupGLState() @@ -68,7 +77,7 @@ class GLGridItem(GLGraphicsItem): xs,ys,zs = self.spacing() xvals = np.arange(-x/2., x/2. + xs*0.001, xs) yvals = np.arange(-y/2., y/2. + ys*0.001, ys) - glColor4f(*self.color) + glColor4f(*self.color().glColor()) for x in xvals: glVertex3f(x, yvals[0], 0) glVertex3f(x, yvals[-1], 0) diff --git a/pyqtgraph/opengl/items/GLImageItem.py b/pyqtgraph/opengl/items/GLImageItem.py index 59ddaf6f..7bd0ec02 100644 --- a/pyqtgraph/opengl/items/GLImageItem.py +++ b/pyqtgraph/opengl/items/GLImageItem.py @@ -29,8 +29,11 @@ class GLImageItem(GLGraphicsItem): GLGraphicsItem.__init__(self) self.setData(data) self.setGLOptions(glOptions) + self.texture = None def initializeGL(self): + if self.texture is not None: + return glEnable(GL_TEXTURE_2D) self.texture = glGenTextures(1) @@ -73,6 +76,7 @@ class GLImageItem(GLGraphicsItem): def paint(self): if self._needUpdate: self._updateTexture() + self._needUpdate = False glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, self.texture) diff --git a/pyqtgraph/opengl/items/GLScatterPlotItem.py b/pyqtgraph/opengl/items/GLScatterPlotItem.py index fe794d48..463ad742 100644 --- a/pyqtgraph/opengl/items/GLScatterPlotItem.py +++ b/pyqtgraph/opengl/items/GLScatterPlotItem.py @@ -20,6 +20,7 @@ class GLScatterPlotItem(GLGraphicsItem): self.pxMode = True #self.vbo = {} ## VBO does not appear to improve performance very much. self.setData(**kwds) + self.shader = None def setData(self, **kwds): """ @@ -54,11 +55,13 @@ class GLScatterPlotItem(GLGraphicsItem): self.update() def initializeGL(self): + if self.shader is not None: + return ## Generate texture for rendering points w = 64 def fn(x,y): - r = ((x-w/2.)**2 + (y-w/2.)**2) ** 0.5 + r = ((x-(w-1)/2.)**2 + (y-(w-1)/2.)**2) ** 0.5 return 255 * (w/2. - np.clip(r, w/2.-1.0, w/2.)) pData = np.empty((w, w, 4)) pData[:] = 255 @@ -120,7 +123,7 @@ class GLScatterPlotItem(GLGraphicsItem): try: pos = self.pos #if pos.ndim > 2: - #pos = pos.reshape((reduce(lambda a,b: a*b, pos.shape[:-1]), pos.shape[-1])) + #pos = pos.reshape((-1, pos.shape[-1])) glVertexPointerf(pos) if isinstance(self.color, np.ndarray): diff --git a/pyqtgraph/opengl/shaders.py b/pyqtgraph/opengl/shaders.py index 8922cd21..7ada939c 100644 --- a/pyqtgraph/opengl/shaders.py +++ b/pyqtgraph/opengl/shaders.py @@ -140,9 +140,9 @@ def initShaders(): ## colors fragments by z-value. ## This is useful for coloring surface plots by height. ## This shader uses a uniform called "colorMap" to determine how to map the colors: - ## red = pow(z * colorMap[0] + colorMap[1], colorMap[2]) - ## green = pow(z * colorMap[3] + colorMap[4], colorMap[5]) - ## blue = pow(z * colorMap[6] + colorMap[7], colorMap[8]) + ## red = pow(colorMap[0]*(z + colorMap[1]), colorMap[2]) + ## green = pow(colorMap[3]*(z + colorMap[4]), colorMap[5]) + ## blue = pow(colorMap[6]*(z + colorMap[7]), colorMap[8]) ## (set the values like this: shader['uniformMap'] = array([...]) ShaderProgram('heightColor', [ VertexShader(""" diff --git a/pyqtgraph/parametertree/Parameter.py b/pyqtgraph/parametertree/Parameter.py index 654a33db..9ef30477 100644 --- a/pyqtgraph/parametertree/Parameter.py +++ b/pyqtgraph/parametertree/Parameter.py @@ -55,6 +55,7 @@ class Parameter(QtCore.QObject): sigDefaultChanged(self, default) Emitted when this parameter's default value has changed sigNameChanged(self, name) Emitted when this parameter's name has changed sigOptionsChanged(self, opts) Emitted when any of this parameter's options have changed + sigContextMenu(self, name) Emitted when a context menu was clicked =================================== ========================================================= """ ## name, type, limits, etc. @@ -81,7 +82,8 @@ class Parameter(QtCore.QObject): ## (but only if monitorChildren() is called) sigTreeStateChanged = QtCore.Signal(object, object) # self, changes # changes = [(param, change, info), ...] - + sigContextMenu = QtCore.Signal(object, object) # self, name + # bad planning. #def __new__(cls, *args, **opts): #try: @@ -135,9 +137,12 @@ class Parameter(QtCore.QObject): (default=False) removable If True, the user may remove this Parameter. (default=False) - expanded If True, the Parameter will appear expanded when - displayed in a ParameterTree (its children will be - visible). (default=True) + expanded If True, the Parameter will initially be expanded in + ParameterTrees: Its children will be visible. + (default=True) + syncExpanded If True, the `expanded` state of this Parameter is + synchronized with all ParameterTrees it is displayed in. + (default=False) title (str or None) If specified, then the parameter will be displayed to the user using this string as its name. However, the parameter will still be referred to @@ -159,6 +164,7 @@ class Parameter(QtCore.QObject): 'removable': False, 'strictNaming': False, # forces name to be usable as a python variable 'expanded': True, + 'syncExpanded': False, 'title': None, #'limits': None, ## This is a bad plan--each parameter type may have a different data type for limits. } @@ -199,6 +205,8 @@ class Parameter(QtCore.QObject): self.sigDefaultChanged.connect(lambda param, data: self.emitStateChanged('default', data)) self.sigNameChanged.connect(lambda param, data: self.emitStateChanged('name', data)) self.sigOptionsChanged.connect(lambda param, data: self.emitStateChanged('options', data)) + self.sigContextMenu.connect(lambda param, data: self.emitStateChanged('contextMenu', data)) + #self.watchParam(self) ## emit treechange signals if our own state changes @@ -206,6 +214,10 @@ class Parameter(QtCore.QObject): """Return the name of this Parameter.""" return self.opts['name'] + def contextMenu(self, name): + """"A context menu entry was clicked""" + self.sigContextMenu.emit(self, name) + def setName(self, name): """Attempt to change the name of this parameter; return the actual name. (The parameter may reject the name change or automatically pick a different name)""" @@ -453,7 +465,7 @@ class Parameter(QtCore.QObject): Set any arbitrary options on this parameter. The exact behavior of this function will depend on the parameter type, but most parameters will accept a common set of options: value, name, limits, - default, readonly, removable, renamable, visible, enabled, and expanded. + default, readonly, removable, renamable, visible, enabled, expanded and syncExpanded. See :func:`Parameter.__init__ ` for more information on default options. diff --git a/pyqtgraph/parametertree/ParameterItem.py b/pyqtgraph/parametertree/ParameterItem.py index c149c411..b697b956 100644 --- a/pyqtgraph/parametertree/ParameterItem.py +++ b/pyqtgraph/parametertree/ParameterItem.py @@ -34,19 +34,20 @@ class ParameterItem(QtGui.QTreeWidgetItem): param.sigOptionsChanged.connect(self.optsChanged) param.sigParentChanged.connect(self.parentChanged) - opts = param.opts + self.updateFlags() + + ## flag used internally during name editing + self.ignoreNameColumnChange = False + + def updateFlags(self): + ## called when Parameter opts changed + opts = self.param.opts - ## Generate context menu for renaming/removing parameter - self.contextMenu = QtGui.QMenu() - self.contextMenu.addSeparator() flags = QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled if opts.get('renamable', False): - if param.opts.get('title', None) is not None: + if opts.get('title', None) is not None: raise Exception("Cannot make parameter with both title != None and renamable == True.") flags |= QtCore.Qt.ItemIsEditable - self.contextMenu.addAction('Rename').triggered.connect(self.editName) - if opts.get('removable', False): - self.contextMenu.addAction("Remove").triggered.connect(self.requestRemove) ## handle movable / dropEnabled options if opts.get('movable', False): @@ -54,10 +55,7 @@ class ParameterItem(QtGui.QTreeWidgetItem): if opts.get('dropEnabled', False): flags |= QtCore.Qt.ItemIsDropEnabled self.setFlags(flags) - - ## flag used internally during name editing - self.ignoreNameColumnChange = False - + def valueChanged(self, param, val): ## called when the parameter's value has changed @@ -106,9 +104,31 @@ class ParameterItem(QtGui.QTreeWidgetItem): pass def contextMenuEvent(self, ev): - if not self.param.opts.get('removable', False) and not self.param.opts.get('renamable', False): + opts = self.param.opts + + if not opts.get('removable', False) and not opts.get('renamable', False)\ + and "context" not in opts: return - + + ## Generate context menu for renaming/removing parameter + self.contextMenu = QtGui.QMenu() # Put in global name space to prevent garbage collection + self.contextMenu.addSeparator() + if opts.get('renamable', False): + self.contextMenu.addAction('Rename').triggered.connect(self.editName) + if opts.get('removable', False): + self.contextMenu.addAction("Remove").triggered.connect(self.requestRemove) + + # context menu + context = opts.get('context', None) + if isinstance(context, list): + for name in context: + self.contextMenu.addAction(name).triggered.connect( + self.contextMenuTriggered(name)) + elif isinstance(context, dict): + for name, title in context.items(): + self.contextMenu.addAction(title).triggered.connect( + self.contextMenuTriggered(name)) + self.contextMenu.popup(ev.globalPos()) def columnChangedEvent(self, col): @@ -129,6 +149,10 @@ class ParameterItem(QtGui.QTreeWidgetItem): self.nameChanged(self, newName) ## If the parameter rejects the name change, we need to set it back. finally: self.ignoreNameColumnChange = False + + def expandedChangedEvent(self, expanded): + if self.param.opts['syncExpanded']: + self.param.setOpts(expanded=expanded) def nameChanged(self, param, name): ## called when the parameter's name has changed. @@ -146,10 +170,27 @@ class ParameterItem(QtGui.QTreeWidgetItem): def optsChanged(self, param, opts): """Called when any options are changed that are not name, value, default, or limits""" - #print opts if 'visible' in opts: self.setHidden(not opts['visible']) + + if 'expanded' in opts: + if self.param.opts['syncExpanded']: + if self.isExpanded() != opts['expanded']: + self.setExpanded(opts['expanded']) + if 'syncExpanded' in opts: + if opts['syncExpanded']: + if self.isExpanded() != self.param.opts['expanded']: + self.setExpanded(self.param.opts['expanded']) + + self.updateFlags() + + + def contextMenuTriggered(self, name): + def trigger(): + self.param.contextMenu(name) + return trigger + def editName(self): self.treeWidget().editItem(self, 0) diff --git a/pyqtgraph/parametertree/ParameterTree.py b/pyqtgraph/parametertree/ParameterTree.py index ef7c1030..de6ab126 100644 --- a/pyqtgraph/parametertree/ParameterTree.py +++ b/pyqtgraph/parametertree/ParameterTree.py @@ -28,6 +28,8 @@ class ParameterTree(TreeWidget): self.header().setResizeMode(QtGui.QHeaderView.ResizeToContents) self.setHeaderHidden(not showHeader) self.itemChanged.connect(self.itemChangedEvent) + self.itemExpanded.connect(self.itemExpandedEvent) + self.itemCollapsed.connect(self.itemCollapsedEvent) self.lastSel = None self.setRootIsDecorated(False) @@ -134,6 +136,14 @@ class ParameterTree(TreeWidget): def itemChangedEvent(self, item, col): if hasattr(item, 'columnChangedEvent'): item.columnChangedEvent(col) + + def itemExpandedEvent(self, item): + if hasattr(item, 'expandedChangedEvent'): + item.expandedChangedEvent(True) + + def itemCollapsedEvent(self, item): + if hasattr(item, 'expandedChangedEvent'): + item.expandedChangedEvent(False) def selectionChanged(self, *args): sel = self.selectedItems() diff --git a/pyqtgraph/parametertree/parameterTypes.py b/pyqtgraph/parametertree/parameterTypes.py index 8d65767d..f1c05179 100644 --- a/pyqtgraph/parametertree/parameterTypes.py +++ b/pyqtgraph/parametertree/parameterTypes.py @@ -44,10 +44,6 @@ class WidgetParameterItem(ParameterItem): self.widget = w self.eventProxy = EventProxy(w, self.widgetEventFilter) - opts = self.param.opts - if 'tip' in opts: - w.setToolTip(opts['tip']) - self.defaultBtn = QtGui.QPushButton() self.defaultBtn.setFixedWidth(20) self.defaultBtn.setFixedHeight(20) @@ -73,6 +69,7 @@ class WidgetParameterItem(ParameterItem): w.sigChanging.connect(self.widgetValueChanging) ## update value shown in widget. + opts = self.param.opts if opts.get('value', None) is not None: self.valueChanged(self, opts['value'], force=True) else: @@ -80,6 +77,8 @@ class WidgetParameterItem(ParameterItem): self.widgetValueChanged() self.updateDefaultBtn() + + self.optsChanged(self.param, self.param.opts) def makeWidget(self): """ @@ -280,6 +279,9 @@ class WidgetParameterItem(ParameterItem): if isinstance(self.widget, (QtGui.QCheckBox,ColorButton)): self.widget.setEnabled(not opts['readonly']) + if 'tip' in opts: + self.widget.setToolTip(opts['tip']) + ## If widget is a SpinBox, pass options straight through if isinstance(self.widget, SpinBox): # send only options supported by spinbox @@ -426,10 +428,13 @@ class GroupParameterItem(ParameterItem): def treeWidgetChanged(self): ParameterItem.treeWidgetChanged(self) - self.treeWidget().setFirstItemColumnSpanned(self, True) + tw = self.treeWidget() + if tw is None: + return + tw.setFirstItemColumnSpanned(self, True) if self.addItem is not None: - self.treeWidget().setItemWidget(self.addItem, 0, self.addWidgetBox) - self.treeWidget().setFirstItemColumnSpanned(self.addItem, True) + tw.setItemWidget(self.addItem, 0, self.addWidgetBox) + tw.setFirstItemColumnSpanned(self.addItem, True) def addChild(self, child): ## make sure added childs are actually inserted before add btn if self.addItem is not None: @@ -437,8 +442,10 @@ class GroupParameterItem(ParameterItem): else: ParameterItem.addChild(self, child) - def optsChanged(self, param, changed): - if 'addList' in changed: + def optsChanged(self, param, opts): + ParameterItem.optsChanged(self, param, opts) + + if 'addList' in opts: self.updateAddList() def updateAddList(self): @@ -612,7 +619,10 @@ class ActionParameterItem(ParameterItem): self.layout = QtGui.QHBoxLayout() self.layout.setContentsMargins(0, 0, 0, 0) self.layoutWidget.setLayout(self.layout) - self.button = QtGui.QPushButton(param.name()) + title = param.opts.get('title', None) + if title is None: + title = param.name() + self.button = QtGui.QPushButton(title) #self.layout.addSpacing(100) self.layout.addWidget(self.button) self.layout.addStretch() @@ -659,8 +669,12 @@ class TextParameterItem(WidgetParameterItem): ## TODO: fix so that superclass method can be called ## (WidgetParameter should just natively support this style) #WidgetParameterItem.treeWidgetChanged(self) - self.treeWidget().setFirstItemColumnSpanned(self.subItem, True) - self.treeWidget().setItemWidget(self.subItem, 0, self.textBox) + tw = self.treeWidget() + if tw is None: + return + + tw.setFirstItemColumnSpanned(self.subItem, True) + tw.setItemWidget(self.subItem, 0, self.textBox) # for now, these are copied from ParameterItem.treeWidgetChanged self.setHidden(not self.param.opts.get('visible', True)) diff --git a/pyqtgraph/pgcollections.py b/pyqtgraph/pgcollections.py index ef3db258..49ed4ed6 100644 --- a/pyqtgraph/pgcollections.py +++ b/pyqtgraph/pgcollections.py @@ -2,7 +2,7 @@ """ advancedTypes.py - Basic data structures not included with python Copyright 2010 Luke Campagnola -Distributed under MIT/X11 license. See license.txt for more infomation. +Distributed under MIT/X11 license. See license.txt for more information. Includes: - OrderedDict - Dictionary which preserves the order of its elements diff --git a/pyqtgraph/pixmaps/compile.py b/pyqtgraph/pixmaps/compile.py index fa0d2408..68fd2da1 100644 --- a/pyqtgraph/pixmaps/compile.py +++ b/pyqtgraph/pixmaps/compile.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- import numpy as np from PyQt4 import QtGui import os, pickle, sys @@ -14,6 +15,5 @@ for f in os.listdir(path): arr = np.asarray(ptr).reshape(img.height(), img.width(), 4).transpose(1,0,2) pixmaps[f] = pickle.dumps(arr) ver = sys.version_info[0] -fh = open(os.path.join(path, 'pixmapData_%d.py' %ver), 'w') -fh.write("import numpy as np; pixmapData=%s" % repr(pixmaps)) - +with open(os.path.join(path, 'pixmapData_%d.py' % (ver, )), 'w') as fh: + fh.write("import numpy as np; pixmapData=%s" % (repr(pixmaps), )) diff --git a/pyqtgraph/ptime.py b/pyqtgraph/ptime.py index 1de8282f..eb012934 100644 --- a/pyqtgraph/ptime.py +++ b/pyqtgraph/ptime.py @@ -2,27 +2,34 @@ """ ptime.py - Precision time function made os-independent (should have been taken care of by python) Copyright 2010 Luke Campagnola -Distributed under MIT/X11 license. See license.txt for more infomation. +Distributed under MIT/X11 license. See license.txt for more information. """ import sys -import time as systime + +if sys.version_info[0] < 3: + from time import clock + from time import time as system_time +else: + from time import perf_counter as clock + from time import time as system_time + START_TIME = None time = None def winTime(): """Return the current time in seconds with high precision (windows version, use Manager.time() to stay platform independent).""" - return systime.clock() + START_TIME + return clock() - START_TIME #return systime.time() def unixTime(): """Return the current time in seconds with high precision (unix version, use Manager.time() to stay platform independent).""" - return systime.time() + return system_time() if sys.platform.startswith('win'): - cstart = systime.clock() ### Required to start the clock in windows - START_TIME = systime.time() - cstart + cstart = clock() ### Required to start the clock in windows + START_TIME = system_time() - cstart time = winTime else: diff --git a/pyqtgraph/python2_3.py b/pyqtgraph/python2_3.py index ae4667eb..952b49b1 100644 --- a/pyqtgraph/python2_3.py +++ b/pyqtgraph/python2_3.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """ Helper functions that smooth out the differences between python 2 and 3. """ @@ -13,46 +14,12 @@ def asUnicode(x): return unicode(x) else: return str(x) - -def cmpToKey(mycmp): - 'Convert a cmp= function into a key= function' - class K(object): - def __init__(self, obj, *args): - self.obj = obj - def __lt__(self, other): - return mycmp(self.obj, other.obj) < 0 - def __gt__(self, other): - return mycmp(self.obj, other.obj) > 0 - def __eq__(self, other): - return mycmp(self.obj, other.obj) == 0 - def __le__(self, other): - return mycmp(self.obj, other.obj) <= 0 - def __ge__(self, other): - return mycmp(self.obj, other.obj) >= 0 - def __ne__(self, other): - return mycmp(self.obj, other.obj) != 0 - return K -def sortList(l, cmpFunc): - if sys.version_info[0] == 2: - l.sort(cmpFunc) - else: - l.sort(key=cmpToKey(cmpFunc)) if sys.version_info[0] == 3: basestring = str - def cmp(a,b): - if a>b: - return 1 - elif b > a: - return -1 - else: - return 0 xrange = range else: import __builtin__ basestring = __builtin__.basestring - cmp = __builtin__.cmp xrange = __builtin__.xrange - - \ No newline at end of file diff --git a/pyqtgraph/reload.py b/pyqtgraph/reload.py index f6c630b9..b0c875f1 100644 --- a/pyqtgraph/reload.py +++ b/pyqtgraph/reload.py @@ -306,7 +306,8 @@ if __name__ == '__main__': import os if not os.path.isdir('test1'): os.mkdir('test1') - open('test1/__init__.py', 'w') + with open('test1/__init__.py', 'w'): + pass modFile1 = "test1/test1.py" modCode1 = """ import sys @@ -345,8 +346,10 @@ def fn(): print("fn: %s") """ - open(modFile1, 'w').write(modCode1%(1,1)) - open(modFile2, 'w').write(modCode2%"message 1") + with open(modFile1, 'w') as f: + f.write(modCode1 % (1, 1)) + with open(modFile2, 'w') as f: + f.write(modCode2 % ("message 1", )) import test1.test1 as test1 import test2 print("Test 1 originals:") @@ -382,7 +385,8 @@ def fn(): c1.fn() os.remove(modFile1+'c') - open(modFile1, 'w').write(modCode1%(2,2)) + with open(modFile1, 'w') as f: + f.write(modCode1 %(2, 2)) print("\n----RELOAD test1-----\n") reloadAll(os.path.abspath(__file__)[:10], debug=True) @@ -393,7 +397,8 @@ def fn(): os.remove(modFile2+'c') - open(modFile2, 'w').write(modCode2%"message 2") + with open(modFile2, 'w') as f: + f.write(modCode2 % ("message 2", )) print("\n----RELOAD test2-----\n") reloadAll(os.path.abspath(__file__)[:10], debug=True) @@ -429,8 +434,10 @@ def fn(): os.remove(modFile1+'c') os.remove(modFile2+'c') - open(modFile1, 'w').write(modCode1%(3,3)) - open(modFile2, 'w').write(modCode2%"message 3") + with open(modFile1, 'w') as f: + f.write(modCode1 % (3, 3)) + with open(modFile2, 'w') as f: + f.write(modCode2 % ("message 3", )) print("\n----RELOAD-----\n") reloadAll(os.path.abspath(__file__)[:10], debug=True) diff --git a/pyqtgraph/tests/test_exit_crash.py b/pyqtgraph/tests/test_exit_crash.py index 7c472104..41703ce6 100644 --- a/pyqtgraph/tests/test_exit_crash.py +++ b/pyqtgraph/tests/test_exit_crash.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- import os import sys import subprocess @@ -59,13 +60,14 @@ def test_exit_crash(): print(name) argstr = initArgs.get(name, "") - open(tmp, 'w').write(code.format(path=path, classname=name, args=argstr)) + with open(tmp, 'w') as f: + f.write(code.format(path=path, classname=name, args=argstr)) proc = subprocess.Popen([sys.executable, tmp]) assert proc.wait() == 0 os.remove(tmp) - +@pytest.mark.skipif(pg.Qt.QtVersion.startswith("5.9"), reason="Functionality not well supported, failing only on this config") def test_pg_exit(): # test the pg.exit() function code = textwrap.dedent(""" @@ -74,5 +76,5 @@ def test_pg_exit(): pg.plot() pg.exit() """) - rc = call_with_timeout([sys.executable, '-c', code], timeout=5) + rc = call_with_timeout([sys.executable, '-c', code], timeout=5, shell=False) assert rc == 0 diff --git a/pyqtgraph/tests/test_functions.py b/pyqtgraph/tests/test_functions.py index 68f3dc24..f9320ef2 100644 --- a/pyqtgraph/tests/test_functions.py +++ b/pyqtgraph/tests/test_functions.py @@ -1,11 +1,16 @@ +# -*- coding: utf-8 -*- import pyqtgraph as pg import numpy as np import sys +from copy import deepcopy +from collections import OrderedDict from numpy.testing import assert_array_almost_equal, assert_almost_equal import pytest + np.random.seed(12345) + def testSolve3D(): p1 = np.array([[0,0,0,1], [1,0,0,1], @@ -206,15 +211,15 @@ def test_makeARGB(): # lut smaller than maxint lut = np.arange(128).astype(np.uint8) im2, alpha = pg.makeARGB(im1, lut=lut) - checkImage(im2, np.linspace(0, 127, 256).astype('ubyte'), alpha, False) + checkImage(im2, np.linspace(0, 127.5, 256, dtype='ubyte'), alpha, False) # lut + levels lut = np.arange(256)[::-1].astype(np.uint8) im2, alpha = pg.makeARGB(im1, lut=lut, levels=[-128, 384]) - checkImage(im2, np.linspace(192, 65.5, 256).astype('ubyte'), alpha, False) + checkImage(im2, np.linspace(191.5, 64.5, 256, dtype='ubyte'), alpha, False) im2, alpha = pg.makeARGB(im1, lut=lut, levels=[64, 192]) - checkImage(im2, np.clip(np.linspace(385.5, -126.5, 256), 0, 255).astype('ubyte'), alpha, False) + checkImage(im2, np.clip(np.linspace(384.5, -127.5, 256), 0, 255).astype('ubyte'), alpha, False) # uint8 data + uint16 LUT lut = np.arange(4096)[::-1].astype(np.uint16) // 16 @@ -266,6 +271,30 @@ def test_makeARGB(): im2, alpha = pg.makeARGB(im1, lut=lut, levels=(1, 17)) checkImage(im2, np.linspace(127.5, 0, 256).astype('ubyte'), alpha, False) + # nans in image + + # 2d input image, one pixel is nan + im1 = np.ones((10, 12)) + im1[3, 5] = np.nan + im2, alpha = pg.makeARGB(im1, levels=(0, 1)) + assert alpha + assert im2[3, 5, 3] == 0 # nan pixel is transparent + assert im2[0, 0, 3] == 255 # doesn't affect other pixels + + # 3d RGB input image, any color channel of a pixel is nan + im1 = np.ones((10, 12, 3)) + im1[3, 5, 1] = np.nan + im2, alpha = pg.makeARGB(im1, levels=(0, 1)) + assert alpha + assert im2[3, 5, 3] == 0 # nan pixel is transparent + assert im2[0, 0, 3] == 255 # doesn't affect other pixels + + # 3d RGBA input image, any color channel of a pixel is nan + im1 = np.ones((10, 12, 4)) + im1[3, 5, 1] = np.nan + im2, alpha = pg.makeARGB(im1, levels=(0, 1), useRGBA=True) + assert alpha + assert im2[3, 5, 3] == 0 # nan pixel is transparent # test sanity checks class AssertExc(object): @@ -356,6 +385,31 @@ def test_eq(): assert eq(a4, a4.copy()) assert not eq(a4, a4.T) + # test containers + + assert not eq({'a': 1}, {'a': 1, 'b': 2}) + assert not eq({'a': 1}, {'a': 2}) + d1 = {'x': 1, 'y': np.nan, 3: ['a', np.nan, a3, 7, 2.3], 4: a4} + d2 = deepcopy(d1) + assert eq(d1, d2) + d1_ordered = OrderedDict(d1) + d2_ordered = deepcopy(d1_ordered) + assert eq(d1_ordered, d2_ordered) + assert not eq(d1_ordered, d2) + items = list(d1.items()) + assert not eq(OrderedDict(items), OrderedDict(reversed(items))) + + assert not eq([1,2,3], [1,2,3,4]) + l1 = [d1, np.inf, -np.inf, np.nan] + l2 = deepcopy(l1) + t1 = tuple(l1) + t2 = tuple(l2) + assert eq(l1, l2) + assert eq(t1, t2) + + assert eq(set(range(10)), set(range(10))) + assert not eq(set(range(10)), set(range(9))) + if __name__ == '__main__': - test_interpolateArray() \ No newline at end of file + test_interpolateArray() diff --git a/pyqtgraph/tests/test_qt.py b/pyqtgraph/tests/test_qt.py index c86cd500..3ecf9db8 100644 --- a/pyqtgraph/tests/test_qt.py +++ b/pyqtgraph/tests/test_qt.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- import pyqtgraph as pg import gc, os import pytest @@ -14,6 +15,12 @@ def test_isQObjectAlive(): @pytest.mark.skipif(pg.Qt.QT_LIB == 'PySide', reason='pysideuic does not appear to be ' 'packaged with conda') +@pytest.mark.skipif( + pg.Qt.QT_LIB == "PySide2" + and tuple(map(int, pg.Qt.PySide2.__version__.split("."))) >= (5, 14) + and tuple(map(int, pg.Qt.PySide2.__version__.split("."))) < (5, 14, 2, 2), + reason="new PySide2 doesn't have loadUi functionality" +) def test_loadUiType(): path = os.path.dirname(__file__) formClass, baseClass = pg.Qt.loadUiType(os.path.join(path, 'uictest.ui')) diff --git a/pyqtgraph/tests/test_ref_cycles.py b/pyqtgraph/tests/test_ref_cycles.py index e05c4ef1..121a09e4 100644 --- a/pyqtgraph/tests/test_ref_cycles.py +++ b/pyqtgraph/tests/test_ref_cycles.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """ Test for unwanted reference cycles @@ -9,9 +10,7 @@ import six import pytest app = pg.mkQApp() -skipreason = ('unclear why test is failing on python 3. skipping until someone ' - 'has time to fix it. Or pyside is being used. This test is ' - 'failing on pyside for an unknown reason too.') +skipreason = ('This test is failing on pyside and pyside2 for an unknown reason.') def assert_alldead(refs): for ref in refs: @@ -36,11 +35,10 @@ def mkrefs(*objs): obj = [obj] for o in obj: allObjs[id(o)] = o - - return map(weakref.ref, allObjs.values()) + return [weakref.ref(obj) for obj in allObjs.values()] -@pytest.mark.skipif(six.PY3 or pg.Qt.QT_LIB == 'PySide', reason=skipreason) +@pytest.mark.skipif(pg.Qt.QT_LIB in {'PySide', 'PySide2'}, reason=skipreason) def test_PlotWidget(): def mkobjs(*args, **kwds): w = pg.PlotWidget(*args, **kwds) @@ -58,7 +56,7 @@ def test_PlotWidget(): for i in range(5): assert_alldead(mkobjs()) -@pytest.mark.skipif(six.PY3 or pg.Qt.QT_LIB == 'PySide', reason=skipreason) +@pytest.mark.skipif(pg.Qt.QT_LIB in {'PySide', 'PySide2'}, reason=skipreason) def test_ImageView(): def mkobjs(): iv = pg.ImageView() @@ -66,12 +64,12 @@ def test_ImageView(): iv.setImage(data) return mkrefs(iv, iv.imageItem, iv.view, iv.ui.histogram, data) - for i in range(5): + gc.collect() assert_alldead(mkobjs()) -@pytest.mark.skipif(six.PY3 or pg.Qt.QT_LIB == 'PySide', reason=skipreason) +@pytest.mark.skipif(pg.Qt.QT_LIB in {'PySide', 'PySide2'}, reason=skipreason) def test_GraphicsWindow(): def mkobjs(): w = pg.GraphicsWindow() diff --git a/pyqtgraph/tests/uictest.ui b/pyqtgraph/tests/uictest.ui index 25d14f2b..a183bdae 100644 --- a/pyqtgraph/tests/uictest.ui +++ b/pyqtgraph/tests/uictest.ui @@ -11,7 +11,7 @@ - Form + PyQtGraph diff --git a/pyqtgraph/widgets/BusyCursor.py b/pyqtgraph/widgets/BusyCursor.py index e7a26810..f6bbc84c 100644 --- a/pyqtgraph/widgets/BusyCursor.py +++ b/pyqtgraph/widgets/BusyCursor.py @@ -1,4 +1,4 @@ -from ..Qt import QtGui, QtCore +from ..Qt import QtGui, QtCore, QT_LIB __all__ = ['BusyCursor'] @@ -17,7 +17,12 @@ class BusyCursor(object): app = QtCore.QCoreApplication.instance() isGuiThread = (app is not None) and (QtCore.QThread.currentThread() == app.thread()) if isGuiThread and QtGui.QApplication.instance() is not None: - QtGui.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor)) + if QT_LIB == 'PySide': + # pass CursorShape rather than QCursor for PySide + # see https://bugreports.qt.io/browse/PYSIDE-243 + QtGui.QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) + else: + QtGui.QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.WaitCursor)) BusyCursor.active.append(self) self._active = True else: @@ -27,4 +32,3 @@ class BusyCursor(object): if self._active: BusyCursor.active.pop(-1) QtGui.QApplication.restoreOverrideCursor() - \ No newline at end of file diff --git a/pyqtgraph/widgets/GradientWidget.py b/pyqtgraph/widgets/GradientWidget.py index ce0cbeb9..77881b30 100644 --- a/pyqtgraph/widgets/GradientWidget.py +++ b/pyqtgraph/widgets/GradientWidget.py @@ -71,4 +71,7 @@ class GradientWidget(GraphicsView): ### wrap methods from GradientEditorItem return getattr(self.item, attr) + def widgetGroupInterface(self): + return (self.sigGradientChanged, self.saveState, self.restoreState) + diff --git a/pyqtgraph/widgets/GraphicsLayoutWidget.py b/pyqtgraph/widgets/GraphicsLayoutWidget.py index 3b41a3ca..6249ba26 100644 --- a/pyqtgraph/widgets/GraphicsLayoutWidget.py +++ b/pyqtgraph/widgets/GraphicsLayoutWidget.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- from ..Qt import QtGui, mkQApp from ..graphicsItems.GraphicsLayout import GraphicsLayout from .GraphicsView import GraphicsView @@ -17,21 +18,20 @@ class GraphicsLayoutWidget(GraphicsView): p2 = w.addPlot(row=0, col=1) v = w.addViewBox(row=1, col=0, colspan=2) - Parameters - ---------- - parent : QWidget or None - The parent widget (see QWidget.__init__) - show : bool - If True, then immediately show the widget after it is created. - If the widget has no parent, then it will be shown inside a new window. - size : (width, height) tuple - Optionally resize the widget. Note: if this widget is placed inside a - layout, then this argument has no effect. - title : str or None - If specified, then set the window title for this widget. - kargs : - All extra arguments are passed to - :func:`GraphicsLayout.__init__() ` + ========= ================================================================= + parent (QWidget or None) The parent widget. + show (bool) If True, then immediately show the widget after it is + created. If the widget has no parent, then it will be shown + inside a new window. + size (width, height) tuple. Optionally resize the widget. Note: if + this widget is placed inside a layout, then this argument has no + effect. + title (str or None) If specified, then set the window title for this + widget. + kargs All extra arguments are passed to + :meth:`GraphicsLayout.__init__ + ` + ========= ================================================================= This class wraps several methods from its internal GraphicsLayout: diff --git a/pyqtgraph/widgets/GraphicsView.py b/pyqtgraph/widgets/GraphicsView.py index 7b8c5986..cfeb4961 100644 --- a/pyqtgraph/widgets/GraphicsView.py +++ b/pyqtgraph/widgets/GraphicsView.py @@ -2,7 +2,7 @@ """ GraphicsView.py - Extension of QGraphicsView Copyright 2010 Luke Campagnola -Distributed under MIT/X11 license. See license.txt for more infomation. +Distributed under MIT/X11 license. See license.txt for more information. """ from ..Qt import QtCore, QtGui, QT_LIB @@ -15,6 +15,7 @@ except ImportError: from ..Point import Point import sys, os +import warnings from .FileDialog import FileDialog from ..GraphicsScene import GraphicsScene import numpy as np @@ -324,9 +325,17 @@ class GraphicsView(QtGui.QGraphicsView): def wheelEvent(self, ev): QtGui.QGraphicsView.wheelEvent(self, ev) if not self.mouseEnabled: - ev.ignore() return - sc = 1.001 ** ev.delta() + + delta = 0 + if QT_LIB in ['PyQt4', 'PySide']: + delta = ev.delta() + else: + delta = ev.angleDelta().x() + if delta == 0: + delta = ev.angleDelta().y() + + sc = 1.001 ** delta #self.scale *= sc #self.updateMatrix() self.scale(sc, sc) @@ -396,5 +405,18 @@ class GraphicsView(QtGui.QGraphicsView): def dragEnterEvent(self, ev): ev.ignore() ## not sure why, but for some reason this class likes to consume drag events - + def _del(self): + try: + if self.parentWidget() is None and self.isVisible(): + msg = "Visible window deleted. To prevent this, store a reference to the window object." + try: + warnings.warn(msg, RuntimeWarning, stacklevel=2) + except TypeError: + # warnings module not available during interpreter shutdown + pass + except RuntimeError: + pass + +if sys.version_info[0] == 3 and sys.version_info[1] >= 4: + GraphicsView.__del__ = GraphicsView._del diff --git a/pyqtgraph/widgets/HistogramLUTWidget.py b/pyqtgraph/widgets/HistogramLUTWidget.py index 9aec837c..5259900c 100644 --- a/pyqtgraph/widgets/HistogramLUTWidget.py +++ b/pyqtgraph/widgets/HistogramLUTWidget.py @@ -13,7 +13,7 @@ __all__ = ['HistogramLUTWidget'] class HistogramLUTWidget(GraphicsView): def __init__(self, parent=None, *args, **kargs): - background = kargs.get('background', 'default') + background = kargs.pop('background', 'default') GraphicsView.__init__(self, parent, useOpenGL=False, background=background) self.item = HistogramLUTItem(*args, **kargs) self.setCentralItem(self.item) diff --git a/pyqtgraph/widgets/MultiPlotWidget.py b/pyqtgraph/widgets/MultiPlotWidget.py index d1f56034..21258839 100644 --- a/pyqtgraph/widgets/MultiPlotWidget.py +++ b/pyqtgraph/widgets/MultiPlotWidget.py @@ -2,7 +2,7 @@ """ MultiPlotWidget.py - Convenience class--GraphicsView widget displaying a MultiPlotItem Copyright 2010 Luke Campagnola -Distributed under MIT/X11 license. See license.txt for more infomation. +Distributed under MIT/X11 license. See license.txt for more information. """ from ..Qt import QtCore from .GraphicsView import GraphicsView diff --git a/pyqtgraph/widgets/PlotWidget.py b/pyqtgraph/widgets/PlotWidget.py index 6e10b13a..610591fb 100644 --- a/pyqtgraph/widgets/PlotWidget.py +++ b/pyqtgraph/widgets/PlotWidget.py @@ -2,7 +2,7 @@ """ PlotWidget.py - Convenience class--GraphicsView widget displaying a single PlotItem Copyright 2010 Luke Campagnola -Distributed under MIT/X11 license. See license.txt for more infomation. +Distributed under MIT/X11 license. See license.txt for more information. """ from ..Qt import QtCore, QtGui @@ -24,6 +24,7 @@ class PlotWidget(GraphicsView): :func:`addItem `, :func:`removeItem `, :func:`clear `, + :func:`setAxisItems `, :func:`setXRange `, :func:`setYRange `, :func:`setRange `, @@ -43,7 +44,7 @@ class PlotWidget(GraphicsView): For all other methods, use :func:`getPlotItem `. """ - def __init__(self, parent=None, background='default', **kargs): + def __init__(self, parent=None, background='default', plotItem=None, **kargs): """When initializing PlotWidget, *parent* and *background* are passed to :func:`GraphicsWidget.__init__() ` and all others are passed @@ -51,11 +52,14 @@ class PlotWidget(GraphicsView): GraphicsView.__init__(self, parent, background=background) self.setSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) self.enableMouse(False) - self.plotItem = PlotItem(**kargs) + if plotItem is None: + self.plotItem = PlotItem(**kargs) + else: + self.plotItem = plotItem self.setCentralItem(self.plotItem) ## Explicitly wrap methods from plotItem ## NOTE: If you change this list, update the documentation above as well. - for m in ['addItem', 'removeItem', 'autoRange', 'clear', 'setXRange', + for m in ['addItem', 'removeItem', 'autoRange', 'clear', 'setAxisItems', 'setXRange', 'setYRange', 'setRange', 'setAspectLocked', 'setMouseEnabled', 'setXLink', 'setYLink', 'enableAutoRange', 'disableAutoRange', 'setLimits', 'register', 'unregister', 'viewRect']: @@ -96,4 +100,4 @@ class PlotWidget(GraphicsView): return self.plotItem - \ No newline at end of file + diff --git a/pyqtgraph/widgets/RawImageWidget.py b/pyqtgraph/widgets/RawImageWidget.py index ef1d7a38..43eafeb6 100644 --- a/pyqtgraph/widgets/RawImageWidget.py +++ b/pyqtgraph/widgets/RawImageWidget.py @@ -1,32 +1,43 @@ +# -*- coding: utf-8 -*- +""" +RawImageWidget.py +Copyright 2010-2016 Luke Campagnola +Distributed under MIT/X11 license. See license.txt for more infomation. +""" + from ..Qt import QtCore, QtGui + try: from ..Qt import QtOpenGL from OpenGL.GL import * + HAVE_OPENGL = True -except Exception: +except (ImportError, AttributeError): # Would prefer `except ImportError` here, but some versions of pyopengl generate # AttributeError upon import HAVE_OPENGL = False -from .. import functions as fn -import numpy as np +from .. import getConfigOption, functions as fn + class RawImageWidget(QtGui.QWidget): """ - Widget optimized for very fast video display. + Widget optimized for very fast video display. Generally using an ImageItem inside GraphicsView is fast enough. On some systems this may provide faster video. See the VideoSpeedTest example for benchmarking. """ + def __init__(self, parent=None, scaled=False): """ - Setting scaled=True will cause the entire image to be displayed within the boundaries of the widget. This also greatly reduces the speed at which it will draw frames. + Setting scaled=True will cause the entire image to be displayed within the boundaries of the widget. + This also greatly reduces the speed at which it will draw frames. """ - QtGui.QWidget.__init__(self, parent=None) - self.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding)) + QtGui.QWidget.__init__(self, parent) + self.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding)) self.scaled = scaled self.opts = None self.image = None - + def setImage(self, img, *args, **kargs): """ img must be ndarray of shape (x,y), (x,y,3), or (x,y,4). @@ -43,22 +54,22 @@ class RawImageWidget(QtGui.QWidget): argb, alpha = fn.makeARGB(self.opts[0], *self.opts[1], **self.opts[2]) self.image = fn.makeQImage(argb, alpha) self.opts = () - #if self.pixmap is None: - #self.pixmap = QtGui.QPixmap.fromImage(self.image) + # if self.pixmap is None: + # self.pixmap = QtGui.QPixmap.fromImage(self.image) p = QtGui.QPainter(self) if self.scaled: rect = self.rect() ar = rect.width() / float(rect.height()) imar = self.image.width() / float(self.image.height()) if ar > imar: - rect.setWidth(int(rect.width() * imar/ar)) + rect.setWidth(int(rect.width() * imar / ar)) else: - rect.setHeight(int(rect.height() * ar/imar)) - + rect.setHeight(int(rect.height() * ar / imar)) + p.drawImage(rect, self.image) else: p.drawImage(QtCore.QPointF(), self.image) - #p.drawPixmap(self.rect(), self.pixmap) + # p.drawPixmap(self.rect(), self.pixmap) p.end() @@ -67,14 +78,18 @@ if HAVE_OPENGL: """ Similar to RawImageWidget, but uses a GL widget to do all drawing. Perfomance varies between platforms; see examples/VideoSpeedTest for benchmarking. + + Checks if setConfigOptions(imageAxisOrder='row-major') was set. """ + def __init__(self, parent=None, scaled=False): - QtOpenGL.QGLWidget.__init__(self, parent=None) + QtOpenGL.QGLWidget.__init__(self, parent) self.scaled = scaled self.image = None self.uploaded = False self.smooth = False self.opts = None + self.row_major = getConfigOption('imageAxisOrder') == 'row-major' def setImage(self, img, *args, **kargs): """ @@ -88,7 +103,7 @@ if HAVE_OPENGL: def initializeGL(self): self.texture = glGenTextures(1) - + def uploadTexture(self): glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, self.texture) @@ -100,17 +115,22 @@ if HAVE_OPENGL: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER) - #glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER) - shape = self.image.shape - - ### Test texture dimensions first - #glTexImage2D(GL_PROXY_TEXTURE_2D, 0, GL_RGBA, shape[0], shape[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, None) - #if glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH) == 0: - #raise Exception("OpenGL failed to create 2D texture (%dx%d); too large for this hardware." % shape[:2]) - - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, shape[0], shape[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, self.image.transpose((1,0,2))) + # glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER) + + if self.row_major: + image = self.image + else: + image = self.image.transpose((1, 0, 2)) + + # ## Test texture dimensions first + # shape = self.image.shape + # glTexImage2D(GL_PROXY_TEXTURE_2D, 0, GL_RGBA, shape[0], shape[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, None) + # if glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH) == 0: + # raise Exception("OpenGL failed to create 2D texture (%dx%d); too large for this hardware." % shape[:2]) + + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.shape[1], image.shape[0], 0, GL_RGBA, GL_UNSIGNED_BYTE, image) glDisable(GL_TEXTURE_2D) - + def paintGL(self): if self.image is None: if self.opts is None: @@ -118,26 +138,23 @@ if HAVE_OPENGL: img, args, kwds = self.opts kwds['useRGBA'] = True self.image, alpha = fn.makeARGB(img, *args, **kwds) - + if not self.uploaded: self.uploadTexture() - + glViewport(0, 0, self.width() * self.devicePixelRatio(), self.height() * self.devicePixelRatio()) glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, self.texture) - glColor4f(1,1,1,1) + glColor4f(1, 1, 1, 1) glBegin(GL_QUADS) - glTexCoord2f(0,0) - glVertex3f(-1,-1,0) - glTexCoord2f(1,0) + glTexCoord2f(0, 1) + glVertex3f(-1, -1, 0) + glTexCoord2f(1, 1) glVertex3f(1, -1, 0) - glTexCoord2f(1,1) + glTexCoord2f(1, 0) glVertex3f(1, 1, 0) - glTexCoord2f(0,1) + glTexCoord2f(0, 0) glVertex3f(-1, 1, 0) glEnd() glDisable(GL_TEXTURE_3D) - - - diff --git a/pyqtgraph/widgets/RemoteGraphicsView.py b/pyqtgraph/widgets/RemoteGraphicsView.py index edf4db3c..9be1b531 100644 --- a/pyqtgraph/widgets/RemoteGraphicsView.py +++ b/pyqtgraph/widgets/RemoteGraphicsView.py @@ -8,6 +8,56 @@ import numpy as np import mmap, tempfile, ctypes, atexit, sys, random __all__ = ['RemoteGraphicsView'] + +class SerializableWheelEvent: + """ + Contains all information of a QWheelEvent, is serializable and can generate QWheelEvents. + + Methods have the functionality of their QWheelEvent equivalent. + """ + def __init__(self, _pos, _globalPos, _delta, _buttons, _modifiers, _orientation): + self._pos = _pos + self._globalPos = _globalPos + self._delta = _delta + self._buttons = _buttons + self._modifiers = _modifiers + self._orientation_vertical = _orientation == QtCore.Qt.Vertical + + def pos(self): + return self._pos + + def globalPos(self): + return self._globalPos + + def delta(self): + return self._delta + + def orientation(self): + if self._orientation_vertical: + return QtCore.Qt.Vertical + else: + return QtCore.Qt.Horizontal + + def angleDelta(self): + if self._orientation_vertical: + return QtCore.QPoint(0, self._delta) + else: + return QtCore.QPoint(self._delta, 0) + + def buttons(self): + return QtCore.Qt.MouseButtons(self._buttons) + + def modifiers(self): + return QtCore.Qt.KeyboardModifiers(self._modifiers) + + def toQWheelEvent(self): + """ + Generate QWheelEvent from SerializableWheelEvent. + """ + if QT_LIB in ['PyQt4', 'PySide']: + return QtGui.QWheelEvent(self.pos(), self.globalPos(), self.delta(), self.buttons(), self.modifiers(), self.orientation()) + else: + return QtGui.QWheelEvent(self.pos(), self.globalPos(), QtCore.QPoint(), self.angleDelta(), self.delta(), self.orientation(), self.buttons(), self.modifiers()) class RemoteGraphicsView(QtGui.QWidget): """ @@ -97,22 +147,34 @@ class RemoteGraphicsView(QtGui.QWidget): p.end() def mousePressEvent(self, ev): - self._view.mousePressEvent(int(ev.type()), ev.pos(), ev.globalPos(), int(ev.button()), int(ev.buttons()), int(ev.modifiers()), _callSync='off') + self._view.mousePressEvent(int(ev.type()), ev.pos(), ev.pos(), int(ev.button()), int(ev.buttons()), int(ev.modifiers()), _callSync='off') ev.accept() return QtGui.QWidget.mousePressEvent(self, ev) def mouseReleaseEvent(self, ev): - self._view.mouseReleaseEvent(int(ev.type()), ev.pos(), ev.globalPos(), int(ev.button()), int(ev.buttons()), int(ev.modifiers()), _callSync='off') + self._view.mouseReleaseEvent(int(ev.type()), ev.pos(), ev.pos(), int(ev.button()), int(ev.buttons()), int(ev.modifiers()), _callSync='off') ev.accept() return QtGui.QWidget.mouseReleaseEvent(self, ev) def mouseMoveEvent(self, ev): - self._view.mouseMoveEvent(int(ev.type()), ev.pos(), ev.globalPos(), int(ev.button()), int(ev.buttons()), int(ev.modifiers()), _callSync='off') + self._view.mouseMoveEvent(int(ev.type()), ev.pos(), ev.pos(), int(ev.button()), int(ev.buttons()), int(ev.modifiers()), _callSync='off') ev.accept() return QtGui.QWidget.mouseMoveEvent(self, ev) def wheelEvent(self, ev): - self._view.wheelEvent(ev.pos(), ev.globalPos(), ev.delta(), int(ev.buttons()), int(ev.modifiers()), int(ev.orientation()), _callSync='off') + delta = 0 + orientation = QtCore.Qt.Horizontal + if QT_LIB in ['PyQt4', 'PySide']: + delta = ev.delta() + orientation = ev.orientation() + else: + delta = ev.angleDelta().x() + if delta == 0: + orientation = QtCore.Qt.Vertical + delta = ev.angleDelta().y() + + serializableEvent = SerializableWheelEvent(ev.pos(), ev.pos(), delta, int(ev.buttons()), int(ev.modifiers()), orientation) + self._view.wheelEvent(serializableEvent, _callSync='off') ev.accept() return QtGui.QWidget.wheelEvent(self, ev) @@ -251,12 +313,9 @@ class Renderer(GraphicsView): btns = QtCore.Qt.MouseButtons(btns) mods = QtCore.Qt.KeyboardModifiers(mods) return GraphicsView.mouseReleaseEvent(self, QtGui.QMouseEvent(typ, pos, gpos, btn, btns, mods)) - - def wheelEvent(self, pos, gpos, d, btns, mods, ori): - btns = QtCore.Qt.MouseButtons(btns) - mods = QtCore.Qt.KeyboardModifiers(mods) - ori = (None, QtCore.Qt.Horizontal, QtCore.Qt.Vertical)[ori] - return GraphicsView.wheelEvent(self, QtGui.QWheelEvent(pos, gpos, d, btns, mods, ori)) + + def wheelEvent(self, ev): + return GraphicsView.wheelEvent(self, ev.toQWheelEvent()) def keyEvent(self, typ, mods, text, autorep, count): typ = QtCore.QEvent.Type(typ) diff --git a/pyqtgraph/widgets/ScatterPlotWidget.py b/pyqtgraph/widgets/ScatterPlotWidget.py index bf8a0f42..08f6d02b 100644 --- a/pyqtgraph/widgets/ScatterPlotWidget.py +++ b/pyqtgraph/widgets/ScatterPlotWidget.py @@ -96,7 +96,7 @@ class ScatterPlotWidget(QtGui.QSplitter): try: self.fieldList.clearSelection() for f in fields: - i = self.fields.keys().index(f) + i = list(self.fields.keys()).index(f) item = self.fieldList.item(i) item.setSelected(True) finally: diff --git a/pyqtgraph/widgets/TableWidget.py b/pyqtgraph/widgets/TableWidget.py index 90b56139..0378b5fc 100644 --- a/pyqtgraph/widgets/TableWidget.py +++ b/pyqtgraph/widgets/TableWidget.py @@ -355,7 +355,8 @@ class TableWidget(QtGui.QTableWidget): fileName = fileName[0] # Qt4/5 API difference if fileName == '': return - open(str(fileName), 'w').write(data) + with open(fileName, 'w') as fd: + fd.write(data) def contextMenuEvent(self, ev): self.contextMenu.popup(ev.globalPos()) diff --git a/pyqtgraph/widgets/ValueLabel.py b/pyqtgraph/widgets/ValueLabel.py index 4e5b3011..b24fb16c 100644 --- a/pyqtgraph/widgets/ValueLabel.py +++ b/pyqtgraph/widgets/ValueLabel.py @@ -1,7 +1,6 @@ from ..Qt import QtCore, QtGui from ..ptime import time from .. import functions as fn -from functools import reduce __all__ = ['ValueLabel'] @@ -54,7 +53,7 @@ class ValueLabel(QtGui.QLabel): self.averageTime = t def averageValue(self): - return reduce(lambda a,b: a+b, [v[1] for v in self.values]) / float(len(self.values)) + return sum(v[1] for v in self.values) / float(len(self.values)) def paintEvent(self, ev): diff --git a/pyqtgraph/widgets/tests/test_histogramlutwidget.py b/pyqtgraph/widgets/tests/test_histogramlutwidget.py new file mode 100644 index 00000000..f8a381a7 --- /dev/null +++ b/pyqtgraph/widgets/tests/test_histogramlutwidget.py @@ -0,0 +1,44 @@ +""" +HistogramLUTWidget test: + +Tests the creation of a HistogramLUTWidget. +""" + +import pyqtgraph as pg +from pyqtgraph.Qt import QtGui +import numpy as np + +def testHistogramLUTWidget(): + pg.mkQApp() + + win = QtGui.QMainWindow() + win.show() + + cw = QtGui.QWidget() + win.setCentralWidget(cw) + + l = QtGui.QGridLayout() + cw.setLayout(l) + l.setSpacing(0) + + v = pg.GraphicsView() + vb = pg.ViewBox() + vb.setAspectLocked() + v.setCentralItem(vb) + l.addWidget(v, 0, 0, 3, 1) + + w = pg.HistogramLUTWidget(background='w') + l.addWidget(w, 0, 1) + + data = pg.gaussianFilter(np.random.normal(size=(256, 256, 3)), (20, 20, 0)) + for i in range(32): + for j in range(32): + data[i*8, j*8] += .1 + img = pg.ImageItem(data) + vb.addItem(img) + vb.autoRange() + + w.setImageItem(img) + + QtGui.QApplication.processEvents() + diff --git a/pytest.ini b/pytest.ini index fa664793..355e9dfd 100644 --- a/pytest.ini +++ b/pytest.ini @@ -4,7 +4,7 @@ xvfb_height = 1080 # use this due to some issues with ndarray reshape errors on CI systems xvfb_colordepth = 24 xvfb_args=-ac +extension GLX +render -addopts = --faulthandler-timeout=15 +faulthandler_timeout = 15 filterwarnings = # comfortable skipping these warnings runtime warnings @@ -12,4 +12,8 @@ filterwarnings = ignore:numpy.ufunc size changed, may indicate binary incompatibility.*:RuntimeWarning # Warnings generated from PyQt5.9 ignore:This method will be removed in future versions. Use 'tree.iter\(\)' or 'list\(tree.iter\(\)\)' instead.:PendingDeprecationWarning - ignore:'U' mode is deprecated\nplugin = open\(filename, 'rU'\):DeprecationWarning \ No newline at end of file + ignore:.*'U' mode is deprecated.*:DeprecationWarning + # py36/pyside2_512 specific issue + ignore:split\(\) requires a non-empty pattern match\.:FutureWarning + # pyqtgraph specific warning we want to ignore during testing + ignore:Visible window deleted. To prevent this, store a reference to the window object. \ No newline at end of file diff --git a/setup.py b/setup.py index a59f7dd5..aa1bb787 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,6 @@ +# -*- coding: utf-8 -*- DESCRIPTION = """\ -PyQtGraph is a pure-python graphics and GUI library built on PyQt4/PySide and +PyQtGraph is a pure-python graphics and GUI library built on PyQt4/PyQt5/PySide/PySide2 and numpy. It is intended for use in mathematics / scientific / engineering applications. @@ -12,14 +13,13 @@ setupOpts = dict( name='pyqtgraph', description='Scientific Graphics and GUI Library for Python', long_description=DESCRIPTION, - license='MIT', + license = 'MIT', url='http://www.pyqtgraph.org', author='Luke Campagnola', author_email='luke.campagnola@gmail.com', classifiers = [ "Programming Language :: Python", "Programming Language :: Python :: 2", - "Programming Language :: Python :: 2.6", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", "Development Status :: 4 - Beta", @@ -141,8 +141,7 @@ setup( package_dir={'pyqtgraph.examples': 'examples'}, ## install examples along with the rest of the source package_data={'pyqtgraph.examples': ['optics/*.gz', 'relativity/presets/*.cfg']}, install_requires = [ - 'numpy', + 'numpy>=1.8.0', ], **setupOpts ) - diff --git a/tools/setupHelpers.py b/tools/setupHelpers.py index 4afec66b..6ebbfa46 100644 --- a/tools/setupHelpers.py +++ b/tools/setupHelpers.py @@ -10,14 +10,15 @@ except ImportError: output = proc.stdout.read() proc.wait() if proc.returncode != 0: - ex = Exception("Process had nonzero return value %d" % proc.returncode) + ex = Exception("Process had nonzero return value " + + "%d " % proc.returncode) ex.returncode = proc.returncode ex.output = output raise ex return output # Maximum allowed repository size difference (in kB) following merge. -# This is used to prevent large files from being inappropriately added to +# This is used to prevent large files from being inappropriately added to # the repository history. MERGE_SIZE_LIMIT = 100 @@ -42,19 +43,19 @@ FLAKE_MANDATORY = set([ 'E901', # SyntaxError or IndentationError 'E902', # IOError - + 'W191', # indentation contains tabs - + 'W601', # .has_key() is deprecated, use ‘in’ 'W602', # deprecated form of raising exception 'W603', # ‘<>’ is deprecated, use ‘!=’ - 'W604', # backticks are deprecated, use ‘repr()’ + 'W604', # backticks are deprecated, use ‘repr()’ ]) FLAKE_RECOMMENDED = set([ 'E124', # closing bracket does not match visual indentation 'E231', # missing whitespace after ‘,’ - + 'E211', # whitespace before ‘(‘ 'E261', # at least two spaces before inline comment 'E271', # multiple spaces after keyword @@ -65,10 +66,10 @@ FLAKE_RECOMMENDED = set([ 'F402', # import module from line N shadowed by loop variable 'F403', # ‘from module import *’ used; unable to detect undefined names 'F404', # future import(s) name after other statements - + 'E501', # line too long (82 > 79 characters) 'E502', # the backslash is redundant between brackets - + 'E702', # multiple statements on one line (semicolon) 'E703', # statement ends with a semicolon 'E711', # comparison to None should be ‘if cond is None:’ @@ -82,7 +83,7 @@ FLAKE_RECOMMENDED = set([ 'F823', # local variable name ... referenced before assignment 'F831', # duplicate argument name in function definition 'F841', # local variable name is assigned to but never used - + 'W292', # no newline at end of file ]) @@ -93,7 +94,7 @@ FLAKE_OPTIONAL = set([ 'E126', # continuation line over-indented for hanging indent 'E127', # continuation line over-indented for visual indent 'E128', # continuation line under-indented for visual indent - + 'E201', # whitespace after ‘(‘ 'E202', # whitespace before ‘)’ 'E203', # whitespace before ‘:’ @@ -105,19 +106,19 @@ FLAKE_OPTIONAL = set([ 'E228', # missing whitespace around modulo operator 'E241', # multiple spaces after ‘,’ 'E251', # unexpected spaces around keyword / parameter equals - 'E262', # inline comment should start with ‘# ‘ - + 'E262', # inline comment should start with ‘# ‘ + 'E301', # expected 1 blank line, found 0 'E302', # expected 2 blank lines, found 0 'E303', # too many blank lines (3) - + 'E401', # multiple imports on one line 'E701', # multiple statements on one line (colon) - + 'W291', # trailing whitespace 'W293', # blank line contains whitespace - + 'W391', # blank line at end of file ]) @@ -128,23 +129,10 @@ FLAKE_IGNORE = set([ ]) -#def checkStyle(): - #try: - #out = check_output(['flake8', '--select=%s' % FLAKE_TESTS, '--statistics', 'pyqtgraph/']) - #ret = 0 - #print("All style checks OK.") - #except Exception as e: - #out = e.output - #ret = e.returncode - #print(out.decode('utf-8')) - #return ret - - def checkStyle(): """ Run flake8, checking only lines that are modified since the last git commit. """ - test = [ 1,2,3 ] - + # First check _all_ code against mandatory error codes print('flake8: check all code against mandatory error set...') errors = ','.join(FLAKE_MANDATORY) @@ -154,39 +142,47 @@ def checkStyle(): output = proc.stdout.read().decode('utf-8') ret = proc.wait() printFlakeOutput(output) - + # Check for DOS newlines print('check line endings in all files...') count = 0 allowedEndings = set([None, '\n']) for path, dirs, files in os.walk('.'): + if path.startswith("." + os.path.sep + ".tox"): + continue for f in files: if os.path.splitext(f)[1] not in ('.py', '.rst'): continue filename = os.path.join(path, f) fh = open(filename, 'U') - x = fh.readlines() - endings = set(fh.newlines if isinstance(fh.newlines, tuple) else (fh.newlines,)) + _ = fh.readlines() + endings = set( + fh.newlines + if isinstance(fh.newlines, tuple) + else (fh.newlines,) + ) endings -= allowedEndings if len(endings) > 0: - print("\033[0;31m" + "File has invalid line endings: %s" % filename + "\033[0m") + print("\033[0;31m" + + "File has invalid line endings: " + + "%s" % filename + "\033[0m") ret = ret | 2 count += 1 print('checked line endings in %d files' % count) - - + + # Next check new code with optional error codes print('flake8: check new code against recommended error set...') diff = subprocess.check_output(['git', 'diff']) - proc = subprocess.Popen(['flake8', '--diff', #'--show-source', + proc = subprocess.Popen(['flake8', '--diff', # '--show-source', '--ignore=' + errors], - stdin=subprocess.PIPE, + stdin=subprocess.PIPE, stdout=subprocess.PIPE) proc.stdin.write(diff) proc.stdin.close() output = proc.stdout.read().decode('utf-8') ret |= printFlakeOutput(output) - + if ret == 0: print('style test passed.') else: @@ -244,14 +240,20 @@ def unitTests(): return ret -def checkMergeSize(sourceBranch=None, targetBranch=None, sourceRepo=None, targetRepo=None): +def checkMergeSize( + sourceBranch=None, + targetBranch=None, + sourceRepo=None, + targetRepo=None +): """ - Check that a git merge would not increase the repository size by MERGE_SIZE_LIMIT. + Check that a git merge would not increase the repository size by + MERGE_SIZE_LIMIT. """ if sourceBranch is None: sourceBranch = getGitBranch() sourceRepo = '..' - + if targetBranch is None: if sourceBranch == 'develop': targetBranch = 'develop' @@ -259,38 +261,38 @@ def checkMergeSize(sourceBranch=None, targetBranch=None, sourceRepo=None, target else: targetBranch = 'develop' targetRepo = '..' - + workingDir = '__merge-test-clone' - env = dict(TARGET_BRANCH=targetBranch, - SOURCE_BRANCH=sourceBranch, - TARGET_REPO=targetRepo, + env = dict(TARGET_BRANCH=targetBranch, + SOURCE_BRANCH=sourceBranch, + TARGET_REPO=targetRepo, SOURCE_REPO=sourceRepo, WORKING_DIR=workingDir, ) - + print("Testing merge size difference:\n" " SOURCE: {SOURCE_REPO} {SOURCE_BRANCH}\n" " TARGET: {TARGET_BRANCH} {TARGET_REPO}".format(**env)) - + setup = """ mkdir {WORKING_DIR} && cd {WORKING_DIR} && git init && git remote add -t {TARGET_BRANCH} target {TARGET_REPO} && - git fetch target {TARGET_BRANCH} && - git checkout -qf target/{TARGET_BRANCH} && + git fetch target {TARGET_BRANCH} && + git checkout -qf target/{TARGET_BRANCH} && git gc -q --aggressive """.format(**env) - + checkSize = """ - cd {WORKING_DIR} && + cd {WORKING_DIR} && du -s . | sed -e "s/\t.*//" """.format(**env) - + merge = """ cd {WORKING_DIR} && - git pull -q {SOURCE_REPO} {SOURCE_BRANCH} && + git pull -q {SOURCE_REPO} {SOURCE_BRANCH} && git gc -q --aggressive """.format(**env) - + try: print("Check out target branch:\n" + setup) check_call(setup, shell=True) @@ -300,13 +302,17 @@ def checkMergeSize(sourceBranch=None, targetBranch=None, sourceRepo=None, target check_call(merge, shell=True) mergeSize = int(check_output(checkSize, shell=True)) print("MERGE SIZE: %d kB" % mergeSize) - + diff = mergeSize - targetSize if diff <= MERGE_SIZE_LIMIT: print("DIFFERENCE: %d kB [OK]" % diff) return 0 else: - print("\033[0;31m" + "DIFFERENCE: %d kB [exceeds %d kB]" % (diff, MERGE_SIZE_LIMIT) + "\033[0m") + print("\033[0;31m" + + "DIFFERENCE: %d kB [exceeds %d kB]" % ( + diff, + MERGE_SIZE_LIMIT) + + "\033[0m") return 2 finally: if os.path.isdir(workingDir): @@ -327,7 +333,11 @@ def mergeTests(): def listAllPackages(pkgroot): path = os.getcwd() n = len(path.split(os.path.sep)) - subdirs = [i[0].split(os.path.sep)[n:] for i in os.walk(os.path.join(path, pkgroot)) if '__init__.py' in i[2]] + subdirs = [ + i[0].split(os.path.sep)[n:] + for i in os.walk(os.path.join(path, pkgroot)) + if '__init__.py' in i[2] + ] return ['.'.join(p) for p in subdirs] @@ -338,48 +348,61 @@ def getInitVersion(pkgroot): init = open(initfile).read() m = re.search(r'__version__ = (\S+)\n', init) if m is None or len(m.groups()) != 1: - raise Exception("Cannot determine __version__ from init file: '%s'!" % initfile) + raise Exception("Cannot determine __version__ from init file: " + + "'%s'!" % initfile) version = m.group(1).strip('\'\"') return version def gitCommit(name): """Return the commit ID for the given name.""" - commit = check_output(['git', 'show', name], universal_newlines=True).split('\n')[0] + commit = check_output( + ['git', 'show', name], + universal_newlines=True).split('\n')[0] assert commit[:7] == 'commit ' return commit[7:] def getGitVersion(tagPrefix): """Return a version string with information about this git checkout. - If the checkout is an unmodified, tagged commit, then return the tag version. - If this is not a tagged commit, return the output of ``git describe --tags``. + If the checkout is an unmodified, tagged commit, then return the tag + version + + If this is not a tagged commit, return the output of + ``git describe --tags`` + If this checkout has been modified, append "+" to the version. """ path = os.getcwd() if not os.path.isdir(os.path.join(path, '.git')): return None - - v = check_output(['git', 'describe', '--tags', '--dirty', '--match=%s*'%tagPrefix]).strip().decode('utf-8') - + + v = check_output(['git', + 'describe', + '--tags', + '--dirty', + '--match=%s*'%tagPrefix]).strip().decode('utf-8') + # chop off prefix assert v.startswith(tagPrefix) v = v[len(tagPrefix):] # split up version parts parts = v.split('-') - + # has working tree been modified? modified = False if parts[-1] == 'dirty': modified = True parts = parts[:-1] - + # have commits been added on top of last tagged version? # (git describe adds -NNN-gXXXXXXX if this is the case) local = None - if len(parts) > 2 and re.match(r'\d+', parts[-2]) and re.match(r'g[0-9a-f]{7}', parts[-1]): + if (len(parts) > 2 and + re.match(r'\d+', parts[-2]) and + re.match(r'g[0-9a-f]{7}', parts[-1])): local = parts[-1] parts = parts[:-2] - + gitVersion = '-'.join(parts) if local is not None: gitVersion += '+' + local @@ -389,7 +412,10 @@ def getGitVersion(tagPrefix): return gitVersion def getGitBranch(): - m = re.search(r'\* (.*)', check_output(['git', 'branch'], universal_newlines=True)) + m = re.search( + r'\* (.*)', + check_output(['git', 'branch'], + universal_newlines=True)) if m is None: return '' else: @@ -397,32 +423,33 @@ def getGitBranch(): def getVersionStrings(pkg): """ - Returns 4 version strings: - + Returns 4 version strings: + * the version string to use for this build, * version string requested with --force-version (or None) * version string that describes the current git checkout (or None). - * version string in the pkg/__init__.py, - + * version string in the pkg/__init__.py, + The first return value is (forceVersion or gitVersion or initVersion). """ - + ## Determine current version string from __init__.py initVersion = getInitVersion(pkgroot=pkg) - ## If this is a git checkout, try to generate a more descriptive version string + # If this is a git checkout + # try to generate a more descriptive version string try: gitVersion = getGitVersion(tagPrefix=pkg+'-') except: gitVersion = None - sys.stderr.write("This appears to be a git checkout, but an error occurred " - "while attempting to determine a version string for the " - "current commit.\n") + sys.stderr.write("This appears to be a git checkout, but an error " + "occurred while attempting to determine a version " + "string for the current commit.\n") sys.excepthook(*sys.exc_info()) # See whether a --force-version flag was given forcedVersion = None - for i,arg in enumerate(sys.argv): + for i, arg in enumerate(sys.argv): if arg.startswith('--force-version'): if arg == '--force-version': forcedVersion = sys.argv[i+1] @@ -431,8 +458,8 @@ def getVersionStrings(pkg): elif arg.startswith('--force-version='): forcedVersion = sys.argv[i].replace('--force-version=', '') sys.argv.pop(i) - - + + ## Finally decide on a version string to use: if forcedVersion is not None: version = forcedVersion @@ -443,7 +470,8 @@ def getVersionStrings(pkg): _, local = gitVersion.split('+') if local != '': version = version + '+' + local - sys.stderr.write("Detected git commit; will use version string: '%s'\n" % version) + sys.stderr.write("Detected git commit; " + + "will use version string: '%s'\n" % version) return version, forcedVersion, gitVersion, initVersion @@ -457,29 +485,31 @@ class DebCommand(Command): maintainer = "Luke Campagnola " debTemplate = "debian" debDir = "deb_build" - + user_options = [] - + def initialize_options(self): self.cwd = None - + def finalize_options(self): self.cwd = os.getcwd() - + def run(self): version = self.distribution.get_version() pkgName = self.distribution.get_name() debName = "python-" + pkgName debDir = self.debDir - - assert os.getcwd() == self.cwd, 'Must be in package root: %s' % self.cwd - + + assert os.getcwd() == self.cwd, 'Must be in package root: ' + + '%s' % self.cwd + if os.path.isdir(debDir): raise Exception('DEB build dir already exists: "%s"' % debDir) sdist = "dist/%s-%s.tar.gz" % (pkgName, version) if not os.path.isfile(sdist): - raise Exception("No source distribution; run `setup.py sdist` first.") - + raise Exception("No source distribution; " + + "run `setup.py sdist` first.") + # copy sdist to build directory and extract os.mkdir(debDir) renamedSdist = '%s_%s.orig.tar.gz' % (debName, version) @@ -489,16 +519,20 @@ class DebCommand(Command): if os.system("cd %s; tar -xzf %s" % (debDir, renamedSdist)) != 0: raise Exception("Error extracting source distribution.") buildDir = '%s/%s-%s' % (debDir, pkgName, version) - + # copy debian control structure print("copytree %s => %s" % (self.debTemplate, buildDir+'/debian')) shutil.copytree(self.debTemplate, buildDir+'/debian') - + # Write new changelog - chlog = generateDebianChangelog(pkgName, 'CHANGELOG', version, self.maintainer) + chlog = generateDebianChangelog( + pkgName, + 'CHANGELOG', + version, + self.maintainer) print("write changelog %s" % buildDir+'/debian/changelog') open(buildDir+'/debian/changelog', 'w').write(chlog) - + # build package print('cd %s; debuild -us -uc' % buildDir) if os.system('cd %s; debuild -us -uc' % buildDir) != 0: @@ -521,43 +555,45 @@ class DebugCommand(Command): class TestCommand(Command): - description = "Run all package tests and exit immediately with informative return code." + description = "Run all package tests and exit immediately with ", \ + "informative return code." user_options = [] - + def run(self): sys.exit(unitTests()) - + def initialize_options(self): pass - + def finalize_options(self): pass - + class StyleCommand(Command): - description = "Check all code for style, exit immediately with informative return code." + description = "Check all code for style, exit immediately with ", \ + "informative return code." user_options = [] - + def run(self): sys.exit(checkStyle()) - + def initialize_options(self): pass - + def finalize_options(self): pass - + class MergeTestCommand(Command): - description = "Run all tests needed to determine whether the current code is suitable for merge." + description = "Run all tests needed to determine whether the current ",\ + "code is suitable for merge." user_options = [] - + def run(self): sys.exit(mergeTests()) - + def initialize_options(self): pass - + def finalize_options(self): pass - diff --git a/tox.ini b/tox.ini index 6bbb5566..130085ba 100644 --- a/tox.ini +++ b/tox.ini @@ -1,22 +1,16 @@ [tox] envlist = - ; qt 5.12.x - py{27,37}-pyside2-pip - py{35,37}-pyqt5-pip + ; qt latest + py{37,38}-{pyqt5,pyside2}_latest - ; qt 5.9.7 - py{27,37}-pyqt5-conda - py{27,37}-pyside2-conda + ; qt 5.12.x (LTS) + py{36,37}-{pyqt5,pyside2}_512 - ; qt 5.6.2 - py35-pyqt5-conda - ; consider dropping support... - ; py35-pyside2-conda + ; qt 5.9.7 (LTS) + py36-{pyqt5,pyside2}_59_conda ; qt 4.8.7 - py{27,36}-pyqt4-conda - py{27,36}-pyside-conda - + py27-{pyqt4,pyside}_conda [base] deps = @@ -30,22 +24,26 @@ deps = [testenv] passenv = DISPLAY XAUTHORITY +setenv = PYTHONWARNINGS=ignore:DEPRECATION::pip._internal.cli.base_command deps= {[base]deps} pytest-cov - pytest-xdist - pytest-faulthandler - pyside2-pip: pyside2 - pyqt5-pip: pyqt5 + h5py + pyside2_512: pyside2>=5.12,<5.13 + pyqt5_512: pyqt5>=5.12,<5.13 + pyside2_latest: pyside2 + pyqt5_latest: pyqt5 conda_deps= - pyside2-conda: pyside2 - pyside-conda: pyside - pyqt5-conda: pyqt - pyqt4-conda: pyqt=4 - + pyside2_59_conda: pyside2=5.9 + pyqt5_59_conda: pyqt=5.9 + pyqt4_conda: pyqt=4 + pyside_conda: pyside + conda_channels= conda-forge + free + commands= python -c "import pyqtgraph as pg; pg.systemInfo()" - pytest {posargs:.} + pytest {posargs:}