diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index f14872dd15..20ae07b0c9 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -438,6 +438,7 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) : void Application::aboutToQuit() { _aboutToQuit = true; + setFullscreen(false); // if you exit while in full screen, you'll get bad behavior when you restart. } Application::~Application() { diff --git a/interface/src/ui/HMDToolsDialog.cpp b/interface/src/ui/HMDToolsDialog.cpp index a5e54fa546..88924f68d1 100644 --- a/interface/src/ui/HMDToolsDialog.cpp +++ b/interface/src/ui/HMDToolsDialog.cpp @@ -30,27 +30,27 @@ HMDToolsDialog::HMDToolsDialog(QWidget* parent) : QDialog(parent, Qt::Window | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint) , _previousScreen(NULL), _hmdScreen(NULL), - _previousDialogScreen(NULL) + _hmdScreenNumber(-1), + _switchModeButton(NULL), + _debugDetails(NULL), + _previousDialogScreen(NULL), + _inHDMMode(false) { this->setWindowTitle("HMD Tools"); // Create layouter QFormLayout* form = new QFormLayout(); + const int WIDTH = 350; // Add a button to enter - QPushButton* enterModeButton = new QPushButton("Enter HMD Mode"); - form->addRow("", enterModeButton); - connect(enterModeButton,SIGNAL(clicked(bool)),this,SLOT(enterModeClicked(bool))); - - // Add a button to leave - QPushButton* leaveModeButton = new QPushButton("Leave HMD Mode"); - form->addRow("", leaveModeButton); - connect(leaveModeButton,SIGNAL(clicked(bool)),this,SLOT(leaveModeClicked(bool))); + _switchModeButton = new QPushButton("Enter HMD Mode"); + _switchModeButton->setFixedWidth(WIDTH); + form->addRow("", _switchModeButton); + connect(_switchModeButton,SIGNAL(clicked(bool)),this,SLOT(switchModeClicked(bool))); // Create a label with debug details... _debugDetails = new QLabel(); _debugDetails->setText(getDebugDetails()); - const int WIDTH = 350; const int HEIGHT = 100; _debugDetails->setFixedSize(WIDTH, HEIGHT); form->addRow("", _debugDetails); @@ -65,14 +65,21 @@ HMDToolsDialog::HMDToolsDialog(QWidget* parent) : QWindow* mainWindow = Application::getInstance()->getWindow()->windowHandle(); connect(mainWindow, &QWindow::screenChanged, this, &HMDToolsDialog::applicationWindowScreenChanged); - // watch for our dialog window moving screens. If it does we want to enforce our rules about what screens we're - // allowed on + // watch for our dialog window moving screens. If it does we want to enforce our rules about + // what screens we're allowed on QWindow* dialogWindow = windowHandle(); connect(dialogWindow, &QWindow::screenChanged, this, &HMDToolsDialog::dialogWindowScreenChanged); connect(dialogWindow, &QWindow::xChanged, this, &HMDToolsDialog::dialogWindowGeometryChanged); connect(dialogWindow, &QWindow::yChanged, this, &HMDToolsDialog::dialogWindowGeometryChanged); connect(dialogWindow, &QWindow::widthChanged, this, &HMDToolsDialog::dialogWindowGeometryChanged); connect(dialogWindow, &QWindow::heightChanged, this, &HMDToolsDialog::dialogWindowGeometryChanged); + + // when the application is about to quit, leave HDM mode + connect(Application::getInstance(), SIGNAL(aboutToQuit()), this, SLOT(aboutToQuit())); + + // keep track of changes to the number of screens + connect(QApplication::desktop(), &QDesktopWidget::screenCountChanged, this, &HMDToolsDialog::screenCountChanged); + } HMDToolsDialog::~HMDToolsDialog() { @@ -96,11 +103,11 @@ void HMDToolsDialog::dialogWindowScreenChanged(QScreen* screen) { // if we have more than one screen, and a known hmdScreen then try to // keep our dialog off of the hmdScreen if (QApplication::desktop()->screenCount() > 1) { - int hmdScreenNumber = OculusManager::getHMDScreen(); - - if (hmdScreenNumber >= 0) { - QScreen* hmdScreen = QGuiApplication::screens()[hmdScreenNumber]; + // we want to use a local variable here because we are not necesarily in HMD mode + int hmdScreenNumber = OculusManager::getHMDScreen(); + if (_hmdScreenNumber >= 0) { + QScreen* hmdScreen = QGuiApplication::screens()[hmdScreenNumber]; if (screen == hmdScreen) { qDebug() << "HMD Tools: Whoa! What are you doing? You don't want to move me to the HMD Screen!"; QWindow* dialogWindow = windowHandle(); @@ -154,42 +161,55 @@ QString HMDToolsDialog::getDebugDetails() const { results += "Application Primary Screen: " + QGuiApplication::primaryScreen()->name() + "\n"; QScreen* mainWindowScreen = Application::getInstance()->getWindow()->windowHandle()->screen(); results += "Application Main Window Screen: " + mainWindowScreen->name() + "\n"; + results += "Total Screens: " + QString::number(QApplication::desktop()->screenCount()) + "\n"; return results; } -void HMDToolsDialog::enterModeClicked(bool checked) { - _debugDetails->setText(getDebugDetails()); +void HMDToolsDialog::switchModeClicked(bool checked) { + if (!_inHDMMode) { + enterHDMMode(); + } else { + leaveHDMMode(); + } +} - int hmdScreen = OculusManager::getHMDScreen(); - qDebug() << "enterModeClicked().... hmdScreen:" << hmdScreen; +void HMDToolsDialog::enterHDMMode() { + if (!_inHDMMode) { + _switchModeButton->setText("Leave HMD Mode"); + _debugDetails->setText(getDebugDetails()); + + _hmdScreenNumber = OculusManager::getHMDScreen(); - if (hmdScreen >= 0) { - QWindow* mainWindow = Application::getInstance()->getWindow()->windowHandle(); - _hmdScreen = QGuiApplication::screens()[hmdScreen]; + if (_hmdScreenNumber >= 0) { + QWindow* mainWindow = Application::getInstance()->getWindow()->windowHandle(); + _hmdScreen = QGuiApplication::screens()[_hmdScreenNumber]; - _previousRect = Application::getInstance()->getWindow()->rect(); - _previousRect = QRect(mainWindow->mapToGlobal(_previousRect.topLeft()), - mainWindow->mapToGlobal(_previousRect.bottomRight())); - _previousScreen = mainWindow->screen(); - QRect rect = QApplication::desktop()->screenGeometry(hmdScreen); - mainWindow->setScreen(_hmdScreen); - mainWindow->setGeometry(rect); + _previousRect = Application::getInstance()->getWindow()->rect(); + _previousRect = QRect(mainWindow->mapToGlobal(_previousRect.topLeft()), + mainWindow->mapToGlobal(_previousRect.bottomRight())); + _previousScreen = mainWindow->screen(); + QRect rect = QApplication::desktop()->screenGeometry(_hmdScreenNumber); + mainWindow->setScreen(_hmdScreen); + mainWindow->setGeometry(rect); - _wasMoved = true; - } + _wasMoved = true; + } - // if we're on a single screen setup, then hide our tools window when entering HMD mode - if (QApplication::desktop()->screenCount() == 1) { - close(); - } + // if we're on a single screen setup, then hide our tools window when entering HMD mode + if (QApplication::desktop()->screenCount() == 1) { + close(); + } - Application::getInstance()->setFullscreen(true); - Application::getInstance()->setEnableVRMode(true); + Application::getInstance()->setFullscreen(true); + Application::getInstance()->setEnableVRMode(true); - const int SLIGHT_DELAY = 500; - QTimer::singleShot(SLIGHT_DELAY, this, SLOT(activateWindowAfterEnterMode())); + const int SLIGHT_DELAY = 500; + QTimer::singleShot(SLIGHT_DELAY, this, SLOT(activateWindowAfterEnterMode())); + + _inHDMMode = true; + } } void HMDToolsDialog::activateWindowAfterEnterMode() { @@ -199,23 +219,26 @@ void HMDToolsDialog::activateWindowAfterEnterMode() { centerCursorOnWidget(Application::getInstance()->getWindow()); } +void HMDToolsDialog::leaveHDMMode() { + if (_inHDMMode) { + _switchModeButton->setText("Enter HMD Mode"); + _debugDetails->setText(getDebugDetails()); -void HMDToolsDialog::leaveModeClicked(bool checked) { - _debugDetails->setText(getDebugDetails()); + Application::getInstance()->setFullscreen(false); + Application::getInstance()->setEnableVRMode(false); + Application::getInstance()->getWindow()->activateWindow(); - Application::getInstance()->setFullscreen(false); - Application::getInstance()->setEnableVRMode(false); - Application::getInstance()->getWindow()->activateWindow(); - - if (_wasMoved) { - QWindow* mainWindow = Application::getInstance()->getWindow()->windowHandle(); - mainWindow->setScreen(_previousScreen); - mainWindow->setGeometry(_previousRect); + if (_wasMoved) { + QWindow* mainWindow = Application::getInstance()->getWindow()->windowHandle(); + mainWindow->setScreen(_previousScreen); + mainWindow->setGeometry(_previousRect); - const int SLIGHT_DELAY = 1500; - QTimer::singleShot(SLIGHT_DELAY, this, SLOT(moveWindowAfterLeaveMode())); + const int SLIGHT_DELAY = 1500; + QTimer::singleShot(SLIGHT_DELAY, this, SLOT(moveWindowAfterLeaveMode())); + } + _wasMoved = false; + _inHDMMode = false; } - _wasMoved = false; } void HMDToolsDialog::moveWindowAfterLeaveMode() { @@ -256,3 +279,31 @@ void HMDToolsDialog::hideEvent(QHideEvent* event) { } +void HMDToolsDialog::aboutToQuit() { + if (_inHDMMode) { + leaveHDMMode(); + } +} + +void HMDToolsDialog::screenCountChanged(int newCount) { + if (!OculusManager::isConnected()) { + OculusManager::connect(); + } + int hmdScreenNumber = OculusManager::getHMDScreen(); + + if (_inHDMMode && _hmdScreenNumber != hmdScreenNumber) { + qDebug() << "HMD Display changed WHILE IN HMD MODE"; + leaveHDMMode(); + + // if there is a new best HDM screen then go back into HDM mode after done leaving + if (hmdScreenNumber >= 0) { + qDebug() << "Trying to go back into HDM Mode"; + const int SLIGHT_DELAY = 2000; + QTimer::singleShot(SLIGHT_DELAY, this, SLOT(enterHDMMode())); + } + } + _debugDetails->setText(getDebugDetails()); +} + + + diff --git a/interface/src/ui/HMDToolsDialog.h b/interface/src/ui/HMDToolsDialog.h index 4fe0dd626f..e3e5573533 100644 --- a/interface/src/ui/HMDToolsDialog.h +++ b/interface/src/ui/HMDToolsDialog.h @@ -28,14 +28,15 @@ signals: public slots: void reject(); - void enterModeClicked(bool checked); - void leaveModeClicked(bool checked); + void switchModeClicked(bool checked); void activateWindowAfterEnterMode(); void moveWindowAfterLeaveMode(); void applicationWindowScreenChanged(QScreen* screen); void dialogWindowScreenChanged(QScreen* screen); void dialogWindowGeometryChanged(int arg); - + void aboutToQuit(); + void screenCountChanged(int newCount); + protected: virtual void closeEvent(QCloseEvent*); // Emits a 'closed' signal when this dialog is closed. virtual void showEvent(QShowEvent* event); @@ -43,15 +44,20 @@ protected: private: void centerCursorOnWidget(QWidget* widget); + void enterHDMMode(); + void leaveHDMMode(); bool _wasMoved; QRect _previousRect; QScreen* _previousScreen; QScreen* _hmdScreen; + int _hmdScreenNumber; + QPushButton* _switchModeButton; QLabel* _debugDetails; QRect _previousDialogRect; QScreen* _previousDialogScreen; + bool _inHDMMode; }; #endif // hifi_HMDToolsDialog_h