]> git.zarvox.org Git - shareboard.git/commitdiff
Implement image insertion and placement, tool selection, and eraser. master
authorDrew Fisher <drew.m.fisher@gmail.com>
Fri, 8 Oct 2010 16:55:49 +0000 (09:55 -0700)
committerDrew Fisher <drew.m.fisher@gmail.com>
Fri, 8 Oct 2010 16:55:49 +0000 (09:55 -0700)
This commit add the remaining required functionality for the assignment.

This code has gone from decent to somewhat ugly in a rather short
timeframe, due to no longer thinking about where data belongs.

connectionmanager.cpp
mainwindow.cpp
mainwindow.h
shareboard.cpp
shareboard.h
shareboardcanvas.cpp
shareboardcanvas.h

index 8908c44f6402173ac8244a31374be6b3e3ba1549..36d7502729965343bedf6032db7a821857cc72a0 100644 (file)
@@ -137,6 +137,7 @@ void ConnectionManager::haveData() {
                                }
                        case Action::AddImage:
                                {
+                               qDebug() << "Sending AddImage action!";
                                act = new AddImageAction();
                                AddImageAction* action = static_cast<AddImageAction*>(act);
                                qreal x = 0;
index 5caf77a2ab448bff665ceafecde379e0d38cff5f..727bd8edd387e6e602f6f3dfd80e157f10d26f0a 100644 (file)
@@ -43,6 +43,13 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
        QObject::connect(board, SIGNAL(indexChanged(int)), sliderWidget, SLOT(setValue(int)));
        QObject::connect(sliderWidget, SIGNAL(valueChanged(int)), board, SLOT(jumpToIndex(int)));
 
+       //QObject::connect(spinboxWidget, SIGNAL(valueChanged(int)), board, SLOT(setDrawState(int)));
+       //QObject::connect(spinboxWidget, SIGNAL(valueChanged(int)), this, SLOT(updateDrawState(int)));
+       QObject::connect(eraserAction, SIGNAL(triggered()), this, SLOT(useEraserTool()));
+       QObject::connect(imageAction, SIGNAL(triggered()), this, SLOT(useImageTool()));
+       QObject::connect(penAction, SIGNAL(triggered()), this, SLOT(usePenTool()));
+
+       usePenTool();
 
        statusBar()->showMessage("Ready");
 }
@@ -51,6 +58,25 @@ MainWindow::~MainWindow() {
 
 }
 
+void MainWindow::useEraserTool() {
+       board->setDrawState(spinboxWidget->value() * -1);
+}
+
+void MainWindow::usePenTool() {
+       board->setDrawState(spinboxWidget->value());
+}
+
+void MainWindow::useImageTool() {
+       QString fileName = QFileDialog::getOpenFileName(this, "Insert file:", ".", "Images (*.PNG *.png *.JPG *.jpg)");
+       if(fileName.isEmpty() )
+               return;
+       QImage im;
+       if (!im.load(fileName))
+               return;
+       board->setInsertImage(im);
+       board->setDrawState(0);
+}
+
 void MainWindow::updateSliderRange(int newmax) {
        sliderWidget->setRange(0, newmax);
 }
