Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Christina Bukas committed Mar 21, 2024
1 parent 9b80454 commit 052c481
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 71 deletions.
187 changes: 119 additions & 68 deletions src/client/dcp_client/gui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,40 @@
import os
from typing import TYPE_CHECKING

from PyQt5.QtWidgets import QPushButton, QVBoxLayout, QFileSystemModel, QHBoxLayout, QLabel, QTreeView, QProgressBar, QShortcut, QApplication, QStyledItemDelegate
from PyQt5.QtCore import Qt, QModelIndex, QThread, pyqtSignal, QRect, QSize, QVariant, QDir
from PyQt5.QtGui import QKeySequence, QIcon, QPixmap, QPainter, QImage, QBrush, QPen, QFont
from PySide2.QtCore import QModelIndex
from PyQt5.QtWidgets import (
QPushButton,
QVBoxLayout,
QFileSystemModel,
QHBoxLayout,
QLabel,
QTreeView,
QProgressBar,
QShortcut,
QApplication,
QStyledItemDelegate,
)
from PyQt5.QtCore import (
Qt,
QModelIndex,
QThread,
pyqtSignal,
QRect,
QSize,
QVariant,
QDir,
)
from PyQt5.QtGui import (
QKeySequence,
QIcon,
QPixmap,
QPainter,
QImage,
QBrush,
QPen,
QFont,
)

# from PySide2.QtCore import QModelIndex


from dcp_client.utils import settings
Expand Down Expand Up @@ -71,14 +101,15 @@ def run(self) -> None:

self.task_finished.emit((message_text, message_title))


class MyQFileSystemModel(QFileSystemModel):
def __init__(self, app):
"""
Initializes a custom QFileSystemModel
"""
super().__init__()
self.app = app

def setFilter(self, filters):
"""
Sets filters for the model.
Expand Down Expand Up @@ -109,7 +140,7 @@ def headerData(self, section, orientation, role):
:rtype: QVariant
"""
if section == 0 and role == Qt.DisplayRole:
return ""
return ""
else:
return super().headerData(section, orientation, role)

Expand All @@ -123,19 +154,18 @@ def data(self, index, role=Qt.DisplayRole):
"""
if not index.isValid():
return QVariant()
if '_seg' in self.filePath(index):

if "_seg" in self.filePath(index):
return QVariant()

if role == Qt.DisplayRole:
filepath_img = self.filePath(index)


if role == Qt.DecorationRole:
filepath_img = self.filePath(index)
if filepath_img.endswith('.tiff') or filepath_img.endswith('.png'):
if '_seg' not in filepath_img:

if filepath_img.endswith(".tiff") or filepath_img.endswith(".png"):
if "_seg" not in filepath_img:
painter = QPainter()

img_x, img_y = 64, 64
Expand All @@ -146,47 +176,67 @@ def data(self, index, role=Qt.DisplayRole):
if os.path.exists(filepath_mask):

mask = tiff.imread(filepath_mask)[0]
num_objects = len(self.app.fs_image_storage.search_segs(
os.path.dirname(filepath_img),
filepath_img
))

mask = cv2.resize(mask, (int(round(1.5*img_x)), int(round(1.5*img_y))), interpolation=cv2.INTER_NEAREST)

num_objects = len(
self.app.fs_image_storage.search_segs(
os.path.dirname(filepath_img), filepath_img
)
)

mask = cv2.resize(
mask,
(int(round(1.5 * img_x)), int(round(1.5 * img_y))),
interpolation=cv2.INTER_NEAREST,
)

mask = label2rgb(mask)
mask = (255 * np.transpose(mask,(1,0,2)).copy()).astype(np.uint8)

mask = QImage(mask, mask.shape[1], mask.shape[0], QImage.Format_RGB888)\
.scaled(123, 123, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)

img = img.scaled(82, 82, Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
mask = (255 * np.transpose(mask, (1, 0, 2)).copy()).astype(
np.uint8
)

mask = QImage(
mask, mask.shape[1], mask.shape[0], QImage.Format_RGB888
).scaled(
123, 123, Qt.IgnoreAspectRatio, Qt.SmoothTransformation
)

img = img.scaled(
82, 82, Qt.IgnoreAspectRatio, Qt.SmoothTransformation
)
img_x, img_y = 82, 82
painter.begin(mask)

rect_image = QRect(0, img_x//2, img_x, img_y)

rect_corner_left = QRect(0, 0, img_x//2, img_y//2)
rect_corner_bottom = QRect(img_x, img_y, img_x//2, img_y//2)
rect_image = QRect(0, img_x // 2, img_x, img_y)

rect_corner_left = QRect(0, 0, img_x // 2, img_y // 2)
rect_corner_bottom = QRect(img_x, img_y, img_x // 2, img_y // 2)

painter.fillRect(rect_corner_left, QBrush(Qt.white, Qt.SolidPattern))
painter.fillRect(rect_corner_bottom, QBrush(Qt.white, Qt.SolidPattern))
painter.fillRect(
rect_corner_left, QBrush(Qt.white, Qt.SolidPattern)
)
painter.fillRect(
rect_corner_bottom, QBrush(Qt.white, Qt.SolidPattern)
)

painter.drawImage(rect_image, img)

pen = QPen(Qt.black)
painter.setPen(pen)

font = QFont()
font.setFamily('Arial')
font.setFamily("Arial")

font.setPointSize(6)
painter.setFont(font)

painter.drawText(int(round((5/4)*img_x))-19, int(round((5/4)*img_x)), f"{str(num_objects)} masks")
painter.drawText(
int(round((5 / 4) * img_x)) - 19,
int(round((5 / 4) * img_x)),
f"{str(num_objects)} masks",
)

painter.end()

pixmap = QPixmap.fromImage(mask)
pixmap = QPixmap.fromImage(mask)

return pixmap

Expand All @@ -199,7 +249,6 @@ def data(self, index, role=Qt.DisplayRole):
return super().data(index, role)



class ImageDelegate(QStyledItemDelegate):
"""
Custom delegate for displaying images in Qt views.
Expand All @@ -213,7 +262,7 @@ def paint(self, painter, option, index):
:param option: The style options for the item. (QStyleOptionViewItem)
:param index: The model index of the item. (QModelIndex)
"""
if '_seg' in index.data():
if "_seg" in index.data():
return
if index.isValid() and index.data(Qt.DecorationRole):
pixmap = index.data(Qt.DecorationRole)
Expand Down Expand Up @@ -262,15 +311,14 @@ def main_window(self) -> None:
self.uncurated_layout = QVBoxLayout()
self.inprogress_layout = QVBoxLayout()
self.curated_layout = QVBoxLayout()

self.eval_dir_layout = QVBoxLayout()
self.eval_dir_layout.setContentsMargins(0,0,0,0)

self.eval_dir_layout = QVBoxLayout()
self.eval_dir_layout.setContentsMargins(0, 0, 0, 0)

self.label_eval = QLabel(self)
self.label_eval.setText("Uncurated Dataset")
self.label_eval.setMinimumHeight(50)
self.label_eval.setMinimumWidth(200)
self.label_eval.setMinimumHeight(50)
self.label_eval.setMinimumWidth(200)
self.label_eval.setAlignment(Qt.AlignCenter)
self.label_eval.setStyleSheet(
"""
Expand All @@ -283,25 +331,24 @@ def main_window(self) -> None:
)

