SearchesPanel.py :  » Development » pyDC » pydc » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » pyDC 
pyDC » pydc » SearchesPanel.py
# This file is part of pyDC software
# Copyright 2002 Anakim Border <aborder@users.sourceforge.net>
#
# pyDC is released under the terms of GPL licence.

from wxPython.wx import *
from Util import *
from ViewerListener import *

from DCSearch import *
from DCSearchWorkerEventListener import *
from DCSearchEventListener import *
import DCWorker

class SearchesPanel(wxPanel, ViewerListener, DCSearchWorkerEventListener, DCSearchEventListener):
  def __init__(self, parent, id):
    wxPanel.__init__(self, parent, id)
    
    self.searches = []
    self.sortAlgos = [ None,
                       (self.getSearchPattern, cmp),
                       (DCSearch.getResultNum, icmp),

                       (DCSearch.getResultNum, cmp),
                       (self.getSearchPattern, icmp) ]
    self.sortAlgo = 1

    self.list = wxListCtrl(self, 1, style = wxLC_REPORT | wxLC_VRULES)
    self.new = wxButton(self, 2, 'New')

    self.list.InsertColumn(0, 'Pattern')
    self.list.InsertColumn(1, 'Hits')
    self.list.Show(1)

    vLayout = wxBoxSizer(wxVERTICAL)

    vLayout.Add(self.list, 1, wxEXPAND)
    vLayout.Add(self.new, 0, wxEXPAND | wxTOP, 5)

    self.SetSizer(vLayout)
    self.Layout()

    EVT_LIST_COL_CLICK(self, 1, self.setSortAlgo)
    EVT_LIST_ITEM_SELECTED(self, 1, self.onSearchActivation)
    EVT_BUTTON(self, 2, self.onSearch)
    self.Connect(1, 1, wxEVT_NULL, self.insertSearch)
    self.Connect(2, 2, wxEVT_NULL, self.updateSearch)
    self.Connect(3, 3, wxEVT_NULL, self.removeSearch)

  def setSortAlgo(self, event):
    col = event.GetColumn() + 1
    if self.sortAlgo == col:
      self.sortAlgo = -col
    else:
      self.sortAlgo = col

    access, comp = self.sortAlgos[self.sortAlgo]
    self.searches = fullSort(self.searches, access, comp)

    self.list.Freeze()
    pos = 0
    for search in self.searches:
      pattern = search.getPattern()
      if pattern == None: pattern = '[New search]'
      self.list.SetStringItem(pos, 0, pattern)
      self.list.SetStringItem(pos, 1, str(search.getResultNum()))
      pos += 1
    self.list.Thaw()

  def onSearch(self, event):
    search = getMainWnd().newSearch()

  def onNewSearch(self, search):
    if search.isPrivate(): return
    wxPostEvent(self, guiEvent(1, search))

  def onSearchActivation(self, event):
    getMainWnd().setActiveJob(self.searches[event.GetIndex()])

  def onSearchStart(self, search):
    wxPostEvent(self, guiEvent(2, search))

  def onNewResult(self, search, result):
    wxPostEvent(self, guiEvent(2, search))

  def onSearchStop(self, search):
    wxPostEvent(self, guiEvent(3, search))

  def onViewerClosed(self, viewer, stoppedJob):
    event = guiEvent(3, viewer.getInfo().getJob())
    self.removeSearch(event)

  #=================#
  # Private section #
  #=================#

  def getSearchPattern(self, search):
    return search.getPattern().lower()

  def insertSearch(self, event):
    search = event.data
    search.registerListener(self)

    try:
      self.searches.index(search)
       return
    except ValueError:
      self.searches.append(search)

    self.sortList()
    pos = self.searches.index(search)

    pattern = search.getPattern()
    if pattern == None: pattern = '[New search]'
    self.list.InsertStringItem(pos, pattern)
    self.list.SetStringItem(pos, 1, str(search.getResultNum()))

  def updateSearch(self, event):
    search = event.data

    oldPos = self.searches.index(search)
    self.sortList()
    pos = self.searches.index(search)

    if oldPos == pos:
      self.list.SetStringItem(pos, 0, search.getPattern())
      self.list.SetStringItem(pos, 1, str(search.getResultNum()))
    else:
      self.list.DeleteItem(oldPos)
      self.list.InsertStringItem(pos, search.getPattern())
      self.list.SetStringItem(pos, 1, str(search.getResultNum()))

  def removeSearch(self, event):
    try:
      pos = self.searches.index(event.data)
      del self.searches[pos]
      self.list.DeleteItem(pos)
    except ValueError:
      return

  def sortList(self):
    if len(self.searches) < 2: return

    if self.sortAlgo == 0:
      return
    elif self.sortAlgo == 1:
      self.searches.sort(lambda x, y: cmp(x.getPattern(), y.getPattern()))
    elif self.sortAlgo == -1:
      self.searches.sort(lambda x, y: cmp(y.getPattern(), x.getPattern()))
    elif self.sortAlgo == 2:
      self.searches.sort(lambda x, y: x.getResultNum() - y.getResultNum())
    elif self.sortAlgo == -2:
      self.searches.sort(lambda x, y: y.getResultNum() - x.getResultNum())
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.