URL recognition (WiP)
authorManuel Nickschas <sputnick@quassel-irc.org>
Mon, 25 Aug 2008 13:12:13 +0000 (15:12 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Mon, 15 Sep 2008 20:54:36 +0000 (22:54 +0200)
src/qtui/chatitem.cpp
src/qtui/chatitem.h

index 7dd7aba..aea98db 100644 (file)
@@ -295,6 +295,28 @@ void ContentsChatItem::updateLayout() {
     h += line.height() + fontMetrics()->leading();
   }
   layout()->endLayout();
     h += line.height() + fontMetrics()->leading();
   }
   layout()->endLayout();
+
+  analyze();
+}
+
+void ContentsChatItem::analyze() {
+  // Match an URL
+  static QString urlEnd("(?:>|[,.;:]?\\s|\\b)");
+  static QString urlChars("(?:[\\w\\-~@/?&=+$()!%#]|[,.;:]\\w)");
+  static QRegExp urlExp(QString("((?:(?:https?://|ftp://|irc://|mailto:)|www)%1+)%2").arg(urlChars, urlEnd));
+
+  // Match a channel name
+  // We don't match for channel names starting with + or &, because that gives us a lot of false positives.
+  static QRegExp chanExp("((?:#|![A-Z0-9]{5})[^,:\\s]+(?::[^,:\\s]+)?)\\b");
+  QString str = data(ChatLineModel::DisplayRole).toString();
+  quint16 idx = 0;
+  // first, we split on characters that might be URL separators
+  int i = str.indexOf(chanExp);
+  if(i >= 0) {
+    qDebug() << i << chanExp.cap(1);
+  }
+
+
 }
 
 void ContentsChatItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
 }
 
 void ContentsChatItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) {
index 8e15c9f..116041c 100644 (file)
@@ -141,15 +141,30 @@ protected:
 
 private:
   struct LayoutData;
 
 private:
   struct LayoutData;
+  struct Clickable;
   class WrapColumnFinder;
 
   qreal computeHeight();
   class WrapColumnFinder;
 
   qreal computeHeight();
+  void analyze();
 
   LayoutData *_layoutData;
 };
 
 
   LayoutData *_layoutData;
 };
 
+struct ContentsChatItem::Clickable {
+  enum Type {
+    Url,
+    Channel,
+    Nick
+  };
+
+  quint16 start;
+  quint16 length;
+  Type type;
+};
+
 struct ContentsChatItem::LayoutData {
   QTextLayout *layout;
 struct ContentsChatItem::LayoutData {
   QTextLayout *layout;
+  QList<Clickable> clickables;
 
   LayoutData() { layout = 0; }
   ~LayoutData() { delete layout; }
 
   LayoutData() { layout = 0; }
   ~LayoutData() { delete layout; }