Skip to content

Commit

Permalink
Scale and center QR code on window resizing
Browse files Browse the repository at this point in the history
* Also add GUI test for QR code resizing
  • Loading branch information
dmaslenko authored and droidmonkey committed Feb 2, 2023
1 parent 3243243 commit f703736
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
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())
, 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

0 comments on commit f703736

Please sign in to comment.