File: TEliteLaChromLogDialog.cpp

package info (click to toggle)
gentle 1.9+cvs20100605+dfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: squeeze
  • size: 12,264 kB
  • ctags: 5,235
  • sloc: cpp: 41,571; ansic: 3,978; sh: 1,420; makefile: 291
file content (349 lines) | stat: -rw-r--r-- 11,743 bytes parent folder | download | duplicates (6)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "TEliteLaChromLogDialog.h"

BEGIN_EVENT_TABLE(TEliteLaChromLogDialog, wxDialog )
    EVT_CHAR_HOOK(TEliteLaChromLogDialog::OnCharHook)
    EVT_LISTBOX(ELL_LB_DATES, TEliteLaChromLogDialog::OnDatesList)
    EVT_LISTBOX(ELL_LB_USERS, TEliteLaChromLogDialog::OnUserList)
    EVT_BUTTON(ELL_B_EXCEL,TEliteLaChromLogDialog::OnExcel)
END_EVENT_TABLE()

TEliteLaChromLogDialog::TEliteLaChromLogDialog ( wxWindow *parent, const wxString& title , wxString basefile )
    : wxDialog ( parent , -1 , title , wxDefaultPosition , wxSize ( 790 , 400 ) )
{
    int a ;
    
    col_headers.Clear() ;
    col_headers.Add ( _T("Date") ) ;
    col_headers.Add ( _T("Time") ) ;
    col_headers.Add ( _T("Sample Id") ) ;
    col_headers.Add ( _T("File Name") ) ;
    col_headers.Add ( _T("Method Name") ) ;
    col_headers.Add ( _T("User Name") ) ;
    col_headers.Add ( _T("Vial") ) ;
    col_headers.Add ( _T("Volume") ) ;
    col_headers.Add ( _T("Autosampler") ) ;
    col_headers.Add ( _T("Apex") ) ;
    col_headers.Add ( _T("Area") ) ;
    col_headers.Add ( _T("EstdConc") ) ;

    
    // Setup data
    basefile = basefile.BeforeLast ( '.' ) ;
    filename_apex = basefile + _T(".Apex") ;
    filename_area = basefile + _T(".Area") ;
    filename_estdconc = basefile + _T(".ESTDConc") ;
    
    ReadFile ( filename_apex , data_apex ) ;
    ReadFile ( filename_area , data_area ) ;
    ReadFile ( filename_estdconc , data_estdconc ) ;
    
    for ( a = 0 ; a <= ELL_DATE_TIME ; a++ ) unique[a].Clear() ;
    Uniquify ( data_apex , tabs_apex ) ;
    Uniquify ( data_area , tabs_area ) ;
    Uniquify ( data_estdconc , tabs_estdconc ) ;
    for ( a = 0 ; a <= ELL_DATE_TIME ; a++ ) unique[a].Sort ( (bool) true ) ;
    
    tabs_merged = tabs_apex ;
    MergeWith ( tabs_area , ELL_DATA2 ) ;
    MergeWith ( tabs_estdconc , ELL_DATA3 ) ;
    SortTabs ( tabs_merged ) ;
    
    // Now create inteface
    wxBoxSizer *v0 = new wxBoxSizer ( wxVERTICAL ) ;
    wxBoxSizer *h0 = new wxBoxSizer ( wxHORIZONTAL ) ;
    wxBoxSizer *h1 = new wxBoxSizer ( wxHORIZONTAL ) ;
    
    wxButton *b_excel = new wxButton ( this , ELL_B_EXCEL , txt("b_open_excel") ) ;
    
    unique_dates = new wxListBox ( this , ELL_LB_DATES , wxDefaultPosition , wxDefaultSize, 0 , NULL , wxLB_EXTENDED ) ;
    unique_users = new wxListBox ( this , ELL_LB_USERS , wxDefaultPosition , wxDefaultSize, 0 , NULL , wxLB_EXTENDED ) ;
    lines = new wxListCtrl ( this , -1 , wxDefaultPosition , wxDefaultSize , wxLC_REPORT ) ;
        
    for ( a = 0 ; a < col_headers.GetCount() ; a++ )
        lines->InsertColumn (  a , col_headers[a] ) ;

    h0->Add ( unique_dates , 1 , wxEXPAND|wxALL , 2 ) ;
    h0->Add ( unique_users , 1 , wxEXPAND|wxALL , 2 ) ;
    
    h1->Add ( b_excel , 0 , wxEXPAND|wxALL , 2 ) ;
    
    v0->Add ( h0 , 1 , wxEXPAND|wxALL , 2 ) ;
    v0->Add ( lines , 1 , wxEXPAND|wxALL , 2 ) ;
    v0->Add ( h1 , 0 , wxEXPAND|wxALL , 2 ) ;
    
    // Fill interface with data
    unique_users->Append ( txt("t_eld_all_users") ) ;
    for ( a = 0 ; a < unique[ELL_USER].GetCount() ; a++ ) unique_users->Append ( unique[ELL_USER][a] ) ;

    unique_dates->Append ( txt("t_eld_all_dates") ) ;
    for ( a = 0 ; a < unique[ELL_DATE_TIME].GetCount() ; a++ )
    {
        unique_dates->Append ( unique[ELL_DATE_TIME][a] ) ;
    }

    unique_users->SetSelection ( 0 ) ;
    unique_dates->SetSelection ( 0 ) ;
    
    UpdateLines() ;

    this->SetSizer ( v0 ) ;
    v0->Layout() ;//Fit ( this ) ;
//    Center() ;
}


