plotting
diff --git a/chatdialog.cpp b/chatdialog.cpp
index 34f5682..b7edf20 100644
--- a/chatdialog.cpp
+++ b/chatdialog.cpp
@@ -17,7 +17,7 @@
DigestTreeScene *scene = new DigestTreeScene();
treeViewer->setScene(scene);
- scene->plot();
+ //scene->plot();
connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed()));
connect(setButton, SIGNAL(pressed()), this, SLOT(buttonPressed()));
@@ -53,8 +53,8 @@
void
ChatDialog::formChatMessage(const QString &text, SyncDemo::ChatMessage &msg) {
- msg.set_from(m_nick.toStdString());
- msg.set_to(m_chatroom.toStdString());
+ msg.set_from(m_user.getNick().toStdString());
+ msg.set_to(m_user.getChatroom().toStdString());
msg.set_data(text.toStdString());
time_t seconds = time(NULL);
msg.set_timestamp(seconds);
@@ -64,29 +64,34 @@
ChatDialog::readSettings()
{
QSettings s(ORGANIZATION, APPLICATION);
- m_nick = s.value("nick", "").toString();
- m_chatroom = s.value("chatroom", "").toString();
- m_prefix = s.value("prefix", "").toString();
- if (m_nick == "" || m_chatroom == "" || m_prefix == "") {
+ QString nick = s.value("nick", "").toString();
+ QString chatroom = s.value("chatroom", "").toString();
+ QString prefix = s.value("prefix", "").toString();
+ if (nick == "" || chatroom == "" || prefix == "") {
QTimer::singleShot(500, this, SLOT(buttonPressed()));
}
+ else {
+ m_user.setNick(nick);
+ m_user.setChatroom(chatroom);
+ m_user.setPrefix(prefix);
+ }
}
void
ChatDialog::writeSettings()
{
QSettings s(ORGANIZATION, APPLICATION);
- s.setValue("nick", m_nick);
- s.setValue("chatroom", m_chatroom);
- s.setValue("prefix", m_prefix);
+ s.setValue("nick", m_user.getNick());
+ s.setValue("chatroom", m_user.getChatroom());
+ s.setValue("prefix", m_user.getPrefix());
}
void
ChatDialog::updateLabels()
{
- QString settingDisp = QString("<User: %1>, <Chatroom: %2>").arg(m_nick).arg(m_chatroom);
+ QString settingDisp = QString("<User: %1>, <Chatroom: %2>").arg(m_user.getNick()).arg(m_user.getChatroom());
infoLabel->setText(settingDisp);
- QString prefixDisp = QString("<Prefix: %1>").arg(m_prefix);
+ QString prefixDisp = QString("<Prefix: %1>").arg(m_user.getPrefix());
prefixLabel->setText(prefixDisp);
}
@@ -111,7 +116,7 @@
void
ChatDialog::buttonPressed()
{
- SettingDialog dialog(this, m_nick, m_chatroom, m_prefix);
+ SettingDialog dialog(this, m_user.getNick(), m_user.getChatroom(), m_user.getPrefix());
connect(&dialog, SIGNAL(updated(QString, QString, QString)), this, SLOT(settingUpdated(QString, QString, QString)));
dialog.exec();
}
@@ -120,17 +125,17 @@
ChatDialog::settingUpdated(QString nick, QString chatroom, QString prefix)
{
bool needWrite = false;
- if (!nick.isEmpty() && nick != m_nick) {
- m_nick = nick;
+ if (!nick.isEmpty() && nick != m_user.getNick()) {
+ m_user.setNick(nick);
needWrite = true;
}
- if (!prefix.isEmpty() && prefix != m_prefix) {
- m_prefix = prefix;
+ if (!prefix.isEmpty() && prefix != m_user.getPrefix()) {
+ m_user.setPrefix(prefix);
needWrite = true;
// TODO: set the previous prefix as left?
}
- if (!chatroom.isEmpty() && chatroom != m_chatroom) {
- m_chatroom = chatroom;
+ if (!chatroom.isEmpty() && chatroom != m_user.getChatroom()) {
+ m_user.setChatroom(chatroom);
needWrite = true;
// TODO: perhaps need to do a lot. e.g. use a new SyncAppSokcet
}
diff --git a/chatdialog.h b/chatdialog.h
index 337f20f..3ae668b 100644
--- a/chatdialog.h
+++ b/chatdialog.h
@@ -33,9 +33,7 @@
void settingUpdated(QString, QString, QString);
private:
- QString m_nick;
- QString m_chatroom;
- QString m_prefix;
+ User m_user;
Sync::SyncAppSocket *m_sock;
};
#endif
diff --git a/digesttreescene.cpp b/digesttreescene.cpp
index 483f1f6..2d2f631 100644
--- a/digesttreescene.cpp
+++ b/digesttreescene.cpp
@@ -6,6 +6,7 @@
#include <vector>
#include <iostream>
#include <assert.h>
+#include <boost/lexical_cast.hpp>
static const double Pi = 3.14159265358979323846264338327950288419717;
@@ -15,86 +16,176 @@
}
void
-DigestTreeScene::plot() {
+DigestTreeScene::processUpdate(std::vector<Sync::MissingDataInfo> &v, QString digest)
+{
+ int n = v.size();
+ bool rePlot = false;
+ for (int i = 0; i < n; i++)
+ {
+ Roster_iterator it = m_roster.find(v[i].prefix.c_str());
+ if (it == m_roster.end()) {
+ rePlot = true;
+ DisplayUserPtr p(new DisplayUser());
+ p->setPrefix(v[i].prefix.c_str());
+ p->setSeq(v[i].high);
+ m_roster.insert(p->getPrefix(), p);
+ }
+ }
+
+ if (rePlot)
+ {
+ plot(digest);
+ }
+ else
+ {
+ for (int i = 0; i < n; i++)
+ {
+ Roster_iterator it = m_roster.find(v[i].prefix.c_str());
+ if (it != m_roster.end()) {
+ DisplayUserPtr p = it.value();
+ QGraphicsTextItem *item = p->getSeqTextItem();
+ std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
+ item->setPlainText(s.c_str());
+ }
+ }
+ m_rootDigest->setPlainText(digest);
+ }
+}
+
+void
+DigestTreeScene::msgReceived(QString prefix, QString nick)
+{
+ Roster_iterator it = m_roster.find(prefix);
+ if (it != m_roster.end())
+ {
+ DisplayUserPtr p = it.value();
+ if (nick != p->getNick()) {
+ p->setNick(nick);
+ QGraphicsTextItem *nickItem = p->getNickTextItem();
+ nickItem->setPlainText(p->getNick());
+ }
+ QGraphicsRectItem *rimItem = p->getRimRectItem();
+ rimItem->setBrush(QBrush(Qt::red));
+ }
+}
+
+void
+DigestTreeScene::plot(QString digest)
+{
clear();
m_graph.clear();
- std::vector<ogdf::node> v(5);
- for (int i = 0; i < 5; i++)
- v[i] = m_graph.newNode();
-
- m_graph.newEdge(v[0], v[1]);
- m_graph.newEdge(v[0], v[2]);
- m_graph.newEdge(v[0], v[3]);
- m_graph.newEdge(v[0], v[4]);
-
+ int n = m_roster.size();
+ ogdf::node root = m_graph.newNode();
+ int rootIndex = root->index();
+ for (int i = 0; i < n; i++) {
+ ogdf::node leaf = m_graph.newNode();
+ m_graph.newEdge(root, leaf);
+ }
ogdf::GraphAttributes GA(m_graph);
- GA.initAttributes(ogdf::GraphAttributes::nodeLabel);
- int nodeWidth = 30, nodeHeight = 30, siblingDistance = 60;
+
+ int nodeSize = 50;
+ int siblingDistance = 100, levelDistance = 100;
ogdf::TreeLayout layout;
layout.siblingDistance(siblingDistance);
- layout.rootSelection(ogdf::TreeLayout::rootIsSource);
- //layout.call(GA);
+ layout.levelDistance(levelDistance);
layout.callSortByPositions(GA, m_graph);
int width = GA.boundingBox().width();
- std::cout << "GA width " << width << std::endl;
int height = GA.boundingBox().height();
- std::cout << "GA height " << height << std::endl;
- setSceneRect(QRect(0, 0, width + nodeWidth, height + nodeHeight));
- GA.setAllWidth(nodeWidth);
- GA.setAllHeight(nodeHeight);
+ setSceneRect(QRect(- (width + nodeSize) / 2, - 40, width + nodeSize, height + nodeSize));
+ GA.setAllWidth(nodeSize);
+ GA.setAllHeight(nodeSize);
+ plotEdge(GA);
+ plotNode(GA, rootIndex, digest);
+
+}
+
+void
+DigestTreeScene::plotEdge(ogdf::GraphAttributes &GA)
+{
ogdf::edge e;
forall_edges(e, m_graph) {
ogdf::node source = e->source();
ogdf::node target = e->target();
+ int nodeSize = GA.width(target);
int x1 = GA.x(source), y1 = -GA.y(source);
int x2 = GA.x(target), y2 = -GA.y(target);
- // QPainterPath p;
- QPointF src(x1 + nodeWidth/2, y1 + nodeHeight/2);
- QPointF dest(x2 + nodeWidth/2, y2 + nodeHeight/2);
+ QPointF src(x1 + nodeSize/2, y1 + nodeSize/2);
+ QPointF dest(x2 + nodeSize/2, y2 + nodeSize/2);
QLineF line(src, dest);
- //p.moveTo(x1 + nodeWidth/2, y1 + nodeHeight/2);
- //p.lineTo(x2 + nodeWidth/2, y2 + nodeHeight/2);
- //addPath(p, QPen(Qt::black), QBrush(Qt::black));
- addLine(line, QPen(Qt::black));
-
double angle = ::acos(line.dx() / line.length());
double arrowSize = 10;
-
- QPointF sourceArrowP0 = src + QPointF(nodeWidth/2 * line.dx() / line.length(), nodeHeight/2 * line.dy() / line.length());
- //QPointF sourceArrowP0 = src + QPointF(cos(angle) * nodeHeight/2, sin(angle) * nodeHeight/2);
-
- std::cout << "src " << src.x() << ", " << src.y() << std::endl;
- std::cout << "dest " << dest.x() << ", " << dest.y() << std::endl;
- std::cout << "line dx " << line.dx() << ", dy " << line.dy() << ", lenght << " << line.length() << std::endl;
- std::cout << "sap " << sourceArrowP0.x() << ", " << sourceArrowP0.y() << std::endl;
-
- QPointF sourceArrowP1 = sourceArrowP0 + QPointF(cos(angle + Pi / 3 - Pi/2) * arrowSize,
+ QPointF sourceArrowP0 = src + QPointF((nodeSize/2 + 10) * line.dx() / line.length(), (nodeSize/2 +10) * line.dy() / line.length());
+ QPointF sourceArrowP1 = sourceArrowP0 + QPointF(cos(angle + Pi / 3 - Pi/2) * arrowSize,
sin(angle + Pi / 3 - Pi/2) * arrowSize);
- QPointF sourceArrowP2 = sourceArrowP0 + QPointF(cos(angle + Pi - Pi / 3 - Pi/2) * arrowSize,
+ QPointF sourceArrowP2 = sourceArrowP0 + QPointF(cos(angle + Pi - Pi / 3 - Pi/2) * arrowSize,
sin(angle + Pi - Pi / 3 - Pi/2) * arrowSize);
- QLineF line1(sourceArrowP1, sourceArrowP2);
- addLine(line1, QPen(Qt::black));
-
- addPolygon(QPolygonF() << sourceArrowP0<< sourceArrowP1 << sourceArrowP2, QPen(Qt::black), QBrush(Qt::black));
- }
+ addLine(QLineF(sourceArrowP0, dest), QPen(Qt::black));
+ addPolygon(QPolygonF() << sourceArrowP0<< sourceArrowP1 << sourceArrowP2, QPen(Qt::black), QBrush(Qt::black));
+ }
+}
+
+void
+DigestTreeScene::plotNode(ogdf::GraphAttributes &GA, int rootIndex, QString digest)
+{
ogdf::node n;
- int i = 0;
- std::string s[5] = {"A", "B", "C", "D", "E"};
+ RosterIterator it(m_roster);
forall_nodes(n, m_graph) {
double x = GA.x(n);
-
double y = -GA.y(n);
double w = GA.width(n);
double h = GA.height(n);
+ int rim = 3;
QRectF boundingRect(x, y, w, h);
- addEllipse(boundingRect, QPen(Qt::black), QBrush(Qt::green));
- QGraphicsTextItem *text = addText(s[i].c_str());
- text->setPos(x + 5, y + 5);
- i++;
+ QRectF innerBoundingRect(x + rim, y + rim, w - rim * 2, h - rim * 2);
+ addRect(innerBoundingRect, QPen(Qt::black), QBrush(Qt::lightGray));
+
+ if (n->index() == rootIndex)
+ {
+ addRect(boundingRect, QPen(Qt::black), QBrush(Qt::darkRed));
+
+ QRectF digestRect(x - w / 2, y - h - 5, 2 * w, 30);
+ addRect(digestRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan));
+ QGraphicsTextItem *digestItem = addText(digest);
+ QRectF digestBoundingRect = digestItem->boundingRect();
+ digestItem->setDefaultTextColor(Qt::white);
+ digestItem->setFont(QFont("Cursive", 12, QFont::Bold));
+ digestItem->setPos(x + w / 2 - digestBoundingRect.width() / 2, y - h - 15);
+ m_rootDigest = digestItem;
+ }
+ else
+ {
+ if (it.hasNext())
+ {
+ it.next();
+ }
+ else
+ {
+ abort();
+ }
+ DisplayUserPtr p = it.value();
+ QGraphicsRectItem *rectItem = addRect(boundingRect, QPen(Qt::black), QBrush(Qt::darkBlue));
+ p->setRimRectItem(rectItem);
+
+ std::string s = boost::lexical_cast<std::string>(p->getSeqNo().getSeq());
+ QGraphicsTextItem *seqItem = addText(s.c_str());
+ seqItem->setFont(QFont("Cursive", 12, QFont::Bold));
+ QRectF seqBoundingRect = seqItem->boundingRect();
+ seqItem->setPos(x + w / 2 - seqBoundingRect.width() / 2, y + h / 2 - seqBoundingRect.height() / 2);
+ p->setSeqTextItem(seqItem);
+
+ QRectF textRect(x - w / 2, y + h + 10, 2 * w, 30);
+ addRect(textRect, QPen(Qt::darkCyan), QBrush(Qt::darkCyan));
+ QGraphicsTextItem *nickItem = addText(p->getNick());
+ QRectF textBoundingRect = nickItem->boundingRect();
+ nickItem->setDefaultTextColor(Qt::white);
+ nickItem->setFont(QFont("Cursive", 12, QFont::Bold));
+ nickItem->setPos(x + w / 2 - textBoundingRect.width() / 2, y + h + 15);
+ p->setNickTextItem(nickItem);
+ }
}
}
diff --git a/digesttreescene.h b/digesttreescene.h
index 1915c2f..5c8af9d 100644
--- a/digesttreescene.h
+++ b/digesttreescene.h
@@ -4,15 +4,79 @@
#include <QtGui/QGraphicsScene>
#include "ogdf/basic/GraphAttributes.h"
#include "ogdf/basic/Graph.h"
+#include <sync-seq-no.h>
+#include <sync-logic.h>
+#include <ctime>
+#include <QMap>
+#include <vector>
+#include <tr1/memory>
+
+class QGraphicsTextItem;
+
+class User;
+class DisplayUser;
class DigestTreeScene : public QGraphicsScene
{
Q_OBJECT
+typedef std::tr1::shared_ptr<DisplayUser> DisplayUserPtr;
+typedef QMap<QString, DisplayUserPtr> Roster;
+typedef QMap<QString, DisplayUserPtr>::iterator Roster_iterator;
+typedef QMapIterator<QString, DisplayUserPtr> RosterIterator;
+
public:
DigestTreeScene(QWidget *parent = 0);
- void plot();
+ void processUpdate(std::vector<Sync::MissingDataInfo> &v, QString digest);
+ void msgReceived(QString prefix, QString nick);
+ void plot(QString digest);
+ void plotEdge(ogdf::GraphAttributes &GA);
+ void plotNode(ogdf::GraphAttributes &GA, int rootIndex, QString digest);
private:
ogdf::Graph m_graph;
+ Roster m_roster;
+ QGraphicsTextItem *m_rootDigest;
+
};
+
+class User
+{
+public:
+ User():m_received(time(NULL)) {}
+ User(QString n, QString p, QString c): m_nick(n), m_prefix(p), m_chatroom(c), m_received(time(NULL)) {}
+ void setNick(QString nick) {m_nick = nick;}
+ void setPrefix(QString prefix) {m_prefix = prefix;}
+ void setChatroom(QString chatroom) {m_chatroom = chatroom;}
+ void setSeq(Sync::SeqNo seq) {m_seq = seq;}
+ void setReceived() {m_received = time(NULL);}
+ QString getNick() { return m_nick;}
+ QString getPrefix() { return m_prefix;}
+ QString getChatroom() { return m_chatroom;}
+ Sync::SeqNo getSeqNo() { return m_seq;}
+ time_t getReceived() { return m_received;}
+private:
+ QString m_nick;
+ QString m_prefix;
+ QString m_chatroom;
+ Sync::SeqNo m_seq;
+ time_t m_received;
+};
+
+class DisplayUser : public User
+{
+public:
+ DisplayUser():m_seqTextItem(NULL), m_nickTextItem(NULL), m_rimRectItem(NULL) {}
+ DisplayUser(QString n, QString p , QString c): User(n, p, c), m_seqTextItem(NULL), m_nickTextItem(NULL), m_rimRectItem(NULL) {}
+ QGraphicsTextItem *getSeqTextItem() {return m_seqTextItem;}
+ QGraphicsTextItem *getNickTextItem() {return m_nickTextItem;}
+ QGraphicsRectItem *getRimRectItem() {return m_rimRectItem;}
+ void setSeqTextItem(QGraphicsTextItem *item) { m_seqTextItem = item;}
+ void setNickTextItem(QGraphicsTextItem *item) { m_nickTextItem = item;}
+ void setRimRectItem(QGraphicsRectItem *item) {m_rimRectItem = item;}
+private:
+ QGraphicsTextItem *m_seqTextItem;
+ QGraphicsTextItem *m_nickTextItem;
+ QGraphicsRectItem *m_rimRectItem;
+};
+
#endif