@@ -118,7 +144,10 @@ void MainWindow::marshallAction(Action* action) {
                case Action::DrawLine: // set the action's thickness, color
                        {
                                DrawLineAction* a = static_cast<DrawLineAction*>(action);
-                               a->color = lineColor;
+                               if(board->getDrawState() < 0 ) // This should probably all get reworked into MainWindow...
+                                       a->color = QColor(Qt::white);
+                               else
+                                       a->color = lineColor;
                                a->width = spinboxWidget->value();
                        }
                        break;
@@ -152,14 +181,14 @@ void MainWindow::createToolBars() {
        // Create some of the basic tool actions.
        //   They should also be added to an exclusive tool group.
        QActionGroup* toolGroup = new QActionGroup(this);
-       QAction* penAction = new QAction(QIcon("icons/pen.png"), "Pen", this);
-       QAction* eraserAction = new QAction(QIcon("icons/eraser.png"), "Eraser", this);
+       penAction = new QAction(QIcon("icons/pen.png"), "Pen", this);
+       eraserAction = new QAction(QIcon("icons/eraser.png"), "Eraser", this);
        penAction->setChecked(true);
        toolGroup->addAction(penAction);
        toolGroup->addAction(eraserAction);
 
        // Create some other one-click actions that are not a part of the action group.
-       QAction* imageAction = new QAction(QIcon("icons/import_image.png"), "Import Image", this);
+       imageAction = new QAction(QIcon("icons/import_image.png"), "Import Image", this);
        clearAction = new QAction(QIcon("icons/clear.png"), "Clear", this);
 
        // Style
@@ -176,6 +205,8 @@ void MainWindow::createToolBars() {
        QAction* restartAction = new QAction(QIcon("icons/rewind.png"), "Replay History", this);
        QAction* playAction = new QAction(QIcon("icons/play.png"), "Play", this);
        sliderWidget = new QSlider(Qt::Horizontal, this);
+       sliderWidget->setSingleStep(1);
+       sliderWidget->setPageStep(30);
 
        toolBar = addToolBar("Test");
        toolBar->setMovable(false);
index cb82b6a2561ad080745e2d50215e5d0637092b3f..31af80ea9d1a51a82e9a7fd6aee7e59b58e579e4 100644 (file)
@@ -30,6 +30,9 @@ public slots:
        void clearBoard();
        void marshallAction(Action* action);
        void updateSliderRange(int newmax);
+       void useEraserTool();
+       void useImageTool();
+       void usePenTool();
 private:
        void createActions();
        void createMenus();
@@ -42,6 +45,9 @@ private:
        QAction* quitAction;
        QAction* colorAction;
        QAction* clearAction;
+       QAction* imageAction;
+       QAction* penAction;
+       QAction* eraserAction;
        QSpinBox* spinboxWidget;
        QSlider* sliderWidget;
        ConnectionManager* connMan;
index 5ecc3243d0479fe8032a4bdf2f2bbe217a5fef37..48554619739adddf0314d2132f7cecef0daccb21 100644 (file)
@@ -30,6 +30,7 @@ Shareboard::Shareboard(QWidget* parent) : QWidget(parent) {
        QObject::connect(prompt, SIGNAL(connectToServer(QString, QString)), this, SIGNAL(connectToServer(QString, QString)));
        QObject::connect(canvas, SIGNAL(mouseMovedTo(QPointF)), this, SLOT(handleMouseMoved(QPointF)));
        QObject::connect(canvas, SIGNAL(segmentDrawn(QPointF,QPointF)), this, SLOT(handleSegmentDrawn(QPointF,QPointF)));
+       QObject::connect(canvas, SIGNAL(insertImage(QPointF)), this, SLOT(handleInsertImage(QPointF)));
        qDebug() << viewImage.rect();
 }
 
@@ -44,6 +45,10 @@ QImage Shareboard::view() {
        return viewImage;
 }
 
+int Shareboard::getDrawState() {
+       return drawState;
+}
+
 void Shareboard::handleMouseMoved(QPointF pos) {
        MouseMoveAction* action = new MouseMoveAction();
        action->userID = userID;
@@ -63,6 +68,14 @@ void Shareboard::handleSegmentDrawn(QPointF start, QPointF end) {
        //qDebug() << "shareboard.cpp: segment drawn from" << start << "to" << end;
 }
 
+void Shareboard::handleInsertImage(QPointF pos) {
+       AddImageAction* action = new AddImageAction();
+       action->userID = userID;
+       action->topLeft = pos;
+       action->image = insertImage;
+       emit actionHappened(action);
+}
+
 void Shareboard::postAction(Action* action) {
        if(action->userID == userID) {
                delete localHistory[0];
@@ -140,7 +153,10 @@ void Shareboard::jumpToIndex(int index) {
                                }
                                break;
                        case Action::AddImage:
-                               //TODO Implement this
+                               {
+                                       AddImageAction* a = static_cast<AddImageAction*>(action);
+                                       p.drawImage(a->topLeft, a->image);
+                               }
                        case Action::UserChat:
                        case Action::UserSynced:
                                break;
@@ -156,3 +172,11 @@ void Shareboard::jumpToIndex(int index) {
        canvas->update();
        emit indexChanged(viewIndex);
 }
+
+void Shareboard::setDrawState(int newstate) {
+       drawState = newstate;
+}
+
+void Shareboard::setInsertImage(QImage newimage) {
+       insertImage = newimage;
+}
index 28ab2589d26112c907f84b66069555c3059cba4e..a7e9de7ce46258746ffb41b35917433be449fd4c 100644 (file)
@@ -19,10 +19,12 @@ class Shareboard : public QWidget {
                Shareboard(QWidget* parent = 0);
                ~Shareboard();
                QImage view();
+               int getDrawState();
 
        public slots:
                void handleMouseMoved(QPointF pos);
                void handleSegmentDrawn(QPointF start, QPointF end);
+               void handleInsertImage(QPointF pos);
                void postAction(Action* action);
                void postLocalAction(Action* action);
                void switchToBoard();
@@ -30,6 +32,8 @@ class Shareboard : public QWidget {
                void setUserID(int id);
                void jumpToIndex(int index);
                //void jumpToTime(QDateTime time);
+               void setDrawState(int newstate);
+               void setInsertImage(QImage newimage);
 
        signals:
                void rangeChanged(int max);
@@ -44,6 +48,8 @@ class Shareboard : public QWidget {
                History history;
                History localHistory;
                int userID;
+               int drawState; // if > 0, line width.  If < 0, eraser size.  If == 0, image tool.
+               QImage insertImage;
                // These items represent the present state of the view.
                QDateTime viewTime;
                QImage viewImage;
index af45aeb876b18d37dac2e44156707b2b1b535a0a..90e0474a8d9ace29c13f422b323d2c3b1acb8791 100644 (file)
@@ -24,6 +24,7 @@ void ShareboardCanvas::setModel(Shareboard* b) {
 void ShareboardCanvas::mouseMoveEvent(QMouseEvent* event) {
        // Send mouse move event to network thread
        emit mouseMovedTo(event->posF());
+       lastPos = event->posF();
        // If the button is down
        if(mouseDown) {
                emit segmentDrawn(dragPath.last(), event->posF());
@@ -35,6 +36,10 @@ void ShareboardCanvas::mouseMoveEvent(QMouseEvent* event) {
 
 void ShareboardCanvas::mousePressEvent(QMouseEvent* event) {
        if(event->button() & Qt::LeftButton) {
+               if(board->drawState == 0) {
+                       emit insertImage(event->posF());
+                       return;
+               }
                mouseDown = true;
                dragPath.append(event->posF()); // We're starting a new drag path
        }
@@ -55,12 +60,23 @@ void ShareboardCanvas::paintEvent(QPaintEvent* event) {
        QPainter p(this);
        // Paint the background from the saved state
        p.drawImage(0, 0, board->view());
-       // Now paint our local changes on top of it
+       // Now paint our local changes on top of it (optional, will make things look snappier)
        
-       //p.set
        // Now paint the set of mouse cursors on top of that
        QMap<int, QPointF>::const_iterator i;
        for (i = board->viewCursors.constBegin(); i!=board->viewCursors.constEnd(); ++i) {
+               if(i.key() == board->userID) {
+                       if( board->drawState < 0) {// Eraser mode
+                               p.setPen(QColor(Qt::black));
+                               p.setBrush(QColor(Qt::white));
+                               p.drawEllipse(lastPos, qAbs(board->drawState), qAbs(board->drawState));
+                               //p.drawEllipse(i.value(), qAbs(board->drawState), qAbs(board->drawState));
+                       }
+                       if( board->drawState == 0) { // Image mode
+                               p.drawImage(lastPos, board->insertImage);
+                       }
+                       continue;
+               }
                QPen pen(QColor(255,0,0,50));
                pen.setCapStyle(Qt::RoundCap);
                p.setPen(pen);
index 3414580b03a68314080c0c7192e705884bc4a1eb..bf008f356ac53d50c8d2f8f082247868c65ca119 100644 (file)
@@ -23,10 +23,12 @@ public:
 signals:
        void mouseMovedTo(QPointF pos);
        void segmentDrawn(QPointF start, QPointF end);
+       void insertImage(QPointF pos);
 private:
        bool mouseDown;
        QVector<QPointF> dragPath;
        Shareboard* board;
+       QPointF lastPos;
 };
 
 #endif //__SHAREBOARDCANVAS_H__