1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "controllerwindow.h"
#include "controls.h"
#include <QAction>
#include <QApplication>
#include <QCheckBox>
#include <QDebug>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QLibraryInfo>
#include <qlogging.h>
#include <QMainWindow>
#include <QMenu>
#include <QMoveEvent>
#include <QPushButton>
#include <QRadioButton>
#include <QTabWidget>
#include <QWindow>
ControllerWidget::ControllerWidget(QWidget *parent)
: QWidget(parent)
, previewWidget(0)
{
parentWindow = new QMainWindow;
parentWindow->setWindowTitle(tr("Preview parent window"));
QLabel *label = new QLabel(tr("Parent window"));
parentWindow->setCentralWidget(label);
previewWindow = new PreviewWindow;
previewWindow->installEventFilter(this);
previewWidget = new PreviewWidget;
previewWidget->installEventFilter(this);
previewDialog = new PreviewDialog;
previewDialog->installEventFilter(this);
createTypeGroupBox();
hintsControl = new HintControl;
hintsControl->setHints(previewWidget->windowFlags());
connect(hintsControl, SIGNAL(changed(Qt::WindowFlags)), this, SLOT(updatePreview()));
statesControl = new WindowStatesControl;
statesControl->setStates(previewWidget->windowState());
statesControl->setVisibleValue(true);
connect(statesControl, SIGNAL(changed()), this, SLOT(updatePreview()));
typeControl = new TypeControl;
typeControl->setType(previewWidget->windowFlags());
connect(typeControl, SIGNAL(changed(Qt::WindowFlags)), this, SLOT(updatePreview()));
QVBoxLayout *mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(widgetTypeGroupBox);
mainLayout->addWidget(additionalOptionsGroupBox);
mainLayout->addWidget(typeControl);
mainLayout->addWidget(hintsControl);
mainLayout->addWidget(statesControl);
updatePreview();
}
bool ControllerWidget::eventFilter(QObject *, QEvent *e)
{
if (e->type() == QEvent::WindowStateChange)
updateStateControl();
return false;
}
void ControllerWidget::updateStateControl()
{
if (activePreview)
statesControl->setStates(activePreview->windowStates());
}
void ControllerWidget::updatePreview(QWindow *preview)
{
activePreview = preview;
const Qt::WindowFlags flags = typeControl->type() | hintsControl->hints();
if (modalWindowCheckBox->isChecked()) {
parentWindow->show();
preview->setModality(Qt::WindowModal);
preview->setParent(parentWindow->windowHandle());
} else {
preview->setModality(Qt::NonModal);
preview->setParent(0);
parentWindow->hide();
}
preview->setFlags(flags);
if (fixedSizeWindowCheckBox->isChecked()) {
preview->setMinimumSize(preview->size());
preview->setMaximumSize(preview->size());
} else {
preview->setMinimumSize(QSize(0, 0));
preview->setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
}
preview->setWindowStates(statesControl->states());
preview->setVisible(statesControl->visibleValue());
}
void ControllerWidget::updatePreview(QWidget *preview)
{
activePreview = preview->windowHandle();
const Qt::WindowFlags flags = typeControl->type() | hintsControl->hints();
if (modalWindowCheckBox->isChecked()) {
parentWindow->show();
preview->setWindowModality(Qt::WindowModal);
preview->setParent(parentWindow);
} else {
preview->setWindowModality(Qt::NonModal);
preview->setParent(0);
parentWindow->hide();
}
preview->setWindowFlags(flags);
QSize fixedSize = fixedSizeWindowCheckBox->isChecked() ?
preview->size() : QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
preview->setFixedSize(fixedSize);
QPoint pos = preview->pos();
if (pos.x() < 0)
pos.setX(0);
if (pos.y() < 0)
pos.setY(0);
preview->move(pos);
preview->setWindowState(statesControl->states());
preview->setVisible(statesControl->visibleValue());
}
void ControllerWidget::updatePreview()
{
if (previewWindowButton->isChecked()) {
previewDialog->hide();
previewWidget->close();
updatePreview(previewWindow);
} else if (previewWidgetButton->isChecked()) {
previewWindow->hide();
previewDialog->hide();
updatePreview(previewWidget);
} else {
previewWindow->hide();
previewWidget->close();
updatePreview(previewDialog);
}
}
void ControllerWidget::createTypeGroupBox()
{
widgetTypeGroupBox = new QGroupBox(tr("Window Type"));
previewWindowButton = createRadioButton(tr("QWindow"));
previewWidgetButton = createRadioButton(tr("QWidget"));
previewDialogButton = createRadioButton(tr("QDialog"));
previewWindowButton->setChecked(true);
QHBoxLayout *l = new QHBoxLayout;
l->addWidget(previewWindowButton);
l->addWidget(previewWidgetButton);
l->addWidget(previewDialogButton);
widgetTypeGroupBox->setLayout(l);
additionalOptionsGroupBox = new QGroupBox(tr("Additional options"));
l = new QHBoxLayout;
modalWindowCheckBox = createCheckBox(tr("Modal window"));
fixedSizeWindowCheckBox = createCheckBox(tr("Fixed size window"));
l->addWidget(modalWindowCheckBox);
l->addWidget(fixedSizeWindowCheckBox);
additionalOptionsGroupBox->setLayout(l);
}
QCheckBox *ControllerWidget::createCheckBox(const QString &text)
{
QCheckBox *checkBox = new QCheckBox(text);
connect(checkBox, SIGNAL(clicked()), this, SLOT(updatePreview()));
return checkBox;
}
QRadioButton *ControllerWidget::createRadioButton(const QString &text)
{
QRadioButton *button = new QRadioButton(text);
connect(button, SIGNAL(clicked()), this, SLOT(updatePreview()));
return button;
}
static bool isTopLevel(const QObject *o)
{
if (o->isWidgetType())
return static_cast<const QWidget *>(o)->isWindow();
if (o->isWindowType())
return static_cast<const QWindow *>(o)->isTopLevel();
return false;
}
static Qt::WindowStates windowState(const QObject *o)
{
if (o->isWidgetType()) {
Qt::WindowStates states = static_cast<const QWidget *>(o)->windowState();
states &= ~Qt::WindowActive;
return states;
}
if (o->isWindowType())
return static_cast<const QWindow *>(o)->windowStates();
return Qt::WindowNoState;
}
class EventFilter : public QObject {
public:
explicit EventFilter(QObject *parent = nullptr) : QObject(parent) {}
bool eventFilter(QObject *o, QEvent *e)
{
switch (e->type()) {
case QEvent::Move:
case QEvent::Resize:
case QEvent::WindowStateChange:
case QEvent::ApplicationActivate:
case QEvent::ApplicationDeactivate:
case QEvent::ApplicationStateChange:
if (isTopLevel(o))
formatEvent(o, e);
break;
default:
break;
}
return QObject::eventFilter(o ,e);
}
private:
void formatEvent(QObject *o, QEvent *e)
{
static int n = 0;
QDebug debug = qDebug().nospace();
debug.noquote();
debug << '#' << n++ << ' ' << o->metaObject()->className();
const QString name = o->objectName();
if (!name.isEmpty())
debug << "/\"" << name << '"';
debug << ' ' << e;
if (e->type() == QEvent::WindowStateChange)
debug << ' ' << windowState(o);
}
};
LogWidget *LogWidget::m_instance = 0;
static QtMessageHandler originalMessageHandler = nullptr;
static void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &text)
{
if (LogWidget *lw = LogWidget::instance())
lw->appendText(text);
originalMessageHandler(type, context, text);
}
LogWidget::LogWidget(QWidget *parent)
: QPlainTextEdit(parent)
{
LogWidget::m_instance = this;
setReadOnly(true);
appendText(startupMessage());
}
LogWidget::~LogWidget()
{
LogWidget::m_instance = 0;
}
void LogWidget::install()
{
originalMessageHandler = qInstallMessageHandler(messageHandler);
}
QString LogWidget::startupMessage()
{
QString result;
result += QLatin1String(QLibraryInfo::build());
result += QLatin1Char(' ');
result += QGuiApplication::platformName();
return result;
}
void LogWidget::appendText(const QString &message)
{
appendPlainText(message);
ensureCursorVisible();
}
ControllerWindow::ControllerWindow()
{
setWindowTitle(tr("Window Flags (Qt version %1, %2)")
.arg(QLatin1String(qVersion()),
qApp->platformName()));
QVBoxLayout *layout = new QVBoxLayout(this);
QTabWidget *tabWidget = new QTabWidget(this);
ControllerWidget *controllerWidget = new ControllerWidget(tabWidget);
tabWidget->addTab(controllerWidget, tr("Control"));
LogWidget *logWidget = new LogWidget(tabWidget);
tabWidget->addTab(logWidget, tr("Event log"));
layout->addWidget(tabWidget);
QHBoxLayout *bottomLayout = new QHBoxLayout;
layout->addLayout(bottomLayout);
bottomLayout->addStretch();
QPushButton *updateControlsButton = new QPushButton(tr("&Update"));
connect(updateControlsButton, SIGNAL(clicked()), controllerWidget, SLOT(updateStateControl()));
bottomLayout->addWidget(updateControlsButton);
QPushButton *clearLogButton = new QPushButton(tr("Clear &Log"));
connect(clearLogButton, SIGNAL(clicked()), logWidget, SLOT(clear()));
bottomLayout->addWidget(clearLogButton);
QPushButton *quitButton = new QPushButton(tr("&Quit"));
connect(quitButton, SIGNAL(clicked()), qApp, SLOT(quit()));
quitButton->setShortcut(Qt::CTRL | Qt::Key_Q);
bottomLayout->addWidget(quitButton);
}
void ControllerWindow::registerEventFilter()
{
qApp->installEventFilter(new EventFilter(qApp));
}
|