1 #include "mainwindow.h"
3 #include <QApplication>
6 #include <QColorDialog>
13 #include <QColorDialog>
15 #include "connectionmanager.h"
16 #include "shareboard.h"
19 MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) {
26 board = new Shareboard(this);
27 setCentralWidget(board);
29 connMan = new ConnectionManager(this);
30 connMan->setTarget(board);
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*)));
39 QObject::connect(colorAction, SIGNAL(triggered()), this, SLOT(chooseColor()));
40 QObject::connect(clearAction, SIGNAL(triggered()), this, SLOT(clearBoard()));
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)));
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()));
54 statusBar()->showMessage("Ready");
57 MainWindow::~MainWindow() {
61 void MainWindow::useEraserTool() {
62 board->setDrawState(spinboxWidget->value() * -1);
65 void MainWindow::usePenTool() {
66 board->setDrawState(spinboxWidget->value());
69 void MainWindow::useImageTool() {
70 QString fileName = QFileDialog::getOpenFileName(this, "Insert file:", ".", "Images (*.PNG *.png *.JPG *.jpg)");
71 if(fileName.isEmpty() )
74 if (!im.load(fileName))
76 board->setInsertImage(im);
77 board->setDrawState(0);
80 void MainWindow::updateSliderRange(int newmax) {
81 sliderWidget->setRange(0, newmax);
84 void MainWindow::chooseColor() {
85 lineColor = QColorDialog::getColor(lineColor, this, "Choose drawing color");
87 QPixmap pixmap(16, 16);
88 pixmap.fill(lineColor);
89 colorAction->setIcon(QIcon(pixmap));
92 void MainWindow::clearBoard() {
93 ClearBoardAction* action = new ClearBoardAction();
94 marshallAction(action);
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.");
103 QFile file(fileName);
104 QDataStream in(&file);
105 // Load all history from that file
106 statusBar()->showMessage(QString("Successfully loaded ").append(fileName));
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.");
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));
123 void MainWindow::quit() {
124 // If we want, we can add some annoying dialog that says "Quit now?" and throws the user off
128 void MainWindow::switchToBoard() {
129 board->switchToBoard();
130 statusBar()->showMessage(QString("Switched to Shareboard drawing surface"));
133 void MainWindow::switchToConnect() {
134 board->switchToPrompt();
135 statusBar()->showMessage(QString("Switched to connection prompt"));
138 void MainWindow::showError(QString error) {
139 statusBar()->showMessage(error);
142 void MainWindow::marshallAction(Action* action) {
143 switch(action->type) {
144 case Action::DrawLine: // set the action's thickness, color
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);
150 a->color = lineColor;
151 a->width = spinboxWidget->value();
156 board->postLocalAction(action);
157 connMan->sendAction(action);
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()));
169 void MainWindow::createMenus() {
171 fileMenu = menuBar()->addMenu("&File");
172 fileMenu->addAction(loadPastAction);
173 fileMenu->addAction(saveAction);
174 fileMenu->addSeparator();
175 fileMenu->addAction(quitAction);
179 void MainWindow::createToolBars() {
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);
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);
195 spinboxWidget = new QSpinBox(this);
196 spinboxWidget->setMinimum(4);
197 spinboxWidget->setMaximum(40);
200 QPixmap pixmap(16, 16);
201 pixmap.fill(lineColor);
202 colorAction = new QAction(QIcon(pixmap), "Select Color", this);
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);
211 toolBar = addToolBar("Test");
212 toolBar->setMovable(false);
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();
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();
229 // toolBar->addWidget(new QLabel("History", this));
230 toolBar->addAction(restartAction);
231 toolBar->addWidget(sliderWidget);
232 toolBar->addAction(playAction);