self.eval_dir_layout.addWidget(self.label_eval)

model_eval = MyQFileSystemModel(app=self.app)
model_eval.setIconProvider(IconProvider())
model_eval.sort(0, Qt.AscendingOrder)



self.list_view_eval = QTreeView(self)

self.list_view_eval.setItemDelegate(ImageDelegate())

self.list_view_eval.setToolTip("Select an image, click it, then press Enter")
self.list_view_eval.setIconSize(QSize(300,300))
self.list_view_eval.setIconSize(QSize(300, 300))
self.list_view_eval.setStyleSheet("background-color: #ffffff")
self.list_view_eval.setModel(model_eval)
model_eval.setRootPath('/')
model_eval.setRootPath("/")
delegate = CustomItemDelegate()
self.list_view_eval.setItemDelegate(delegate)

for i in range(1,4):
for i in range(1, 4):
self.list_view_eval.hideColumn(i)
# self.list_view_eval.setFixedSize(600, 600)
self.list_view_eval.setRootIndex(
Expand All @@ -315,7 +362,7 @@ def main_window(self) -> None:
# add buttons
self.inference_button = QPushButton("Generate Labels", self)
self.inference_button.setStyleSheet(
"""QPushButton
"""QPushButton
{
background-color: #3d81d1;
font-size: 12px;
Expand All @@ -326,30 +373,31 @@ def main_window(self) -> None:
"QPushButton:hover { background-color: #7bc432; }"
"QPushButton:pressed { background-color: #7bc432; }"
)
self.inference_button.clicked.connect(self.on_run_inference_button_clicked) # add selected image
self.inference_button.clicked.connect(
self.on_run_inference_button_clicked
) # add selected image
self.uncurated_layout.addWidget(self.inference_button, alignment=Qt.AlignCenter)

dir_layout.addLayout(self.uncurated_layout)

# In progress layout
self.inprogr_dir_layout = QVBoxLayout()
self.inprogr_dir_layout.setContentsMargins(0,0,0,0)
self.inprogr_dir_layout = QVBoxLayout()
self.inprogr_dir_layout.setContentsMargins(0, 0, 0, 0)

self.label_inprogr = QLabel(self)
self.label_inprogr.setMinimumHeight(50)
self.label_inprogr.setMinimumWidth(200)
self.label_inprogr.setMinimumHeight(50)
self.label_inprogr.setMinimumWidth(200)
self.label_inprogr.setAlignment(Qt.AlignCenter)
self.label_inprogr.setStyleSheet(
"font-size: 20px; font-weight: bold; background-color: #015998; color: #ffffff; border-radius: 5px; padding: 8px 16px;"
)


self.label_inprogr.setText("Curation in progress")
self.inprogr_dir_layout.addWidget(self.label_inprogr)
# add in progress dir list
model_inprogr = MyQFileSystemModel(app=self.app)
#self.list_view = QListView(self)

# self.list_view = QListView(self)

self.list_view_inprogr = QTreeView(self)
self.list_view_inprogr.setToolTip("Select an image, click it, then press Enter")
Expand All @@ -358,11 +406,11 @@ def main_window(self) -> None:
model_inprogr.setIconProvider(IconProvider())
self.list_view_inprogr.setModel(model_inprogr)

model_inprogr.setRootPath('/')
model_inprogr.setRootPath("/")
delegate = CustomItemDelegate()
self.list_view_inprogr.setItemDelegate(delegate)

for i in range(1,4):
for i in range(1, 4):
self.list_view_inprogr.hideColumn(i)
# self.list_view_inprogr.setFixedSize(600, 600)
self.list_view_inprogr.setRootIndex(
Expand All @@ -374,12 +422,13 @@ def main_window(self) -> None:

self.launch_nap_button = QPushButton()
self.launch_nap_button.setStyleSheet(
"QPushButton { background-color: transparent; border: none; border-radius: 5px; padding: 8px 16px; }"
"QPushButton { background-color: transparent; border: none; border-radius: 5px; padding: 8px 16px; }"
)


self.launch_nap_button.setEnabled(False)
self.inprogress_layout.addWidget(self.launch_nap_button, alignment=Qt.AlignCenter)
self.inprogress_layout.addWidget(
self.launch_nap_button, alignment=Qt.AlignCenter
)

# Create a shortcut for the Enter key to click the button
enter_shortcut = QShortcut(QKeySequence(Qt.Key_Return), self)
Expand All @@ -392,8 +441,8 @@ def main_window(self) -> None:
self.train_dir_layout.setContentsMargins(0, 0, 0, 0)
self.label_train = QLabel(self)
self.label_train.setText("Curated dataset")
self.label_train.setMinimumHeight(50)
self.label_train.setMinimumWidth(200)
self.label_train.setMinimumHeight(50)
self.label_train.setMinimumWidth(200)
self.label_train.setAlignment(Qt.AlignCenter)
self.label_train.setStyleSheet(
"font-size: 20px; font-weight: bold; background-color: #015998; color: #ffffff; border-radius: 5px; padding: 8px 16px;"
Expand All @@ -402,7 +451,7 @@ def main_window(self) -> None:
# add train dir list
model_train = MyQFileSystemModel(app=self.app)
# model_train.setNameFilters(["*_seg.tiff"])
#self.list_view = QListView(self)
# self.list_view = QListView(self)

self.list_view_train = QTreeView(self)
self.list_view_train.setToolTip("Select an image, click it, then press Enter")
Expand All @@ -411,11 +460,11 @@ def main_window(self) -> None:
model_train.setIconProvider(IconProvider())
self.list_view_train.setModel(model_train)

model_train.setRootPath('/')
model_train.setRootPath("/")
delegate = CustomItemDelegate()
self.list_view_train.setItemDelegate(delegate)

for i in range(1,4):
for i in range(1, 4):
self.list_view_train.hideColumn(i)
# self.list_view_train.setFixedSize(600, 600)
self.list_view_train.setRootIndex(
Expand All @@ -438,7 +487,9 @@ def main_window(self) -> None:
"QPushButton:hover { background-color: #7bc432; }"
"QPushButton:pressed { background-color: #7bc432; }"
)
self.train_button.clicked.connect(self.on_train_button_clicked) # add selected image
self.train_button.clicked.connect(
self.on_train_button_clicked
) # add selected image

self.curated_layout.addWidget(self.train_button, alignment=Qt.AlignCenter)
dir_layout.addLayout(self.curated_layout)
Expand All @@ -451,7 +502,7 @@ def main_window(self) -> None:
self.progress_bar = QProgressBar(self)
self.progress_bar.setMinimumWidth(1000)
self.progress_bar.setAlignment(Qt.AlignCenter)
self.progress_bar.setRange(0,1)
self.progress_bar.setRange(0, 1)

progress_layout.addWidget(self.progress_bar)
main_layout.addLayout(progress_layout)
Expand Down
Loading

0 comments on commit 052c481

Please sign in to comment.