11from __future__ import annotations
22
3+ import warnings
4+ from pathlib import Path
35from typing import TYPE_CHECKING , cast
46
57from pymmcore_widgets import MDAWidget
68from qtpy .QtCore import Qt
7- from qtpy .QtWidgets import QCheckBox , QGridLayout , QSizePolicy , QVBoxLayout , QWidget
9+ from qtpy .QtWidgets import (
10+ QCheckBox ,
11+ QGridLayout ,
12+ QMessageBox ,
13+ QSizePolicy ,
14+ QVBoxLayout ,
15+ QWidget ,
16+ )
817from useq import MDASequence
918
1019from napari_micromanager ._mda_meta import SEQUENCE_META_KEY , SequenceMeta
1120
1221from ._save_widget import SaveWidget
1322
1423if TYPE_CHECKING :
15- from pathlib import Path
16-
1724 from pymmcore_plus import CMMCorePlus
1825
1926
@@ -25,36 +32,35 @@ def __init__(
2532 ) -> None :
2633 super ().__init__ (include_run_button = True , parent = parent , mmcore = mmcore )
2734
35+ # add save widget
36+ v_layout = cast (QVBoxLayout , self ._central_widget .layout ())
2837 self ._save_groupbox = SaveWidget ()
2938 self ._save_groupbox .setSizePolicy (
3039 QSizePolicy .Policy .Minimum , QSizePolicy .Policy .Fixed
3140 )
3241 self ._save_groupbox .setChecked (False )
33-
34- v_layout = cast (QVBoxLayout , self .layout ())
42+ self ._save_groupbox .toggled .connect (self ._on_save_toggled )
43+ self ._save_groupbox ._directory .textChanged .connect (self ._on_save_toggled )
44+ self ._save_groupbox ._fname .textChanged .connect (self ._on_save_toggled )
3545 v_layout .insertWidget (0 , self ._save_groupbox )
3646
37- self .channel_groupbox .setMinimumHeight (230 )
47+ # add split channel checkbox
48+ self .channel_widget .setMinimumHeight (230 )
3849 self .checkBox_split_channels = QCheckBox (text = "Split Channels" )
3950 self .checkBox_split_channels .toggled .connect (self ._toggle_split_channel )
40- g_layout = cast (QGridLayout , self .channel_groupbox .layout ())
51+ g_layout = cast (QGridLayout , self .channel_widget .layout ())
4152 g_layout .addWidget (self .checkBox_split_channels , 1 , 0 )
42-
43- # TODO: stage_pos_groupbox should have a valueChanged signal
44- # and that should be connected to _toggle_checkbox_save_pos
45- self ._save_groupbox .toggled .connect (self ._toggle_checkbox_save_pos )
46- self .position_groupbox .valueChanged .connect (self ._toggle_checkbox_save_pos )
47- self .channel_groupbox .valueChanged .connect (self ._toggle_split_channel )
53+ self .channel_widget .valueChanged .connect (self ._toggle_split_channel )
4854
4955 def _toggle_split_channel (self ) -> None :
50- if not self .channel_groupbox .value ():
51- self .checkBox_split_channels .setChecked (False )
52-
53- def _toggle_checkbox_save_pos (self ) -> None :
5456 if (
55- self .position_groupbox . isChecked ()
56- and len ( self .position_groupbox . value ()) > 0
57+ not self .channel_widget . value ()
58+ or self .channel_widget . _table . rowCount () == 1
5759 ):
60+ self .checkBox_split_channels .setChecked (False )
61+
62+ def _on_save_toggled (self ) -> None :
63+ if self .position_widget .value ():
5864 self ._save_groupbox ._split_pos_checkbox .setEnabled (True )
5965
6066 else :
@@ -89,3 +95,33 @@ def set_state(self, state: dict | MDASequence | str | Path) -> None:
8995
9096 self .checkBox_split_channels .setChecked (meta .split_channels )
9197 self ._save_groupbox .set_state (meta )
98+
99+ def _on_run_clicked (self ) -> None :
100+ if (
101+ self ._save_groupbox .isChecked ()
102+ and not self ._save_groupbox ._directory .text ()
103+ ):
104+ warnings .warn ("Select a directory to save the data." , stacklevel = 2 )
105+ return
106+
107+ if not Path (self ._save_groupbox ._directory .text ()).exists ():
108+ if self ._create_new_folder ():
109+ Path (self ._save_groupbox ._directory .text ()).mkdir (parents = True )
110+ else :
111+ return
112+
113+ super ()._on_run_clicked ()
114+
115+ def _create_new_folder (self ) -> bool :
116+ """Create a QMessageBox to ask to create directory if it doesn't exist."""
117+ msgBox = QMessageBox ()
118+ msgBox .setWindowTitle ("Create Directory" )
119+ msgBox .setIcon (QMessageBox .Icon .Question )
120+ msgBox .setText (
121+ f"Directory { self ._save_groupbox ._directory .text ()} "
122+ "does not exist. Create it?"
123+ )
124+ msgBox .setStandardButtons (
125+ QMessageBox .StandardButton .Ok | QMessageBox .StandardButton .Cancel
126+ )
127+ return bool (msgBox .exec () == QMessageBox .StandardButton .Ok )
0 commit comments