Ejemplo n.º 1
0
 public Config_Description(Config_Options _label_, string _tooltip_)
 {
     LABEL   = _label_;
     TOOLTIP = _tooltip_;
 }
Ejemplo n.º 2
0
        /* ConfigurationOptionPrompt: ask for configuration options, refer to description of enum `Config_Options` for details
         * This is called before `MainPrompt` to decide where to load the configuration file from first and
         * whether to start `MainPrompt` at all.
         * + LAYOUT:
         *      - Initialize the dialog and title
         *      - Create RadioButton object for each option
         *      - Create components for choosing the directory of configuration file
         *      - Create buttons to either continue or exit
         *      - Return configuration option, file location and warning
         * + INPUT: None
         * + OUTPUT:
         *      - config_option:            configuration option, refer to `Config_Options` enum
         *      - config_file_path:         path to look for or configuration file
         *      - warning:                  configuration file loading warnings, refer to `LoadConfig_Warning` enum
         *
         */
        private void ConfigurationOptionPrompt(out Config_Options config_option, out string config_file_path, out LoadConfig_Warning warning)
        {
            // (1) Initialize the dialog
            Font font_style = new Font(fontname, 12F, FontStyle.Regular, GraphicsUnit.Point);

            ConfigPrompt = new Form()
            {
                Size            = new Size(400, 400),
                FormBorderStyle = FormBorderStyle.Sizable,
                Text            = "Configuration loading option",
                StartPosition   = FormStartPosition.CenterParent,
                TopMost         = true
            };
            // Dialog title
            ConfigPrompt.Controls.Add(new Label {
                Location  = new Point(20, 20),
                Size      = new Size(400, 50),
                Text      = "Would you wish to?",
                TextAlign = ContentAlignment.MiddleLeft,
                Font      = font_style
            });

            // Radiobutton options and tooltip description (for mouse hovering)
            Dictionary <string, Config_Description> radio_options = new Dictionary <string, Config_Description>
            {
                { "Create a new configuration file", new Config_Description()
                  {
                      LABEL   = Config_Options.CREATE_NEW,
                      TOOLTIP = "The Main Prompt will appear, with values from the preset default configuration file.\n" +
                                "You can specify the location to save a new configuration file in the Main Prompt"
                  } },
                { "Load an existing configuration file and plot", new Config_Description()
                  {
                      LABEL   = Config_Options.LOAD_AND_PLOT,
                      TOOLTIP = "The Main Prompt will NOT appear.\n" +
                                "But you would need to specify the location of the configuration file."
                  } },
                { "Load and change a configuration file", new Config_Description()
                  {
                      LABEL   = Config_Options.LOAD_AND_MODIFY,
                      TOOLTIP = "The Main Prompt will appear.\n" +
                                "You would need to specific the location of the configuration file.\n" +
                                "You can change the configuration parameters.\n" +
                                "You can then either save your changes to the existing file, or in a new file. "
                  } }
            };

            RadioButton[] Radio_List = new RadioButton[radio_options.Count];

            // Position and size parameters
            int cur_radio = 0;
            int left_radio = 20, width_radio = 350, top_radio = 65, height_radio = 30, spacing = 10;

            // (2) Create RadioButton object for each option
            foreach (var item in radio_options)
            {
                string option      = item.Key;
                string description = item.Value.TOOLTIP;
                Radio_List[cur_radio] = new RadioButton
                {
                    Location = new Point(left_radio, top_radio),
                    Size     = new Size(width_radio, height_radio),
                    Font     = font_style,
                    Text     = option
                };
                ConfigPrompt.Controls.Add(Radio_List[cur_radio]);
                top_radio += height_radio + spacing; // for the next one

                (new ToolTip()).SetToolTip(Radio_List[cur_radio], description);
            }
            Radio_List[0].Checked = true;

            // (3) Create components for choosing the directory of configuration file
            string current_directory = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
            // FolderDialogButton
            Button FolderDialogButton = new Button()
            {
                Text      = "Choose configuration file",
                Location  = new Point(left_radio, top_radio + 10),
                Size      = new Size(width_radio, 30),
                Font      = font_style,
                BackColor = Color.WhiteSmoke,
                FlatStyle = FlatStyle.Flat
            };
            // TextBox to report the name
            TextBox ConfigFileText = new TextBox()
            {
                Text      = current_directory + "\\customized_config_file.txt",
                Location  = new Point(left_radio, FolderDialogButton.Bottom + 10),
                Size      = new Size(width_radio, 30),
                Font      = font_style,
                BackColor = Color.WhiteSmoke
            };

            // Report which file it is in ConfigFileText after action on FolderDialogButton
            FolderDialogButton.FlatAppearance.MouseOverBackColor = Color.Silver;
            FolderDialogButton.Click += (sender, e) => {
                OpenFileDialog folderBrowser = new OpenFileDialog()
                {
                    ValidateNames    = false,
                    CheckFileExists  = false,
                    CheckPathExists  = true,
                    InitialDirectory = current_directory
                };
                folderBrowser.FileName = "Choose the configuration file here";
                if (folderBrowser.ShowDialog() == DialogResult.OK)
                {
                    ConfigFileText.Text = Path.GetFullPath(folderBrowser.FileName);
                }
            };
            ConfigPrompt.Controls.Add(FolderDialogButton);
            ConfigPrompt.Controls.Add(ConfigFileText);

            // (4) Create buttons to either continue or exit
            // ConfirmationButton to continue with either MainPrompt (then MainForm) or
            // MainForm directly
            Button ConfirmationButton = new Button()
            {
                Text         = "Continue",
                Location     = new Point(20, ConfigFileText.Bottom + 50),
                Size         = new Size(100, 30),
                DialogResult = DialogResult.OK,
                Font         = font_style,
                BackColor    = Color.WhiteSmoke,
                FlatStyle    = FlatStyle.Flat
            };

            ConfirmationButton.FlatAppearance.BorderSize         = 1;
            ConfirmationButton.FlatAppearance.MouseOverBackColor = Color.Silver;
            ConfirmationButton.Click += (sender, e) => {
                ConfigPrompt.Close();
            };
            ConfigPrompt.Controls.Add(ConfirmationButton);
            ConfigPrompt.AcceptButton = ConfirmationButton;

            // ExitButton to exit the program before starting either MainPrompt nor MainForm
            Button ExitButton = new Button()
            {
                Text      = "Exit",
                Location  = new Point(FolderDialogButton.Right - ConfirmationButton.Width, ConfirmationButton.Top),
                Size      = new Size(100, 30),
                BackColor = Color.WhiteSmoke,
                Font      = font_style,
                FlatStyle = FlatStyle.Flat
            };

            ExitButton.FlatAppearance.BorderSize         = 1;
            ExitButton.FlatAppearance.MouseOverBackColor = Color.Silver;
            ExitButton.Click += (sender, e) =>
            {
                ConfigPrompt.Close();
                Environment.Exit(1);
            };
            ConfigPrompt.Controls.Add(ExitButton);
            ConfigPrompt.CancelButton = ExitButton;

            config_option    = Config_Options.CREATE_NEW;
            config_file_path = ConfigFileText.Text;
            warning          = LoadConfig_Warning.NO_WARNING;
            // (5) Return configuration option, file location and warning
            if (ConfigPrompt.ShowDialog() == DialogResult.OK)
            {
                // an efficient way to find checked RadioButton of a given group
                // https://stackoverflow.com/questions/1797907/which-radio-button-in-the-group-is-checked by SLaks
                var checkedButton = ConfigPrompt.Controls.OfType <RadioButton>().FirstOrDefault(r => r.Checked);

                config_option    = radio_options[checkedButton.Text].LABEL;
                config_file_path = ConfigFileText.Text;
                if ((config_option == Config_Options.CREATE_NEW || config_option == Config_Options.LOAD_AND_MODIFY) && File.Exists(config_file_path))
                {
                    warning = LoadConfig_Warning.POSSIBLE_OVERWRITE_FILE;
                }
                if (config_option == Config_Options.LOAD_AND_PLOT && !File.Exists(config_file_path))
                {
                    warning = LoadConfig_Warning.NO_EXISTING_FILE_TO_PLOT;
                }
                if (config_option == Config_Options.LOAD_AND_MODIFY && !File.Exists(config_file_path))
                {
                    warning = LoadConfig_Warning.NO_EXISTING_FILE_TO_MODIFY;
                }
            }
        }