~ubuntu-branches/ubuntu/saucy/quassel/saucy-proposed

« back to all changes in this revision

Viewing changes to src/qtui/chatitem.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2010-02-17 12:49:50 UTC
  • mto: This revision was merged to the branch mainline in revision 59.
  • Revision ID: james.westby@ubuntu.com-20100217124950-v9hajw5d2xa6fszn
Tags: upstream-0.6~beta1
ImportĀ upstreamĀ versionĀ 0.6~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/***************************************************************************
2
 
 *   Copyright (C) 2005-09 by the Quassel Project                          *
 
2
 *   Copyright (C) 2005-2010 by the Quassel Project                        *
3
3
 *   devel@quassel-irc.org                                                 *
4
4
 *                                                                         *
5
5
 *   This program is free software; you can redistribute it and/or modify  *
31
31
#include "buffermodel.h"
32
32
#include "bufferview.h"
33
33
#include "chatitem.h"
 
34
#include "chatline.h"
34
35
#include "chatlinemodel.h"
35
36
#include "contextmenuactionprovider.h"
36
37
#include "iconloader.h"
38
39
#include "qtui.h"
39
40
#include "qtuistyle.h"
40
41
 
41
 
ChatItem::ChatItem(const qreal &width, const qreal &height, const QPointF &pos, QGraphicsItem *parent)
42
 
  : QGraphicsItem(parent),
43
 
    _boundingRect(0, 0, width, height),
44
 
    _selectionMode(NoSelection),
45
 
    _selectionStart(-1)
 
42
ChatItem::ChatItem(const QRectF &boundingRect, ChatLine *parent)
 
43
: _parent(parent),
 
44
  _boundingRect(boundingRect),
 
45
  _selectionMode(NoSelection),
 
46
  _selectionStart(-1)
46
47
{
47
 
  setAcceptHoverEvents(true);
48
 
  setZValue(20);
49
 
  setPos(pos);
 
48
 
50
49
}
51
50
 
52
51
QVariant ChatItem::data(int role) const {
58
57
  return model()->data(index, role);
59
58
}
60
59
 
61
 
qint16 ChatItem::posToCursor(const QPointF &pos) const {
 
60
qint16 ChatItem::posToCursor(const QPointF &posInLine) const {
 
61
  QPointF pos = mapFromLine(posInLine);
62
62
  if(pos.y() > height()) return data(MessageModel::DisplayRole).toString().length();
63
63
  if(pos.y() < 0) return 0;
64
64
 
103
103
}
104
104
 
105
105
void ChatItem::paintBackground(QPainter *painter) {
106
 
  painter->setClipRect(boundingRect()); // no idea why QGraphicsItem clipping won't work
107
 
 
108
106
  QVariant bgBrush;
109
107
  if(_selectionMode == FullSelection)
110
108
    bgBrush = data(ChatLineModel::SelectedBackgroundRole);
118
116
//       This is a deliberate trade-off. (-> selectFmt creation, data() call)
119
117
void ChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
120
118
  Q_UNUSED(option); Q_UNUSED(widget);
 
119
  painter->save();
 
120
  painter->setClipRect(boundingRect());
121
121
  paintBackground(painter);
122
122
 
123
123
  QTextLayout layout;
124
124
  initLayout(&layout);
125
 
  layout.draw(painter, QPointF(0,0), additionalFormats(), boundingRect());
 
125
  layout.draw(painter, pos(), additionalFormats(), boundingRect());
126
126
 
127
127
  //  layout()->draw(painter, QPointF(0,0), formats, boundingRect());
128
128
 
152
152
//   }
153
153
  // 3) draw bounding rect
154
154
//   painter->drawRect(_boundingRect.adjusted(0, 0, -1, -1));
 
155
 
 
156
  painter->restore();
155
157
}
156
158
 
157
159
void ChatItem::overlayFormat(UiStyle::FormatList &fmtList, int start, int end, quint32 overlayFmt) const {
229
231
  _selectionMode = mode;
230
232
  _selectionStart = start;
231
233
  _selectionEnd = end;
232
 
  update();
 
234
  chatLine()->update();
233
235
}
234
236
 
235
237
void ChatItem::setFullSelection() {
236
238
  if(_selectionMode != FullSelection) {
237
239
    _selectionMode = FullSelection;
238
 
    update();
 
240
    chatLine()->update();
239
241
  }
240
242
}
241
243
 
242
244
void ChatItem::clearSelection() {
243
245
  if(_selectionMode != NoSelection) {
244
246
    _selectionMode = NoSelection;
245
 
    update();
 
247
    chatLine()->update();
246
248
  }
247
249
}
248
250
 
249
251
void ChatItem::continueSelecting(const QPointF &pos) {
250
252
  _selectionMode = PartialSelection;
251
253
  _selectionEnd = posToCursor(pos);
252
 
  update();
 
254
  chatLine()->update();
253
255
}
254
256
 
255
257
bool ChatItem::isPosOverSelection(const QPointF &pos) const {
296
298
    chatScene()->setSelectingItem(this);
297
299
    _selectionStart = _selectionEnd = posToCursor(pos);
298
300
    _selectionMode = NoSelection; // will be set to PartialSelection by mouseMoveEvent
299
 
    update();
 
301
    chatLine()->update();
300
302
  }
301
303
}
302
304
 
303
305
void ChatItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
304
306
  if(event->buttons() == Qt::LeftButton) {
305
 
    if(contains(event->pos())) {
 
307
    if(boundingRect().contains(event->pos())) {
306
308
      qint16 end = posToCursor(event->pos());
307
309
      if(end != _selectionEnd) {
308
310
        _selectionEnd = end;
309
311
        _selectionMode = (_selectionStart != _selectionEnd ? PartialSelection : NoSelection);
310
 
        update();
 
312
        chatLine()->update();
311
313
      }
312
314
    } else {
313
315
      setFullSelection();
346
348
 
347
349
void SenderChatItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
348
350
  Q_UNUSED(option); Q_UNUSED(widget);
 
351
  painter->save();
 
352
  painter->setClipRect(boundingRect());
349
353
  paintBackground(painter);
350
354
 
351
355
  QTextLayout layout;
381
385
      gradient.setColorAt(0, Qt::white);
382
386
      gradient.setColorAt(1, Qt::black);
383
387
    }
384
 
    maskPainter.fillRect(boundingRect(), gradient);
 
388
    maskPainter.fillRect(0, 0, pixmap.width(), pixmap.height(), gradient);
385
389
    pixmap.setAlphaChannel(mask);
386
 
    painter->drawPixmap(0, 0, pixmap);
 
390
    painter->drawPixmap(pos(), pixmap);
387
391
  } else {
388
 
    layout.draw(painter, QPointF(0,0), additionalFormats(), boundingRect());
389
 
  }
 
392
    layout.draw(painter, pos(), additionalFormats(), boundingRect());
 
393
  }
 
394
  painter->restore();
 
395
}
 
396
 
 
397
void SenderChatItem::handleClick(const QPointF &pos, ChatScene::ClickMode clickMode) {
 
398
  if(clickMode == ChatScene::DoubleClick) {
 
399
    BufferInfo curBufInfo = Client::networkModel()->bufferInfo(data(MessageModel::BufferIdRole).value<BufferId>());
 
400
    QString nick = data(MessageModel::EditRole).toString();
 
401
    // check if the nick is a valid ircUser
 
402
    if(!nick.isEmpty() && Client::network(curBufInfo.networkId())->ircUser(nick))
 
403
      Client::bufferModel()->switchToOrStartQuery(curBufInfo.networkId(), nick);
 
404
  }
 
405
  else
 
406
    ChatItem::handleClick(pos, clickMode);
390
407
}
391
408
 
392
409
// ************************************************************
395
412
 
396
413
ContentsChatItem::ActionProxy ContentsChatItem::_actionProxy;
397
414
 
398
 
ContentsChatItem::ContentsChatItem(const qreal &width, const QPointF &pos, QGraphicsItem *parent)
399
 
  : ChatItem(0, 0, pos, parent),
 
415
ContentsChatItem::ContentsChatItem(const QPointF &pos, const qreal &width, ChatLine *parent)
 
416
  : ChatItem(QRectF(pos, QSizeF(width, 0)), parent),
400
417
    _data(0)
401
418
{
 
419
  setPos(pos);
402
420
  setGeometryByWidth(width);
403
421
}
404
422
 
431
449
  delete _data;
432
450
  _data = 0;
433
451
 
434
 
  if(w != width() || h != height()) {
435
 
    prepareGeometryChange();
 
452
  if(w != width() || h != height())
436
453
    setGeometry(w, h);
437
 
  }
 
454
 
438
455
  return h;
439
456
}
440
457
 
508
525
void ContentsChatItem::endHoverMode() {
509
526
  if(privateData()) {
510
527
    if(privateData()->currentClickable.isValid()) {
511
 
      setCursor(Qt::ArrowCursor);
 
528
      chatLine()->setCursor(Qt::ArrowCursor);
512
529
      privateData()->currentClickable = Clickable();
513
530
    }
514
531
    clearWebPreview();
515
 
    update();
 
532
    chatLine()->update();
516
533
  }
517
534
}
518
535
 
542
559
      setSelectionStart(start);
543
560
      setSelectionEnd(end);
544
561
    }
545
 
    update();
 
562
    chatLine()->update();
546
563
  } else if(clickMode == ChatScene::TripleClick) {
547
564
    setSelection(PartialSelection, 0, data(ChatLineModel::DisplayRole).toString().length());
548
565
  }
575
592
        onClickable = true;
576
593
    }
577
594
    if(onClickable) {
578
 
      setCursor(Qt::PointingHandCursor);
 
595
      chatLine()->setCursor(Qt::PointingHandCursor);
579
596
      privateData()->currentClickable = click;
580
 
      update();
 
597
      chatLine()->update();
581
598
      return;
582
599
    }
583
600
  }
635
652
  qreal height = line.height();
636
653
  qreal y = height * line.lineNumber();
637
654
 
638
 
  QPointF topLeft = scenePos() + QPointF(x, y);
 
655
  QPointF topLeft = mapToScene(pos()) + QPointF(x, y);
639
656
  QRectF urlRect = QRectF(topLeft.x(), topLeft.y(), width, height);
640
657
 
641
658
  QString urlstr = data(ChatLineModel::DisplayRole).toString().mid(click.start(), click.length());