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()));
41 statusBar()->showMessage("Ready");
44 MainWindow::~MainWindow() {
48 void MainWindow::chooseColor() {
49 lineColor = QColorDialog::getColor(lineColor, this, "Choose drawing color");
51 QPixmap pixmap(16, 16);
52 pixmap.fill(lineColor);
53 colorAction->setIcon(QIcon(pixmap));
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.");
63 QDataStream in(&file);
64 // Load all history from that file
65 statusBar()->showMessage(QString("Successfully loaded ").append(fileName));
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.");
74 if(!fileName.endsWith(".board"))
75 fileName = fileName.append(".board");
77 QDataStream out(&file);
78 // Output all history to that file
79 statusBar()->showMessage(QString("Successfully saved ").append(fileName));
82 void MainWindow::quit() {
83 // If we want, we can add some annoying dialog that says "Quit now?" and throws the user off
87 void MainWindow::switchToBoard() {
88 board->switchToBoard();
89 statusBar()->showMessage(QString("Switched to Shareboard drawing surface"));
92 void MainWindow::switchToConnect() {
93 board->switchToPrompt();
94 statusBar()->showMessage(QString("Switched to connection prompt"));
97 void MainWindow::showError(QString error) {
98 statusBar()->showMessage(error);
101 void MainWindow::marshallAction(Action* action) {
102 switch(action->type) {
103 case Action::DrawLine: // set the action's thickness, color
105 DrawLineAction* a = static_cast<DrawLineAction*>(action);
106 a->color = lineColor;
107 a->width = spinboxWidget->value();
112 board->postLocalAction(action);
113 connMan->sendAction(action);
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()));
125 void MainWindow::createMenus() {
127 fileMenu = menuBar()->addMenu("&File");
128 fileMenu->addAction(loadPastAction);
129 fileMenu->addAction(saveAction);
130 fileMenu->addSeparator();
131 fileMenu->addAction(quitAction);
135 void MainWindow::createToolBars() {
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);
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);
151 spinboxWidget = new QSpinBox(this);
152 spinboxWidget->setMinimum(4);
153 spinboxWidget->setMaximum(40);
156 QPixmap pixmap(16, 16);
157 pixmap.fill(lineColor);
158 colorAction = new QAction(QIcon(pixmap), "Select Color", this);
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);
165 toolBar = addToolBar("Test");
166 toolBar->setMovable(false);
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();
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();
183 // toolBar->addWidget(new QLabel("History", this));
184 toolBar->addAction(restartAction);
185 toolBar->addWidget(sliderWidget);
186 toolBar->addAction(playAction);