Initial Project Commit
[retroconv] / testother2.py
1 #Written by Robin Burchell 
2 # No licence specified or required, but please give credit where it's due, and please let me know if this helped you.
3 # Feel free to contact with corrections or suggestions.
4 #
5 # We're using PySide, Nokia's official LGPL bindings.
6 # You can however easily use PyQt (Riverside Computing's GPL bindings) by commenting these and fixing the appropriate imports.
7 #from PySide.QtCore import *
8 #from PySide.QtGui import *
9 from PyQt4 import *
10 from PyQt4.QtCore import *
11 from PyQt4.QtGui import *
12 import sys
13
14 # This is our model. It will maintain, modify, and present data to our view(s).
15 # As this is read-only, it's pretty straightforward, but it can get pretty complex.
16 # This is something that Qt Development Frameworks/Nokia are aware of and working on, in terms of
17 # better documentation, as well as a better implementation of all this, but both of those aren't
18 # really within the scope of this tutorial. ;)
19 #
20 # For more information on list models, take a look at:
21 # http://doc.trolltech.com/4.6/qabstractitemmodel.html
22 # but do bear in mind there are other models (like tables) available, depending on your data needs.
23 # Again, beyond the scope of this tutorial for now. :)
24 class SimpleListModel(QAbstractListModel):
25  def __init__(self, mlist):
26   QAbstractListModel.__init__(self)
27
28   # Cache the passed data list as a class member.
29   self._items = mlist
30
31  # We need to tell the view how many rows we have present in our data.
32  # For us, at least, it's fairly straightforward, as we have a python list of data,
33  # so we can just return the length of that list.
34  def rowCount(self, parent = QModelIndex()):
35   return len(self._items)
36
37  # Here, it's a little more complex.
38  # data() is where the view asks us for all sorts of information about our data:
39  # this can be purely informational (the data itself), as well as all sorts of 'extras'
40  # such as how the data should be presented.
41  #
42  # For the sake of keeping it simple, I'm only going to show you the data, and one presentational
43  # aspect.
44  #
45  # For more information on what kind of data the views can ask for, take a look at:
46  # http://doc.trolltech.com/4.6/qabstractitemmodel.html#data
47  #
48  # Oh, and just  to clarify: when it says 'invalid QVariant', it means a null QVariant.
49  # i.e. QVariant().
50  #
51  # 'index' is of type QModelIndex, which actually has a whole host of stuff, but we
52  # only really care about the row number for the sake of this tutorial.
53  # For more information, see:
54  # http://doc.trolltech.com/4.6/qmodelindex.html
55  def data(self, index, role = Qt.DisplayRole):
56   if role == Qt.DisplayRole:
57    # The view is asking for the actual data, so, just return the item it's asking for.
58    return QVariant(self._items[index.row()])
59   elif role == Qt.BackgroundRole:
60    # Here, it's asking for some background decoration.
61    # Let's mix it up a bit: mod the row number to get even or odd, and return different
62    # colours depending.
63    # (you can, and should, more easily do this using this:
64    # http://doc.trolltech.com/4.6/qabstractitemview.html#alternatingRowColors-prop
65    # but I deliberately chose to show that you can put your own logic/processing here.)
66    #
67    # Exercise for the reader: make it print different colours for each row.
68    # Implementation is up to you.
69    if index.row() % 2 == 0:
70     return QVariant(QColor(Qt.gray))
71    else:
72     return QVariant(QColor(Qt.lightGray))
73   else:
74    # We don't care about anything else, so make sure to return an empty QVariant.
75    return QVariant()
76
77 # This widget is our view of the readonly list.
78 # Obviously, in a real application, this will be more complex, with signals/etc usage, but
79 # for the scope of this tutorial, let's keep it simple, as always.
80 #
81 # For more information, see:
82 # http://doc.trolltech.com/4.6/qlistview.html
83 class SimpleListView(QListView):
84  def __init__(self, parent = None):
85   QListView.__init__(self, parent)
86
87 # Our main application window.
88 # You should be used to this from previous tutorials.
89 class MyMainWindow(QWidget):
90  def __init__(self):
91   QWidget.__init__(self, None)
92
93   # main section of the window
94   vbox = QVBoxLayout()
95
96   # create a data source:
97   m = SimpleListModel(["test", "tes1t", "t3est", "t5est", "t3est"])
98
99   # let's add two views of the same data source we just created:
100   v = SimpleListView()
101   v.setModel(m)
102   vbox.addWidget(v)
103
104   # second view..
105   v = SimpleListView()
106   v.setModel(m)
107   vbox.addWidget(v)
108
109   # bottom section of the window
110   hbox = QHBoxLayout()
111
112   # add bottom to main window layout
113   vbox.addLayout(hbox)
114
115   # set layout on the window
116   self.setLayout(vbox)
117
118 # set things up, and run it. :)
119 if __name__ == '__main__':
120  app = QApplication(sys.argv)
121  w = MyMainWindow()
122  w.show()
123  app.exec_()
124  sys.exit()