Skip to content

Commit

Permalink
add the information screen
Browse files Browse the repository at this point in the history
  • Loading branch information
danghoangnhan committed Nov 28, 2023
1 parent a2c4161 commit ce7edbb
Show file tree
Hide file tree
Showing 5 changed files with 351 additions and 271 deletions.
67 changes: 0 additions & 67 deletions component/button.py
Original file line number Diff line number Diff line change
@@ -1,67 +0,0 @@
from component.text import TextElement
from psychopy.visual.rect import Rect
import psychopy.visual.line


class ButtonElement:
def __init__(self, win, text, pos, width, height, color, action=None):
self.button = Rect(win, width=width, height=height, fillColor=color, pos=pos)
self.text = TextElement(win=win,text=text,pos=pos)

self.action = action
self.states = {
'unpress': {
'button_color': color,
'text_color': 'white'
},
'press': {
'button_color': 'orange',
'text_color': 'black'
}
}
self.state = 'unpress' # Initial state is 'unpress'

def draw(self):
self.button.fillColor = self.states[self.state]['button_color']
self.text.set_color(self.states[self.state]['text_color'])
self.button.draw()
self.text.draw()

def containMouse(self,mouse):
return self.button.contains(mouse)


class ButtonList:
def __init__(self, win, textList, pos, width, height, color, action=None,rows=0,columns=0,horizontal_spacing=0,vertical_spacing=0):
self.pos = pos
positions = []
for row in range(rows):
for col in range(columns):
x = col * (width + horizontal_spacing) - (columns - 1) * (width + horizontal_spacing) / 2
y = (rows - 1 - row) * (height + vertical_spacing) - (rows - 1) * (height + vertical_spacing) / 2
positions.append((x, y))

self.button_elements = []
for i, text in enumerate(textList):
button_pos = (positions[i][0] + self.pos[0], positions[i][1] + self.pos[1]) # Use positions[i] here
self.button_elements.append(ButtonElement(win, text, pos=button_pos, width=width, height=height, color=color, action=action))

def draw(self):
for element in self.button_elements:
element.draw()

def containMouse(self, mouse):
return any(element.containMouse(mouse) for element in self.button_elements)


def action(self,mouse):
for element_id,element in enumerate(self.button_elements):
if element.containMouse(mouse):
return element.action(element_id)

def updateState(self,buttonId):
for element_id,element in enumerate(self.button_elements):
element.state = 'unpress'
if element_id==buttonId:
element.state = 'press'

59 changes: 27 additions & 32 deletions component/modal.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
from component.text import TextElement
from component.button import ButtonElement
import psychopy.visual.line
from psychopy.visual.rect import Rect
from config.constant import color_dict

class ConfirmationModal:
def __init__(self, win, text,confirm_action=None,cancel_action=None):
self.win = win
self.text = TextElement(win, text, pos=(0, 0), color=color_dict["white"],)
self.confirm_button = ButtonElement(win, "Confirm", pos=(-0.3, -0.5), width=0.3, height=0.2, color="green", action=confirm_action)
self.cancel_button = ButtonElement(win, "Cancel", pos=(0.3, -0.5), width=0.3, height=0.2, color="red", action=cancel_action)
self.background_rect = Rect(win, width=1.2, height=0.9, fillColor=(0, 0, 0, 1.0), pos=(0, -0.2))
self.buttons = [self.confirm_button, self.cancel_button]
self.visible = False

def draw(self):
if self.visible:
self.background_rect.draw()
self.text.draw()
for element in self.buttons:
element.draw()

def containMouse(self, mouse):
if(self.visible)==False:
return False
return self.confirm_button.containMouse(mouse) or self.cancel_button.containMouse(mouse)

def action(self, mouse):
for button_element in self.buttons:
if button_element.containMouse(mouse):
return button_element.action(mouse)
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QDialog, QHBoxLayout, QLabel

class ConfirmDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.initUI()

def initUI(self):
self.setWindowTitle("Confirm")

layout = QHBoxLayout()

confirm_btn = QPushButton("Confirm", self)
confirm_btn.clicked.connect(self.confirm)
layout.addWidget(confirm_btn)

cancel_btn = QPushButton("Cancel", self)
cancel_btn.clicked.connect(self.cancel)
layout.addWidget(cancel_btn)

self.setLayout(layout)

def confirm(self):
self.accept() # Closes the dialog and returns QDialog.Accepted

def cancel(self):
self.reject() # Closes the dialog and returns QDialog.Rejected
Loading

0 comments on commit ce7edbb

Please sign in to comment.