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 //setAutoFillBackground(false);
30 //palette.setColor(backgroundRole(), QColor(0, 0, 0, 50));
31 //setPalette(palette);
32 scrollTimer = new QTimer(this);
33 scrollTimer->setSingleShot(false);
34 scrollTimer->setInterval(100);
35 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
36 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
37 setMinimumSize(QSize(400,400));
38 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
39 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
43 pointerPosition = QPoint(0,0);
44 connect(verticalScrollBar(), SIGNAL(actionTriggered(int)), this, SLOT(scrollBarAction(int)));
45 connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(scrollBarValChanged(int)));
48 void ChatWidget::init(QString netname, QString bufname) {
49 networkName = netname;
51 setBackgroundRole(QPalette::Base);
52 setFont(QFont("Fixed"));
57 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
58 //verticalScrollBar()->setPageStep(viewport()->height());
59 //verticalScrollBar()->setSingleStep(20);
60 //verticalScrollBar()->setMinimum(0);
61 //verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
63 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
64 setMouseTracking(true);
66 selectionMode = NoSelection;
67 connect(scrollTimer, SIGNAL(timeout()), this, SLOT(handleScrollTimer()));
70 ChatWidget::~ChatWidget() {
71 //qDebug() << "destroying chatwidget" << bufferName;
72 //foreach(ChatLine *l, lines) {
77 QSize ChatWidget::sizeHint() const {
82 void ChatWidget::adjustScrollBar() {
83 verticalScrollBar()->setPageStep(viewport()->height());
84 verticalScrollBar()->setSingleStep(20);
85 verticalScrollBar()->setMinimum(0);
86 verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
87 //qDebug() << height << viewport()->height() << verticalScrollBar()->pageStep();
88 //if(bottomLine < 0) {
89 // verticalScrollBar()->setValue(verticalScrollBar()->maximum());
91 //int bot = verticalScrollBar()->value() + viewport()->height(); //qDebug() << bottomLine;
92 //verticalScrollBar()->setValue(qMax(0, (int)ycoords[bottomLine+1] - viewport()->height()));
96 void ChatWidget::scrollBarValChanged(int /*val*/) {
98 if(val >= verticalScrollBar()->maximum()) bottomLine = -1;
100 int bot = val + viewport()->height();
101 int line = yToLineIdx(bot);
107 void ChatWidget::scrollBarAction(int action) {
109 case QScrollBar::SliderSingleStepAdd:
110 // More elaborate. But what with loooong lines?
111 // verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value() + viewport()->height()) + 1] - viewport()->height());
113 case QScrollBar::SliderSingleStepSub:
114 //verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value())]);
121 void ChatWidget::handleScrollTimer() {
122 if(mouseMode == MarkText || mouseMode == MarkLines) {
123 if(pointerPosition.y() > viewport()->height()) {
124 verticalScrollBar()->setValue(verticalScrollBar()->value() + pointerPosition.y() - viewport()->height());
125 handleMouseMoveEvent(QPoint(pointerPosition.x(), viewport()->height()));
126 } else if(pointerPosition.y() < 0) {
127 verticalScrollBar()->setValue(verticalScrollBar()->value() + pointerPosition.y());
128 handleMouseMoveEvent(QPoint(pointerPosition.x(), 0));
133 void ChatWidget::ensureVisible(int line) {
134 int top = verticalScrollBar()->value();
135 int bot = top + viewport()->height();
136 if(ycoords[line+1] > bot) {
137 verticalScrollBar()->setValue(qMax(0, (int)ycoords[line+1] - viewport()->height()));
138 } else if(ycoords[line] < top) {
139 verticalScrollBar()->setValue((int)ycoords[line]);
144 void ChatWidget::clear() {
148 void ChatWidget::prependMsg(AbstractUiMsg *msg) {
149 ChatLine *line = dynamic_cast<ChatLine*>(msg);
151 prependChatLine(line);
154 void ChatWidget::prependChatLine(ChatLine *line) {
155 qreal h = line->layout(tsWidth, senderWidth, textWidth);
156 for(int i = 1; i < ycoords.count(); i++) ycoords[i] += h;
157 ycoords.insert(1, h);
160 // Fix all variables containing line numbers
163 selectionStart ++; selectionEnd ++;
165 verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
166 viewport()->update();
169 void ChatWidget::prependChatLines(QList<ChatLine *> clist) {
170 QList<qreal> tmpy; tmpy.append(0);
172 foreach(ChatLine *l, clist) {
173 h += l->layout(tsWidth, senderWidth, textWidth);
176 ycoords.removeFirst();
177 for(int i = 0; i < ycoords.count(); i++) ycoords[i] += h;
178 ycoords = tmpy + ycoords;
179 lines = clist + lines;
181 // Fix all variables containing line numbers
182 int i = clist.count();
185 selectionStart += i; selectionEnd += i; //? selectionEnd += i;
186 //if(bottomLine >= 0) bottomLine += i;
188 //verticalScrollBar()->setPageStep(viewport()->height());
189 //verticalScrollBar()->setSingleStep(20);
190 //verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
191 verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
192 viewport()->update();
195 void ChatWidget::appendMsg(AbstractUiMsg *msg) {
196 ChatLine *line = dynamic_cast<ChatLine*>(msg);
198 appendChatLine(line);
201 void ChatWidget::appendChatLine(ChatLine *line) {
202 qreal h = line->layout(tsWidth, senderWidth, textWidth);
203 ycoords.append(h + ycoords[ycoords.count() - 1]);
205 bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
207 if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
209 viewport()->update();
213 void ChatWidget::appendChatLines(QList<ChatLine *> list) {
214 foreach(ChatLine *line, list) {
215 qreal h = line->layout(tsWidth, senderWidth, textWidth);
216 ycoords.append(h + ycoords[ycoords.count() - 1]);
220 bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
222 if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
223 viewport()->update();
226 void ChatWidget::setContents(QList<ChatLine *> list) {
231 appendChatLines(list);
234 //!\brief Computes the different x position vars for given tsWidth and senderWidth.
235 void ChatWidget::computePositions() {
236 senderX = tsWidth + Style::sepTsSender();
237 textX = senderX + senderWidth + Style::sepSenderText();
238 tsGrabPos = tsWidth + (int)Style::sepTsSender()/2;
239 senderGrabPos = senderX + senderWidth + (int)Style::sepSenderText()/2;
240 textWidth = viewport()->size().width() - textX;
243 void ChatWidget::resizeEvent(QResizeEvent *event) {
244 //qDebug() << bufferName << isVisible() << event->size() << event->oldSize();
245 /*if(event->oldSize().isValid())*/
246 //contents->setWidth(event->size().width());
247 //setAlignment(Qt::AlignBottom);
248 if(event->size().width() != event->oldSize().width()) {
253 //qDebug() << viewport()->size() << viewport()->height();
254 //QAbstractScrollArea::resizeEvent(event);
255 //qDebug() << viewport()->size() << viewport()->geometry();
258 void ChatWidget::paintEvent(QPaintEvent *event) {
259 QPainter painter(viewport());
261 //qDebug() << verticalScrollBar()->value();
262 painter.translate(0, -verticalScrollBar()->value());
263 int top = event->rect().top() + verticalScrollBar()->value();
264 int bot = top + event->rect().height();
265 int idx = yToLineIdx(top);
267 for(int i = idx; i < lines.count() ; i++) {
268 lines[i]->draw(&painter, QPointF(0, ycoords[i]));
269 if(ycoords[i+1] > bot) return;
273 //!\brief Layout the widget.
274 void ChatWidget::layout() {
275 // TODO fix scrollbars
276 //int botLine = yToLineIdx(verticalScrollBar()->value() +
277 for(int i = 0; i < lines.count(); i++) {
278 qreal h = lines[i]->layout(tsWidth, senderWidth, textWidth);
279 ycoords[i+1] = h + ycoords[i];
281 height = ycoords[ycoords.count()-1];
283 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
284 viewport()->update();
287 int ChatWidget::yToLineIdx(qreal y) {
288 if(y >= ycoords[ycoords.count()-1]) return ycoords.count()-2;
289 if(ycoords.count() <= 1) return 0;
291 int oidx = ycoords.count() - 1;
294 if(uidx == oidx - 1) return uidx;
295 idx = (uidx + oidx) / 2;
296 if(ycoords[idx] > y) oidx = idx;
301 void ChatWidget::mousePressEvent(QMouseEvent *event) {
302 if(lines.isEmpty()) return;
303 QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
304 if(event->button() == Qt::LeftButton) {
306 dragStartMode = Normal;
309 if(mousePos == OverTsSep) {
310 dragStartMode = DragTsSep;
311 setCursor(Qt::ClosedHandCursor);
312 } else if(mousePos == OverTextSep) {
313 dragStartMode = DragTextSep;
314 setCursor(Qt::ClosedHandCursor);
316 dragStartLine = yToLineIdx(pos.y());
317 dragStartCursor = lines[dragStartLine]->posToCursor(QPointF(pos.x(), pos.y()-ycoords[dragStartLine]));
327 void ChatWidget::mouseDoubleClickEvent(QMouseEvent * /*event*/) {
333 void ChatWidget::mouseReleaseEvent(QMouseEvent *event) {
334 //QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
336 if(event->button() == Qt::LeftButton) {
337 dragStartPos = QPoint();
338 if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
339 else setCursor(Qt::ArrowCursor);
348 selectionMode = TextSelected;
349 selectionLine = dragStartLine;
350 selectionStart = qMin(dragStartCursor, curCursor);
351 selectionEnd = qMax(dragStartCursor, curCursor);
352 // TODO Make X11SelectionMode configurable!
353 QApplication::clipboard()->setText(selectionToString());
357 selectionMode = LinesSelected;
358 selectionStart = qMin(dragStartLine, curLine);
359 selectionEnd = qMax(dragStartLine, curLine);
360 // TODO Make X11SelectionMode configurable!
361 QApplication::clipboard()->setText(selectionToString());
369 //!\brief React to mouse movements over the ChatWidget.
370 /** This is called by Qt whenever the mouse moves. Here we have to do most of the mouse handling,
371 * such as changing column widths, marking text or initiating drag & drop.
373 void ChatWidget::mouseMoveEvent(QMouseEvent *event) {
374 QPoint pos = event->pos(); pointerPosition = pos;
375 // Scroll if mouse pointer leaves widget while dragging
376 if((mouseMode == MarkText || mouseMode == MarkLines) && (pos.y() > viewport()->height() || pos.y() < 0)) {
377 if(!scrollTimer->isActive()) {
378 scrollTimer->start();
381 if(scrollTimer->isActive()) {
385 handleMouseMoveEvent(pos);
388 void ChatWidget::handleMouseMoveEvent(const QPoint &_pos) {
390 if(lines.count() <= 0) return;
391 // Set some basic properties of the current position
392 QPoint pos = _pos + QPoint(0, verticalScrollBar()->value());
395 //MousePos oldpos = mousePos;
396 if(x >= tsGrabPos - 3 && x <= tsGrabPos + 3) mousePos = OverTsSep;
397 else if(x >= senderGrabPos - 3 && x <= senderGrabPos + 3) mousePos = OverTextSep;
398 else mousePos = None;
400 // Pass 1: Do whatever we can before switching mouse mode (if at all).
402 // No special mode. Set mouse cursor if appropriate.
405 //if(oldpos != mousePos) {
406 if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
408 int l = yToLineIdx(y);
409 int c = lines[l]->posToCursor(QPointF(x, y - ycoords[l]));
410 if(c >= 0 && lines[l]->isUrl(c)) {
411 setCursor(Qt::PointingHandCursor);
413 setCursor(Qt::ArrowCursor);
418 // Left button pressed. Might initiate marking or drag & drop if we moved past the drag distance.
420 if(!dragStartPos.isNull() && (dragStartPos - pos).manhattanLength() >= QApplication::startDragDistance()) {
421 // Moving a column separator?
422 if(dragStartMode == DragTsSep) mouseMode = DragTsSep;
423 else if(dragStartMode == DragTextSep) mouseMode = DragTextSep;
424 // Nope. Check if we are over a selection to start drag & drop.
425 else if(dragStartMode == Normal) {
426 bool dragdrop = false;
427 if(selectionMode == TextSelected) {
428 int l = yToLineIdx(y);
429 if(selectionLine == l) {
430 int p = lines[l]->posToCursor(QPointF(x, y - ycoords[l]));
431 if(p >= selectionStart && p <= selectionEnd) dragdrop = true;
433 } else if(selectionMode == LinesSelected) {
434 int l = yToLineIdx(y);
435 if(l >= selectionStart && l <= selectionEnd) dragdrop = true;
437 // Ok, so just start drag & drop if appropriate.
439 QDrag *drag = new QDrag(this);
440 QMimeData *mimeData = new QMimeData;
441 mimeData->setText(selectionToString());
442 drag->setMimeData(mimeData);
445 // Otherwise, clear the selection and start text marking!
447 setCursor(Qt::ArrowCursor);
449 if(dragStartCursor < 0) { mouseMode = MarkLines; curLine = -1; }
450 else mouseMode = MarkText;
462 // Pass 2: Some mouse modes need work after being set...
463 if(mouseMode == DragTsSep && x < size().width() - Style::sepSenderText() - senderWidth - 10) {
464 // Drag first column separator
465 int foo = Style::sepTsSender()/2;
466 tsWidth = qMax(x, foo) - foo;
469 } else if(mouseMode == DragTextSep && x < size().width() - 10) {
470 // Drag second column separator
471 int foo = tsWidth + Style::sepTsSender() + Style::sepSenderText()/2;
472 senderWidth = qMax(x, foo) - foo;
475 } else if(mouseMode == MarkText) {
476 // Change currently marked text
477 curLine = yToLineIdx(y);
478 int c = lines[curLine]->posToCursor(QPointF(x, y - ycoords[curLine]));
479 if(curLine == dragStartLine && c >= 0) {
482 lines[curLine]->setSelection(ChatLine::Partial, dragStartCursor, c);
483 viewport()->update();
486 mouseMode = MarkLines;
487 selectionStart = qMin(curLine, dragStartLine); selectionEnd = qMax(curLine, dragStartLine);
488 for(int i = selectionStart; i <= selectionEnd; i++) lines[i]->setSelection(ChatLine::Full);
489 viewport()->update();
491 } else if(mouseMode == MarkLines) {
493 int l = yToLineIdx(y);
495 selectionStart = qMin(l, dragStartLine); selectionEnd = qMax(l, dragStartLine);
497 Q_ASSERT(selectionStart == selectionEnd);
498 lines[l]->setSelection(ChatLine::Full);
500 if(curLine < selectionStart) {
501 for(int i = curLine; i < selectionStart; i++) lines[i]->setSelection(ChatLine::None);
502 } else if(curLine > selectionEnd) {
503 for(int i = selectionEnd+1; i <= curLine; i++) lines[i]->setSelection(ChatLine::None);
504 } else if(selectionStart < curLine && l < curLine) {
505 for(int i = selectionStart; i < curLine; i++) lines[i]->setSelection(ChatLine::Full);
506 } else if(curLine < selectionEnd && l > curLine) {
507 for(int i = curLine+1; i <= selectionEnd; i++) lines[i]->setSelection(ChatLine::Full);
512 viewport()->update();
517 //!\brief Clear current text selection.
518 void ChatWidget::clearSelection() {
519 if(selectionMode == TextSelected) {
520 lines[selectionLine]->setSelection(ChatLine::None);
521 } else if(selectionMode == LinesSelected) {
522 for(int i = selectionStart; i <= selectionEnd; i++) {
523 lines[i]->setSelection(ChatLine::None);
526 selectionMode = NoSelection;
527 viewport()->update();
530 //!\brief Convert current selection to human-readable string.
531 QString ChatWidget::selectionToString() {
532 //TODO Make selection format configurable!
533 if(selectionMode == NoSelection) return "";
534 if(selectionMode == LinesSelected) {
536 for(int l = selectionStart; l <= selectionEnd; l++) {
537 result += QString("[%1] %2 %3\n").arg(lines[l]->timeStamp().toLocalTime().toString("hh:mm:ss"))
538 .arg(lines[l]->sender()).arg(lines[l]->text());
542 // selectionMode == TextSelected
543 return lines[selectionLine]->text().mid(selectionStart, selectionEnd - selectionStart);