File: loctree.cpp

package info (click to toggle)
trustedqsl 2.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 19,964 kB
  • ctags: 3,229
  • sloc: cpp: 20,654; xml: 4,289; ansic: 624; sh: 57; python: 18; makefile: 11
file content (134 lines) | stat: -rw-r--r-- 3,944 bytes parent folder | download
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
/***************************************************************************
                          loctree.cpp  -  description
                             -------------------
    begin                : Sun Jun 23 2002
    copyright            : (C) 2002 by ARRL
    author               : Jon Bloom
    email                : jbloom@arrl.org
    revision             : $Id$
 ***************************************************************************/

#include "loctree.h"
#include <errno.h>
#include <wx/imaglist.h>
#include <map>
#include <vector>
#include <algorithm>
#include <iostream>
#include <utility>

#include "tqslctrls.h"
#include "tqslerrno.h"
#include "tqsltrace.h"

#include "util.h"

#include "folder.xpm"
#include "home.xpm"

using std::pair;
using std::vector;
using std::map;
using std::make_pair;

enum {
	FOLDER_ICON = 0,
	HOME_ICON = 1
};

///////////// Location Tree Control ////////////////


BEGIN_EVENT_TABLE(LocTree, wxTreeCtrl)
	EVT_TREE_ITEM_ACTIVATED(-1, LocTree::OnItemActivated)
	EVT_RIGHT_DOWN(LocTree::OnRightDown)
END_EVENT_TABLE()

LocTree::LocTree(wxWindow *parent, const wxWindowID id, const wxPoint& pos,
		const wxSize& size, long style)
		: wxTreeCtrl(parent, id, pos, size, style), _nloc(0) {
	tqslTrace("LocTree::LocTree", "parent=0x%lx, id=0x%lx, style=%d", reinterpret_cast<void *>(parent), reinterpret_cast<void *>(id), style);
	useContextMenu = true;
	wxBitmap homebm(home_xpm);
	wxBitmap folderbm(folder_xpm);
	wxImageList *il = new wxImageList(16, 16, false, 2);
	il->Add(folderbm);
	il->Add(homebm);
	SetImageList(il);
}


LocTree::~LocTree() {
}

typedef pair<wxString, int> locitem;
typedef vector<locitem> loclist;

static bool
cl_cmp(const locitem& i1, const locitem& i2) {
	return i1.first < i2.first;
}
static void
check_tqsl_error(int rval) {
	if (rval == 0)
		return;
	wxLogError(wxT("%hs"), tqsl_getErrorString());
}

int
LocTree::Build(int flags, const TQSL_PROVIDER *provider) {
	tqslTrace("LocTree::Build", "provider=0x%lx", reinterpret_cast<void *>(const_cast<TQSL_PROVIDER *>(provider)));
	typedef map<wxString, loclist> locmap;
	locmap callsigns;

	DeleteAllItems();
	wxTreeItemId rootId = AddRoot(wxT("Station Locations"), FOLDER_ICON);
        tQSL_Location loc;
        check_tqsl_error(tqsl_initStationLocationCapture(&loc));
        check_tqsl_error(tqsl_getNumStationLocations(loc, &_nloc));
        for (int i = 0; i < _nloc && i < 2000; i++) {
                char locname[256];
                check_tqsl_error(tqsl_getStationLocationName(loc, i, locname, sizeof locname));
                char callsign[256];
                check_tqsl_error(tqsl_getStationLocationCallSign(loc, i, callsign, sizeof callsign));
		wxString locutf8 = wxString::FromUTF8(locname);
		callsigns[wxString::FromUTF8(callsign)].push_back(make_pair(locutf8, i));
        }
	// Sort each callsign list and add items to tree
	locmap::iterator loc_it;
	for (loc_it = callsigns.begin(); loc_it != callsigns.end(); loc_it++) {
		wxTreeItemId id = AppendItem(rootId, loc_it->first, FOLDER_ICON);
		loclist& list = loc_it->second;
		sort(list.begin(), list.end(), cl_cmp);
		for (int i = 0; i < static_cast<int>(list.size()); i++) {
			LocTreeItemData *loc = new LocTreeItemData(list[i].first, loc_it->first);
			AppendItem(id, list[i].first, HOME_ICON, -1, loc);
		}
		Expand(id);
	}
	Expand(rootId);
	return _nloc;
}

void
LocTree::OnItemActivated(wxTreeEvent& event) {
	tqslTrace("LocTree::OnItemActivated");
	wxTreeItemId id = event.GetItem();
	displayLocProperties(reinterpret_cast<LocTreeItemData *>(GetItemData(id)), this);
}

void
LocTree::OnRightDown(wxMouseEvent& event) {
	tqslTrace("LocTree::OnRightDown");
	if (!useContextMenu)
		return;
	wxTreeItemId id = HitTest(event.GetPosition());
	if (id && GetItemData(id)) {
		SelectItem(id);
		wxString locname = GetItemData(id)->getLocname();
		wxMenu *cm = makeLocationMenu(true);
		PopupMenu(cm, event.GetPosition());
		delete cm;
	}
}