Main Page | Class List | File List | Class Members | Related Pages

dlgfind.cpp

00001 /***************************************************************************
00002                            dlgfind.cpp
00003                            -------------------
00004     begin                : 2004-06-07 21:46
00005     copyright            : (C) 2004-2005 by Michael Menne
00006     email                : menne@users.sourceforge.net
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  This program is free software; you can redistribute it and/or
00011  modify it under the terms of the GNU General Public License
00012  as published by the Free Software Foundation; either version 2
00013  of the License, or (at your option) any later version.
00014 
00015  This program is distributed in the hope that it will be useful,
00016  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  GNU General Public License for more details.
00019 
00020  You should have received a copy of the GNU General Public License
00021  along with this program; if not, write to the Free Software
00022  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023  ***************************************************************************/
00024 
00025 #include "dlgfind.h"
00026 
00027 #include "app.h"
00028 #include "frame.h"
00029 #include "glcanvas.h"
00030 #include <algorithm>
00031 #include <cctype>
00032 
00033 namespace WDS
00034 {
00035 
00036 DECLARE_APP(CApp)
00037 
00038 BEGIN_EVENT_TABLE(CDlgFind, wxDialog)
00039     EVT_TEXT( IDC_EDT_NAME,  CDlgFind::OnEditUpdate )
00040     EVT_TEXT( IDC_EDT_GILDE, CDlgFind::OnEditUpdate )
00041     EVT_TEXT( IDC_EDT_STADT, CDlgFind::OnEditUpdate )
00042     EVT_BUTTON( IDC_BTN_FIND,  CDlgFind::OnBtnFind  )
00043     EVT_BUTTON( IDC_BTN_CLEAR, CDlgFind::OnBtnClear )
00044     EVT_LIST_ITEM_ACTIVATED(IDC_LST_RESULTS, CDlgFind::OnActivateItem )
00045 END_EVENT_TABLE()
00046 
00047 /*----------------------------------------------------------------------------/
00048     Constructor / Destructor
00049 -----------------------------------------------------------------------------*/
00050 
00051 CDlgFind::CDlgFind( wxWindow *pParent )
00052     : wxDialog( pParent, -1, wxString("Suchen... - WDSMap"), wxDefaultPosition,
00053                 wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
00054 {
00055     // Steuerelement erstellen
00056     m_pTxtName  = new wxStaticText( this, -1, "Spieler" );
00057     m_pTxtGilde = new wxStaticText( this, -1, "Gilde" );
00058     m_pTxtStadt = new wxStaticText( this, -1, "Stadt" );
00059 
00060     m_pEdtName  = new wxTextCtrl( this, IDC_EDT_NAME,  "" );
00061     m_pEdtGilde = new wxTextCtrl( this, IDC_EDT_GILDE, "" );
00062     m_pEdtStadt = new wxTextCtrl( this, IDC_EDT_STADT, "" );
00063 
00064     m_pBtnFind  = new wxButton( this, IDC_BTN_FIND, "Suche starten" );
00065     m_pBtnClear = new wxButton( this, IDC_BTN_CLEAR, "Neue Suche" );
00066 
00067     m_pLine     = new wxStaticLine( this, -1, wxDefaultPosition );
00068 
00069     m_pLstResults = new wxListCtrl( this, IDC_LST_RESULTS, wxDefaultPosition, wxDefaultSize,
00070                                     wxLC_REPORT | wxLC_VRULES | wxLC_SINGLE_SEL );
00071 
00072     // HACK: Versteckter Cancel Button, damit kann der Dialog mit Escape beendet werden
00073     (new wxButton( this, wxID_CANCEL, "Abbrechen" ))->Show( false );
00074 
00075     // Header zur Liste hinzufuegen
00076     m_pLstResults->InsertColumn( 0, "Spieler", wxLIST_FORMAT_LEFT, 100 );
00077     m_pLstResults->InsertColumn( 1, "Gilde", wxLIST_FORMAT_LEFT, 90 );
00078     m_pLstResults->InsertColumn( 2, "Stadt", wxLIST_FORMAT_LEFT, 100 );
00079     m_pLstResults->InsertColumn( 3, "Position", wxLIST_FORMAT_LEFT, 80 );
00080 
00081     // Layout festlegen
00082     wxBoxSizer      *sVert = new wxBoxSizer( wxVERTICAL );
00083     wxFlexGridSizer *sGrid = new wxFlexGridSizer( 3, 3, 5, 5 );
00084     sGrid->AddGrowableCol(1);
00085 
00086     sVert->Add( sGrid, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, 10 );
00087     sVert->Add( m_pLine, 0, wxEXPAND | wxALL, 5 );
00088     sVert->Add( m_pLstResults, 1, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 10 );
00089 
00090     // sGrid has 5 pixed space in row and col, so no need to add those here
00091     sGrid->Add( m_pTxtName,  0, wxALIGN_CENTER_VERTICAL, 0 );
00092     sGrid->Add( m_pEdtName,  1, wxEXPAND, 0 );
00093     sGrid->Add( m_pBtnFind,  0, wxEXPAND, 0 );
00094 
00095     sGrid->Add( m_pTxtGilde, 0, wxALIGN_CENTER_VERTICAL, 0 );
00096     sGrid->Add( m_pEdtGilde, 1, wxEXPAND, 0 );
00097     sGrid->Add( m_pBtnClear, 0, wxEXPAND, 0 );
00098 
00099     sGrid->Add( m_pTxtStadt, 0, wxALIGN_CENTER_VERTICAL, 0 );
00100     sGrid->Add( m_pEdtStadt, 1, wxEXPAND, 0 );
00101 
00102     SetAutoLayout( true );
00103     SetSizer( sVert );
00104 
00105     sVert->SetSizeHints( this );
00106     // sane size
00107     SetClientSize( 400, 300 );
00108 
00109     m_pEdtName->SetFocus( );
00110     m_pBtnFind->SetDefault( );
00111 
00112     // Voreinstellungen
00113     m_pBtnFind->Disable();
00114     m_pBtnClear->Disable();
00115 }
00116 
00117 CDlgFind::~CDlgFind()
00118 {
00119 }
00120 
00121 /*----------------------------------------------------------------------------/
00122     Member Functions
00123 -----------------------------------------------------------------------------*/
00124 
00125 void CDlgFind::OnEditUpdate( wxEvent &event )
00126 {
00127     if( m_pEdtName->GetValue().IsEmpty() &&
00128         m_pEdtGilde->GetValue().IsEmpty() &&
00129         m_pEdtStadt->GetValue().IsEmpty() )
00130     {
00131         m_pBtnFind->Disable();
00132         m_pBtnClear->Disable();
00133     }
00134     else
00135     {
00136         m_pBtnFind->Enable();
00137         m_pBtnClear->Enable();
00138     }
00139 }
00140 
00141 /*
00142  *  OnBtnFind
00143  *  Durchsucht alle Staedte und fuegt die hinzu bei denen die gegebenen Sucheintraege
00144  *  mit den Stadteintraegen ( Name, Gilde, Spieler ) bereinstimmen.
00145  *  Die Uebereinstimmung ist case-insensitive und kann nur Teilweise stimmen
00146  *  z.B. Wenn man nach 'Di' sucht, wird 'Isdir' gefunden, da 'Isdir' das 'di' enthaelt
00147  */
00148 void CDlgFind::OnBtnFind( wxEvent &event )
00149 {
00150     wxBusyCursor wait;
00151 
00152     // Alle Felder als lowercase auslesen
00153     wxString strPlayer = m_pEdtName->GetValue().MakeLower();
00154     wxString strGuild  = m_pEdtGilde->GetValue().MakeLower();
00155     wxString strTown   = m_pEdtStadt->GetValue().MakeLower();
00156 
00157     // Zeiger auf Karte holen
00158     CMap *pMap = GetMap();
00159     if( pMap == NULL )
00160         return;     // Keine Karte geladen
00161 
00162     // Towns array von der Map bekommen
00163     CTownArray& towns = pMap->GetTownArray();
00164 
00165     // Testen Städte geladen sind
00166     if ( towns.size() == 0)
00167         return;
00168 
00169     // Alte Selektionen loeschen
00170     m_aSelected.clear();
00171 
00172     CTown* pTown = towns.firstSibling();
00173     while ( pTown != 0L )
00174     {
00175         bool bAdd = true;
00176 
00177         // Wir gehen davon aus, das die Stadt hinzugefgt wird, deshalb bAdd = true
00178         // In den nachsten Bloecken versuchen wir dies zu wiederlegen, indem wir
00179         // testen ob die Eintraege der Stadt mit den Sucheintraeen bereinstimmen
00180 
00181         if( !strPlayer.empty() )
00182         {
00183             // Spielername auslesen und zu lowercase transformieren
00184             wxString str = pTown->GetOwner().MakeLower();
00185 
00186             if(  str.find( strPlayer ) == wxString::npos )      // Spielername stimmt nicht ueberein -> Nicht hinzufuegen
00187                 bAdd = false;
00188         }
00189 
00190         if( bAdd && !strGuild.empty() )
00191         {
00192             // Gildenname auslesen und zu lowercase transformieren
00193             wxString str = pTown->GetGuild().MakeLower();
00194 
00195             if( str.find( strGuild ) == wxString::npos )    // Gildenname stimmt nicht ueberein -> Nicht hinzufgen
00196                 bAdd = false;
00197         }
00198 
00199         if( bAdd && !strTown.empty() )
00200         {
00201             // Stadtname auslesen und zu lowercase transformieren
00202             wxString str = pTown->GetName().MakeLower();
00203 
00204             if( str.find( strTown ) == wxString::npos )     // Spielername stimmt nicht ueberein -> Nicht hinzufgen
00205                 bAdd = false;
00206         }
00207 
00208         // Nun muessen wir sehen, ob der String immernoch hinzugefgt werden muss
00209         if( bAdd == true )
00210         {
00211             m_aSelected.push_back( *pTown );
00212         }
00213 
00214         pTown = towns.nextSibling();
00215     }
00216 
00217     FillListBox();
00218 }
00219 
00220 void CDlgFind::OnBtnClear( wxEvent &event )
00221 {
00222     // Alle Textfelder und die Liste loeschen
00223     m_pEdtName->Clear();
00224     m_pEdtGilde->Clear();
00225     m_pEdtStadt->Clear();
00226 
00227     m_aSelected.clear();
00228     m_pLstResults->DeleteAllItems();
00229 
00230     m_pEdtName->SetFocus(); // Focus auf das "Namen" Textfeld
00231 }
00232 
00233 void CDlgFind::FillListBox()
00234 {
00235     wxListItem item;
00236 
00237     // Alle momentanen Eintraege loeschen
00238     m_pLstResults->DeleteAllItems();
00239 
00240     // Die neuen Eintraege aus dem m_aSelected Array hinzfuegen
00241     int nSize = m_aSelected.size();
00242     for(int i=0; i<nSize; i++)
00243     {
00244         CTown *pTown = &m_aSelected[i];
00245 
00246         m_pLstResults->InsertItem(i, pTown->GetOwner().c_str() );
00247         m_pLstResults->SetItemData( i, i );
00248         m_pLstResults->SetItem(i, 1, pTown->GetGuild().c_str() );
00249         m_pLstResults->SetItem(i, 2, pTown->GetName().c_str()  );
00250         m_pLstResults->SetItem(i, 3, wxString::Format( "%d:%d", pTown->GetXPos(), pTown->GetYPos() ));
00251 
00252         // Hintergrundfarbe abwechselnd auf ein ungesaettigtes Tuerkies und Gelb setzen
00253         if( i & 1 )
00254             item.SetBackgroundColour( wxColor( 238, 246, 255) );    // Hellblau
00255         else
00256             item.SetBackgroundColour( wxColor( 255, 255, 255) );    // Weiss
00257 
00258         item.m_itemId = i;
00259         m_pLstResults->SetItem( item );
00260     }
00261 
00262     // Wenn nichts gefunden wurd, Nachricht in ListCtrl ausgeben
00263     if( nSize == 0 )
00264     {
00265         m_pLstResults->InsertItem(0, "Suche erfolglos..." );
00266         m_pLstResults->SetItemData( 0, -1 );
00267     }
00268 
00269     //m_pLstResults->SortItems( CbSort, 0);
00270 }
00271 
00272 /*
00273  *  OnActivateItem
00274  *  Wird aufgerufen, wenn ein Eintrag in der Liste aktiviert wird ( Doppelclick )
00275  *  Scrollt zur Koordinate die zum aktivierten Eintrag gehoert
00276  */
00277 void CDlgFind::OnActivateItem( wxListEvent& event )
00278 {
00279     int nIndex = event.GetData();
00280     if( nIndex < 0 || nIndex >= int(m_aSelected.size()) )   // Invalid Index
00281         return;
00282 
00283     CTown *pTown = &m_aSelected[nIndex];
00284     int x = pTown->GetXPos();
00285     int y = pTown->GetYPos();
00286 
00287     CMap *pMap = GetMap( );
00288     CGLCanvas *pCanvas = GetCanvas( );
00289 
00290     // Prüfen ob Karte existiert
00291     if( pMap )
00292     {
00293         wxPoint pt = pMap->GetPosFromField( x, y );
00294         pCanvas->ScrollTo( pt.x, pt.y );
00295     }
00296 }
00297 
00298 /*----------------------------------------------------------------------------/
00299     Hilfsfunktionen
00300 -----------------------------------------------------------------------------*/
00301 
00302 //int wxCALLBACK CDlgFind::CbSort( long item1, long item2, long sortData )
00303 //{
00304 //  CTown *pTown1 = (CTown*) item1;
00305 //  CTown *pTown2 = (CTown*) item2;
00306 //
00307 //  return pTown1->GetName().compare( pTown2->GetName() );
00308 //}
00309 
00310 CMap* CDlgFind::GetMap()
00311 {
00312     CMap *pMap = wxGetApp().GetFrame()->GetMap();
00313     return pMap;
00314 }
00315 
00316 CGLCanvas* CDlgFind::GetCanvas()
00317 {
00318     CGLCanvas *pCanvas = wxGetApp().GetFrame()->GetGLCanvas();
00319     return pCanvas;
00320 }
00321 
00322 }
00323 // kate: space-indent off; tab-width 4; replace-tabs off;
00324 

Generated on Sun Jan 16 18:20:26 2005 for WDSMap by  doxygen 1.3.9.1