Some lines have been to long. Here's the example again:
import sys
import PyQt4.QtGui as QtGui
import PyQt4.QtCore as QtCore
###############################################################################
# Underlying data
# ----------------
# - RuntimeItems hold the data. They come from a database.
# - ViewItems are the objects, that are given to the model indexes of
Qt. They
# are constructed according to some rules like filters and configuration.
# - DummieViewItemFactory processes the rules and configurations. The
example
# here is simplfied. An instance of the factory is given to each ViewItem.
# The view item calls the DummieViewItemFactory.get_view_item_children
method
# to request calculation of its children on demand.
# - For this demo-version, the number of items is controlled by
# DummieViewItemFactory.max_items. It's passed in by the constructor.
# - Nesting as high as possible: One child per parent.
###############################################################################
class RuntimeItem(object):
"""Represent the real world business items. These objects have a
lot of
relations.
"""
def __init__(self, name, ident, item_type):
self.name = name
self.ident = ident
self.item_type = item_type
class ViewItem(object):
"""Represent items that are to be shown to the user in a QTreeView.
Those items do only occur one time in a view. They have a corresponding
runtime_item.
The children are calculated by the view_item_factory on demand.
"""
def __init__(self, view_item_factory, runtime_item=None, parent=None,
hidden_runtime_items=None):
self.view_item_factory = view_item_factory
self.runtime_item = runtime_item
self.parent = parent
self.hidden_runtime_items = hidden_runtime_items
@property
def children(self):
try:
return self._children
except AttributeError:
self._children = \
self.view_item_factory.get_view_item_children(self)
return self._children
@children.setter
def children(self, children):
self._children = children
class DummieViewItemFactory(object):
"""Creates the view_items. This is a dumb dummie as a simple example.
Normally a lot of things happen here like filtering and configuration.
But once the view_item hierachy is build, this shouldn't be called
at all.
"""
def __init__(self, runtime_item, max_items):
self.runtime_item = runtime_item
self.max_items = max_items
self.item_counter = 0
self.aux_root_view_item = ViewItem(self)
def get_view_item_children(self, view_item_parent):
if self.item_counter > self.max_items:
return []
self.item_counter += 1
view_item = ViewItem(self, self.runtime_item, view_item_parent)
return [view_item]
##############################################################################
# Qt classes
# ----------------
# - This should be standard stuff. I've got most of it from the Rapid GUI
# Programming book.
# - The ActiveColums class tells the model which colums to use.
# - The TreeView has a context menu with navigation actions.
# - The expand_all calls the Qt slot. Here the surprise for the performance.
##############################################################################
class ActiveColumns(object):
def __init__(self, columns):
self.columns = columns
class TreeView(QtGui.QTreeView):
def __init__(self, aux_root_view_item, active_columns, parent=None,
header_hidden=False):
super(TreeView, self).__init__(parent)
self.setIndentation(10)
self.active_columns = active_columns
self.setAlternatingRowColors(True)
self.setHeaderHidden(header_hidden)
self.setAllColumnsShowFocus(True)
self.setUniformRowHeights(True)
self.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
model = TreeModel(aux_root_view_item, self)
self.setModel(model)
e_a_action = QtGui.QAction("Expand all", self)
e_a_action.setToolTip("Expands all items of the tree.")
e_a_action.triggered.connect(self.expand_all)
e_a_b_action = QtGui.QAction("Expand all below", self)
e_a_b_action.setToolTip("Expands all items under the selection.")
e_a_b_action.triggered.connect(self.expand_all_below)
c_a_action = QtGui.QAction("Collapse all", self)
c_a_action.setToolTip("Collapses all items of the tree.")
c_a_action.triggered.connect(self.collapse_all)
c_a_b_action = QtGui.QAction("Collapse all below", self)
c_a_b_action.setToolTip("Collapses all items under the selection.")
c_a_b_action.triggered.connect(self.collapse_all_below)
for action in (e_a_action, c_a_action, e_a_b_action, c_a_b_action):
self.addAction(action)
def expand_all(self):
self.expandAll()
def collapse_all(self):
self.collapseAll()
def expand_all_below(self):
def expand_all_below_recursive(parent_index):
self.expand(parent_index)
children_indexes = \
self.tree_itemmodel.get_children_indexes(parent_index)
for child_index in children_indexes:
expand_all_below_recursive(child_index)
indexes = self.selectedIndexes()
if indexes:
index = indexes[0]
expand_all_below_recursive(index)
def collapse_all_below(self):
def collapse_all_below_recursive(parent_index):
self.collapse(parent_index)
children_indexes = \
self.tree_itemmodel.get_children_indexes(parent_index)
for child_index in children_indexes:
collapse_all_below_recursive(child_index)
indexes = self.selectedIndexes()
if indexes:
index = indexes[0]
collapse_all_below_recursive(index)
class TreeModel(QtCore.QAbstractItemModel):
def __init__(self, aux_root_view_item, parent):
super(TreeModel, self).__init__(parent)
self.aux_root_view_item = aux_root_view_item
self.active_columns = parent.active_columns
def rowCount(self, parent_index):
parent_view_item = self.view_item_from_index(parent_index)
if parent_view_item is None:
return 0
return len(parent_view_item.children)
def get_children_indexes(self, parent_index):
children_indexes = []
for row_no in range(self.rowCount(parent_index)):
children_indexes.append(self.index(row_no, 0, parent_index))
return children_indexes
def columnCount(self, parent):
return len(self.active_columns.columns)
def data(self, index, role):
if role == QtCore.Qt.TextAlignmentRole:
return int(QtCore.Qt.AlignTop|QtCore.Qt.AlignLeft)
if role != QtCore.Qt.DisplayRole:
return None
view_item = self.view_item_from_index(index)
try:
data = getattr(view_item.runtime_item,
self.active_columns.columns[index.column()])
except AttributeError:
data = ""
return data
def headerData(self, section, orientation, role):
if (orientation == QtCore.Qt.Horizontal and
role == QtCore.Qt.DisplayRole):
assert 0 <= section <= len(self.active_columns.columns)
return self.active_columns.columns[section]
return QtCore.QVariant()
def index(self, row, column, parent_index):
view_item_parent = self.view_item_from_index(parent_index)
return self.createIndex(row, column,
view_item_parent.children[row])
def parent(self, child_index):
child_view_item = self.view_item_from_index(child_index)
if child_view_item is None:
return QtCore.QModelIndex()
parent_view_item = child_view_item.parent
if parent_view_item is None:
return QtCore.QModelIndex()
grandparent_view_item = parent_view_item.parent
if grandparent_view_item is None:
return QtCore.QModelIndex()
grandparent_view_item
row = grandparent_view_item.children.index(parent_view_item)
assert row != -1
return self.createIndex(row, 0, parent_view_item)
def view_item_from_index(self, index):
return (index.internalPointer()
if index.isValid() else self.aux_root_view_item)
if __name__ == "__main__":
run_time_item = RuntimeItem("Test", "test_12", "Test Item")
view_factory = DummieViewItemFactory(run_time_item, max_items=5000)
active_colums = ActiveColumns(["name", "id", "item_type"])
app = QtGui.QApplication(sys.argv)
tree_view = TreeView(view_factory.aux_root_view_item, active_colums)
app.setApplicationName("IPDM")
tree_view.show()
app.exec_()
More information about the PyQt
mailing list
‘She has never mentioned her father to me. Was he—well, the sort of man whom the County Club would not have blackballed?’ "We walked by the side of our teams or behind the wagons, we slept on the ground at night, we did our own cooking, we washed our knives by sticking them into the ground rapidly a few times, and we washed our plates with sand and wisps of grass. When we stopped, we arranged our wagons in a circle, and thus formed a 'corral,' or yard, where we drove our oxen to yoke them up. And the corral was often very useful as a fort, or camp, for defending ourselves against the Indians. Do you see that little hollow down there?" he asked, pointing to a depression in the ground a short distance to the right of the train. "Well, in that hollow our wagon-train was kept three days and nights by the Indians. Three days and nights they stayed around, and made several attacks. Two of our men were killed and three were wounded by their arrows, and others had narrow escapes. One arrow hit me on the throat, but I was saved by the knot of my neckerchief, and the point only tore the skin a little. Since that time I have always had a fondness for large neckties. I don't know how many of the Indians we killed, as they carried off their dead and wounded, to save them from being scalped. Next to getting the scalps of their enemies, the most important thing with the Indians is to save their own. We had several fights during our journey, but that one was the worst. Once a little party of us were surrounded in a small 'wallow,' and had a tough time to defend ourselves successfully. Luckily for us, the Indians had no fire-arms then, and their bows and arrows were no match for our rifles. Nowadays they are well armed, but there are[Pg 41] not so many of them, and they are not inclined to trouble the railway trains. They used to do a great deal of mischief in the old times, and many a poor fellow has been killed by them." As dusk came on nearly the whole population of Maastricht, with all their temporary guests, formed an endless procession and went to invoke God's mercy by the Virgin Mary's intercession. They went to Our Lady's Church, in which stands the miraculous statue of Sancta Maria Stella Maris. The procession filled all the principal streets and squares of the town. I took my stand at the corner of the Vrijthof, where all marched past me, men, women, and children, all praying aloud, with loud voices beseeching: "Our Lady, Star of the Sea, pray for us ... pray for us ... pray for us ...!" It had not occurred to her for some hours after Mrs. Campbell had told her of Landor's death that she was free now to give herself to Cairness. She had gasped, indeed, when she did remember it, and had put the thought away, angrily and self-reproachfully. But it returned now, and she felt that she might cling to it. She had been grateful, and she had been faithful, too.[Pg 286] She remembered only that Landor had been kind to her, and forgot that for the last two years she had borne with much harsh coldness, and with a sort of contempt which she felt in her unanalyzing mind to have been entirely unmerited. Gradually she raised herself until she sat quite erect by the side of the mound, the old exultation of her half-wild girlhood shining in her face as she planned the future, which only a few minutes before had seemed so hopeless. After he had gloated over Sergeant Ramsey, Shorty got his men into the road ready to start. Si placed himself in front of the squad and deliberately loaded his musket in their sight. Shorty took his place in the rear, and gave out: The groups about each gun thinned out, as the shrieking fragments of shell mowed down man after man, but the rapidity of the fire did not slacken in the least. One of the Lieutenants turned and motioned with his saber to the riders seated on their horses in the line of limbers under the cover of the slope. One rider sprang from each team and ran up to take the place of men who had fallen. "As long as there's men and women in the world, the men 'ull be top and the women bottom." Then, in the house, the little girls were useful. Mrs. Backfield was not so energetic as she used to be. She had never been a robust woman, and though her husband's care had kept her well and strong, her frame was not equal to Reuben's demands; after fourteen years' hard labour, she suffered from rheumatism, which though seldom acute, was inclined to make her stiff and slow. It was here that Caro and Tilly came in, and Reuben began to appreciate his girls. After all, girls were needed in a house—and as for young men and marriage, their father could easily see that such follies did not spoil their usefulness or take them from him. Caro and Tilly helped their grandmother in all sorts of ways—they dusted, they watched pots, they shelled peas and peeled potatoes, they darned house-linen, they could even make a bed between them. HoME一级毛片视频免费公开
ENTER NUMBET 0018www.zh-hong.com.cn ysxxy.com.cn zjhd688.com.cn www.2jia.org.cn usbu.com.cn bxlt854.com.cn www.hhqqq.com.cn www.gszbj.com.cn www.mvrk.com.cn www.bsrb554.com.cn