Skip to content

Commit

Permalink
ENH: Add 'Select Legend File' button (#45)
Browse files Browse the repository at this point in the history
ENH: Add 'Select Legend File' button

This commit addresses issue #41
  • Loading branch information
JEHoctor authored Jul 13, 2020
1 parent 57875e0 commit c3840c9
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 24 deletions.
69 changes: 50 additions & 19 deletions Q3DC/Q3DC.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import vtk, qt, ctk, slicer
from slicer.ScriptedLoadableModule import *
from slicer.util import NodeModify
import csv, os
import csv
from collections import defaultdict
from pathlib import Path
import json
import time
import logging
import math
import os
import time

import ctk
import numpy as np

# needed for kd-trees
import qt
import scipy.spatial
import vtk

# needed for topological sort. Yes, this is basically just DFS.
try:
Expand All @@ -21,6 +20,10 @@
slicer.util.pip_install('networkx')
import networkx as nx

import slicer
from slicer.ScriptedLoadableModule import *
from slicer.util import NodeModify


#
# CalculateDisplacement
Expand Down Expand Up @@ -100,7 +103,6 @@ def setup(self):
self.anatomical_legend_space = self.ui.landmarkModifLayout
self.anatomical_radio_buttons_layout = qt.QHBoxLayout()
self.anatomical_legend_space.addLayout(self.anatomical_radio_buttons_layout)
self.init_anatomical_radio_buttons()

self.anatomical_legend = None
self.init_anatomical_legend()
Expand All @@ -113,7 +115,9 @@ def setup(self):
)
self.anatomical_legend_view.connect('selectionChanged()', self.on_legend_row_selected)

self.anatomical_radio_buttons[0].toggle()
self.init_anatomical_radio_buttons()

self.ui.legendFileButton.connect('clicked()', self.on_select_legend_file_clicked)

# ----------------- Compute Mid Point -------------
self.ui.landmarkComboBox1.connect('currentIndexChanged(int)', self.UpdateInterface)
Expand Down Expand Up @@ -361,13 +365,14 @@ def init_anatomical_radio_buttons(self):
self.anatomical_radio_buttons = \
[qt.QRadioButton(region) for region in self.suggested_landmarks.keys()]
for i in range(self.anatomical_radio_buttons_layout.count()-1, -1, -1):
self.anatomical_radio_buttons_layout.itemAt[i].widget().setParent(None)
self.anatomical_radio_buttons_layout.itemAt(i).widget().setParent(None)
for radio_button in self.anatomical_radio_buttons:
self.anatomical_radio_buttons_layout.addWidget(radio_button)
radio_button.toggled.connect(
lambda state, _radio_button=radio_button:
self.on_anatomical_radio_button_toggled(state, _radio_button)
)
self.anatomical_radio_buttons[0].toggle()

def on_anatomical_radio_button_toggled(self, state, radio_button):
if state:
Expand Down Expand Up @@ -426,6 +431,19 @@ def on_legend_row_selected(self):
box.setCurrentText(new_selection)
self.UpdateInterface()

def on_select_legend_file_clicked(self):
legend_filename = qt.QFileDialog.getOpenFileName(
None,'Select File', '', 'CSV (*.csv)')
if legend_filename == '':
# User canceled the file selection dialog.
return
suggested_landmarks = self.logic.load_suggested_landmarks(
legend_filename)
if suggested_landmarks is None:
return
self.suggested_landmarks = suggested_landmarks
self.init_anatomical_radio_buttons()

def onModelChanged(self):
print("-------Model Changed--------")
if self.logic.selectedModel:
Expand Down Expand Up @@ -657,14 +675,27 @@ def __init__(self, interface):
@staticmethod
def load_suggested_landmarks(filepath):
suggested_landmarks = defaultdict(list)
with open(filepath, newline='') as suggestions_file:
reader = csv.DictReader(suggestions_file)
for row in reader:
region = row['Region'].title()
landmark = row['Landmark']
name = row['Name']
suggested_landmarks[region].append((landmark, name))
return suggested_landmarks
try:
with open(filepath, newline='') as suggestions_file:
reader = csv.DictReader(suggestions_file)
for row in reader:
region = row['Region'].title()
landmark = row['Landmark']
name = row['Name']
suggested_landmarks[region].append((landmark, name))
return suggested_landmarks
except OSError as e:
slicer.util.delayDisplay('Unable to find/open file.')
logging.info('User attempted to open a landmark legend file.\n' + repr(e))
return None
except csv.Error as e:
slicer.util.delayDisplay('The selected file is not formatted properly.')
logging.info('User attempted to open a landmark legend file.\n' + repr(e))
return None
except KeyError as e:
slicer.util.delayDisplay('The selected file does not have the right column names.')
logging.info('User attempted to open a landmark legend file.\n' + repr(e))
return None

def initComboboxdict(self):
self.comboboxdict[self.interface.landmarkComboBoxA] = None
Expand Down
21 changes: 16 additions & 5 deletions Q3DC/Resources/UI/Q3DC.ui
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,22 @@
</layout>
</item>
<item>
<widget class="QLabel" name="landmarkLegendLabel">
<property name="text">
<string>Landmark Legend</string>
</property>
</widget>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="landmarkLegendLabel">
<property name="text">
<string>Landmark Legend</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="legendFileButton">
<property name="text">
<string>Select Legend File</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
Expand Down

0 comments on commit c3840c9

Please sign in to comment.