Example #1
0
        /// <summary>
        /// Displays the adjust layout dialog (for scaling primarily)
        /// </summary>
        /// <param name="bCreateNew">Flag indicating that a copy of the specified layout should be created.</param>
        /// <param name="zLayout">The base layout to adjust or duplicate</param>
        public static void ShowAdjustLayoutSettingsDialog(bool bCreateNew, ProjectLayout zLayout, Form zParentForm)
        {
            var zQuery = new QueryPanelDialog(bCreateNew ? "Duplicate Layout (Custom)" : "Resize Layout", 450, false);

            zQuery.SetIcon(CardMakerInstance.ApplicationIcon);
            const string LAYOUT_NAME = "layoutName";

            const string RESIZE_ADJUST_DIMENSIONS = "Dimensions";
            const string RESIZE_ADJUST_SCALE      = "Scale";
            const string RESIZE_ADJUST_DPI        = "DPI";
            const string RESIZE_ADJUST            = "resizeAdjust";

            const string ELEMENTS_ADJUST_NOTHING = "Nothing";
            const string ELEMENTS_ADJUST_SCALE   = "Scale";
            const string ELEMENTS_ADJUST_CENTER  = "Center";
            const string ELEMENTS_ADJUST         = "elementAdjust";
            const string DPI    = "dpi";
            const string SCALE  = "scale";
            const string WIDTH  = "width";
            const string HEIGHT = "height";

            var listMeasureUnits = new List <string>();

            listMeasureUnits.AddRange(Enum.GetNames(typeof(MeasurementUnit)));
            listMeasureUnits.Add(MeasurementUtil.PIXEL);

            if (bCreateNew)
            {
                zQuery.AddTextBox("Layout Name", zLayout.Name + " copy", false, LAYOUT_NAME);
            }
            // intentionall start the index as non-zero
            var comboResizeType = zQuery.AddPullDownBox("Resize Type", new string[] { RESIZE_ADJUST_DIMENSIONS, RESIZE_ADJUST_SCALE, RESIZE_ADJUST_DPI }, 0, RESIZE_ADJUST);

            zQuery.AddLabel("Note: The DPI is only adjusted if DPI Resize Type is used.", 18);
            zQuery.AddVerticalSpace(10);
            var comboUnitMeasure = zQuery.AddPullDownBox("Unit of Measure",
                                                         listMeasureUnits.ToArray(),
                                                         (int)CardMakerSettings.PrintPageMeasurementUnit,
                                                         IniSettings.PrintPageMeasurementUnit);

            // beware of the minimums as things like measurements definitely can be very small (<1)
            var numericWidth  = zQuery.AddNumericBox("Width", 1, 0.001m, int.MaxValue, 1, 3, WIDTH);
            var numericHeight = zQuery.AddNumericBox("Height", 1, 0.001m, int.MaxValue, 1, 3, HEIGHT);
            var numericDPI    = zQuery.AddNumericBox("Export DPI (Scales)", zLayout.dpi, 100, int.MaxValue, DPI);
            var numericScale  = zQuery.AddNumericBox("Scale", 1, 0.01m, int.MaxValue, 0.01m, 3, SCALE);

            EventHandler zComboUnitMeasureAction = (sender, args) =>
            {
                decimal dWidth, dHeight;
                MeasurementUtil.GetMeasurement(zLayout.width, zLayout.height, zLayout.dpi, comboUnitMeasure.Text, out dWidth, out dHeight);
                numericWidth.Value  = dWidth;
                numericHeight.Value = dHeight;
            };

            comboUnitMeasure.SelectedIndexChanged += zComboUnitMeasureAction;
            zComboUnitMeasureAction.Invoke(null, null);

            EventHandler zComboResizeTypeAction = (sender, args) =>
            {
                switch (comboResizeType.Text)
                {
                case RESIZE_ADJUST_DIMENSIONS:
                    comboUnitMeasure.Enabled = numericWidth.Enabled = numericHeight.Enabled = true;
                    numericDPI.Enabled       = numericScale.Enabled = false;
                    break;

                case RESIZE_ADJUST_SCALE:
                    comboUnitMeasure.SelectedIndex = listMeasureUnits.Count - 1;     // pixel index
                    numericScale.Enabled           = true;
                    comboUnitMeasure.Enabled       = numericDPI.Enabled = numericWidth.Enabled = numericHeight.Enabled = false;
                    break;

                case RESIZE_ADJUST_DPI:
                    comboUnitMeasure.SelectedIndex = listMeasureUnits.Count - 1;     // pixel index
                    numericDPI.Enabled             = true;
                    comboUnitMeasure.Enabled       = numericScale.Enabled = numericWidth.Enabled = numericHeight.Enabled = false;
                    break;
                }
            };

            comboResizeType.SelectedIndexChanged += zComboResizeTypeAction;
            zComboResizeTypeAction.Invoke(null, null);

            numericScale.ValueChanged += (sender, e) =>
            {
                var dScale = numericScale.Value / 1m;
                numericWidth.Value  = (int)Math.Max(numericWidth.Minimum, ((decimal)zLayout.width * dScale));
                numericHeight.Value = (int)Math.Max(numericHeight.Minimum, ((decimal)zLayout.height * dScale));
                // do not adjust the DPI in any mode but the DPI mode
                //numericDPI.Value = (int) Math.Max(numericDPI.Minimum, ((decimal) zLayout.dpi * dScale));
            };

            numericDPI.ValueChanged += (sender, e) =>
            {
                var dScale = (decimal)numericDPI.Value / (decimal)zLayout.dpi;
                numericWidth.Value  = (int)Math.Max(numericWidth.Minimum, ((decimal)zLayout.width * dScale));
                numericHeight.Value = (int)Math.Max(numericHeight.Minimum, ((decimal)zLayout.height * dScale));
                numericScale.Value  = Math.Max(numericScale.Minimum, (1m * dScale));
            };

            zQuery.AddVerticalSpace(10);
            zQuery.AddPullDownBox("Element Adjustment", new string[] { ELEMENTS_ADJUST_NOTHING, ELEMENTS_ADJUST_SCALE, ELEMENTS_ADJUST_CENTER }, 0, ELEMENTS_ADJUST);

            // remove the accept button so the enter key does not automatically accept/close the dialog
            zQuery.Form.AcceptButton = null;

            if (DialogResult.OK == zQuery.ShowDialog(zParentForm))
            {
                var listUserActions = new List <Action <bool> >();
                var zLayoutAdjusted = bCreateNew ? new ProjectLayout(zQuery.GetString(LAYOUT_NAME)) : zLayout;

                var nOriginalWidth  = zLayout.width;
                var nOriginalHeight = zLayout.height;
                var nOriginalDPI    = zLayout.dpi;

                if (bCreateNew)
                {
                    zLayoutAdjusted.DeepCopy(zLayout);
                }

                int nNewWidth, nNewHeight;
                var nNewDPI = nOriginalDPI;

                switch (comboResizeType.Text)
                {
                case RESIZE_ADJUST_DIMENSIONS:
                    MeasurementUtil.GetPixelMeasurement(zQuery.GetDecimal(WIDTH), zQuery.GetDecimal(HEIGHT), zLayout.dpi, comboUnitMeasure.Text, out nNewWidth, out nNewHeight);
                    break;

                case RESIZE_ADJUST_DPI:
                    nNewWidth  = (int)zQuery.GetDecimal(WIDTH);
                    nNewHeight = (int)zQuery.GetDecimal(HEIGHT);
                    // this is the only path where the DPI actually changes
                    nNewDPI = (int)zQuery.GetDecimal(DPI);
                    break;

                default:
                    nNewWidth  = (int)zQuery.GetDecimal(WIDTH);
                    nNewHeight = (int)zQuery.GetDecimal(HEIGHT);
                    break;
                }

                zLayoutAdjusted.width  = nNewWidth;
                zLayoutAdjusted.height = nNewHeight;
                zLayoutAdjusted.dpi    = nNewDPI;

                // create the user action for adjusting the layout settings
                listUserActions.Add((performAction) =>
                {
                    if (performAction)
                    {
                        zLayoutAdjusted.width  = nNewWidth;
                        zLayoutAdjusted.height = nNewHeight;
                        zLayoutAdjusted.dpi    = nNewDPI;
                    }
                    else
                    {
                        zLayoutAdjusted.width  = nOriginalWidth;
                        zLayoutAdjusted.height = nOriginalHeight;
                        zLayoutAdjusted.dpi    = nOriginalDPI;
                    }
                    LayoutManager.Instance.FireLayoutUpdatedEvent(true);
                });

                var bProcessedElementChange = false;
                // create the user action for adjusting the elements
                if (zLayout.Element != null)
                {
                    switch (zQuery.GetString(ELEMENTS_ADJUST))
                    {
                    case ELEMENTS_ADJUST_CENTER:
                        var nXAdjust = (int)(zLayoutAdjusted.width / 2) - (int)(nOriginalWidth / 2);
                        var nYAdjust = (int)(zLayoutAdjusted.height / 2) - (int)(nOriginalHeight / 2);
                        ElementManager.ProcessElementsChange(zLayoutAdjusted.Element, nXAdjust, nYAdjust, 0, 0, 1, 1, true, listUserActions);
                        bProcessedElementChange = true;
                        break;

                    case ELEMENTS_ADJUST_SCALE:
                        var dHorizontalScale = (decimal)zLayoutAdjusted.width / (decimal)nOriginalWidth;
                        var dVerticalScale   = (decimal)zLayoutAdjusted.height / (decimal)nOriginalHeight;
                        ElementManager.ProcessElementsChange(zLayoutAdjusted.Element, 0, 0, 0, 0, dHorizontalScale, dVerticalScale, true, listUserActions);
                        bProcessedElementChange = true;
                        break;
                    }
                }
                if (!bProcessedElementChange)
                {
                    UserAction.PushActions(listUserActions);
                }

                if (bCreateNew)
                {
                    ProjectManager.Instance.AddLayout(zLayoutAdjusted);
                }
                else
                {
                    LayoutManager.Instance.FireLayoutUpdatedEvent(true);
                }
            }
        }