summaryrefslogtreecommitdiffstats
path: root/examples/widgets/widgets/icons/iconpreviewarea.cpp
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@theqtcompany.com>2015-09-01 09:56:28 +0200
committerFriedemann Kleint <Friedemann.Kleint@theqtcompany.com>2015-09-14 19:20:06 +0000
commit8132cb655ad9f1479110d20c4f31f32a9da094d9 (patch)
treeff20cd03c881fc9180c08f366d555ac233a52294 /examples/widgets/widgets/icons/iconpreviewarea.cpp
parentf3fd7b3d961ffed480e5851e594f6981f558f486 (diff)
Polish the Icons example.
- Remove unneeded member variables. - Use new connection syntax in createActions() to assemble the menu there, removing the createMenus() function. - Use a QButtonGroup for the sizes with the style metrics as id. - Streamline code, rename variables for clarity. - Introduce static functions returning the list of states/modes and their names in display order to IconPreviewArea, removing the duplicates. - Split actions to open sample icons provided from source directory or standard pictures location to make sample icons more prominently visible. - Check and display @2x images and add tooltips showing device pixel ratio and actual size. - HighDPI: Add groupbox displaying screen name and device pixel ratio, connect to screen changed signal and update icons panel accordingly. Add check box for toggling Qt::AA_UseHighDpiPixmaps. - Adjust documentation Task-number: QTBUG-18680 Task-number: QTBUG-46615 Change-Id: Ice96e9898f168ef2a30e9f46cb260ed57ae015f0 Reviewed-by: Topi Reiniƶ <topi.reinio@digia.com>
Diffstat (limited to 'examples/widgets/widgets/icons/iconpreviewarea.cpp')
-rw-r--r--examples/widgets/widgets/icons/iconpreviewarea.cpp134
1 files changed, 100 insertions, 34 deletions
diff --git a/examples/widgets/widgets/icons/iconpreviewarea.cpp b/examples/widgets/widgets/icons/iconpreviewarea.cpp
index 0736261a02..090615e64d 100644
--- a/examples/widgets/widgets/icons/iconpreviewarea.cpp
+++ b/examples/widgets/widgets/icons/iconpreviewarea.cpp
@@ -46,33 +46,95 @@
IconPreviewArea::IconPreviewArea(QWidget *parent)
: QWidget(parent)
{
- QGridLayout *mainLayout = new QGridLayout;
- setLayout(mainLayout);
+ QGridLayout *mainLayout = new QGridLayout(this);
- stateLabels[0] = createHeaderLabel(tr("Off"));
- stateLabels[1] = createHeaderLabel(tr("On"));
+ for (int row = 0; row < NumStates; ++row) {
+ stateLabels[row] = createHeaderLabel(IconPreviewArea::iconStateNames().at(row));
+ mainLayout->addWidget(stateLabels[row], row + 1, 0);
+ }
Q_ASSERT(NumStates == 2);
- modeLabels[0] = createHeaderLabel(tr("Normal"));
- modeLabels[1] = createHeaderLabel(tr("Active"));
- modeLabels[2] = createHeaderLabel(tr("Disabled"));
- modeLabels[3] = createHeaderLabel(tr("Selected"));
+ for (int column = 0; column < NumModes; ++column) {
+ modeLabels[column] = createHeaderLabel(IconPreviewArea::iconModeNames().at(column));
+ mainLayout->addWidget(modeLabels[column], 0, column + 1);
+ }
Q_ASSERT(NumModes == 4);
- for (int j = 0; j < NumStates; ++j)
- mainLayout->addWidget(stateLabels[j], j + 1, 0);
-
- for (int i = 0; i < NumModes; ++i) {
- mainLayout->addWidget(modeLabels[i], 0, i + 1);
-
- for (int j = 0; j < NumStates; ++j) {
- pixmapLabels[i][j] = createPixmapLabel();
- mainLayout->addWidget(pixmapLabels[i][j], j + 1, i + 1);
+ for (int column = 0; column < NumModes; ++column) {
+ for (int row = 0; row < NumStates; ++row) {
+ pixmapLabels[column][row] = createPixmapLabel();
+ mainLayout->addWidget(pixmapLabels[column][row], row + 1, column + 1);
}
}
}
//! [0]
+#ifdef Q_COMPILER_INITIALIZER_LISTS
+
+//! [42]
+QVector<QIcon::Mode> IconPreviewArea::iconModes()
+{
+ static const QVector<QIcon::Mode> result = {QIcon::Normal, QIcon::Active, QIcon::Disabled, QIcon::Selected};
+ return result;
+}
+
+QVector<QIcon::State> IconPreviewArea::iconStates()
+{
+ static const QVector<QIcon::State> result = {QIcon::Off, QIcon::On};
+ return result;
+}
+
+QStringList IconPreviewArea::iconModeNames()
+{
+ static const QStringList result = {tr("Normal"), tr("Active"), tr("Disabled"), tr("Selected")};
+ return result;
+}
+
+QStringList IconPreviewArea::iconStateNames()
+{
+ static const QStringList result = {tr("Off"), tr("On")};
+ return result;
+}
+//! [42]
+
+#else // Q_COMPILER_INITIALIZER_LISTS
+
+//! [43]
+QVector<QIcon::Mode> IconPreviewArea::iconModes()
+{
+ static QVector<QIcon::Mode> result;
+ if (result.isEmpty())
+ result << QIcon::Normal << QIcon::Active << QIcon::Disabled << QIcon::Selected;
+ return result;
+}
+//! [43]
+
+QVector<QIcon::State> IconPreviewArea::iconStates()
+{
+ static QVector<QIcon::State> result;
+ if (result.isEmpty())
+ result << QIcon::Off << QIcon::On;
+ return result;
+}
+
+QStringList IconPreviewArea::iconModeNames()
+{
+ static QStringList result;
+ if (result.isEmpty())
+ result << tr("Normal") << tr("Active") << tr("Disabled") << tr("Selected");
+ return result;
+}
+
+QStringList IconPreviewArea::iconStateNames()
+{
+ static QStringList result;
+ if (result.isEmpty())
+ result << tr("Off") << tr("On");
+ return result;
+}
+
+#endif // !Q_COMPILER_INITIALIZER_LISTS
+
//! [1]
void IconPreviewArea::setIcon(const QIcon &icon)
{
@@ -118,23 +180,27 @@ QLabel *IconPreviewArea::createPixmapLabel()
//! [5]
void IconPreviewArea::updatePixmapLabels()
{
- for (int i = 0; i < NumModes; ++i) {
- QIcon::Mode mode;
- if (i == 0) {
- mode = QIcon::Normal;
- } else if (i == 1) {
- mode = QIcon::Active;
- } else if (i == 2) {
- mode = QIcon::Disabled;
- } else {
- mode = QIcon::Selected;
- }
-
- for (int j = 0; j < NumStates; ++j) {
- QIcon::State state = (j == 0) ? QIcon::Off : QIcon::On;
- QPixmap pixmap = icon.pixmap(size, mode, state);
- pixmapLabels[i][j]->setPixmap(pixmap);
- pixmapLabels[i][j]->setEnabled(!pixmap.isNull());
+ QWindow *window = Q_NULLPTR;
+ if (const QWidget *nativeParent = nativeParentWidget())
+ window = nativeParent->windowHandle();
+ for (int column = 0; column < NumModes; ++column) {
+ for (int row = 0; row < NumStates; ++row) {
+ const QPixmap pixmap =
+ icon.pixmap(window, size, IconPreviewArea::iconModes().at(column),
+ IconPreviewArea::iconStates().at(row));
+ QLabel *pixmapLabel = pixmapLabels[column][row];
+ pixmapLabel->setPixmap(pixmap);
+ pixmapLabel->setEnabled(!pixmap.isNull());
+ QString toolTip;
+ if (!pixmap.isNull()) {
+ const QSize actualSize = icon.actualSize(size);
+ toolTip =
+ tr("Size: %1x%2\nActual size: %3x%4\nDevice pixel ratio: %5")
+ .arg(size.width()).arg(size.height())
+ .arg(actualSize.width()).arg(actualSize.height())
+ .arg(pixmap.devicePixelRatioF());
+ }
+ pixmapLabel->setToolTip(toolTip);
}
}
}