Make ChatLine/-Items and ChatScene honor ChatView width.
authorManuel Nickschas <sputnick@quassel-irc.org>
Mon, 19 May 2008 23:15:18 +0000 (23:15 +0000)
committerManuel Nickschas <sputnick@quassel-irc.org>
Mon, 19 May 2008 23:15:18 +0000 (23:15 +0000)
The items now fill out the available space and automatically re-adjust to size changes.

src/qtui/chatitem.cpp
src/qtui/chatitem.h
src/qtui/chatline.cpp
src/qtui/chatline.h
src/qtui/chatscene.cpp
src/qtui/chatscene.h
src/qtui/chatview.cpp
src/qtui/chatview.h

index 7c0cc9a..1202b93 100644 (file)
 #include "chatitem.h"
 
 ChatItem::ChatItem(const QPersistentModelIndex &index_, QGraphicsItem *parent) : QGraphicsItem(parent), _index(index_) {
-  //if(_wrapMode == WordWrap) {
-  //  setFlags(QGraphicsItem::ItemClipsToShape, true);
-  //}
-  
+  _width = _height = 0;
 }
 
 ChatItem::~ChatItem() {
@@ -47,23 +44,27 @@ QVariant ChatItem::data(int role) const {
 }
 
 QRectF ChatItem::boundingRect() const {
-  return QRectF(0, 0, 500,20);
+  return QRectF(0, 0, _width, _height);
 }
 
 void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
   Q_UNUSED(option); Q_UNUSED(widget);
 
-  painter->drawRect(boundingRect());
   painter->drawText(boundingRect(), data(MessageModel::DisplayRole).toString());
+  painter->setPen(Qt::DotLine);
+  painter->drawRect(boundingRect());
 }
 
 
-/*
-void ChatItem::setWidth(int w) {
+
+int ChatItem::setWidth(int w) {
   _width = w;
-  layout();
+  _height = 20; // FIXME
+  return _height;
 }
 
+/*
+
 void ChatItem::setTextOption(const QTextOption &option) {
   _textOption = option;
   layout();
index b58dc03..e41b7b7 100644 (file)
@@ -49,14 +49,16 @@ class ChatItem : public QGraphicsItem {
     //QTextOption textOption() const;
     //void setTextOption(const QTextOption &option);
 
-    //void setWidth(int width);
+    // returns height
+    int setWidth(int width);
     //virtual void layout();
 
   protected:
     //void mouseMoveEvent ( QGraphicsSceneMouseEvent * event );
 
   private:
-    //int _width;
+    int _width;
+    int _height;
     //QTextLayout _layout;
     //QTextOption _textOption;
     QPersistentModelIndex _index;
index 71ed2c8..921258b 100644 (file)
@@ -32,18 +32,31 @@ ChatLine::ChatLine(const QModelIndex &index, QGraphicsItem *parent) : QGraphicsI
   _senderItem = new ChatItem(QPersistentModelIndex(index.sibling(index.row(), ChatLineModel::SenderColumn)), this);
   _contentsItem = new ChatItem(QPersistentModelIndex(index.sibling(index.row(), ChatLineModel::ContentsColumn)), this);
 
-  _senderItem->setPos(80, 0);
-  _contentsItem->setPos(160, 0);
+  _timestampItem->setPos(0,0);
 }
 
 ChatLine::~ChatLine() {
-
+  delete _timestampItem;
+  delete _senderItem;
+  delete _contentsItem;
 }
 
+// FIXME make more efficient by caching width/height
 QRectF ChatLine::boundingRect () const {
   return childrenBoundingRect();
 }
 
+int ChatLine::setColumnWidths(int ts, int sender, int contents) {
+  _timestampItem->setWidth(ts);
+  _senderItem->setWidth(sender);
+  int h = _contentsItem->setWidth(contents);
+
+  _senderItem->setPos(ts, 0);
+  _contentsItem->setPos(ts + sender, 0);
+
+  return h;
+}
+
 void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
 
 }
index 8531f39..a7db7e4 100644 (file)
@@ -37,7 +37,8 @@ class ChatLine : public QGraphicsItem {
     virtual void paint (QPainter * painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
     //void layout();
 
-    //void setColumnWidths(int tsColWidth, int senderColWidth, int textColWidth);
+    // returns height
+    int setColumnWidths(int tsColWidth, int senderColWidth, int textColWidth);
 
     //void myMousePressEvent ( QGraphicsSceneMouseEvent * event ) { qDebug() << "press"; mousePressEvent(event); }
 
index 85a1787..d51050f 100644 (file)
@@ -31,11 +31,9 @@ ChatScene::ChatScene(MessageModel *model, QObject *parent) : QGraphicsScene(pare
   connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)));
   for(int i = 0; i < model->rowCount(); i++) {
     ChatLine *line = new ChatLine(model->index(i, 0));
+    _lines.append(line);
     addItem(line);
-    line->setPos(30, i*line->boundingRect().height());
   }
-
-  
 }
 
 ChatScene::~ChatScene() {
@@ -43,6 +41,15 @@ ChatScene::~ChatScene() {
 
 }
 
+void ChatScene::setWidth(int w) {
+  _width = w;
+  int h = 0;
+  foreach(ChatLine *line, _lines) {
+    line->setPos(0, h);
+    h += line->setColumnWidths(60, 80, w - 60 - 80);
+  }
+  setSceneRect(QRectF(0, 0, w, h));
+}
 
 void ChatScene::mousePressEvent ( QGraphicsSceneMouseEvent * mouseEvent ) {
   /*
index 1ce9304..8122872 100644 (file)
@@ -42,16 +42,16 @@ class ChatScene : public QGraphicsScene {
     inline MessageModel *model() const { return _model; }
 
   public slots:
+    void setWidth(int);
 
   protected slots:
 
     void mousePressEvent ( QGraphicsSceneMouseEvent * mouseEvent );
 
   private:
-    //Buffer *_buffer;
-    //QList<ChatLine*> _lines;
+    int _width;
     MessageModel *_model;
-    QList<ChatItem *> _items;
+    QList<ChatLine *> _lines;
 
 };
 
index f76cf2a..8551a83 100644 (file)
 #include "quasselui.h"
 
 ChatView::ChatView(Buffer *buf, QWidget *parent) : QGraphicsView(parent), AbstractChatView() {
+  setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
   _scene = new ChatScene(Client::messageModel(), this);
+  _scene->setWidth(width());
   setScene(_scene);
 
-  //QGraphicsTextItem *item = scene()->addText(buf->bufferInfo().bufferName());
 
 }
 
@@ -45,6 +46,10 @@ ChatScene *ChatView::scene() const {
   return _scene;
 }
 
+void ChatView::resizeEvent(QResizeEvent *event) {
+  scene()->setWidth(event->size().width());
+
+}
 
 void ChatView::clear()
 {
index 7ae7c8d..9799142 100644 (file)
@@ -53,6 +53,9 @@ class ChatView : public QGraphicsView, public AbstractChatView {
 
     void setContents(const QList<AbstractUiMsg *> &);
 
+  protected:
+    virtual void resizeEvent(QResizeEvent *event);
+
   private:
     ChatScene *_scene;
 };