NCBI C++ Toolkit Cross Reference

  C++/src/gui/widgets/edit/structuredcomment_panel.cpp


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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
/* $Id: structuredcomment_panel.cpp 34924 2016-03-02 18:38:14Z filippov $ * =========================================================================== * * 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: Colleen Bollin */ #include <ncbi_pch.hpp> #include <gui/widgets/wx/wx_utils.hpp> #include <objects/general/User_field.hpp> #include <objects/general/Object_id.hpp> #include <util/util_misc.hpp> #include <objects/valid/Comment_rule.hpp> #include <objects/valid/Field_set.hpp> #include <serial/objistr.hpp> ////@begin includes ////@end includes #include <wx/panel.h> #include <wx/sizer.h> #include <wx/bitmap.h> #include <wx/icon.h> #include <wx/choice.h> #include <wx/textctrl.h> #include <wx/stattext.h> #include <wx/valtext.h> #include <wx/toplevel.h> #include <wx/scrolwin.h> #include <wx/hyperlink.h> #include <wx/checkbox.h> #include "structuredcomment_panel.hpp" ////@begin XPM images ////@end XPM images BEGIN_NCBI_SCOPE USING_SCOPE(objects); /*! * CStructuredCommentPanel type definition */ IMPLEMENT_DYNAMIC_CLASS( CStructuredCommentPanel, wxPanel ) /*! * CStructuredCommentPanel event table definition */ BEGIN_EVENT_TABLE( CStructuredCommentPanel, wxPanel ) ////@begin CStructuredCommentPanel event table entries EVT_CHOICE( ID_CHOICE11, CStructuredCommentPanel::OnChoice11Selected ) EVT_HYPERLINK( ID_HYPERLINKCTRL, CStructuredCommentPanel::OnHyperlinkctrlHyperlinkClicked ) ////@end CStructuredCommentPanel event table entries EVT_HYPERLINK(wxID_ANY, CStructuredCommentPanel::OnDelete) END_EVENT_TABLE() /*! * CStructuredCommentPanel constructors */ CStructuredCommentPanel::CStructuredCommentPanel() { Init(); } CStructuredCommentPanel::CStructuredCommentPanel( wxWindow* parent, CRef<CUser_object> user, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style ) { Init(); m_User = new objects::CUser_object(); if (user) { m_User->Assign(*user); } Create(parent, id, caption, pos, size, style); } /*! * CStructuredCommentPanel creator */ bool CStructuredCommentPanel::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style ) { ////@begin CStructuredCommentPanel creation SetExtraStyle(wxWS_EX_BLOCK_EVENTS); wxPanel::Create( parent, id, pos, size, style ); CreateControls(); if (GetSizer()) { GetSizer()->SetSizeHints(this); } Centre(); ////@end CStructuredCommentPanel creation return true; } /*! * CStructuredCommentPanel destructor */ CStructuredCommentPanel::~CStructuredCommentPanel() { ////@begin CStructuredCommentPanel destruction ////@end CStructuredCommentPanel destruction } /*! * Member initialisation */ void CStructuredCommentPanel::Init() { ////@begin CStructuredCommentPanel member initialisation m_StandardCommentSizer = NULL; m_StandardCommentTypeLabel = NULL; m_StandardCommentCtrl = NULL; m_ScrolledWindow = NULL; m_Sizer = NULL; m_ReplaceAll = NULL; ////@end CStructuredCommentPanel member initialisation } static void s_RemovePoundSigns (string& str) { while (NStr::StartsWith (str, "#")) { str = str.substr(1); } while (NStr::EndsWith (str, "#")) { str = str.substr(0, str.length() - 1); } } static void s_AddPoundSigns (string& str) { if (NStr::StartsWith(str, "##")) { // done } else if (NStr::StartsWith (str, "#")) { // add one more str = "#" + str; } else { // add two str = "##" + str; } if (NStr::EndsWith(str, "##")) { // done } else if (NStr::EndsWith (str, "#")) { // add one more str = str + "#"; } else { // add two str = str + "##"; } } /*! * Control creation for CStructuredCommentPanel */ void CStructuredCommentPanel::CreateControls() { ////@begin CStructuredCommentPanel content construction CStructuredCommentPanel* itemPanel1 = this; wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL); itemPanel1->SetSizer(itemBoxSizer2); m_StandardCommentSizer = new wxBoxSizer(wxHORIZONTAL); itemBoxSizer2->Add(m_StandardCommentSizer, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5); m_StandardCommentTypeLabel = new wxStaticText( itemPanel1, wxID_STATIC, _("Standard Comment Type"), wxDefaultPosition, wxDefaultSize, 0 ); m_StandardCommentSizer->Add(m_StandardCommentTypeLabel, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5); wxArrayString m_StandardCommentCtrlStrings; m_StandardCommentCtrl = new wxChoice( itemPanel1, ID_CHOICE11, wxDefaultPosition, wxDefaultSize, m_StandardCommentCtrlStrings, 0 ); m_StandardCommentSizer->Add(m_StandardCommentCtrl, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5); wxBoxSizer* itemBoxSizer6 = new wxBoxSizer(wxHORIZONTAL); itemBoxSizer2->Add(itemBoxSizer6, 0, wxALIGN_LEFT|wxALL, 5); wxStaticText* itemStaticText7 = new wxStaticText( itemPanel1, wxID_STATIC, _("Field Name"), wxDefaultPosition, wxSize(200, -1), 0 ); itemBoxSizer6->Add(itemStaticText7, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5); wxStaticText* itemStaticText8 = new wxStaticText( itemPanel1, wxID_STATIC, _("Value"), wxDefaultPosition, wxSize(200, -1), 0 ); itemBoxSizer6->Add(itemStaticText8, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5); wxHyperlinkCtrl* itemHyperlinkCtrl9 = new wxHyperlinkCtrl( itemPanel1, ID_HYPERLINKCTRL, _("Add Field"), wxEmptyString, wxDefaultPosition, wxSize(50, -1), wxHL_DEFAULT_STYLE ); itemBoxSizer6->Add(itemHyperlinkCtrl9, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5); m_ScrolledWindow = new wxScrolledWindow( itemPanel1, ID_SCROLLEDWINDOW, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER|wxTAB_TRAVERSAL ); itemBoxSizer2->Add(m_ScrolledWindow, 1, wxGROW|wxALL, 5); m_ScrolledWindow->SetScrollbars(1, 1, 0, 0); m_Sizer = new wxBoxSizer(wxVERTICAL); m_ScrolledWindow->SetSizer(m_Sizer); m_ScrolledWindow->SetMinSize(wxSize(-1, 250)); m_ReplaceAll = new wxCheckBox(itemPanel1, ID_CHECK_REPLACE_ALL, _("Replace All"), wxDefaultPosition, wxDefaultSize, 0); m_ReplaceAll->SetValue(true); itemBoxSizer2->Add(m_ReplaceAll, 0, wxALIGN_RIGHT | wxALL, 5); ////@end CStructuredCommentPanel content construction GetStructuredCommentRules(); if (m_StructuredCommentRules) { // add wxchoice with available comment rules m_StandardCommentCtrlStrings.Add(wxEmptyString); ITERATE (CComment_set::Tdata, it, m_StructuredCommentRules->Get()) { const CComment_rule& rule = **it; string prefix = rule.GetPrefix(); s_RemovePoundSigns(prefix); m_StandardCommentCtrlStrings.Add(ToWxString(prefix)); } m_StandardCommentCtrl->Append(m_StandardCommentCtrlStrings); } else { } } void CStructuredCommentPanel::s_AddRow (string field_name, string field_value, bool static_fieldname) { wxBoxSizer* row_sizer = new wxBoxSizer(wxHORIZONTAL); m_Sizer->Add (row_sizer, 0, wxALIGN_NOT|wxALL); if (static_fieldname) { wxStaticText* fieldNameText = new wxStaticText( m_ScrolledWindow, 0, ToWxString(field_name), wxDefaultPosition, wxSize(200, -1), 0 ); row_sizer->Add(fieldNameText, 0, wxALIGN_NOT|wxALL); m_FieldNameCtrls.push_back(fieldNameText); } else { wxTextCtrl* fieldNameText = new wxTextCtrl( m_ScrolledWindow, 0, ToWxString(field_name), wxDefaultPosition, wxSize(200, -1), 0 ); row_sizer->Add(fieldNameText, 0, wxALIGN_NOT|wxALL); m_FieldNameCtrls.push_back(fieldNameText); } wxTextCtrl* fieldValueText = new wxTextCtrl( m_ScrolledWindow, 0, ToWxString(field_value), wxDefaultPosition, wxSize(200, -1), 0 ); row_sizer->Add(fieldValueText, 0, wxALIGN_NOT|wxALL); m_FieldValueCtrls.push_back(fieldValueText); if (!static_fieldname) { wxHyperlinkCtrl* itemHyperLink = new wxHyperlinkCtrl(m_ScrolledWindow, wxID_ANY, wxT("Delete"), wxT("")); itemHyperLink->SetVisitedColour(itemHyperLink->GetNormalColour()); row_sizer->Add(itemHyperLink, 0, wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT, 5); } int row_width; int row_height; fieldValueText->GetClientSize(&row_width, &row_height); m_TotalHeight += row_height; m_ScrollRate = row_height; } bool CStructuredCommentPanel::TransferDataToWindow() { if (!wxPanel::TransferDataToWindow()) return false; Freeze(); m_Sizer->Clear(true); m_TotalHeight = 0; m_ScrollRate = 0; m_FieldNameCtrls.clear(); m_FieldValueCtrls.clear(); // first, find prefix and add it, save suffix for later string prefix = ""; string suffix = ""; vector<string> field_names; vector<string> field_values; vector<bool> displayed; ITERATE (CUser_object::TData, it, m_User->GetData()) { string field_name = ""; const CObject_id& obj_id = (*it)->GetLabel(); if ( obj_id.IsStr() ) { field_name = obj_id.GetStr(); } if (NStr::EqualNocase (field_name, "StructuredCommentPrefix")) { if ((*it)->IsSetData() && (*it)->GetData().IsStr()) { prefix = (*it)->GetData().GetStr(); s_RemovePoundSigns(prefix); } } else if (NStr::EqualNocase(field_name, "StructuredCommentSuffix")) { if ((*it)->IsSetData() && (*it)->GetData().IsStr()) { suffix = (*it)->GetData().GetStr(); s_RemovePoundSigns(suffix); } } else { field_names.push_back(field_name); string value = ""; if ((*it)->IsSetData()) { if ((*it)->GetData().IsStr()) { value = (*it)->GetData().GetStr(); } else if ((*it)->GetData().IsInt()) { value = NStr::IntToString ((*it)->GetData().GetInt()); } } field_values.push_back(value); displayed.push_back(false); } } // if this is a standard comment, set the control, and list the expected fields first bool use_default_layout = true; if (m_StructuredCommentRules) { try { const CComment_rule& rule = m_StructuredCommentRules->FindCommentRule("##" + prefix + "##"); m_StandardCommentCtrl->SetStringSelection(ToWxString (prefix)); ITERATE (CComment_rule::TFields::Tdata, field_it, rule.GetFields().Get()) { // find existing value if we have one // note - display multiple values here if present bool found = false; for (int i = 0; i < field_names.size(); i++) { if (NStr::EqualNocase(field_names[i], (*field_it)->GetField_name())) { s_AddRow((*field_it)->GetField_name(), field_values[i], true); displayed[i] = true; found = true; } } if (!found) { s_AddRow((*field_it)->GetField_name(), "", true); } } use_default_layout = false; } catch (CException ) { } } if (use_default_layout) { s_AddRow("StructuredCommentPrefix", prefix, true); } for (int i = 0; i < field_names.size(); i++) { if (!displayed[i]) { s_AddRow (field_names[i], field_values[i]); } } if (use_default_layout) { s_AddRow("StructuredCommentSuffix", suffix, true); } m_ScrolledWindow->SetVirtualSize(400 + 10, m_TotalHeight); m_ScrolledWindow->SetScrollRate(0, m_ScrollRate); m_ScrolledWindow->FitInside(); Layout(); Thaw(); return true; } bool CStructuredCommentPanel::x_GetFields (bool keep_blanks) { if (!wxPanel::TransferDataFromWindow()) return false; m_User->ResetData(); string prefix = ""; if (m_StandardCommentCtrl) { prefix = ToStdString (m_StandardCommentCtrl->GetStringSelection()); if (!NStr::IsBlank(prefix)) { // structured comment prefix s_AddPoundSigns(prefix); m_User->AddField ("StructuredCommentPrefix", prefix); } } for (int i = 0; i < m_FieldValueCtrls.size(); i++) { string field_name = ""; string field_value = ""; /* first is either label or text */ wxTextCtrl* fieldNameText = dynamic_cast<wxTextCtrl*> (m_FieldNameCtrls[i]); if (fieldNameText) { field_name = ToStdString(fieldNameText->GetValue()); } else { wxStaticText* staticFieldNameText = dynamic_cast<wxStaticText*> (m_FieldNameCtrls[i]); if (staticFieldNameText) { field_name = ToStdString (staticFieldNameText->GetLabel()); } } /* second is value - could be wxText or ?*/ field_value = ToStdString(m_FieldValueCtrls[i]->GetValue()); if (keep_blanks || (!NStr::IsBlank(field_name) && !NStr::IsBlank(field_value))) { if (NStr::EqualNocase(field_name, "StructuredCommentPrefix") || NStr::EqualNocase(field_name, "StructuredCommentSuffix")) { if (NStr::IsBlank (prefix)) { s_AddPoundSigns (field_value); m_User->AddField (field_name, field_value); } } else { m_User->AddField (field_name, field_value); } } } if (!NStr::IsBlank(prefix)) { NStr::ReplaceInPlace (prefix, "START##", "END##"); m_User->AddField ("StructuredCommentSuffix", prefix); } return true; } bool CStructuredCommentPanel::TransferDataFromWindow() { return x_GetFields (false); } CRef<objects::CUser_object> CStructuredCommentPanel::GetUser_object() const { if (m_User) return m_User; return CRef<objects::CUser_object>(); } static bool s_FieldRuleCompare ( const CRef<CField_rule>& p1, const CRef<CField_rule>& p2 ) { return NStr::Compare(p1->GetField_name(), p2->GetField_name()) < 0; } CRef<CComment_set> CStructuredCommentPanel::GetStructuredCommentRules() { if (m_StructuredCommentRules) { return m_StructuredCommentRules; } string fname = g_FindDataFile("validrules.prt"); if (fname.empty()) { // do nothing } else { auto_ptr<CObjectIStream> in; in.reset(CObjectIStream::Open(fname, eSerial_AsnText)); string header = in->ReadFileHeader(); m_StructuredCommentRules.Reset(new CComment_set()); in->Read(ObjectInfo(*m_StructuredCommentRules), CObjectIStream::eNoFileHeader); if (m_StructuredCommentRules->IsSet()) { NON_CONST_ITERATE(CComment_set::Tdata, it, m_StructuredCommentRules->Set()) { if (!(*it)->GetRequire_order() && (*it)->IsSetFields()) { CField_set& fields = (*it)->SetFields(); fields.Set().sort(s_FieldRuleCompare); } } } } return m_StructuredCommentRules; } /*! * Should we show tooltips? */ bool CStructuredCommentPanel::ShowToolTips() { return true; } /*! * Get bitmap resources */ wxBitmap CStructuredCommentPanel::GetBitmapResource( const wxString& name ) { // Bitmap retrieval ////@begin CStructuredCommentPanel bitmap retrieval wxUnusedVar(name); return wxNullBitmap; ////@end CStructuredCommentPanel bitmap retrieval } /*! * Get icon resources */ wxIcon CStructuredCommentPanel::GetIconResource( const wxString& name ) { // Icon retrieval ////@begin CStructuredCommentPanel icon retrieval wxUnusedVar(name); return wxNullIcon; ////@end CStructuredCommentPanel icon retrieval } /*! * wxEVT_COMMAND_HYPERLINK event handler for ID_HYPERLINKCTRL */ void CStructuredCommentPanel::OnHyperlinkctrlHyperlinkClicked( wxHyperlinkEvent& event ) { x_GetFields(true); m_User->AddField("", ""); TransferDataToWindow(); } int CStructuredCommentPanel::x_FindRow(wxWindow* wnd, wxSizerItemList& itemList) { if (NULL == wnd) return -1; int row_num = 0; if (!NStr::IsBlank(ToStdString(m_StandardCommentCtrl->GetStringSelection()))) { // add one because prefix field will be first in user object but will not // be represented by a sizer row_num ++; } for(wxSizerItemList::iterator it = itemList.begin(); it != itemList.end(); ++it, ++row_num) { wxSizer* sizer = (**it).GetSizer(); if (sizer && sizer->GetItem(wnd)) return row_num; } return -1; } void CStructuredCommentPanel::OnDelete (wxHyperlinkEvent& event) { wxSizerItemList& itemList = m_Sizer->GetChildren(); int row_num = x_FindRow((wxWindow*)event.GetEventObject(), itemList); if (row_num > -1) { x_GetFields(true); int row = 0; NON_CONST_ITERATE (CUser_object::TData, it, m_User->SetData()) { if (row == row_num) { it = m_User->SetData().erase(it); break; } row++; } TransferDataToWindow(); } } /*! * wxEVT_COMMAND_CHOICE_SELECTED event handler for ID_CHOICE11 */ void CStructuredCommentPanel::OnChoice11Selected( wxCommandEvent& event ) { x_GetFields(true); TransferDataToWindow(); } void CStructuredCommentPanel::ChangeSeqdesc(const objects::CSeqdesc& desc) { m_User.Reset(new CUser_object); m_User->Assign(desc.GetUser()); TransferDataToWindow(); } void CStructuredCommentPanel::UpdateSeqdesc(objects::CSeqdesc& desc) { CRef<CUser_object> user = GetUser_object(); desc.SetUser(*user); } bool CStructuredCommentPanel::IsImportEnabled() { return true; } bool CStructuredCommentPanel::IsExportEnabled() { return true; } CRef<CSerialObject> CStructuredCommentPanel::OnExport() { TransferDataFromWindow(); CRef<CSerialObject> so((CSerialObject*)(new objects::CUser_object)); so->Assign(*GetUser_object()); return so; } void CStructuredCommentPanel::OnImport( CNcbiIfstream &istr) { m_User.Reset(new CUser_object); istr >> MSerial_AsnText >> *m_User; TransferDataToWindow(); } END_NCBI_SCOPE

source navigation ]   [ diff markup ]   [ identifier search ]   [ freetext search ]   [ file search ]  

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.