+void ChatScene::handlePositionChanged(qreal xpos) {
+ bool first = (sender() == firstColHandle);
+ qreal oldx;
+ if(first) {
+ oldx = firstColHandlePos;
+ firstColHandlePos = xpos;
+ } else {
+ oldx = secondColHandlePos;
+ secondColHandlePos = xpos;
+ }
+
+ ChatViewSettings viewSettings(this);
+ viewSettings.setValue("FirstColumnHandlePos", firstColHandlePos);
+ viewSettings.setValue("SecondColumnHandlePos", secondColHandlePos);
+
+ ChatViewSettings defaultSettings;
+ defaultSettings.setValue("FirstColumnHandlePos", firstColHandlePos);
+ defaultSettings.setValue("SecondColumnHandlePos", secondColHandlePos);
+
+ setWidth(width(), true); // readjust all chatlines
+ // we get ugly redraw errors if we don't update this explicitly... :(
+ // width() should be the same for both handles, so just use firstColHandle regardless
+ //update(qMin(oldx, xpos), 0, qMax(oldx, xpos) + firstColHandle->width(), height());
+}
+
+void ChatScene::setHandleXLimits() {
+ firstColHandle->setXLimits(0, secondColHandle->sceneLeft());
+ secondColHandle->setXLimits(firstColHandle->sceneRight(), width() - minContentsWidth);
+}
+
+void ChatScene::setSelectingItem(ChatItem *item) {
+ if(_selectingItem) _selectingItem->clearSelection();
+ _selectingItem = item;
+}
+
+void ChatScene::startGlobalSelection(ChatItem *item, const QPointF &itemPos) {
+ _selectionStart = _selectionEnd = _lastSelectionRow = _firstSelectionRow = item->row();
+ _selectionStartCol = _selectionMinCol = item->column();
+ _isSelecting = true;
+ _lines[_selectionStart]->setSelected(true, (ChatLineModel::ColumnType)_selectionMinCol);
+ updateSelection(item->mapToScene(itemPos));
+}
+
+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(secondColHandle->sceneRight() + 1, pos.y())));
+ if(!contentItem) return;
+
+ int curRow = contentItem->row();
+ int curColumn;
+ if(pos.x() > secondColHandle->sceneRight()) 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);
+ }
+ }
+ int newstart = qMin(curRow, _firstSelectionRow);
+ int newend = qMax(curRow, _firstSelectionRow);
+ if(newstart < _selectionStart) {
+ for(int l = newstart; l < _selectionStart; l++)
+ _lines[l]->setSelected(true, minColumn);
+ }
+ if(newstart > _selectionStart) {
+ for(int l = _selectionStart; l < newstart; l++)
+ _lines[l]->setSelected(false);
+ }
+ if(newend > _selectionEnd) {
+ for(int l = _selectionEnd+1; l <= newend; l++)
+ _lines[l]->setSelected(true, minColumn);
+ }
+ if(newend < _selectionEnd) {
+ for(int l = newend+1; l <= _selectionEnd; l++)
+ _lines[l]->setSelected(false);
+ }
+
+ _selectionStart = newstart;
+ _selectionEnd = newend;
+ _lastSelectionRow = curRow;
+
+ if(newstart == newend && minColumn == ChatLineModel::ContentsColumn) {
+ if(!_selectingItem) {
+ qWarning() << "WARNING: ChatScene::updateSelection() has a null _selectingItem, this should never happen! Please report.";
+ return;
+ }
+ _lines[curRow]->setSelected(false);
+ _isSelecting = false;
+ _selectingItem->continueSelecting(_selectingItem->mapFromScene(pos));
+ }
+}
+
+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) {
+ if(event->buttons() == Qt::LeftButton && _selectionStart >= 0) {
+ for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) {
+ _lines[l]->setSelected(false);
+ }
+ _selectionStart = -1;
+ QGraphicsScene::mousePressEvent(event); // so we can start a new local selection
+ } else {
+ QGraphicsScene::mousePressEvent(event);
+ }
+}
+
+void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
+ if(_isSelecting && !event->buttons() & Qt::LeftButton) {
+ putToClipboard(selectionToString());
+ _isSelecting = false;
+ event->accept();
+ } else {
+ QGraphicsScene::mouseReleaseEvent(event);
+ }
+}
+
+void ChatScene::putToClipboard(const QString &selection) {
+ // TODO Configure clipboards
+# ifdef Q_WS_X11
+ QApplication::clipboard()->setText(selection, QClipboard::Selection);
+# endif
+//# else
+ QApplication::clipboard()->setText(selection);
+//# endif
+}
+
+//!\brief Convert current selection to human-readable string.
+QString ChatScene::selectionToString() const {
+ //TODO Make selection format configurable!
+ if(!_isSelecting) return QString();
+ int start = qMin(_selectionStart, _selectionEnd);
+ int end = qMax(_selectionStart, _selectionEnd);
+ if(start < 0 || end >= _lines.count()) {
+ qDebug() << "Invalid selection range:" << start << end;
+ return QString();
+ }
+ QString result;
+ for(int l = start; l <= end; l++) {
+ if(_selectionMinCol == ChatLineModel::TimestampColumn)
+ result += _lines[l]->item(ChatLineModel::TimestampColumn).data(MessageModel::DisplayRole).toString() + " ";
+ if(_selectionMinCol <= ChatLineModel::SenderColumn)
+ result += _lines[l]->item(ChatLineModel::SenderColumn).data(MessageModel::DisplayRole).toString() + " ";
+ result += _lines[l]->item(ChatLineModel::ContentsColumn).data(MessageModel::DisplayRole).toString() + "\n";
+ }
+ return result;
+}
+
+void ChatScene::requestBacklog() {
+ static const int REQUEST_COUNT = 100;
+ int backlogSize = model()->rowCount();
+ if(isSingleBufferScene() && backlogSize != 0 && _lastBacklogSize + REQUEST_COUNT <= backlogSize) {
+ QModelIndex msgIdx = model()->index(0, 0);
+ MsgId msgId = model()->data(msgIdx, ChatLineModel::MsgIdRole).value<MsgId>();
+ BufferId bufferId = model()->data(msgIdx, ChatLineModel::BufferIdRole).value<BufferId>();
+ _lastBacklogSize = backlogSize;
+ Client::backlogManager()->requestBacklog(bufferId, REQUEST_COUNT, msgId.toInt());
+ }
+}
+
+int ChatScene::sectionByScenePos(int x) {
+ if(x < firstColHandle->x())
+ return ChatLineModel::TimestampColumn;
+ if(x < secondColHandle->x())
+ return ChatLineModel::SenderColumn;
+
+ return ChatLineModel::ContentsColumn;
+}