~ubuntu-branches/debian/wheezy/agtl/wheezy

« back to all changes in this revision

Viewing changes to files/advancedcaching/qt/searchgeocachesdialog.py

  • Committer: Bazaar Package Importer
  • Author(s): Heiko Stuebner
  • Date: 2011-01-22 13:55:12 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20110122135512-1mik0vssgpnx2fgu
Tags: 0.8.0.3-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
#   Copyright (C) 2010 Daniel Fett
 
5
#   This program is free software: you can redistribute it and/or modify
 
6
#   it under the terms of the GNU General Public License as published by
 
7
#   the Free Software Foundation, either version 3 of the License, or
 
8
#   (at your option) any later version.
 
9
#
 
10
#   This program is distributed in the hope that it will be useful,
 
11
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
#   GNU General Public License for more details.
 
14
#
 
15
#   You should have received a copy of the GNU General Public License
 
16
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
#
 
18
#   Author: Daniel Fett agtl@danielfett.de
 
19
#   Jabber: fett.daniel@jaber.ccc.de
 
20
#   Bugtracker and GIT Repository: http://github.com/webhamster/advancedcaching
 
21
#
 
22
 
 
23
import logging
 
24
 
 
25
from PyQt4.QtCore import *
 
26
from PyQt4.QtGui import *
 
27
import geocaching
 
28
from searchresultsdialog import QtSearchResultsDialog
 
29
from ui_searchgeocachesdialog import Ui_SearchGeocachesDialog
 
30
d = lambda x: x.decode('utf-8')
 
31
 
 
32
logger = logging.getLogger('qtsearchgeocachesdialog')
 
33
 
 
34
class QtSearchGeocachesDialog(Ui_SearchGeocachesDialog, QDialog):
 
35
 
 
36
    RADI = [1, 2, 3, 5, 10, 15, 20, 50, 100]
 
37
 
 
38
    TYPELIST = [
 
39
        ('traditional', geocaching.GeocacheCoordinate.TYPE_REGULAR),
 
40
        ('multi stage', geocaching.GeocacheCoordinate.TYPE_MULTI),
 
41
        ('virtual', geocaching.GeocacheCoordinate.TYPE_VIRTUAL),
 
42
        ('earth', geocaching.GeocacheCoordinate.TYPE_EARTH),
 
43
        ('event', geocaching.GeocacheCoordinate.TYPE_EVENT),
 
44
        ('mystery', geocaching.GeocacheCoordinate.TYPE_MYSTERY),
 
45
        ('webcam', geocaching.GeocacheCoordinate.TYPE_WEBCAM),
 
46
        ('all/other', geocaching.GeocacheCoordinate.TYPE_UNKNOWN)
 
47
    ]
 
48
 
 
49
    def __init__(self, core, map_position, user_position, parent=None):
 
50
        QDialog.__init__(self, parent)
 
51
        self.core = core
 
52
        self.setupUi(self)
 
53
        self.populateUi()
 
54
        self.setModal(True)
 
55
        self.dialogButtonBox.clicked.connect(self.__button_clicked)
 
56
        self.map_position = map_position
 
57
        self.user_position = user_position
 
58
 
 
59
 
 
60
    def populateUi(self):
 
61
        for name, type in self.TYPELIST:
 
62
            m = QListWidgetItem(name, self.listWidgetType)
 
63
            m.setCheckState(Qt.Unchecked if type == geocaching.GeocacheCoordinate.TYPE_UNKNOWN else Qt.Checked)
 
64
        self.comboBoxLocation.currentIndexChanged.connect(self.__combo_box_changed)
 
65
 
 
66
    def __combo_box_changed(self, index):
 
67
        self.spinBoxRadius.setEnabled(index != 0)
 
68
        if index == 1:
 
69
            text = self.map_position.get_latlon() if self.map_position != None else 'not available'
 
70
        elif index == 2:
 
71
            text = self.user_position.get_latlon() if self.user_position != None else 'not available'
 
72
        else:
 
73
            text = ''
 
74
        self.labelLocation.setText(d(text))
 
75
 
 
76
 
 
77
    def show(self):
 
78
        QDialog.show(self)
 
