Polish the manual High DPI test

- Use C++ constructs like range-based for, member initialization
- Fix formatting in a few places
- Silence clang warnings:
  - Add override
  - Make member variables private

Task-number: QTBUG-80323
Change-Id: I5b0fda06acb6c8054aafa4dd934b763f8493a6b3
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
This commit is contained in:
Friedemann Kleint 2019-11-27 09:40:08 +01:00
parent ee3e0ed751
commit c94e756f01
3 changed files with 145 additions and 114 deletions

View File

@ -42,7 +42,7 @@ public:
};
DragWidget::DragWidget(QString text, QWidget *parent)
: QWidget(parent), otherWindow(nullptr)
: QWidget(parent)
{
int x = 5;
int y = 5;
@ -52,9 +52,9 @@ DragWidget::DragWidget(QString text, QWidget *parent)
text = "You can drag from this window and drop text here";
QStringList words = text.split(' ');
foreach (QString word, words) {
for (const QString &word : words) {
if (!word.isEmpty()) {
FramedLabel *wordLabel = new FramedLabel(word, this);
auto wordLabel = new FramedLabel(word, this);
wordLabel->move(x, y);
wordLabel->show();
x += wordLabel->width() + 2;
@ -105,7 +105,6 @@ void DragWidget::dragLeaveEvent(QDragLeaveEvent *)
update();
}
void DragWidget::dropEvent(QDropEvent *event)
{
if (event->mimeData()->hasText()) {
@ -141,9 +140,9 @@ void DragWidget::dropEvent(QDropEvent *event)
} else {
event->ignore();
}
foreach (QObject *child, children()) {
if (child->inherits("QWidget")) {
QWidget *widget = static_cast<QWidget *>(child);
for (QObject *child : children()) {
if (child->isWidgetType()) {
auto widget = static_cast<QWidget *>(child);
if (!widget->isVisible())
widget->deleteLater();
}
@ -170,7 +169,7 @@ void DragWidget::mousePressEvent(QMouseEvent *event)
pixmap.setDevicePixelRatio(dpr);
child->render(&pixmap);
QDrag *drag = new QDrag(this);
auto drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setPixmap(pixmap);
drag->setHotSpot(hotSpot);

View File

@ -40,7 +40,7 @@ QT_END_NAMESPACE
class DragWidget : public QWidget
{
public:
DragWidget(QString text = QString(), QWidget *parent = 0);
DragWidget(QString text = QString(), QWidget *parent = nullptr);
protected:
void dragEnterEvent(QDragEnterEvent *event) override;
@ -52,12 +52,13 @@ protected:
void timerEvent(QTimerEvent *event) override;
void showEvent(QShowEvent *event) override;
void hideEvent(QHideEvent *event) override;
private:
QPoint dragPos;
QPoint dropPos;
QBasicTimer dragTimer;
QBasicTimer dropTimer;
QWidget *otherWindow;
QWidget *otherWindow = nullptr;
};
#endif // DRAGWIDGET_H

View File

@ -61,6 +61,8 @@
#include "dragwidget.h"
#include <utility>
static QTextStream &operator<<(QTextStream &str, const QRect &r)
{
str << r.width() << 'x' << r.height() << forcesign << r.x() << r.y() << noforcesign;
@ -70,18 +72,18 @@ static QTextStream &operator<<(QTextStream &str, const QRect &r)
class DemoContainerBase
{
public:
DemoContainerBase() : m_widget(nullptr) {}
virtual ~DemoContainerBase() {}
QString name() { return option().names().first(); }
DemoContainerBase() = default;
virtual ~DemoContainerBase() = default;
QString name() { return option().names().constFirst(); }
virtual QCommandLineOption &option() = 0;
virtual void makeVisible(bool visible, QWidget *parent) = 0;
QWidget *widget() { return m_widget; }
QWidget *widget() const { return m_widget; }
protected:
QWidget *m_widget;
QWidget *m_widget = nullptr;
};
typedef QList<DemoContainerBase*> DemoContainerList ;
using DemoContainerList = QVector<DemoContainerBase*>;
template <class T>
class DemoContainer : public DemoContainerBase
@ -93,9 +95,10 @@ public:
}
~DemoContainer() { delete m_widget; }
QCommandLineOption &option() { return m_option; }
QCommandLineOption &option() override { return m_option; }
void makeVisible(bool visible, QWidget *parent) {
void makeVisible(bool visible, QWidget *parent) override
{
if (visible && !m_widget) {
m_widget = new T;
m_widget->installEventFilter(parent);
@ -103,6 +106,7 @@ public:
if (m_widget)
m_widget->setVisible(visible);
}
private:
QCommandLineOption m_option;
};
@ -134,12 +138,15 @@ public:
connect(m_slider, &QSlider::sliderMoved, this, &LabelSlider::updateLabel);
connect(m_slider, &QSlider::valueChanged, this, &LabelSlider::valueChanged);
}
void setValue(int scaleFactor) {
void setValue(int scaleFactor)
{
m_slider->setValue(scaleFactor);
updateLabel(scaleFactor);
}
private slots:
void updateLabel(int scaleFactor) {
void updateLabel(int scaleFactor)
{
// slider value is scale factor times ten;
qreal scalefactorF = qreal(scaleFactor) / 10.0;
@ -149,8 +156,10 @@ private slots:
number.append(".0");
m_label->setText(number);
}
signals:
void valueChanged(int scaleFactor);
private:
QSlider *m_slider;
QLabel *m_label;
@ -172,28 +181,30 @@ static inline qreal getGlobalScaleFactor()
class DemoController : public QWidget
{
Q_OBJECT
Q_OBJECT
public:
DemoController(DemoContainerList *demos, QCommandLineParser *parser);
DemoController(DemoContainerList demos, QCommandLineParser *parser);
~DemoController();
protected:
bool eventFilter(QObject *object, QEvent *event);
void closeEvent(QCloseEvent *) { qApp->quit(); }
bool eventFilter(QObject *object, QEvent *event) override;
void closeEvent(QCloseEvent *) override { QCoreApplication::quit(); }
private slots:
void handleButton(int id, bool toggled);
private:
DemoContainerList *m_demos;
DemoContainerList m_demos;
QButtonGroup *m_group;
};
DemoController::DemoController(DemoContainerList *demos, QCommandLineParser *parser)
: m_demos(demos)
DemoController::DemoController(DemoContainerList demos, QCommandLineParser *parser)
: m_demos(std::move(demos))
{
setWindowTitle("screen scale factors");
setObjectName("controller"); // make WindowScaleFactorSetter skip this window
QGridLayout *layout = new QGridLayout;
setLayout(layout);
auto layout = new QGridLayout(this);
int layoutRow = 0;
LabelSlider *globalScaleSlider = new LabelSlider(this, "Global scale factor", layout, layoutRow++);
@ -205,8 +216,8 @@ DemoController::DemoController(DemoContainerList *demos, QCommandLineParser *par
});
// set up one scale control line per screen
QList<QScreen *> screens = QGuiApplication::screens();
foreach (QScreen *screen, screens) {
const auto screens = QGuiApplication::screens();
for (QScreen *screen : screens) {
// create scale control line
QSize screenSize = screen->geometry().size();
QString screenId = screen->name() + QLatin1Char(' ') + QString::number(screenSize.width())
@ -231,8 +242,8 @@ DemoController::DemoController(DemoContainerList *demos, QCommandLineParser *par
m_group = new QButtonGroup(this);
m_group->setExclusive(false);
for (int i = 0; i < m_demos->size(); ++i) {
DemoContainerBase *demo = m_demos->at(i);
for (int i = 0; i < m_demos.size(); ++i) {
DemoContainerBase *demo = m_demos.at(i);
QPushButton *button = new QPushButton(demo->name());
button->setToolTip(demo->option().description());
button->setCheckable(true);
@ -244,19 +255,20 @@ DemoController::DemoController(DemoContainerList *demos, QCommandLineParser *par
button->setChecked(true);
}
}
connect(m_group, SIGNAL(buttonToggled(int, bool)), this, SLOT(handleButton(int, bool)));
connect(m_group, QOverload<int,bool>::of(&QButtonGroup::buttonToggled),
this, &DemoController::handleButton);
}
DemoController::~DemoController()
{
qDeleteAll(*m_demos);
qDeleteAll(m_demos);
}
bool DemoController::eventFilter(QObject *object, QEvent *event)
{
if (event->type() == QEvent::Close) {
for (int i = 0; i < m_demos->size(); ++i) {
DemoContainerBase *demo = m_demos->at(i);
for (int i = 0; i < m_demos.size(); ++i) {
DemoContainerBase *demo = m_demos.at(i);
if (demo->widget() == object) {
m_group->button(i)->setChecked(false);
break;
@ -268,15 +280,17 @@ bool DemoController::eventFilter(QObject *object, QEvent *event)
void DemoController::handleButton(int id, bool toggled)
{
m_demos->at(id)->makeVisible(toggled, this);
m_demos.at(id)->makeVisible(toggled, this);
}
class PixmapPainter : public QWidget
{
public:
PixmapPainter();
void paintEvent(QPaintEvent *event);
void paintEvent(QPaintEvent *event) override;
private:
QPixmap pixmap1X;
QPixmap pixmap2X;
QPixmap pixmapLarge;
@ -348,12 +362,14 @@ void PixmapPainter::paintEvent(QPaintEvent *)
class TiledPixmapPainter : public QWidget
{
public:
TiledPixmapPainter();
void paintEvent(QPaintEvent *event) override;
private:
QPixmap pixmap1X;
QPixmap pixmap2X;
QPixmap pixmapLarge;
TiledPixmapPainter();
void paintEvent(QPaintEvent *event);
};
TiledPixmapPainter::TiledPixmapPainter()
@ -404,6 +420,7 @@ class Labels : public QWidget
public:
Labels();
private:
QPixmap pixmap1X;
QPixmap pixmap2X;
QPixmap pixmapLarge;
@ -454,12 +471,11 @@ private:
QIcon qtIcon2x;
QToolBar *fileToolBar;
int menuCount;
QAction *m_maskAction;
int menuCount = 0;
};
MainWindow::MainWindow()
:menuCount(0)
{
// beware that QIcon auto-loads the @2x versions.
qtIcon1x.addFile(":/qticon16.png");
@ -484,7 +500,6 @@ MainWindow::MainWindow()
addNewMenu("&Help", 2);
}
QMenu *MainWindow::addNewMenu(const QString &title, int itemCount)
{
QMenu *menu = menuBar()->addMenu(title);
@ -516,7 +531,7 @@ void MainWindow::maskActionToggled(bool t)
class StandardIcons : public QWidget
{
public:
void paintEvent(QPaintEvent *)
void paintEvent(QPaintEvent *) override
{
int x = 10;
int y = 10;
@ -538,7 +553,7 @@ public:
class Caching : public QWidget
{
public:
void paintEvent(QPaintEvent *)
void paintEvent(QPaintEvent *) override
{
QSize layoutSize(75, 75);
@ -576,16 +591,12 @@ public:
}
};
class Style : public QWidget {
class Style : public QWidget
{
public:
QPushButton *button;
QLineEdit *lineEdit;
QSlider *slider;
QHBoxLayout *row1;
Style() {
row1 = new QHBoxLayout();
setLayout(row1);
Style()
{
row1 = new QHBoxLayout(this);
button = new QPushButton();
button->setText("Test Button");
@ -601,17 +612,23 @@ public:
row1->addWidget(new QSpinBox);
row1->addWidget(new QScrollBar);
QTabBar *tab = new QTabBar();
auto tab = new QTabBar();
tab->addTab("Foo");
tab->addTab("Bar");
row1->addWidget(tab);
}
private:
QPushButton *button;
QLineEdit *lineEdit;
QSlider *slider;
QHBoxLayout *row1;
};
class Fonts : public QWidget
{
public:
void paintEvent(QPaintEvent *)
void paintEvent(QPaintEvent *) override
{
QPainter painter(this);
@ -690,7 +707,7 @@ public:
iconNormalDpi.reset(new QIcon(path32_2)); // does not have a 2x version.
}
void paintEvent(QPaintEvent *)
void paintEvent(QPaintEvent *) override
{
int x = 10;
int y = 10;
@ -782,7 +799,7 @@ public:
tab->move(10, 100);
tab->show();
QToolBar *toolBar = new QToolBar(this);
auto toolBar = new QToolBar(this);
toolBar->addAction(QIcon(":/qticon16.png"), "16");
toolBar->addAction(QIcon(":/qticon16@2x.png"), "16@2x");
toolBar->addAction(QIcon(":/qticon32.png"), "32");
@ -796,11 +813,12 @@ public:
class LinePainter : public QWidget
{
public:
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
private:
QPoint lastMousePoint;
QVector<QPoint> linePoints;
};
@ -855,17 +873,15 @@ void LinePainter::mouseMoveEvent(QMouseEvent *event)
class CursorTester : public QWidget
{
public:
CursorTester()
:moveLabel(nullptr), moving(false)
{
}
CursorTester() = default;
inline QRect getRect(int idx) const
{
int h = height() / 2;
return QRect(10, 10 + h * (idx - 1), width() - 20, h - 20);
}
void paintEvent(QPaintEvent *)
void paintEvent(QPaintEvent *) override
{
QPainter p(this);
QRect r1 = getRect(1);
@ -899,7 +915,7 @@ public:
}
}
void mousePressEvent(QMouseEvent *e)
void mousePressEvent(QMouseEvent *e) override
{
if (moving)
return;
@ -923,7 +939,7 @@ public:
moveLabel->show();
}
void mouseReleaseEvent(QMouseEvent *)
void mouseReleaseEvent(QMouseEvent *) override
{
if (moveLabel)
moveLabel->hide();
@ -931,7 +947,7 @@ public:
moving = false;
}
void mouseMoveEvent(QMouseEvent *e)
void mouseMoveEvent(QMouseEvent *e) override
{
if (!moving)
return;
@ -943,32 +959,32 @@ public:
}
private:
QLabel *moveLabel;
bool useCursorPos;
bool moving;
QLabel *moveLabel = nullptr;
QPoint mousePos;
bool useCursorPos = false;
bool moving = false;
};
class ScreenDisplayer : public QWidget
{
public:
ScreenDisplayer()
: QWidget(), moveLabel(nullptr), scaleFactor(1.0)
{
}
ScreenDisplayer() = default;
void timerEvent(QTimerEvent *) {
void timerEvent(QTimerEvent *) override
{
update();
}
void mousePressEvent(QMouseEvent *) {
void mousePressEvent(QMouseEvent *) override
{
if (!moveLabel)
moveLabel = new QLabel(this,Qt::BypassWindowManagerHint|Qt::FramelessWindowHint|Qt::Window );
moveLabel->setText("Hello, Qt this is a label\nwith some text");
moveLabel->show();
}
void mouseMoveEvent(QMouseEvent *e) {
void mouseMoveEvent(QMouseEvent *e) override
{
if (!moveLabel)
return;
moveLabel->move(e->pos() / scaleFactor);
@ -978,23 +994,30 @@ public:
dbg << moveLabel->geometry();
moveLabel->setText(str);
}
void mouseReleaseEvent(QMouseEvent *) {
void mouseReleaseEvent(QMouseEvent *) override
{
if (moveLabel)
moveLabel->hide();
}
void showEvent(QShowEvent *) {
void showEvent(QShowEvent *) override
{
refreshTimer.start(300, this);
}
void hideEvent(QHideEvent *) {
void hideEvent(QHideEvent *) override
{
refreshTimer.stop();
}
void paintEvent(QPaintEvent *) {
void paintEvent(QPaintEvent *) override
{
QPainter p(this);
QRectF total;
QList<QScreen*> screens = qApp->screens();
foreach (QScreen *screen, screens) {
const auto screens = QGuiApplication::screens();
for (const QScreen *screen : screens)
total |= screen->geometry();
}
if (total.isEmpty())
return;
@ -1006,8 +1029,7 @@ public:
p.setPen(QPen(Qt::white, 10));
p.setBrush(Qt::gray);
foreach (QScreen *screen, screens) {
for (const QScreen *screen : screens) {
p.drawRect(screen->geometry());
QFont f = font();
f.setPixelSize(screen->geometry().height() / 8);
@ -1015,7 +1037,9 @@ public:
p.drawText(screen->geometry(), Qt::AlignCenter, screen->name());
}
p.setBrush(QColor(200,220,255,127));
foreach (QWidget *widget, QApplication::topLevelWidgets()) {
const auto topLevels = QApplication::topLevelWidgets();
for (QWidget *widget : topLevels) {
if (!widget->isHidden())
p.drawRect(widget->geometry());
}
@ -1028,42 +1052,51 @@ public:
cursorShape.translate(QCursor::pos());
p.drawPolygon(cursorShape);
}
private:
QLabel *moveLabel;
QLabel *moveLabel = nullptr;
qreal scaleFactor = 1;
QBasicTimer refreshTimer;
qreal scaleFactor;
};
class PhysicalSizeTest : public QWidget
{
Q_OBJECT
Q_OBJECT
public:
PhysicalSizeTest() : QWidget(), m_ignoreResize(false) {}
void paintEvent(QPaintEvent *event);
void resizeEvent(QResizeEvent *) {
PhysicalSizeTest() = default;
void paintEvent(QPaintEvent *event) override;
void resizeEvent(QResizeEvent *) override
{
qreal ppi = window()->windowHandle()->screen()->physicalDotsPerInchX();
QSizeF s = size();
if (!m_ignoreResize)
m_physicalSize = s / ppi;
}
bool event(QEvent *event) {
bool event(QEvent *event) override
{
if (event->type() == QEvent::ScreenChangeInternal) {
// we will get resize events when the scale factor changes
m_ignoreResize = true;
QTimer::singleShot(100, this, SLOT(handleScreenChange()));
QTimer::singleShot(100, this, &PhysicalSizeTest::handleScreenChange);
}
return QWidget::event(event);
}
public slots:
void handleScreenChange() {
void handleScreenChange()
{
qreal ppi = window()->windowHandle()->screen()->physicalDotsPerInchX();
QSizeF newSize = m_physicalSize * ppi;
resize(newSize.toSize());
m_ignoreResize = false;
}
private:
QSizeF m_physicalSize;
bool m_ignoreResize;
bool m_ignoreResize = false;
};
void PhysicalSizeTest::paintEvent(QPaintEvent *)
@ -1166,8 +1199,9 @@ void PhysicalSizeTest::paintEvent(QPaintEvent *)
class GraphicsViewCaching : public QGraphicsView
{
public:
GraphicsViewCaching() {
QGraphicsScene *scene = new QGraphicsScene(0, 0, 400, 400);
GraphicsViewCaching()
{
auto scene = new QGraphicsScene(0, 0, 400, 400);
QGraphicsTextItem *item = scene->addText("NoCache");
item->setCacheMode(QGraphicsItem::NoCache);
@ -1205,8 +1239,7 @@ QT_DPI_ADJUSTMENT_POLICY=AdjustDpi|DontAdjustDpi|AdjustUpOnly)";
resize(480, 360);
QVBoxLayout *layout = new QVBoxLayout();
setLayout(layout);
auto layout = new QVBoxLayout(this);
m_textEdit = new QPlainTextEdit;
m_textEdit->setReadOnly(true);
@ -1254,7 +1287,7 @@ QT_DPI_ADJUSTMENT_POLICY=AdjustDpi|DontAdjustDpi|AdjustUpOnly)";
m_textEdit->setPlainText(text);
}
void paintEvent(QPaintEvent *ev)
void paintEvent(QPaintEvent *ev) override
{
// We get a paint event on screen change, so this is a convenient place
// to update the metrics, at the possible risk of doing something else
@ -1270,8 +1303,6 @@ int main(int argc, char **argv)
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
QCoreApplication::setApplicationVersion(QT_VERSION_STR);
int argumentCount = QCoreApplication::arguments().count();
QCommandLineParser parser;
parser.setApplicationDescription("High DPI tester. Pass one or more of the options to\n"
"test various high-dpi aspects. \n"
@ -1302,15 +1333,15 @@ int main(int argc, char **argv)
demoList << new DemoContainer<GraphicsViewCaching>("graphicsview", "Test QGraphicsView caching");
demoList << new DemoContainer<MetricsTest>("metrics", "Show display metrics");
foreach (DemoContainerBase *demo, demoList)
for (DemoContainerBase *demo : qAsConst(demoList))
parser.addOption(demo->option());
parser.process(app);
//controller takes ownership of all demos
DemoController controller(&demoList, &parser);
DemoController controller(demoList, &parser);
if (parser.isSet(controllerOption) || argumentCount <= 1)
if (parser.isSet(controllerOption) || QCoreApplication::arguments().count() <= 1)
controller.show();
if (QApplication::topLevelWidgets().isEmpty())