summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoanna Hulboj <joanna.hulboj@isode.com>2017-01-12 11:10:23 (GMT)
committerJoanna Hulboj <joanna.hulboj@isode.com>2017-01-18 10:39:02 (GMT)
commitfa068e62cbf70d93296de7887cbcfdda5d36d2b2 (patch)
tree8993c4d5bddb7446dcd675f522af73c519e3db44
parente649c54daa666c99d2f934b223acd0262c05e11a (diff)
downloadswift-fa068e62cbf70d93296de7887cbcfdda5d36d2b2.zip
swift-fa068e62cbf70d93296de7887cbcfdda5d36d2b2.tar.bz2
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
-rw-r--r--Swift/QtUI/QtChatTabs.cpp10
-rw-r--r--Swift/QtUI/Trellis/QtGridSelectionDialog.cpp11
-rw-r--r--Swift/QtUI/Trellis/QtGridSelectionDialog.h2
3 files changed, 15 insertions, 8 deletions
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
@@ -1,51 +1,52 @@
/*
* Copyright (c) 2010-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#include <Swift/QtUI/QtChatTabs.h>
#include <algorithm>
#include <vector>
#include <QAction>
#include <QApplication>
#include <QCloseEvent>
#include <QCursor>
#include <QDesktopWidget>
#include <QLayout>
#include <QMenu>
#include <QTabBar>
#include <QTabWidget>
#include <QtGlobal>
+#include <QWindow>
#include <Swiften/Base/Log.h>
#include <Swift/Controllers/ChatMessageSummarizer.h>
#include <Swift/QtUI/QtSwiftUtil.h>
#include <Swift/QtUI/QtTabWidget.h>
#include <Swift/QtUI/QtTabbable.h>
#include <Swift/QtUI/QtUISettingConstants.h>
#include <Swift/QtUI/Trellis/QtDynamicGridLayout.h>
#include <Swift/QtUI/Trellis/QtGridSelectionDialog.h>
namespace Swift {
QtChatTabs::QtChatTabs(bool singleWindow, SettingsProvider* settingsProvider, bool trellisMode) : QWidget(), singleWindow_(singleWindow), settingsProvider_(settingsProvider), trellisMode_(trellisMode), dynamicGrid_(nullptr), gridSelectionDialog_(nullptr) {
#ifndef Q_OS_MAC
setWindowIcon(QIcon(":/logo-chat-16.png"));
#else
setAttribute(Qt::WA_ShowWithoutActivating);
#endif
dynamicGrid_ = new QtDynamicGridLayout(this, trellisMode);
connect(dynamicGrid_, SIGNAL(tabCloseRequested(int)), this, SLOT(handleTabCloseRequested(int)));
connect(dynamicGrid_, SIGNAL(onCurrentIndexChanged(int)), this, SLOT(handleCurrentTabIndexChanged(int)));
QVBoxLayout *layout = new QVBoxLayout;
layout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(dynamicGrid_);
setLayout(layout);
if (trellisMode) {
@@ -345,63 +346,70 @@ void QtChatTabs::handleTabTitleUpdated(QWidget* widget) {
case QtTabbable::WaitingActivity : tabTextColor = QColor(217, 20, 43); break;
case QtTabbable::ImpendingActivity : tabTextColor = QColor(27, 171, 32); break;
case QtTabbable::NoActivity : tabTextColor = QColor(); break;
}
tabWidget->tabBar()->setTabTextColor(tabIndex, tabTextColor);
std::vector<std::pair<std::string, int> > unreads;
for (int i = 0; i < dynamicGrid_->count(); i++) {
QtTabbable* tab = qobject_cast<QtTabbable*>(dynamicGrid_->widget(i));
if (tab) {
unreads.push_back(std::pair<std::string, int>(Q2PSTRING(tab->windowTitle()), tab->getCount()));
}
}
std::string current(Q2PSTRING(qobject_cast<QtTabbable*>(dynamicGrid_->currentWidget())->windowTitle()));
ChatMessageSummarizer summary;
QString title = summary.getSummary(current, unreads).c_str();
setWindowTitle(title);
emit onTitleChanged(title);
}
void QtChatTabs::flash() {
#ifndef SWIFTEN_PLATFORM_MACOSX
QApplication::alert(this, 0);
#endif
}
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() {
// save size
QByteArray gridSizeData;
QDataStream dataStreamGridSize(&gridSizeData, QIODevice::ReadWrite);
dataStreamGridSize << dynamicGrid_->getDimension();
settingsProvider_->storeSetting(QtUISettingConstants::TRELLIS_GRID_SIZE, Q2PSTRING(QString(gridSizeData.toBase64())));
// save positions
QByteArray tabPositionsData;
QDataStream dataStreamTabPositions(&tabPositionsData, QIODevice::ReadWrite);
dynamicGrid_->updateTabPositions();
dataStreamTabPositions << dynamicGrid_->getTabPositions();
settingsProvider_->storeSetting(QtUISettingConstants::TRELLIS_GRID_POSITIONS, Q2PSTRING(QString(tabPositionsData.toBase64())));
}
void QtChatTabs::resizeEvent(QResizeEvent*) {
emit geometryChanged();
}
void QtChatTabs::moveEvent(QMoveEvent*) {
emit geometryChanged();
}
void QtChatTabs::checkForFirstShow() {
if (!isVisible()) {
#ifndef Q_OS_MAC
showMinimized();
#else
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
@@ -41,60 +41,64 @@ QSize QtGridSelectionDialog::sizeHint() const {
return QSize(width, height);
}
void QtGridSelectionDialog::setCurrentGridSize(const QSize& size) {
currentGridSize = size;
emit currentGridSizeChanged(size);
}
QSize QtGridSelectionDialog::getCurrentGridSize() const {
return currentGridSize;
}
void QtGridSelectionDialog::setMinGridSize(const QSize& size) {
minGridSize = size;
emit minGridSizeChanged(size);
}
QSize QtGridSelectionDialog::getMinGridSize() const {
return minGridSize;
}
void QtGridSelectionDialog::setMaxGridSize(const QSize& size) {
maxGridSize = size;
emit maxGridSizeChanged(size);
}
QSize QtGridSelectionDialog::getMaxGridSize() const {
return maxGridSize;
}
+QSize QtGridSelectionDialog::getFrameSize() const {
+ return frameSize;
+}
+
void QtGridSelectionDialog::keyReleaseEvent(QKeyEvent* event) {
if (event) {
QSize newGridSize = currentGridSize;
if (event->key() == Qt::Key_Up) {
newGridSize += QSize(0, -1);
}
else if (event->key() == Qt::Key_Down) {
newGridSize += QSize(0, 1);
}
else if (event->key() == Qt::Key_Left) {
newGridSize += QSize(-1, 0);
}
else if (event->key() == Qt::Key_Right) {
newGridSize += QSize(1, 0);
}
else if (event->key() == Qt::Key_Return) {
hide();
setCurrentGridSize(currentGridSize);
}
else if (event->key() == Qt::Key_Escape) {
hide();
}
if (minGridSize.expandedTo(newGridSize).boundedTo(maxGridSize) != currentGridSize) {
currentGridSize = minGridSize.expandedTo(newGridSize).boundedTo(maxGridSize);
QSize newSizeHint = sizeHint();
resize(newSizeHint);
QCursor::setPos(mapToGlobal(QPoint(newSizeHint.width(), newSizeHint.height()) - QPoint(frameSize.width() / 2, frameSize.height() / 2)));
}
@@ -106,67 +110,60 @@ void QtGridSelectionDialog::mousePressEvent(QMouseEvent*) {
setCurrentGridSize(currentGridSize);
}
void QtGridSelectionDialog::paintEvent(QPaintEvent*) {
QPainter painter(this);
// draw grid
QRect gridCell = QRect(QPoint(0,0), frameSize);
painter.setBrush(palette().highlight());
painter.setPen(Qt::NoPen);
for (int x = 0; x < currentGridSize.width(); x++) {
for (int y = 0; y < currentGridSize.height(); y++) {
int xPos = horizontalMargin + (x * (frameSize.width() + padding));
int yPos = verticalMargin + (y * (frameSize.height() + padding));
gridCell.moveTo(QPoint(xPos, yPos));
painter.drawRect(gridCell);
}
}
// draw description text
auto fontMetrics = QFontMetrics(QApplication::font());
auto descriptionBB = fontMetrics.boundingRect(QRect(0,0, width() - 2 * horizontalMargin,0), Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, descriptionText, 0, 0);
QStyleOption opt;
opt.initFrom(this);
int textY = verticalMargin + (currentGridSize.height() * (frameSize.height() + padding));
int textX = (size().width() - descriptionBB.width()) / 2;
style()->drawItemText(&painter, QRect(textX, textY, descriptionBB.width(), descriptionBB.height()), Qt::AlignHCenter | Qt::AlignTop | Qt::TextWordWrap, opt.palette, true, descriptionText, foregroundRole());
}
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);
}
void QtGridSelectionDialog::hideEvent(QHideEvent*) {
setMouseTracking(false);
}
void QtGridSelectionDialog::mouseMoveEvent(QMouseEvent*) {
QPoint diff = (frameGeometry().bottomRight() - QCursor::pos());
QSize newDimensions = currentGridSize;
if (diff.x() > frameSize.width() * 1.5) {
newDimensions -= QSize(diff.x() / (frameSize.width() * 1.5), 0);
}
if (diff.y() > frameSize.height() * 1.5) {
newDimensions -= QSize(0, diff.y() / (frameSize.height() * 1.5));
}
if (minGridSize.expandedTo(newDimensions).boundedTo(maxGridSize) != currentGridSize) {
currentGridSize = minGridSize.expandedTo(newDimensions).boundedTo(maxGridSize);
resize(sizeHint());
}
}
void QtGridSelectionDialog::leaveEvent(QEvent *) {
QPoint diff = (frameGeometry().bottomRight() - QCursor::pos());
QSize newGridSize = currentGridSize;
if (diff.x() < 0) {
newGridSize += QSize(1,0);
}
if (diff.y() < 0) {
newGridSize += QSize(0,1);
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
@@ -2,59 +2,61 @@
* Copyright (c) 2014-2016 Isode Limited.
* All rights reserved.
* See the COPYING file for more information.
*/
#pragma once
#include <QSize>
#include <QWidget>
namespace Swift {
class QtGridSelectionDialog : public QWidget {
Q_OBJECT
Q_PROPERTY(QSize currentGridSize READ getCurrentGridSize WRITE setCurrentGridSize NOTIFY currentGridSizeChanged)
Q_PROPERTY(QSize minGridSize READ getMinGridSize WRITE setMinGridSize NOTIFY minGridSizeChanged)
Q_PROPERTY(QSize maxGridSize READ getMaxGridSize WRITE setMaxGridSize NOTIFY maxGridSizeChanged)
public:
explicit QtGridSelectionDialog(QWidget* parent = nullptr);
virtual QSize sizeHint() const;
void setCurrentGridSize(const QSize& size);
QSize getCurrentGridSize() const;
void setMinGridSize(const QSize& size);
QSize getMinGridSize() const;
void setMaxGridSize(const QSize& size);
QSize getMaxGridSize() const;
+ QSize getFrameSize() const;
+
signals:
void currentGridSizeChanged(QSize);
void minGridSizeChanged(QSize);
void maxGridSizeChanged(QSize);
protected:
void keyReleaseEvent(QKeyEvent* event);
void mousePressEvent(QMouseEvent* event);
void mouseMoveEvent(QMouseEvent* event);
void paintEvent(QPaintEvent* event);
void showEvent(QShowEvent* event);
void hideEvent(QHideEvent* event);
void leaveEvent(QEvent *event);
bool event(QEvent* event);
private:
int padding;
int horizontalMargin;
int verticalMargin;
QSize frameSize;
QSize currentGridSize;
QSize minGridSize;
QSize maxGridSize;
const QString descriptionText;
};
}