79
 
 
80
    def __button_clicked(self, button):
 
81
        id = self.dialogButtonBox.standardButton(button)
 
82
        if id == QDialogButtonBox.Ok:
 
83
            self.__start_search()
 
84
 
 
85
    def __start_search(self):
 
86
 
 
87
        # Name
 
88
 
 
89
        name = str(self.lineEditName.text()).strip().lower()
 
90
        if name == '':
 
91
            name = None
 
92
 
 
93
        logger.debug("Name: %s" % name)
 
94
 
 
95
        # Location & Radius
 
96
 
 
97
        i = self.comboBoxLocation.currentIndex()
 
98
        if i == 1 and self.map_position != None:
 
99
            center = self.map_position
 
100
        elif i == 2 and self.user_position != None:
 
101
            center = self.user_position
 
102
        else:
 
103
            center = None
 
104
 
 
105
        if center != None:
 
106
            radius = self.spinBoxRadius.value()
 
107
            sqrt_2 = 1.41421356
 
108
            c1 = center.transform(-45, radius * 1000 * sqrt_2)
 
109
            c2 = center.transform(-45 + 180, radius * 1000 * sqrt_2)
 
110
            location = (c2, c1)
 
111
            logger.debug("Location: %s %s" % location)
 
112
        else:
 
113
            location = None
 
114
            logger.debug("Location: None")
 
115
 
 
116
 
 
117
 
 
118
        # Details (1)
 
119
 
 
120
        types = [self.TYPELIST[x][1] for x in range(self.listWidgetType.count()) if self.listWidgetType.item(x).checkState() == Qt.Checked]
 
121
        if geocaching.GeocacheCoordinate.TYPE_UNKNOWN in types:
 
122
            types = None
 
123
        logger.debug("Types: %s" % types)
 
124
 
 
125
        if self.checkBoxHideFound.checkState() == Qt.Checked:
 
126
            found = False
 
127
        else:
 
128
            found = None
 
129
        logger.debug("Found: %s" % found)
 
130
 
 
131
        if self.checkBoxShowOnlyMarked.checkState() == Qt.Checked:
 
132
            marked = True
 
133
        else:
 
134
            marked = False
 
135
        logger.debug("Marked: %s" % marked)
 
136
 
 
137
        # Details (2)
 
138
        sizes = [x + 1 for x in range(self.listWidgetSize.count()) if self.listWidgetSize.item(x).checkState() == Qt.Checked]
 
139
        if sizes == [1, 2, 3, 4, 5]:
 
140
            sizes = None
 
141
        logger.debug("Sizes: %s" % sizes)
 
142
 
 
143
        r = lambda x: int(x / 0.5) * 0.5
 
144
 
 
145
        diff_min = r(self.doubleSpinBoxDifficultyMin.value())
 
146
        diff_max = r(self.doubleSpinBoxDifficultyMax.value() + 0.5)
 
147
        if diff_min == 1 and diff_max == 5.5:
 
148
            difficulties = None
 
149
        else:
 
150
            # range doesn't support floats!
 
151
            difficulties = [x / 10.0 for x in range(int(diff_min * 10), int(diff_max * 10), 5)]
 
152
        logger.debug("Difficulties: %s" % difficulties)
 
153
 
 
154
        terr_min = r(self.doubleSpinBoxTerrainMin.value())
 
155
        terr_max = r(self.doubleSpinBoxTerrainMax.value() + 0.5)
 
156
        if terr_min == 1 and terr_max == 5.5:
 
157
            terrains = None
 
158
        else:
 
159
            # range doesn't support floats!
 
160
            terrains = [x / 10.0 for x in range(int(terr_min * 10), int(terr_max * 10), 5)]
 
161
        logger.debug("Terrains: %s" % terrains)
 
162
 
 
163
        results = self.core.get_points_filter(found=found, name_search=name, size=sizes, terrain=terrains, diff=difficulties, ctype=types, marked=marked, location=location)
 
164
        logger.debug("Found %d results" % len(results[0]))
 
165
 
 
166
        self.__show_results(results)
 
167
 
 
168
    def __show_results(self, results):
 
169
        d = QtSearchResultsDialog(self.core)
 
170
        d.show(results[0])