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::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(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::sizeHint() const {
89 void ChatWidget::adjustScrollBar() {
90 verticalScrollBar()->setPageStep(viewport()->height());
91 verticalScrollBar()->setSingleStep(20);
92 verticalScrollBar()->setMinimum(0);
93 verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
94 //qDebug() << height << viewport()->height() << verticalScrollBar()->pageStep();
95 //if(bottomLine < 0) {
96 // verticalScrollBar()->setValue(verticalScrollBar()->maximum());
98 //int bot = verticalScrollBar()->value() + viewport()->height(); //qDebug() << bottomLine;
99 //verticalScrollBar()->setValue(qMax(0, (int)ycoords[bottomLine+1] - viewport()->height()));
103 void ChatWidget::scrollBarValChanged(int /*val*/) {
105 if(val >= verticalScrollBar()->maximum()) bottomLine = -1;
107 int bot = val + viewport()->height();
108 int line = yToLineIdx(bot);
114 void ChatWidget::scrollBarAction(int action) {
116 case QScrollBar::SliderSingleStepAdd:
117 // More elaborate. But what with loooong lines?
118 // verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value() + viewport()->height()) + 1] - viewport()->height());
120 case QScrollBar::SliderSingleStepSub:
121 //verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value())]);
128 void ChatWidget::handleScrollTimer() {
129 if(mouseMode == MarkText || mouseMode == MarkLines) {
130 if(pointerPosition.y() > viewport()->height()) {
131 verticalScrollBar()->setValue(verticalScrollBar()->value() + pointerPosition.y() - viewport()->height());
132 handleMouseMoveEvent(QPoint(pointerPosition.x(), viewport()->height()));
133 } else if(pointerPosition.y() < 0) {
134 verticalScrollBar()->setValue(verticalScrollBar()->value() + pointerPosition.y());
135 handleMouseMoveEvent(QPoint(pointerPosition.x(), 0));
140 void ChatWidget::ensureVisible(int line) {
141 int top = verticalScrollBar()->value();
142 int bot = top + viewport()->height();
143 if(ycoords[line+1] > bot) {
144 verticalScrollBar()->setValue(qMax(0, (int)ycoords[line+1] - viewport()->height()));
145 } else if(ycoords[line] < top) {
146 verticalScrollBar()->setValue((int)ycoords[line]);
151 void ChatWidget::clear() {
155 void ChatWidget::prependMsg(AbstractUiMsg *msg) {
156 ChatLine *line = dynamic_cast<ChatLine*>(msg);
158 prependChatLine(line);
161 void ChatWidget::prependChatLine(ChatLine *line) {
162 qreal h = line->layout(tsWidth, senderWidth, textWidth);
163 for(int i = 1; i < ycoords.count(); i++) ycoords[i] += h;
164 ycoords.insert(1, h);
167 // Fix all variables containing line numbers
170 selectionStart ++; selectionEnd ++;
172 verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
173 viewport()->update();
176 void ChatWidget::prependChatLines(QList<ChatLine *> clist) {
177 QList<qreal> tmpy; tmpy.append(0);
179 foreach(ChatLine *l, clist) {
180 h += l->layout(tsWidth, senderWidth, textWidth);
183 ycoords.removeFirst();
184 for(int i = 0; i < ycoords.count(); i++) ycoords[i] += h;
185 ycoords = tmpy + ycoords;
186 lines = clist + lines;
188 // Fix all variables containing line numbers
189 int i = clist.count();
192 selectionStart += i; selectionEnd += i; //? selectionEnd += i;
193 //if(bottomLine >= 0) bottomLine += i;
195 //verticalScrollBar()->setPageStep(viewport()->height());
196 //verticalScrollBar()->setSingleStep(20);
197 //verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
198 verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
199 viewport()->update();
202 void ChatWidget::appendMsg(AbstractUiMsg *msg) {
203 ChatLine *line = dynamic_cast<ChatLine*>(msg);
205 appendChatLine(line);
208 void ChatWidget::appendChatLine(ChatLine *line) {
209 qreal h = line->layout(tsWidth, senderWidth, textWidth);
210 ycoords.append(h + ycoords[ycoords.count() - 1]);
212 bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
214 if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
216 viewport()->update();
220 void ChatWidget::appendChatLines(QList<ChatLine *> list) {
221 foreach(ChatLine *line, list) {
222 qreal h = line->layout(tsWidth, senderWidth, textWidth);
223 ycoords.append(h + ycoords[ycoords.count() - 1]);
227 bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
229 if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
230 viewport()->update();
233 void ChatWidget::setContents(QList<ChatLine *> list) {
238 appendChatLines(list);
241 //!\brief Computes the different x position vars for given tsWidth and senderWidth.
242 void ChatWidget::computePositions() {
243 senderX = tsWidth + QtUi::style()->sepTsSender();
244 textX = senderX + senderWidth + QtUi::style()->sepSenderText();
245 tsGrabPos = tsWidth + (int)QtUi::style()->sepTsSender()/2;
246 senderGrabPos = senderX + senderWidth + (int)QtUi::style()->sepSenderText()/2;
247 textWidth = viewport()->size().width() - textX;
250 void ChatWidget::resizeEvent(QResizeEvent *event) {
251 //qDebug() << bufferName << isVisible() << event->size() << event->oldSize();
252 /*if(event->oldSize().isValid())*/
253 //contents->setWidth(event->size().width());
254 //setAlignment(Qt::AlignBottom);
255 if(event->size().width() != event->oldSize().width()) {
260 //qDebug() << viewport()->size() << viewport()->height();
261 //QAbstractScrollArea::resizeEvent(event);
262 //qDebug() << viewport()->size() << viewport()->geometry();
265 void ChatWidget::paintEvent(QPaintEvent *event) {
266 QPainter painter(viewport());
268 //qDebug() << verticalScrollBar()->value();
269 painter.translate(0, -verticalScrollBar()->value());
270 int top = event->rect().top() + verticalScrollBar()->value();
271 int bot = top + event->rect().height();
272 int idx = yToLineIdx(top);
274 for(int i = idx; i < lines.count() ; i++) {
275 lines[i]->draw(&painter, QPointF(0, ycoords[i]));
276 if(ycoords[i+1] > bot) return;
280 //!\brief Layout the widget.
281 void ChatWidget::layout() {
282 // TODO fix scrollbars
283 //int botLine = yToLineIdx(verticalScrollBar()->value() +
284 for(int i = 0; i < lines.count(); i++) {
285 qreal h = lines[i]->layout(tsWidth, senderWidth, textWidth);
286 ycoords[i+1] = h + ycoords[i];
288 height = ycoords[ycoords.count()-1];
290 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
291 viewport()->update();
294 int ChatWidget::yToLineIdx(qreal y) {
295 if(y >= ycoords[ycoords.count()-1]) return ycoords.count()-2;
296 if(ycoords.count() <= 1) return 0;
298 int oidx = ycoords.count() - 1;
301 if(uidx == oidx - 1) return uidx;
302 idx = (uidx + oidx) / 2;
303 if(ycoords[idx] > y) oidx = idx;
308 void ChatWidget::mousePressEvent(QMouseEvent *event) {
309 if(lines.isEmpty()) return;
310 QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
311 if(event->button() == Qt::LeftButton) {
313 dragStartMode = Normal;
316 if(mousePos == OverTsSep) {
317 dragStartMode = DragTsSep;
318 setCursor(Qt::ClosedHandCursor);
319 } else if(mousePos == OverTextSep) {
320 dragStartMode = DragTextSep;
321 setCursor(Qt::ClosedHandCursor);
323 dragStartLine = yToLineIdx(pos.y());
324 dragStartCursor = lines[dragStartLine]->posToCursor(QPointF(pos.x(), pos.y()-ycoords[dragStartLine]));
334 void ChatWidget::mouseDoubleClickEvent(QMouseEvent * /*event*/) {
340 void ChatWidget::mouseReleaseEvent(QMouseEvent *event) {
341 //QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
343 if(event->button() == Qt::LeftButton) {
344 dragStartPos = QPoint();
345 if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
346 else setCursor(Qt::ArrowCursor);
355 selectionMode = TextSelected;
356 selectionLine = dragStartLine;
357 selectionStart = qMin(dragStartCursor, curCursor);
358 selectionEnd = qMax(dragStartCursor, curCursor);
359 // TODO Make X11SelectionMode configurable!
360 QApplication::clipboard()->setText(selectionToString());
364 selectionMode = LinesSelected;
365 selectionStart = qMin(dragStartLine, curLine);
366 selectionEnd = qMax(dragStartLine, curLine);
367 // TODO Make X11SelectionMode configurable!
368 QApplication::clipboard()->setText(selectionToString());
376 //!\brief React to mouse movements over the ChatWidget.
377 /** This is called by Qt whenever the mouse moves. Here we have to do most of the mouse handling,
378 * such as changing column widths, marking text or initiating drag & drop.
380 void ChatWidget::mouseMoveEvent(QMouseEvent *event) {
381 QPoint pos = event->pos(); pointerPosition = pos;
382 // Scroll if mouse pointer leaves widget while dragging
383 if((mouseMode == MarkText || mouseMode == MarkLines) && (pos.y() > viewport()->height() || pos.y() < 0)) {
384 if(!scrollTimer->isActive()) {
385 scrollTimer->start();
388 if(scrollTimer->isActive()) {
392 handleMouseMoveEvent(pos);
395 void ChatWidget::handleMouseMoveEvent(const QPoint &_pos) {
397 if(lines.count() <= 0) return;
398 // Set some basic properties of the current position
399 QPoint pos = _pos + QPoint(0, verticalScrollBar()->value());
402 //MousePos oldpos = mousePos;
403 if(x >= tsGrabPos - 3 && x <= tsGrabPos + 3) mousePos = OverTsSep;
404 else if(x >= senderGrabPos - 3 && x <= senderGrabPos + 3) mousePos = OverTextSep;
405 else mousePos = None;
407 // Pass 1: Do whatever we can before switching mouse mode (if at all).
409 // No special mode. Set mouse cursor if appropriate.
412 //if(oldpos != mousePos) {
413 if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
415 int l = yToLineIdx(y);
416 int c = lines[l]->posToCursor(QPointF(x, y - ycoords[l]));
417 if(c >= 0 && lines[l]->isUrl(c)) {
418 setCursor(Qt::PointingHandCursor);
420 setCursor(Qt::ArrowCursor);
425 // Left button pressed. Might initiate marking or drag & drop if we moved past the drag distance.
427 if(!dragStartPos.isNull() && (dragStartPos - pos).manhattanLength() >= QApplication::startDragDistance()) {
428 // Moving a column separator?
429 if(dragStartMode == DragTsSep) mouseMode = DragTsSep;
430 else if(dragStartMode == DragTextSep) mouseMode = DragTextSep;
431 // Nope. Check if we are over a selection to start drag & drop.
432 else if(dragStartMode == Normal) {
433 bool dragdrop = false;
434 if(selectionMode == TextSelected) {
435 int l = yToLineIdx(y);
436 if(selectionLine == l) {
437 int p = lines[l]->posToCursor(QPointF(x, y - ycoords[l]));
438 if(p >= selectionStart && p <= selectionEnd) dragdrop = true;
440 } else if(selectionMode == LinesSelected) {
441 int l = yToLineIdx(y);
442 if(l >= selectionStart && l <= selectionEnd) dragdrop = true;
444 // Ok, so just start drag & drop if appropriate.
446 QDrag *drag = new QDrag(this);
447 QMimeData *mimeData = new QMimeData;
448 mimeData->setText(selectionToString());
449 drag->setMimeData(mimeData);
452 // Otherwise, clear the selection and start text marking!
454 setCursor(Qt::ArrowCursor);
456 if(dragStartCursor < 0) { mouseMode = MarkLines; curLine = -1; }
457 else mouseMode = MarkText;
469 // Pass 2: Some mouse modes need work after being set...
470 if(mouseMode == DragTsSep && x < size().width() - QtUi::style()->sepSenderText() - senderWidth - 10) {
471 // Drag first column separator
472 int foo = QtUi::style()->sepTsSender()/2;
473 tsWidth = qMax(x, foo) - foo;
476 } else if(mouseMode == DragTextSep && x < size().width() - 10) {
477 // Drag second column separator
478 int foo = tsWidth + QtUi::style()->sepTsSender() + QtUi::style()->sepSenderText()/2;
479 senderWidth = qMax(x, foo) - foo;
482 } else if(mouseMode == MarkText) {
483 // Change currently marked text
484 curLine = yToLineIdx(y);
485 int c = lines[curLine]->posToCursor(QPointF(x, y - ycoords[curLine]));
486 if(curLine == dragStartLine && c >= 0) {
489 lines[curLine]->setSelection(ChatLine::Partial, dragStartCursor, c);
490 viewport()->update();
493 mouseMode = MarkLines;
494 selectionStart = qMin(curLine, dragStartLine); selectionEnd = qMax(curLine, dragStartLine);
495 for(int i = selectionStart; i <= selectionEnd; i++) lines[i]->setSelection(ChatLine::Full);
496 viewport()->update();
498 } else if(mouseMode == MarkLines) {
500 int l = yToLineIdx(y);
502 selectionStart = qMin(l, dragStartLine); selectionEnd = qMax(l, dragStartLine);
504 Q_ASSERT(selectionStart == selectionEnd);
505 lines[l]->setSelection(ChatLine::Full);
507 if(curLine < selectionStart) {
508 for(int i = curLine; i < selectionStart; i++) lines[i]->setSelection(ChatLine::None);
509 } else if(curLine > selectionEnd) {
510 for(int i = selectionEnd+1; i <= curLine; i++) lines[i]->setSelection(ChatLine::None);
511 } else if(selectionStart < curLine && l < curLine) {
512 for(int i = selectionStart; i < curLine; i++) lines[i]->setSelection(ChatLine::Full);
513 } else if(curLine < selectionEnd && l > curLine) {
514 for(int i = curLine+1; i <= selectionEnd; i++) lines[i]->setSelection(ChatLine::Full);
519 viewport()->update();
524 //!\brief Clear current text selection.
525 void ChatWidget::clearSelection() {
526 if(selectionMode == TextSelected) {
527 lines[selectionLine]->setSelection(ChatLine::None);
528 } else if(selectionMode == LinesSelected) {
529 for(int i = selectionStart; i <= selectionEnd; i++) {
530 lines[i]->setSelection(ChatLine::None);
533 selectionMode = NoSelection;
534 viewport()->update();
537 //!\brief Convert current selection to human-readable string.
538 QString ChatWidget::selectionToString() {
539 //TODO Make selection format configurable!
540 if(selectionMode == NoSelection) return "";
541 if(selectionMode == LinesSelected) {
543 for(int l = selectionStart; l <= selectionEnd; l++) {
544 result += QString("[%1] %2 %3\n").arg(lines[l]->timestamp().toLocalTime().toString("hh:mm:ss"))
545 . arg(lines[l]->sender()).arg(lines[l]->text());
549 // selectionMode == TextSelected
550 return lines[selectionLine]->text().mid(selectionStart, selectionEnd - selectionStart);