NCBI C++ ToolKit
search_form_base.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

00001 /*  $Id: search_form_base.cpp 25811 2012-05-11 18:39:00Z falkrb $
00002  * ===========================================================================
00003  *
00004  *                            PUBLIC DOMAIN NOTICE
00005  *               National Center for Biotechnology Information
00006  *
00007  *  This software/database is a "United States Government Work" under the
00008  *  terms of the United States Copyright Act.  It was written as part of
00009  *  the author's official duties as a United States Government employee and
00010  *  thus cannot be copyrighted.  This software/database is freely available
00011  *  to the public for use. The National Library of Medicine and the U.S.
00012  *  Government have not placed any restriction on its use or reproduction.
00013  *
00014  *  Although all reasonable efforts have been taken to ensure the accuracy
00015  *  and reliability of the software and data, the NLM and the U.S.
00016  *  Government do not and cannot warrant the performance or results that
00017  *  may be obtained by using this software or data. The NLM and the U.S.
00018  *  Government disclaim all warranties, express or implied, including
00019  *  warranties of performance, merchantability or fitness for any particular
00020  *  purpose.
00021  *
00022  *  Please cite the author in any work or product based on this material.
00023  *
00024  * ===========================================================================
00025  *
00026  * Authors:  Andrey Yazhuk
00027  *
00028  * File Description:
00029  *
00030  */
00031 
00032 #include <ncbi_pch.hpp>
00033 
00034 #include <gui/core/search_form_base.hpp>
00035 #include <gui/core/data_mining_service.hpp>
00036 #include <gui/core/seqloc_search_context.hpp>
00037 
00038 #include <gui/widgets/wx/message_box.hpp>
00039 #include <gui/widgets/wx/wx_utils.hpp>
00040 
00041 #include <gui/widgets/seq/feature_check_dialog.hpp>
00042 
00043 #include <objects/seqfeat/Seq_feat.hpp>
00044 #include <objects/seqfeat/SeqFeatData.hpp>
00045 
00046 #include <wx/sizer.h>
00047 #include <wx/srchctrl.h>
00048 #include <wx/choice.h>
00049 
00050 BEGIN_NCBI_SCOPE
00051 USING_SCOPE(objects);
00052 
00053 ///////////////////////////////////////////////////////////////////////////////
00054 /// CSearchFormBase
00055 
00056 #define DICTIONARY_MAX_SIZE 50
00057 
00058 CSearchControl::CSearchControl (wxWindow* parent, wxWindowID id, const wxString& value,
00059                                 const wxPoint& pos, const wxSize& size,
00060                                 long style, const wxValidator& validator,
00061                                 const wxString& name)
00062     : CAutoCompleteCombo(parent, id, value, pos, size, wxArrayString(), style, validator, name)
00063     , m_MaxSize(DICTIONARY_MAX_SIZE)
00064     , m_bDoSelectAll(false)
00065     , m_bIgnoreNextTextEdit(false)
00066     , m_pDict(NULL)
00067 {
00068 }
00069 
00070 CSearchControl::CSearchControl(wxWindow *parent, wxWindowID id,
00071                                const wxPoint& pos,
00072                                const wxSize& size,
00073                                long style,
00074                                const wxValidator& validator,
00075                                const wxString& name)
00076     : CAutoCompleteCombo(parent, id, wxT(""), pos, size, wxArrayString(), style, validator, name)
00077     , m_MaxSize(DICTIONARY_MAX_SIZE)
00078     , m_bDoSelectAll(false)
00079     , m_bIgnoreNextTextEdit(false)
00080     , m_pDict(NULL)
00081 {
00082 }
00083 
00084 class StringCmp
00085 {
00086 public:
00087     string m_Str;
00088 
00089     StringCmp(string str) {m_Str = str;}
00090 
00091     bool operator()(string str)
00092     {
00093         return str.find(m_Str) == 0;
00094     }
00095 };
00096 
00097 void CSearchControl::SetDictionary(list<string> * dict)
00098 {
00099     m_pDict = dict;
00100     
00101     wxArrayString items;
00102     ITERATE (list<string>, str, *m_pDict) {
00103         items.Add(ToWxString(*str));
00104     }    
00105     SetBaseItems(items);
00106 }
00107 
00108 void CSearchControl::Push()
00109 {
00110     if (m_pDict) {
00111         while (m_pDict->size() >= m_MaxSize) {
00112             m_pDict->pop_back();
00113             if (GetCount()) Delete(0);
00114         }
00115         string value = ToStdString(GetValue());
00116         if (value.size() && (find(m_pDict->begin(), m_pDict->end(), value)==m_pDict->end())) {
00117             m_pDict->push_front(value);
00118             Append(GetValue());
00119         }
00120         //m_pDict->unique();        
00121     } 
00122 }
00123 
00124 ///////////////////////////////////////////////////////////////////////////////
00125 /// CSearchFormBase
00126 CSearchFormBase::CSearchFormBase()
00127 :   m_ActiveContext(NULL),
00128     m_Controller(NULL),
00129     m_Sizer(NULL),
00130     m_Text(NULL),
00131     m_bRange(false)
00132 {
00133 
00134     const CFeatList* feat_list = CSeqFeatData::GetFeatList();
00135     ITERATE(CFeatList, ft_it, *feat_list) {
00136         const CFeatListItem& item = *ft_it;
00137         string desc = item.GetDescription();
00138         int feat_type = item.GetType();
00139 
00140         if (feat_type == CSeqFeatData::e_Rna  ||
00141             feat_type == CSeqFeatData::e_Gene ||
00142             feat_type == CSeqFeatData::e_Cdregion) {
00143             m_FeatTypesSet.insert(item);
00144         }
00145     }
00146 }
00147 
00148 
00149 CSearchFormBase::~CSearchFormBase()
00150 {
00151 }
00152 
00153 
00154 void CSearchFormBase::SetController(IDMSearchFormController* controller)
00155 {
00156     m_Controller = controller;
00157 }
00158 
00159 void CSearchFormBase::Push()
00160 {
00161     if (m_Text) {
00162         m_Text->Push();
00163     }
00164 }
00165 
00166 void CSearchFormBase::SetRangeLimit(bool bLimit)
00167 {
00168     m_bRange = bLimit;
00169 }
00170 
00171 
00172 wxSizer* CSearchFormBase::GetWidget(wxWindow * parent)
00173 {
00174     return NULL;
00175 }
00176 
00177 
00178 CIRef<IDMSearchQuery> CSearchFormBase::ConstructQuery()
00179 {
00180     CIRef<IDMSearchQuery> ref(new CSearchQueryNull());
00181     return ref;
00182 }
00183 
00184 
00185 /// updates m_ContextCombo
00186 void CSearchFormBase::UpdateContexts()
00187 {
00188     m_ActiveContext = 0;
00189 
00190     m_Contexts.clear();
00191 
00192     CDataMiningService* srv = m_Controller->GetDataMiningService();
00193     if(srv) {
00194         /// create a list of compatible contexts
00195         vector<string> context_names;
00196 
00197         /// Add "Active" Context
00198         IDataMiningContext* act_context = srv->GetLastActiveContext();
00199         bool ok = false;
00200 
00201         if (act_context) {
00202             ok = x_GetTool()->IsCompatible(act_context);
00203             if (ok) {
00204                 m_ActiveContext = act_context;
00205             }
00206         }
00207 
00208         /// now add all the compatible contexts
00209         vector<IDataMiningContext*> all_contexts;
00210         srv->GetContexts(all_contexts);
00211 
00212         for( size_t i = 0;  i < all_contexts.size();  i++ )   {
00213             IDataMiningContext* ctx = all_contexts[i];
00214             ok = x_GetTool()->IsCompatible(ctx);
00215             if(ok)  {
00216                 m_Contexts.push_back(ctx);
00217                 //string name = ctx->GetDMContextName();
00218                 //context_names.push_back(name);
00219             }
00220         }
00221 
00222         /// initilize the ComboBox with the list of context names
00223         // DDX_ComboBox(m_ContextCombo, context_names, eDDX_Set);
00224     }
00225 
00226     bool bEnable = false;
00227 
00228     ITERATE(TContexts, cont, m_Contexts) {
00229         ISeqLocSearchContext* seq_ctx =
00230             dynamic_cast<ISeqLocSearchContext*>(*cont);
00231         if (seq_ctx && !seq_ctx->GetSearchLoc().Empty()){
00232             bEnable=true;
00233             break;
00234         }
00235     }
00236     if (m_Controller) {
00237         m_Controller->OnSearchEnabled(bEnable);
00238     }
00239 }
00240 
00241 void CSearchFormBase::UpdateContextCombo(wxChoice* combo)
00242 {
00243     combo->Clear();
00244 
00245     combo->Append(wxT("All Searchable Contexts (") +
00246                       ToWxString(NStr::SizetToString(m_Contexts.size())) +
00247                       wxT(" available)"));       
00248     combo->Select(0);
00249 
00250     for( size_t i = 0;  i < m_Contexts.size();  i++ ) {
00251         if (m_Contexts[i]) {
00252             string cname = m_Contexts[i]->GetDMContextName();
00253             if (cname.length() > 40) {
00254                 cname = cname.substr(0, 40) + "...";
00255             }
00256             combo->Append(ToWxString(cname));
00257 
00258             if (m_ActiveContext && (m_ActiveContext->GetDMContextName() == m_Contexts[i]->GetDMContextName())) {
00259                 combo->Select((int)(i+1));
00260             }
00261         }
00262     }
00263 }
00264 
00265 /// TODO this function should be here - it should be move the the form
00266 /// that needs this dialog
00267 
00268 /// popup a dialog for additional info
00269 void CSearchFormBase::PopupADialog(TDialogType dt)
00270 {
00271     switch (dt) {
00272     case IDMSearchForm::FEATURE_SEL_DIALOG:
00273         CFeatureCheckDialog dlg(NULL);
00274         dlg.SetRegistryPath(m_RegPath);
00275         dlg.SetSelected(m_FeatTypesSet); //default values
00276         dlg.ShowModal();
00277         dlg.GetSelected(m_FeatTypesSet);
00278         break;
00279     }
00280 }
00281 
00282 
00283 void CSearchFormBase::SetDictionary(list<string> * ddd)
00284 {
00285     if (m_Text) {
00286         m_Text->SetDictionary(ddd);
00287     }
00288 }
00289 
00290 
00291 void CSearchFormBase::SetRegistryPath(const string& reg_path)
00292 {
00293     m_RegPath = reg_path;
00294 }
00295 
00296 
00297 void CSearchFormBase::LoadSettings()
00298 {
00299     if( ! m_RegPath.empty() ){
00300         CGuiRegistry &          gui_reg = CGuiRegistry::GetInstance();
00301         CGuiRegistry::TReadView view    = gui_reg.GetReadView( m_RegPath );
00302 
00303         x_LoadSettings(view);
00304     }
00305 }
00306 
00307 
00308 void CSearchFormBase::SaveSettings() const
00309 {
00310     if( ! m_RegPath.empty() ){
00311         CGuiRegistry& gui_reg = CGuiRegistry::GetInstance();
00312         CGuiRegistry::TReadWriteView view = gui_reg.GetReadWriteView(m_RegPath);
00313 
00314         x_SaveSettings(view);
00315     }
00316 }
00317 
00318 
00319 string  CSearchFormBase::GetMainValue()
00320 {
00321     return m_Text ? ToStdString(m_Text->GetValue()) : string("#empty#");
00322 }
00323 
00324 void    CSearchFormBase::SetMainValue(string val)
00325 {
00326     if (m_Text) {
00327         wxString wVal = ToWxString(val);
00328         if (wVal != m_Text->GetValue())
00329             m_Text->SetValue(wVal);
00330     }
00331 }
00332 
00333 
00334 END_NCBI_SCOPE
Modified on Sat Aug 30 16:48:17 2014 by modify_doxy.py rev. 426318