diff --git a/Tests/Medication/ImporterTest.cs b/Tests/Medication/ImporterTest.cs new file mode 100755 index 0000000..2a69659 --- /dev/null +++ b/Tests/Medication/ImporterTest.cs @@ -0,0 +1,45 @@ +/* ImporterTest.cs + * part of zaaReloaded2 + * + * Copyright 2015 Daniel Kraus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; + +namespace Tests.Medication +{ + [TestFixture] + class ImporterTest + { + [Test] + public void ImportDrugsTwoColumns() + { + string s = + "Aktuelle Medikation:\r" + + "Advagraf 1 mg 2-0-0 CellCept 500 mg 1-0-1\r" + + "CellCept 250 mg 1-0-1 Decortin 10 mg 1-0-0\r" + + "Beloc-Zok mite 1-0-1 Ramipril 5 mg 0-0-1 (neu)\r" + + "Pantozol 40 mg 0-0-1 Decostriol 0,5 µg 2-0-0\r" + + "Euthyrox 200 µg 1-1-1 (gesteigert) Ossofortin forte 1-0-1\r" + + "Vfend 200 mg 2-0-2 CPS-Pulver 0-1-0\r" + + "Cyklokapron 500 mg 1-1-1 Tamsulosin 0,4 mg 1-0-0 "; + zaaReloaded2.Medication.Importer i = new zaaReloaded2.Medication.Importer(s); + Assert.AreEqual(14, i.Prescriptions.Count); + } + } +} diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index d47ddf6..91145c5 100755 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -83,6 +83,7 @@ + diff --git a/gimp/m.xcf b/gimp/m.xcf new file mode 100644 index 0000000..bf91a15 Binary files /dev/null and b/gimp/m.xcf differ diff --git a/zaaReloaded2/Commands.cs b/zaaReloaded2/Commands.cs index 6cd54ec..6354980 100755 --- a/zaaReloaded2/Commands.cs +++ b/zaaReloaded2/Commands.cs @@ -138,6 +138,31 @@ namespace zaaReloaded2 Globals.ThisAddIn.Application.Selection); } + public static void FormatDrugs() + { + // 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; + Word.Selection sel = activeWindow.Selection; + if (!(sel.Paragraphs.Count > 1 + || (sel.Text.Length > 1 && sel.Text.EndsWith("\r")))) + { + if (!Medication.Importer.AutoDetect(activeWindow.Document)) + { + NotificationAction a = new NotificationAction(); + a.Caption = "Formatieren nicht möglich"; + a.Message = "Das Dokument scheint keine Medikationsliste zu enthalten."; + a.OkButtonLabel = "Schließen"; + a.Invoke(); + return; + } + } + + Medication.Importer importer = new Medication.Importer(activeWindow.Selection.Text); + Medication.Formatter formatter = new Medication.Formatter(importer.Prescriptions); + formatter.FormatOneColumn(activeWindow.Document); + } + #endregion #region Private methods diff --git a/zaaReloaded2/Demo/Demo.docx b/zaaReloaded2/Demo/Demo.docx index ac0714c..e9f87bc 100755 Binary files a/zaaReloaded2/Demo/Demo.docx and b/zaaReloaded2/Demo/Demo.docx differ diff --git a/zaaReloaded2/Formatter/Formatter.cs b/zaaReloaded2/Formatter/Formatter.cs index 2d39b55..27c0c2b 100755 --- a/zaaReloaded2/Formatter/Formatter.cs +++ b/zaaReloaded2/Formatter/Formatter.cs @@ -142,13 +142,7 @@ namespace zaaReloaded2.Formatter // Create undo record and styles prior to iterating over the elements // because a column switching element might trigger output to the // document. - bool hasAddin = Globals.ThisAddIn != null; - if (hasAddin) - { - Globals.ThisAddIn.Application.UndoRecord.StartCustomRecord( - String.Format("Laborformatierung ({0})", Properties.Settings.Default.AddinName) - ); - } + Helpers.StartUndo("Laborformatierung"); CreateStyles(); int current = 0; @@ -177,10 +171,7 @@ namespace zaaReloaded2.Formatter } _secondaryBuffer.Flush(); - if (hasAddin) - { - Globals.ThisAddIn.Application.UndoRecord.EndCustomRecord(); - } + Helpers.EndUndo(); } /// diff --git a/zaaReloaded2/Helpers.cs b/zaaReloaded2/Helpers.cs index 265bd15..b658361 100755 --- a/zaaReloaded2/Helpers.cs +++ b/zaaReloaded2/Helpers.cs @@ -40,5 +40,30 @@ namespace zaaReloaded2 new string[] { "\r\n", "\n\r", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries); } + + /// + /// Starts a custom undo record. + /// + /// + public static void StartUndo(string message) + { + if (Globals.ThisAddIn != null) + { + Globals.ThisAddIn.Application.UndoRecord.StartCustomRecord( + String.Format("{0} ({1})", message, Properties.Settings.Default.AddinName) + ); + } + } + + /// + /// Ends an undo record. + /// + public static void EndUndo() + { + if (Globals.ThisAddIn != null) + { + Globals.ThisAddIn.Application.UndoRecord.EndCustomRecord(); + } + } } } diff --git a/zaaReloaded2/Icons/m.png b/zaaReloaded2/Icons/m.png new file mode 100644 index 0000000..b1c5cba Binary files /dev/null and b/zaaReloaded2/Icons/m.png differ diff --git a/zaaReloaded2/Medication/Formatter.cs b/zaaReloaded2/Medication/Formatter.cs new file mode 100755 index 0000000..9aadc30 --- /dev/null +++ b/zaaReloaded2/Medication/Formatter.cs @@ -0,0 +1,83 @@ +using Microsoft.Office.Interop.Word; +/* Formatter.cs + * part of zaaReloaded2 + * + * Copyright 2015 Daniel Kraus + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace zaaReloaded2.Medication +{ + /// + /// Formats prescriptions + /// + public class Formatter + { + #region Properties + + public IList Prescriptions { get; set; } + + #endregion + + #region Constructor + + public Formatter() { } + + public Formatter(IList prescriptions) + : this() + { + Prescriptions = prescriptions; + } + + #endregion + + #region Methods + + /// + /// Writes a block of prescriptions with one column to a + /// Word document. + /// + /// + public void FormatOneColumn(Document document) + { + if (document == null) + { + throw new ArgumentNullException( + "Cannot format prescriptions because no document was given."); + } + + Helpers.StartUndo("Medikation formatieren"); + foreach (Prescription p in Prescriptions) + { + document.ActiveWindow.Selection.TypeText(p.ToString() + "\r"); + } + Helpers.EndUndo(); + } + + /// + /// Creates a table containing all prescriptions and copies it to + /// the clipboard. + /// + public void CreatePrescriptionsTable() + { + throw new NotImplementedException(); + } + + #endregion + } +} diff --git a/zaaReloaded2/Medication/Importer.cs b/zaaReloaded2/Medication/Importer.cs index 2da3f3d..2057683 100755 --- a/zaaReloaded2/Medication/Importer.cs +++ b/zaaReloaded2/Medication/Importer.cs @@ -36,7 +36,7 @@ namespace zaaReloaded2.Medication /// The detected block is selected. /// /// True if a block was detected, false if not. - public bool AutoDetect(Document document) + public static bool AutoDetect(Document document) { Paragraph start = null; Paragraph end = null; @@ -47,20 +47,20 @@ namespace zaaReloaded2.Medication string line = document.Paragraphs[i].Range.Text; if (Prescription.IsPrescriptionLine(line)) { - start = document.Paragraphs[i]; + end = document.Paragraphs[i]; break; } i--; } - if (start != null) + if (end != null) { - end = start; - while (i > 1) + start = end; + while (i > 2) { if (!Prescription.IsPrescriptionLine(document.Paragraphs[i - 1].Range.Text)) { - end = document.Paragraphs[i]; + start = document.Paragraphs[i]; break; } i--; @@ -102,7 +102,7 @@ namespace zaaReloaded2.Medication { if (Prescription.IsPrescriptionLine(line)) { - Prescriptions.Add(Prescription.FromLine(line)); + Prescriptions.AddRange(Prescription.ManyFromLine(line)); } } } diff --git a/zaaReloaded2/Ribbon.cs b/zaaReloaded2/Ribbon.cs index d098166..95996b6 100755 --- a/zaaReloaded2/Ribbon.cs +++ b/zaaReloaded2/Ribbon.cs @@ -105,6 +105,9 @@ namespace zaaReloaded2 case "zrlDemo": Commands.LoadDemo(); break; + case "zrlFormatDrugs": + Commands.FormatDrugs(); + break; default: throw new InvalidOperationException("No operation defined for " + control.Id); } @@ -150,6 +153,11 @@ namespace zaaReloaded2 return Commands.CanFormat(); } + public bool CanFormatDrugs(Office.IRibbonControl control) + { + return Commands.CanFormat(); + } + #endregion #region Public methods diff --git a/zaaReloaded2/Ribbon.xml b/zaaReloaded2/Ribbon.xml index 0fb6d2e..a59f63b 100755 --- a/zaaReloaded2/Ribbon.xml +++ b/zaaReloaded2/Ribbon.xml @@ -23,8 +23,8 @@ - -