void TEliteLaChromLogDialog::ReadFile ( wxString filename , wxArrayString &data )
{
    data.Clear() ;
    if ( !wxFileExists ( filename ) ) return ; // Too bad...
    wxFile f ( filename ) ;
    long l = f.Length() ;
    char *c = new char[l+5] , *d , *ld ;
    f.Read ( c , l ) ;
    c[l] = '\n' ;
    c[l+1] = 0 ;
    wxCSConv co ( _T("iso-8859-1") ) ;
    
    for ( d = ld = c ; *d ; d++ )
    {
        if ( *d == '\n' )
        {
            *d = 0 ;
            wxString s ( ld , co ) ;
            s.Replace ( _T("\r") , _T("") ) ;
            data.Add ( s ) ;
            ld = d + 1 ;
        }
    }
    delete c ;
}

#define MAKE_DATE_TIME(line) line[0].Mid(6,4) + _T("-") + line[0].Mid(3,2) + _T("-") + line[0].Mid(0,2) + _T(" ") + line[1]

void TEliteLaChromLogDialog::Uniquify ( wxArrayString &data , vector <wxArrayString> &tabs )
{
    int a , b ;
    tabs.clear() ;
    for ( a = 3 ; a < data.GetCount() ; a++ )
    {
        wxString s = data[a] ;
        if ( s.IsEmpty() ) continue ;
        wxArrayString line ;
        while ( !s.IsEmpty() )
        {
            line.Add ( s.BeforeFirst ( (char) 9 ) ) ;
            s = s.AfterFirst ( (char) 9 ) ;
        }
        while ( line.GetCount() < ELL_DATE_TIME ) line.Add ( _T("") ) ;
        s = MAKE_DATE_TIME ( line ) ; // DateTime
        line.Add ( s ) ;
        for ( b = 0 ; b <= ELL_DATE_TIME ; b++ )
        {
            if ( wxNOT_FOUND == unique[b].Index ( line[b] ) ) unique[b].Add ( line[b] ) ;
        }
        tabs.push_back ( line ) ;
    }
}

