Skip to content

Commit

Permalink
changes to get drag and drop working for the tables in the routine ed…
Browse files Browse the repository at this point in the history
…itor
  • Loading branch information
YektaY committed Dec 9, 2024
1 parent 66dc90d commit 244cca0
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/badger/gui/default/components/env_cbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,14 @@ def init_ui(self):
btn_env_play.setFixedSize(128, 24)
if not strtobool(config_singleton.read_value("BADGER_ENABLE_ADVANCED")):
btn_env_play.hide()
self.btn_pv = btn_pv = QPushButton("PV Search")
btn_pv.setFixedSize(128, 24)
self.btn_docs = btn_docs = QPushButton("Open Docs")
btn_docs.setFixedSize(128, 24)
hbox_name.addWidget(lbl)
hbox_name.addWidget(cb, 1)
hbox_name.addWidget(btn_env_play)
hbox_name.addWidget(btn_pv)
hbox_name.addWidget(btn_docs)
vbox.addWidget(name)

Expand Down Expand Up @@ -203,6 +206,7 @@ def init_ui(self):
hbox_action_var.addWidget(check_only_var)

self.var_table = VariableTable()
self.var_table.setAcceptDrops(True)
self.var_table.lock_bounds()
vbox_var_edit.addWidget(self.var_table)

Expand Down
51 changes: 50 additions & 1 deletion src/badger/gui/default/components/obj_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
QComboBox,
QStyledItemDelegate,
)

from PyQt5.QtCore import Qt

class ObjectiveTable(QTableWidget):
def __init__(self, *args, **kwargs):
Expand All @@ -19,6 +19,11 @@ def __init__(self, *args, **kwargs):
# self.setShowGrid(False)
# self.setDragDropMode(self.InternalMove)
# self.setDragDropOverwriteMode(False)

self.setAcceptDrops(True)
self.setDragEnabled(True)
self.setDragDropMode(QAbstractItemView.DragDrop)
self.setDefaultDropAction(Qt.MoveAction)

self.setRowCount(0)
self.setColumnCount(3)
Expand All @@ -41,6 +46,50 @@ def __init__(self, *args, **kwargs):

self.config_logic()

def dragEnterEvent(self, event):
if event.mimeData().hasText():
event.acceptProposedAction()
else:
event.ignore()

def dragMoveEvent(self, event):
if event.mimeData().hasText():
event.acceptProposedAction()
else:
event.ignore()

def dropEvent(self, event):
if event.mimeData().hasText():
text = event.mimeData().text()
strings = text.strip().split('\n') # Adjust the delimiter as needed

position = event.pos()
drop_row = self.rowAt(position.y())
if drop_row == -1:
drop_row = self.rowCount()

for i, string in enumerate(strings):
string = string.strip()
if not string:
continue # Skip empty strings

row = drop_row + i
# Insert a new row if necessary
if row >= self.rowCount():
self.insertRow(row)

# Create a QTableWidgetItem for the variable name
item = QTableWidgetItem(string)
# Set the item at the specified row and column (column 1 for variable names)
self.setItem(row, 1, item)

# Call add_additional_variable with the item
self.all_objectives.append(item)

event.acceptProposedAction()
else:
event.ignore()

def config_logic(self):
self.horizontalHeader().sectionClicked.connect(self.header_clicked)

Expand Down
6 changes: 6 additions & 0 deletions src/badger/gui/default/components/routine_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from badger.gui.default.windows.message_dialog import BadgerScrollableMessageBox
from badger.gui.default.windows.expandable_message_box import ExpandableMessageBox
from badger.gui.default.utils import filter_generator_config
from badger.gui.default.components.archive_search import ArchiveSearchWidget
from badger.db import save_routine, update_routine, get_runs_by_routine
from badger.environment import instantiate_env
from badger.errors import BadgerRoutineError
Expand Down Expand Up @@ -191,6 +192,7 @@ def config_logic(self):
self.generator_box.btn_edit_script.clicked.connect(self.edit_script)
self.env_box.cb.currentIndexChanged.connect(self.select_env)
self.env_box.btn_env_play.clicked.connect(self.open_playground)
self.env_box.btn_pv.clicked.connect(self.open_archive_search)
self.env_box.btn_docs.clicked.connect(self.open_environment_docs)
self.env_box.btn_add_var.clicked.connect(self.add_var)
self.env_box.btn_lim_vrange.clicked.connect(self.limit_variable_ranges)
Expand Down Expand Up @@ -669,6 +671,10 @@ def open_generator_docs(self):
def open_environment_docs(self):
self.window_env_docs.show()

