1 /***************************************************************************
2 * Copyright (C) 2005-07 by The Quassel Team *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
23 #include "chatwidget.h"
27 ChatWidget::ChatWidget(QWidget *parent) : QAbstractScrollArea(parent) {
28 scrollTimer = new QTimer(this);
29 scrollTimer->setSingleShot(false);
30 scrollTimer->setInterval(100);
31 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
32 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
33 setMinimumSize(QSize(400,400));
34 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
35 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
39 pointerPosition = QPoint(0,0);
40 connect(verticalScrollBar(), SIGNAL(actionTriggered(int)), this, SLOT(scrollBarAction(int)));
41 connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(scrollBarValChanged(int)));
44 void ChatWidget::init(QString netname, QString bufname) {
45 networkName = netname;
47 setBackgroundRole(QPalette::Base);
48 setFont(QFont("Fixed"));
53 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
54 //verticalScrollBar()->setPageStep(viewport()->height());
55 //verticalScrollBar()->setSingleStep(20);
56 //verticalScrollBar()->setMinimum(0);
57 //verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
59 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
60 setMouseTracking(true);
62 selectionMode = NoSelection;
63 connect(scrollTimer, SIGNAL(timeout()), this, SLOT(handleScrollTimer()));
66 ChatWidget::~ChatWidget() {
67 //qDebug() << "destroying chatwidget" << bufferName;
68 //foreach(ChatLine *l, lines) {
73 QSize ChatWidget::sizeHint() const {
78 void ChatWidget::adjustScrollBar() {
79 verticalScrollBar()->setPageStep(viewport()->height());
80 verticalScrollBar()->setSingleStep(20);
81 verticalScrollBar()->setMinimum(0);
82 verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
83 //qDebug() << height << viewport()->height() << verticalScrollBar()->pageStep();
84 //if(bottomLine < 0) {
85 // verticalScrollBar()->setValue(verticalScrollBar()->maximum());
87 //int bot = verticalScrollBar()->value() + viewport()->height(); //qDebug() << bottomLine;
88 //verticalScrollBar()->setValue(qMax(0, (int)ycoords[bottomLine+1] - viewport()->height()));
92 void ChatWidget::scrollBarValChanged(int val) {
94 if(val >= verticalScrollBar()->maximum()) bottomLine = -1;
96 int bot = val + viewport()->height();
97 int line = yToLineIdx(bot);
102 void ChatWidget::scrollBarAction(int action) {
104 case QScrollBar::SliderSingleStepAdd:
105 // More elaborate. But what with loooong lines?
106 // verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value() + viewport()->height()) + 1] - viewport()->height());
108 case QScrollBar::SliderSingleStepSub:
109 //verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value())]);
116 void ChatWidget::handleScrollTimer() {
117 if(mouseMode == MarkText || mouseMode == MarkLines) {
118 if(pointerPosition.y() > viewport()->height()) {
119 verticalScrollBar()->setValue(verticalScrollBar()->value() + pointerPosition.y() - viewport()->height());
120 handleMouseMoveEvent(QPoint(pointerPosition.x(), viewport()->height()));
121 } else if(pointerPosition.y() < 0) {
122 verticalScrollBar()->setValue(verticalScrollBar()->value() + pointerPosition.y());
123 handleMouseMoveEvent(QPoint(pointerPosition.x(), 0));
128 void ChatWidget::ensureVisible(int line) {
129 int top = verticalScrollBar()->value();
130 int bot = top + viewport()->height();
131 if(ycoords[line+1] > bot) {
132 verticalScrollBar()->setValue(qMax(0, (int)ycoords[line+1] - viewport()->height()));
133 } else if(ycoords[line] < top) {
134 verticalScrollBar()->setValue((int)ycoords[line]);
139 void ChatWidget::clear() {
143 void ChatWidget::prependMsg(AbstractUiMsg *msg) {
144 ChatLine *line = dynamic_cast<ChatLine*>(msg);
146 prependChatLine(line);
149 void ChatWidget::prependChatLine(ChatLine *line) {
150 qreal h = line->layout(tsWidth, senderWidth, textWidth);
151 for(int i = 1; i < ycoords.count(); i++) ycoords[i] += h;
152 ycoords.insert(1, h);
155 // Fix all variables containing line numbers
158 selectionStart ++; selectionEnd ++;
160 verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
161 viewport()->update();
164 void ChatWidget::prependChatLines(QList<ChatLine *> clist) {
165 QList<qreal> tmpy; tmpy.append(0);
167 foreach(ChatLine *l, clist) {
168 h += l->layout(tsWidth, senderWidth, textWidth);
171 ycoords.removeFirst();
172 for(int i = 0; i < ycoords.count(); i++) ycoords[i] += h;
173 ycoords = tmpy + ycoords;
174 lines = clist + lines;
176 // Fix all variables containing line numbers
177 int i = clist.count();
180 selectionStart += i; selectionEnd += i; //? selectionEnd += i;
181 //if(bottomLine >= 0) bottomLine += i;
183 //verticalScrollBar()->setPageStep(viewport()->height());
184 //verticalScrollBar()->setSingleStep(20);
185 //verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
186 verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
187 viewport()->update();
190 void ChatWidget::appendMsg(AbstractUiMsg *msg) {
191 ChatLine *line = dynamic_cast<ChatLine*>(msg);
193 appendChatLine(line);
196 void ChatWidget::appendChatLine(ChatLine *line) {
197 qreal h = line->layout(tsWidth, senderWidth, textWidth);
198 ycoords.append(h + ycoords[ycoords.count() - 1]);
200 bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
202 if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
204 viewport()->update();
208 void ChatWidget::appendChatLines(QList<ChatLine *> list) {
209 foreach(ChatLine *line, list) {
210 qreal h = line->layout(tsWidth, senderWidth, textWidth);
211 ycoords.append(h + ycoords[ycoords.count() - 1]);
215 bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
217 if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
218 viewport()->update();
221 void ChatWidget::setContents(QList<ChatLine *> list) {
226 appendChatLines(list);
229 //!\brief Computes the different x position vars for given tsWidth and senderWidth.
230 void ChatWidget::computePositions() {
231 senderX = tsWidth + Style::sepTsSender();
232 textX = senderX + senderWidth + Style::sepSenderText();
233 tsGrabPos = tsWidth + (int)Style::sepTsSender()/2;
234 senderGrabPos = senderX + senderWidth + (int)Style::sepSenderText()/2;
235 textWidth = viewport()->size().width() - textX;
238 void ChatWidget::resizeEvent(QResizeEvent *event) {
239 //qDebug() << bufferName << isVisible() << event->size() << event->oldSize();
240 /*if(event->oldSize().isValid())*/
241 //contents->setWidth(event->size().width());
242 //setAlignment(Qt::AlignBottom);
243 if(event->size().width() != event->oldSize().width()) {
248 //qDebug() << viewport()->size() << viewport()->height();
249 //QAbstractScrollArea::resizeEvent(event);
250 //qDebug() << viewport()->size() << viewport()->geometry();
253 void ChatWidget::paintEvent(QPaintEvent *event) {
254 QPainter painter(viewport());
256 //qDebug() << verticalScrollBar()->value();
257 painter.translate(0, -verticalScrollBar()->value());
258 int top = event->rect().top() + verticalScrollBar()->value();
259 int bot = top + event->rect().height();
260 int idx = yToLineIdx(top);
262 for(int i = idx; i < lines.count() ; i++) {
263 lines[i]->draw(&painter, QPointF(0, ycoords[i]));
264 if(ycoords[i+1] > bot) return;
268 //!\brief Layout the widget.
269 void ChatWidget::layout() {
270 // TODO fix scrollbars
271 //int botLine = yToLineIdx(verticalScrollBar()->value() +
273 for(int i = 0; i < lines.count(); i++) {
274 qreal h = lines[i]->layout(tsWidth, senderWidth, textWidth);
275 ycoords[i+1] = h + ycoords[i];
277 height = ycoords[ycoords.count()-1];
279 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
280 viewport()->update();
283 int ChatWidget::yToLineIdx(qreal y) {
284 if(y >= ycoords[ycoords.count()-1]) ycoords.count()-1;
285 if(ycoords.count() <= 1) return 0;
287 int oidx = ycoords.count() - 1;
290 if(uidx == oidx - 1) return uidx;
291 idx = (uidx + oidx) / 2;
292 if(ycoords[idx] > y) oidx = idx;
297 void ChatWidget::mousePressEvent(QMouseEvent *event) {
298 if(lines.isEmpty()) return;
299 QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
300 if(event->button() == Qt::LeftButton) {
302 dragStartMode = Normal;
305 if(mousePos == OverTsSep) {
306 dragStartMode = DragTsSep;
307 setCursor(Qt::ClosedHandCursor);
308 } else if(mousePos == OverTextSep) {
309 dragStartMode = DragTextSep;
310 setCursor(Qt::ClosedHandCursor);
312 dragStartLine = yToLineIdx(pos.y());
313 dragStartCursor = lines[dragStartLine]->posToCursor(QPointF(pos.x(), pos.y()-ycoords[dragStartLine]));
321 void ChatWidget::mouseDoubleClickEvent(QMouseEvent *event) {
327 void ChatWidget::mouseReleaseEvent(QMouseEvent *event) {
328 //QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
330 if(event->button() == Qt::LeftButton) {
331 dragStartPos = QPoint();
332 if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
333 else setCursor(Qt::ArrowCursor);
342 selectionMode = TextSelected;
343 selectionLine = dragStartLine;
344 selectionStart = qMin(dragStartCursor, curCursor);
345 selectionEnd = qMax(dragStartCursor, curCursor);
346 // TODO Make X11SelectionMode configurable!
347 QApplication::clipboard()->setText(selectionToString());
351 selectionMode = LinesSelected;
352 selectionStart = qMin(dragStartLine, curLine);
353 selectionEnd = qMax(dragStartLine, curLine);
354 // TODO Make X11SelectionMode configurable!
355 QApplication::clipboard()->setText(selectionToString());
363 //!\brief React to mouse movements over the ChatWidget.
364 /** This is called by Qt whenever the mouse moves. Here we have to do most of the mouse handling,
365 * such as changing column widths, marking text or initiating drag & drop.
367 void ChatWidget::mouseMoveEvent(QMouseEvent *event) {
368 QPoint pos = event->pos(); pointerPosition = pos;
369 // Scroll if mouse pointer leaves widget while dragging
370 if((mouseMode == MarkText || mouseMode == MarkLines) && (pos.y() > viewport()->height() || pos.y() < 0)) {
371 if(!scrollTimer->isActive()) {
372 scrollTimer->start();
375 if(scrollTimer->isActive()) {
379 handleMouseMoveEvent(pos);
382 void ChatWidget::handleMouseMoveEvent(const QPoint &_pos) {
384 if(lines.count() <= 0) return;
385 // Set some basic properties of the current position
386 QPoint pos = _pos + QPoint(0, verticalScrollBar()->value());
389 MousePos oldpos = mousePos;
390 if(x >= tsGrabPos - 3 && x <= tsGrabPos + 3) mousePos = OverTsSep;
391 else if(x >= senderGrabPos - 3 && x <= senderGrabPos + 3) mousePos = OverTextSep;
392 else mousePos = None;
394 // Pass 1: Do whatever we can before switching mouse mode (if at all).
396 // No special mode. Set mouse cursor if appropriate.
399 //if(oldpos != mousePos) {
400 if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
402 int l = yToLineIdx(y);
403 int c = lines[l]->posToCursor(QPointF(x, y - ycoords[l]));
404 if(c >= 0 && lines[l]->isUrl(c)) {
405 setCursor(Qt::PointingHandCursor);
407 setCursor(Qt::ArrowCursor);
412 // Left button pressed. Might initiate marking or drag & drop if we moved past the drag distance.
414 if(!dragStartPos.isNull() && (dragStartPos - pos).manhattanLength() >= QApplication::startDragDistance()) {
415 // Moving a column separator?
416 if(dragStartMode == DragTsSep) mouseMode = DragTsSep;
417 else if(dragStartMode == DragTextSep) mouseMode = DragTextSep;
418 // Nope. Check if we are over a selection to start drag & drop.
419 else if(dragStartMode == Normal) {
420 bool dragdrop = false;
421 if(selectionMode == TextSelected) {
422 int l = yToLineIdx(y);
423 if(selectionLine == l) {
424 int p = lines[l]->posToCursor(QPointF(x, y - ycoords[l]));
425 if(p >= selectionStart && p <= selectionEnd) dragdrop = true;
427 } else if(selectionMode == LinesSelected) {
428 int l = yToLineIdx(y);
429 if(l >= selectionStart && l <= selectionEnd) dragdrop = true;
431 // Ok, so just start drag & drop if appropriate.
433 QDrag *drag = new QDrag(this);
434 QMimeData *mimeData = new QMimeData;
435 mimeData->setText(selectionToString());
436 drag->setMimeData(mimeData);
439 // Otherwise, clear the selection and start text marking!
441 setCursor(Qt::ArrowCursor);
443 if(dragStartCursor < 0) { mouseMode = MarkLines; curLine = -1; }
444 else mouseMode = MarkText;
454 // Pass 2: Some mouse modes need work after being set...
455 if(mouseMode == DragTsSep && x < size().width() - Style::sepSenderText() - senderWidth - 10) {
456 // Drag first column separator
457 int foo = Style::sepTsSender()/2;
458 tsWidth = qMax(x, foo) - foo;
461 } else if(mouseMode == DragTextSep && x < size().width() - 10) {
462 // Drag second column separator
463 int foo = tsWidth + Style::sepTsSender() + Style::sepSenderText()/2;
464 senderWidth = qMax(x, foo) - foo;
467 } else if(mouseMode == MarkText) {
468 // Change currently marked text
469 curLine = yToLineIdx(y);
470 int c = lines[curLine]->posToCursor(QPointF(x, y - ycoords[curLine]));
471 if(curLine == dragStartLine && c >= 0) {
474 lines[curLine]->setSelection(ChatLine::Partial, dragStartCursor, c);
475 viewport()->update();
478 mouseMode = MarkLines;
479 selectionStart = qMin(curLine, dragStartLine); selectionEnd = qMax(curLine, dragStartLine);
480 for(int i = selectionStart; i <= selectionEnd; i++) lines[i]->setSelection(ChatLine::Full);
481 viewport()->update();
483 } else if(mouseMode == MarkLines) {
485 int l = yToLineIdx(y);
487 selectionStart = qMin(l, dragStartLine); selectionEnd = qMax(l, dragStartLine);
489 Q_ASSERT(selectionStart == selectionEnd);
490 lines[l]->setSelection(ChatLine::Full);
492 if(curLine < selectionStart) {
493 for(int i = curLine; i < selectionStart; i++) lines[i]->setSelection(ChatLine::None);
494 } else if(curLine > selectionEnd) {
495 for(int i = selectionEnd+1; i <= curLine; i++) lines[i]->setSelection(ChatLine::None);
496 } else if(selectionStart < curLine && l < curLine) {
497 for(int i = selectionStart; i < curLine; i++) lines[i]->setSelection(ChatLine::Full);
498 } else if(curLine < selectionEnd && l > curLine) {
499 for(int i = curLine+1; i <= selectionEnd; i++) lines[i]->setSelection(ChatLine::Full);
504 viewport()->update();
509 //!\brief Clear current text selection.
510 void ChatWidget::clearSelection() {
511 if(selectionMode == TextSelected) {
512 lines[selectionLine]->setSelection(ChatLine::None);
513 } else if(selectionMode == LinesSelected) {
514 for(int i = selectionStart; i <= selectionEnd; i++) {
515 lines[i]->setSelection(ChatLine::None);
518 selectionMode = NoSelection;
519 viewport()->update();
522 //!\brief Convert current selection to human-readable string.
523 QString ChatWidget::selectionToString() {
524 //TODO Make selection format configurable!
525 if(selectionMode == NoSelection) return "";
526 if(selectionMode == LinesSelected) {
528 for(int l = selectionStart; l <= selectionEnd; l++) {
529 result += QString("[%1] %2 %3\n").arg(lines[l]->timeStamp().toLocalTime().toString("hh:mm:ss"))
530 .arg(lines[l]->sender()).arg(lines[l]->text());
534 // selectionMode == TextSelected
535 return lines[selectionLine]->text().mid(selectionStart, selectionEnd - selectionStart);
538 /************************************************************************************/
541 /******************************************************************************************************************/