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>
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();
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
);
56 QPushButton::mouseReleaseEvent(event
);
62 PollPlugin::PollPlugin(QStringList list
) : Plugin()
65 for (int i
= 0; i
< list
.size(); i
++)
67 m_questionList
.insert(i
, list
[i
]);
72 PollPlugin::PollPlugin(const QDomNode
&node
) : Plugin()
78 PollPlugin::PollPlugin() : Plugin()
83 PollPlugin::~PollPlugin()
89 void PollPlugin::init()
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()));
100 bool PollPlugin::getQuestions()
103 PollPluginCreate
*create
= new PollPluginCreate();
104 if (create
->exec() == QDialog::Accepted
)
106 m_questionList
= create
->getQuestions();
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();
125 m_answerWidgets
.clear();
129 layout
= new QGridLayout();
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
141 QLabel
*label
= new QLabel(m_pollName
);
142 layout
->addWidget(label
, 0, 1);
146 // Mapping question ID's to 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
);
160 for (i
= 0; i
< m_answerList
.size(); i
++)
162 foreach (int index
, m_answerList
[i
].keys())
164 j
= indexes
.indexOf(index
);
167 label
= new QLabel();
168 m_allWidgets
.append(label
);
169 if (!m_answerList
[i
].contains(index
))
171 else if (m_answerList
[i
].value(index
))
172 label
->setText("<font color=green>YES</font>");
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);
197 dynamic_cast<QGraphicsWidgetItem
*> (m_graphicsItem
)->setWidget(w
);
201 void PollPlugin::send()
203 if (m_respondent
->text().isEmpty())
205 m_respondent
->setFocus();
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
);
216 m_answerList
.append(l
);
217 m_respondents
.append(r
);
221 m_answerList
.replace(index
, l
);
222 m_respondents
.replace(index
, r
);
228 void PollPlugin::buttonClicked()
230 m_minimized
= !m_minimized
;
234 QDomElement
PollPlugin::svg()
237 QDomElement svg
, xml
, el
, child
;
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");
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)
259 el
= doc
.createElement("answerList");
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
);
280 void PollPlugin::parseSvg(const QDomNode
&svg
)
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");
292 if (svg
.toElement().hasAttribute("y"))
294 y
= svg
.toElement().attribute("y", "0").toDouble();
297 graphicsItem()->setPos(x
, y
);
300 root
= svg
.toElement();
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())
317 int id
= question
.attribute("id", "-1").toInt();
318 // Question has to have an ID
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
;
335 m_questionList
.insert(id
, q
);
339 // Continue with next question
340 question
= question
.nextSiblingElement("question");
343 QDomElement answerList
= root
.firstChildElement("answerList"), answersNode
, answerNode
;
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
);
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();
368 answer
.insert(id
, answerNode
.firstChild().toText().nodeValue() == "0" ? false : true);
370 answerNode
= answerNode
.nextSiblingElement("answer");
372 // Insert or update answer list
374 m_answerList
.append(answer
);
376 m_answerList
.replace(index
, answer
);
378 answersNode
= answersNode
.nextSiblingElement("answers");
384 void PollPlugin::contextMenu(const QPoint
&pos
)
386 m_contextMenu
->popup(m_button
->mapToGlobal(pos
));
389 void PollPlugin::moved(const QPointF
&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());
412 // This shouldn't happened
413 qDebug() << "Error in changing position of the widget";
416 void PollPlugin::remove()