Skip to content

Commit

Permalink
WIP dialog orientation fix
Browse files Browse the repository at this point in the history
  • Loading branch information
martinburchell committed May 17, 2024
1 parent 3fa05bf commit f5c24ac
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 16 deletions.
107 changes: 92 additions & 15 deletions tablet_qt/dialogs/passwordchangedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@

#include "passwordchangedialog.h"
#include <QDialogButtonBox>
#include <QEvent>
#include <QLabel>
#include <QLineEdit>
#include <QScreen>
#include <QTimer>

#include <QVBoxLayout>
#include "lib/filefunc.h"
Expand Down Expand Up @@ -99,6 +101,8 @@ PasswordChangeDialog::PasswordChangeDialog(const QString& text,
this, &PasswordChangeDialog::orientationChanged);

setLayout(mainlayout);
centre();
installEventFilter(this);
}

void PasswordChangeDialog::orientationChanged(Qt::ScreenOrientation orientation)
Expand Down Expand Up @@ -128,32 +132,105 @@ void PasswordChangeDialog::orientationChanged(Qt::ScreenOrientation orientation)
break;
}

QScreen *screen = uifunc::screen();
QRect screen_rect = screen->geometry();
//const int screen_width = uifunc::screenWidth();
//const int screen_height = uifunc::screenHeight();
//const int old_height = height();
//const int old_width = width();

QString label = QString("Orientation:%1 Screen:%2x%3 Dialog:%4x%5 Pos:%6,%7").
arg(description).arg(screen_rect.width()).arg(screen_rect.height()).arg(width()).arg(height()).arg(pos().x()).arg(pos().y());
qInfo() << Q_FUNC_INFO;
qInfo() << label;

qInfo() << "Hide";
hide();
qInfo() << "Resize";
//resize(height(), width());
qInfo() << "Show";
show();
qInfo() << QString("Orientation:%1").arg(description);

reportSize();
//qInfo() << "Hide";
//hide();

//const int x = (screen_width - old_height) / 2;
//const int y = (screen_height - old_width) / 2;
//qInfo() << QString("Moving to: %1, %2").arg(x).arg(y);
//move(y, x);
//qInfo() << QString("Resizing to: %1, %2").arg(old_height).arg(old_width);
//resize(old_height, old_width);
QTimer::singleShot(200, this, &PasswordChangeDialog::centre);
//qInfo() << "Show";
//show();

}


void PasswordChangeDialog::centre()
{
sizeToScreen();

const int screen_width = uifunc::screenWidth();
const int screen_height = uifunc::screenHeight();
const int old_height = height();
const int old_width = width();
const int x = (screen_width - old_height) / 2;
const int y = (screen_height - old_width) / 2;
qInfo() << QString("Moving to: %1, %2").arg(x).arg(y);
move(x, y);
}


void PasswordChangeDialog::sizeToScreen()
{
const int screen_width = uifunc::screenWidth();
const int screen_height = uifunc::screenHeight();

bool changed = false;

int new_width = width();
int new_height = height();

if (new_width > screen_width)
{
new_width = screen_width;
changed = true;
}
if (new_height > screen_height)
{
new_height = screen_height;
changed = true;
}
if (changed) {
resize(new_width, new_height);
}
}


void PasswordChangeDialog::reportSize()
{
const int screen_width = uifunc::screenWidth();
const int screen_height = uifunc::screenHeight();
const int old_height = height();
const int old_width = width();
QPoint old_pos = pos();

qInfo() << Q_FUNC_INFO;
qInfo() << QString("Screen:%2x%3 Dialog:%4x%5 Pos:%6,%7").
arg(screen_width).arg(screen_height).arg(old_width).arg(old_height).arg(old_pos.x()).arg(old_pos.y());

}


void PasswordChangeDialog::resizeEvent(QResizeEvent* event)
{
Q_UNUSED(event)

qInfo()<< Q_FUNC_INFO;

qInfo()
<< "dialog geometry = " << geometry()
<< "screen geometry = " << uifunc::screenGeometry();
reportSize();
}


bool PasswordChangeDialog::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::Show)
{
centre();
}

return QObject::eventFilter(obj, event);
}


Expand Down
6 changes: 5 additions & 1 deletion tablet_qt/dialogs/passwordchangedialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@ class PasswordChangeDialog : public QDialog
QWidget* parent = nullptr);
QString oldPassword() const;
QString newPassword() const;
void resizeEvent(QResizeEvent* event);
void resizeEvent(QResizeEvent* event) override;
void reportSize();
void sizeToScreen();
void centre();
public slots:
void orientationChanged(Qt::ScreenOrientation orientation);
protected:
void okClicked();
bool eventFilter(QObject *obj, QEvent *event) override;
protected:
QPointer<QLineEdit> m_editor_old;
QPointer<QLineEdit> m_editor_new1;
Expand Down

0 comments on commit f5c24ac

Please sign in to comment.