File indexing completed on 2024-04-28 09:26:02

0001 /*
0002  *   SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
0003  *
0004  *   SPDX-License-Identifier: LGPL-3.0-or-later
0005  */
0006 
0007 #include <QCommandLineParser>
0008 
0009 #include <QGuiApplication>
0010 #include <QPainter>
0011 #include <QRasterWindow>
0012 #include <QTimer>
0013 #include <QWindow>
0014 
0015 #include <QMetaEnum>
0016 
0017 #include <interfaces/window.h>
0018 
0019 using namespace LayerShellQt;
0020 
0021 QStringList enumsToStringList(QMetaEnum metaEnum)
0022 {
0023     QStringList ret;
0024     ret.reserve(metaEnum.keyCount());
0025     for (int i = 0; i < metaEnum.keyCount(); ++i) {
0026         ret.append(metaEnum.key(i));
0027     }
0028     return ret;
0029 }
0030 
0031 template<typename T>
0032 T stringToEnum(QMetaEnum metaEnum, const QString &str)
0033 {
0034     T ret = {};
0035     const auto splitted = str.split(QLatin1Char('|'), Qt::SkipEmptyParts);
0036     for (const auto &value : splitted) {
0037         ret |= T(metaEnum.keyToValue(qPrintable(value)));
0038     }
0039     return ret;
0040 }
0041 
0042 class BasicWindow : public QRasterWindow
0043 {
0044     void paintEvent(QPaintEvent *) override
0045     {
0046         QPainter p(this);
0047         p.fillRect(QRect(0, 0, width(), height()), Qt::red);
0048     }
0049 };
0050 
0051 int main(int argc, char **argv)
0052 {
0053     QGuiApplication app(argc, argv);
0054 
0055     const auto layerMetaEnum = QMetaEnum::fromType<Window::Layer>();
0056     const auto anchorMetaEnum = QMetaEnum::fromType<Window::Anchor>();
0057 
0058     QCommandLineParser parser;
0059     QCommandLineOption marginsOption(QStringLiteral("margins"), QStringLiteral("Window margins"), QStringLiteral("pixels"), QStringLiteral("0"));
0060     QCommandLineOption scopeOption(QStringLiteral("scope"), QStringLiteral("Window scope"), QStringLiteral("namespace"), QStringLiteral("normal"));
0061     QCommandLineOption anchorsOption(QStringLiteral("anchors"),
0062                                      QStringLiteral("Either ") + enumsToStringList(anchorMetaEnum).join(QLatin1String("|")),
0063                                      QStringLiteral("anchors"),
0064                                      QStringLiteral("AnchorTop|AnchorBottom|AnchorLeft|AnchorRight"));
0065     QCommandLineOption layerOption(QStringLiteral("layer"),
0066                                    QStringLiteral("One of ") + enumsToStringList(layerMetaEnum).join(QLatin1String("|")),
0067                                    QStringLiteral("layer"),
0068                                    QStringLiteral("LayerTop"));
0069     QCommandLineOption widthOption(QStringLiteral("width"), QStringLiteral("Width of the window"), QStringLiteral("pixels"), QStringLiteral("0"));
0070     QCommandLineOption heightOption(QStringLiteral("height"), QStringLiteral("Height of the window"), QStringLiteral("pixels"), QStringLiteral("0"));
0071 
0072     parser.addOptions({marginsOption, scopeOption, anchorsOption, layerOption, widthOption, heightOption});
0073     parser.addHelpOption();
0074     parser.process(app);
0075 
0076     BasicWindow window;
0077 
0078     LayerShellQt::Window *layerShell = LayerShellQt::Window::get(&window);
0079     layerShell->setLayer(Window::LayerBottom);
0080 
0081     if (parser.isSet(marginsOption)) {
0082         int margins = parser.value(marginsOption).toInt();
0083         layerShell->setMargins({margins, margins, margins, margins});
0084     }
0085 
0086     if (parser.isSet(scopeOption)) {
0087         layerShell->setScope(parser.value(scopeOption));
0088     }
0089     if (parser.isSet(layerOption)) {
0090         layerShell->setLayer(Window::Layer(layerMetaEnum.keyToValue(qPrintable(parser.value(layerOption)))));
0091     }
0092     if (parser.isSet(anchorsOption)) {
0093         layerShell->setAnchors(stringToEnum<Window::Anchors>(anchorMetaEnum, parser.value(anchorsOption)));
0094     }
0095     if (parser.isSet(widthOption)) {
0096         window.setWidth(parser.value(widthOption).toInt());
0097     }
0098     if (parser.isSet(heightOption)) {
0099         window.setHeight(parser.value(heightOption).toInt());
0100     }
0101 
0102     window.show();
0103 
0104     BasicWindow window2;
0105     window2.resize(400, 400);
0106     window2.show();
0107 
0108     // just so you don't block yourself out whilst testing
0109     QTimer::singleShot(5000, &app, &QGuiApplication::quit);
0110     return app.exec();
0111 }