-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFormPrintSampleLabel.cs
265 lines (221 loc) · 9.3 KB
/
FormPrintSampleLabel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
DSA Lims - Laboratory Information Management System
Copyright (C) 2018 Norwegian Radiation Protection Authority
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Authors: Dag Robole,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Printing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DSA_lims
{
public partial class FormPrintSampleLabel : Form
{
PrivateFontCollection privateFonts = new PrivateFontCollection();
Font fontBarcode = null;
Font fontLabel = null;
DSASettings mSettings = null;
List<Guid> mSampleIds = new List<Guid>();
string sampleNumber;
string externalSampleId;
string projectMain;
string projectSub;
string laboratory;
string sampleType;
string samplePart;
PrintDocument printDocument = new PrintDocument();
public FormPrintSampleLabel(DSASettings s, List<Guid> sampleIds)
{
InitializeComponent();
mSampleIds = sampleIds;
tbCopies.Text = "1";
tbCopies.KeyPress += CustomEvents.Integer_KeyPress;
tbReplications.Text = "1";
tbReplications.KeyPress += CustomEvents.Integer_KeyPress;
mSettings = s;
cboxPaperSizes.DisplayMember = "PaperName";
}
private void FormPrintSampleLabel_Load(object sender, EventArgs e)
{
try
{
fontLabel = new Font("Arial", 8);
string InstallationDirectory = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
string fontFileName = InstallationDirectory + Path.DirectorySeparatorChar + "free3of9.ttf";
if (File.Exists(fontFileName))
{
privateFonts.AddFontFile(InstallationDirectory + Path.DirectorySeparatorChar + "free3of9.ttf");
fontBarcode = new Font(privateFonts.Families[0], 38, FontStyle.Regular);
}
else fontBarcode = fontLabel;
cboxPrinters.SelectedIndexChanged -= cboxPrinters_SelectedIndexChanged;
foreach (string p in PrinterSettings.InstalledPrinters)
cboxPrinters.Items.Add(p.ToString());
cboxPrinters.SelectedIndexChanged += cboxPrinters_SelectedIndexChanged;
if (cboxPrinters.FindString(mSettings.LabelPrinterName) > -1)
cboxPrinters.Text = mSettings.LabelPrinterName;
cbLandscape.Checked = mSettings.LabelPrinterLandscape;
}
catch (Exception ex)
{
Common.Log.Error(ex);
MessageBox.Show(ex.Message);
DialogResult = DialogResult.Abort;
Close();
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void btnOk_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(cboxPrinters.Text))
{
MessageBox.Show("You must select a printer");
return;
}
if (cboxPaperSizes.SelectedItem == null)
{
MessageBox.Show("You must select a printer size");
return;
}
if (String.IsNullOrEmpty(tbCopies.Text))
{
MessageBox.Show("Number of copies must be a positive number");
return;
}
int copies = Convert.ToInt32(tbCopies.Text);
if (copies < 1)
{
MessageBox.Show("Number of copies must be a positive number");
return;
}
if (String.IsNullOrEmpty(tbReplications.Text))
{
MessageBox.Show("Number of repliactions must be a positive number");
return;
}
int reps = Convert.ToInt32(tbReplications.Text);
if(reps < 1)
{
MessageBox.Show("Number of repliactions must be a positive number");
return;
}
PaperSize paperSize = cboxPaperSizes.SelectedItem as PaperSize;
printDocument.DefaultPageSettings.Landscape = cbLandscape.Checked;
printDocument.DefaultPageSettings.PaperSize = paperSize;
printDocument.PrintPage += PrintDocument_PrintPage;
string query = @"
select
s.number as 'sample_number',
s.external_id as 'external_id',
st.name as 'sample_type_name',
pm.name as 'project_main_name',
ps.name as 'project_sub_name',
l.name as 'laboratory_name'
from sample s
inner join sample_type st on s.sample_type_id = st.id
inner join project_sub ps on s.project_sub_id = ps.id
inner join project_main pm on ps.project_main_id = pm.id
inner join laboratory l on s.laboratory_id = l.id
where s.id = @id
";
SqlConnection conn = null;
try
{
conn = DB.OpenConnection();
foreach (Guid sid in mSampleIds)
{
using (SqlDataReader reader = DB.GetDataReader(conn, null, query, CommandType.Text, new SqlParameter("@id", sid)))
{
if (!reader.HasRows)
continue;
reader.Read();
sampleNumber = reader.GetString("sample_number");
externalSampleId = reader.GetString("external_id");
sampleType = reader.GetString("sample_type_name");
projectMain = reader.GetString("project_main_name");
projectSub = reader.GetString("project_sub_name");
laboratory = reader.GetString("laboratory_name");
for (int c = 0; c < copies; c++)
{
for (int r = 1; r <= reps; r++)
{
samplePart = r.ToString() + "/" + reps.ToString();
printDocument.Print();
}
}
}
}
}
catch (Exception ex)
{
Common.Log.Error(ex);
MessageBox.Show(ex.Message);
return;
}
finally
{
conn?.Close();
}
mSettings.LabelPrinterName = cboxPrinters.Text;
mSettings.LabelPrinterPaperName = paperSize.PaperName;
mSettings.LabelPrinterLandscape = cbLandscape.Checked;
DialogResult = DialogResult.OK;
Close();
}
private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString("ID: " + sampleNumber, fontLabel, Brushes.Black, 1, 1);
e.Graphics.DrawString("Ex.ID: " + externalSampleId, fontLabel, Brushes.Black, 1, 12);
e.Graphics.DrawString("Sample type: " + sampleType, fontLabel, Brushes.Black, 1, 24);
e.Graphics.DrawString("Main project: " + projectMain, fontLabel, Brushes.Black, 1, 36);
e.Graphics.DrawString("Sub project: " + projectSub, fontLabel, Brushes.Black, 1, 48);
e.Graphics.DrawString("Laboratory: " + laboratory, fontLabel, Brushes.Black, 1, 60);
e.Graphics.DrawString("Batch: " + samplePart, fontLabel, Brushes.Black, 1, 72);
e.Graphics.DrawString("*" + sampleNumber + "*", fontBarcode, Brushes.Black, 1, 86);
if (Common.LabLogo != null)
{
Image img = UtilsMedia.CropImageToHeight(Common.LabLogo, 40);
e.Graphics.DrawImage(img, 210f, 84f);
}
}
private void cboxPrinters_SelectedIndexChanged(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(cboxPrinters.Text))
return;
cboxPaperSizes.Items.Clear();
printDocument.PrinterSettings.PrinterName = cboxPrinters.Text;
foreach(PaperSize ps in printDocument.PrinterSettings.PaperSizes)
cboxPaperSizes.Items.Add(ps);
foreach (PaperSize ps in cboxPaperSizes.Items)
{
if (ps.PaperName == mSettings.LabelPrinterPaperName)
{
cboxPaperSizes.SelectedItem = ps;
break;
}
}
}
}
}