/* * 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 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); /* else if (mode == 'F') { FreehandLineItem *freehandLineItem = new FreehandLineItem(); getline(stream, temp, ','); x1 = atoi(temp.c_str()); getline(stream, temp, ','); y1 = atoi(temp.c_str()); freehandLineItem->setStartPoint(QPointF(x1, y1)); while (getline(stream, temp, ',')) { x1 = atoi(temp.c_str()); getline(stream, temp, ','); y1 = atoi(temp.c_str()); freehandLineItem->lineTo(QPointF(x1, y1)); } graphicsView->scene()->addItem(freehandLineItem); }*/ } 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) { whiteboardSession_ = session; 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(0).toString().toStdString()); whiteboardSession_->sendElement(element); } FreehandLineItem* freehandLineItem = qgraphicsitem_cast(item); if (freehandLineItem != 0) { QVector points = freehandLineItem->points(); QVector::iterator it; std::stringstream stream; stream << "F"; QPoint point; for (it = points.begin(); it != points.end(); ++it) { stream << it->x() << "," << it->y() << ","; } stream >> serialized; } } 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(); } } }