diff options
author | Tobias Markmann <tm@ayena.de> | 2015-05-31 09:25:48 (GMT) |
---|---|---|
committer | Tobias Markmann <tm@ayena.de> | 2015-05-31 18:10:20 (GMT) |
commit | 5b851c72510ef88feb059455033915588fa6fb8c (patch) | |
tree | 8f02dba6cefa1d564f4ea29c74a2504e4c7a181a | |
parent | c62f1fc1006474809597a75ed920795da38e4113 (diff) | |
download | swift-5b851c72510ef88feb059455033915588fa6fb8c.zip swift-5b851c72510ef88feb059455033915588fa6fb8c.tar.bz2 |
Open profile windows in their ideal size so everything is readable
Test-Information:
Tested on OS X 10.9.5 with Qt 5.4.1 and Windows 8 with Qt 5.3.2 and
KUbuntu 14.04 with Qt 5.4.1.
Change-Id: Ia6cf54baad3020d05be94c6159aa623f7a619816
-rw-r--r-- | Swift/QtUI/QtProfileWindow.cpp | 55 | ||||
-rw-r--r-- | Swift/QtUI/QtProfileWindow.h | 11 |
2 files changed, 41 insertions, 25 deletions
diff --git a/Swift/QtUI/QtProfileWindow.cpp b/Swift/QtUI/QtProfileWindow.cpp index 31a530f..71d4281 100644 --- a/Swift/QtUI/QtProfileWindow.cpp +++ b/Swift/QtUI/QtProfileWindow.cpp @@ -1,25 +1,26 @@ /* - * Copyright (c) 2011-2014 Isode Limited. + * Copyright (c) 2011-2015 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ /* * Copyright (c) 2012 Tobias Markmann * Licensed under the simplified BSD license. * See Documentation/Licenses/BSD-simplified.txt for more information. */ -#include "QtProfileWindow.h" -#include "ui_QtProfileWindow.h" +#include <Swift/QtUI/QtProfileWindow.h> +#include <Swift/QtUI/ui_QtProfileWindow.h> #include <QCloseEvent> #include <QMovie> #include <QShortcut> #include <QTextDocument> +#include <QTimer> #include <Swift/QtUI/QtSwiftUtil.h> #include <Swift/QtUI/QtUtilities.h> namespace Swift { @@ -36,12 +37,16 @@ QtProfileWindow::QtProfileWindow() : new QShortcut(QKeySequence::Close, this, SLOT(close())); ui->throbberLabel->setMovie(new QMovie(":/icons/throbber.gif", QByteArray(), this)); connect(ui->savePushButton, SIGNAL(clicked()), SLOT(handleSave())); setEditable(false); setAttribute(Qt::WA_DeleteOnClose); + setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); + + adjustSizeTimer.setSingleShot(true); + connect(&adjustSizeTimer, SIGNAL(timeout()), SLOT(handleAdjustSizeTimeout())); } QtProfileWindow::~QtProfileWindow() { delete ui; } @@ -56,13 +61,12 @@ void QtProfileWindow::setVCard(VCard::ref vcard) { ui->vcard->setVisible(false); ui->emptyLabel->setVisible(true); } else { ui->vcard->setVisible(true); ui->emptyLabel->setVisible(false); } - updateWindowSize(); } void QtProfileWindow::setEnabled(bool b) { ui->vcard->setEnabled(b); ui->savePushButton->setEnabled(b); @@ -71,12 +75,13 @@ void QtProfileWindow::setEnabled(bool b) { void QtProfileWindow::setEditable(bool b) { ui->throbberLabel->setVisible(b); ui->errorLabel->setVisible(b); ui->savePushButton->setVisible(b); ui->vcard->setEditable(b); updateTitle(); + updateWindowSize(); } void QtProfileWindow::setProcessing(bool processing) { if (processing) { ui->throbberLabel->movie()->start(); ui->throbberLabel->show(); @@ -86,14 +91,12 @@ void QtProfileWindow::setProcessing(bool processing) { else { ui->throbberLabel->hide(); ui->throbberLabel->movie()->stop(); ui->statusLabel->setVisible(false); ui->vcard->setVisible(true); } - - updateWindowSize(); } void QtProfileWindow::setError(const std::string& error) { if (!error.empty()) { ui->errorLabel->setText("<font color='red'>" + QtUtilities::htmlEscape(P2QSTRING(error)) + "</font>"); } @@ -109,12 +112,16 @@ void QtProfileWindow::show() { } void QtProfileWindow::hide() { QWidget::hide(); } +QSize QtProfileWindow::sizeHint() const { + return QWidget::sizeHint() + QSize(0, 15); +} + void QtProfileWindow::updateTitle() { QString jidString; if (jid.isValid()) { jidString = QString(" ( %1 )").arg(P2QSTRING(jid.toString())); } @@ -123,34 +130,38 @@ void QtProfileWindow::updateTitle() { } else { setWindowTitle(tr("Show Profile") + jidString); } } void QtProfileWindow::updateWindowSize() { - int width = 0; - int height = 0; - - QSize size = ui->statusLabel->size(); - width = std::max(width, size.width()); - height = std::max(height, size.height() * 3); - - size = ui->emptyLabel->size(); - width = std::max(width, size.width()); - height = std::max(height, size.height() * 3); - - size = ui->vcard->size(); - width = std::max(width, size.width()); - height = std::max(height, size.height()); - - resize(width, height); + // Delay resizing to the end of the event loop, because Qt calculates the correct layout asynchronously. + // Qt will post LayoutRequests for widgets on the event loop on show and widgets will recaluclate their + // layout as they process these events. + // We use the complete and correct size hint from the freshly calculated layout by delaying execution of + // the resize code to the end of Qt's event loop. + if (!adjustSizeTimer.isActive()) { + adjustSizeTimer.start(0); + } } void QtProfileWindow::closeEvent(QCloseEvent* event) { event->accept(); onWindowAboutToBeClosed(jid); } void QtProfileWindow::handleSave() { onVCardChangeRequest(ui->vcard->getVCard()); } +void QtProfileWindow::handleAdjustSizeTimeout() { + // Force recaluclation of all layout geometry in children widgets. + // This is required on Windows to have the correct size even on first show. + QList<QWidget *> children = findChildren<QWidget*>(); + foreach(QWidget* child, children) { + child->updateGeometry(); + } + + updateGeometry(); + adjustSize(); +} + } diff --git a/Swift/QtUI/QtProfileWindow.h b/Swift/QtUI/QtProfileWindow.h index d1eed99..0821444 100644 --- a/Swift/QtUI/QtProfileWindow.h +++ b/Swift/QtUI/QtProfileWindow.h @@ -1,26 +1,27 @@ /* - * Copyright (c) 2011 Isode Limited. + * Copyright (c) 2011-2015 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ /* * Copyright (c) 2012 Tobias Markmann * Licensed under the simplified BSD license. * See Documentation/Licenses/BSD-simplified.txt for more information. */ #pragma once +#include <QTimer> +#include <QWidget> + #include <Swiften/JID/JID.h> #include <Swift/Controllers/UIInterfaces/ProfileWindow.h> -#include <QWidget> - namespace Ui { class QtProfileWindow; } namespace Swift { @@ -39,20 +40,24 @@ class QtProfileWindow : public QWidget, public ProfileWindow { virtual void setError(const std::string& error); virtual void setEditable(bool b); virtual void show(); virtual void hide(); + virtual QSize sizeHint() const; + private: void updateTitle(); void updateWindowSize(); virtual void closeEvent(QCloseEvent* event); private slots: void handleSave(); + void handleAdjustSizeTimeout(); private: Ui::QtProfileWindow* ui; JID jid; + QTimer adjustSizeTimer; }; } |