Fixed URL recognizer to not endlessly loop anymore
[quassel.git] / src / qtui / chatitem.cpp
index e0d3e22..14fd3ad 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <QApplication>
 #include <QClipboard>
+#include <QDesktopServices>
 #include <QFontMetrics>
 #include <QGraphicsSceneMouseEvent>
 #include <QPainter>
 #include "chatlinemodel.h"
 #include "qtui.h"
 
-ChatItem::ChatItem(const QPersistentModelIndex &index_, QGraphicsItem *parent) : QGraphicsItem(parent), _index(index_) {
-  _fontMetrics = QtUi::style()->fontMetrics(data(ChatLineModel::FormatRole).value<UiStyle::FormatList>().at(0).second);
-  _layout = 0;
-  _lines = 0;
-  _selectionStart = -1;
-  _selectionMode = NoSelection;
+ChatItem::ChatItem(int col, QAbstractItemModel *model, QGraphicsItem *parent)
+  : QGraphicsItem(parent),
+    _fontMetrics(0),
+    _col(col),
+    _lines(0),
+    _layout(0),
+    _selectionMode(NoSelection),
+    _selectionStart(-1)
+{
+  Q_ASSERT(model);
+  QModelIndex index = model->index(row(), col);
+  _fontMetrics = QtUi::style()->fontMetrics(model->data(index, ChatLineModel::FormatRole).value<UiStyle::FormatList>().at(0).second);
   setAcceptHoverEvents(true);
   setZValue(20);
 }
@@ -45,11 +52,12 @@ ChatItem::~ChatItem() {
 }
 
 QVariant ChatItem::data(int role) const {
-  if(!_index.isValid()) {
-    qWarning() << "ChatItem::data(): Model index is invalid!" << _index;
+  QModelIndex index = model()->index(row(), column());
+  if(!index.isValid()) {
+    qWarning() << "ChatItem::data(): model index is invalid!" << index;
     return QVariant();
   }
-  return _index.data(role);
+  return model()->data(index, role);
 }
 
 qreal ChatItem::setWidth(qreal w) {
@@ -237,6 +245,25 @@ void ChatItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
   }
 }
 
+void ChatItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
+  // FIXME dirty and fast hack to make http:// urls klickable
+
+  QRegExp regex("\\b([hf]t{1,2}ps?://[^\\s]+)\\b");
+  QString str = data(ChatLineModel::DisplayRole).toString();
+  int idx = posToCursor(event->pos());
+  int mi = 0;
+  do {
+    mi = regex.indexIn(str, mi);
+    if(mi < 0) break;
+    if(idx >= mi && idx < mi + regex.matchedLength()) {
+      QDesktopServices::openUrl(QUrl(regex.capturedTexts()[1]));
+      break;
+    }
+    mi += regex.matchedLength();
+  } while(mi >= 0);
+  event->accept();
+}
+
 void ChatItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
   //qDebug() << (void*)this << "entering";