-
-
Notifications
You must be signed in to change notification settings - Fork 785
[Qt] Add Selection widget. #3921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
windelbouwman
wants to merge
1
commit into
beeware:main
Choose a base branch
from
windelbouwman:qt-selection-widget
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+86
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| The Selection widget is now supported in the Qt backend. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| from contextlib import contextmanager | ||
|
|
||
| from PySide6.QtWidgets import QComboBox | ||
| from travertino.size import at_least | ||
|
|
||
| from .base import Widget | ||
|
|
||
|
|
||
| class Selection(Widget): | ||
| def create(self): | ||
| self.native = QComboBox() | ||
| self.native.setSizeAdjustPolicy(QComboBox.SizeAdjustPolicy.AdjustToContents) | ||
| self.native.currentTextChanged.connect(self.qt_on_current_text_changed) | ||
| self._send_notifications = True | ||
|
|
||
| @contextmanager | ||
| def suspend_notifications(self): | ||
| self._send_notifications = False | ||
| yield | ||
| self._send_notifications = True | ||
|
|
||
| def qt_on_current_text_changed(self, text): | ||
| if self._send_notifications: | ||
| self.interface.on_change() | ||
|
|
||
| def clear(self): | ||
| self.native.clear() | ||
|
|
||
| def insert(self, index, item): | ||
| self.native.insertItem(index, self.interface._title_for_item(item)) | ||
|
|
||
| def change(self, item): | ||
| index = self.interface._items.index(item) | ||
| with self.suspend_notifications(): | ||
| self.native.setItemText(index, self.interface._title_for_item(item)) | ||
| self.interface.refresh() | ||
|
|
||
| def remove(self, index, item): | ||
| current_index = self.native.currentIndex() | ||
| with self.suspend_notifications(): | ||
| self.native.removeItem(index) | ||
| if index == current_index: | ||
| if self.native.count() > 0: | ||
| self.native.setCurrentIndex(0) | ||
| else: | ||
| self.interface.on_change() | ||
|
|
||
| def select_item(self, index, item): | ||
| self.native.setCurrentIndex(index) | ||
|
|
||
| def get_selected_index(self): | ||
| index = self.native.currentIndex() | ||
| return None if index == -1 else index | ||
|
|
||
| def rehint(self): | ||
| content_size = self.native.sizeHint() | ||
| self.interface.intrinsic.width = at_least(content_size.width()) | ||
| self.interface.intrinsic.height = content_size.height() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from PySide6.QtWidgets import QComboBox | ||
|
|
||
| from .base import SimpleProbe | ||
|
|
||
|
|
||
| class SelectionProbe(SimpleProbe): | ||
| native_class = QComboBox | ||
|
|
||
| def assert_resizes_on_content_change(self): | ||
| pass | ||
|
|
||
| @property | ||
| def titles(self): | ||
| titles = [self.native.itemText(index) for index in range(self.native.count())] | ||
| return titles | ||
|
|
||
| @property | ||
| def selected_title(self): | ||
| if self.native.currentIndex() < 0: | ||
| return None | ||
| else: | ||
| return self.native.currentText() | ||
|
|
||
| async def select_item(self): | ||
| self.native.setCurrentIndex(1) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could use currentIndexChanged signal here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the signal used here make any difference? AFAICT, we get a signal even if the text remains the same. See the
selectionexample - the on_change example handler has 2 copies of every entry, and changing fromdubniumtodubniumtriggers a change.The only oddity here is that the handler accepts an
indexparameter, but the actual content (AFAICT) is the text. That wouldn't be the case if currentIndexChanged was used.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind... I missed that the implementation removes and then readds an item. Good catch @freakboy3742
Edit -- nevermind... I was getting myself messed up a bit in the impl code here.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to Qt documentation, the
currentTextChangedsignal only fires when the text actually changes (which is what we need in our case, and also the reason I used this signal instead ofcurrentIndexChanged). So switching from the firstdubniumto the seconddubniumdoes not fire acurrentTextChangedevent.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@windelbouwman I think using currentIndexChanged would be better -- both GTK and Cocoa backends emit on_change when we change from first Dubium to the second Dubium. So that would be the desired behavior.
EDIT -- this should also remove the need to suspend notifications in the change(self, item) thing but I'm not very sure.