Skip to content
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

Add searchable combobox to core.gui #137

Merged
merged 1 commit into from
Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions QtBrainChartGUI/core/gui/SearchableQComboBox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# This Python file uses the following encoding: utf-8
"""
Author: Ashish Singh
contact: software@cbica.upenn.edu
Copyright (c) 2018 University of Pennsylvania. All rights reserved.
Use of this source code is governed by license located in license file: https://github.com/CBICA/BrainChart/blob/main/LICENSE
"""
from PyQt5 import QtCore, QtWidgets

class SearchableQComboBox(QtWidgets.QComboBox):
def __init__(self, parent=None):
super(SearchableQComboBox, self).__init__(parent)

self.setFocusPolicy(QtCore.Qt.StrongFocus)
self.setEditable(True)

# add a filter model to filter matching items
self.pFilterModel = QtCore.QSortFilterProxyModel(self)
self.pFilterModel.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive)
self.pFilterModel.setSourceModel(self.model())

# add a completer, which uses the filter model
self.completer = QtWidgets.QCompleter(self.pFilterModel, self)
# always show all (filtered) completions
self.completer.setCompletionMode(QtWidgets.QCompleter.UnfilteredPopupCompletion)
self.setCompleter(self.completer)

# connect signals
self.lineEdit().textEdited.connect(self.pFilterModel.setFilterFixedString)
self.completer.activated.connect(self.on_completer_activated)


# on selection of an item from the completer, select the corresponding item from combobox
def on_completer_activated(self, text):
if text:
index = self.findText(text)
self.setCurrentIndex(index)
self.activated[str].emit(self.itemText(index))


# on model change, update the models of the filter and completer as well
def setModel(self, model):
super(SearchableQComboBox, self).setModel(model)
self.pFilterModel.setSourceModel(model)
self.completer.setModel(self.pFilterModel)


# on model column change, update the model column of the filter and completer as well
def setModelColumn(self, column):
self.completer.setCompletionColumn(column)
self.pFilterModel.setFilterKeyColumn(column)
super(SearchableQComboBox, self).setModelColumn(column)

51 changes: 3 additions & 48 deletions QtBrainChartGUI/plugins/agetrends/agetrends.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,7 @@
import numpy as np
import pandas as pd
from QtBrainChartGUI.core.plotcanvas import PlotCanvas


class ExtendedComboBox(QtWidgets.QComboBox):
def __init__(self, parent=None):
super(ExtendedComboBox, self).__init__(parent)

self.setFocusPolicy(QtCore.Qt.StrongFocus)
self.setEditable(True)

# add a filter model to filter matching items
self.pFilterModel = QtCore.QSortFilterProxyModel(self)
self.pFilterModel.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive)
self.pFilterModel.setSourceModel(self.model())

# add a completer, which uses the filter model
self.completer = QtWidgets.QCompleter(self.pFilterModel, self)
# always show all (filtered) completions
self.completer.setCompletionMode(QtWidgets.QCompleter.UnfilteredPopupCompletion)
self.setCompleter(self.completer)

# connect signals
self.lineEdit().textEdited.connect(self.pFilterModel.setFilterFixedString)
self.completer.activated.connect(self.on_completer_activated)


# on selection of an item from the completer, select the corresponding item from combobox
def on_completer_activated(self, text):
if text:
index = self.findText(text)
self.setCurrentIndex(index)
self.activated[str].emit(self.itemText(index))


# on model change, update the models of the filter and completer as well
def setModel(self, model):
super(ExtendedComboBox, self).setModel(model)
self.pFilterModel.setSourceModel(model)
self.completer.setModel(self.pFilterModel)


# on model column change, update the model column of the filter and completer as well
def setModelColumn(self, column):
self.completer.setCompletionColumn(column)
self.pFilterModel.setFilterKeyColumn(column)
super(ExtendedComboBox, self).setModelColumn(column)

from QtBrainChartGUI.core.gui.SearchableQComboBox import SearchableQComboBox

class AgeTrends(QtWidgets.QWidget,BasePlugin):

Expand All @@ -66,8 +21,8 @@ def __init__(self):
self.readAdditionalInformation(root)
self.ui = uic.loadUi(os.path.join(root, 'agetrends.ui'),self)
self.plotCanvas = PlotCanvas(self.ui)
self.ui.comboBoxROI = ExtendedComboBox(self.ui)
self.ui.comboBoxHue = ExtendedComboBox(self.ui)
self.ui.comboBoxROI = SearchableQComboBox(self.ui)
self.ui.comboBoxHue = SearchableQComboBox(self.ui)
self.ui.horizontalLayout.addWidget(self.comboBoxROI)
self.ui.horizontalLayout.addWidget(self.comboBoxHue)
self.ui.verticalLayout.addWidget(self.plotCanvas)
Expand Down
49 changes: 2 additions & 47 deletions QtBrainChartGUI/plugins/computeSPAREs/computeSPAREs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,7 @@
import pandas as pd
from QtBrainChartGUI.core.plotcanvas import PlotCanvas
from QtBrainChartGUI.core.baseplugin import BasePlugin


