+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 = 500;
+ int backlogSize = model()->rowCount();
+ if(isSingleBufferScene() && backlogSize != 0 && _lastBacklogSize + REQUEST_COUNT <= backlogSize) {
+ QModelIndex msgIdx = model()->index(0, 0);
+ while((Message::Type)(model()->data(msgIdx, ChatLineModel::TypeRole).toInt()) == Message::DayChange) {
+ msgIdx = msgIdx.sibling(msgIdx.row() + 1, 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;
+}
+
+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
+ ChatLine *firstLine = _lines.at(_firstLineRow);
+ ChatLine *lastLine = _lines.last();
+ updateSceneRect(QRectF(0, firstLine->pos().y(), width, lastLine->pos().y() + lastLine->height() - firstLine->pos().y()));
+}
+
+void ChatScene::updateSceneRect(const QRectF &rect) {
+ _sceneRect = rect;
+ setSceneRect(rect);
+ update();
+}
+
+void ChatScene::customEvent(QEvent *event) {
+ switch(event->type()) {
+ default:
+ return;
+ }
+}
+
+void ChatScene::loadWebPreview(ChatItem *parentItem, const QString &url, const QRectF &urlRect) {
+#ifndef HAVE_WEBKIT
+ Q_UNUSED(parentItem)
+ Q_UNUSED(url)
+ Q_UNUSED(urlRect)
+#else
+ if(webPreview.parentItem != parentItem)
+ webPreview.parentItem = parentItem;
+
+ if(webPreview.url != url) {
+ webPreview.url = url;
+ // load a new web view and delete the old one (if exists)
+ if(webPreview.previewItem && webPreview.previewItem->scene()) {
+ removeItem(webPreview.previewItem);
+ delete webPreview.previewItem;
+ }
+ webPreview.previewItem = new WebPreviewItem(url);
+ webPreview.delayTimer.start(2000);
+ webPreview.deleteTimer.stop();
+ } else if(webPreview.previewItem && !webPreview.previewItem->scene()) {
+ // we just have to readd the item to the scene
+ webPreview.delayTimer.start(2000);
+ webPreview.deleteTimer.stop();
+ }
+ if(webPreview.urlRect != urlRect) {
+ webPreview.urlRect = urlRect;
+ qreal previewY = urlRect.bottom();
+ qreal previewX = urlRect.x();
+ if(previewY + webPreview.previewItem->boundingRect().height() > sceneRect().bottom())
+ previewY = urlRect.y() - webPreview.previewItem->boundingRect().height();
+
+ if(previewX + webPreview.previewItem->boundingRect().width() > sceneRect().width())
+ previewX = sceneRect().right() - webPreview.previewItem->boundingRect().width();
+
+ webPreview.previewItem->setPos(previewX, previewY);
+ }
+#endif
+}
+
+void ChatScene::showWebPreviewEvent() {
+#ifdef HAVE_WEBKIT
+ if(webPreview.previewItem)
+ addItem(webPreview.previewItem);
+#endif
+}
+
+void ChatScene::clearWebPreview(ChatItem *parentItem) {
+#ifndef HAVE_WEBKIT
+ Q_UNUSED(parentItem)
+#else
+ if(parentItem == 0 || webPreview.parentItem == parentItem) {
+ if(webPreview.previewItem && webPreview.previewItem->scene()) {
+ removeItem(webPreview.previewItem);
+ webPreview.deleteTimer.start();
+ }
+ webPreview.delayTimer.stop();
+ }
+#endif
+}
+
+void ChatScene::deleteWebPreviewEvent() {
+#ifdef HAVE_WEBKIT
+ if(webPreview.previewItem) {
+ delete webPreview.previewItem;
+ webPreview.previewItem = 0;
+ }
+ webPreview.parentItem = 0;
+ webPreview.url = QString();
+ webPreview.urlRect = QRectF();
+#endif
+}