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

WIP: Refactor Label to MVC #46

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions pdsview/channels_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def __init__(self, model):
self.controller = ChannelsDialogController(model, self)
super(ChannelsDialog, self).__init__()

#Setting window details
self.setWindowTitle("Channel")

# Create display of image names and highlight the current image/channel
self.image_tree = QtWidgets.QTreeWidget()
self.image_tree.setColumnCount(1)
Expand Down
30 changes: 27 additions & 3 deletions pdsview/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,32 @@
from pdsview import textfinder


class LabelModel(object):
def __init__(self):
self._views = set()
# self.parent = parent

def register(self, view):
self._views.add(view)

def unregister(self, view):
self._views.remove(view)


class LabelController(object):
def __init__(self, model, view):
self.model = model
self.view = view


class LabelView(QtWidgets.QDialog):
"""A PDS image label viewer."""

def __init__(self, parent):
def __init__(self, model, parent):
self.model = model
self.model.register(self)
self.controller = LabelController(model, self)
super(LabelView, self).__init__(parent)

# Initialize the subdialogs
self._finder_window = None

self.parent = parent
Expand All @@ -47,6 +66,11 @@ def __init__(self, parent):

# Setting up the label and adding it to the label field.
self.label_contents.setText('\n'.join(self.parent.image_label))
# print(len(self.parent.image_label))
self.doc_len = len(self.parent.image_label)
# print(self.doc_len)
# for i in self.parent.image_label:
# print(i)

# Creating and binding the buttons.
self.find_button = QtWidgets.QPushButton("Find")
Expand Down
21 changes: 14 additions & 7 deletions pdsview/pdsview.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

from .histogram import HistogramWidget, HistogramModel
from .channels_dialog import ChannelsDialog, ChannelsDialogModel

try:
from . import label
except ImportError:
from pdsview import label


app = QtWidgets.QApplication.instance()
if not app:
app = QtWidgets.QApplication(sys.argv)
Expand Down Expand Up @@ -64,7 +64,8 @@ def __init__(self, filepath, name, pds_image, data_np, metadata=None,
BaseImage.__init__(self, data_np=data_np, metadata=metadata,
logger=logger)
self.set_data(data_np)
with open(filepath, 'rb') as f:
_open = gzip.open if filepath.endswith('gz') else open
with _open(filepath, 'rb') as f:
label_array = []
for lineno, line in enumerate(f):
line = line.decode().rstrip()
Expand Down Expand Up @@ -127,7 +128,7 @@ def __init__(self, filepaths):
# self._last_channel = None
self._x_value = 0
self._y_value = 0
self._pixel_value = (0, )
self._pixel_value = (0,)
self.use_default_text = True
self.rgb = []
if self.images:
Expand Down Expand Up @@ -288,6 +289,7 @@ def wrapper(self):
)
else:
return func(self)

return wrapper

