_lines = 0;
_selectionStart = -1;
_selectionMode = NoSelection;
+ setAcceptHoverEvents(true);
+ setZValue(20);
}
ChatItem::~ChatItem() {
Q_UNUSED(option); Q_UNUSED(widget);
if(!haveLayout()) updateLayout();
painter->setClipRect(boundingRect()); // no idea why QGraphicsItem clipping won't work
- if(_selectionMode == FullSelection) {
- painter->save();
- painter->fillRect(boundingRect(), QApplication::palette().brush(QPalette::Highlight));
- painter->restore();
- } // TODO: add selection format here
+ //if(_selectionMode == FullSelection) {
+ //painter->save();
+ //painter->fillRect(boundingRect(), QApplication::palette().brush(QPalette::Highlight));
+ //painter->restore();
+ //}
QVector<QTextLayout::FormatRange> formats;
if(_selectionMode != NoSelection) {
QTextLayout::FormatRange selectFmt;
}
void ChatItem::setFullSelection() {
- _selectionMode = FullSelection;
- update();
+ if(_selectionMode != FullSelection) {
+ _selectionMode = FullSelection;
+ update();
+ }
}
void ChatItem::clearSelection() {
- _selectionMode = NoSelection;
+ if(_selectionMode != NoSelection) {
+ _selectionMode = NoSelection;
+ update();
+ }
+}
+
+void ChatItem::continueSelecting(const QPointF &pos) {
+ _selectionMode = PartialSelection;
+ _selectionEnd = posToCursor(pos);
update();
}
void ChatItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
if(contains(event->pos())) {
- _selectionEnd = posToCursor(event->pos());
- update();
+ qint16 end = posToCursor(event->pos());
+ if(end != _selectionEnd) {
+ _selectionEnd = end;
+ update();
+ }
} else {
setFullSelection();
- ungrabMouse();
- chatScene()->startGlobalSelection(this);
+ chatScene()->startGlobalSelection(this, event->pos());
}
}
}
}
+void ChatItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
+ //qDebug() << (void*)this << "entering";
+
+}
+
+void ChatItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
+ //qDebug() << (void*)this << "leaving";
+
+}
+
+void ChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
+ //qDebug() << (void*)this << event->pos();
+
+}
+
+
/*************************************************************************************************/
ChatItem::WrapColumnFinder::WrapColumnFinder(ChatItem *_item) : item(_item) {
{
_width = 0;
_selectingItem = 0;
+ _isSelecting = false;
connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &)));
connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)));
update(qMin(oldx, xpos) - firstColHandle->width()/2, 0, qMax(oldx, xpos) + firstColHandle->width()/2, height());
}
-void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
-
- QGraphicsScene::mouseMoveEvent(event);
+void ChatScene::setSelectingItem(ChatItem *item) {
+ if(_selectingItem) _selectingItem->clearSelection();
+ _selectingItem = item;
}
-void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
-
- QGraphicsScene::mousePressEvent(event);
+void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
+ _selectionStart = _selectionEnd = item->index().row();
+ _selectionStartCol = _selectionMinCol = item->index().column();
+ _isSelecting = true;
+ _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
+ updateSelection(item->mapToScene(itemPos));
}
-void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
+void ChatScene::updateSelection(const QPointF &pos) {
+ // This is somewhat hacky... we look at the contents item that is at the cursor's y position (ignoring x), since
+ // it has the full height. From this item, we can then determine the row index and hence the ChatLine.
+ ChatItem *contentItem = static_cast<ChatItem *>(itemAt(QPointF(secondColHandlePos + secondColHandle->width()/2, pos.y())));
+ if(!contentItem) return;
+
+ int curRow = contentItem->index().row();
+ int curColumn;
+ if(pos.x() > secondColHandlePos + secondColHandle->width()/2) curColumn = ChatLineModel::ContentsColumn;
+ else if(pos.x() > firstColHandlePos) curColumn = ChatLineModel::SenderColumn;
+ else curColumn = ChatLineModel::TimestampColumn;
+
+ ChatLineModel::ColumnType minColumn = (ChatLineModel::ColumnType)qMin(curColumn, _selectionStartCol);
+ if(minColumn != _selectionMinCol) {
+ _selectionMinCol = minColumn;
+ for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
+ _lines[l]->setSelected(true, minColumn);
+ }
+ }
- QGraphicsScene::mouseReleaseEvent(event);
-}
+ if(curRow > _selectionEnd && curRow > _selectionStart) { // select further towards bottom
+ for(int l = _selectionEnd + 1; l <= curRow; l++) {
+ _lines[l]->setSelected(true, minColumn);
+ }
+ } else if(curRow > _selectionEnd && curRow <= _selectionStart) { // deselect towards bottom
+ for(int l = _selectionEnd; l < curRow; l++) {
+ _lines[l]->setSelected(false);
+ }
+ } else if(curRow < _selectionEnd && curRow >= _selectionStart) {
+ for(int l = _selectionEnd; l > curRow; l--) {
+ _lines[l]->setSelected(false);
+ }
+ } else if(curRow < _selectionEnd && curRow < _selectionStart) {
+ for(int l = _selectionEnd - 1; l >= curRow; l--) {
+ _lines[l]->setSelected(true, minColumn);
+ }
+ }
+ _selectionEnd = curRow;
-void ChatScene::setSelectingItem(ChatItem *item) {
- if(_selectingItem) _selectingItem->clearSelection();
- _selectingItem = item;
+ if(curRow == _selectionStart && minColumn == ChatLineModel::ContentsColumn) {
+ _lines[curRow]->setSelected(false);
+ _isSelecting = false;
+ _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
+ }
}
-void ChatScene::startGlobalSelection(ChatItem *item) {
+void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
+ if(_isSelecting && event->buttons() & Qt::LeftButton) {
+ updateSelection(event->scenePos());
+ event->accept();
+ } else {
+ QGraphicsScene::mouseMoveEvent(event);
+ }
+}
+void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
+ qDebug() << "pressed";
+ QGraphicsScene::mousePressEvent(event);
+}
+void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
+ if(_isSelecting) {
+ _isSelecting = false;
+ event->accept();
+ } else {
+ QGraphicsScene::mouseReleaseEvent(event);
+ }
}
#include <QAbstractItemModel>
#include <QGraphicsScene>
+#include <QSet>
class AbstractUiMsg;
class Buffer;
public slots:
void setWidth(qreal);
- // these are used by the chatitems to notify the scene
+ // these are used by the chatitems to notify the scene and manage selections
void setSelectingItem(ChatItem *item);
ChatItem *selectingItem() const { return _selectingItem; }
- void startGlobalSelection(ChatItem *item);
+ void startGlobalSelection(ChatItem *item, const QPointF &itemPos);
signals:
void heightChanged(qreal height);
void handlePositionChanged(qreal xpos);
private:
+ void updateSelection(const QPointF &pos);
+
QString _idString;
qreal _width, _height;
QAbstractItemModel *_model;
ColumnHandleItem *firstColHandle, *secondColHandle;
qreal firstColHandlePos, secondColHandlePos;
- ChatItem *_selectingItem;
+ ChatItem *_selectingItem, *_lastItem;
+ QSet<ChatLine *> _selectedItems;
+ int _selectionStartCol, _selectionMinCol;
+ int _selectionStart;
+ int _selectionEnd;
+ bool _isSelecting;
};
#endif