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(QString netname, QString bufname) {
49 networkName = netname;
51 setBackgroundRole(QPalette::Base);
52 setFont(QFont("Fixed"));
54 QVariant tsDef = s.value("DefaultTimestampColumnWidth", 90);
55 QVariant senderDef = s.value("DefaultSenderColumnWidth", 100);
56 tsWidth = s.value(QString("%1/%2/TimestampColumnWidth").arg(netname, bufname), tsDef).toInt();
57 senderWidth = s.value(QString("%1/%2/SenderColumnWidth").arg(netname, bufname), senderDef).toInt();
60 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
61 //verticalScrollBar()->setPageStep(viewport()->height());
62 //verticalScrollBar()->setSingleStep(20);
63 //verticalScrollBar()->setMinimum(0);
64 //verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
66 setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
67 setMouseTracking(true);
69 selectionMode = NoSelection;
70 connect(scrollTimer, SIGNAL(timeout()), this, SLOT(handleScrollTimer()));
73 ChatWidget::~ChatWidget() {
74 //qDebug() << "destroying chatwidget" << bufferName;
75 //foreach(ChatLine *l, lines) {
79 s.setValue("DefaultTimestampColumnWidth", tsWidth); // FIXME stupid dirty quicky
80 s.setValue("DefaultSenderColumnWidth", senderWidth);
81 s.setValue(QString("%1/%2/TimestampColumnWidth").arg(networkName, bufferName), tsWidth);
82 s.setValue(QString("%1/%2/SenderColumnWidth").arg(networkName, bufferName), senderWidth);
85 QSize ChatWidget::sizeHint() const {
90 void ChatWidget::adjustScrollBar() {
91 verticalScrollBar()->setPageStep(viewport()->height());
92 verticalScrollBar()->setSingleStep(20);
93 verticalScrollBar()->setMinimum(0);
94 verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
95 //qDebug() << height << viewport()->height() << verticalScrollBar()->pageStep();
96 //if(bottomLine < 0) {
97 // verticalScrollBar()->setValue(verticalScrollBar()->maximum());
99 //int bot = verticalScrollBar()->value() + viewport()->height(); //qDebug() << bottomLine;
100 //verticalScrollBar()->setValue(qMax(0, (int)ycoords[bottomLine+1] - viewport()->height()));
104 void ChatWidget::scrollBarValChanged(int /*val*/) {
106 if(val >= verticalScrollBar()->maximum()) bottomLine = -1;
108 int bot = val + viewport()->height();
109 int line = yToLineIdx(bot);
115 void ChatWidget::scrollBarAction(int action) {
117 case QScrollBar::SliderSingleStepAdd:
118 // More elaborate. But what with loooong lines?
119 // verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value() + viewport()->height()) + 1] - viewport()->height());
121 case QScrollBar::SliderSingleStepSub:
122 //verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value())]);
129 void ChatWidget::handleScrollTimer() {
130 if(mouseMode == MarkText || mouseMode == MarkLines) {
131 if(pointerPosition.y() > viewport()->height()) {
132 verticalScrollBar()->setValue(verticalScrollBar()->value() + pointerPosition.y() - viewport()->height());
133 handleMouseMoveEvent(QPoint(pointerPosition.x(), viewport()->height()));
134 } else if(pointerPosition.y() < 0) {
135 verticalScrollBar()->setValue(verticalScrollBar()->value() + pointerPosition.y());
136 handleMouseMoveEvent(QPoint(pointerPosition.x(), 0));
141 void ChatWidget::ensureVisible(int line) {
142 int top = verticalScrollBar()->value();
143 int bot = top + viewport()->height();
144 if(ycoords[line+1] > bot) {
145 verticalScrollBar()->setValue(qMax(0, (int)ycoords[line+1] - viewport()->height()));
146 } else if(ycoords[line] < top) {
147 verticalScrollBar()->setValue((int)ycoords[line]);
152 void ChatWidget::clear() {
156 void ChatWidget::prependMsg(AbstractUiMsg *msg) {
157 ChatLine *line = dynamic_cast<ChatLine*>(msg);
159 prependChatLine(line);
162 void ChatWidget::prependChatLine(ChatLine *line) {
163 qreal h = line->layout(tsWidth, senderWidth, textWidth);
164 for(int i = 1; i < ycoords.count(); i++) ycoords[i] += h;
165 ycoords.insert(1, h);
168 // Fix all variables containing line numbers
171 selectionStart ++; selectionEnd ++;
173 verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
174 viewport()->update();
177 void ChatWidget::prependChatLines(QList<ChatLine *> clist) {
178 QList<qreal> tmpy; tmpy.append(0);
180 foreach(ChatLine *l, clist) {
181 h += l->layout(tsWidth, senderWidth, textWidth);
184 ycoords.removeFirst();
185 for(int i = 0; i < ycoords.count(); i++) ycoords[i] += h;
186 ycoords = tmpy + ycoords;
187 lines = clist + lines;
189 // Fix all variables containing line numbers
190 int i = clist.count();
193 selectionStart += i; selectionEnd += i; //? selectionEnd += i;
194 //if(bottomLine >= 0) bottomLine += i;
196 //verticalScrollBar()->setPageStep(viewport()->height());
197 //verticalScrollBar()->setSingleStep(20);
198 //verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
199 verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
200 viewport()->update();
203 void ChatWidget::appendMsg(AbstractUiMsg *msg) {
204 ChatLine *line = dynamic_cast<ChatLine*>(msg);
206 appendChatLine(line);
209 void ChatWidget::appendChatLine(ChatLine *line) {
210 qreal h = line->layout(tsWidth, senderWidth, textWidth);
211 ycoords.append(h + ycoords[ycoords.count() - 1]);
213 bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
215 if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
217 viewport()->update();
221 void ChatWidget::appendChatLines(QList<ChatLine *> list) {
222 foreach(ChatLine *line, list) {
223 qreal h = line->layout(tsWidth, senderWidth, textWidth);
224 ycoords.append(h + ycoords[ycoords.count() - 1]);
228 bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
230 if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
231 viewport()->update();
234 void ChatWidget::setContents(QList<ChatLine *> list) {
239 appendChatLines(list);
242 //!\brief Computes the different x position vars for given tsWidth and senderWidth.
243 void ChatWidget::computePositions() {
244 senderX = tsWidth + QtUi::style()->sepTsSender();
245 textX = senderX + senderWidth + QtUi::style()->sepSenderText();
246 tsGrabPos = tsWidth + (int)QtUi::style()->sepTsSender()/2;
247 senderGrabPos = senderX + senderWidth + (int)QtUi::style()->sepSenderText()/2;
248 textWidth = viewport()->size().width() - textX;
251 void ChatWidget::resizeEvent(QResizeEvent *event) {
252 //qDebug() << bufferName << isVisible() << event->size() << event->oldSize();
253 /*if(event->oldSize().isValid())*/
254 //contents->setWidth(event->size().width());
255 //setAlignment(Qt::AlignBottom);
256 if(event->size().width() != event->oldSize().width()) {
261 //qDebug() << viewport()->size() << viewport()->height();
262 //QAbstractScrollArea::resizeEvent(event);
263 //qDebug() << viewport()->size() << viewport()->geometry();
266 void ChatWidget::paintEvent(QPaintEvent *event) {
267 QPainter painter(viewport());
269 //qDebug() << verticalScrollBar()->value();
270 painter.translate(0, -verticalScrollBar()->value());
271 int top = event->rect().top() + verticalScrollBar()->value();
272 int bot = top + event->rect().height();
273 int idx = yToLineIdx(top);
275 for(int i = idx; i < lines.count() ; i++) {
276 lines[i]->draw(&painter, QPointF(0, ycoords[i]));
277 if(ycoords[i+1] > bot) return;
281 //!\brief Layout the widget.
282 void ChatWidget::layout() {
283 // TODO fix scrollbars
284 //int botLine = yToLineIdx(verticalScrollBar()->value() +
285 for(int i = 0; i < lines.count(); i++) {
286 qreal h = lines[i]->layout(tsWidth, senderWidth, textWidth);
287 ycoords[i+1] = h + ycoords[i];
289 height = ycoords[ycoords.count()-1];
291 verticalScrollBar()->setValue(verticalScrollBar()->maximum());
292 viewport()->update();
295 int ChatWidget::yToLineIdx(qreal y) {
296 if(y >= ycoords[ycoords.count()-1]) return ycoords.count()-2;
297 if(ycoords.count() <= 1) return 0;
299 int oidx = ycoords.count() - 1;
302 if(uidx == oidx - 1) return uidx;
303 idx = (uidx + oidx) / 2;
304 if(ycoords[idx] > y) oidx = idx;
309 void ChatWidget::mousePressEvent(QMouseEvent *event) {
310 if(lines.isEmpty()) return;
311 QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
312 if(event->button() == Qt::LeftButton) {
314 dragStartMode = Normal;
317 if(mousePos == OverTsSep) {
318 dragStartMode = DragTsSep;
319 setCursor(Qt::ClosedHandCursor);
320 } else if(mousePos == OverTextSep) {
321 dragStartMode = DragTextSep;
322 setCursor(Qt::ClosedHandCursor);
324 dragStartLine = yToLineIdx(pos.y());
325 dragStartCursor = lines[dragStartLine]->posToCursor(QPointF(pos.x(), pos.y()-ycoords[dragStartLine]));
335 void ChatWidget::mouseDoubleClickEvent(QMouseEvent * /*event*/) {
341 void ChatWidget::mouseReleaseEvent(QMouseEvent *event) {
342 //QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
344 if(event->button() == Qt::LeftButton) {
345 dragStartPos = QPoint();
346 if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
347 else setCursor(Qt::ArrowCursor);
356 selectionMode = TextSelected;
357 selectionLine = dragStartLine;
358 selectionStart = qMin(dragStartCursor, curCursor);
359 selectionEnd = qMax(dragStartCursor, curCursor);
360 // TODO Make X11SelectionMode configurable!
361 QApplication::clipboard()->setText(selectionToString());
365 selectionMode = LinesSelected;
366 selectionStart = qMin(dragStartLine, curLine);
367 selectionEnd = qMax(dragStartLine, curLine);
368 // TODO Make X11SelectionMode configurable!
369 QApplication::clipboard()->setText(selectionToString());
377 //!\brief React to mouse movements over the ChatWidget.
378 /** This is called by Qt whenever the mouse moves. Here we have to do most of the mouse handling,
379 * such as changing column widths, marking text or initiating drag & drop.
381 void ChatWidget::mouseMoveEvent(QMouseEvent *event) {
382 QPoint pos = event->pos(); pointerPosition = pos;
383 // Scroll if mouse pointer leaves widget while dragging
384 if((mouseMode == MarkText || mouseMode == MarkLines) && (pos.y() > viewport()->height() || pos.y() < 0)) {
385 if(!scrollTimer->isActive()) {
386 scrollTimer->start();
389 if(scrollTimer->isActive()) {
393 handleMouseMoveEvent(pos);
396 void ChatWidget::handleMouseMoveEvent(const QPoint &_pos) {
398 if(lines.count() <= 0) return;
399 // Set some basic properties of the current position
400 QPoint pos = _pos + QPoint(0, verticalScrollBar()->value());
403 //MousePos oldpos = mousePos;
404 if(x >= tsGrabPos - 3 && x <= tsGrabPos + 3) mousePos = OverTsSep;
405 else if(x >= senderGrabPos - 3 && x <= senderGrabPos + 3) mousePos = OverTextSep;
406 else mousePos = None;
408 // Pass 1: Do whatever we can before switching mouse mode (if at all).
410 // No special mode. Set mouse cursor if appropriate.
413 //if(oldpos != mousePos) {
414 if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
416 int l = yToLineIdx(y);
417 int c = lines[l]->posToCursor(QPointF(x, y - ycoords[l]));
418 if(c >= 0 && lines[l]->isUrl(c)) {
419 setCursor(Qt::PointingHandCursor);
421 setCursor(Qt::ArrowCursor);
426 // Left button pressed. Might initiate marking or drag & drop if we moved past the drag distance.
428 if(!dragStartPos.isNull() && (dragStartPos - pos).manhattanLength() >= QApplication::startDragDistance()) {
429 // Moving a column separator?
430 if(dragStartMode == DragTsSep) mouseMode = DragTsSep;
431 else if(dragStartMode == DragTextSep) mouseMode = DragTextSep;
432 // Nope. Check if we are over a selection to start drag & drop.
433 else if(dragStartMode == Normal) {
434 bool dragdrop = false;
435 if(selectionMode == TextSelected) {
436 int l = yToLineIdx(y);
437 if(selectionLine == l) {
438 int p = lines[l]->posToCursor(QPointF(x, y - ycoords[l]));
439 if(p >= selectionStart && p <= selectionEnd) dragdrop = true;
441 } else if(selectionMode == LinesSelected) {
442 int l = yToLineIdx(y);
443 if(l >= selectionStart && l <= selectionEnd) dragdrop = true;
445 // Ok, so just start drag & drop if appropriate.
447 QDrag *drag = new QDrag(this);
448 QMimeData *mimeData = new QMimeData;
449 mimeData->setText(selectionToString());
450 drag->setMimeData(mimeData);
453 // Otherwise, clear the selection and start text marking!
455 setCursor(Qt::ArrowCursor);
457 if(dragStartCursor < 0) { mouseMode = MarkLines; curLine = -1; }
458 else mouseMode = MarkText;
470 // Pass 2: Some mouse modes need work after being set...
471 if(mouseMode == DragTsSep && x < size().width() - QtUi::style()->sepSenderText() - senderWidth - 10) {
472 // Drag first column separator
473 int foo = QtUi::style()->sepTsSender()/2;
474 tsWidth = qMax(x, foo) - foo;
477 } else if(mouseMode == DragTextSep && x < size().width() - 10) {
478 // Drag second column separator
479 int foo = tsWidth + QtUi::style()->sepTsSender() + QtUi::style()->sepSenderText()/2;
480 senderWidth = qMax(x, foo) - foo;
483 } else if(mouseMode == MarkText) {
484 // Change currently marked text
485 curLine = yToLineIdx(y);
486 int c = lines[curLine]->posToCursor(QPointF(x, y - ycoords[curLine]));
487 if(curLine == dragStartLine && c >= 0) {
490 lines[curLine]->setSelection(ChatLine::Partial, dragStartCursor, c);
491 viewport()->update();
494 mouseMode = MarkLines;
495 selectionStart = qMin(curLine, dragStartLine); selectionEnd = qMax(curLine, dragStartLine);
496 for(int i = selectionStart; i <= selectionEnd; i++) lines[i]->setSelection(ChatLine::Full);
497 viewport()->update();
499 } else if(mouseMode == MarkLines) {
501 int l = yToLineIdx(y);
503 selectionStart = qMin(l, dragStartLine); selectionEnd = qMax(l, dragStartLine);
505 Q_ASSERT(selectionStart == selectionEnd);
506 lines[l]->setSelection(ChatLine::Full);
508 if(curLine < selectionStart) {
509 for(int i = curLine; i < selectionStart; i++) lines[i]->setSelection(ChatLine::None);
510 } else if(curLine > selectionEnd) {
511 for(int i = selectionEnd+1; i <= curLine; i++) lines[i]->setSelection(ChatLine::None);
512 } else if(selectionStart < curLine && l < curLine) {
513 for(int i = selectionStart; i < curLine; i++) lines[i]->setSelection(ChatLine::Full);
514 } else if(curLine < selectionEnd && l > curLine) {
515 for(int i = curLine+1; i <= selectionEnd; i++) lines[i]->setSelection(ChatLine::Full);
520 viewport()->update();
525 //!\brief Clear current text selection.
526 void ChatWidget::clearSelection() {
527 if(selectionMode == TextSelected) {
528 lines[selectionLine]->setSelection(ChatLine::None);
529 } else if(selectionMode == LinesSelected) {
530 for(int i = selectionStart; i <= selectionEnd; i++) {
531 lines[i]->setSelection(ChatLine::None);
534 selectionMode = NoSelection;
535 viewport()->update();
538 //!\brief Convert current selection to human-readable string.
539 QString ChatWidget::selectionToString() {
540 //TODO Make selection format configurable!
541 if(selectionMode == NoSelection) return "";
542 if(selectionMode == LinesSelected) {
544 for(int l = selectionStart; l <= selectionEnd; l++) {
545 result += QString("[%1] %2 %3\n").arg(lines[l]->timestamp().toLocalTime().toString("hh:mm:ss"))
546 . arg(lines[l]->sender()).arg(lines[l]->text());
550 // selectionMode == TextSelected
551 return lines[selectionLine]->text().mid(selectionStart, selectionEnd - selectionStart);