source: branches/properties/EDIT4/ED4_plugins.cxx

Last change on this file was 19407, checked in by westram, 20 months ago
  • reintegrates 'ali' into 'trunk'
    • use bond settings from EDIT4 in SECEDIT
    • fix refresh logic
    • refactor underlying code
  • adds: log:branches/ali@19393:19406
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : ED4_plugins.cxx                                   //
4//   Purpose   : implement plugin connector + wrap plugin calls    //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#include <secedit_extern.hxx>
12#include <rna3d_extern.hxx>
13
14#include <aw_msg.hxx>
15#include <arbdb.h>
16#include <arb_defs.h>
17
18#include "ed4_visualizeSAI.hxx"
19#include "ed4_class.hxx"
20
21#define e4_assert(bed) arb_assert(bed)
22
23static bool has_species_name(ED4_base *base, const char *species_name) {
24    if (base->is_sequence_terminal()) {
25        ED4_sequence_terminal *seq_term = base->to_sequence_terminal();
26        return species_name && seq_term && seq_term->species_name && strcmp(species_name, seq_term->species_name)==0;
27    }
28    return false;
29}
30
31// -----------------
32//      ED4_host
33
34class ED4_host : public ED4_plugin_host, virtual Noncopyable {
35    AW_root *aw_root;
36    GBDATA  *gb_main;
37
38    const ED4_sequence_terminal *seq_term;
39    mutable ED4_base_position    base_pos;
40
41public:
42    ED4_host(AW_root *aw_root_, GBDATA *gb_main_)
43        : aw_root(aw_root_),
44          gb_main(gb_main_),
45          seq_term(NULp)
46    {}
47    ~ED4_host() OVERRIDE {}
48
49    AW_root *get_application_root() const OVERRIDE { return aw_root; }
50    GBDATA *get_database() const OVERRIDE { return gb_main; }
51
52    void announce_current_species(const char *species_name) OVERRIDE {
53        ED4_base *base = ED4_ROOT->main_manager->find_first_that(LEV_SEQUENCE_STRING, makeED4_basePredicate(has_species_name, species_name));
54        seq_term       = base ? base->to_sequence_terminal() : NULp;
55    }
56
57    bool SAIs_visualized() const OVERRIDE { return ED4_ROOT->visualizeSAI; }
58    const char* get_SAI_background(int start, int end) const OVERRIDE {
59        return ED4_getSaiColorString(aw_root, start, end);
60    }
61
62    const char* get_search_background(int start, int end) const OVERRIDE {
63        return seq_term
64            ? seq_term->results().buildColorString(seq_term, start, end)
65            : NULp;
66    }
67
68    int get_base_position(int seq_position) const OVERRIDE {
69        e4_assert(seq_term);
70        return base_pos.get_base_position(seq_term, seq_position);
71    }
72
73    void forward_event(AW_event *event) const OVERRIDE { ED4_remote_event(event); }
74
75    const AW_helix *get_helix() const OVERRIDE {
76        return ED4_ROOT->helix;
77    }
78};
79
80// ---------------
81//      PlugIn
82
83class PlugIn {
84    char              *name;
85    ED4_plugin        *start_plugin;
86    mutable AW_window *window;
87
88public:
89    PlugIn(const char *name_, ED4_plugin *start_plugin_)
90        : name(ARB_strdup(name_)),
91          start_plugin(start_plugin_),
92          window(NULp)
93    {}
94    PlugIn(const PlugIn& other)
95        : name(ARB_strdup(other.name)),
96          start_plugin(other.start_plugin),
97          window(other.window)
98    {}
99    DECLARE_ASSIGNMENT_OPERATOR(PlugIn);
100    ~PlugIn() { free(name); }
101
102    bool has_name(const char *Name) const { return strcmp(Name, name) == 0; }
103
104    GB_ERROR activate(AW_root *aw_root, GBDATA *gb_main) const {
105        GB_ERROR error = NULp;
106        if (!window) {
107            static ED4_plugin_host *host = NULp;
108            if (!host) host = new ED4_host(aw_root, gb_main);
109            window = start_plugin(*host);
110            error = window ? NULp : GB_await_error();
111            if (error) error = GBS_global_string("Couldn't start plugin '%s'\nReason: %s", name, error);
112        }
113        if (!error) window->activate();
114        return error;
115    }
116};
117
118static const PlugIn *findPlugin(const char *name) {
119    static PlugIn registered[] = { // register plugins here
120        PlugIn("SECEDIT", start_SECEDIT_plugin),
121#if defined(ARB_OPENGL)
122        PlugIn("RNA3D", start_RNA3D_plugin),
123#endif // ARB_OPENGL
124    };
125
126    for (size_t plug = 0; plug<ARRAY_ELEMS(registered); ++plug) {
127        if (registered[plug].has_name(name)) {
128            return &registered[plug];
129        }
130    }
131    return NULp;
132}
133
134void ED4_start_plugin(AW_window *aw, GBDATA *gb_main, const char *pluginname) {
135    const PlugIn *plugin = findPlugin(pluginname);
136
137    GB_ERROR error = plugin
138        ? plugin->activate(aw->get_root(), gb_main)
139        : GBS_global_string("plugin '%s' is unknown", pluginname);
140
141    aw_message_if(error);
142}
143
144
145// --------------------------------------------------------------------------------
146
147#ifdef UNIT_TESTS
148#include <test_unit.h>
149
150void TEST_plugin_found() {
151    TEST_REJECT_NULL(findPlugin("SECEDIT"));
152    TEST_EXPECT_NULL(findPlugin("unknown"));
153#if defined(ARB_OPENGL)
154    TEST_REJECT_NULL(findPlugin("RNA3D"));
155#else
156    TEST_EXPECT_NULL(findPlugin("RNA3D"));
157#endif
158}
159
160#endif // UNIT_TESTS
161
Note: See TracBrowser for help on using the repository browser.