d67eb9efff46090fa2205dc0d275b181925446be
[quassel.git] / src / qtgui / chatwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel Team                             *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "util.h"
22 #include "style.h"
23 #include "chatwidget.h"
24 #include "chatline.h"
25
26
27 ChatWidget::ChatWidget(QWidget *parent) : QAbstractScrollArea(parent) {
28   scrollTimer = new QTimer(this);
29   scrollTimer->setSingleShot(false);
30   scrollTimer->setInterval(100);
31   setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
32   setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
33   setMinimumSize(QSize(400,400));
34   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
35   setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
36   bottomLine = -1;
37   height = 0;
38   ycoords.append(0);
39   pointerPosition = QPoint(0,0);
40   connect(verticalScrollBar(), SIGNAL(actionTriggered(int)), this, SLOT(scrollBarAction(int)));
41   connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(scrollBarValChanged(int)));
42 }
43
44 void ChatWidget::init(QString netname, QString bufname) {
45   networkName = netname;
46   bufferName = bufname;
47   setBackgroundRole(QPalette::Base);
48   setFont(QFont("Fixed"));
49   tsWidth = 90;
50   senderWidth = 100;
51   computePositions();
52   adjustScrollBar();
53   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
54   //verticalScrollBar()->setPageStep(viewport()->height());
55   //verticalScrollBar()->setSingleStep(20);
56   //verticalScrollBar()->setMinimum(0);
57   //verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
58
59   setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
60   setMouseTracking(true);
61   mouseMode = Normal;
62   selectionMode = NoSelection;
63   connect(scrollTimer, SIGNAL(timeout()), this, SLOT(handleScrollTimer()));
64 }
65
66 ChatWidget::~ChatWidget() {
67   //qDebug() << "destroying chatwidget" << bufferName;
68   //foreach(ChatLine *l, lines) {
69   //  delete l;
70   //}
71 }
72
73 QSize ChatWidget::sizeHint() const {
74   //qDebug() << size();
75   return size();
76 }
77
78 void ChatWidget::adjustScrollBar() {
79   verticalScrollBar()->setPageStep(viewport()->height());
80   verticalScrollBar()->setSingleStep(20);
81   verticalScrollBar()->setMinimum(0);
82   verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
83   //qDebug() << height << viewport()->height() << verticalScrollBar()->pageStep();
84   //if(bottomLine < 0) {
85   //  verticalScrollBar()->setValue(verticalScrollBar()->maximum());
86   //} else {
87     //int bot = verticalScrollBar()->value() + viewport()->height(); //qDebug() << bottomLine;
88     //verticalScrollBar()->setValue(qMax(0, (int)ycoords[bottomLine+1] - viewport()->height()));
89   //}
90 }
91
92 void ChatWidget::scrollBarValChanged(int val) {
93   return;
94   if(val >= verticalScrollBar()->maximum()) bottomLine = -1;
95   else {
96     int bot = val + viewport()->height();
97     int line = yToLineIdx(bot);
98     //bottomLine = line;
99   }
100 }
101
102 void ChatWidget::scrollBarAction(int action) {
103   switch(action) {
104     case QScrollBar::SliderSingleStepAdd:
105       // More elaborate. But what with loooong lines?
106       // verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value() + viewport()->height()) + 1] - viewport()->height());
107       break;
108     case QScrollBar::SliderSingleStepSub:
109       //verticalScrollBar()->setValue((int)ycoords[yToLineIdx(verticalScrollBar()->value())]);
110       break;
111
112   }
113
114 }
115
116 void ChatWidget::handleScrollTimer() {
117   if(mouseMode == MarkText || mouseMode == MarkLines) {
118     if(pointerPosition.y() > viewport()->height()) {
119       verticalScrollBar()->setValue(verticalScrollBar()->value() + pointerPosition.y() - viewport()->height());
120       handleMouseMoveEvent(QPoint(pointerPosition.x(), viewport()->height()));
121     } else if(pointerPosition.y() < 0) {
122       verticalScrollBar()->setValue(verticalScrollBar()->value() + pointerPosition.y());
123       handleMouseMoveEvent(QPoint(pointerPosition.x(), 0));
124     }
125   }
126 }
127
128 void ChatWidget::ensureVisible(int line) {
129   int top = verticalScrollBar()->value();
130   int bot = top + viewport()->height();
131   if(ycoords[line+1] > bot) {
132     verticalScrollBar()->setValue(qMax(0, (int)ycoords[line+1] - viewport()->height()));
133   } else if(ycoords[line] < top) {
134     verticalScrollBar()->setValue((int)ycoords[line]);
135   }
136
137 }
138
139 void ChatWidget::clear() {
140   //contents->clear();
141 }
142
143 void ChatWidget::prependMsg(AbstractUiMsg *msg) {
144   ChatLine *line = dynamic_cast<ChatLine*>(msg);
145   Q_ASSERT(line);
146   prependChatLine(line);
147 }
148
149 void ChatWidget::prependChatLine(ChatLine *line) {
150   qreal h = line->layout(tsWidth, senderWidth, textWidth);
151   for(int i = 1; i < ycoords.count(); i++) ycoords[i] += h;
152   ycoords.insert(1, h);
153   lines.prepend(line);
154   height += h;
155   // Fix all variables containing line numbers
156   dragStartLine ++;
157   curLine ++;
158   selectionStart ++; selectionEnd ++;
159   adjustScrollBar();
160   verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
161   viewport()->update();
162 }
163
164 void ChatWidget::prependChatLines(QList<ChatLine *> clist) {
165   QList<qreal> tmpy; tmpy.append(0);
166   qreal h = 0;
167   foreach(ChatLine *l, clist) {
168     h += l->layout(tsWidth, senderWidth, textWidth);
169     tmpy.append(h);
170   }
171   ycoords.removeFirst();
172   for(int i = 0; i < ycoords.count(); i++) ycoords[i] += h;
173   ycoords = tmpy + ycoords;
174   lines = clist + lines;
175   height += h;
176   // Fix all variables containing line numbers
177   int i = clist.count();
178   dragStartLine += i;
179   curLine += i;
180   selectionStart += i; selectionEnd += i; //? selectionEnd += i;
181   //if(bottomLine >= 0) bottomLine += i;
182   adjustScrollBar();
183   //verticalScrollBar()->setPageStep(viewport()->height());
184   //verticalScrollBar()->setSingleStep(20);
185   //verticalScrollBar()->setMaximum((int)height - verticalScrollBar()->pageStep());
186   verticalScrollBar()->setValue(verticalScrollBar()->value() + (int)h);
187   viewport()->update();
188 }
189
190 void ChatWidget::appendMsg(AbstractUiMsg *msg) {
191   ChatLine *line = dynamic_cast<ChatLine*>(msg);
192   Q_ASSERT(line);
193   appendChatLine(line);
194 }
195
196 void ChatWidget::appendChatLine(ChatLine *line) {
197   qreal h = line->layout(tsWidth, senderWidth, textWidth);
198   ycoords.append(h + ycoords[ycoords.count() - 1]);
199   height += h;
200   bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
201   adjustScrollBar();
202   if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
203   lines.append(line);
204   viewport()->update();
205 }
206
207
208 void ChatWidget::appendChatLines(QList<ChatLine *> list) {
209   foreach(ChatLine *line, list) {
210     qreal h = line->layout(tsWidth, senderWidth, textWidth);
211     ycoords.append(h + ycoords[ycoords.count() - 1]);
212     height += h;
213     lines.append(line);
214   }
215   bool flg = (verticalScrollBar()->value() == verticalScrollBar()->maximum());
216   adjustScrollBar();
217   if(flg) verticalScrollBar()->setValue(verticalScrollBar()->maximum());
218   viewport()->update();
219 }
220
221 void ChatWidget::setContents(QList<ChatLine *> list) {
222   ycoords.clear();
223   ycoords.append(0);
224   height = 0;
225   lines.clear();
226   appendChatLines(list);
227 }
228
229 //!\brief Computes the different x position vars for given tsWidth and senderWidth.
230 void ChatWidget::computePositions() {
231   senderX = tsWidth + Style::sepTsSender();
232   textX = senderX + senderWidth + Style::sepSenderText();
233   tsGrabPos = tsWidth + (int)Style::sepTsSender()/2;
234   senderGrabPos = senderX + senderWidth + (int)Style::sepSenderText()/2;
235   textWidth = viewport()->size().width() - textX;
236 }
237
238 void ChatWidget::resizeEvent(QResizeEvent *event) {
239   //qDebug() << bufferName << isVisible() << event->size() << event->oldSize();
240   /*if(event->oldSize().isValid())*/
241   //contents->setWidth(event->size().width());
242   //setAlignment(Qt::AlignBottom);
243   if(event->size().width() != event->oldSize().width()) {
244     computePositions();
245     layout();
246   }
247   //adjustScrollBar();
248   //qDebug() << viewport()->size() << viewport()->height();
249   //QAbstractScrollArea::resizeEvent(event);
250   //qDebug() << viewport()->size() << viewport()->geometry();
251 }
252
253 void ChatWidget::paintEvent(QPaintEvent *event) {
254   QPainter painter(viewport());
255
256   //qDebug() <<  verticalScrollBar()->value();
257   painter.translate(0, -verticalScrollBar()->value());
258   int top = event->rect().top() + verticalScrollBar()->value();
259   int bot = top + event->rect().height();
260   int idx = yToLineIdx(top);
261   if(idx < 0) return;
262   for(int i = idx; i < lines.count() ; i++) {
263     lines[i]->draw(&painter, QPointF(0, ycoords[i]));
264     if(ycoords[i+1] > bot) return;
265   }
266 }
267
268 //!\brief Layout the widget.
269 void ChatWidget::layout() {
270   // TODO fix scrollbars
271   //int botLine = yToLineIdx(verticalScrollBar()->value() + 
272   qreal y = 0;
273   for(int i = 0; i < lines.count(); i++) {
274     qreal h = lines[i]->layout(tsWidth, senderWidth, textWidth);
275     ycoords[i+1] = h + ycoords[i];
276   }
277   height = ycoords[ycoords.count()-1];
278   adjustScrollBar();
279   verticalScrollBar()->setValue(verticalScrollBar()->maximum());
280   viewport()->update();
281 }
282
283 int ChatWidget::yToLineIdx(qreal y) {
284   if(y >= ycoords[ycoords.count()-1]) ycoords.count()-1;
285   if(ycoords.count() <= 1) return 0;
286   int uidx = 0;
287   int oidx = ycoords.count() - 1;
288   int idx;
289   while(1) {
290     if(uidx == oidx - 1) return uidx;
291     idx = (uidx + oidx) / 2;
292     if(ycoords[idx] > y) oidx = idx;
293     else uidx = idx;
294   }
295 }
296
297 void ChatWidget::mousePressEvent(QMouseEvent *event) {
298   if(lines.isEmpty()) return;
299   QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
300   if(event->button() == Qt::LeftButton) {
301     dragStartPos = pos;
302     dragStartMode = Normal;
303     switch(mouseMode) {
304       case Normal:
305         if(mousePos == OverTsSep) {
306           dragStartMode = DragTsSep;
307           setCursor(Qt::ClosedHandCursor);
308         } else if(mousePos == OverTextSep) {
309           dragStartMode = DragTextSep;
310           setCursor(Qt::ClosedHandCursor);
311         } else {
312           dragStartLine = yToLineIdx(pos.y());
313           dragStartCursor = lines[dragStartLine]->posToCursor(QPointF(pos.x(), pos.y()-ycoords[dragStartLine]));
314         }
315         mouseMode = Pressed;
316         break;
317     }
318   }
319 }
320
321 void ChatWidget::mouseDoubleClickEvent(QMouseEvent *event) {
322
323
324
325 }
326
327 void ChatWidget::mouseReleaseEvent(QMouseEvent *event) {
328   //QPoint pos = event->pos() + QPoint(0, verticalScrollBar()->value());
329
330   if(event->button() == Qt::LeftButton) {
331     dragStartPos = QPoint();
332     if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
333     else setCursor(Qt::ArrowCursor);
334
335     switch(mouseMode) {
336       case Pressed:
337         mouseMode = Normal;
338         clearSelection();
339         break;
340       case MarkText:
341         mouseMode = Normal;
342         selectionMode = TextSelected;
343         selectionLine = dragStartLine;
344         selectionStart = qMin(dragStartCursor, curCursor);
345         selectionEnd = qMax(dragStartCursor, curCursor);
346         // TODO Make X11SelectionMode configurable!
347         QApplication::clipboard()->setText(selectionToString());
348         break;
349       case MarkLines:
350         mouseMode = Normal;
351         selectionMode = LinesSelected;
352         selectionStart = qMin(dragStartLine, curLine);
353         selectionEnd = qMax(dragStartLine, curLine);
354         // TODO Make X11SelectionMode configurable!
355         QApplication::clipboard()->setText(selectionToString());
356         break;
357       default:
358         mouseMode = Normal;
359     }
360   }
361 }
362
363 //!\brief React to mouse movements over the ChatWidget.
364 /** This is called by Qt whenever the mouse moves. Here we have to do most of the mouse handling,
365  * such as changing column widths, marking text or initiating drag & drop.
366  */
367 void ChatWidget::mouseMoveEvent(QMouseEvent *event) {
368   QPoint pos = event->pos(); pointerPosition = pos;
369   // Scroll if mouse pointer leaves widget while dragging
370   if((mouseMode == MarkText || mouseMode == MarkLines) && (pos.y() > viewport()->height() || pos.y() < 0)) {
371     if(!scrollTimer->isActive()) {
372       scrollTimer->start();
373     }
374   } else {
375     if(scrollTimer->isActive()) {
376       scrollTimer->stop();
377     }
378   }
379   handleMouseMoveEvent(pos);
380 }
381
382 void ChatWidget::handleMouseMoveEvent(const QPoint &_pos) {
383   // FIXME
384   if(lines.count() <= 0) return;
385   // Set some basic properties of the current position
386   QPoint pos = _pos + QPoint(0, verticalScrollBar()->value());
387   int x = pos.x();
388   int y = pos.y();
389   MousePos oldpos = mousePos;
390   if(x >= tsGrabPos - 3 && x <= tsGrabPos + 3) mousePos = OverTsSep;
391   else if(x >= senderGrabPos - 3 && x <= senderGrabPos + 3) mousePos = OverTextSep;
392   else mousePos = None;
393
394   // Pass 1: Do whatever we can before switching mouse mode (if at all).
395   switch(mouseMode) {
396     // No special mode. Set mouse cursor if appropriate.
397     case Normal:
398     {
399       //if(oldpos != mousePos) {
400       if(mousePos == OverTsSep || mousePos == OverTextSep) setCursor(Qt::OpenHandCursor);
401       else {
402         int l = yToLineIdx(y);
403         int c = lines[l]->posToCursor(QPointF(x, y - ycoords[l]));
404         if(c >= 0 && lines[l]->isUrl(c)) {
405           setCursor(Qt::PointingHandCursor);
406         } else {
407           setCursor(Qt::ArrowCursor);
408         }
409       }
410     }
411       break;
412     // Left button pressed. Might initiate marking or drag & drop if we moved past the drag distance.
413     case Pressed:
414       if(!dragStartPos.isNull() && (dragStartPos - pos).manhattanLength() >= QApplication::startDragDistance()) {
415         // Moving a column separator?
416         if(dragStartMode == DragTsSep) mouseMode = DragTsSep;
417         else if(dragStartMode == DragTextSep) mouseMode = DragTextSep;
418         // Nope. Check if we are over a selection to start drag & drop.
419         else if(dragStartMode == Normal) {
420           bool dragdrop = false;
421           if(selectionMode == TextSelected) {
422             int l = yToLineIdx(y);
423             if(selectionLine == l) {
424               int p = lines[l]->posToCursor(QPointF(x, y - ycoords[l]));
425               if(p >= selectionStart && p <= selectionEnd) dragdrop = true;
426             }
427           } else if(selectionMode == LinesSelected) {
428             int l = yToLineIdx(y);
429             if(l >= selectionStart && l <= selectionEnd) dragdrop = true;
430           }
431           // Ok, so just start drag & drop if appropriate.
432           if(dragdrop) {
433             QDrag *drag = new QDrag(this);
434             QMimeData *mimeData = new QMimeData;
435             mimeData->setText(selectionToString());
436             drag->setMimeData(mimeData);
437             drag->start();
438             mouseMode = Normal;
439           // Otherwise, clear the selection and start text marking!
440           } else {
441             setCursor(Qt::ArrowCursor);
442             clearSelection();
443             if(dragStartCursor < 0) { mouseMode = MarkLines; curLine = -1; }
444             else mouseMode = MarkText;
445           }
446         }
447       }
448       break;
449     case DragTsSep:
450       break;
451     case DragTextSep:
452       break;
453   }
454   // Pass 2: Some mouse modes need work after being set...
455   if(mouseMode == DragTsSep && x < size().width() - Style::sepSenderText() - senderWidth - 10) {
456     // Drag first column separator
457     int foo = Style::sepTsSender()/2;
458     tsWidth = qMax(x, foo) - foo;
459     computePositions();
460     layout();
461   } else if(mouseMode == DragTextSep && x < size().width() - 10) {
462     // Drag second column separator
463     int foo = tsWidth + Style::sepTsSender() + Style::sepSenderText()/2;
464     senderWidth = qMax(x, foo) - foo;
465     computePositions();
466     layout();
467   } else if(mouseMode == MarkText) {
468     // Change currently marked text
469     curLine = yToLineIdx(y);
470     int c = lines[curLine]->posToCursor(QPointF(x, y - ycoords[curLine]));
471     if(curLine == dragStartLine && c >= 0) {
472       if(c != curCursor) {
473         curCursor = c;
474         lines[curLine]->setSelection(ChatLine::Partial, dragStartCursor, c);
475         viewport()->update();
476       }
477     } else {
478       mouseMode = MarkLines;
479       selectionStart = qMin(curLine, dragStartLine); selectionEnd = qMax(curLine, dragStartLine);
480       for(int i = selectionStart; i <= selectionEnd; i++) lines[i]->setSelection(ChatLine::Full);
481       viewport()->update();
482     }
483   } else if(mouseMode == MarkLines) {
484     // Line marking
485     int l = yToLineIdx(y);
486     if(l != curLine) {
487       selectionStart = qMin(l, dragStartLine); selectionEnd = qMax(l, dragStartLine);
488       if(curLine < 0) {
489         Q_ASSERT(selectionStart == selectionEnd);
490         lines[l]->setSelection(ChatLine::Full);
491       } else {
492         if(curLine < selectionStart) {
493           for(int i = curLine; i < selectionStart; i++) lines[i]->setSelection(ChatLine::None);
494         } else if(curLine > selectionEnd) {
495           for(int i = selectionEnd+1; i <= curLine; i++) lines[i]->setSelection(ChatLine::None);
496         } else if(selectionStart < curLine && l < curLine) {
497           for(int i = selectionStart; i < curLine; i++) lines[i]->setSelection(ChatLine::Full);
498         } else if(curLine < selectionEnd && l > curLine) {
499           for(int i = curLine+1; i <= selectionEnd; i++) lines[i]->setSelection(ChatLine::Full);
500         }
501       }
502       curLine = l;
503       //ensureVisible(l);
504       viewport()->update();
505     }
506   }
507 }
508
509 //!\brief Clear current text selection.
510 void ChatWidget::clearSelection() {
511   if(selectionMode == TextSelected) {
512     lines[selectionLine]->setSelection(ChatLine::None);
513   } else if(selectionMode == LinesSelected) {
514     for(int i = selectionStart; i <= selectionEnd; i++) {
515       lines[i]->setSelection(ChatLine::None);
516     }
517   }
518   selectionMode = NoSelection;
519   viewport()->update();
520 }
521
522 //!\brief Convert current selection to human-readable string.
523 QString ChatWidget::selectionToString() {
524   //TODO Make selection format configurable!
525   if(selectionMode == NoSelection) return "";
526   if(selectionMode == LinesSelected) {
527     QString result;
528     for(int l = selectionStart; l <= selectionEnd; l++) {
529       result += QString("[%1] %2 %3\n").arg(lines[l]->timeStamp().toLocalTime().toString("hh:mm:ss"))
530           .arg(lines[l]->sender()).arg(lines[l]->text());
531     }
532     return result;
533   }
534   // selectionMode == TextSelected
535   return lines[selectionLine]->text().mid(selectionStart, selectionEnd - selectionStart);
536 }
537
538 /************************************************************************************/
539
540
541 /******************************************************************************************************************/
542