1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
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) version 3. *
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 ***************************************************************************/
22 #include "chatwidget.h"
23 #include "chatline-old.h"
25 #include "uisettings.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::Fixed, QSizePolicy::Fixed);
36 setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
37 setMinimumSize(QSize(20,20));
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(BufferId id) {
50 setBackgroundRole(QPalette::Base);
51 setFont(QFont("Fixed"));
53 QVariant tsDef = s.value("DefaultTimestampColumnWidth", 90);
54 QVariant senderDef = s.value("DefaultSenderColumnWidth", 100);
55 tsWidth = s.value(QString("%1/TimestampColumnWidth").arg(bufferId.toInt()), tsDef).toInt();
56 senderWidth = s.value(QString("%1/SenderColumnWidth").arg(bufferId.toInt()), senderDef).toInt();
59 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
60 //verticalScrollBar()->setPageStep(viewport()->height());
61 //verticalScrollBar()->setSingleStep(20);
62 //verticalScrollBar()->setMinimum(0);
63 //verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
65 // setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
66 setMouseTracking(true);
68 selectionMode = NoSelection;
69 connect(scrollTimer, SIGNAL(timeout()), this, SLOT(handleScrollTimer()));
72 ChatWidget::~ChatWidget() {
73 //qDebug() << "destroying chatwidget" << bufferName;
74 //foreach(ChatLine *l, lines) {
78 s.setValue("DefaultTimestampColumnWidth", tsWidth); // FIXME stupid dirty quicky
79 s.setValue("DefaultSenderColumnWidth", senderWidth);
80 s.setValue(QString("%1/TimestampColumnWidth").arg(bufferId.toInt()), tsWidth);
81 s.setValue(QString("%1/SenderColumnWidth").arg(bufferId.toInt()), senderWidth);
84 QSize ChatWidget::minimumSizeHint() const {
88 // QSize ChatWidget::sizeHint() const {
89 // //qDebug() << size();
93 void ChatWidget::adjustScrollBar() {
94 verticalScrollBar()->setPageStep(viewport()->height());
95 verticalScrollBar()->setSingleStep(20);
96 verticalScrollBar()->setMinimum(0);
97 verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
98 //qDebug() << height << viewport()->height() << verticalScrollBar()->pageStep();
99 //if(bottomLine < 0) {
100 // verticalScrollBar()->setValue(verticalScrollBar()->maximum());
102 //int bot = verticalScrollBar()->value() + viewport()->height(); //qDebug() << bottomLine;
103 //verticalScrollBar()->setValue(qMax(0, (int)ycoords[bottomLine+1] - viewport()->height()));
107 void ChatWidget::scrollBarValChanged(int /*val*/) {
109 if(val >= verticalScrollBar()->maximum()) bottomLine = -1;
111 int bot = val + viewport()->height();
112 int line = yToLineIdx(bot);
118 void ChatWidget::scrollBarAction(int action) {
120 case QScrollBar::SliderSingleStepAdd:
121 // More elaborate. But what with loooong lines?
122 // verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value() + viewport()->height()) + 1] - viewport()->height());
124 case QScrollBar::SliderSingleStepSub:
125 //verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value())]);
132 void ChatWidget::handleScrollTimer() {
133 if(mouseMode == MarkText || mouseMode == MarkLines) {
134 if(pointerPosition.y() > viewport()->height()) {
135 verticalScrollBar()->setValue(verticalScrollBar()->value() + pointerPosition.y() - viewport()->height());
136 handleMouseMoveEvent(QPoint(pointerPosition.x(), viewport()->height()));
137 } else if(pointerPosition.y() < 0) {
138 verticalScrollBar()->setValue(verticalScrollBar()->value() + pointerPosition.y());
139 handleMouseMoveEvent(QPoint(pointerPosition.x(), 0));
144 void ChatWidget::ensureVisible(int line) {
145 int top = verticalScrollBar()->value();
146 int bot = top + viewport()->height();
147 if(ycoords[line+1] > bot) {
148 verticalScrollBar()->setValue(qMax(0, (int)ycoords[line+1] - viewport()->height()));
149 } else if(ycoords[line] < top) {
150 verticalScrollBar()->setValue((int)ycoords[line]);
155 void ChatWidget::clear() {
159 void ChatWidget::prependMsg(AbstractUiMsg *msg) {
160 ChatLine *line = dynamic_cast<ChatLine*>(msg);
162 prependChatLine(line);
165 void ChatWidget::prependChatLine(ChatLine *line) {
166 qreal h = line->layout(tsWidth, senderWidth, textWidth);
167 for(int i = 1; i < ycoords.count(); i++) ycoords[i] += h;
168 ycoords.insert(1, h);
171 // Fix all variables containing line numbers
174 selectionStart ++; selectionEnd ++;
176 verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
177 viewport()->update();
180 void ChatWidget::prependChatLines(QList<ChatLine *> clist) {
181 QList<qreal> tmpy; tmpy.append(0);
183 foreach(ChatLine *l, clist) {
184 h += l->layout(tsWidth, senderWidth, textWidth);
187 ycoords.removeFirst();
188 for(int i = 0; i < ycoords.count(); i++) ycoords[i] += h;
189 ycoords = tmpy + ycoords;
190 lines = clist + lines;
192 // Fix all variables containing line numbers
193 int i = clist.count();
196 selectionStart += i; selectionEnd += i; //? selectionEnd += i;
197 //if(bottomLine >= 0) bottomLine += i;
199 //verticalScrollBar()->setPageStep(viewport()->height());
200 //verticalScrollBar()->setSingleStep(20);
201 //verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
202 verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
203 viewport()->update();
206 void ChatWidget::appendMsg(AbstractUiMsg *msg) {
207 ChatLine *line = dynamic_cast<ChatLine*>(msg);
209 appendChatLine(line);
212 void ChatWidget::appendChatLine(ChatLine *line) {
213 qreal h = line->layout(tsWidth, senderWidth, textWidth);
214 ycoords.append(h + ycoords[ycoords.count() - 1]);
216 bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
218 if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
220 viewport()->update();
224 void ChatWidget::appendChatLines(QList<ChatLine *> list) {
225 foreach(ChatLine *line, list) {
226 qreal h = line->layout(tsWidth, senderWidth, textWidth);
227 ycoords.append(h + ycoords[ycoords.count() - 1]);
231 bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
233 if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
234 viewport()->update();
237 void ChatWidget::setContents(QList<ChatLine *> list) {
242 appendChatLines(list);
245 //!\brief Computes the different x position vars for given tsWidth and senderWidth.
246 void ChatWidget::computePositions() {
247 senderX = tsWidth + QtUi::style()->sepTsSender();
248 textX = senderX + senderWidth + QtUi::style()->sepSenderText();
249 tsGrabPos = tsWidth + (int)QtUi::style()->sepTsSender()/2;
250 senderGrabPos = senderX + senderWidth + (int)QtUi::style()->sepSenderText()/2;
251 textWidth = viewport()->size().width() - textX;
254 void ChatWidget::resizeEvent(QResizeEvent *event) {
255 //qDebug() << bufferName << isVisible() << event->size() << event->oldSize();
256 /*if(event->oldSize().isValid())*/
257 //contents->setWidth(event->size().width());
258 //setAlignment(Qt::AlignBottom);
259 if(event->size() != event->oldSize()) {
264 //qDebug() << viewport()->size() << viewport()->height();
265 //QAbstractScrollArea::resizeEvent(event);
266 //qDebug() << viewport()->size() << viewport()->geometry();
269 void ChatWidget::paintEvent(QPaintEvent *event) {
270 QPainter painter(viewport());
272 //qDebug() << verticalScrollBar()->value();
273 painter.translate(0, -verticalScrollBar()->value());
274 int top = event->rect().top() + verticalScrollBar()->value();
275 int bot = top + event->rect().height();
276 int idx = yToLineIdx(top);
278 for(int i = idx; i < lines.count() ; i++) {
279 lines[i]->draw(&painter, QPointF(0, ycoords[i]));
280 if(ycoords[i+1] > bot) return;
284 //!\brief Layout the widget.
285 void ChatWidget::layout() {
286 // TODO fix scrollbars
287 //int botLine = yToLineIdx(verticalScrollBar()->value() +
288 for(int i = 0; i < lines.count(); i++) {
289 qreal h = lines[i]->layout(tsWidth, senderWidth, textWidth);
290 ycoords[i+1] = h + ycoords[i];
292 height = ycoords[ycoords.count()-1];
294 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
295 viewport()->update();
298 int ChatWidget::yToLineIdx(qreal y) {
299 if(y >= ycoords[ycoords.count()-1]) return ycoords.count()-2;
300 if(ycoords.count() <= 1) return 0;
302 int oidx = ycoords.count() - 1;
305 if(uidx == oidx - 1) return uidx;
306 idx = (uidx + oidx) / 2;
307 if(ycoords[idx] > y) oidx = idx;
312 void ChatWidget::mousePressEvent(QMouseEvent *event) {
313 if(lines.isEmpty()) return;
314 QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
315 if(event->button() == Qt::LeftButton) {
317 dragStartMode = Normal;
320 if(mousePos == OverTsSep) {
321 dragStartMode = DragTsSep;
322 setCursor(Qt::ClosedHandCursor);
323 } else if(mousePos == OverTextSep) {
324 dragStartMode = DragTextSep;
325 setCursor(Qt::ClosedHandCursor);
327 dragStartLine = yToLineIdx(pos.y());
328 dragStartCursor = lines[dragStartLine]->posToCursor(QPointF(pos.x(), pos.y()-ycoords[dragStartLine]));
338 void ChatWidget::mouseDoubleClickEvent(QMouseEvent *event) {
339 // dirty and fast hack to make http:// urls klickable
343 QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
346 int l = yToLineIdx(y);
347 if(lines.count() <= l)
350 ChatLine *line = lines[l];
351 QString text = line->text();
352 int cursorAt = qMax(0, line->posToCursor(QPointF(x, y - ycoords[l])) - 1);
356 for(int i = cursorAt; i > 0; i--) {
364 int end = text.indexOf(" ", start);
369 QString word = text.mid(start, len);
370 if(word.startsWith("http://")) {
371 QDesktopServices::openUrl(QUrl(word));
376 void ChatWidget::mouseReleaseEvent(QMouseEvent *event) {
377 //QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
379 if(event->button() == Qt::LeftButton) {
380 dragStartPos = QPoint();
381 if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
382 else setCursor(Qt::ArrowCursor);
391 selectionMode = TextSelected;
392 selectionLine = dragStartLine;
393 selectionStart = qMin(dragStartCursor, curCursor);
394 selectionEnd = qMax(dragStartCursor, curCursor);
395 // TODO Make X11SelectionMode configurable!
396 QApplication::clipboard()->setText(selectionToString());
400 selectionMode = LinesSelected;
401 selectionStart = qMin(dragStartLine, curLine);
402 selectionEnd = qMax(dragStartLine, curLine);
403 // TODO Make X11SelectionMode configurable!
404 QApplication::clipboard()->setText(selectionToString());
412 //!\brief React to mouse movements over the ChatWidget.
413 /** This is called by Qt whenever the mouse moves. Here we have to do most of the mouse handling,
414 * such as changing column widths, marking text or initiating drag & drop.
416 void ChatWidget::mouseMoveEvent(QMouseEvent *event) {
417 QPoint pos = event->pos(); pointerPosition = pos;
418 // Scroll if mouse pointer leaves widget while dragging
419 if((mouseMode == MarkText || mouseMode == MarkLines) && (pos.y() > viewport()->height() || pos.y() < 0)) {
420 if(!scrollTimer->isActive()) {
421 scrollTimer->start();
424 if(scrollTimer->isActive()) {
428 handleMouseMoveEvent(pos);
431 void ChatWidget::handleMouseMoveEvent(const QPoint &_pos) {
433 if(lines.count() <= 0) return;
434 // Set some basic properties of the current position
435 QPoint pos = _pos + QPoint(0, verticalScrollBar()->value());
438 //MousePos oldpos = mousePos;
439 if(x >= tsGrabPos - 3 && x <= tsGrabPos + 3) mousePos = OverTsSep;
440 else if(x >= senderGrabPos - 3 && x <= senderGrabPos + 3) mousePos = OverTextSep;
441 else mousePos = None;
443 // Pass 1: Do whatever we can before switching mouse mode (if at all).
445 // No special mode. Set mouse cursor if appropriate.
448 //if(oldpos != mousePos) {
449 if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
451 int l = yToLineIdx(y);
452 int c = lines[l]->posToCursor(QPointF(x, y - ycoords[l]));
453 if(c >= 0 && lines[l]->isUrl(c)) {
454 setCursor(Qt::PointingHandCursor);
456 setCursor(Qt::ArrowCursor);
461 // Left button pressed. Might initiate marking or drag & drop if we moved past the drag distance.
463 if(!dragStartPos.isNull() && (dragStartPos - pos).manhattanLength() >= QApplication::startDragDistance()) {
464 // Moving a column separator?
465 if(dragStartMode == DragTsSep) mouseMode = DragTsSep;
466 else if(dragStartMode == DragTextSep) mouseMode = DragTextSep;
467 // Nope. Check if we are over a selection to start drag & drop.
468 else if(dragStartMode == Normal) {
469 bool dragdrop = false;
470 if(selectionMode == TextSelected) {
471 int l = yToLineIdx(y);
472 if(selectionLine == l) {
473 int p = lines[l]->posToCursor(QPointF(x, y - ycoords[l]));
474 if(p >= selectionStart && p <= selectionEnd) dragdrop = true;
476 } else if(selectionMode == LinesSelected) {
477 int l = yToLineIdx(y);
478 if(l >= selectionStart && l <= selectionEnd) dragdrop = true;
480 // Ok, so just start drag & drop if appropriate.
482 QDrag *drag = new QDrag(this);
483 QMimeData *mimeData = new QMimeData;
484 mimeData->setText(selectionToString());
485 drag->setMimeData(mimeData);
488 // Otherwise, clear the selection and start text marking!
490 setCursor(Qt::ArrowCursor);
492 if(dragStartCursor < 0) { mouseMode = MarkLines; curLine = -1; }
493 else mouseMode = MarkText;
505 // Pass 2: Some mouse modes need work after being set...
506 if(mouseMode == DragTsSep && x < size().width() - QtUi::style()->sepSenderText() - senderWidth - 10) {
507 // Drag first column separator
508 int foo = QtUi::style()->sepTsSender()/2;
509 tsWidth = qMax(x, foo) - foo;
512 } else if(mouseMode == DragTextSep && x < size().width() - 10) {
513 // Drag second column separator
514 int foo = tsWidth + QtUi::style()->sepTsSender() + QtUi::style()->sepSenderText()/2;
515 senderWidth = qMax(x, foo) - foo;
518 } else if(mouseMode == MarkText) {
519 // Change currently marked text
520 curLine = yToLineIdx(y);
521 int c = lines[curLine]->posToCursor(QPointF(x, y - ycoords[curLine]));
522 if(curLine == dragStartLine && c >= 0) {
525 lines[curLine]->setSelection(ChatLine::Partial, dragStartCursor, c);
526 viewport()->update();
529 mouseMode = MarkLines;
530 selectionStart = qMin(curLine, dragStartLine); selectionEnd = qMax(curLine, dragStartLine);
531 for(int i = selectionStart; i <= selectionEnd; i++) lines[i]->setSelection(ChatLine::Full);
532 viewport()->update();
534 } else if(mouseMode == MarkLines) {
536 int l = yToLineIdx(y);
538 selectionStart = qMin(l, dragStartLine); selectionEnd = qMax(l, dragStartLine);
540 Q_ASSERT(selectionStart == selectionEnd);
541 lines[l]->setSelection(ChatLine::Full);
543 if(curLine < selectionStart) {
544 for(int i = curLine; i < selectionStart; i++) lines[i]->setSelection(ChatLine::None);
545 } else if(curLine > selectionEnd) {
546 for(int i = selectionEnd+1; i <= curLine; i++) lines[i]->setSelection(ChatLine::None);
547 } else if(selectionStart < curLine && l < curLine) {
548 for(int i = selectionStart; i < curLine; i++) lines[i]->setSelection(ChatLine::Full);
549 } else if(curLine < selectionEnd && l > curLine) {
550 for(int i = curLine+1; i <= selectionEnd; i++) lines[i]->setSelection(ChatLine::Full);
555 viewport()->update();
560 //!\brief Clear current text selection.
561 void ChatWidget::clearSelection() {
562 if(selectionMode == TextSelected) {
563 lines[selectionLine]->setSelection(ChatLine::None);
564 } else if(selectionMode == LinesSelected) {
565 for(int i = selectionStart; i <= selectionEnd; i++) {
566 lines[i]->setSelection(ChatLine::None);
569 selectionMode = NoSelection;
570 viewport()->update();
573 //!\brief Convert current selection to human-readable string.
574 QString ChatWidget::selectionToString() {
575 //TODO Make selection format configurable!
576 if(selectionMode == NoSelection) return "";
577 if(selectionMode == LinesSelected) {
579 for(int l = selectionStart; l <= selectionEnd; l++) {
580 result += QString("[%1] %2 %3\n").arg(lines[l]->timestamp().toLocalTime().toString("hh:mm:ss"))
581 . arg(lines[l]->sender()).arg(lines[l]->text());
585 // selectionMode == TextSelected
586 return lines[selectionLine]->text().mid(selectionStart, selectionEnd - selectionStart);