void TEliteLaChromLogDialog::FilterLines ()
{
    tabs_display = tabs_merged ;
    
    if ( !unique_dates->IsSelected ( 0 ) ) // If not "All dates"
    {
        int a , b ;
        wxArrayString dates , times ;
        for ( a = 1 ; a < unique_dates->GetCount() ; a++ ) // Parse dates/times
        {
            if ( !unique_dates->IsSelected ( a ) ) continue ; // Not selected
            wxString s = unique_dates->GetString ( a ) ;
            wxString d = s.Mid ( 8 , 2 ) + _T(".") + s.Mid ( 5 , 2 ) + _T(".") + s.Left ( 4 ) ;
            wxString t = s.Mid ( 11 ) ;
            dates.Add ( d ) ;
            times.Add ( t ) ;
        }
        vector <wxArrayString> n ;
        for ( a = 0 ; a < tabs_display.size() ; a++ )
        {
            for ( b = 0 ; b < dates.GetCount() && ( dates[b] != tabs_display[a][0] || times[b] != tabs_display[a][1] ) ; b++ ) ;
            if ( b == dates.GetCount() ) continue ; // No matching time/date
            n.push_back ( tabs_display[a] ) ;
        }
        tabs_display = n ;
    }
    
    if ( !unique_users->IsSelected ( 0 ) ) // If not "All dates"
    {
        int a , b ;
        wxArrayString users ;
        for ( a = 1 ; a < unique_users->GetCount() ; a++ ) // Parse dates/times
        {
            if ( !unique_users->IsSelected ( a ) ) continue ; // Not selected
            users.Add ( unique_users->GetString ( a ) ) ;
        }
        vector <wxArrayString> n ;
        for ( a = 0 ; a < tabs_display.size() ; a++ )
        {
            if ( wxNOT_FOUND == users.Index ( tabs_display[a][ELL_USER] ) ) continue ;
            n.push_back ( tabs_display[a] ) ;
        }
        tabs_display = n ;
    }
    
    
}

void TEliteLaChromLogDialog::UpdateLines ()
{
    FilterLines() ;
    lines->DeleteAllItems() ;
    int a , b ;
    for ( a = 0 ; a < tabs_display.size() ; a++ )
    {
        for ( b = 0 ; b < tabs_display[a].GetCount() ; b++ )
        {
            if ( b == 0 ) lines->InsertItem ( a , tabs_display[a][b] ) ;
            else lines->SetItem ( a , b , tabs_display[a][b] ) ;
        }
    }
    lines->SetColumnWidth ( ELL_DATE , -2 ) ;
    lines->SetColumnWidth ( ELL_TIME , -2 ) ;
//    lines->SetColumnWidth ( ELL_SAMPLE , -2 ) ;
    for ( a = 3 ; a < 13 ; a++ ) lines->SetColumnWidth ( a , 70 ) ;
    lines->SetColumnWidth ( ELL_USER , 50 ) ;
    lines->SetColumnWidth ( ELL_VIAL , 30 ) ;
    lines->SetColumnWidth ( ELL_AUTOSAMPLER , 50 ) ;
}

void TEliteLaChromLogDialog::MergeWith ( vector <wxArrayString> &tabs , int col )
{
    vector <wxArrayString> nt ;
    int a , b , c ;
    
    for ( a = 0 ; a < tabs.size() ; a++ )
    {
        for ( b = 0 ; b < tabs_merged.size() ; b++ )
        {
            for ( c = 0 ; c < ELL_PROGRAM && tabs[a][c] == tabs_merged[b][c] ; c++ ) ;
            if ( c < ELL_PROGRAM ) continue ; // No match
            wxArrayString n = tabs_merged[b] ;
            while ( n.GetCount() <= col ) n.Add ( _T("") ) ;
            n[col] = tabs[a][ELL_DATA1] ;
            nt.push_back ( n ) ;
//            tabs_merged.erase ( b ) ;
            break ;
        }
    }
    
    tabs_merged = nt ;
}


void TEliteLaChromLogDialog::OnCharHook(wxKeyEvent& event)
{
    int k = event.GetKeyCode () ;
    wxCommandEvent ev ;
    if ( k == WXK_ESCAPE ) EndModal ( wxID_CANCEL ) ; //OnCancel ( ev ) ;
//    else if ( k == WXK_F1 ) myapp()->frame->OnHelp(ev) ;
    else event.Skip() ;
}