def open_archive_search(self):
self.archive_search = ArchiveSearchWidget()
self.archive_search.show()

def add_var(self):
# TODO: Use a cached env
env_params = load_config(self.env_box.edit.toPlainText())
Expand Down
73 changes: 60 additions & 13 deletions src/badger/gui/default/components/var_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
QHeaderView,
QCheckBox,
QMessageBox,
QAbstractItemView
)
from PyQt5.QtCore import pyqtSignal, Qt
from PyQt5.QtGui import QColor
Expand All @@ -19,27 +20,19 @@ class VariableTable(QTableWidget):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# Reorder the rows by dragging around
# self.setSelectionBehavior(self.SelectRows)
# self.setSelectionMode(self.SingleSelection)
# self.setShowGrid(False)
# self.setDragDropMode(self.InternalMove)
# self.setDragDropOverwriteMode(False)

self.setAcceptDrops(True)
self.setDragEnabled(True)
self.setDragDropMode(QAbstractItemView.DragDrop)
self.setDefaultDropAction(Qt.MoveAction)
self.setRowCount(0)
self.setColumnCount(4)
self.setAlternatingRowColors(True)
self.setStyleSheet("alternate-background-color: #262E38;")
# self.setEditTriggers(QAbstractItemView.NoEditTriggers)

self.verticalHeader().setVisible(False)
header = self.horizontalHeader()
# header.setSectionResizeMode(0, QHeaderView.ResizeToContents)
header.setSectionResizeMode(0, QHeaderView.Fixed)
header.setSectionResizeMode(1, QHeaderView.Stretch)
# header.setSectionResizeMode(2, QHeaderView.ResizeToContents)
# header.setSectionResizeMode(3, QHeaderView.ResizeToContents)
self.setColumnWidth(0, 20)
self.setColumnWidth(2, 96)
self.setColumnWidth(3, 96)
Expand All @@ -57,6 +50,60 @@ def __init__(self, *args, **kwargs):
self.previous_values = {} # to track changes in table

self.config_logic()

def dragEnterEvent(self, event):
if event.mimeData().hasText():
event.acceptProposedAction()
else:
event.ignore()

def dragMoveEvent(self, event):
if event.mimeData().hasText():
event.acceptProposedAction()
else:
event.ignore()

def dropEvent(self, event):
if event.mimeData().hasText():
text = event.mimeData().text()
strings = text.strip().split('\n') # Adjust the delimiter as needed

position = event.pos()
drop_row = self.rowAt(position.y())
if drop_row == -1:
drop_row = self.rowCount()

for i, string in enumerate(strings):
string = string.strip()
if not string:
continue # Skip empty strings

row = drop_row + i
# Insert a new row if necessary
if row >= self.rowCount():
self.insertRow(row)

# Create a QTableWidgetItem for the variable name
item = QTableWidgetItem(string)
# Set the item at the specified row and column (column 1 for variable names)
self.setItem(row, 1, item)

# Call add_additional_variable with the item
self.add_additional_variable(item)

event.acceptProposedAction()
else:
event.ignore()

def flags(self, index):
if not index.isValid():
return Qt.ItemIsEnabled
flags = Qt.ItemIsSelectable | Qt.ItemIsEnabled
if index.column() == 1:
flags |= (
Qt.ItemIsEditable | Qt.ItemIsDropEnabled
) # First column is editable and droppable
return flags

def config_logic(self):
self.horizontalHeader().sectionClicked.connect(self.header_clicked)
Expand Down Expand Up @@ -310,7 +357,7 @@ def add_additional_variable(self, item):
else:
# TODO: handle this case? Right now I don't think it should happen
raise "Environment cannot be found for new variable bounds!"

# Sanitize vrange
# TODO: raise a heads-up regarding the invalid bounds
if vrange[1] <= vrange[0]:
Expand Down

0 comments on commit 244cca0

Please sign in to comment.