Update ChatViews whenever the stylesheet is reloaded
[quassel.git] / src / qtui / chatitem.cpp
index fc45b77..290542b 100644 (file)
@@ -32,6 +32,7 @@
 #include "bufferview.h"
 #include "chatitem.h"
 #include "chatlinemodel.h"
+#include "contextmenuactionprovider.h"
 #include "iconloader.h"
 #include "mainwin.h"
 #include "qtui.h"
@@ -68,7 +69,7 @@ void ChatItem::initLayoutHelper(QTextLayout *layout, QTextOption::WrapMode wrapM
   layout->setTextOption(option);
 
   QList<QTextLayout::FormatRange> formatRanges
-         = QtUi::style()->toTextLayoutList(data(MessageModel::FormatRole).value<UiStyle::FormatList>(), layout->text().length());
+         = QtUi::style()->toTextLayoutList(data(MessageModel::FormatRole).value<UiStyle::FormatList>(), layout->text().length(), data(ChatLineModel::MsgLabelRole).toUInt());
   layout->setAdditionalFormats(formatRanges);
 }
 
@@ -88,6 +89,11 @@ void ChatItem::doLayout(QTextLayout *layout) const {
 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
   Q_UNUSED(option); Q_UNUSED(widget);
   painter->setClipRect(boundingRect()); // no idea why QGraphicsItem clipping won't work
+
+  QVariant bgBrush = data(ChatLineModel::BackgroundRole);
+  if(bgBrush.isValid())
+    painter->fillRect(boundingRect(), bgBrush.value<QBrush>());
+
   QVector<QTextLayout::FormatRange> formats = additionalFormats();
   QTextLayout::FormatRange selectFmt = selectionFormat();
   if(selectFmt.format.isValid()) formats.append(selectFmt);
@@ -278,7 +284,7 @@ void ChatItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
 }
 
 void ChatItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
-  if(_selectionMode != NoSelection && !event->buttons() & Qt::LeftButton) {
+  if(_selectionMode != NoSelection && event->button() == Qt::LeftButton) {
     chatScene()->selectionToClipboard(QClipboard::Selection);
     event->accept();
   } else
@@ -286,9 +292,9 @@ void ChatItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
 }
 
 void ChatItem::addActionsToMenu(QMenu *menu, const QPointF &pos) {
-  Q_UNUSED(menu);
   Q_UNUSED(pos);
 
+  GraphicalUi::contextMenuActionProvider()->addActions(menu, chatScene()->filter(), data(MessageModel::BufferIdRole).value<BufferId>());
 }
 
 // ************************************************************
@@ -299,6 +305,11 @@ void SenderChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *op
   Q_UNUSED(option); Q_UNUSED(widget);
 
   painter->setClipRect(boundingRect()); // no idea why QGraphicsItem clipping won't work
+
+  QVariant bgBrush = data(ChatLineModel::BackgroundRole);
+  if(bgBrush.isValid())
+    painter->fillRect(boundingRect(), bgBrush.value<QBrush>());
+
   QTextLayout layout;
   initLayout(&layout);
   qreal layoutWidth = layout.minimumWidth();
@@ -352,13 +363,13 @@ ContentsChatItem::ContentsChatItem(const qreal &width, const QPointF &pos, QGrap
   : ChatItem(0, 0, pos, parent),
     _data(0)
 {
-  const QAbstractItemModel *model_ = model();
-  QModelIndex index = model_->index(row(), column());
-  _fontMetrics = QtUi::style()->fontMetrics(model_->data(index, ChatLineModel::FormatRole).value<UiStyle::FormatList>().at(0).second);
-
   setGeometryByWidth(width);
 }
 
+QFontMetricsF *ContentsChatItem::fontMetrics() const {
+  return QtUi::style()->fontMetrics(data(ChatLineModel::FormatRole).value<UiStyle::FormatList>().at(0).second);
+}
+
 ContentsChatItem::~ContentsChatItem() {
   delete _data;
 }
@@ -412,12 +423,12 @@ void ContentsChatItem::doLayout(QTextLayout *layout) const {
 QList<ContentsChatItem::Clickable> ContentsChatItem::findClickables() const {
   // For matching URLs
   static QString urlEnd("(?:>|[,.;:\"]*\\s|\\b|$)");
-  static QString urlChars("(?:[,.;:]*[\\w\\-~@/?&=+$()!%#*|{}\\[\\]])");
+  static QString urlChars("(?:[,.;:]*[\\w\\-~@/?&=+$()!%#*|{}\\[\\]'^])");
 
   static QRegExp regExp[] = {
     // URL
     // QRegExp(QString("((?:https?://|s?ftp://|irc://|mailto:|www\\.)%1+|%1+\\.[a-z]{2,4}(?:?=/%1+|\\b))%2").arg(urlChars, urlEnd)),
-    QRegExp(QString("((?:(?:https?://|s?ftp://|irc://|gopher://|mailto:)|www)%1+)%2").arg(urlChars, urlEnd), Qt::CaseInsensitive),
+    QRegExp(QString("((?:(?:mailto:|\\w+://)|www\\.)%1+)%2").arg(urlChars, urlEnd), Qt::CaseInsensitive),
 
     // Channel name
     // We don't match for channel names starting with + or &, because that gives us a lot of false positives.
@@ -444,7 +455,7 @@ QList<ContentsChatItem::Clickable> ContentsChatItem::findClickables() const {
     for(int i = 0; i < regExpCount; i++) {
       if(matches[i] < 0 || matchEnd[i] > str.length()) continue;
       if(idx >= matchEnd[i]) {
-        matches[i] = str.indexOf(regExp[i], qMax(matchEnd[i], idx));
+        matches[i] = regExp[i].indexIn(str, qMax(matchEnd[i], idx));
         if(matches[i] >= 0) matchEnd[i] = matches[i] + regExp[i].cap(1).length();
       }
       if(matches[i] >= 0 && matches[i] < minidx) {
@@ -602,8 +613,6 @@ void ContentsChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
 }
 
 void ContentsChatItem::addActionsToMenu(QMenu *menu, const QPointF &pos) {
-  Q_UNUSED(pos); // we assume that the current mouse cursor pos is the point of invocation
-
   if(privateData()->currentClickable.isValid()) {
     Clickable click = privateData()->currentClickable;
     switch(click.type) {
@@ -617,16 +626,15 @@ void ContentsChatItem::addActionsToMenu(QMenu *menu, const QPointF &pos) {
         foreach(QAction *action, menu->actions())
           action->setVisible(false);
         QString name = data(ChatLineModel::DisplayRole).toString().mid(click.start, click.length);
-        Client::mainUi()->actionProvider()->addActions(menu, chatScene()->filter(), data(MessageModel::BufferIdRole).value<BufferId>(), name);
+        GraphicalUi::contextMenuActionProvider()->addActions(menu, chatScene()->filter(), data(MessageModel::BufferIdRole).value<BufferId>(), name);
         break;
       }
       default:
         break;
     }
   } else {
-
     // Buffer-specific actions
-    Client::mainUi()->actionProvider()->addActions(menu, chatScene()->filter(), data(MessageModel::BufferIdRole).value<BufferId>());
+    ChatItem::addActionsToMenu(menu, pos);
   }
 }