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(ChatLineOld *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 return QSize(400, 100);
92 // QSize ChatWidget::sizeHint() const {
93 // //qDebug() << size();
97 void ChatWidget::adjustScrollBar() {
98 verticalScrollBar()->setPageStep(viewport()->height());
99 verticalScrollBar()->setSingleStep(20);
100 verticalScrollBar()->setMinimum(0);
101 verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
102 //qDebug() << height << viewport()->height() << verticalScrollBar()->pageStep();
103 //if(bottomLine < 0) {
104 // verticalScrollBar()->setValue(verticalScrollBar()->maximum());
106 //int bot = verticalScrollBar()->value() + viewport()->height(); //qDebug() << bottomLine;
107 //verticalScrollBar()->setValue(qMax(0, (int)ycoords[bottomLine+1] - viewport()->height()));
111 void ChatWidget::scrollBarValChanged(int /*val*/) {
113 if(val >= verticalScrollBar()->maximum()) bottomLine = -1;
115 int bot = val + viewport()->height();
116 int line = yToLineIdx(bot);
122 void ChatWidget::scrollBarAction(int action) {
124 case QScrollBar::SliderSingleStepAdd:
125 // More elaborate. But what with loooong lines?
126 // verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value() + viewport()->height()) + 1] - viewport()->height());
128 case QScrollBar::SliderSingleStepSub:
129 //verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value())]);
136 void ChatWidget::handleScrollTimer() {
137 if(mouseMode == MarkText || mouseMode == MarkLines) {
138 if(pointerPosition.y() > viewport()->height()) {
139 verticalScrollBar()->setValue(verticalScrollBar()->value() + pointerPosition.y() - viewport()->height());
140 handleMouseMoveEvent(QPoint(pointerPosition.x(), viewport()->height()));
141 } else if(pointerPosition.y() < 0) {
142 verticalScrollBar()->setValue(verticalScrollBar()->value() + pointerPosition.y());
143 handleMouseMoveEvent(QPoint(pointerPosition.x(), 0));
148 void ChatWidget::ensureVisible(int line) {
149 int top = verticalScrollBar()->value();
150 int bot = top + viewport()->height();
151 if(ycoords[line+1] > bot) {
152 verticalScrollBar()->setValue(qMax(0, (int)ycoords[line+1] - viewport()->height()));
153 } else if(ycoords[line] < top) {
154 verticalScrollBar()->setValue((int)ycoords[line]);
159 void ChatWidget::clear() {
163 void ChatWidget::prependMsg(AbstractUiMsg *msg) {
164 ChatLineOld *line = dynamic_cast<ChatLineOld*>(msg);
166 prependChatLine(line);
169 void ChatWidget::prependChatLine(ChatLineOld *line) {
170 qreal h = line->layout(tsWidth, senderWidth, textWidth);
171 for(int i = 1; i < ycoords.count(); i++) ycoords[i] += h;
172 ycoords.insert(1, h);
175 // Fix all variables containing line numbers
178 selectionStart ++; selectionEnd ++;
180 verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
181 viewport()->update();
184 void ChatWidget::prependChatLines(QList<ChatLineOld *> clist) {
185 QList<qreal> tmpy; tmpy.append(0);
187 foreach(ChatLineOld *l, clist) {
188 h += l->layout(tsWidth, senderWidth, textWidth);
191 ycoords.removeFirst();
192 for(int i = 0; i < ycoords.count(); i++) ycoords[i] += h;
193 ycoords = tmpy + ycoords;
194 lines = clist + lines;
196 // Fix all variables containing line numbers
197 int i = clist.count();
200 selectionStart += i; selectionEnd += i; //? selectionEnd += i;
201 //if(bottomLine >= 0) bottomLine += i;
203 //verticalScrollBar()->setPageStep(viewport()->height());
204 //verticalScrollBar()->setSingleStep(20);
205 //verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
206 verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
207 viewport()->update();
210 void ChatWidget::appendMsg(AbstractUiMsg *msg) {
211 ChatLineOld *line = dynamic_cast<ChatLineOld*>(msg);
213 appendChatLine(line);
216 void ChatWidget::appendChatLine(ChatLineOld *line) {
217 qreal h = line->layout(tsWidth, senderWidth, textWidth);
218 ycoords.append(h + ycoords[ycoords.count() - 1]);
220 bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
222 if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
224 viewport()->update();
228 void ChatWidget::appendChatLines(QList<ChatLineOld *> list) {
229 foreach(ChatLineOld *line, list) {
230 qreal h = line->layout(tsWidth, senderWidth, textWidth);
231 ycoords.append(h + ycoords[ycoords.count() - 1]);
235 bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
237 if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
238 viewport()->update();
241 void ChatWidget::setContents(QList<ChatLineOld *> list) {
246 appendChatLines(list);
249 //!\brief Computes the different x position vars for given tsWidth and senderWidth.
250 void ChatWidget::computePositions() {
251 senderX = tsWidth + QtUi::style()->sepTsSender();
252 textX = senderX + senderWidth + QtUi::style()->sepSenderText();
253 tsGrabPos = tsWidth + (int)QtUi::style()->sepTsSender()/2;
254 senderGrabPos = senderX + senderWidth + (int)QtUi::style()->sepSenderText()/2;
255 textWidth = viewport()->size().width() - textX;
258 void ChatWidget::resizeEvent(QResizeEvent *event) {
259 //qDebug() << bufferName << isVisible() << event->size() << event->oldSize();
260 /*if(event->oldSize().isValid())*/
261 //contents->setWidth(event->size().width());
262 //setAlignment(Qt::AlignBottom);
263 if(event->size() != event->oldSize()) {
268 //qDebug() << viewport()->size() << viewport()->height();
269 //QAbstractScrollArea::resizeEvent(event);
270 //qDebug() << viewport()->size() << viewport()->geometry();
273 void ChatWidget::paintEvent(QPaintEvent *event) {
274 QPainter painter(viewport());
276 //qDebug() << verticalScrollBar()->value();
277 painter.translate(0, -verticalScrollBar()->value());
278 int top = event->rect().top() + verticalScrollBar()->value();
279 int bot = top + event->rect().height();
280 int idx = yToLineIdx(top);
282 for(int i = idx; i < lines.count() ; i++) {
283 lines[i]->draw(&painter, QPointF(0, ycoords[i]));
284 if(ycoords[i+1] > bot) return;
288 //!\brief Layout the widget.
289 void ChatWidget::layout() {
290 // TODO fix scrollbars
291 //int botLine = yToLineIdx(verticalScrollBar()->value() +
292 for(int i = 0; i < lines.count(); i++) {
293 qreal h = lines[i]->layout(tsWidth, senderWidth, textWidth);
294 ycoords[i+1] = h + ycoords[i];
296 height = ycoords[ycoords.count()-1];
298 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
299 viewport()->update();
302 int ChatWidget::yToLineIdx(qreal y) {
303 if(y >= ycoords[ycoords.count()-1]) return ycoords.count()-2;
304 if(ycoords.count() <= 1) return 0;
306 int oidx = ycoords.count() - 1;
309 if(uidx == oidx - 1) return uidx;
310 idx = (uidx + oidx) / 2;
311 if(ycoords[idx] > y) oidx = idx;
316 void ChatWidget::mousePressEvent(QMouseEvent *event) {
317 if(lines.isEmpty()) return;
318 QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
319 if(event->button() == Qt::LeftButton) {
321 dragStartMode = Normal;
324 if(mousePos == OverTsSep) {
325 dragStartMode = DragTsSep;
326 setCursor(Qt::ClosedHandCursor);
327 } else if(mousePos == OverTextSep) {
328 dragStartMode = DragTextSep;
329 setCursor(Qt::ClosedHandCursor);
331 dragStartLine = yToLineIdx(pos.y());
332 dragStartCursor = lines[dragStartLine]->posToCursor(QPointF(pos.x(), pos.y()-ycoords[dragStartLine]));
342 void ChatWidget::mouseDoubleClickEvent(QMouseEvent *event) {
343 // dirty and fast hack to make http:// urls klickable
347 QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
350 int l = yToLineIdx(y);
351 if(lines.count() <= l)
354 ChatLineOld *line = lines[l];
355 QString text = line->text();
356 int cursorAt = qMax(0, line->posToCursor(QPointF(x, y - ycoords[l])) - 1);
360 for(int i = cursorAt; i > 0; i--) {
368 int end = text.indexOf(" ", start);
373 QString word = text.mid(start, len);
374 if(word.startsWith("http://")) {
375 QDesktopServices::openUrl(QUrl(word));
380 void ChatWidget::mouseReleaseEvent(QMouseEvent *event) {
381 //QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
383 if(event->button() == Qt::LeftButton) {
384 dragStartPos = QPoint();
385 if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
386 else setCursor(Qt::ArrowCursor);
395 selectionMode = TextSelected;
396 selectionLine = dragStartLine;
397 selectionStart = qMin(dragStartCursor, curCursor);
398 selectionEnd = qMax(dragStartCursor, curCursor);
399 // TODO Make X11SelectionMode configurable!
400 QApplication::clipboard()->setText(selectionToString());
404 selectionMode = LinesSelected;
405 selectionStart = qMin(dragStartLine, curLine);
406 selectionEnd = qMax(dragStartLine, curLine);
407 // TODO Make X11SelectionMode configurable!
408 QApplication::clipboard()->setText(selectionToString());
416 //!\brief React to mouse movements over the ChatWidget.
417 /** This is called by Qt whenever the mouse moves. Here we have to do most of the mouse handling,
418 * such as changing column widths, marking text or initiating drag & drop.
420 void ChatWidget::mouseMoveEvent(QMouseEvent *event) {
421 QPoint pos = event->pos(); pointerPosition = pos;
422 // Scroll if mouse pointer leaves widget while dragging
423 if((mouseMode == MarkText || mouseMode == MarkLines) && (pos.y() > viewport()->height() || pos.y() < 0)) {
424 if(!scrollTimer->isActive()) {
425 scrollTimer->start();
428 if(scrollTimer->isActive()) {
432 handleMouseMoveEvent(pos);
435 void ChatWidget::handleMouseMoveEvent(const QPoint &_pos) {
437 if(lines.count() <= 0) return;
438 // Set some basic properties of the current position
439 QPoint pos = _pos + QPoint(0, verticalScrollBar()->value());
442 //MousePos oldpos = mousePos;
443 if(x >= tsGrabPos - 3 && x <= tsGrabPos + 3) mousePos = OverTsSep;
444 else if(x >= senderGrabPos - 3 && x <= senderGrabPos + 3) mousePos = OverTextSep;
445 else mousePos = None;
447 // Pass 1: Do whatever we can before switching mouse mode (if at all).
449 // No special mode. Set mouse cursor if appropriate.
452 //if(oldpos != mousePos) {
453 if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
455 int l = yToLineIdx(y);
456 int c = lines[l]->posToCursor(QPointF(x, y - ycoords[l]));
457 if(c >= 0 && lines[l]->isUrl(c)) {
458 setCursor(Qt::PointingHandCursor);
460 setCursor(Qt::ArrowCursor);
465 // Left button pressed. Might initiate marking or drag & drop if we moved past the drag distance.
467 if(!dragStartPos.isNull() && (dragStartPos - pos).manhattanLength() >= QApplication::startDragDistance()) {
468 // Moving a column separator?
469 if(dragStartMode == DragTsSep) mouseMode = DragTsSep;
470 else if(dragStartMode == DragTextSep) mouseMode = DragTextSep;
471 // Nope. Check if we are over a selection to start drag & drop.
472 else if(dragStartMode == Normal) {
473 bool dragdrop = false;
474 if(selectionMode == TextSelected) {
475 int l = yToLineIdx(y);
476 if(selectionLine == l) {
477 int p = lines[l]->posToCursor(QPointF(x, y - ycoords[l]));
478 if(p >= selectionStart && p <= selectionEnd) dragdrop = true;
480 } else if(selectionMode == LinesSelected) {
481 int l = yToLineIdx(y);
482 if(l >= selectionStart && l <= selectionEnd) dragdrop = true;
484 // Ok, so just start drag & drop if appropriate.
486 QDrag *drag = new QDrag(this);
487 QMimeData *mimeData = new QMimeData;
488 mimeData->setText(selectionToString());
489 drag->setMimeData(mimeData);
492 // Otherwise, clear the selection and start text marking!
494 setCursor(Qt::ArrowCursor);
496 if(dragStartCursor < 0) { mouseMode = MarkLines; curLine = -1; }
497 else mouseMode = MarkText;
509 // Pass 2: Some mouse modes need work after being set...
510 if(mouseMode == DragTsSep && x < size().width() - QtUi::style()->sepSenderText() - senderWidth - 10) {
511 // Drag first column separator
512 int foo = QtUi::style()->sepTsSender()/2;
513 tsWidth = qMax(x, foo) - foo;
516 } else if(mouseMode == DragTextSep && x < size().width() - 10) {
517 // Drag second column separator
518 int foo = tsWidth + QtUi::style()->sepTsSender() + QtUi::style()->sepSenderText()/2;
519 senderWidth = qMax(x, foo) - foo;
522 } else if(mouseMode == MarkText) {
523 // Change currently marked text
524 curLine = yToLineIdx(y);
525 int c = lines[curLine]->posToCursor(QPointF(x, y - ycoords[curLine]));
526 if(curLine == dragStartLine && c >= 0) {
529 lines[curLine]->setSelection(ChatLineOld::Partial, dragStartCursor, c);
530 viewport()->update();
533 mouseMode = MarkLines;
534 selectionStart = qMin(curLine, dragStartLine); selectionEnd = qMax(curLine, dragStartLine);
535 for(int i = selectionStart; i <= selectionEnd; i++) lines[i]->setSelection(ChatLineOld::Full);
536 viewport()->update();
538 } else if(mouseMode == MarkLines) {
540 int l = yToLineIdx(y);
542 selectionStart = qMin(l, dragStartLine); selectionEnd = qMax(l, dragStartLine);
544 Q_ASSERT(selectionStart == selectionEnd);
545 lines[l]->setSelection(ChatLineOld::Full);
547 if(curLine < selectionStart) {
548 for(int i = curLine; i < selectionStart; i++) lines[i]->setSelection(ChatLineOld::None);
549 } else if(curLine > selectionEnd) {
550 for(int i = selectionEnd+1; i <= curLine; i++) lines[i]->setSelection(ChatLineOld::None);
551 } else if(selectionStart < curLine && l < curLine) {
552 for(int i = selectionStart; i < curLine; i++) lines[i]->setSelection(ChatLineOld::Full);
553 } else if(curLine < selectionEnd && l > curLine) {
554 for(int i = curLine+1; i <= selectionEnd; i++) lines[i]->setSelection(ChatLineOld::Full);
559 viewport()->update();
564 //!\brief Clear current text selection.
565 void ChatWidget::clearSelection() {
566 if(selectionMode == TextSelected) {
567 lines[selectionLine]->setSelection(ChatLineOld::None);
568 } else if(selectionMode == LinesSelected) {
569 for(int i = selectionStart; i <= selectionEnd; i++) {
570 lines[i]->setSelection(ChatLineOld::None);
573 selectionMode = NoSelection;
574 viewport()->update();
577 //!\brief Convert current selection to human-readable string.
578 QString ChatWidget::selectionToString() {
579 //TODO Make selection format configurable!
580 if(selectionMode == NoSelection) return "";
581 if(selectionMode == LinesSelected) {
583 for(int l = selectionStart; l <= selectionEnd; l++) {
584 result += QString("[%1] %2 %3\n").arg(lines[l]->timestamp().toLocalTime().toString("hh:mm:ss"))
585 . arg(lines[l]->sender()).arg(lines[l]->text());
589 // selectionMode == TextSelected
590 return lines[selectionLine]->text().mid(selectionStart, selectionEnd - selectionStart);