X-Git-Url: https://git.quassel-irc.org/?a=blobdiff_plain;f=src%2Fqtui%2Fchatscene.cpp;h=6dbe17109f9966be9b11ab4debe174c131b96d1b;hb=c55bd7dc80547563d8c9cc70d54aa3d24ff03166;hp=797290d7711a414835b78ba481b09a07c1312d8d;hpb=dab68263675422d923a0ccbd2b84e2e52ed7e157;p=quassel.git diff --git a/src/qtui/chatscene.cpp b/src/qtui/chatscene.cpp index 797290d7..6dbe1710 100644 --- a/src/qtui/chatscene.cpp +++ b/src/qtui/chatscene.cpp @@ -18,6 +18,8 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include +#include #include #include @@ -39,9 +41,11 @@ ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, QObject _width = 0; _selectingItem = 0; _isSelecting = false; + _selectionStart = -1; connect(this, SIGNAL(sceneRectChanged(const QRectF &)), this, SLOT(rectChanged(const QRectF &))); connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int))); + connect(model, SIGNAL(modelAboutToBeReset()), this, SLOT(modelReset())); for(int i = 0; i < model->rowCount(); i++) { ChatLine *line = new ChatLine(model->index(i, 0)); _lines.append(line); @@ -73,7 +77,6 @@ ChatScene::ChatScene(QAbstractItemModel *model, const QString &idString, QObject ChatScene::~ChatScene() { - } void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) { @@ -103,6 +106,15 @@ void ChatScene::rowsInserted(const QModelIndex &index, int start, int end) { } } +void ChatScene::modelReset() { + foreach(ChatLine *line, _lines) { + removeItem(line); + delete line; + } + _lines.clear(); + setSceneRect(QRectF(0, 0, _width, 0)); +} + void ChatScene::setWidth(qreal w) { _width = w; _height = 0; @@ -211,12 +223,25 @@ void ChatScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { } void ChatScene::mousePressEvent(QGraphicsSceneMouseEvent *event) { - qDebug() << "pressed"; - QGraphicsScene::mousePressEvent(event); + if(event->buttons() & Qt::LeftButton && _selectionStart >= 0) { + for(int l = qMin(_selectionStart, _selectionEnd); l <= qMax(_selectionStart, _selectionEnd); l++) { + _lines[l]->setSelected(false); + } + _selectionStart = -1; + event->accept(); + } else { + QGraphicsScene::mousePressEvent(event); + } } void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { if(_isSelecting) { +# ifdef Q_WS_X11 + QApplication::clipboard()->setText(selectionToString(), QClipboard::Selection); +# endif +//# else + QApplication::clipboard()->setText(selectionToString()); +//# endif _isSelecting = false; event->accept(); } else { @@ -224,3 +249,17 @@ void ChatScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) { } } +//!\brief Convert current selection to human-readable string. +QString ChatScene::selectionToString() const { + //TODO Make selection format configurable! + if(!_isSelecting) return ""; + QString result; + for(int l = _selectionStart; l <= _selectionEnd; l++) { + if(_selectionMinCol == ChatLineModel::TimestampColumn) + result += _lines[l]->item(ChatLineModel::TimestampColumn)->data(MessageModel::DisplayRole).toString() + " "; + if(_selectionMinCol <= ChatLineModel::SenderColumn) + result += _lines[l]->item(ChatLineModel::SenderColumn)->data(MessageModel::DisplayRole).toString() + " "; + result += _lines[l]->item(ChatLineModel::ContentsColumn)->data(MessageModel::DisplayRole).toString() + "\n"; + } + return result; +}