#include "chatline-old.h"
#include "qtui.h"
#include "uisettings.h"
-
-ChatWidget::ChatWidget(QWidget *parent) : QAbstractScrollArea(parent) {
+#include "client.h"
+#include "buffer.h"
+#include "clientbacklogmanager.h"
+
+ChatWidget::ChatWidget(BufferId bufid, QWidget *parent) : QAbstractScrollArea(parent), AbstractChatView(),
+ lastBacklogOffset(0),
+ lastBacklogSize(0)
+{
//setAutoFillBackground(false);
//QPalette palette;
//palette.setColor(backgroundRole(), QColor(0, 0, 0, 50));
pointerPosition = QPoint(0,0);
connect(verticalScrollBar(), SIGNAL(actionTriggered(int)), this, SLOT(scrollBarAction(int)));
connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(scrollBarValChanged(int)));
+
+ init(bufid);
}
void ChatWidget::init(BufferId id) {
mouseMode = Normal;
selectionMode = NoSelection;
connect(scrollTimer, SIGNAL(timeout()), this, SLOT(handleScrollTimer()));
+
+ if(bufferId.isValid())
+ connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(viewportChanged(int)));
}
ChatWidget::~ChatWidget() {
//qDebug() << "destroying chatwidget" << bufferName;
- //foreach(ChatLine *l, lines) {
+ //foreach(ChatLineOld *l, lines) {
// delete l;
//}
UiSettings s;
}
void ChatWidget::prependMsg(AbstractUiMsg *msg) {
- ChatLine *line = dynamic_cast<ChatLine*>(msg);
+ ChatLineOld *line = dynamic_cast<ChatLineOld*>(msg);
Q_ASSERT(line);
prependChatLine(line);
}
-void ChatWidget::prependChatLine(ChatLine *line) {
+void ChatWidget::prependChatLine(ChatLineOld *line) {
qreal h = line->layout(tsWidth, senderWidth, textWidth);
for(int i = 1; i < ycoords.count(); i++) ycoords[i] += h;
ycoords.insert(1, h);
viewport()->update();
}
-void ChatWidget::prependChatLines(QList<ChatLine *> clist) {
+void ChatWidget::prependChatLines(QList<ChatLineOld *> clist) {
QList<qreal> tmpy; tmpy.append(0);
qreal h = 0;
- foreach(ChatLine *l, clist) {
+ foreach(ChatLineOld *l, clist) {
h += l->layout(tsWidth, senderWidth, textWidth);
tmpy.append(h);
}
}
void ChatWidget::appendMsg(AbstractUiMsg *msg) {
- ChatLine *line = dynamic_cast<ChatLine*>(msg);
+ ChatLineOld *line = dynamic_cast<ChatLineOld*>(msg);
Q_ASSERT(line);
appendChatLine(line);
}
-void ChatWidget::appendChatLine(ChatLine *line) {
+void ChatWidget::appendChatLine(ChatLineOld *line) {
qreal h = line->layout(tsWidth, senderWidth, textWidth);
ycoords.append(h + ycoords[ycoords.count() - 1]);
height += h;
}
-void ChatWidget::appendChatLines(QList<ChatLine *> list) {
- foreach(ChatLine *line, list) {
+void ChatWidget::appendChatLines(QList<ChatLineOld *> list) {
+ foreach(ChatLineOld *line, list) {
qreal h = line->layout(tsWidth, senderWidth, textWidth);
ycoords.append(h + ycoords[ycoords.count() - 1]);
height += h;
viewport()->update();
}
-void ChatWidget::setContents(QList<ChatLine *> list) {
+void ChatWidget::setContents(const QList<AbstractUiMsg *> &list) {
ycoords.clear();
ycoords.append(0);
height = 0;
lines.clear();
- appendChatLines(list);
+ QList<ChatLineOld *> cl;
+ foreach(AbstractUiMsg *msg, list) cl << dynamic_cast<ChatLineOld *>(msg);
+ appendChatLines(cl);
}
//!\brief Computes the different x position vars for given tsWidth and senderWidth.
if(lines.count() <= l)
return;
- ChatLine *line = lines[l];
+ ChatLineOld *line = lines[l];
QString text = line->text();
int cursorAt = qMax(0, line->posToCursor(QPointF(x, y - ycoords[l])) - 1);
len = end - start;
}
QString word = text.mid(start, len);
- if(word.startsWith("http://")) {
+ QRegExp regex("^(h|f)t{1,2}ps?:\\/\\/");
+ if(regex.indexIn(word) != -1) {
QDesktopServices::openUrl(QUrl(word));
}
selectionStart = qMin(dragStartCursor, curCursor);
selectionEnd = qMax(dragStartCursor, curCursor);
// TODO Make X11SelectionMode configurable!
+#ifdef Q_WS_X11
+ QApplication::clipboard()->setText(selectionToString(), QClipboard::Selection);
+#else
QApplication::clipboard()->setText(selectionToString());
+#endif
break;
case MarkLines:
mouseMode = Normal;
selectionStart = qMin(dragStartLine, curLine);
selectionEnd = qMax(dragStartLine, curLine);
// TODO Make X11SelectionMode configurable!
+#ifdef Q_WS_X11
+ QApplication::clipboard()->setText(selectionToString(), QClipboard::Selection);
+#else
QApplication::clipboard()->setText(selectionToString());
+#endif
break;
default:
mouseMode = Normal;
if(curLine == dragStartLine && c >= 0) {
if(c != curCursor) {
curCursor = c;
- lines[curLine]->setSelection(ChatLine::Partial, dragStartCursor, c);
+ lines[curLine]->setSelection(ChatLineOld::Partial, dragStartCursor, c);
viewport()->update();
}
} else {
mouseMode = MarkLines;
selectionStart = qMin(curLine, dragStartLine); selectionEnd = qMax(curLine, dragStartLine);
- for(int i = selectionStart; i <= selectionEnd; i++) lines[i]->setSelection(ChatLine::Full);
+ for(int i = selectionStart; i <= selectionEnd; i++) lines[i]->setSelection(ChatLineOld::Full);
viewport()->update();
}
} else if(mouseMode == MarkLines) {
selectionStart = qMin(l, dragStartLine); selectionEnd = qMax(l, dragStartLine);
if(curLine < 0) {
Q_ASSERT(selectionStart == selectionEnd);
- lines[l]->setSelection(ChatLine::Full);
+ lines[l]->setSelection(ChatLineOld::Full);
} else {
if(curLine < selectionStart) {
- for(int i = curLine; i < selectionStart; i++) lines[i]->setSelection(ChatLine::None);
+ for(int i = curLine; i < selectionStart; i++) lines[i]->setSelection(ChatLineOld::None);
} else if(curLine > selectionEnd) {
- for(int i = selectionEnd+1; i <= curLine; i++) lines[i]->setSelection(ChatLine::None);
+ for(int i = selectionEnd+1; i <= curLine; i++) lines[i]->setSelection(ChatLineOld::None);
} else if(selectionStart < curLine && l < curLine) {
- for(int i = selectionStart; i < curLine; i++) lines[i]->setSelection(ChatLine::Full);
+ for(int i = selectionStart; i < curLine; i++) lines[i]->setSelection(ChatLineOld::Full);
} else if(curLine < selectionEnd && l > curLine) {
- for(int i = curLine+1; i <= selectionEnd; i++) lines[i]->setSelection(ChatLine::Full);
+ for(int i = curLine+1; i <= selectionEnd; i++) lines[i]->setSelection(ChatLineOld::Full);
}
}
curLine = l;
//!\brief Clear current text selection.
void ChatWidget::clearSelection() {
if(selectionMode == TextSelected) {
- lines[selectionLine]->setSelection(ChatLine::None);
+ lines[selectionLine]->setSelection(ChatLineOld::None);
} else if(selectionMode == LinesSelected) {
for(int i = selectionStart; i <= selectionEnd; i++) {
- lines[i]->setSelection(ChatLine::None);
+ lines[i]->setSelection(ChatLineOld::None);
}
}
selectionMode = NoSelection;
return lines[selectionLine]->text().mid(selectionStart, selectionEnd - selectionStart);
}
+void ChatWidget::viewportChanged(int newPos) {
+ const int REQUEST_COUNT = 50;
+ QAbstractSlider *vbar = verticalScrollBar();
+ if(!vbar)
+ return;
+
+ int relativePos = 100;
+ if(vbar->maximum() - vbar->minimum() != 0)
+ relativePos = (newPos - vbar->minimum()) * 100 / (vbar->maximum() - vbar->minimum());
+
+ if(relativePos < 20) {
+ Buffer *buffer = Client::buffer(bufferId);
+ Q_CHECK_PTR(buffer);
+ if(buffer->contents().isEmpty())
+ return;
+ MsgId msgId = buffer->contents().first()->msgId();
+ if(!lastBacklogOffset.isValid() || msgId < lastBacklogOffset && lastBacklogSize + REQUEST_COUNT <= buffer->contents().count()) {
+ Client::backlogManager()->requestBacklog(bufferId, REQUEST_COUNT, msgId.toInt());
+ lastBacklogOffset = msgId;
+ lastBacklogSize = buffer->contents().size();
+ }
+ }
+}