class ExtendedComboBox(QtWidgets.QComboBox):
def __init__(self, parent=None):
super(ExtendedComboBox, self).__init__(parent)

self.setFocusPolicy(QtCore.Qt.StrongFocus)
self.setEditable(True)

# add a filter model to filter matching items
self.pFilterModel = QtCore.QSortFilterProxyModel(self)
self.pFilterModel.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive)
self.pFilterModel.setSourceModel(self.model())

# add a completer, which uses the filter model
self.completer = QtWidgets.QCompleter(self.pFilterModel, self)
# always show all (filtered) completions
self.completer.setCompletionMode(QtWidgets.QCompleter.UnfilteredPopupCompletion)
self.setCompleter(self.completer)

# connect signals
self.lineEdit().textEdited.connect(self.pFilterModel.setFilterFixedString)
self.completer.activated.connect(self.on_completer_activated)


# on selection of an item from the completer, select the corresponding item from combobox
def on_completer_activated(self, text):
if text:
index = self.findText(text)
self.setCurrentIndex(index)
self.activated[str].emit(self.itemText(index))


# on model change, update the models of the filter and completer as well
def setModel(self, model):
super(ExtendedComboBox, self).setModel(model)
self.pFilterModel.setSourceModel(model)
self.completer.setModel(self.pFilterModel)


# on model column change, update the model column of the filter and completer as well
def setModelColumn(self, column):
self.completer.setCompletionColumn(column)
self.pFilterModel.setFilterKeyColumn(column)
super(ExtendedComboBox, self).setModelColumn(column)

from QtBrainChartGUI.core.gui.SearchableQComboBox import SearchableQComboBox

class computeSPAREs(QtWidgets.QWidget,BasePlugin):

Expand All @@ -64,7 +19,7 @@ def __init__(self):
root = os.path.dirname(__file__)
self.readAdditionalInformation(root)
self.ui = uic.loadUi(os.path.join(root, 'computeSPAREs.ui'),self)
self.ui.comboBoxHue = ExtendedComboBox(self.ui)
self.ui.comboBoxHue = SearchableQComboBox(self.ui)
self.ui.horizontalLayout_3.addWidget(self.comboBoxHue)
self.plotCanvas = PlotCanvas(self.ui.page_2)
self.ui.verticalLayout.addWidget(self.plotCanvas)
Expand Down
47 changes: 2 additions & 45 deletions QtBrainChartGUI/plugins/harmonization/harmonization.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,7 @@
import pandas as pd
from QtBrainChartGUI.core.plotcanvas import PlotCanvas
from QtBrainChartGUI.core.baseplugin import BasePlugin

class ExtendedComboBox(QtWidgets.QComboBox):
def __init__(self, parent=None):
super(ExtendedComboBox, self).__init__(parent)

self.setFocusPolicy(QtCore.Qt.StrongFocus)
self.setEditable(True)

# add a filter model to filter matching items
self.pFilterModel = QtCore.QSortFilterProxyModel(self)
self.pFilterModel.setFilterCaseSensitivity(QtCore.Qt.CaseInsensitive)
self.pFilterModel.setSourceModel(self.model())

# add a completer, which uses the filter model
self.completer = QtWidgets.QCompleter(self.pFilterModel, self)
# always show all (filtered) completions
self.completer.setCompletionMode(QtWidgets.QCompleter.UnfilteredPopupCompletion)
self.setCompleter(self.completer)

# connect signals
self.lineEdit().textEdited.connect(self.pFilterModel.setFilterFixedString)
self.completer.activated.connect(self.on_completer_activated)


# on selection of an item from the completer, select the corresponding item from combobox
def on_completer_activated(self, text):
if text:
index = self.findText(text)
self.setCurrentIndex(index)
self.activated[str].emit(self.itemText(index))


# on model change, update the models of the filter and completer as well
def setModel(self, model):
super(ExtendedComboBox, self).setModel(model)
self.pFilterModel.setSourceModel(model)
self.completer.setModel(self.pFilterModel)


# on model column change, update the model column of the filter and completer as well
def setModelColumn(self, column):
self.completer.setCompletionColumn(column)
self.pFilterModel.setFilterKeyColumn(column)
super(ExtendedComboBox, self).setModelColumn(column)
from QtBrainChartGUI.core.gui.SearchableQComboBox import SearchableQComboBox

class Harmonization(QtWidgets.QWidget,BasePlugin):

Expand All @@ -63,7 +20,7 @@ def __init__(self):
self.readAdditionalInformation(root)
self.ui = uic.loadUi(os.path.join(root, 'harmonization.ui'),self)
self.ui.Harmonization_Model_Loaded_Lbl.setHidden(True)
self.ui.comboBoxROI = ExtendedComboBox(self.ui)
self.ui.comboBoxROI = SearchableQComboBox(self.ui)
self.plotCanvas = PlotCanvas(self.ui.page_2)
self.plotCanvas.axes1 = self.plotCanvas.fig.add_subplot(131)
self.plotCanvas.axes2 = self.plotCanvas.fig.add_subplot(132)
Expand Down