From fa068e62cbf70d93296de7887cbcfdda5d36d2b2 Mon Sep 17 00:00:00 2001
From: Joanna Hulboj <joanna.hulboj@isode.com>
Date: Thu, 12 Jan 2017 11:10:23 +0000
Subject: Set cursor position to the center of the screen to show layout window
 in the middle of the screen

Test-Information:
Tested with multiple screen setup.
Tested only on Windows.
From view menu choose "Change Layout", the layout window appears in the
middle of the screen.

Change-Id: Iab4ba64d54766426be39926773aa5b5bb628b9e9

diff --git a/Swift/QtUI/QtChatTabs.cpp b/Swift/QtUI/QtChatTabs.cpp
index 3241858..de9d2d2 100644
--- a/Swift/QtUI/QtChatTabs.cpp
+++ b/Swift/QtUI/QtChatTabs.cpp
@@ -19,6 +19,7 @@
 #include <QTabBar>
 #include <QTabWidget>
 #include <QtGlobal>
+#include <QWindow>
 
 #include <Swiften/Base/Log.h>
 
@@ -372,9 +373,16 @@ void QtChatTabs::flash() {
 void QtChatTabs::handleOpenLayoutChangeDialog() {
     disconnect(gridSelectionDialog_, SIGNAL(currentGridSizeChanged(QSize)), dynamicGrid_, SLOT(setDimensions(QSize)));
     gridSelectionDialog_->setCurrentGridSize(dynamicGrid_->getDimension());
-    gridSelectionDialog_->move(QCursor::pos());
+
+    int screen = QApplication::desktop()->screenNumber(QCursor::pos());
+    QPoint center = QApplication::desktop()->screenGeometry(screen).center();
+    gridSelectionDialog_->move(center);
+
     connect(gridSelectionDialog_, SIGNAL(currentGridSizeChanged(QSize)), dynamicGrid_, SLOT(setDimensions(QSize)));
     gridSelectionDialog_->show();
+
+    QPoint pos(gridSelectionDialog_->getFrameSize().width() / 2, gridSelectionDialog_->getFrameSize().height() / 2);
+    QCursor::setPos(gridSelectionDialog_->windowHandle()->screen(), gridSelectionDialog_->mapToGlobal(QPoint(gridSelectionDialog_->width(), gridSelectionDialog_->height()) - pos));
 }
 
 void QtChatTabs::storeTabPositions() {
diff --git a/Swift/QtUI/Trellis/QtGridSelectionDialog.cpp b/Swift/QtUI/Trellis/QtGridSelectionDialog.cpp
index 1ca1953..fb9734d 100644
--- a/Swift/QtUI/Trellis/QtGridSelectionDialog.cpp
+++ b/Swift/QtUI/Trellis/QtGridSelectionDialog.cpp
@@ -68,6 +68,10 @@ QSize QtGridSelectionDialog::getMaxGridSize() const {
     return maxGridSize;
 }
 
+QSize QtGridSelectionDialog::getFrameSize() const {
+    return frameSize;
+}
+
 void QtGridSelectionDialog::keyReleaseEvent(QKeyEvent* event) {
     if (event) {
         QSize newGridSize = currentGridSize;
@@ -133,13 +137,6 @@ void QtGridSelectionDialog::paintEvent(QPaintEvent*) {
 }
 
 void QtGridSelectionDialog::showEvent(QShowEvent*) {
-    int xPos = horizontalMargin + frameSize.width() + (padding + frameSize.width()) * (currentGridSize.width() - 1) - frameSize.width()/2;
-    int yPos = verticalMargin + frameSize.height() + (padding + frameSize.height()) * (currentGridSize.height() - 1) - frameSize.height()/2;
-    // Apply the above offset to the global cursor position (which was positioned at a known position before showing the widget):
-    QPoint pos = QCursor::pos();
-    pos.rx() += xPos;
-    pos.ry() += yPos;
-    QCursor::setPos(pos);
     setMouseTracking(true);
 }
 
diff --git a/Swift/QtUI/Trellis/QtGridSelectionDialog.h b/Swift/QtUI/Trellis/QtGridSelectionDialog.h
index 557963d..3ccba55 100644
--- a/Swift/QtUI/Trellis/QtGridSelectionDialog.h
+++ b/Swift/QtUI/Trellis/QtGridSelectionDialog.h
@@ -29,6 +29,8 @@ namespace Swift {
             void setMaxGridSize(const QSize& size);
             QSize getMaxGridSize() const;
 
+            QSize getFrameSize() const;
+
         signals:
             void currentGridSizeChanged(QSize);
             void minGridSizeChanged(QSize);
-- 
cgit v0.10.2-6-g49f6