]> git.zarvox.org Git - shareboard.git/blob - mainwindow.cpp
Implement ClearBoard call from protocol.
[shareboard.git] / mainwindow.cpp
1 #include "mainwindow.h"
2 #include <QAction>
3 #include <QApplication>
4 #include <QFile>
5 #include <QFileDialog>
6 #include <QColorDialog>
7 #include <QLabel>
8 #include <QMenuBar>
9 #include <QStatusBar>
10 #include <QToolBar>
11 #include <QSlider>
12 #include <QSpinBox>
13 #include <QColorDialog>
14
15 #include "connectionmanager.h"
16 #include "shareboard.h"
17 #include "action.h"
18
19 MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
20         createActions();
21         createMenus();
22
23         lineColor = Qt::blue;
24         createToolBars();
25
26         board = new Shareboard(this);
27         setCentralWidget(board);
28
29         connMan = new ConnectionManager(this);
30         connMan->setTarget(board);
31
32         // Connect all actions
33         QObject::connect(board, SIGNAL(connectToServer(QString, QString)), connMan, SLOT(joinServer(QString, QString)));
34         QObject::connect(connMan, SIGNAL(connected()),    this, SLOT(switchToBoard()));
35         QObject::connect(connMan, SIGNAL(disconnected()), this, SLOT(switchToConnect()));
36         QObject::connect(connMan, SIGNAL(error(QString)), this, SLOT(showError(QString)));
37         QObject::connect(board, SIGNAL(actionHappened(Action*)), this, SLOT(marshallAction(Action*)));
38
39         QObject::connect(colorAction, SIGNAL(triggered()), this, SLOT(chooseColor()));
40
41         statusBar()->showMessage("Ready");
42 }
43
44 MainWindow::~MainWindow() {
45
46 }
47
48 void MainWindow::chooseColor() {
49         lineColor = QColorDialog::getColor(lineColor, this, "Choose drawing color");
50         // update pixmap
51         QPixmap pixmap(16, 16);
52         pixmap.fill(lineColor);
53         colorAction->setIcon(QIcon(pixmap));
54 }
55
56 void MainWindow::load() {
57         QString fileName = QFileDialog::getOpenFileName(this, "Load saved board:", ".", "Shareboards (*.board)");
58         if(fileName.isEmpty()) {
59                 statusBar()->showMessage("Cancelled loading board.");
60                 return;
61         }
62         QFile file(fileName);
63         QDataStream in(&file);
64         // Load all history from that file
65         statusBar()->showMessage(QString("Successfully loaded ").append(fileName));
66 }
67
68 void MainWindow::save() {
69         QString fileName = QFileDialog::getSaveFileName(this, "Save board history as:", ".", "Shareboards (*.board)");
70         if(fileName.isEmpty()) {
71                 statusBar()->showMessage("Cancelled saving board.");
72                 return;
73         }
74         if(!fileName.endsWith(".board"))
75                 fileName = fileName.append(".board");
76         QFile file(fileName);
77         QDataStream out(&file);
78         // Output all history to that file
79         statusBar()->showMessage(QString("Successfully saved ").append(fileName));
80 }
81
82 void MainWindow::quit() {
83         // If we want, we can add some annoying dialog that says "Quit now?" and throws the user off
84         qApp->quit();
85 }
86
87 void MainWindow::switchToBoard() {
88         board->switchToBoard();
89         statusBar()->showMessage(QString("Switched to Shareboard drawing surface"));
90 }
91
92 void MainWindow::switchToConnect() {
93         board->switchToPrompt();
94         statusBar()->showMessage(QString("Switched to connection prompt"));
95 }
96
97 void MainWindow::showError(QString error) {
98         statusBar()->showMessage(error);
99 }
100
101 void MainWindow::marshallAction(Action* action) {
102         switch(action->type) {
103                 case Action::DrawLine: // set the action's thickness, color
104                         {
105                                 DrawLineAction* a = static_cast<DrawLineAction*>(action);
106                                 a->color = lineColor;
107                                 a->width = spinboxWidget->value();
108                         }
109                         break;
110                 default: break;
111         }
112         board->postLocalAction(action);
113         connMan->sendAction(action);
114 }
115
116 void MainWindow::createActions() {
117         loadPastAction = new QAction("&Open a board", this);
118         saveAction = new QAction("&Save board", this);
119         quitAction = new QAction("&Quit", this);
120         QObject::connect(loadPastAction, SIGNAL(triggered()), this, SLOT(load()));
121         QObject::connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
122         QObject::connect(quitAction, SIGNAL(triggered()), this, SLOT(quit()));
123 }
124
125 void MainWindow::createMenus() {
126
127         fileMenu = menuBar()->addMenu("&File");
128         fileMenu->addAction(loadPastAction);
129         fileMenu->addAction(saveAction);
130         fileMenu->addSeparator();
131         fileMenu->addAction(quitAction);
132
133 }
134
135 void MainWindow::createToolBars() {
136
137         // Create some of the basic tool actions.
138         //   They should also be added to an exclusive tool group.
139         QActionGroup* toolGroup = new QActionGroup(this);
140         QAction* penAction = new QAction(QIcon("icons/pen.png"), "Pen", this);
141         QAction* eraserAction = new QAction(QIcon("icons/eraser.png"), "Eraser", this);
142         penAction->setChecked(true);
143         toolGroup->addAction(penAction);
144         toolGroup->addAction(eraserAction);
145
146         // Create some other one-click actions that are not a part of the action group.
147         QAction* imageAction = new QAction(QIcon("icons/import_image.png"), "Import Image", this);
148         QAction* clearAction = new QAction(QIcon("icons/clear.png"), "Clear", this);
149
150         // Style
151         spinboxWidget = new QSpinBox(this);
152         spinboxWidget->setMinimum(4);
153         spinboxWidget->setMaximum(40);
154
155
156         QPixmap pixmap(16, 16);
157         pixmap.fill(lineColor);
158         colorAction = new QAction(QIcon(pixmap), "Select Color", this);
159
160         // Create the history UI elements.
161         QAction* restartAction = new QAction(QIcon("icons/rewind.png"), "Replay History", this);
162         QAction* playAction = new QAction(QIcon("icons/play.png"), "Play", this);
163         QSlider* sliderWidget = new QSlider(Qt::Horizontal, this);
164
165         toolBar = addToolBar("Test");
166         toolBar->setMovable(false);
167
168         toolBar->addSeparator();
169 //      toolBar->addWidget(new QLabel("Tools", this));
170         toolBar->addAction(penAction);
171         toolBar->addAction(eraserAction);
172         toolBar->addAction(imageAction);
173         toolBar->addAction(clearAction);
174         toolBar->addSeparator();
175
176 //      toolBar->addWidget(new QLabel("Style", this));
177         toolBar->addWidget(new QLabel("Width: ", this));
178         toolBar->addWidget(spinboxWidget);
179         toolBar->addWidget(new QLabel("Color: ", this));
180         toolBar->addAction(colorAction);
181         toolBar->addSeparator();
182
183 //      toolBar->addWidget(new QLabel("History", this));
184         toolBar->addAction(restartAction);
185         toolBar->addWidget(sliderWidget);
186         toolBar->addAction(playAction);
187 }