@_create_rgb_image_wrapper
Expand Down Expand Up @@ -477,7 +479,6 @@ def ROI_max(


class PDSController(object):

def __init__(self, model, view):
self.model = model
self.view = view
Expand Down Expand Up @@ -603,7 +604,7 @@ def __init__(self, image_set):
self.pixel_value_lbl = QtWidgets.QLabel(
'R: ######, G: ###### B: ######')

self.pixels = QtWidgets.QLabel('#Pixels: #######')
self.pixels = QtWidgets.QLabel('Pixels: #######')
self.std_dev = QtWidgets.QLabel(
'Std Dev: R: ######### G: ######### B: #########')
self.mean = QtWidgets.QLabel(
Expand Down Expand Up @@ -771,7 +772,9 @@ def wrapper(self):
elif channel_was_changed:
self._renew_display_values()
return result

return wrapper

return decorator

@_change_wrapper(True)
Expand Down Expand Up @@ -872,14 +875,18 @@ def display_label(self):
# Utilizing the sub window variables to check if the label window has
# been opened before. If not, the window is initialized.
if self._label_window is None:
self._label_window = label.LabelView(self)
label_model = label.LabelModel()
self._label_window = label.LabelView(label_model, self)
self._label_window.is_open = True
self._label_window.show()
self._label_window.activateWindow()

def _update_label(self):
# Update label
self.image_label = self.current_image.label
# print("Type of image label", len(self.image_label))
# for i in self.image_label:
# print(i)

# This checks to see if the label window exists and is open. If so,
# this resets the label field so that the label being displayed is the
Expand Down Expand Up @@ -1400,6 +1407,6 @@ def cli():
parser.add_argument(
'file', nargs='*',
help="Input filename or glob for files with certain extensions"
)
)
args = parser.parse_args()
pdsview(args.file)
104 changes: 92 additions & 12 deletions pdsview/textfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@
from qtpy import QtWidgets, QtCore, QtGui


# class LabelSearchModel(object):
# def __init__(self):
# self._views = set()
#
# def register(self, view):
# self._views.add(view)
#
# def unregister(self, view):
# self._views.remove(view)
#
#
# class LabelSearchController(object):
#
# pass
#
#
# class LabelSearchView(QtWidgets.QDialog):
# pass


class LabelSearch(QtWidgets.QDialog):
"""A simple search tool for text widgets."""

Expand All @@ -33,49 +53,109 @@ def __init__(self, parent):
# This is a live search, so the only button needed is one to hide the
# window.
self.ok_button = QtWidgets.QPushButton("Ok")
self.ok_button.clicked.connect(self.cancel)
self.previous_button = QtWidgets.QPushButton("Previous")
self.next_button = QtWidgets.QPushButton("Next")
self.ok_button.clicked.connect(self.highlighter)
self.next_button.clicked.connect(self.click_next)
self.previous_button.clicked.connect(self.click_previous)

self.layout = QtWidgets.QVBoxLayout()
self.hlayout = QtWidgets.QHBoxLayout()
self.layout.addWidget(self.find_field)
self.layout.addWidget(self.ok_button)
self.hlayout.addWidget(self.ok_button)
self.hlayout.addWidget(self.previous_button)
self.hlayout.addWidget(self.next_button)

self.layout.addLayout(self.hlayout)

self.setLayout(self.layout)

self.find_field.textChanged.connect(self.highlighter)
self.list_of_indexes = list()
self.counter = 0

self.query_primary_color = QtGui.QTextCharFormat()
self.query_primary_color.setBackground(QtGui.QBrush(QtGui.QColor("red")))

self.query_secondary_color = QtGui.QTextCharFormat()
self.query_secondary_color.setBackground(QtGui.QBrush(QtGui.QColor("yellow")))

self.slider = self.parent.label_contents.verticalScrollBar()
self.scale_constant = 11
self.max_range = self.parent.doc_len * self.scale_constant
self.slider.setRange(0, self.max_range)
# self.slider.setMinimum(0)
# self.slider.setMaximum(self.parent.doc_len)

# self.find_field.textChanged.connect(self.highlighter)

def highlighter(self):
self.list_of_indexes = list()
self.cursor = self.parent.label_contents.textCursor()
self.highlight_reset()

self.query_edit = True

# Setting and using the query. X.toPlainText() returns an empty string
# print(self.parent.doc_len)
# Setting and using the query. X.toPlainText() returns
# an empty string
# if there is nothing in the box. The search spazzes out if there is an
# empty string, so the "if" filter is needed. This method does nothing
# if the query is an empty string.
query = self.find_field.toPlainText()
if(query != ""):
if query != "":
# Setting the highlight color, the query, and the cursor for the
# label contents window.
query_color = QtGui.QTextCharFormat()
query_color.setBackground(QtGui.QBrush(QtGui.QColor("red")))
regex = QtCore.QRegExp(query)
self.cursor = self.parent.label_contents.textCursor()
pos = 0
index = regex.indexIn(
self.parent.label_contents.toPlainText(), pos)

# This finds and highlights all occurences of the query.
while (index != -1):
while index != -1:
self.cursor.setPosition(index)
self.cursor.movePosition(QtGui.QTextCursor.Right,
QtGui.QTextCursor.KeepAnchor,
len(query))
self.cursor.mergeCharFormat(query_color)

self.cursor.mergeCharFormat(self.query_primary_color)
pos = index + regex.matchedLength()
self.list_of_indexes.append(pos)
index = regex.indexIn(self.parent.label_contents.toPlainText(),
pos)

def click_next(self):
self.cursor.mergeCharFormat(self.query_primary_color)
query = self.find_field.toPlainText()

self.counter += 1
index = self.list_of_indexes[self.counter % (len(self.list_of_indexes))]

self.cursor.setPosition(index - len(query))
self.cursor.movePosition(QtGui.QTextCursor.Right,
QtGui.QTextCursor.KeepAnchor,
len(query))
self.cursor.mergeCharFormat(self.query_secondary_color)
# print(self.cursor.blockNumber())
# print("Slider Value", (self.cursor.blockNumber() * self.scale_constant))
self.slider.setValue(self.cursor.blockNumber() * self.scale_constant)
# print(self.parent.label_contents.verticalScrollBar().value())
# print(index)
# self.parent.label_contents.verticalScrollBar().setValue(index)
# print(self.parent.label_contents.verticalScrollBar().value())

def click_previous(self):
self.cursor.mergeCharFormat(self.query_primary_color)
query = self.find_field.toPlainText()

self.counter -= 1
index = self.list_of_indexes[self.counter % (len(self.list_of_indexes))]

self.cursor.setPosition(index - len(query))
self.cursor.movePosition(QtGui.QTextCursor.Right,
QtGui.QTextCursor.KeepAnchor,
len(query))
self.cursor.mergeCharFormat(self.query_secondary_color)
# print("Slider Value", (self.cursor.blockNumber() * self.scale_constant))
self.slider.setValue(self.cursor.blockNumber() * self.scale_constant)

def highlight_reset(self):
# This method makes sure the text is unhighlighted.
normal_color = QtGui.QTextCharFormat()
Expand Down