Add selection (and highlight) display to ChatLine
authorManuel Nickschas <sputnick@quassel-irc.org>
Wed, 30 Jul 2008 22:56:22 +0000 (00:56 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Sat, 2 Aug 2008 13:17:11 +0000 (15:17 +0200)
src/qtui/chatline.cpp
src/qtui/chatline.h

index 237fad6..6e4982a 100644 (file)
@@ -34,6 +34,7 @@ ChatLine::ChatLine(const QModelIndex &index, QGraphicsItem *parent) : QGraphicsI
 
   _timestampItem->setPos(0,0);
   _width = _height = 0;
+  _selection = 0;
 }
 
 ChatLine::~ChatLine() {
@@ -47,6 +48,15 @@ QRectF ChatLine::boundingRect () const {
   return QRectF(0, 0, _width, _height);
 }
 
+ChatItem *ChatLine::item(ChatLineModel::ColumnType column) const {
+  switch(column) {
+    case ChatLineModel::TimestampColumn: return _timestampItem;
+    case ChatLineModel::SenderColumn: return _senderItem;
+    case ChatLineModel::ContentsColumn: return _contentsItem;
+    default: return 0;
+  }
+}
+
 qreal ChatLine::setGeometry(qreal width, qreal firstHandlePos, qreal secondHandlePos) {
   if(width != _width) prepareGeometryChange();
   qreal firstsep = QtUi::style()->firstColumnSeparator()/2;
@@ -63,6 +73,32 @@ qreal ChatLine::setGeometry(qreal width, qreal firstHandlePos, qreal secondHandl
   return _height;
 }
 
-void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
+void ChatLine::setSelected(bool selected, ChatLineModel::ColumnType minColumn) {
+  if(selected) {
+    _selection = (_selection & 0x80) | 0x40 | minColumn;
+    for(int i = 0; i < minColumn; i++) item((ChatLineModel::ColumnType)i)->clearSelection();
+    for(int i = minColumn; i <= ChatLineModel::ContentsColumn; i++) item((ChatLineModel::ColumnType)i)->setFullSelection();
+  }
+  else {
+    _selection &= 0x80;
+    for(int i = 0; i <= ChatLineModel::ContentsColumn; i++) item((ChatLineModel::ColumnType)i)->clearSelection();
+  }
+  update();
+}
 
+void ChatLine::setHighlighted(bool highlighted) {
+  if(highlighted) _selection |= 0x80;
+  else _selection &= 0x7f;
+  update();
+}
+
+void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
+  if(_selection & Highlighted) {
+    painter->fillRect(boundingRect(), QBrush(QtUi::style()->highlightColor()));
+  }
+  if(_selection & Selected) {
+    qreal left = item((ChatLineModel::ColumnType)(_selection & 0x3f))->x();
+    QRectF selectRect(left, 0, width() - left, height());
+    painter->fillRect(selectRect, QApplication::palette().brush(QPalette::Highlight));
+  }
 }
index c7ac49b..3649c54 100644 (file)
@@ -23,7 +23,7 @@
 
 #include <QGraphicsItem>
 
-#include "messagemodel.h"
+#include "chatlinemodel.h"
 
 class ChatItem;
 
@@ -34,23 +34,25 @@ class ChatLine : public QGraphicsItem {
     virtual ~ChatLine();
 
     virtual QRectF boundingRect () const;
-    inline int width() const { return _width; }
-    inline int height() const { return _height; }
+    inline qreal width() const { return _width; }
+    inline qreal height() const { return _height; }
+    ChatItem *item(ChatLineModel::ColumnType) const;
 
     virtual void paint (QPainter * painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
-    //void layout();
 
     // returns height
     qreal setGeometry(qreal width, qreal firstColPos, qreal secondColPos);
-
-    //void myMousePressEvent ( QGraphicsSceneMouseEvent * event ) { qDebug() << "press"; mousePressEvent(event); }
+    void setSelected(bool selected, ChatLineModel::ColumnType minColumn = ChatLineModel::ContentsColumn);
+    void setHighlighted(bool highlighted);
 
   protected:
-    //bool sceneEvent ( QEvent * event );
 
   private:
     ChatItem *_timestampItem, *_senderItem, *_contentsItem;
-    int _width, _height;
+    qreal _width, _height;
+
+    enum { Selected = 0x40, Highlighted = 0x80 };
+    quint8 _selection;  // save space, so we put both the col and the flags into one byte
 };
 
 #endif