nimqml/examples/charts/mylistmodel.nim

77 lines
1.9 KiB
Nim
Raw Normal View History

2018-03-03 09:31:27 +00:00
import NimQml, Tables, random
2018-03-02 22:20:57 +00:00
type
Point = object
x: int
y: int
QtObject:
type
MyListModel* = ref object of QAbstractTableModel
2018-03-02 22:20:57 +00:00
points*: seq[Point]
2018-03-03 09:31:27 +00:00
maxX: int
maxY: int
2018-03-02 22:20:57 +00:00
proc delete(self: MyListModel) =
self.QAbstractTableModel.delete
2018-03-02 22:20:57 +00:00
proc setup(self: MyListModel) =
self.QAbstractTableModel.setup
2018-03-02 22:20:57 +00:00
method rowCount(self: MyListModel, index: QModelIndex = nil): int =
2018-03-03 09:31:27 +00:00
return self.points.len
2018-03-02 22:20:57 +00:00
method columnCount(self: MyListModel, index: QModelIndex = nil): int =
2018-03-03 09:31:27 +00:00
return 2
2018-03-02 22:20:57 +00:00
method data(self: MyListModel, index: QModelIndex, role: int): QVariant =
result = nil
if not index.isValid or index.row < 0 or index.row >= self.rowCount() or index.column < 0 or index.column >= self.columnCount():
return
2018-03-03 09:31:27 +00:00
if role == 0:
let point = self.points[index.row]
if index.column == 0:
return newQVariant(point.x)
elif index.column == 1:
return newQVariant(point.y)
proc getMaxY(self: MyListModel): int {.slot.} =
return self.maxY
proc maxYChanged(self: MyListModel, value: int) {.signal.}
QtProperty[int] maxY:
read = getMaxY
notify = maxYChanged
proc getMaxX(self: MyListModel): int {.slot.} =
return self.maxX
proc maxXChanged(self: MyListModel, value: int) {.signal.}
QtProperty[int] maxX:
read = getMaxX
notify = maxXChanged
proc addRandomPoint(self: MyListModel) {.slot.} =
let pos = self.points.len
self.beginInsertRows(newQModelIndex(), pos, pos)
let x = self.maxX + 1
let y = random(50)
if x > self.maxX:
self.maxX = x
self.maxXChanged(x)
if y > self.maxY:
self.maxY = y
self.maxYChanged(y)
self.points.add(Point(x: x, y: y))
self.endInsertRows()
proc newMyListModel*(): MyListModel =
new(result, delete)
result.setup
result.points = @[]
result.maxX = 0
result.maxY = 50
result.addRandomPoint()