Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong horizontal QR code resizing #9027

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/gui/SquareSvgWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
*/

#include "SquareSvgWidget.h"
#include <QResizeEvent>

SquareSvgWidget::SquareSvgWidget(QWidget* parent)
: QSvgWidget(parent)
{
Q_ASSERT(parent);
setObjectName("squareSvgWidget");
}

bool SquareSvgWidget::hasHeightForWidth() const
{
Expand All @@ -26,3 +34,24 @@ int SquareSvgWidget::heightForWidth(int width) const
{
return width;
}

// The overridden logic allows to keep the SVG image as square and centered by width and height.
void SquareSvgWidget::resizeEvent(QResizeEvent*)
{
QWidget* pWidget = parentWidget();
Q_ASSERT(pWidget);
if (pWidget) {
auto containerRect = pWidget->contentsRect();

auto containerWidth = containerRect.width();
auto containerHeight = containerRect.height();

auto squareSize = qMin(containerWidth, containerHeight);
auto halfSquareSize = squareSize >> 1;

auto startX = (containerWidth >> 1) - halfSquareSize;
auto startY = (containerHeight >> 1) - halfSquareSize;

setGeometry(startX, startY, squareSize, squareSize);
}
}
4 changes: 3 additions & 1 deletion src/gui/SquareSvgWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
class SquareSvgWidget : public QSvgWidget
{
public:
SquareSvgWidget() = default;
explicit SquareSvgWidget(QWidget* parent);
~SquareSvgWidget() override = default;

bool hasHeightForWidth() const override;
int heightForWidth(int width) const override;

void resizeEvent(QResizeEvent* event) override;
};

#endif // KEEPASSX_SquareSvgWidget_H
14 changes: 9 additions & 5 deletions src/gui/TotpExportSettingsDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,29 @@
#include <QMessageBox>
#include <QPushButton>
#include <QShortcut>
#include <QStackedWidget>

TotpExportSettingsDialog::TotpExportSettingsDialog(DatabaseWidget* parent, Entry* entry)
: QDialog(parent)
, m_timer(new QTimer(this))
, m_verticalLayout(new QVBoxLayout())
, m_totpSvgWidget(new SquareSvgWidget())
, m_totpSvgContainerWidget(new QStackedWidget())
droidmonkey marked this conversation as resolved.
Show resolved Hide resolved
, m_totpSvgWidget(new SquareSvgWidget(m_totpSvgContainerWidget))
, m_countDown(new QLabel())
, m_warningLabel(new QLabel())
, m_buttonBox(new QDialogButtonBox(QDialogButtonBox::Close | QDialogButtonBox::Ok))
{
setObjectName("entryQrCodeWidget");
m_totpSvgContainerWidget->addWidget(m_totpSvgWidget);

m_verticalLayout->addWidget(m_warningLabel);
m_verticalLayout->addItem(new QSpacerItem(0, 0));

m_verticalLayout->addStretch(0);
m_verticalLayout->addWidget(m_totpSvgWidget);
m_verticalLayout->addStretch(0);
m_verticalLayout->addWidget(m_totpSvgContainerWidget);
m_verticalLayout->addWidget(m_countDown);
m_verticalLayout->addWidget(m_buttonBox);

m_verticalLayout->setAlignment(m_buttonBox, Qt::AlignBottom);

setLayout(m_verticalLayout);
setAttribute(Qt::WA_DeleteOnClose);

Expand Down
1 change: 1 addition & 0 deletions src/gui/TotpExportSettingsDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private slots:
QTimer* m_timer;

QVBoxLayout* m_verticalLayout;
QStackedWidget* m_totpSvgContainerWidget;
SquareSvgWidget* m_totpSvgWidget;
QLabel* m_countDown;
QLabel* m_warningLabel;
Expand Down
15 changes: 15 additions & 0 deletions tests/gui/TestGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -895,12 +895,27 @@ void TestGui::testTotp()
auto* editEntryWidgetButtonBox = editEntryWidget->findChild<QDialogButtonBox*>("buttonBox");
QTest::mouseClick(editEntryWidgetButtonBox->button(QDialogButtonBox::Ok), Qt::LeftButton);

// Test the TOTP value
triggerAction("actionEntryTotp");

auto* totpDialog = m_dbWidget->findChild<TotpDialog*>("TotpDialog");
auto* totpLabel = totpDialog->findChild<QLabel*>("totpLabel");

QCOMPARE(totpLabel->text().replace(" ", ""), entry->totp());
QTest::keyClick(totpDialog, Qt::Key_Escape);

// Test the QR code
triggerAction("actionEntryTotpQRCode");
auto* qrCodeDialog = m_mainWindow->findChild<QDialog*>("entryQrCodeWidget");
QVERIFY(qrCodeDialog);
QVERIFY(qrCodeDialog->isVisible());
auto* qrCodeWidget = qrCodeDialog->findChild<QWidget*>("squareSvgWidget");
QVERIFY2(qrCodeWidget->geometry().width() == qrCodeWidget->geometry().height(), "Initial QR code is not square");

// Test the QR code window resizing, make the dialog bigger.
qrCodeDialog->setFixedSize(800, 600);
QVERIFY2(qrCodeWidget->geometry().width() == qrCodeWidget->geometry().height(), "Resized QR code is not square");
QTest::keyClick(qrCodeDialog, Qt::Key_Escape);
}

void TestGui::testSearch()
Expand Down