From 6ec282ee681029ae1e88147bad54503906ad305b Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Tue, 1 Dec 2015 17:38:44 +0100 Subject: [PATCH] Improve formatting of prescriptions. --- gimp/mm.xcf | Bin 0 -> 2881 bytes zaaReloaded2/Commands.cs | 20 ++- zaaReloaded2/Formatter/DocumentWriter.cs | 8 ++ zaaReloaded2/Icons/mm.png | Bin 0 -> 723 bytes zaaReloaded2/Medication/Formatter.cs | 131 +++++++++++++++++-- zaaReloaded2/Properties/Settings.Designer.cs | 9 ++ zaaReloaded2/Properties/Settings.settings | 3 + zaaReloaded2/Ribbon.cs | 7 +- zaaReloaded2/Ribbon.xml | 13 +- zaaReloaded2/app.config | 3 + zaaReloaded2/zaaReloaded2.csproj | 5 +- 11 files changed, 179 insertions(+), 20 deletions(-) create mode 100644 gimp/mm.xcf create mode 100644 zaaReloaded2/Icons/mm.png diff --git a/gimp/mm.xcf b/gimp/mm.xcf new file mode 100644 index 0000000000000000000000000000000000000000..b4162edb1cb367a7d698fd3792257bfc60fe0cb9 GIT binary patch literal 2881 zcmeHJOHUL*5bjx)W!aVZ_Fz1;cyKw4LVR322?t|P@#e|HFw--%nFr3yhFwi$e}VBg zsQ<#mn>T-gF$PW~9#jx;1Jiyr4_tKB8^mCds;>UJdb+3T+hQE)&GVLNb6dKCL3pm@ zABAcf%yDRS@)={KkQ2}{bO?GX_bKiq;%6|9IC!jX2A(JU7;{F8$EHGI#KPhWGM?i% zZr;92D@W2$=^2jDolshYAzG_8qPXn}F8x@9kuYN!_|aIcVLN8v1|e^xgC07w1|PpL zo=P8QB3~NA*4aK=i*^2chqkt&dBc>x!zYTf0Ji&qIi@##8RPQioS<8`0^5#6ymLX? z{VS9qySGw4a?xHhRyo92?OuUkJPbVzr3?>x{M-aaR)JD|6ttTA6fm6o7}%K}zt-c| zdwd+0J_iFE>GA1MdM**91SKBOc_all=nix-!Uv$ljr$n0g2$p2>#orjA)vJ2*BgZK zQEwE&9ORL~5i2>Bhcz$XLODpvGJJ`()$m7=y;e2Qkto0l!&Qr-cAy9Y zpLSzey`Hc{p@f=RhochwZ6{jNRb86Y)YeLuDNU_y(JGlF!E^&rS@1=b&^k;hGuw#K z@C-sTt0kpf^CV#oJ8;LAk>nMmd|9bCAJy03l~V5;WtwLB>LP*9hcp&$s%iGJt7h1X z^@o*8=gZ?7nc+%jwS#GoS)}P73=2Pp41064go3PYu+BG?nx$pT*EZQxrBbtuqCg71 zP_rm`gzQx-Nl(Oi{aVQ&@c*r3e_BcPJh1>$Py-~cUdx~Yt4`DW!2PE8sd4@zt%QRT zr1ZnC+$#Rl)7JvYUbU6%wEk^{T1_r>`nn 2) + { + throw new ArgumentOutOfRangeException("Can only format 1 or 2 columns, not " + columns); + } + // If no "real" selection exists, attempt to auto-detect the drugs section. // (NB Technically, there is never _no_ selection in a document.) Word.Window activeWindow = Globals.ThisAddIn.Application.ActiveWindow; @@ -160,7 +165,18 @@ namespace zaaReloaded2 Medication.Importer importer = new Medication.Importer(activeWindow.Selection.Text); Medication.Formatter formatter = new Medication.Formatter(importer.Prescriptions); - formatter.FormatOneColumn(activeWindow.Document); + + switch (columns) + { + case 1: + formatter.FormatOneColumn(activeWindow.Document); + break; + case 2: + formatter.FormatTwoColumns(activeWindow.Document); + break; + default: + break; + } } #endregion diff --git a/zaaReloaded2/Formatter/DocumentWriter.cs b/zaaReloaded2/Formatter/DocumentWriter.cs index 460ca9f..29548c2 100755 --- a/zaaReloaded2/Formatter/DocumentWriter.cs +++ b/zaaReloaded2/Formatter/DocumentWriter.cs @@ -171,6 +171,14 @@ namespace zaaReloaded2.Formatter _buffer.AppendLine(text); } + /// + /// Appends a newline to the buffer. + /// + public void WriteLine() + { + _buffer.AppendLine(); + } + /// /// Inserts text at the start of the buffer. /// diff --git a/zaaReloaded2/Icons/mm.png b/zaaReloaded2/Icons/mm.png new file mode 100644 index 0000000000000000000000000000000000000000..0fceba4c70089b2176236c461a9fea5707f1f1b9 GIT binary patch literal 723 zcmV;^0xbQBP)eSad^gZEa<4bO1wgWnpw>WFU8GbZ8()Nlj2!fese{00JvX zL_t(o!|hf*Q`-M9DkT_g`>45tu+x5#eEY17-Il{!|q`-?kBz?ae;vZOI!eo zSjjD&$9-nE?b_DeRviFV^Q*4_upvLxE|t_FGwP>Tk$e`Zw_(x*8D!WvLL4^$z{*>N zwU=wJIXJ5S$*<#G(HZW!cCmi{sRe9oZ!(d-3nSV zIwk@g3|;&r+*qlueDt-imOn3lD0j;Ba;ID`U6nrhI#PU70e3w=L;|a;ONa! zao(JN0st5CqF3OBY>ws3KKgq1+irF7+2V`h)8cag@RTRlo9mZXgDa_^C^cWW!V#`; zcJ6j=`ns=)h&GxVpM)!%litZ~IDa?4Y+ediILE!?TdAem{zq{W!`*GIc;H;{uazF#x>+Urw-z#kn&T6VAHEHBBx!=c^hCiwY=#jAp|BEN$ z3s*Q^;0=BDq>@Uw!m- public void FormatOneColumn(Document document) { - if (document == null) - { - throw new ArgumentNullException( - "Cannot format prescriptions because no document was given."); - } + DoFormat("Medikation einspaltig formatieren", + document, + writer => + { + foreach (Prescription p in Prescriptions) + { + writer.WriteLine(p.ToString()); + } + }); + } - Helpers.StartUndo("Medikation formatieren"); - foreach (Prescription p in Prescriptions) - { - document.ActiveWindow.Selection.TypeText(p.ToString() + "\r"); - } - Helpers.EndUndo(); + /// + /// Writes a block of prescriptions with two columns to a + /// Word document. + /// + /// + public void FormatTwoColumns(Document document) + { + DoFormat("Medikation zweispaltig formatieren", + document, + writer => + { + for (int i = 0; i < Prescriptions.Count; i += 2) + { + writer.Write(Prescriptions[i].ToString()); + if (i + 1 < Prescriptions.Count) + { + writer.Write("\t" + Prescriptions[i+1].ToString()); + } + writer.WriteLine(); + } + }); } /// @@ -77,6 +97,95 @@ namespace zaaReloaded2.Medication { throw new NotImplementedException(); } + + #endregion + + #region Private methods + + void AddDisclaimer(zaaReloaded2.Formatter.DocumentWriter writer) + { + writer.WriteLine("Bitte Medikation überprüfen!"); + } + + /// + /// Creates a paragraph and character styles in the document. + /// + void CreateStyles(Document document) + { + if (document != null) + { + Style style; + // Don't see a better way to check for the existence of a particular + // paragraph style than by using a try...catch construction. + try + { + style = document.Styles[Properties.Settings.Default.DrugsParagraph]; + } + catch + { + // Add default paragraph style for laboratory + style = document.Styles.Add(Properties.Settings.Default.DrugsParagraph); + style.Font.Size = 10; // pt + style.Font.Bold = 0; + style.Font.Italic = 0; + style.Font.Underline = 0; + style.ParagraphFormat.SpaceAfter = 0; + style.ParagraphFormat.SpaceBefore = 0; + style.ParagraphFormat.LeftIndent = 0; // pt + style.ParagraphFormat.FirstLineIndent = 0; // pt + style.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft; + style.ParagraphFormat.TabStops.ClearAll(); + int tabStop = 108; // 108 pt = 2.5 in = 3.8 cm + int halfWay = 227; // 227 pt = 3.15 in = 8 cm + style.ParagraphFormat.TabStops.Add(tabStop); + style.ParagraphFormat.TabStops.Add(halfWay); + style.ParagraphFormat.TabStops.Add(halfWay + tabStop); + } + + // try + // { + // style = document.Styles[Properties.Settings.Default.DrugsHeader]; + // } + // catch + // { + // // Add header paragraph style for laboratory + // style = document.Styles.Add(Properties.Settings.Default.DrugsHeader); + // style.Font.Size = 10; // pt + // style.Font.Bold = 1; + // style.Font.Italic = 0; + // style.Font.Underline = WdUnderline.wdUnderlineSingle; + // style.ParagraphFormat.SpaceAfter = 0; + // style.ParagraphFormat.SpaceBefore = 12; + // style.ParagraphFormat.LeftIndent = 36; // pt + // style.ParagraphFormat.FirstLineIndent = -36; // pt + // style.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphJustify; + // style.set_NextParagraphStyle(document.Styles[Properties.Settings.Default.DrugsParagraph]); + // } + } + } + + /// + /// Does the heavy lifting in a DRY way. + /// + void DoFormat(string description, Document document, + Action outputAction) + { + if (document == null) + { + throw new ArgumentNullException( + "Cannot format prescriptions because no document was given."); + } + + Helpers.StartUndo(description); + zaaReloaded2.Formatter.DocumentWriter writer = new zaaReloaded2.Formatter.DocumentWriter(document); + CreateStyles(document); + writer.Write(String.Format("", Properties.Settings.Default.DrugsParagraph)); + outputAction(writer); + AddDisclaimer(writer); + // writer.Write(""); // causes COM exceptions, needs fix + writer.Flush(); + Helpers.EndUndo(); + } #endregion } diff --git a/zaaReloaded2/Properties/Settings.Designer.cs b/zaaReloaded2/Properties/Settings.Designer.cs index 53fccbd..3ef9b9e 100755 --- a/zaaReloaded2/Properties/Settings.Designer.cs +++ b/zaaReloaded2/Properties/Settings.Designer.cs @@ -276,5 +276,14 @@ namespace zaaReloaded2.Properties { this["NeedUpgrade"] = value; } } + + [global::System.Configuration.ApplicationScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("zaaReloaded2-Medikamente")] + public string DrugsParagraph { + get { + return ((string)(this["DrugsParagraph"])); + } + } } } diff --git a/zaaReloaded2/Properties/Settings.settings b/zaaReloaded2/Properties/Settings.settings index d448e89..0416614 100755 --- a/zaaReloaded2/Properties/Settings.settings +++ b/zaaReloaded2/Properties/Settings.settings @@ -80,5 +80,8 @@ True + + zaaReloaded2-Medikamente + \ No newline at end of file diff --git a/zaaReloaded2/Ribbon.cs b/zaaReloaded2/Ribbon.cs index eb3809e..f008fbc 100755 --- a/zaaReloaded2/Ribbon.cs +++ b/zaaReloaded2/Ribbon.cs @@ -105,8 +105,11 @@ namespace zaaReloaded2 case "zrlDemo": Commands.LoadDemo(); break; - case "zrlFormatDrugs": - Commands.FormatDrugs(); + case "zrlFormatDrugsOneCol": + Commands.FormatDrugs(1); + break; + case "zrlFormatDrugsTwoCol": + Commands.FormatDrugs(2); break; default: throw new InvalidOperationException("No operation defined for " + control.Id); diff --git a/zaaReloaded2/Ribbon.xml b/zaaReloaded2/Ribbon.xml index a59f63b..db54938 100755 --- a/zaaReloaded2/Ribbon.xml +++ b/zaaReloaded2/Ribbon.xml @@ -29,13 +29,18 @@ getEnabled="CanFormat" />