/* * Copyright (c) 2012 Mateusz Piękos * Licensed under the simplified BSD license. * See Documentation/Licenses/BSD-simplified.txt for more information. */ #include "QtWhiteboardWindow.h" #include #include #include #include #include #include #include #include #include using namespace std; namespace Swift { QtWhiteboardWindow::QtWhiteboardWindow(WhiteboardSession::ref whiteboardSession) : QWidget() { layout = new QVBoxLayout(this); hLayout = new QHBoxLayout; sidebarLayout = new QVBoxLayout; toolboxLayout = new QGridLayout; scene = new QGraphicsScene(this); scene->setSceneRect(0, 0, 400, 400); // scene->setItemIndexMethod(QGraphicsScene::NoIndex); graphicsView = new GView(scene, this); graphicsView->setMode(GView::Line); connect(graphicsView, SIGNAL(lastItemChanged(QGraphicsItem*)), this, SLOT(handleLastItemChanged(QGraphicsItem*))); widthBox = new QSpinBox(this); connect(widthBox, SIGNAL(valueChanged(int)), this, SLOT(changeLineWidth(int))); widthBox->setValue(1); moveUpButton = new QPushButton("Move Up", this); connect(moveUpButton, SIGNAL(clicked()), graphicsView, SLOT(moveUpSelectedItem())); moveDownButton = new QPushButton("Move Down", this); connect(moveDownButton, SIGNAL(clicked()), graphicsView, SLOT(moveDownSelectedItem())); colorDialogButton = new QPushButton("Color", this); connect(colorDialogButton, SIGNAL(clicked()), this, SLOT(showColorDialog())); brushColorDialogButton = new QPushButton("Brush", this); connect(brushColorDialogButton, SIGNAL(clicked()), this, SLOT(showBrushColorDialog())); rubberButton = new QToolButton(this); rubberButton->setCheckable(true); rubberButton->setAutoExclusive(true); connect(rubberButton, SIGNAL(clicked()), this, SLOT(setRubberMode())); lineButton = new QToolButton(this); lineButton->setIcon(QIcon(":/icons/line.png")); lineButton->setCheckable(true); lineButton->setAutoExclusive(true); lineButton->setChecked(true); connect(lineButton, SIGNAL(clicked()), this, SLOT(setLineMode())); rectButton = new QToolButton(this); rectButton->setIcon(QIcon(":/icons/rect.png")); rectButton->setCheckable(true); rectButton->setAutoExclusive(true); connect(rectButton, SIGNAL(clicked()), this, SLOT(setRectMode())); circleButton = new QToolButton(this); circleButton->setIcon(QIcon(":/icons/circle.png")); circleButton->setCheckable(true); circleButton->setAutoExclusive(true); connect(circleButton, SIGNAL(clicked()), this, SLOT(setCircleMode())); handLineButton = new QToolButton(this); handLineButton->setIcon(QIcon(":/icons/handline.png")); handLineButton->setCheckable(true); handLineButton->setAutoExclusive(true); connect(handLineButton, SIGNAL(clicked()), this, SLOT(setHandLineMode())); filledHandLineButton = new QToolButton(this); filledHandLineButton->setIcon(QIcon(":/icons/filledHandline.png")); filledHandLineButton->setCheckable(true); filledHandLineButton->setAutoExclusive(true); connect(filledHandLineButton, SIGNAL(clicked()), this, SLOT(setFilledHandLineMode())); textButton = new QToolButton(this); textButton->setIcon(QIcon(":/icons/text.png")); textButton->setCheckable(true); textButton->setAutoExclusive(true); connect(textButton, SIGNAL(clicked()), this, SLOT(setTextMode())); polygonButton = new QToolButton(this); polygonButton->setIcon(QIcon(":/icons/polygon.png")); polygonButton->setCheckable(true); polygonButton->setAutoExclusive(true); connect(polygonButton, SIGNAL(clicked()), this, SLOT(setPolygonMode())); selectButton = new QToolButton(this); selectButton->setIcon(QIcon(":/icons/cursor.png")); selectButton->setCheckable(true); selectButton->setAutoExclusive(true); connect(selectButton, SIGNAL(clicked()), this, SLOT(setSelectMode())); toolboxLayout->addWidget(rubberButton, 0, 0); toolboxLayout->addWidget(lineButton, 0, 1); toolboxLayout->addWidget(rectButton, 0, 2); toolboxLayout->addWidget(circleButton, 1, 0); toolboxLayout->addWidget(handLineButton, 1, 1); toolboxLayout->addWidget(filledHandLineButton, 1, 2); toolboxLayout->addWidget(textButton, 2, 0); toolboxLayout->addWidget(polygonButton, 2, 1); toolboxLayout->addWidget(selectButton, 2, 2); sidebarLayout->addLayout(toolboxLayout); sidebarLayout->addWidget(moveUpButton); sidebarLayout->addWidget(moveDownButton); sidebarLayout->addWidget(widthBox); sidebarLayout->addWidget(colorDialogButton); sidebarLayout->addWidget(brushColorDialogButton); hLayout->addWidget(graphicsView); hLayout->addLayout(sidebarLayout); layout->addLayout(hLayout); this->setLayout(layout); setSession(whiteboardSession); } void QtWhiteboardWindow::handleWhiteboardElementReceive(const WhiteboardElement::ref element) { WhiteboardElementDrawingVisitor visitor(graphicsView); element->accept(visitor); } void QtWhiteboardWindow::changeLineWidth(int i) { graphicsView->setLineWidth(i); } void QtWhiteboardWindow::showColorDialog() { QColor color = QColorDialog::getColor(graphicsView->getLineColor(), 0, "Select pen color", QColorDialog::ShowAlphaChannel); if(color.isValid()) graphicsView->setLineColor(color); } void QtWhiteboardWindow::showBrushColorDialog() { QColor color = QColorDialog::getColor(graphicsView->getBrushColor(), 0, "Select brush color", QColorDialog::ShowAlphaChannel); if(color.isValid()) graphicsView->setBrushColor(color); } void QtWhiteboardWindow::setRubberMode() { graphicsView->setMode(GView::Rubber); } void QtWhiteboardWindow::setLineMode() { graphicsView->setMode(GView::Line); } void QtWhiteboardWindow::setRectMode() { graphicsView->setMode(GView::Rect); } void QtWhiteboardWindow::setCircleMode() { graphicsView->setMode(GView::Circle); } void QtWhiteboardWindow::setHandLineMode() { graphicsView->setMode(GView::HandLine); } void QtWhiteboardWindow::setFilledHandLineMode() { graphicsView->setMode(GView::FilledHandLine); } void QtWhiteboardWindow::setTextMode() { graphicsView->setMode(GView::Text); } void QtWhiteboardWindow::setPolygonMode() { graphicsView->setMode(GView::Polygon); } void QtWhiteboardWindow::setSelectMode() { graphicsView->setMode(GView::Select); } void QtWhiteboardWindow::show() { QWidget::show(); } void QtWhiteboardWindow::setSession(WhiteboardSession::ref session) { graphicsView->clear(); whiteboardSession_ = session; graphicsView->setIDPrefix(P2QSTRING(whiteboardSession_->getClientID())); whiteboardSession_->onElementReceived.connect(boost::bind(&QtWhiteboardWindow::handleWhiteboardElementReceive, this, _1)); whiteboardSession_->onRequestAccepted.connect(boost::bind(&QWidget::show, this)); whiteboardSession_->onSessionTerminateReceived.connect(boost::bind(&QtWhiteboardWindow::handleSessionTerminate, this)); } void QtWhiteboardWindow::activateWindow() { QWidget::activateWindow(); } void QtWhiteboardWindow::handleLastItemChanged(QGraphicsItem* item) { std::string serialized; QGraphicsLineItem* lineItem = qgraphicsitem_cast(item); if (lineItem != 0) { QLine line = lineItem->line().toLine(); QColor color = lineItem->pen().color(); WhiteboardLineElement::ref element = boost::make_shared(line.x1(), line.y1(), line.x2(), line.y2()); element->setColor(Color(color.red(), color.green(), color.blue(), color.alpha())); element->setPenWidth(lineItem->pen().width()); element->setID(lineItem->data(100).toString().toStdString()); whiteboardSession_->sendElement(element); } FreehandLineItem* freehandLineItem = qgraphicsitem_cast(item); if (freehandLineItem != 0) { WhiteboardFreehandPathElement::ref element = boost::make_shared(); QColor color = freehandLineItem->pen().color(); std::vector > points; QVector::const_iterator it = freehandLineItem->points().constBegin(); for ( ; it != freehandLineItem->points().constEnd(); ++it) { points.push_back(std::pair(it->x(), it->y())); } element->setColor(Color(color.red(), color.green(), color.blue(), color.alpha())); element->setPenWidth(freehandLineItem->pen().width()); element->setPoints(points); element->setID(freehandLineItem->data(100).toString().toStdString()); whiteboardSession_->sendElement(element); } QGraphicsRectItem* rectItem = qgraphicsitem_cast(item); if (rectItem != 0) { QRectF rect = rectItem->rect(); WhiteboardRectElement::ref element = boost::make_shared(rect.x(), rect.y(), rect.width(), rect.height()); QColor penColor = rectItem->pen().color(); QColor brushColor = rectItem->brush().color(); element->setBrushColor(Color(brushColor.red(), brushColor.green(), brushColor.blue(), brushColor.alpha())); element->setPenColor(Color(penColor.red(), penColor.green(), penColor.blue(), penColor.alpha())); element->setPenWidth(rectItem->pen().width()); element->setID(rectItem->data(100).toString().toStdString()); whiteboardSession_->sendElement(element); } QGraphicsTextItem* textItem = qgraphicsitem_cast(item); if (textItem != 0) { QPointF point = textItem->pos(); WhiteboardTextElement::ref element = boost::make_shared(point.x(), point.y()); element->setText(textItem->toPlainText().toStdString()); element->setSize(textItem->font().pointSize()); QColor color = textItem->defaultTextColor(); element->setColor(Color(color.red(), color.green(), color.blue(), color.alpha())); element->setID(rectItem->data(100).toString().toStdString()); whiteboardSession_->sendElement(element); } QGraphicsPolygonItem* polygonItem = qgraphicsitem_cast(item); if (polygonItem) { WhiteboardPolygonElement::ref element = boost::make_shared(); QPolygonF polygon = polygonItem->polygon(); std::vector > points; QVector::const_iterator it = polygon.begin(); for (; it != polygon.end(); ++it) { points.push_back(std::pair(it->x(), it->y())); } element->setPoints(points); QColor penColor = polygonItem->pen().color(); QColor brushColor = polygonItem->brush().color(); element->setPenColor(Color(penColor.red(), penColor.green(), penColor.blue(), penColor.alpha())); element->setBrushColor(Color(brushColor.red(), brushColor.green(), brushColor.blue(), brushColor.alpha())); element->setPenWidth(polygonItem->pen().width()); element->setID(polygonItem->data(100).toString().toStdString()); whiteboardSession_->sendElement(element); } QGraphicsEllipseItem* ellipseItem = qgraphicsitem_cast(item); if (ellipseItem) { QRectF rect = ellipseItem->rect(); int cx = rect.x()+rect.width()/2; int cy = rect.y()+rect.height()/2; int rx = rect.width()/2; int ry = rect.height()/2; WhiteboardEllipseElement::ref element = boost::make_shared(cx, cy, rx, ry); QColor penColor = ellipseItem->pen().color(); QColor brushColor = ellipseItem->brush().color(); element->setPenColor(Color(penColor.red(), penColor.green(), penColor.blue(), penColor.alpha())); element->setBrushColor(Color(brushColor.red(), brushColor.green(), brushColor.blue(), brushColor.alpha())); element->setPenWidth(ellipseItem->pen().width()); element->setID(ellipseItem->data(100).toString().toStdString()); whiteboardSession_->sendElement(element); } } void QtWhiteboardWindow::handleSessionTerminate() { hide(); } void QtWhiteboardWindow::closeEvent(QCloseEvent* event) { QMessageBox box(this); box.setText(tr("Closing window is equivalent closing the session. Are you sure you want to do this?")); box.setStandardButtons(QMessageBox::Yes | QMessageBox::No); box.setIcon(QMessageBox::Question); if (box.exec() == QMessageBox::Yes) { whiteboardSession_->cancel(); } else { event->ignore(); } } }