source: tags/ms_r18q1/AWTI/AWTI_export.cxx

Last change on this file was 16763, checked in by westram, 6 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.9 KB
Line 
1// ============================================================= //
2//                                                               //
3//   File      : AWTI_export.cxx                                 //
4//   Purpose   :                                                 //
5//                                                               //
6//   Institute of Microbiology (Technical University Munich)     //
7//   http://www.arb-home.de/                                     //
8//                                                               //
9// ============================================================= //
10
11#include "awti_export.hxx"
12#include "awti_exp_local.hxx"
13
14#include <FileWatch.h>
15#include <seqio.hxx>
16#include <awt_filter.hxx>
17#include <AP_filter.hxx>
18
19#include <aw_awars.hxx>
20#include <aw_file.hxx>
21#include <aw_window.hxx>
22#include <aw_msg.hxx>
23
24#include <arbdbt.h>
25
26#include <arb_progress.h>
27#include <arb_str.h>
28#include <arb_strbuf.h>
29#include "awti_edit.hxx"
30
31using namespace SEQIO;
32
33static void export_go_cb(AW_window *aww, GBDATA *gb_main, adfiltercbstruct *acbs) {
34    awti_assert(!GB_have_error());
35
36    GB_transaction ta(gb_main);
37    arb_progress   progress("Exporting data");
38
39    AW_root     *awr            = aww->get_root();
40    char        *formname       = awr->awar(AWAR_EXPORT_FORMATNAME)->read_string();
41    int          multiple       = awr->awar(AWAR_EXPORT_MULTIPLE_FILES)->read_int();
42    ExportWhich  which          = ExportWhich(awr->awar(AWAR_EXPORT_MARKED)->read_int());
43    int          cut_stop_codon = awr->awar(AWAR_EXPORT_CUTSTOP)->read_int();
44    int          compress       = awr->awar(AWAR_EXPORT_COMPRESS)->read_int();
45    GB_ERROR     error          = NULp;
46
47    char *outname      = awr->awar(AWAR_EXPORT_FILENAME)->read_string();
48    char *real_outname = NULp; // with suffix (name of first file if multiple)
49
50    AP_filter *filter = awt_get_filter(acbs);
51    char *db_name = awr->awar(AWAR_DB_NAME)->read_string();
52
53    error = export_by_format(gb_main, which, NULp, filter, cut_stop_codon, compress,
54                             db_name, formname, outname, multiple, &real_outname);
55    free(db_name);
56    if (error) aw_message(error);
57
58    if (real_outname) awr->awar(AWAR_EXPORT_FILENAME)->write_string(real_outname);
59
60    AW_refresh_fileselection(awr, AWAR_EXPORT_FILEBASE);
61
62    free(real_outname);
63    free(outname);
64    free(formname);
65
66}
67
68static void avoid_empty_target_name(char*& nameOnly) {
69    static char *last_used_name = strdup("noname");
70    if (nameOnly) freedup(last_used_name, nameOnly);
71    else          nameOnly = strdup(last_used_name); // if name was lost (by changing directory) -> reuse last_used_name
72}
73
74static bool in_export_filename_changed_cb = false;
75static void export_filename_changed_cb(AW_root *aw_root) {
76    if (!in_export_filename_changed_cb) {
77        LocallyModify<bool> dont_recurse(in_export_filename_changed_cb, true);
78
79        AW_awar *awar_export = aw_root->awar(AWAR_EXPORT_FILENAME);
80        char    *exportname  = awar_export->read_string();
81
82        if (!GB_is_directory(exportname)) {
83            char *path, *nameOnly, *suffix;
84            GB_split_full_path(exportname, &path, NULp, &nameOnly, &suffix);
85
86            avoid_empty_target_name(nameOnly);
87
88            const char *new_exportname = GB_concat_path(path, GB_append_suffix(nameOnly, suffix));
89            if (new_exportname) awar_export->write_string(new_exportname);
90        }
91        free(exportname);
92    }
93}
94
95static void create_export_awars(AW_root *awr, AW_default def) {
96    {
97        GBS_strstruct path(500);
98        path.cat(GB_path_in_arbprop("filter"));
99        path.put(':');
100        path.cat(GB_path_in_ARBLIB("export"));
101
102        AW_create_fileselection_awars(awr, AWAR_EXPORT_FORMATBASE, path.get_data(), ".eft", "*");
103        AW_create_fileselection_awars(awr, AWAR_EXPORT_FILEBASE,   "",              "",     "noname");
104    }
105
106    awr->awar_string(AWAR_EXPORT_FORMAT_DESC, "<no format selected>", def);
107
108    awr->awar_int(AWAR_EXPORT_MULTIPLE_FILES, 0,          def);
109    awr->awar_int(AWAR_EXPORT_MARKED,         EBF_MARKED, def);
110    awr->awar_int(AWAR_EXPORT_COMPRESS,       1,          def); // vertical gaps
111
112    awt_create_filter_awars(awr, def, AWAR_EXPORT_FILTER_NAME, AWAR_DEFAULT_ALIGNMENT);
113
114    awr->awar_int(AWAR_EXPORT_CUTSTOP, 0, def); // don't cut stop-codon
115
116    awr->awar(AWAR_EXPORT_FILENAME)->add_callback(export_filename_changed_cb);
117}
118
119static void update_format_description_and_suffix(const char *eft) {
120    // called when export format has changed (filter content or selected filter)
121    // -> automatically correct filename suffix
122    // -> restrict view to suffix
123    // -> update description
124
125    ExportFormatInfo eft_info;
126    GB_ERROR         error = get_exportFormat_information(eft, eft_info);
127
128    AW_root *aw_root   = AW_root::SINGLETON;
129    AW_awar *awar_desc = aw_root->awar(AWAR_EXPORT_FORMAT_DESC);
130
131    if (error) awar_desc->write_string(error); // display format-errors in description field
132    else {
133        const char  *current_suffix  = eft_info.suffix.content(); // Note: current_suffix may be NULp.. is that ok?
134        static char *previous_suffix = NULp;
135
136        // modify export filename and view
137        AW_awar *awar_filter = aw_root->awar(AWAR_EXPORT_FILESUFFIX);
138        AW_awar *awar_export = aw_root->awar(AWAR_EXPORT_FILENAME);
139
140        awar_filter->write_string("");
141
142        char *exportname = awar_export->read_string();
143
144        {
145            LocallyModify<bool> dont_recurse(in_export_filename_changed_cb, true);
146
147            char *path, *nameOnly, *suffix;
148            GB_split_full_path(exportname, &path, NULp, &nameOnly, &suffix);
149
150            avoid_empty_target_name(nameOnly);
151
152            if (suffix) {
153                if (previous_suffix && ARB_stricmp(suffix, previous_suffix) == 0) freedup(suffix, current_suffix); // remove old suffix
154                else freedup(suffix, GB_append_suffix(suffix, current_suffix)); // don't know existing suffix -> append
155            }
156            else suffix = ARB_strdup(current_suffix);
157
158            const char *new_exportname = GB_concat_path(path, GB_append_suffix(nameOnly, suffix));
159            if (new_exportname) awar_export->write_string(new_exportname);
160
161            free(suffix);
162            free(nameOnly);
163            free(path);
164        }
165
166        free(exportname);
167
168        awar_filter->write_string(current_suffix);
169
170        // remember last applied suffix
171        freedup(previous_suffix, current_suffix);
172
173        // update description
174        if (eft_info.description.isSet()) awar_desc->write_string(&*eft_info.description);
175        else                              awar_desc->write_string("<no description>");
176    }
177}
178
179AW_window *create_AWTC_export_window(AW_root *awr, GBDATA *gb_main) {
180    static AW_window_simple *aws = NULp;
181
182    if (!aws) {
183        create_export_awars(awr, AW_ROOT_DEFAULT);
184
185        aws = new AW_window_simple;
186
187        aws->init(awr, "ARB_EXPORT", "ARB EXPORT");
188        aws->load_xfig("awt/export_db.fig");
189
190        aws->at("close");
191        aws->callback(AW_POPDOWN);
192        aws->create_button("CLOSE", "CLOSE", "C");
193
194        aws->at("help");
195        aws->callback(makeHelpCallback("arb_export.hlp"));
196        aws->create_button("HELP", "HELP", "H");
197
198        AW_create_fileselection(aws, AWAR_EXPORT_FILEBASE,   "f", "PWD",     ANY_DIR,    false); // select export filename
199        AW_create_fileselection(aws, AWAR_EXPORT_FORMATBASE, "",  "ARBHOME", MULTI_DIRS, false); // select export filter
200
201        aws->at("allmarked");
202        aws->create_option_menu(AWAR_EXPORT_MARKED, true);
203        aws->insert_option("all",    "a", EBF_ALL);
204        aws->insert_option("marked", "m", EBF_MARKED);
205        aws->update_option_menu();
206
207        aws->at("compress");
208        aws->create_option_menu(AWAR_EXPORT_COMPRESS, true);
209        aws->insert_option("no", "n", 0);
210        aws->insert_option("vertical gaps", "v", 1);
211        aws->insert_option("all gaps", "a", 2);
212        aws->update_option_menu();
213
214        aws->at("seqfilter");
215        adfiltercbstruct *filtercd = awt_create_select_filter(awr, gb_main, AWAR_EXPORT_FILTER_NAME);
216        aws->callback(makeCreateWindowCallback(awt_create_select_filter_win, filtercd));
217        aws->create_button("SELECT_FILTER", AWAR_EXPORT_FILTER_NAME);
218
219        aws->at("cutstop");
220        aws->create_toggle(AWAR_EXPORT_CUTSTOP);
221
222        aws->at("multiple");
223        aws->create_toggle(AWAR_EXPORT_MULTIPLE_FILES);
224
225        aws->at("desc");
226        aws->create_text_field(AWAR_EXPORT_FORMAT_DESC);
227
228        aws->at("go");
229        aws->highlight();
230        aws->callback(makeWindowCallback(export_go_cb, gb_main, filtercd));
231        aws->create_button("GO", "GO", "G", "+");
232
233        aws->at("test");
234        aws->callback(makeWindowCallback(AWTI_activate_export_test_window, gb_main, filtercd));
235        aws->create_button("TEST", "Test", "T");
236
237        static FileWatch fwatch(AWAR_EXPORT_FORMATNAME, makeFileChangedCallback(update_format_description_and_suffix));
238    }
239
240    return aws;
241}
Note: See TracBrowser for help on using the repository browser.