-void ChatScene::updateSceneRect(qreal width) {
- if(_lines.isEmpty()) {
- updateSceneRect(QRectF(0, 0, width, 0));
- return;
- }
-
- // we hide day change messages at the top by making the scene rect smaller
- // and by calling QGraphicsItem::hide() on all leading day change messages
- // the first one is needed to ensure proper scrollbar ranges
- // the second for cases where the viewport is larger then the set scenerect
- // (in this case the items are shown anyways)
- if(_firstLineRow == -1) {
- int numRows = model()->rowCount();
- _firstLineRow = 0;
- QModelIndex firstLineIdx;
- while(_firstLineRow < numRows) {
- firstLineIdx = model()->index(_firstLineRow, 0);
- if((Message::Type)(model()->data(firstLineIdx, MessageModel::TypeRole).toInt()) != Message::DayChange)
- break;
- _lines.at(_firstLineRow)->hide();
- _firstLineRow++;
+
+int ChatScene::rowByScenePos(qreal y) const
+{
+ QList<QGraphicsItem *> itemList = items(QPointF(0, y));
+
+ // ChatLine should be at the bottom of the list
+ for (int i = itemList.count()-1; i >= 0; i--) {
+ ChatLine *line = qgraphicsitem_cast<ChatLine *>(itemList.at(i));
+ if (line)
+ return line->row();
+ }
+ return -1;
+}
+
+
+void ChatScene::updateSceneRect(qreal width)
+{
+ if (_lines.isEmpty()) {
+ updateSceneRect(QRectF(0, 0, width, 0));
+ return;
+ }
+
+ // we hide day change messages at the top by making the scene rect smaller
+ // and by calling QGraphicsItem::hide() on all leading day change messages
+ // the first one is needed to ensure proper scrollbar ranges
+ // the second for cases where the viewport is larger then the set scenerect
+ // (in this case the items are shown anyways)
+ if (_firstLineRow == -1) {
+ int numRows = model()->rowCount();
+ _firstLineRow = 0;
+ QModelIndex firstLineIdx;
+ while (_firstLineRow < numRows) {
+ firstLineIdx = model()->index(_firstLineRow, 0);
+ if ((Message::Type)(model()->data(firstLineIdx, MessageModel::TypeRole).toInt()) != Message::DayChange)
+ break;
+ _lines.at(_firstLineRow)->hide();
+ _firstLineRow++;
+ }
+ }
+
+ // the following call should be safe. If it crashes something went wrong during insert/remove
+ if (_firstLineRow < _lines.count()) {
+ ChatLine *firstLine = _lines.at(_firstLineRow);
+ ChatLine *lastLine = _lines.last();
+ updateSceneRect(QRectF(0, firstLine->pos().y(), width, lastLine->pos().y() + lastLine->height() - firstLine->pos().y()));
+ }
+ else {
+ // empty scene rect
+ updateSceneRect(QRectF(0, 0, width, 0));