Last active
September 17, 2019 23:02
-
-
Save jason-s/6c9495e29a4caac7ddf5cd739550a310 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# based on | |
# https://qt.gitorious.org/pyside/pyside-examples/source/060dca8e4b82f301dfb33a7182767eaf8ad3d024:examples/itemviews/simpletreemodel/simpletreemodel.py | |
############################################################################ | |
## | |
## Copyright (C) 2005-2005 Trolltech AS. All rights reserved. | |
## | |
## This file is part of the example classes of the Qt Toolkit. | |
## | |
## This file may be used under the terms of the GNU General Public | |
## License version 2.0 as published by the Free Software Foundation | |
## and appearing in the file LICENSE.GPL included in the packaging of | |
## this file. Please review the following information to ensure GNU | |
## General Public Licensing requirements will be met: | |
## http://www.trolltech.com/products/qt/opensource.html | |
## | |
## If you are unsure which license is appropriate for your use, please | |
## review the following information: | |
## http://www.trolltech.com/products/qt/licensing.html or contact the | |
## sales department at [email protected]. | |
## | |
## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | |
## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
## | |
############################################################################ | |
from PySide import QtCore, QtGui | |
class TreeItem(object): | |
def __init__(self, data, parent=None): | |
self.parentItem = parent | |
self.itemData = data | |
self.childItems = [] | |
def appendChild(self, item): | |
self.childItems.append(item) | |
def child(self, row): | |
return self.childItems[row] | |
def childCount(self): | |
return len(self.childItems) | |
def columnCount(self): | |
return len(self.itemData) | |
def data(self, column): | |
try: | |
result = self.itemData[column] | |
if column == 0: | |
print result | |
return result | |
except IndexError: | |
return None | |
def parent(self): | |
return self.parentItem | |
def row(self): | |
if self.parentItem: | |
return self.parentItem.childItems.index(self) | |
return 0 | |
def __repr__(self): | |
return 'TreeItem(%s)' % self.itemData | |
class TreeModel(QtCore.QAbstractItemModel): | |
def __init__(self, data, parent=None, extra_count=0): | |
super(TreeModel, self).__init__(parent) | |
self.rootItem = TreeItem(("Region", "Abbreviation")) | |
self.setupModelData(data.split('\n'), self.rootItem, extra_count) | |
def columnCount(self, parent): | |
if parent.isValid(): | |
return parent.internalPointer().columnCount() | |
else: | |
return self.rootItem.columnCount() | |
def data(self, index, role): | |
if not index.isValid(): | |
return None | |
if role == QtCore.Qt.EditRole: | |
return self.rootItem.data(0) | |
if role != QtCore.Qt.DisplayRole: | |
return None | |
item = index.internalPointer() | |
return item.data(index.column()) | |
def flags(self, index): | |
if not index.isValid(): | |
return QtCore.Qt.NoItemFlags | |
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | |
def headerData(self, section, orientation, role): | |
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole: | |
return self.rootItem.data(section) | |
return None | |
def index(self, row, column, parent): | |
if not self.hasIndex(row, column, parent): | |
return QtCore.QModelIndex() | |
if not parent.isValid(): | |
parentItem = self.rootItem | |
else: | |
parentItem = parent.internalPointer() | |
childItem = parentItem.child(row) | |
if childItem: | |
return self.createIndex(row, column, childItem) | |
else: | |
return QtCore.QModelIndex() | |
def parent(self, index): | |
if not index.isValid(): | |
return QtCore.QModelIndex() | |
childItem = index.internalPointer() | |
parentItem = childItem.parent() | |
if parentItem == self.rootItem: | |
return QtCore.QModelIndex() | |
return self.createIndex(parentItem.row(), 0, parentItem) | |
def rowCount(self, parent): | |
if parent.column() > 0: | |
return 0 | |
if not parent.isValid(): | |
parentItem = self.rootItem | |
else: | |
parentItem = parent.internalPointer() | |
return parentItem.childCount() | |
def setupModelData(self, lines, parent, extra_count): | |
parents = [parent] | |
indentations = [0] | |
number = 0 | |
while number < len(lines): | |
position = 0 | |
while position < len(lines[number]): | |
if lines[number][position] != ' ': | |
break | |
position += 1 | |
lineData = lines[number][position:].strip() | |
if lineData: | |
# Read the column data from the rest of the line. | |
columnData = [s for s in lineData.split('\t') if s] | |
if position > indentations[-1]: | |
# The last child of the current parent is now the new | |
# parent unless the current parent has no children. | |
if parents[-1].childCount() > 0: | |
parents.append(parents[-1].child(parents[-1].childCount() - 1)) | |
indentations.append(position) | |
else: | |
while position < indentations[-1] and len(parents) > 0: | |
parents.pop() | |
indentations.pop() | |
# Append a new item to the current parent's list of children. | |
newtreeitem = TreeItem(columnData, parents[-1]) | |
parents[-1].appendChild(newtreeitem) | |
number += 1 | |
for i in xrange(extra_count): | |
country = 'Miragi%03dstan' % i | |
abbrev = 'MS%03d' % i | |
newtreeitem = TreeItem([country,abbrev], parent) | |
parent.appendChild(newtreeitem) | |
class MyCompleter(QtGui.QCompleter): | |
def splitPath(self, path): | |
result = path.split('/') | |
print "splitPath: ", result | |
return result | |
def pathFromIndex(self, index): | |
result = [] | |
while index.isValid(): | |
result = [self.model().data(index, QtCore.Qt.DisplayRole)] + result | |
index = index.parent() | |
r = '/'.join(result) | |
print "pathFromIndex: ", index, r | |
return r | |
if __name__ == '__main__': | |
import sys | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--extra-count','-e',type=int,default=0,help='Extra country count') | |
args = parser.parse_args() | |
app = QtGui.QApplication(sys.argv) | |
model = TreeModel(''' | |
Canada CAN | |
British Columbia BC | |
Vancouver | |
Manitoba MB | |
Winnipeg | |
Ontario ON | |
Toronto | |
Quebec QC | |
Montreal | |
France FRA | |
Normandy NOR | |
Brittany BRE | |
Germany DEU | |
Bavaria BY | |
United States USA | |
Alabama AL | |
Birmingham | |
Huntsville | |
Mobile | |
Montgomery | |
Alaska AK | |
Anchorage | |
Fairbanks | |
Juneau | |
Arizona AZ | |
Chandler | |
Gilbert | |
Glendale | |
Mesa | |
Peoria | |
Phoenix | |
Scottsdale | |
Surprise | |
Tempe | |
Tucson | |
Arkansas AR | |
Fayetteville | |
Fort Smith | |
Little Rock | |
California CA | |
Anaheim | |
Bakersfield | |
Berkeley | |
Carlsbad | |
Chula Vista | |
Concord | |
Corona | |
El Monte | |
Elk Grove | |
Escondido | |
Fairfield | |
Fontana | |
Fremont | |
Fresno | |
Fullerton | |
Garden Grove | |
Glendale | |
Hayward | |
Huntington Beach | |
Irvine | |
Lancaster | |
Long Beach | |
Los Angeles | |
Modesto | |
Moreno Valley | |
Oakland | |
Oceanside | |
Ontario | |
Orange | |
Oxnard | |
Palmdale | |
Pasadena | |
Pomona | |
Rancho Cucamonga | |
Riverside | |
Roseville | |
Sacramento | |
Salinas | |
San Bernardino | |
San Diego | |
San Francisco | |
San Jose | |
Santa Ana | |
Santa Clara | |
Santa Clarita | |
Santa Rosa | |
Simi Valley | |
Stockton | |
Sunnyvale | |
Thousand Oaks | |
Torrance | |
Vallejo | |
Victorville | |
Visalia | |
Colorado CO | |
Arvada | |
Aurora | |
Colorado Springs | |
Denver | |
Fort Collins | |
Lakewood | |
Thornton | |
Connecticut CT | |
Bridgeport | |
Hartford | |
New Haven | |
Stamford | |
Delaware DE | |
Dover | |
Newark | |
Wilmington | |
District of Columbia DC | |
Washington | |
Florida FL | |
Cape Coral | |
Clearwater | |
Coral Springs | |
Fort Lauderdale | |
Gainesville | |
Hialeah | |
Hollywood | |
Jacksonville | |
Miami | |
Miramar | |
Orlando | |
Pembroke Pines | |
Port St. Lucie | |
St. Petersburg | |
Tallahassee | |
Tampa | |
Georgia GA | |
Athens | |
Atlanta | |
Augusta | |
Columbus | |
Macon | |
Savannah | |
Hawaii HI | |
Kahului | |
Hilo | |
Honolulu | |
Idaho ID | |
Boise | |
Coeur D'Alene | |
Pocatello | |
Illinois IL | |
Aurora | |
Chicago | |
Joliet | |
Naperville | |
Rockford | |
Indiana IN | |
Evansville | |
Fort Wayne | |
Indianapolis | |
Terre Haute | |
Iowa IA | |
Cedar Rapids | |
Davenport | |
Des Moines | |
Kansas KS | |
Kansas City | |
Olathe | |
Overland Park | |
Topeka | |
Wichita | |
Kentucky KY | |
Frankfort | |
Lexington | |
Louisville | |
Louisiana LA | |
Baton Rouge | |
Lafayette | |
New Orleans | |
Shreveport | |
Maine ME | |
Augusta | |
Bangor | |
Portland | |
Maryland MD | |
Annapolis | |
Baltimore | |
Frederick | |
Hagerstown | |
Massachusetts MA | |
Amherst | |
Boston | |
Cambridge | |
Pittsfield | |
Springfield | |
Worcester | |
Michigan MI | |
Ann Arbor | |
Detroit | |
Grand Rapids | |
Lansing | |
Sterling Heights | |
Warren | |
Minnesota MN | |
Minneapolis | |
Rochester | |
Saint Paul | |
Mississippi MS | |
Gulfport | |
Jackson | |
Tupelo | |
Vicksburg | |
Missouri MO | |
Columbia | |
Independence | |
Kansas City | |
Springfield | |
St. Louis | |
Montana MT | |
Billings | |
Bozeman | |
Helena | |
Missoula | |
Nebraska NE | |
Grand Island | |
Lincoln | |
Omaha | |
Nevada NV | |
Henderson | |
Las Vegas | |
North Las Vegas | |
Reno | |
New Hampshire NH | |
Concord | |
Manchester | |
Nashua | |
New Jersey NJ | |
Elizabeth | |
Jersey City | |
Newark | |
Paterson | |
New Mexico NM | |
Albuquerque | |
Las Cruces | |
Santa Fe | |
New York NY | |
Buffalo | |
New York | |
Rochester | |
Syracuse | |
Yonkers | |
North Carolina NC | |
Cary | |
Charlotte | |
Durham | |
Fayetteville | |
Greensboro | |
Raleigh | |
Wilmington | |
Winston-Salem | |
North Dakota ND | |
Bismarck | |
Fargo | |
Minot | |
Ohio OH | |
Akron | |
Cincinnati | |
Cleveland | |
Columbus | |
Dayton | |
Toledo | |
Oklahoma OK | |
Norman | |
Oklahoma City | |
Tulsa | |
Oregon OR | |
Eugene | |
Portland | |
Salem | |
Pennsylvania PA | |
Allentown | |
Harrisburg | |
Philadelphia | |
Pittsburgh | |
Rhode Island RI | |
Newport | |
Providence | |
Warwick | |
South Carolina SC | |
Charleston | |
Columbia | |
Myrtle Beach | |
South Dakota SD | |
Rapid City | |
Sioux Falls | |
Vermillion | |
Tennessee TN | |
Chattanooga | |
Clarksville | |
Knoxville | |
Memphis | |
Murfreesboro | |
Nashville | |
Texas TX | |
Abilene | |
Amarillo | |
Arlington | |
Austin | |
Beaumont | |
Brownsville | |
Carrollton | |
College Station | |
Corpus Christi | |
Dallas | |
Denton | |
El Paso | |
Fort Worth | |
Frisco | |
Garland | |
Grand Prairie | |
Houston | |
Irving | |
Killeen | |
Laredo | |
Lubbock | |
McAllen | |
McKinney | |
Mesquite | |
Midland | |
Odessa | |
Pasadena | |
Pearland | |
Plano | |
Richardson | |
Round Rock | |
San Antonio | |
Sugar Land | |
Waco | |
Utah UT | |
Moab | |
Provo | |
Salt Lake City | |
West Jordan | |
West Valley City | |
Vermont VT | |
Burlington | |
Montpelier | |
Waterbury | |
Virginia VA | |
Alexandria | |
Chesapeake | |
Fairfax | |
Hampton | |
Newport News | |
Norfolk | |
Richmond | |
Virginia Beach | |
Washington WA | |
Bellevue | |
Kent | |
Seattle | |
Spokane | |
Tacoma | |
Vancouver | |
West Virginia WV | |
Huntington | |
Morgantown | |
Wheeling | |
Wisconsin WI | |
Green Bay | |
Madison | |
Milwaukee | |
Wyoming WY | |
Casper | |
Cheyenne | |
Jackson | |
Mexico MEX | |
Baja California BC | |
Mexicali | |
Chihuahua CI | |
Ciudad Juárez | |
Coahuila CU | |
Saltillo | |
Durango DG | |
Durango | |
Sonora SO | |
Hermosillo | |
Nogales | |
''', extra_count = args.extra_count) | |
mainwindow = QtGui.QMainWindow() | |
mainwindow.setWindowTitle("Tree Model with Completion") | |
mainwindow.sizeHint = lambda: QtCore.QSize(400,400) | |
mainwindow.show() | |
view = QtGui.QTreeView() | |
view.setModel(model) | |
mainwindow.setCentralWidget(view) | |
edit = QtGui.QLineEdit() | |
completer = MyCompleter(edit) | |
completer.setModel(model) | |
completer.setCompletionColumn(0) | |
completer.setCompletionRole(QtCore.Qt.DisplayRole) | |
completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive) | |
edit.setWindowTitle("PySide QLineEdit Auto Complete") | |
edit.setCompleter(completer) | |
docker = QtGui.QDockWidget() | |
docker.setWidget(edit) | |
mainwindow.addDockWidget(QtCore.Qt.DockWidgetArea.BottomDockWidgetArea, docker) | |
sys.exit(app.exec_()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# based on | |
# https://qt.gitorious.org/pyside/pyside-examples/source/060dca8e4b82f301dfb33a7182767eaf8ad3d024:examples/itemviews/simpletreemodel/simpletreemodel.py | |
############################################################################ | |
## | |
## Copyright (C) 2005-2005 Trolltech AS. All rights reserved. | |
## | |
## This file is part of the example classes of the Qt Toolkit. | |
## | |
## This file may be used under the terms of the GNU General Public | |
## License version 2.0 as published by the Free Software Foundation | |
## and appearing in the file LICENSE.GPL included in the packaging of | |
## this file. Please review the following information to ensure GNU | |
## General Public Licensing requirements will be met: | |
## http://www.trolltech.com/products/qt/opensource.html | |
## | |
## If you are unsure which license is appropriate for your use, please | |
## review the following information: | |
## http://www.trolltech.com/products/qt/licensing.html or contact the | |
## sales department at [email protected]. | |
## | |
## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | |
## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
## | |
############################################################################ | |
from PySide import QtCore, QtGui | |
class TreeItem(object): | |
def __init__(self, data, parent=None): | |
self.parentItem = parent | |
self.itemData = data | |
self.childItems = [] | |
def appendChild(self, item): | |
self.childItems.append(item) | |
def child(self, row): | |
return self.childItems[row] | |
def childCount(self): | |
return len(self.childItems) | |
def columnCount(self): | |
return len(self.itemData) | |
def data(self, column): | |
try: | |
result = self.itemData[column] | |
if column == 0: | |
print result | |
return result | |
except IndexError: | |
return None | |
def parent(self): | |
return self.parentItem | |
def row(self): | |
if self.parentItem: | |
return self.parentItem.childItems.index(self) | |
return 0 | |
def __repr__(self): | |
return 'TreeItem(%s)' % self.itemData | |
class TreeModel(QtCore.QAbstractItemModel): | |
def __init__(self, data, parent=None, extra_count=0): | |
super(TreeModel, self).__init__(parent) | |
self.rootItem = TreeItem(("Region", "Abbreviation")) | |
self.setupModelData(data.split('\n'), self.rootItem, extra_count) | |
def columnCount(self, parent): | |
if parent.isValid(): | |
return parent.internalPointer().columnCount() | |
else: | |
return self.rootItem.columnCount() | |
def data(self, index, role): | |
if not index.isValid(): | |
return None | |
if role == QtCore.Qt.EditRole: | |
return self.rootItem.data(0) | |
if role != QtCore.Qt.DisplayRole: | |
return None | |
item = index.internalPointer() | |
return item.data(index.column()) | |
def flags(self, index): | |
if not index.isValid(): | |
return QtCore.Qt.NoItemFlags | |
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | |
def headerData(self, section, orientation, role): | |
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole: | |
return self.rootItem.data(section) | |
return None | |
def index(self, row, column, parent): | |
if not self.hasIndex(row, column, parent): | |
return QtCore.QModelIndex() | |
if not parent.isValid(): | |
parentItem = self.rootItem | |
else: | |
parentItem = parent.internalPointer() | |
childItem = parentItem.child(row) | |
if childItem: | |
return self.createIndex(row, column, childItem) | |
else: | |
return QtCore.QModelIndex() | |
def parent(self, index): | |
if not index.isValid(): | |
return QtCore.QModelIndex() | |
childItem = index.internalPointer() | |
parentItem = childItem.parent() | |
if parentItem == self.rootItem: | |
return QtCore.QModelIndex() | |
return self.createIndex(parentItem.row(), 0, parentItem) | |
def rowCount(self, parent): | |
if parent.column() > 0: | |
return 0 | |
if not parent.isValid(): | |
parentItem = self.rootItem | |
else: | |
parentItem = parent.internalPointer() | |
return parentItem.childCount() | |
def setupModelData(self, lines, parent, extra_count): | |
parents = [parent] | |
indentations = [0] | |
number = 0 | |
while number < len(lines): | |
position = 0 | |
while position < len(lines[number]): | |
if lines[number][position] != ' ': | |
break | |
position += 1 | |
lineData = lines[number][position:].strip() | |
if lineData: | |
# Read the column data from the rest of the line. | |
columnData = [s for s in lineData.split('\t') if s] | |
if position > indentations[-1]: | |
# The last child of the current parent is now the new | |
# parent unless the current parent has no children. | |
if parents[-1].childCount() > 0: | |
parents.append(parents[-1].child(parents[-1].childCount() - 1)) | |
indentations.append(position) | |
else: | |
while position < indentations[-1] and len(parents) > 0: | |
parents.pop() | |
indentations.pop() | |
# Append a new item to the current parent's list of children. | |
newtreeitem = TreeItem(columnData, parents[-1]) | |
parents[-1].appendChild(newtreeitem) | |
number += 1 | |
for i in xrange(extra_count): | |
country = 'West Miragi%03dstan' % i | |
abbrev = 'WMS%03d' % i | |
newtreeitem = TreeItem([country,abbrev], parent) | |
parent.appendChild(newtreeitem) | |
class MyCompleter(QtGui.QCompleter): | |
def splitPath(self, path): | |
result = path.split('/') | |
print "splitPath: ", result | |
return result | |
def pathFromIndex(self, index): | |
result = [] | |
while index.isValid(): | |
result = [self.model().data(index, QtCore.Qt.DisplayRole)] + result | |
index = index.parent() | |
r = '/'.join(result) | |
print "pathFromIndex: ", index, r | |
return r | |
if __name__ == '__main__': | |
import sys | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--extra-count','-e',type=int,default=0,help='Extra country count') | |
args = parser.parse_args() | |
app = QtGui.QApplication(sys.argv) | |
model = TreeModel(''' | |
Canada CAN | |
British Columbia BC | |
Vancouver | |
Manitoba MB | |
Winnipeg | |
Ontario ON | |
Toronto | |
Quebec QC | |
Montreal | |
France FRA | |
Brittany BRE | |
Normandy NOR | |
Germany DEU | |
Bavaria BY | |
Mexico MEX | |
Baja California BC | |
Mexicali | |
Chihuahua CI | |
Ciudad Juárez | |
Coahuila CU | |
Saltillo | |
Durango DG | |
Durango | |
Sonora SO | |
Hermosillo | |
Nogales | |
United States USA | |
Alabama AL | |
Birmingham | |
Huntsville | |
Mobile | |
Montgomery | |
Alaska AK | |
Anchorage | |
Fairbanks | |
Juneau | |
Arizona AZ | |
Chandler | |
Gilbert | |
Glendale | |
Mesa | |
Peoria | |
Phoenix | |
Scottsdale | |
Surprise | |
Tempe | |
Tucson | |
Arkansas AR | |
Fayetteville | |
Fort Smith | |
Little Rock | |
California CA | |
Anaheim | |
Bakersfield | |
Berkeley | |
Carlsbad | |
Chula Vista | |
Concord | |
Corona | |
El Monte | |
Elk Grove | |
Escondido | |
Fairfield | |
Fontana | |
Fremont | |
Fresno | |
Fullerton | |
Garden Grove | |
Glendale | |
Hayward | |
Huntington Beach | |
Irvine | |
Lancaster | |
Long Beach | |
Los Angeles | |
Modesto | |
Moreno Valley | |
Oakland | |
Oceanside | |
Ontario | |
Orange | |
Oxnard | |
Palmdale | |
Pasadena | |
Pomona | |
Rancho Cucamonga | |
Riverside | |
Roseville | |
Sacramento | |
Salinas | |
San Bernardino | |
San Diego | |
San Francisco | |
San Jose | |
Santa Ana | |
Santa Clara | |
Santa Clarita | |
Santa Rosa | |
Simi Valley | |
Stockton | |
Sunnyvale | |
Thousand Oaks | |
Torrance | |
Vallejo | |
Victorville | |
Visalia | |
Colorado CO | |
Arvada | |
Aurora | |
Colorado Springs | |
Denver | |
Fort Collins | |
Lakewood | |
Thornton | |
Connecticut CT | |
Bridgeport | |
Hartford | |
New Haven | |
Stamford | |
Delaware DE | |
Dover | |
Newark | |
Wilmington | |
District of Columbia DC | |
Washington | |
Florida FL | |
Cape Coral | |
Clearwater | |
Coral Springs | |
Fort Lauderdale | |
Gainesville | |
Hialeah | |
Hollywood | |
Jacksonville | |
Miami | |
Miramar | |
Orlando | |
Pembroke Pines | |
Port St. Lucie | |
St. Petersburg | |
Tallahassee | |
Tampa | |
Georgia GA | |
Athens | |
Atlanta | |
Augusta | |
Columbus | |
Macon | |
Savannah | |
Hawaii HI | |
Kahului | |
Hilo | |
Honolulu | |
Idaho ID | |
Boise | |
Coeur D'Alene | |
Pocatello | |
Illinois IL | |
Aurora | |
Chicago | |
Joliet | |
Naperville | |
Rockford | |
Indiana IN | |
Evansville | |
Fort Wayne | |
Indianapolis | |
Terre Haute | |
Iowa IA | |
Cedar Rapids | |
Davenport | |
Des Moines | |
Kansas KS | |
Kansas City | |
Olathe | |
Overland Park | |
Topeka | |
Wichita | |
Kentucky KY | |
Frankfort | |
Lexington | |
Louisville | |
Louisiana LA | |
Baton Rouge | |
Lafayette | |
New Orleans | |
Shreveport | |
Maine ME | |
Augusta | |
Bangor | |
Portland | |
Maryland MD | |
Annapolis | |
Baltimore | |
Frederick | |
Hagerstown | |
Massachusetts MA | |
Amherst | |
Boston | |
Cambridge | |
Pittsfield | |
Springfield | |
Worcester | |
Michigan MI | |
Ann Arbor | |
Detroit | |
Grand Rapids | |
Lansing | |
Sterling Heights | |
Warren | |
Minnesota MN | |
Minneapolis | |
Rochester | |
Saint Paul | |
Mississippi MS | |
Gulfport | |
Jackson | |
Tupelo | |
Vicksburg | |
Missouri MO | |
Columbia | |
Independence | |
Kansas City | |
Springfield | |
St. Louis | |
Montana MT | |
Billings | |
Bozeman | |
Helena | |
Missoula | |
Nebraska NE | |
Grand Island | |
Lincoln | |
Omaha | |
Nevada NV | |
Henderson | |
Las Vegas | |
North Las Vegas | |
Reno | |
New Hampshire NH | |
Concord | |
Manchester | |
Nashua | |
New Jersey NJ | |
Elizabeth | |
Jersey City | |
Newark | |
Paterson | |
New Mexico NM | |
Albuquerque | |
Las Cruces | |
Santa Fe | |
New York NY | |
Buffalo | |
New York | |
Rochester | |
Syracuse | |
Yonkers | |
North Carolina NC | |
Cary | |
Charlotte | |
Durham | |
Fayetteville | |
Greensboro | |
Raleigh | |
Wilmington | |
Winston-Salem | |
North Dakota ND | |
Bismarck | |
Fargo | |
Minot | |
Ohio OH | |
Akron | |
Cincinnati | |
Cleveland | |
Columbus | |
Dayton | |
Toledo | |
Oklahoma OK | |
Norman | |
Oklahoma City | |
Tulsa | |
Oregon OR | |
Eugene | |
Portland | |
Salem | |
Pennsylvania PA | |
Allentown | |
Harrisburg | |
Philadelphia | |
Pittsburgh | |
Rhode Island RI | |
Newport | |
Providence | |
Warwick | |
South Carolina SC | |
Charleston | |
Columbia | |
Myrtle Beach | |
South Dakota SD | |
Rapid City | |
Sioux Falls | |
Vermillion | |
Tennessee TN | |
Chattanooga | |
Clarksville | |
Knoxville | |
Memphis | |
Murfreesboro | |
Nashville | |
Texas TX | |
Abilene | |
Amarillo | |
Arlington | |
Austin | |
Beaumont | |
Brownsville | |
Carrollton | |
College Station | |
Corpus Christi | |
Dallas | |
Denton | |
El Paso | |
Fort Worth | |
Frisco | |
Garland | |
Grand Prairie | |
Houston | |
Irving | |
Killeen | |
Laredo | |
Lubbock | |
McAllen | |
McKinney | |
Mesquite | |
Midland | |
Odessa | |
Pasadena | |
Pearland | |
Plano | |
Richardson | |
Round Rock | |
San Antonio | |
Sugar Land | |
Waco | |
Utah UT | |
Moab | |
Provo | |
Salt Lake City | |
West Jordan | |
West Valley City | |
Vermont VT | |
Burlington | |
Montpelier | |
Waterbury | |
Virginia VA | |
Alexandria | |
Chesapeake | |
Fairfax | |
Hampton | |
Newport News | |
Norfolk | |
Richmond | |
Virginia Beach | |
Washington WA | |
Bellevue | |
Kent | |
Seattle | |
Spokane | |
Tacoma | |
Vancouver | |
West Virginia WV | |
Huntington | |
Morgantown | |
Wheeling | |
Wisconsin WI | |
Green Bay | |
Madison | |
Milwaukee | |
Wyoming WY | |
Casper | |
Cheyenne | |
Jackson | |
''', extra_count = args.extra_count) | |
mainwindow = QtGui.QMainWindow() | |
mainwindow.setWindowTitle("Tree Model with Completion") | |
mainwindow.sizeHint = lambda: QtCore.QSize(400,400) | |
mainwindow.show() | |
view = QtGui.QTreeView() | |
view.setModel(model) | |
mainwindow.setCentralWidget(view) | |
edit = QtGui.QLineEdit() | |
completer = MyCompleter(edit) | |
completer.setModel(model) | |
completer.setCompletionColumn(0) | |
completer.setCompletionRole(QtCore.Qt.DisplayRole) | |
completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive) | |
completer.setModelSorting(QtGui.QCompleter.CaseInsensitivelySortedModel) | |
edit.setWindowTitle("PySide QLineEdit Auto Complete") | |
edit.setCompleter(completer) | |
docker = QtGui.QDockWidget() | |
docker.setWidget(edit) | |
mainwindow.addDockWidget(QtCore.Qt.DockWidgetArea.BottomDockWidgetArea, docker) | |
sys.exit(app.exec_()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# based on | |
# https://qt.gitorious.org/pyside/pyside-examples/source/060dca8e4b82f301dfb33a7182767eaf8ad3d024:examples/itemviews/simpletreemodel/simpletreemodel.py | |
############################################################################ | |
## | |
## Copyright (C) 2005-2005 Trolltech AS. All rights reserved. | |
## | |
## This file is part of the example classes of the Qt Toolkit. | |
## | |
## This file may be used under the terms of the GNU General Public | |
## License version 2.0 as published by the Free Software Foundation | |
## and appearing in the file LICENSE.GPL included in the packaging of | |
## this file. Please review the following information to ensure GNU | |
## General Public Licensing requirements will be met: | |
## http://www.trolltech.com/products/qt/opensource.html | |
## | |
## If you are unsure which license is appropriate for your use, please | |
## review the following information: | |
## http://www.trolltech.com/products/qt/licensing.html or contact the | |
## sales department at [email protected]. | |
## | |
## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | |
## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
## | |
############################################################################ | |
from PySide import QtCore, QtGui | |
class TreeItem(object): | |
def __init__(self, data, parent=None): | |
self.parentItem = parent | |
self.itemData = data | |
self.childItems = [] | |
def appendChild(self, item): | |
self.childItems.append(item) | |
def child(self, row): | |
return self.childItems[row] | |
def childCount(self): | |
return len(self.childItems) | |
def columnCount(self): | |
return len(self.itemData) | |
def data(self, column): | |
try: | |
result = self.itemData[column] | |
if column == 0: | |
print result | |
return result | |
except IndexError: | |
return None | |
def parent(self): | |
return self.parentItem | |
def row(self): | |
if self.parentItem: | |
return self.parentItem.childItems.index(self) | |
return 0 | |
def isRoot(self): | |
return False | |
def __repr__(self): | |
return 'TreeItem(%s)' % str(self.itemData) | |
class RootItem(TreeItem): | |
def isRoot(self): | |
return True | |
class TreeModel(QtCore.QAbstractItemModel): | |
SENTINEL = object() | |
def __init__(self, data, parent=None, extra_count=0): | |
super(TreeModel, self).__init__(parent) | |
self.headerItem = TreeItem(("Region", "Abbreviation")) | |
self.rootItem = RootItem(("", "[Dummy node]")) | |
self.setupModelData(data.split('\n'), self.rootItem, extra_count) | |
def columnCount(self, parent): | |
if parent.isValid(): | |
return parent.internalPointer().columnCount() | |
else: | |
return self.rootItem.columnCount() | |
def data(self, index, role): | |
if not index.isValid(): | |
return None | |
if role == QtCore.Qt.EditRole: | |
return self.rootItem.data(0) | |
if role != QtCore.Qt.DisplayRole: | |
return None | |
item = index.internalPointer() | |
return item.data(index.column()) | |
def flags(self, index): | |
if not index.isValid(): | |
return QtCore.Qt.NoItemFlags | |
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | |
def headerData(self, section, orientation, role): | |
if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole: | |
return self.headerItem.data(section) | |
return None | |
def index(self, row, column, parent): | |
if not self.hasIndex(row, column, parent): | |
return QtCore.QModelIndex() | |
if not parent.isValid(): | |
# top | |
if row == 0: | |
return self.createIndex(row, column, self.rootItem) | |
else: | |
return QtCore.QModelIndex() | |
else: | |
parentItem = parent.internalPointer() | |
childItem = parentItem.child(row) | |
if childItem: | |
return self.createIndex(row, column, childItem) | |
else: | |
return QtCore.QModelIndex() | |
def parent(self, index): | |
if not index.isValid(): | |
return QtCore.QModelIndex() | |
childItem = index.internalPointer() | |
parentItem = childItem.parent() | |
if childItem == self.rootItem: | |
return QtCore.QModelIndex() | |
return self.createIndex(parentItem.row(), 0, parentItem) | |
def rowCount(self, parent): | |
if parent.column() > 0: | |
return 0 | |
if not parent.isValid(): | |
# top | |
return 1 | |
else: | |
parentItem = parent.internalPointer() | |
return parentItem.childCount() | |
def setupModelData(self, lines, parent, extra_count): | |
parents = [parent] | |
indentations = [0] | |
number = 0 | |
while number < len(lines): | |
position = 0 | |
while position < len(lines[number]): | |
if lines[number][position] != ' ': | |
break | |
position += 1 | |
lineData = lines[number][position:].strip() | |
if lineData: | |
# Read the column data from the rest of the line. | |
columnData = [s for s in lineData.split('\t') if s] | |
if position > indentations[-1]: | |
# The last child of the current parent is now the new | |
# parent unless the current parent has no children. | |
if parents[-1].childCount() > 0: | |
parents.append(parents[-1].child(parents[-1].childCount() - 1)) | |
indentations.append(position) | |
else: | |
while position < indentations[-1] and len(parents) > 0: | |
parents.pop() | |
indentations.pop() | |
# Append a new item to the current parent's list of children. | |
p = parents[-1] | |
print p, lineData | |
newtreeitem = TreeItem(columnData, p) | |
p.appendChild(newtreeitem) | |
number += 1 | |
for i in xrange(extra_count): | |
country = 'Miragi%03dstan' % i | |
abbrev = 'MS%03d' % i | |
newtreeitem = TreeItem([country,abbrev], parent) | |
parent.appendChild(newtreeitem) | |
class MyCompleter(QtGui.QCompleter): | |
def splitPath(self, path): | |
# add dummy element | |
result = [""]+path.split('/') | |
print "splitPath: ", result | |
return result | |
def pathFromIndex(self, index): | |
result = [] | |
while index.isValid(): | |
item = index.internalPointer() | |
if item.isRoot(): | |
break | |
result = [self.model().data(index, QtCore.Qt.DisplayRole)] + result | |
index = index.parent() | |
r = '/'.join(result) | |
print "pathFromIndex: ", index, r | |
return r | |
if __name__ == '__main__': | |
import sys | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--extra-count','-e',type=int,default=0,help='Extra country count') | |
args = parser.parse_args() | |
app = QtGui.QApplication(sys.argv) | |
model = TreeModel(''' | |
Canada CAN | |
British Columbia BC | |
Vancouver | |
Manitoba MB | |
Winnipeg | |
Ontario ON | |
Toronto | |
Quebec QC | |
Montreal | |
France FRA | |
Normandy NOR | |
Brittany BRE | |
Germany DEU | |
Bavaria BY | |
United States USA | |
Alabama AL | |
Birmingham | |
Huntsville | |
Mobile | |
Montgomery | |
Alaska AK | |
Anchorage | |
Fairbanks | |
Juneau | |
Arizona AZ | |
Chandler | |
Gilbert | |
Glendale | |
Mesa | |
Peoria | |
Phoenix | |
Scottsdale | |
Surprise | |
Tempe | |
Tucson | |
Arkansas AR | |
Fayetteville | |
Fort Smith | |
Little Rock | |
California CA | |
Anaheim | |
Bakersfield | |
Berkeley | |
Carlsbad | |
Chula Vista | |
Concord | |
Corona | |
El Monte | |
Elk Grove | |
Escondido | |
Fairfield | |
Fontana | |
Fremont | |
Fresno | |
Fullerton | |
Garden Grove | |
Glendale | |
Hayward | |
Huntington Beach | |
Irvine | |
Lancaster | |
Long Beach | |
Los Angeles | |
Modesto | |
Moreno Valley | |
Oakland | |
Oceanside | |
Ontario | |
Orange | |
Oxnard | |
Palmdale | |
Pasadena | |
Pomona | |
Rancho Cucamonga | |
Riverside | |
Roseville | |
Sacramento | |
Salinas | |
San Bernardino | |
San Diego | |
San Francisco | |
San Jose | |
Santa Ana | |
Santa Clara | |
Santa Clarita | |
Santa Rosa | |
Simi Valley | |
Stockton | |
Sunnyvale | |
Thousand Oaks | |
Torrance | |
Vallejo | |
Victorville | |
Visalia | |
Colorado CO | |
Arvada | |
Aurora | |
Colorado Springs | |
Denver | |
Fort Collins | |
Lakewood | |
Thornton | |
Connecticut CT | |
Bridgeport | |
Hartford | |
New Haven | |
Stamford | |
Delaware DE | |
Dover | |
Newark | |
Wilmington | |
District of Columbia DC | |
Washington | |
Florida FL | |
Cape Coral | |
Clearwater | |
Coral Springs | |
Fort Lauderdale | |
Gainesville | |
Hialeah | |
Hollywood | |
Jacksonville | |
Miami | |
Miramar | |
Orlando | |
Pembroke Pines | |
Port St. Lucie | |
St. Petersburg | |
Tallahassee | |
Tampa | |
Georgia GA | |
Athens | |
Atlanta | |
Augusta | |
Columbus | |
Macon | |
Savannah | |
Hawaii HI | |
Kahului | |
Hilo | |
Honolulu | |
Idaho ID | |
Boise | |
Coeur D'Alene | |
Pocatello | |
Illinois IL | |
Aurora | |
Chicago | |
Joliet | |
Naperville | |
Rockford | |
Indiana IN | |
Evansville | |
Fort Wayne | |
Indianapolis | |
Terre Haute | |
Iowa IA | |
Cedar Rapids | |
Davenport | |
Des Moines | |
Kansas KS | |
Kansas City | |
Olathe | |
Overland Park | |
Topeka | |
Wichita | |
Kentucky KY | |
Frankfort | |
Lexington | |
Louisville | |
Louisiana LA | |
Baton Rouge | |
Lafayette | |
New Orleans | |
Shreveport | |
Maine ME | |
Augusta | |
Bangor | |
Portland | |
Maryland MD | |
Annapolis | |
Baltimore | |
Frederick | |
Hagerstown | |
Massachusetts MA | |
Amherst | |
Boston | |
Cambridge | |
Pittsfield | |
Springfield | |
Worcester | |
Michigan MI | |
Ann Arbor | |
Detroit | |
Grand Rapids | |
Lansing | |
Sterling Heights | |
Warren | |
Minnesota MN | |
Minneapolis | |
Rochester | |
Saint Paul | |
Mississippi MS | |
Gulfport | |
Jackson | |
Tupelo | |
Vicksburg | |
Missouri MO | |
Columbia | |
Independence | |
Kansas City | |
Springfield | |
St. Louis | |
Montana MT | |
Billings | |
Bozeman | |
Helena | |
Missoula | |
Nebraska NE | |
Grand Island | |
Lincoln | |
Omaha | |
Nevada NV | |
Henderson | |
Las Vegas | |
North Las Vegas | |
Reno | |
New Hampshire NH | |
Concord | |
Manchester | |
Nashua | |
New Jersey NJ | |
Elizabeth | |
Jersey City | |
Newark | |
Paterson | |
New Mexico NM | |
Albuquerque | |
Las Cruces | |
Santa Fe | |
New York NY | |
Buffalo | |
New York | |
Rochester | |
Syracuse | |
Yonkers | |
North Carolina NC | |
Cary | |
Charlotte | |
Durham | |
Fayetteville | |
Greensboro | |
Raleigh | |
Wilmington | |
Winston-Salem | |
North Dakota ND | |
Bismarck | |
Fargo | |
Minot | |
Ohio OH | |
Akron | |
Cincinnati | |
Cleveland | |
Columbus | |
Dayton | |
Toledo | |
Oklahoma OK | |
Norman | |
Oklahoma City | |
Tulsa | |
Oregon OR | |
Eugene | |
Portland | |
Salem | |
Pennsylvania PA | |
Allentown | |
Harrisburg | |
Philadelphia | |
Pittsburgh | |
Rhode Island RI | |
Newport | |
Providence | |
Warwick | |
South Carolina SC | |
Charleston | |
Columbia | |
Myrtle Beach | |
South Dakota SD | |
Rapid City | |
Sioux Falls | |
Vermillion | |
Tennessee TN | |
Chattanooga | |
Clarksville | |
Knoxville | |
Memphis | |
Murfreesboro | |
Nashville | |
Texas TX | |
Abilene | |
Amarillo | |
Arlington | |
Austin | |
Beaumont | |
Brownsville | |
Carrollton | |
College Station | |
Corpus Christi | |
Dallas | |
Denton | |
El Paso | |
Fort Worth | |
Frisco | |
Garland | |
Grand Prairie | |
Houston | |
Irving | |
Killeen | |
Laredo | |
Lubbock | |
McAllen | |
McKinney | |
Mesquite | |
Midland | |
Odessa | |
Pasadena | |
Pearland | |
Plano | |
Richardson | |
Round Rock | |
San Antonio | |
Sugar Land | |
Waco | |
Utah UT | |
Moab | |
Provo | |
Salt Lake City | |
West Jordan | |
West Valley City | |
Vermont VT | |
Burlington | |
Montpelier | |
Waterbury | |
Virginia VA | |
Alexandria | |
Chesapeake | |
Fairfax | |
Hampton | |
Newport News | |
Norfolk | |
Richmond | |
Virginia Beach | |
Washington WA | |
Bellevue | |
Kent | |
Seattle | |
Spokane | |
Tacoma | |
Vancouver | |
West Virginia WV | |
Huntington | |
Morgantown | |
Wheeling | |
Wisconsin WI | |
Green Bay | |
Madison | |
Milwaukee | |
Wyoming WY | |
Casper | |
Cheyenne | |
Jackson | |
Mexico MEX | |
Baja California BC | |
Mexicali | |
Chihuahua CI | |
Ciudad Juárez | |
Coahuila CU | |
Saltillo | |
Durango DG | |
Durango | |
Sonora SO | |
Hermosillo | |
Nogales | |
''', extra_count = args.extra_count) | |
mainwindow = QtGui.QMainWindow() | |
mainwindow.setWindowTitle("Tree Model with Completion") | |
mainwindow.sizeHint = lambda: QtCore.QSize(400,400) | |
mainwindow.show() | |
view = QtGui.QTreeView() | |
view.setModel(model) | |
mainwindow.setCentralWidget(view) | |
edit = QtGui.QLineEdit() | |
completer = MyCompleter(edit) | |
completer.setModel(model) | |
completer.setCompletionColumn(0) | |
completer.setCompletionRole(QtCore.Qt.DisplayRole) | |
completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive) | |
edit.setWindowTitle("PySide QLineEdit Auto Complete") | |
edit.setCompleter(completer) | |
docker = QtGui.QDockWidget() | |
docker.setWidget(edit) | |
mainwindow.addDockWidget(QtCore.Qt.DockWidgetArea.BottomDockWidgetArea, docker) | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment