Cleanups, tweaks and fixes
authorManuel Nickschas <sputnick@quassel-irc.org>
Mon, 18 Aug 2008 23:16:06 +0000 (01:16 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Mon, 18 Aug 2008 23:16:06 +0000 (01:16 +0200)
src/qtui/chatitem.cpp
src/qtui/chatitem.h
src/qtui/chatline.cpp
src/qtui/chatviewsearchcontroller.cpp

index 6a9b143..602a2c9 100644 (file)
@@ -34,9 +34,9 @@
 ChatItem::ChatItem(ChatLineModel::ColumnType col, QAbstractItemModel *model, QGraphicsItem *parent)
   : QGraphicsItem(parent),
     _fontMetrics(0),
 ChatItem::ChatItem(ChatLineModel::ColumnType col, QAbstractItemModel *model, QGraphicsItem *parent)
   : QGraphicsItem(parent),
     _fontMetrics(0),
-    _layoutData(0),
     _selectionMode(NoSelection),
     _selectionMode(NoSelection),
-    _selectionStart(-1)
+    _selectionStart(-1),
+    _layout(0)
 {
   Q_ASSERT(model);
   QModelIndex index = model->index(row(), col);
 {
   Q_ASSERT(model);
   QModelIndex index = model->index(row(), col);
@@ -46,7 +46,7 @@ ChatItem::ChatItem(ChatLineModel::ColumnType col, QAbstractItemModel *model, QGr
 }
 
 ChatItem::~ChatItem() {
 }
 
 ChatItem::~ChatItem() {
-  delete _layoutData;
+  delete _layout;
 }
 
 QVariant ChatItem::data(int role) const {
 }
 
 QVariant ChatItem::data(int role) const {
@@ -86,12 +86,6 @@ QTextLayout *ChatItem::createLayout(QTextOption::WrapMode wrapMode, Qt::Alignmen
   return layout;
 }
 
   return layout;
 }
 
-void ChatItem::setLayout(QTextLayout *layout) {
-  if(!_layoutData)
-    _layoutData = new LayoutData;
-  _layoutData->layout = layout;
-}
-
 void ChatItem::updateLayout() {
   if(!haveLayout())
     setLayout(createLayout(QTextOption::WrapAnywhere, Qt::AlignLeft));
 void ChatItem::updateLayout() {
   if(!haveLayout())
     setLayout(createLayout(QTextOption::WrapAnywhere, Qt::AlignLeft));
@@ -105,9 +99,9 @@ void ChatItem::updateLayout() {
   layout()->endLayout();
 }
 
   layout()->endLayout();
 }
 
-void ChatItem::clearLayoutData() {
-  delete _layoutData;
-  _layoutData = 0;
+void ChatItem::clearLayout() {
+  delete _layout;
+  _layout = 0;
 }
 
 // NOTE: This is not the most time-efficient implementation, but it saves space by not caching unnecessary data
 }
 
 // NOTE: This is not the most time-efficient implementation, but it saves space by not caching unnecessary data
@@ -199,7 +193,6 @@ QList<QRectF> ChatItem::findWords(const QString &searchWord, Qt::CaseSensitivity
   return resultList;
 }
 
   return resultList;
 }
 
-
 void ChatItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
   if(event->buttons() == Qt::LeftButton) {
     if(_selectionMode == NoSelection) {
 void ChatItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
   if(event->buttons() == Qt::LeftButton) {
     if(_selectionMode == NoSelection) {
@@ -249,40 +242,6 @@ 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";
-  event->ignore();
-}
-
-void ChatItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
-  //qDebug() << (void*)this << "leaving";
-  event->ignore();
-}
-
-void ChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
-  //qDebug() << (void*)this << event->pos();
-  event->ignore();
-}
-
 /*************************************************************************************************/
 
 /*************************************************************************************************/
 /*************************************************************************************************/
 
 /*************************************************************************************************/
@@ -294,6 +253,16 @@ void SenderChatItem::updateLayout() {
 
 /*************************************************************************************************/
 
 
 /*************************************************************************************************/
 
+ContentsChatItem::ContentsChatItem(QAbstractItemModel *model, QGraphicsItem *parent) : ChatItem(column(), model, parent),
+  _layoutData(0)
+{
+
+}
+
+ContentsChatItem::~ContentsChatItem() {
+  delete _layoutData;
+}
+
 qreal ContentsChatItem::computeHeight() {
   int lines = 1;
   WrapColumnFinder finder(this);
 qreal ContentsChatItem::computeHeight() {
   int lines = 1;
   WrapColumnFinder finder(this);
@@ -301,6 +270,17 @@ qreal ContentsChatItem::computeHeight() {
   return lines * fontMetrics()->lineSpacing();
 }
 
   return lines * fontMetrics()->lineSpacing();
 }
 
+void ContentsChatItem::setLayout(QTextLayout *layout) {
+  if(!_layoutData)
+    _layoutData = new LayoutData;
+  _layoutData->layout = layout;
+}
+
+void ContentsChatItem::clearLayout() {
+  delete _layoutData;
+  _layoutData = 0;
+}
+
 void ContentsChatItem::updateLayout() {
   if(!haveLayout()) setLayout(createLayout(QTextOption::WrapAnywhere));
 
 void ContentsChatItem::updateLayout() {
   if(!haveLayout()) setLayout(createLayout(QTextOption::WrapAnywhere));
 
@@ -324,6 +304,39 @@ void ContentsChatItem::updateLayout() {
   layout()->endLayout();
 }
 
   layout()->endLayout();
 }
 
+void ContentsChatItem::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 ContentsChatItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
+  //qDebug() << (void*)this << "entering";
+  event->ignore();
+}
+
+void ContentsChatItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
+  //qDebug() << (void*)this << "leaving";
+  event->ignore();
+}
+
+void ContentsChatItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
+  //qDebug() << (void*)this << event->pos();
+  event->ignore();
+}
 
 /*************************************************************************************************/
 
 
 /*************************************************************************************************/
 
index 745f2d3..8e15c9f 100644 (file)
@@ -48,8 +48,8 @@ public:
   inline qreal width() const { return _boundingRect.width(); }
   inline qreal height() const { return _boundingRect.height(); }
 
   inline qreal width() const { return _boundingRect.width(); }
   inline qreal height() const { return _boundingRect.height(); }
 
-  inline bool haveLayout() const { return _layoutData != 0 && layout() != 0; }
-  void clearLayoutData();
+  virtual inline bool haveLayout() const { return layout() != 0; }
+  virtual void clearLayout();
   virtual QTextLayout *createLayout(QTextOption::WrapMode, Qt::Alignment = Qt::AlignLeft);
   virtual void updateLayout();
   virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
   virtual QTextLayout *createLayout(QTextOption::WrapMode, Qt::Alignment = Qt::AlignLeft);
   virtual void updateLayout();
   virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
@@ -70,15 +70,9 @@ protected:
   virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
   virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
   virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
   virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
   virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
   virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
-  virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
-
-  virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
-  virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
-  virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
 
 
-  struct LayoutData;
-  inline QTextLayout *layout() const;
-  void setLayout(QTextLayout *);
+  virtual inline QTextLayout *layout() const { return _layout; }
+  virtual inline void setLayout(QTextLayout *l) { _layout = l; }
   qint16 posToCursor(const QPointF &pos);
 
   virtual qreal computeHeight();
   qint16 posToCursor(const QPointF &pos);
 
   virtual qreal computeHeight();
@@ -95,16 +89,12 @@ private:
   SelectionMode _selectionMode;
   qint16 _selectionStart, _selectionEnd;
 
   SelectionMode _selectionMode;
   qint16 _selectionStart, _selectionEnd;
 
-  LayoutData *_layoutData;
+  QTextLayout *_layout;
 };
 
 };
 
-struct ChatItem::LayoutData {
-  QTextLayout *layout;
-
-  LayoutData() { layout = 0; }
-  ~LayoutData() { delete layout; }
-};
+/*************************************************************************************************/
 
 
+//! A ChatItem for the timestamp column
 class TimestampChatItem : public ChatItem {
 
 public:
 class TimestampChatItem : public ChatItem {
 
 public:
@@ -113,30 +103,57 @@ public:
 
 };
 
 
 };
 
+/*************************************************************************************************/
+
+//! A ChatItem for the sender column
 class SenderChatItem : public ChatItem {
 
 public:
   SenderChatItem(QAbstractItemModel *model, QGraphicsItem *parent) : ChatItem(column(), model, parent) {}
   inline ChatLineModel::ColumnType column() const { return ChatLineModel::SenderColumn; }
 
 class SenderChatItem : public ChatItem {
 
 public:
   SenderChatItem(QAbstractItemModel *model, QGraphicsItem *parent) : ChatItem(column(), model, parent) {}
   inline ChatLineModel::ColumnType column() const { return ChatLineModel::SenderColumn; }
 
-  void updateLayout();
+  virtual void updateLayout();
 };
 
 };
 
+/*************************************************************************************************/
+
+//! A ChatItem for the contents column
 class ContentsChatItem : public ChatItem {
 
 public:
 class ContentsChatItem : public ChatItem {
 
 public:
-  ContentsChatItem(QAbstractItemModel *model, QGraphicsItem *parent) : ChatItem(column(), model, parent) {}
+  ContentsChatItem(QAbstractItemModel *model, QGraphicsItem *parent);
+  virtual ~ContentsChatItem();
+
   inline ChatLineModel::ColumnType column() const { return ChatLineModel::ContentsColumn; }
 
   inline ChatLineModel::ColumnType column() const { return ChatLineModel::ContentsColumn; }
 
-  void updateLayout();
+  virtual void clearLayout();
+  virtual void updateLayout();
+  virtual inline bool haveLayout() const { return _layoutData != 0 && layout() != 0; }
+
+protected:
+  virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event);
+  virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
+  virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
+  virtual void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
+
+  virtual inline QTextLayout *layout() const;
+  virtual void setLayout(QTextLayout *l);
 
 private:
 
 private:
+  struct LayoutData;
+  class WrapColumnFinder;
+
   qreal computeHeight();
 
   qreal computeHeight();
 
-  class WrapColumnFinder;
+  LayoutData *_layoutData;
 };
 
 };
 
+struct ContentsChatItem::LayoutData {
+  QTextLayout *layout;
 
 
+  LayoutData() { layout = 0; }
+  ~LayoutData() { delete layout; }
+};
 
 class ContentsChatItem::WrapColumnFinder {
 public:
 
 class ContentsChatItem::WrapColumnFinder {
 public:
@@ -156,8 +173,11 @@ private:
   qreal w;
 };
 
   qreal w;
 };
 
+/*************************************************************************************************/
+
+// Avoid circular include deps
 #include "chatline.h"
 #include "chatline.h"
-inline int ChatItem::row() const { return static_cast<ChatLine *>(parentItem())->row(); }
-inline QTextLayout *ChatItem::layout() const { return _layoutData->layout; }
+int ChatItem::row() const { return static_cast<ChatLine *>(parentItem())->row(); }
+QTextLayout *ContentsChatItem::layout() const { return _layoutData->layout; }
 
 #endif
 
 #endif
index 94820d6..a28f4e8 100644 (file)
@@ -111,6 +111,8 @@ void ChatLine::setHighlighted(bool highlighted) {
 }
 
 void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
 }
 
 void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
+  Q_UNUSED(option);
+  Q_UNUSED(widget);
   if(_selection & Highlighted) {
     painter->fillRect(boundingRect(), QBrush(QtUi::style()->highlightColor()));
   }
   if(_selection & Highlighted) {
     painter->fillRect(boundingRect(), QBrush(QtUi::style()->highlightColor()));
   }
index 44ddc69..591c20e 100644 (file)
@@ -76,7 +76,7 @@ void ChatViewSearchController::setSearchString(const QString &searchString) {
 void ChatViewSearchController::updateHighlights(bool reuse) {
   if(!_scene)
     return;
 void ChatViewSearchController::updateHighlights(bool reuse) {
   if(!_scene)
     return;
-  
+
   QAbstractItemModel *model = _scene->model();
   Q_ASSERT(model);
 
   QAbstractItemModel *model = _scene->model();
   Q_ASSERT(model);
 
@@ -130,10 +130,10 @@ void ChatViewSearchController::highlightLine(ChatLine *line) {
   QList<ChatItem *> checkItems;
   if(_searchSenders)
     checkItems << &(line->item(MessageModel::SenderColumn));
   QList<ChatItem *> checkItems;
   if(_searchSenders)
     checkItems << &(line->item(MessageModel::SenderColumn));
-  
+
   if(_searchMsgs)
     checkItems << &(line->item(MessageModel::ContentsColumn));
   if(_searchMsgs)
     checkItems << &(line->item(MessageModel::ContentsColumn));
-  
+
   foreach(ChatItem *item, checkItems) {
     foreach(QRectF wordRect, item->findWords(searchString(), caseSensitive())) {
       _highlightItems << new SearchHighlightItem(wordRect.adjusted(item->x(), 0, item->x(), 0), line);
   foreach(ChatItem *item, checkItems) {
     foreach(QRectF wordRect, item->findWords(searchString(), caseSensitive())) {
       _highlightItems << new SearchHighlightItem(wordRect.adjusted(item->x(), 0, item->x(), 0), line);
@@ -192,8 +192,6 @@ void ChatViewSearchController::setSearchOnlyRegularMsgs(bool searchOnlyRegularMs
   updateHighlights(searchOnlyRegularMsgs);
 }
 
   updateHighlights(searchOnlyRegularMsgs);
 }
 
-
-
 SearchHighlightItem::SearchHighlightItem(QRectF wordRect, QGraphicsItem *parent)
   : QGraphicsItem(parent),
     _boundingRect(QRectF(-wordRect.width() / 2, -wordRect.height() / 2, wordRect.width(), wordRect.height()))
 SearchHighlightItem::SearchHighlightItem(QRectF wordRect, QGraphicsItem *parent)
   : QGraphicsItem(parent),
     _boundingRect(QRectF(-wordRect.width() / 2, -wordRect.height() / 2, wordRect.width(), wordRect.height()))
@@ -203,8 +201,9 @@ SearchHighlightItem::SearchHighlightItem(QRectF wordRect, QGraphicsItem *parent)
 }
 
 void SearchHighlightItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
 }
 
 void SearchHighlightItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
+  Q_UNUSED(option);
   Q_UNUSED(widget);
   Q_UNUSED(widget);
-  
+
   painter->setPen(QPen(Qt::black, 1.5));
   //painter->setBrush(QColor(127, 133, 250));
   painter->setBrush(QColor(254, 237, 45));
   painter->setPen(QPen(Qt::black, 1.5));
   //painter->setBrush(QColor(127, 133, 250));
   painter->setBrush(QColor(254, 237, 45));