plugin_utils.cpp

来自「ncbi源码」· C++ 代码 · 共 1,523 行 · 第 1/4 页

CPP
1,523
字号
/* * =========================================================================== * PRODUCTION $Log: plugin_utils.cpp,v $ * PRODUCTION Revision 1000.5  2004/06/01 20:44:35  gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.69 * PRODUCTION * =========================================================================== *//*  $Id: plugin_utils.cpp,v 1000.5 2004/06/01 20:44:35 gouriano Exp $ * =========================================================================== * *                            PUBLIC DOMAIN NOTICE *               National Center for Biotechnology Information * *  This software/database is a "United States Government Work" under the *  terms of the United States Copyright Act.  It was written as part of *  the author's official duties as a United States Government employee and *  thus cannot be copyrighted.  This software/database is freely available *  to the public for use. The National Library of Medicine and the U.S. *  Government have not placed any restriction on its use or reproduction. * *  Although all reasonable efforts have been taken to ensure the accuracy *  and reliability of the software and data, the NLM and the U.S. *  Government do not and cannot warrant the performance or results that *  may be obtained by using this software or data. The NLM and the U.S. *  Government disclaim all warranties, express or implied, including *  warranties of performance, merchantability or fitness for any particular *  purpose. * *  Please cite the author in any work or product based on this material. * * =========================================================================== * * Authors:  Mike DiCuccio * * File Description: *    CPluginUtils -- utilities for managing the interface between the plugin *                    architecture and the GBENCH framework */#include <ncbi_pch.hpp>#include <gui/core/idocument.hpp>#include <gui/core/obj_convert.hpp>#include <gui/plugin/PluginValue.hpp>#include <gui/plugin/PluginValueConstraint.hpp>#include <gui/plugin/PluginValueRangeConstraint.hpp>#include <gui/plugin/PluginReply.hpp>#include <gui/plugin/PluginMessage.hpp>#include <gui/core/plugin_registry.hpp>#include <gui/core/plugin_utils.hpp>#include <gui/core/message_queue.hpp>#include <gui/core/doc_manager.hpp>#include <gui/objutils/label.hpp>#include <gui/utils/message_box.hpp>#include <gui/core/plugin_arg_dialog.hpp>#include <serial/iterator.hpp>#include <objects/seq/Bioseq.hpp>#include <objects/seq/Seq_annot.hpp>#include <objects/seqloc/Seq_loc.hpp>#include <objects/seqloc/Seq_point.hpp>#include <objects/seqset/Seq_entry.hpp>#include <objects/seqset/Seq_entry.hpp>#include <objects/seqalign/Seq_align.hpp>#include <objmgr/seq_vector.hpp>#include <objmgr/util/sequence.hpp>#include <objmgr/util/feature.hpp>#include <objmgr/annot_selector.hpp>#include <objmgr/annot_ci.hpp>#include <objmgr/align_ci.hpp>#include <algorithm>BEGIN_NCBI_SCOPEUSING_SCOPE(objects);// simple functor to return the best argument in a list of arguments// 'best' is determined by a simple user-supplied rankstruct SChoiceRank{    const vector<int>& m_Ranks;    SChoiceRank(const vector<int>& ranks)        : m_Ranks(ranks) {}    int operator() (CConstRef<CPluginArg> arg) const    {        if ( !arg ) {            return m_Ranks.size();        }        return m_Ranks[ arg->GetType() ];    }};//// call a plugin by name, passing in a selection buffer//template <class Command>void s_CallPlugin(const string& plugin_name, Command cmd,                  const CSelectionBuffer& selections,                  IReporter* reporter,                  const string& ctx_name,                  CPluginUtils::EDispatchWhen when){    // prepare a plugin message to handle our command    CRef<objects::CPluginMessage> msg(new objects::CPluginMessage());    msg->SetDestination(plugin_name);    if ( !ctx_name.empty() ) {        msg->SetContext(ctx_name);    }    msg->SetReporter(reporter);    objects::CPluginRequest& request = msg->SetRequest();    objects::CPluginCommand& args    = request.SetCommand(cmd);    // don't forget to fill in our default arguments    CPluginHandle handle =        CPluginRegistry::GetPlugin(plugin_name);    if ( !handle ) {        LOG_POST(Error << "CallPlugin(): failed to find plugin named "                 << plugin_name);        return;    }    handle.FillDefaults(cmd, args);    // now, pass off to internal processing    CPluginUtils::CallPlugin(*msg, selections.DecomposeToPairs(), when);}//// call a plugin by name, passing in a document//template <class Command>void s_CallPlugin(const string& plugin_name, Command cmd,                  const IDocument* doc,                  IReporter* reporter,                  const string& ctx_name,                  CPluginUtils::EDispatchWhen when){    CRef<CSelectionBuffer> buffer(new CSelectionBuffer());    if (doc) {        buffer->AddSelection(const_cast<IDocument*>(doc));    }    // pass off to a more intelligent CallPlugin() command    CPluginUtils::CallPlugin(plugin_name, cmd, *buffer,                             reporter, ctx_name, when);}void CPluginUtils::CallPlugin(const string& name, EAlgoCommand cmd,                              const CSelectionBuffer& buffer,                              IReporter* reporter,                              const string& ctx_name,                              EDispatchWhen when){    s_CallPlugin(name, cmd, buffer, reporter, ctx_name, when);}void CPluginUtils::CallPlugin(const string& name, EDataCommand cmd,                              const CSelectionBuffer& buffer,                              IReporter* reporter,                              const string& ctx_name,                              EDispatchWhen when){    s_CallPlugin(name, cmd, buffer, reporter, ctx_name, when);}void CPluginUtils::CallPlugin(const string& name, EViewCommand cmd,                              const CSelectionBuffer& buffer,                              IReporter* reporter,                              const string& ctx_name,                              EDispatchWhen when){    s_CallPlugin(name, cmd, buffer, reporter, ctx_name, when);}void CPluginUtils::CallPlugin(const string& name, EAlgoCommand cmd,                              const IDocument* doc,                              IReporter* reporter,                              const string& ctx_name,                              EDispatchWhen when){    s_CallPlugin(name, cmd, doc, reporter, ctx_name, when);}void CPluginUtils::CallPlugin(const string& name, EDataCommand cmd,                              const IDocument* doc,                              IReporter* reporter,                              const string& ctx_name,                              EDispatchWhen when){    s_CallPlugin(name, cmd, doc, reporter, ctx_name, when);}void CPluginUtils::CallPlugin(const string& name, EViewCommand cmd,                              const IDocument* doc,                              IReporter* reporter,                              const string& ctx_name,                              EDispatchWhen when){    s_CallPlugin(name, cmd, doc, reporter, ctx_name, when);}//// internal handler for plugin requests that use the CPluginMessage interface//void CPluginUtils::CallPlugin(CPluginMessage& msg,                              EDispatchWhen when){    switch (when) {    case eDispatch_Deferred:        // now, place the request message in the message queue.        CPluginMessageQueue::Add(msg);        break;    case eDispatch_Now:        CPluginMessageQueue::ProcessMessage(msg);        break;    }}//// internal handler for all plugin requests//void CPluginUtils::CallPlugin(CPluginMessage& msg,                              const TConstScopedObjects& selections,                              EDispatchWhen when){    try {        // first, fill our default values        string          plugin_name = msg.GetDestination();        CPluginHandle   handle      = CPluginRegistry::GetPlugin(plugin_name);        CPluginRequest& request     = msg.SetRequest();        CPluginCommand& cmd         = request.SetCommand();        CPluginArgSet&  args        = cmd.SetArgs();        // give the plugin the opportunity to modify it aguments        handle.FinalizeArgs(msg);        // now, try and add any object arguments to our list        if ( FillArgs(args, selections)  ||  !CPluginUtils::IsValid(args) ) {            // we have remaining arguments that need to be addressed            // pop up a form dialog to handle these            CPluginArgDialog dlg(handle, args, selections);            if (dlg.ShowModal() == eCancel) {                msg.SetReply().SetStatus(eMessageStatus_ignored);                return;            }        }        // now, place the request message in the message queue.        CallPlugin(msg, when);    }    catch (CException& e) {        NcbiMessageBox("Failed to execute plugin:\n" + e.GetMsg());    }    catch (exception& e) {        NcbiMessageBox(string("Failed to execute plugin:\n") + e.what());    }#ifndef _DEBUG    catch (...) {        NcbiMessageBox("Failed to execute plugin:\nUnknown error");    }#endif}//// verify that a set of arguments is valid//bool CPluginUtils::IsValid(const CPluginArgSet& arg){    ITERATE(CPluginArgSet::Tdata, iter, arg.Get()) {        if ( !IsValid(**iter) ) {            return false;        }    }    return true;}//// verify that a given argument's values are consistent with the argument's// specifications//bool CPluginUtils::IsValid(const CPluginArg& arg){    if (arg.IsSetHidden()  && arg.GetHidden()) {        // hidden arguments are ignored for the sake of validity        return true;    }    CTypeConstIterator<CPluginValue> iter(arg);    if ( !iter ) {        return false;    }    CPluginValue::E_Choice type = iter->Which();    string subtype = iter->GetObjectSubtype();    for ( ;  iter;  ++iter) {        if (iter->Which() != type  ||            iter->GetObjectSubtype() != subtype) {            return false;        }        if ( !IsValid(*iter) ) {            return false;        }    }    return CheckConstraints(arg);}//// verify that a single plugin value is valid//bool CPluginUtils::IsValid(const CPluginValue& val){    switch (val.Which()) {    default:    case CPluginValue::e_not_set:        return false;    case CPluginValue::e_Integer:    case CPluginValue::e_String:    case CPluginValue::e_Double:    case CPluginValue::e_Boolean:    case CPluginValue::e_File:        return !val.IsEmpty();    case CPluginValue::e_Document:        {{            const IDocument* doc = val.GetDocument();            const CObject*   obj = val.GetObject();            if ( !doc  ||  !obj ) {                return false;            }            doc = dynamic_cast<const IDocument*>(obj);            if ( !doc ) {                return false;            }            return true;        }}        break;    case CPluginValue::e_Object:        {{            const IDocument* doc = val.GetDocument();            const CObject*   obj = val.GetObject();            if ( !doc  ||  !obj ) {                return false;            }            if (val.GetObjectSubtype() == "Document") {                doc = dynamic_cast<const IDocument*>(obj);                if ( !doc ) {                    return false;                }                return true;            } else if (val.GetObjectSubtype() == "Object") {                return true;            } else {                const CSerialObject* o =

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?