mohass98 / pyqtgraph

Fast data visualization and GUI tools for scientific / engineering applications
https://www.pyqtgraph.org
Other
0 stars 2 forks source link

Change Background Color of the Plot within the Context Menu #5

Open mohass98 opened 5 months ago

mohass98 commented 5 months ago

Description

We want to introduce a feature that allows users to change the background color of a plot directly through its context menu. This enhancement aims to provide a more interactive and customizable plotting experience, catering to different user preferences and improving visual clarity for presentations or data analysis.

Current Behavior

At present, users cannot modify the background color of a plot through the context menu. The plot background is set to a default color and just can be changed within the code.

Expected Behavior:

Users should be able to right-click on a plot to access the context menu and choose a new background color for the plot. The color change should be applied immediately and should remain effective until the plot is reset or the application is closed

Silas-K commented 5 months ago

Created a branch for this.

The first commit in this branch 72fbb4a3fbccfa786c3832a1db334538a486ddff already identified possible areas to make changes to the code: GraphicsScene.py:

  1. Added an action to the context menu of the GraphicsScene
        self.setBackgroundAction = QtGui.QAction(
            QtCore.QCoreApplication.translate("GraphicsScene", "Set Background color"), self
        )
        self.setBackgroundAction.triggered.connect(self.setBackgroundFromContextMenu)
        self.contextMenu.append(self.setBackgroundAction)
  2. Added a new signal -> Pass the new color as argument (str or maybe another datatype?)
    sigBackgroundChanged = QtCore.Signal(str)   ## emitted when the Background Color was changed via the context menu
  3. Emit the signal when Action was triggered
    def setBackgroundFromContextMenu(self):
        # TODO: Remove Debug Statements
        print(self.parent)
        print("BG Changed")
        self.sigBackgroundChanged.emit("#ff000f")

    GraphicsView.py:

    • Listen to signal of the GraphicsScene (which is a member of GraphicsView with the name sceneObj) and connect to setBackground method
      self.sceneObj.sigBackgroundChanged.connect(self.setBackground)

[!NOTE]
Currently a hard-coded color value is used

  • Maybe use the QColorDialog for color picking
  • Or embed a ColorButton in the context menu by inheriting from QWidgetAction as shown here
mohass98 commented 4 months ago

Hello @Silas-K, thank you very much for your first approach.

I have pursued the idea of opening a QColorDialogue. This saves the hex value of the colour in a variable, which is then emitted to the application. I tried it with the Example FlowChart, where it worked. It was even possible to customise the individual backgrounds of both plots here. All you have to do is select the context menu on the respective plot.

I have pushed the change in the commit ad8360e.

The implemented feature looks like this:

FlowChart FlowChart2

To-dos

I have currently imported the QColorDialogue manually. This does not take into account the current import logic of the library.

import weakref
from time import perf_counter, perf_counter_ns

from PyQt6.QtWidgets import QColorDialog

from .. import debug as debug
from .. import getConfigOption
from ..Point import Point

I still have to reference QColorDialogue in the corresponding files. After finishing that, we can then create a PR for the issue.

mohass98 commented 4 months ago

Have fixed the issue with the imports in 205ea2f

mohass98 commented 4 months ago

I opened an Issue https://github.com/pyqtgraph/pyqtgraph/issues/3073 in the Main Repository to get Feedback before opening a PR.

Silas-K commented 4 months ago

I think it's a good idea to create an issue in the official repository to discuss this feature and gather feedback.

Given that I've already opened issue #7 (including a preliminary approach for the solution, see this branch) for a similar feature, do you think it would be appropriate to also create an issue in the official repository for this one? Your thoughts?

mohass98 commented 4 months ago

I think it's a good idea to create an issue in the official repository to discuss this feature and gather feedback.

Given that I've already opened issue #7 (including a preliminary approach for the solution, see this branch) for a similar feature, do you think it would be appropriate to also create an issue in the official repository for this one? Your thoughts?

I really like the idea of changing not only the background but also the plot colour via the context menu. This allows the user to make individual settings to the plot without having to change the code. In my opinion, you should publish this as an issue in the main repository to get feedback from the community.

mohass98 commented 4 months ago

I ran the local test on this and it produced the same results as in the main branch.

Regular tests:

Example tests:

I opened now a PR #3080 for this.

Silas-K commented 4 months ago

Based on the feedback from the Pull Request pyqtgraph/pyqtgraph#3080 i created another branch Change-BackgroundColor-in-ContextMenu-Refactored based on this one to include the feedback.

It is not perfect so please review the commit 4410242 before updating and merging into Change-BackgroundColor-in-ContextMenu to automatically update the pull request.