Skip to content

Commit

Permalink
first commit from local
Browse files Browse the repository at this point in the history
  • Loading branch information
yjg30737 committed Nov 3, 2023
1 parent 8f9d3d7 commit b8f6cdc
Show file tree
Hide file tree
Showing 16 changed files with 432 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/pyqt_dicom_viewer.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

114 changes: 114 additions & 0 deletions dicomImageView.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import numpy
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtWidgets import QGraphicsScene, QGraphicsView, QWidget, QVBoxLayout, QHBoxLayout, QPushButton


class DicomImageView(QGraphicsView):
def __init__(self):
super().__init__()
self.__aspectRatioMode = Qt.KeepAspectRatio
self.__gradient_enabled = False
self.__initVal()

def __initVal(self):
self._scene = QGraphicsScene()
self._p = QPixmap()
self._item = ''

def setNdArray(self, dicom_file_data, width, height):
# Convert to QImage - for 8-bit grayscale image
q_image = QImage(dicom_file_data, width, height, width, QImage.Format_Grayscale8)

# Convert QImage to QPixmap
self._p = QPixmap.fromImage(q_image)

# The rest of your code remains the same
self._scene = QGraphicsScene()
self._item = self._scene.addPixmap(self._p)
self._item.setTransformationMode(Qt.SmoothTransformation)
self.setScene(self._scene)
self.fitInView(self._item, self.__aspectRatioMode)

def setAspectRatioMode(self, mode):
self.__aspectRatioMode = mode

def resizeEvent(self, e):
if self._item:
self.fitInView(self.sceneRect(), self.__aspectRatioMode)
return super().resizeEvent(e)


class DicomImageViewer(QWidget):
def __init__(self):
super().__init__()
self.__initVal()
self.__initUi()

def __initVal(self):
self.__current_dicom_file_info = []
self.__cur_idx = 0

def __initUi(self):
self.__view = DicomImageView()

self.__prevBtn = QPushButton('Prev')
self.__prevBtn.clicked.connect(self.__prev)
self.__nextBtn = QPushButton('Next')
self.__nextBtn.clicked.connect(self.__next)

lay = QHBoxLayout()
lay.addWidget(self.__prevBtn)
lay.addWidget(self.__nextBtn)
lay.setContentsMargins(0, 0, 0, 0)
btnWidget = QWidget()
btnWidget.setLayout(lay)

lay = QVBoxLayout()
lay.addWidget(self.__view)
lay.addWidget(btnWidget)
lay.setContentsMargins(0, 0, 0, 0)
self.setLayout(lay)

def __toggleBtn(self):
self.__prevBtn.setEnabled(not self.__cur_idx == 0)
self.__nextBtn.setEnabled(not (self.__cur_idx == len(self.__current_dicom_file_info['data']) - 1))

def __setNdArray(self):
self.__view.setNdArray(dicom_file_data=self.__current_dicom_file_info['data'][self.__cur_idx],
width=self.__current_dicom_file_info['width'],
height=self.__current_dicom_file_info['height'])

def setDicomArr(self, dicom_file_info):
self.__cur_idx = 0
self.__current_dicom_file_info = dicom_file_info
self.__setNdArray()
self.__toggleBtn()

def __prev(self):
self.__cur_idx -= 1
self.__cur_idx = max(0, self.__cur_idx)
self.__toggleBtn()
self.__setNdArray()

def __next(self):
self.__cur_idx += 1
self.__cur_idx = min(len(self.__current_dicom_file_info['data'])-1, self.__cur_idx)
self.__toggleBtn()
self.__setNdArray()

def keyReleaseEvent(self, e):
# 16777234 is left
if e.key() == 16777234:
self.__prev()
# 16777236 is right
elif e.key() == 16777236:
self.__next()
return super().keyReleaseEvent(e)

def wheelEvent(self, e):
if e.angleDelta().y() < 0:
self.__next()
else:
self.__prev()
return super().wheelEvent(e)
122 changes: 122 additions & 0 deletions findPathWidget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import subprocess

from PyQt5.QtCore import Qt
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QPushButton, QFileDialog, QHBoxLayout, QLabel, QWidget, QAction, \
QMenu, QLineEdit


class FindPathLineEdit(QLineEdit):
def __init__(self):
super().__init__()
self.__initUi()

def __initUi(self):
self.setMouseTracking(True)
self.setReadOnly(True)
self.setContextMenuPolicy(Qt.CustomContextMenu)
self.customContextMenuRequested.connect(self.__prepareMenu)

def mouseMoveEvent(self, e):
self.__showToolTip()
return super().mouseMoveEvent(e)

def __showToolTip(self):
text = self.text()
text_width = self.fontMetrics().boundingRect(text).width()

if text_width > self.width():
self.setToolTip(text)
else:
self.setToolTip('')

def __prepareMenu(self, pos):
menu = QMenu(self)
openDirAction = QAction('Open Path')
openDirAction.setEnabled(self.text().strip() != '')
openDirAction.triggered.connect(self.__openPath)
menu.addAction(openDirAction)
menu.exec(self.mapToGlobal(pos))

def __openPath(self):
filename = self.text()
path = filename.replace('/', '\\')
subprocess.Popen(r'explorer /select,"' + path + '"')


class FindPathWidget(QWidget):
findClicked = pyqtSignal()
added = pyqtSignal(str)

def __init__(self, default_filename: str = ''):
super().__init__()
self.__initVal()
self.__initUi(default_filename)

def __initVal(self):
self.__ext_of_files = ''
self.__directory = False

def __initUi(self, default_filename: str = ''):
self.__pathLineEdit = FindPathLineEdit()
if default_filename:
self.__pathLineEdit.setText(default_filename)

self.__pathFindBtn = QPushButton('Find')

self.__pathFindBtn.clicked.connect(self.__find)

self.__pathLineEdit.setMaximumHeight(self.__pathFindBtn.sizeHint().height())

lay = QHBoxLayout()
lay.addWidget(self.__pathLineEdit)
lay.addWidget(self.__pathFindBtn)
lay.setContentsMargins(0, 0, 0, 0)

self.setLayout(lay)

def setLabel(self, text):
self.layout().insertWidget(0, QLabel(text))

def setExtOfFiles(self, ext_of_files):
self.__ext_of_files = ext_of_files

def getLineEdit(self):
return self.__pathLineEdit

def getButton(self):
return self.__pathFindBtn

def getFileName(self):
return self.__pathLineEdit.text()

def setCustomFind(self, f: bool):
if f:
self.__pathFindBtn.clicked.disconnect(self.__find)
self.__pathFindBtn.clicked.connect(self.__customFind)

def __customFind(self):
self.findClicked.emit()

def __find(self):
if self.isForDirectory():
filename = QFileDialog.getExistingDirectory(self, 'Open Directory', '', QFileDialog.ShowDirsOnly)
if filename:
pass
else:
return
else:
str_exp_files_to_open = self.__ext_of_files if self.__ext_of_files else 'All Files (*.*)'
filename = QFileDialog.getOpenFileName(self, 'Find', '', str_exp_files_to_open)
if filename[0]:
filename = filename[0]
else:
return
self.__pathLineEdit.setText(filename)
self.added.emit(filename)

def setAsDirectory(self, f: bool):
self.__directory = f

def isForDirectory(self) -> bool:
return self.__directory
Loading

0 comments on commit b8f6cdc

Please sign in to comment.