explicit set video queue to 500ms (sender)
[makneto-zunavac1.git] / src / plugins / pollplugin.cpp
blob32baa667a05976d5f1b81bed2a1e3b309a91c759
1 #include "pollplugin.h"
2 #include "qgraphicswidgetitem.h"
3 #include "pollplugincreate.h"
5 #include <QtGui/QGraphicsItem>
6 #include <QtGui/QGraphicsProxyWidget>
7 #include <QtGui/QLabel>
8 #include <QtGui/QGridLayout>
9 #include <QtGui/QCheckBox>
10 #include <QtGui/QPushButton>
11 #include <QtGui/QLineEdit>
12 #include <QtGui/QMenu>
13 #include <QtGui/QGraphicsScene>
14 #include <QtGui/QGraphicsView>
15 #include <QtCore/QTextStream>
16 #include <QtCore/QDebug>
17 #include <kicon.h>
19 PollMainButton::PollMainButton(const QIcon &icon, const QString &text, QWidget *parent) : QPushButton(icon, text, parent)
21 setMouseTracking(true);
24 void PollMainButton::mouseMoveEvent(QMouseEvent *event)
26 if (event->buttons() & Qt::LeftButton)
28 QPointF diff = event->globalPos() - lastPos;
29 lastPos = event->globalPos();
30 emit moved(diff);
33 QPushButton::mouseMoveEvent(event);
36 void PollMainButton::mousePressEvent(QMouseEvent *event)
38 if (event->buttons() & Qt::LeftButton)
40 lastPos = event->globalPos();
41 startPos = event->globalPos();
43 QPushButton::mousePressEvent(event);
46 void PollMainButton::mouseReleaseEvent(QMouseEvent *event)
48 if (event->globalPos() != startPos)
50 emit positionChanged(event->globalPos());
51 startPos = event->globalPos();
52 QPushButton::mouseReleaseEvent(event);
54 else
56 QPushButton::mouseReleaseEvent(event);
57 emit pushed();
62 PollPlugin::PollPlugin(QStringList list) : Plugin()
64 init();
65 for (int i = 0; i < list.size(); i++)
67 m_questionList.insert(i, list[i]);
69 recreateWidget();
72 PollPlugin::PollPlugin(const QDomNode &node) : Plugin()
74 init();
75 parseSvg(node);
78 PollPlugin::PollPlugin() : Plugin()
80 init();
83 PollPlugin::~PollPlugin()
85 if (w)
86 delete w;
89 void PollPlugin::init()
91 w = new QWidget(0);
92 layout = new QGridLayout(w);
93 m_graphicsItem = new QGraphicsWidgetItem();
94 m_contextMenu = new QMenu(0);
95 //m_contextMenu->addAction(KIcon("transform-move"), "Move", this, SLOT(move()));
96 m_contextMenu->addAction(KIcon("draw-eraser"), "Delete", this, SLOT(remove()));
97 m_minimized = false;
100 bool PollPlugin::getQuestions()
102 bool res = false;
103 PollPluginCreate *create = new PollPluginCreate();
104 if (create->exec() == QDialog::Accepted)
106 m_questionList = create->getQuestions();
107 recreateWidget();
108 res = true;
110 delete create;
111 return res;
114 void PollPlugin::recreateWidget()
116 w->setUpdatesEnabled(false);
117 dynamic_cast<QGraphicsWidgetItem *> (m_graphicsItem)->setWidget(0);
118 // Delete all widgets
119 while (!m_allWidgets.empty())
121 delete m_allWidgets.takeLast();
123 delete layout;
124 delete w;
125 m_answerWidgets.clear();
127 // Recreate widget
128 w = new QWidget(0);
129 layout = new QGridLayout();
130 // Create top button
131 m_button = new PollMainButton(KIcon("maknetopoll"), QString(), w);
132 m_button->setContextMenuPolicy(Qt::CustomContextMenu);
133 connect(m_button, SIGNAL(pushed()), this, SLOT(buttonClicked()));
134 connect(m_button, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(contextMenu(const QPoint &)));
135 connect(m_button, SIGNAL(moved(const QPointF &)), this, SLOT(moved(const QPointF &)));
136 connect(m_button, SIGNAL(positionChanged(const QPointF &)), this, SLOT(positionChanged(const QPointF &)));
137 layout->addWidget(m_button);
138 // Don't create widget when minimized
139 if (!m_minimized)
141 QLabel *label = new QLabel(m_pollName);
142 layout->addWidget(label, 0, 1);
144 int i = 0, j;
145 QCheckBox *checkBox;
146 // Mapping question ID's to indexes
147 QList<int> indexes;
148 foreach (int index, m_questionList.keys())
150 label = new QLabel(m_questionList.value(index, ""));
151 indexes.append(index);
152 m_allWidgets.append(label);
153 layout->addWidget(label, 0, i + 2);
154 checkBox = new QCheckBox();
155 m_allWidgets.append(checkBox);
156 layout->addWidget(checkBox, m_answerList.size() + 1, i + 2);
157 m_answerWidgets.insert(index, checkBox);
158 i++;
160 for (i = 0; i < m_answerList.size(); i++)
162 foreach (int index, m_answerList[i].keys())
164 j = indexes.indexOf(index);
165 if (j < 0)
166 continue;
167 label = new QLabel();
168 m_allWidgets.append(label);
169 if (!m_answerList[i].contains(index))
170 label->setText("?");
171 else if (m_answerList[i].value(index))
172 label->setText("<font color=green>YES</font>");
173 else
174 label->setText("<font color=red>NO</font>");
175 layout->addWidget(label, i + 1, j + 2);
178 for (i = 0; i < m_respondents.size(); i++)
180 label = new QLabel(m_respondents[i]);
181 m_allWidgets.append(label);
182 layout->addWidget(label, i + 1, 0);
184 m_respondent = new QLineEdit();
185 m_respondent->setText("");//TODO: Current user JID
186 m_allWidgets.append(m_respondent);
187 layout->addWidget(m_respondent, m_answerList.size() + 1, 0, 1, 2);
188 QPushButton *send = new QPushButton("Send");
189 m_allWidgets.append(send);
190 layout->addWidget(send, m_answerList.size() + 1, m_questionList.size() + 2);
191 connect(send, SIGNAL(clicked(bool)), SLOT(send()));
193 w->setLayout(layout);
194 w->setUpdatesEnabled(true);
195 if (m_graphicsItem)
197 dynamic_cast<QGraphicsWidgetItem *> (m_graphicsItem)->setWidget(w);
201 void PollPlugin::send()
203 if (m_respondent->text().isEmpty())
205 m_respondent->setFocus();
206 return;
209 QMap<int, bool> l;
210 foreach (int i, m_answerWidgets.keys())
211 l.insert(i, dynamic_cast<QCheckBox *> (m_answerWidgets.value(i))->checkState() == Qt::Checked);
212 QString r = m_respondent->text();
213 int index = m_respondents.indexOf(r);
214 if (index < 0)
216 m_answerList.append(l);
217 m_respondents.append(r);
219 else
221 m_answerList.replace(index, l);
222 m_respondents.replace(index, r);
224 emit sendChanges();
225 recreateWidget();
228 void PollPlugin::buttonClicked()
230 m_minimized = !m_minimized;
231 recreateWidget();
234 QDomElement PollPlugin::svg()
236 QDomDocument doc;
237 QDomElement svg, xml, el, child;
238 QDomText text;
239 svg = doc.createElement("foreignObject");
240 svg.setAttribute("requiredExtensions", "http://makneto.org/plugins/poll");
241 svg.setAttribute("x", graphicsItem()->x());
242 svg.setAttribute("y", graphicsItem()->y());
243 xml = doc.createElement("poll");
244 xml.setAttribute("name", m_pollName);
245 svg.appendChild(xml);
246 el = doc.createElement("questions");
247 xml.appendChild(el);
248 foreach (int i, m_questionList.keys())
250 child = doc.createElement("question");
251 child.setAttribute("id", i);
252 el.appendChild(child);
253 text = doc.createTextNode(m_questionList.value(i));
254 child.appendChild(text);
256 if (m_answerList.size() > 0)
258 QDomElement answer;
259 el = doc.createElement("answerList");
260 xml.appendChild(el);
261 qDebug() << "m_answerList.size(): " << m_answerList.size();
262 for (int i = 0; i < m_answerList.size(); i++)
264 child = doc.createElement("answers");
265 child.setAttribute("by", m_respondents[i]);
266 el.appendChild(child);
267 foreach (int j, m_answerList[i].keys())
269 answer = doc.createElement("answer");
270 answer.setAttribute("id", j);
271 child.appendChild(answer);
272 text = doc.createTextNode((m_answerList[i].value(j)) ? "1" : "0");
273 answer.appendChild(text);
277 return svg;
280 void PollPlugin::parseSvg(const QDomNode &svg)
282 QDomElement root;
283 if (svg.nodeName().compare("foreignObject") == 0)
285 root = svg.firstChildElement().toElement();
286 double x = 0.0, y = 0.0;
287 if (svg.toElement().hasAttribute("x"))
289 QString s = svg.toElement().attribute("x", "0");
290 x = s.toDouble();
292 if (svg.toElement().hasAttribute("y"))
294 y = svg.toElement().attribute("y", "0").toDouble();
297 graphicsItem()->setPos(x, y);
299 else
300 root = svg.toElement();
302 if (root.isNull())
303 return;
305 // Get name of the poll
306 if (root.isElement())
308 m_pollName = root.toElement().attribute("name", "");
310 QDomElement questions = root.firstChildElement("questions"), question;
311 if (!questions.isNull())
313 question = questions.firstChildElement("question");
314 while (!question.isNull())
316 QString q;
317 int id = question.attribute("id", "-1").toInt();
318 // Question has to have an ID
319 if (id != -1)
321 // Try to get text of the element
322 if (question.firstChild().isText())
324 QString q = question.firstChild().toText().nodeValue();
326 // Find question with the same ID
327 if (m_questionList.contains(id))
329 // Owerwrite question
330 m_questionList[id] = q;
332 else
334 // Append question
335 m_questionList.insert(id, q);
339 // Continue with next question
340 question = question.nextSiblingElement("question");
343 QDomElement answerList = root.firstChildElement("answerList"), answersNode, answerNode;
344 QString r;
345 int index = -1, id;
346 if (!answerList.isNull())
348 answersNode = answerList.firstChildElement("answers");
349 // Iteration through all asnwers
350 while (!answersNode.isNull())
352 // Obtain respondent's name
353 r = answersNode.attribute("by", QString());
354 // Exist this respondent?
355 index = m_respondents.indexOf(r);
356 if (index < 0)
357 // If not, add him
358 m_respondents.append(r);
360 // Fill the answer list
361 QMap<int, bool> answer;
362 answerNode = answersNode.firstChildElement("answer");
363 while (!answerNode.isNull())
365 // If answer has an ID, add the value, skip it otherwise
366 id = answerNode.attribute("id", "-1").toInt();
367 if (id != -1)
368 answer.insert(id, answerNode.firstChild().toText().nodeValue() == "0" ? false : true);
370 answerNode = answerNode.nextSiblingElement("answer");
372 // Insert or update answer list
373 if (index < 0)
374 m_answerList.append(answer);
375 else
376 m_answerList.replace(index, answer);
378 answersNode = answersNode.nextSiblingElement("answers");
381 recreateWidget();
384 void PollPlugin::contextMenu(const QPoint &pos)
386 m_contextMenu->popup(m_button->mapToGlobal(pos));
389 void PollPlugin::moved(const QPointF &by)
391 QPointF pt = by;
392 if (graphicsItem() && graphicsItem()->scene())
394 if (graphicsItem()->scene()->views().size() > 0)
395 pt = graphicsItem()->scene()->views()[0]->mapToScene(by.toPoint());
397 graphicsItem()->setPos(graphicsItem()->pos() + pt);
400 void PollPlugin::positionChanged(const QPointF &newLocalPos)
402 QPointF pt = newLocalPos;
403 if (graphicsItem()->scene())
405 if (graphicsItem()->scene()->views().size() > 0)
407 pt = graphicsItem()->scene()->views()[0]->mapToScene(newLocalPos.toPoint());
408 emit sendChanges();
409 return;
412 // This shouldn't happened
413 qDebug() << "Error in changing position of the widget";
416 void PollPlugin::remove()
418 emit removed();
419 delete this;