void TEliteLaChromLogDialog::OnDatesList(wxCommandEvent &event)
{
    FilterLines() ;
    UpdateLines() ;
}

void TEliteLaChromLogDialog::OnUserList(wxCommandEvent &event)
{
    FilterLines() ;
    UpdateLines() ;
}

void TEliteLaChromLogDialog::SortTabs ( vector <wxArrayString> &tabs )
{
    int a , b ;
    
    // Sorting
    for ( a = 1 ; a < tabs.size() ; a++ )
    {
        wxArrayString t1 = tabs[a-1] ;
        wxArrayString t2 = tabs[a] ;
        wxString dt1 = MAKE_DATE_TIME ( t1 ) ;
        wxString dt2 = MAKE_DATE_TIME ( t2 ) ;
        bool swap = false ;
        if ( dt1 > dt2 ) swap = true ;
        else if ( dt1 == dt2 )
        {
            for ( b = 2 ; b < t1.GetCount() && b < t2.GetCount() && t1[b] == t2[b] ; b++ ) ;
            if ( b < t1.GetCount() && b < t2.GetCount() ) swap = t1[b] > t2[b] ;
            else if ( b < t1.GetCount() ) swap = true ;
        }
        if ( !swap ) continue ; // In correct order
        tabs[a-1] = t2 ;
        tabs[a] = t1 ;
        a -= 2 ;
        if ( a < 0 ) a = 0 ;
    }
    
    // Removing doubles, inverting
    vector <wxArrayString> n ;
    for ( a = 0 ; a < tabs.size() ; a++ )
    {
        if ( a == 0 ) n.push_back ( tabs[a] ) ;
        else
        {
            for ( b = 0 ; b < tabs[a-1].GetCount() && b < tabs[a].GetCount() && tabs[a-1][b] == tabs[a][b] ; b++ ) ;
            if ( b < tabs[a-1].GetCount() || b < tabs[a].GetCount() ) n.push_back ( tabs[a] ) ;
        }
    }
    tabs = n ;
}

void TEliteLaChromLogDialog::OnExcel(wxCommandEvent &event)
{
    int a , b ;
    wxString s , sep = _T("\t") , q = _T("\"") ;
    
    // Headings
    for ( a = 0 ; a < col_headers.GetCount() ; a++ )
    {
        if ( a > 0 ) s += sep ;
        s += q + col_headers[a] + q ;
    }
    s += _T("\r\n") ;
    
    // Data
    for ( a = 0 ; a < tabs_display.size() ; a++ )
    {
        for ( b = 0 ; b < tabs_display[a].GetCount() ; b++ )
        {
            if ( b > 0 ) s += sep ;
            wxString t = tabs_display[a][b] ;
            t.Replace ( q , _T("'") ) ;
//            if ( b > ELL_USER ) t.Replace ( _T(".") , _T(",") ) ;
            t = t.Trim().Trim(false) ;
            if ( b >= ELL_DATA1 )
            {
                t = _T("=") + t.BeforeFirst ( ' ' ) ;
                if ( t == _T("=") ) t = _T("=0") ;
                t = t.Trim().Trim(false) ;
            }
            s += q + t + q ;
        }
        s += _T("\r\n") ;
    }
    
    wxString filename = wxFileName::CreateTempFileName ( _T("") ) + _T(".txt") ;
//    filename = _T("C:\\1.txt") ; // Testing...
    wxFile file ( filename , wxFile::write ) ;
    file.Write ( s , *wxConvCurrent ) ;
    file.Close() ;
    
//    wxString cmd = myapp()->getFileFormatCommand ( _T("csv") , filename ) ;
    wxString cmd = myapp()->getFileFormatApplication ( _T("xls") ) ;
    cmd += _T(" \"") + filename + q ;
//    wxMessageBox ( cmd ) ;
    wxExecute ( cmd ) ;
//    wxRemoveFile ( filename ) ;
}