From f193e6f2e2385a9e240db18d2fda07aae42e1d2a Mon Sep 17 00:00:00 2001 From: Christian Klinger Date: Wed, 27 Apr 2016 12:24:37 +0200 Subject: refactor to use Room struct to save additional properties like gridSize and clientSize. --- src/server/mainwindow/mainwindow.cpp | 78 ++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 35 deletions(-) (limited to 'src/server/mainwindow/mainwindow.cpp') diff --git a/src/server/mainwindow/mainwindow.cpp b/src/server/mainwindow/mainwindow.cpp index a60d1a7..4963ccc 100644 --- a/src/server/mainwindow/mainwindow.cpp +++ b/src/server/mainwindow/mainwindow.cpp @@ -59,7 +59,7 @@ MainWindow::MainWindow(QString ipListUrl, QWidget* parent) : _mode = Mode::Multicast; _streamingSource = 0; - /* stupid default, just for testing */ + /* default value, these should be updated on loading a room */ _tilesX = 20; _tilesY = 20; @@ -142,11 +142,6 @@ MainWindow::MainWindow(QString ipListUrl, QWidget* parent) : this->onSessionNameUpdate(); // Just make lable visible. _countSessionNameUpdate = 0; - /* better calculate on demand */ -// _tileWidth = ui->frmRoom->size().width() / _tilesX; -// _tileHeight = ui->frmRoom->size().height() / _tilesY; - - tryToUseRoomTemplate(); } @@ -310,42 +305,54 @@ void MainWindow::tellClientCurrentSituation(Client* client) */ void MainWindow::tryToUseRoomTemplate() { - QMap > roomsList; + QMap roomsList; SYSTEM_SETTINGS(conf); - if (!conf.contains("rooms")) { - qDebug() << "Invalid config file!"; - return; - } + if (!conf.contains("rooms")) { qDebug() << "Invalid config file!"; return; } QStringList rooms = conf.value("rooms").toStringList(); qDebug() << rooms; - for (auto i : rooms) + for (QString roomName : rooms) { - qDebug() << "i: " << i; - conf.beginGroup(i); + qDebug() << "i: " << roomName; + conf.beginGroup(roomName); if (!conf.contains("mgrIP")) { qDebug() << "Invalid config file!"; return; } - + QMap clientPositions; // First store all room configurations in _rooms. int size = conf.beginReadArray("client"); for (int j = 0; j < size; j++) { conf.setArrayIndex(j); // qDebug() << "ip: " << conf.value("ip").toString() << " pos: " << conf.value("pos").toPoint(); - roomsList[i].insert(conf.value("ip").toString(), conf.value("pos").toPoint()); + // roomsList[i].insert(conf.value("ip").toString(), conf.value("pos").toPoint()); + clientPositions.insert(conf.value("ip").toString(), conf.value("pos").toPoint()); } conf.endArray(); // Find the managerIP of current room. QString mgrIP = conf.value("mgrIP").toString(); + + QSize gridSize; + QSize clientSize(1,1); + /* read some other properties of the room */ + if (conf.contains("gridSize")) { + gridSize = conf.value("gridSize").toSize(); + qDebug() << "read gridSize: " << gridSize ; + } + if (conf.contains("clientSize")) { + clientSize = conf.value("clientSize").toSize(); + qDebug() << "read clientSize: " << clientSize; + } + + foreach (const QHostAddress &address, QNetworkInterface::allAddresses()) { if (address != QHostAddress(QHostAddress::LocalHost) && mgrIP == address.toString()) { qDebug("Found this ip in config."); - Global::setCurrentRoom(i); + Global::setCurrentRoom(roomName); int size = conf.beginReadArray("client"); for (int j = 0; j < size; ++j) { conf.setArrayIndex(j); @@ -377,6 +384,9 @@ void MainWindow::tryToUseRoomTemplate() } } conf.endGroup(); + + Room* r = new Room(clientPositions, gridSize, clientSize); + roomsList.insert(roomName, r); } Global::setRooms(roomsList); } @@ -472,16 +482,12 @@ void MainWindow::resizeEvent(QResizeEvent* e) const int nh = ui->frmRoom->size().height() / _tilesY; for (QList::iterator it = _clientFrames.begin(); it != _clientFrames.end(); ++it) { - const QPoint &oldpos = (*it)->frameGeometry().topLeft(); int newPosX = (*it)->getGridPosition().x() * getTileWidthPx(); int newPosY = (*it)->getGridPosition().y() * getTileHeightPx(); QPoint newPos(newPosX, newPosY); - qDebug("Move C"); (*it)->setSize(getTileWidthPx(), getTileHeightPx()); (*it)->move(newPos); - //(*it)->move((oldpos.x() / getTileWidthPx()) * nw, (oldpos.y() / getTileHeightPx()) * nh); - //(*it)->setSize(nw, nh); } // Resize trash and set position. @@ -754,26 +760,28 @@ void MainWindow::onReloadRoomOk() _reloadWindow->hide(); - QMap room = Global::getRooms()[roomToReload]; + Room *room = Global::getRooms()[roomToReload]; + bool gridSizeSet = false; /* TODO: read grid size from config */ + if (!gridSizeSet) { + /* collect the maximum coordinates */ + _tilesX = 0; + _tilesY = 0; - /* collect the maximum coordinates to resize the grid */ - _tilesX = 0; - _tilesY = 0; + for (QMap::iterator it = room->clientPositions.begin(); it != room->clientPositions.end(); ++it) { + QPoint pos = it.value(); + if (pos.x() > _tilesX) { _tilesX = pos.x(); } + if (pos.y() > _tilesY) { _tilesY = pos.y(); } - for (QMap::iterator it = room.begin(); it != room.end(); ++it) { - QPoint pos = it.value(); - if (pos.x() > _tilesX) { _tilesX = pos.x(); } - if (pos.y() > _tilesY) { _tilesY = pos.y(); } + } + /* need a little extra space */ + _tilesX += 1; + _tilesY += 1; } - /* need a little extra space */ - _tilesX += 1; - _tilesY += 1; - qDebug() << "New Grid Size: " << _tilesX << ", " << _tilesY << endl; // qDebug() << " Room: " << room; // qDebug() << "_rooms: " << _rooms; - for (QMap::iterator it = room.begin(); it != room.end(); ++it) + for (QMap::iterator it = room->clientPositions.begin(); it != room->clientPositions.end(); ++it) { QString computerId = it.key(); QPoint pos = it.value(); @@ -786,7 +794,7 @@ void MainWindow::onReloadRoomOk() QPoint pxPos(pos.x() * getTileWidthPx(), pos.y() * getTileHeightPx()); ConnectionFrame *cf = createFrame(computerId, pxPos, pos); - cf->move(cf->getCurrentPosition()); /* TODO: this affects the placement */ + cf->move(cf->getCurrentPosition()); onPlaceFrame(false, cf); } } -- cgit v1.2.3-55-g7522