]> git.zarvox.org Git - shareboard.git/blob - mainwindow.cpp
Implement image insertion and placement, tool selection, and eraser.
[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         QObject::connect(clearAction, SIGNAL(triggered()), this, SLOT(clearBoard()));
41
42         QObject::connect(board, SIGNAL(rangeChanged(int)), this, SLOT(updateSliderRange(int)));
43         QObject::connect(board, SIGNAL(indexChanged(int)), sliderWidget, SLOT(setValue(int)));
44         QObject::connect(sliderWidget, SIGNAL(valueChanged(int)), board, SLOT(jumpToIndex(int)));
45
46         //QObject::connect(spinboxWidget, SIGNAL(valueChanged(int)), board, SLOT(setDrawState(int)));
47         //QObject::connect(spinboxWidget, SIGNAL(valueChanged(int)), this, SLOT(updateDrawState(int)));
48         QObject::connect(eraserAction, SIGNAL(triggered()), this, SLOT(useEraserTool()));
49         QObject::connect(imageAction, SIGNAL(triggered()), this, SLOT(useImageTool()));
50         QObject::connect(penAction, SIGNAL(triggered()), this, SLOT(usePenTool()));
51
52         usePenTool();
53
54         statusBar()->showMessage("Ready");
55 }
56
57 MainWindow::~MainWindow() {
58
59 }
60
61 void MainWindow::useEraserTool() {
62         board->setDrawState(spinboxWidget->value() * -1);
63 }
64
65 void MainWindow::usePenTool() {
66         board->setDrawState(spinboxWidget->value());
67 }
68
69 void MainWindow::useImageTool() {
70         QString fileName = QFileDialog::getOpenFileName(this, "Insert file:", ".", "Images (*.PNG *.png *.JPG *.jpg)");
71         if(fileName.isEmpty() )
72                 return;
73         QImage im;
74         if (!im.load(fileName))
75                 return;
76         board->setInsertImage(im);
77         board->setDrawState(0);
78 }
79
80 void MainWindow::updateSliderRange(int newmax) {
81         sliderWidget->setRange(0, newmax);
82 }
83
84 void MainWindow::chooseColor() {
85         lineColor = QColorDialog::getColor(lineColor, this, "Choose drawing color");
86         // update pixmap
87         QPixmap pixmap(16, 16);
88         pixmap.fill(lineColor);
89         colorAction->setIcon(QIcon(pixmap));
90 }
91
92 void MainWindow::clearBoard() {
93         ClearBoardAction* action = new ClearBoardAction();
94         marshallAction(action);
95 }
96
97 void MainWindow::load() {
98         QString fileName = QFileDialog::getOpenFileName(this, "Load saved board:", ".", "Shareboards (*.board)");
99         if(fileName.isEmpty()) {
100                 statusBar()->showMessage("Cancelled loading board.");
101                 return;
102         }
103         QFile file(fileName);
104         QDataStream in(&file);
105         // Load all history from that file
106         statusBar()->showMessage(QString("Successfully loaded ").append(fileName));
107 }
108
109 void MainWindow::save() {
110         QString fileName = QFileDialog::getSaveFileName(this, "Save board history as:", ".", "Shareboards (*.board)");
111         if(fileName.isEmpty()) {
112                 statusBar()->showMessage("Cancelled saving board.");
113                 return;
114         }
115         if(!fileName.endsWith(".board"))
116                 fileName = fileName.append(".board");
117         QFile file(fileName);
118         QDataStream out(&file);
119         // Output all history to that file
120         statusBar()->showMessage(QString("Successfully saved ").append(fileName));
121 }
122
123 void MainWindow::quit() {
124         // If we want, we can add some annoying dialog that says "Quit now?" and throws the user off
125         qApp->quit();
126 }
127
128 void MainWindow::switchToBoard() {
129         board->switchToBoard();
130         statusBar()->showMessage(QString("Switched to Shareboard drawing surface"));
131 }
132
133 void MainWindow::switchToConnect() {
134         board->switchToPrompt();
135         statusBar()->showMessage(QString("Switched to connection prompt"));
136 }
137
138 void MainWindow::showError(QString error) {
139         statusBar()->showMessage(error);
140 }
141
142 void MainWindow::marshallAction(Action* action) {
143         switch(action->type) {
144                 case Action::DrawLine: // set the action's thickness, color
145                         {
146                                 DrawLineAction* a = static_cast<DrawLineAction*>(action);
147                                 if(board->getDrawState() < 0 ) // This should probably all get reworked into MainWindow...
148                                         a->color = QColor(Qt::white);
149                                 else
150                                         a->color = lineColor;
151                                 a->width = spinboxWidget->value();
152                         }
153                         break;
154                 default: break;
155         }
156         board->postLocalAction(action);
157         connMan->sendAction(action);
158 }
159
160 void MainWindow::createActions() {
161         loadPastAction = new QAction("&Open a board", this);
162         saveAction = new QAction("&Save board", this);
163         quitAction = new QAction("&Quit", this);
164         QObject::connect(loadPastAction, SIGNAL(triggered()), this, SLOT(load()));
165         QObject::connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
166         QObject::connect(quitAction, SIGNAL(triggered()), this, SLOT(quit()));
167 }
168
169 void MainWindow::createMenus() {
170
171         fileMenu = menuBar()->addMenu("&File");
172         fileMenu->addAction(loadPastAction);
173         fileMenu->addAction(saveAction);
174         fileMenu->addSeparator();
175         fileMenu->addAction(quitAction);
176
177 }
178
179 void MainWindow::createToolBars() {
180
181         // Create some of the basic tool actions.
182         //   They should also be added to an exclusive tool group.
183         QActionGroup* toolGroup = new QActionGroup(this);
184         penAction = new QAction(QIcon("icons/pen.png"), "Pen", this);
185         eraserAction = new QAction(QIcon("icons/eraser.png"), "Eraser", this);
186         penAction->setChecked(true);
187         toolGroup->addAction(penAction);
188         toolGroup->addAction(eraserAction);
189
190         // Create some other one-click actions that are not a part of the action group.
191         imageAction = new QAction(QIcon("icons/import_image.png"), "Import Image", this);
192         clearAction = new QAction(QIcon("icons/clear.png"), "Clear", this);
193
194         // Style
195         spinboxWidget = new QSpinBox(this);
196         spinboxWidget->setMinimum(4);
197         spinboxWidget->setMaximum(40);
198
199
200         QPixmap pixmap(16, 16);
201         pixmap.fill(lineColor);
202         colorAction = new QAction(QIcon(pixmap), "Select Color", this);
203
204         // Create the history UI elements.
205         QAction* restartAction = new QAction(QIcon("icons/rewind.png"), "Replay History", this);
206         QAction* playAction = new QAction(QIcon("icons/play.png"), "Play", this);
207         sliderWidget = new QSlider(Qt::Horizontal, this);
208         sliderWidget->setSingleStep(1);
209         sliderWidget->setPageStep(30);
210
211         toolBar = addToolBar("Test");
212         toolBar->setMovable(false);
213
214         toolBar->addSeparator();
215 //      toolBar->addWidget(new QLabel("Tools", this));
216         toolBar->addAction(penAction);
217         toolBar->addAction(eraserAction);
218         toolBar->addAction(imageAction);
219         toolBar->addAction(clearAction);
220         toolBar->addSeparator();
221
222 //      toolBar->addWidget(new QLabel("Style", this));
223         toolBar->addWidget(new QLabel("Width: ", this));
224         toolBar->addWidget(spinboxWidget);
225         toolBar->addWidget(new QLabel("Color: ", this));
226         toolBar->addAction(colorAction);
227         toolBar->addSeparator();
228
229 //      toolBar->addWidget(new QLabel("History", this));
230         toolBar->addAction(restartAction);
231         toolBar->addWidget(sliderWidget);
232         toolBar->addAction(playAction);
233 }