forked from quickshell/quickshell
feat: basic QtShell and ShellComponent types
This commit is contained in:
commit
23837e5195
9 changed files with 483 additions and 0 deletions
63
src/cpp/main.cpp
Normal file
63
src/cpp/main.cpp
Normal file
|
@ -0,0 +1,63 @@
|
|||
#include <LayerShellQt/shell.h>
|
||||
#include <qcommandlineoption.h>
|
||||
#include <qcommandlineparser.h>
|
||||
#include <qdir.h>
|
||||
#include <qguiapplication.h>
|
||||
#include <qlogging.h>
|
||||
#include <qobject.h>
|
||||
#include <qqmlapplicationengine.h>
|
||||
#include <qstandardpaths.h>
|
||||
#include <qstring.h>
|
||||
|
||||
#include "shell.hpp"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
const auto app = QGuiApplication(argc, argv);
|
||||
QGuiApplication::setApplicationName("qtshell");
|
||||
QGuiApplication::setApplicationVersion("0.0.1");
|
||||
|
||||
QCommandLineParser parser;
|
||||
parser.setApplicationDescription("Qt based desktop shell");
|
||||
parser.addHelpOption();
|
||||
parser.addVersionOption();
|
||||
|
||||
auto configOption = QCommandLineOption({"c", "config"}, "Path to configuration file.", "path");
|
||||
parser.addOption(configOption);
|
||||
parser.process(app);
|
||||
|
||||
QString configPath;
|
||||
if (parser.isSet(configOption)) {
|
||||
configPath = parser.value(configOption);
|
||||
} else {
|
||||
configPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
|
||||
configPath = QDir(QDir(configPath).filePath("qtshell")).filePath("config.qml");
|
||||
}
|
||||
|
||||
qInfo() << "config file path:" << configPath;
|
||||
|
||||
if (!QFile(configPath).exists()) {
|
||||
qCritical() << "config file does not exist";
|
||||
return -1;
|
||||
}
|
||||
|
||||
CONFIG_PATH = configPath;
|
||||
|
||||
LayerShellQt::Shell::useLayerShell();
|
||||
|
||||
auto engine = QQmlApplicationEngine();
|
||||
engine.load(configPath);
|
||||
|
||||
if (engine.rootObjects().isEmpty()) {
|
||||
qCritical() << "failed to load config file";
|
||||
return -1;
|
||||
}
|
||||
|
||||
auto* shellobj = qobject_cast<QtShell*>(engine.rootObjects().first());
|
||||
|
||||
if (shellobj == nullptr) {
|
||||
qCritical() << "root item was not a QtShell";
|
||||
return -1;
|
||||
}
|
||||
|
||||
return QGuiApplication::exec();
|
||||
}
|
96
src/cpp/shell.cpp
Normal file
96
src/cpp/shell.cpp
Normal file
|
@ -0,0 +1,96 @@
|
|||
#include "shell.hpp"
|
||||
#include <utility>
|
||||
|
||||
#include <qfileinfo.h>
|
||||
#include <qlogging.h>
|
||||
#include <qobject.h>
|
||||
#include <qobjectdefs.h>
|
||||
#include <qqmlcomponent.h>
|
||||
#include <qqmlcontext.h>
|
||||
#include <qqmlengine.h>
|
||||
#include <qqmllist.h>
|
||||
#include <qtmetamacros.h>
|
||||
|
||||
QString CONFIG_PATH; // NOLINT
|
||||
|
||||
QtShell::QtShell(): QObject(nullptr), path(CONFIG_PATH), dir(QFileInfo(this->path).dir()) {
|
||||
CONFIG_PATH = "";
|
||||
}
|
||||
|
||||
void QtShell::componentComplete() {
|
||||
if (this->path.isEmpty()) {
|
||||
qWarning() << "Multiple QtShell objects were created. You should not do this.";
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto* component: this->mComponents) {
|
||||
component->prepare(this->dir);
|
||||
}
|
||||
}
|
||||
|
||||
QQmlListProperty<ShellComponent> QtShell::components() {
|
||||
return QQmlListProperty<ShellComponent>(
|
||||
this,
|
||||
nullptr,
|
||||
&QtShell::appendComponent,
|
||||
nullptr,
|
||||
nullptr,
|
||||
nullptr,
|
||||
nullptr,
|
||||
nullptr
|
||||
);
|
||||
}
|
||||
|
||||
void QtShell::appendComponent(QQmlListProperty<ShellComponent>* list, ShellComponent* component) {
|
||||
auto* shell = qobject_cast<QtShell*>(list->object);
|
||||
|
||||
if (shell != nullptr) shell->mComponents.append(component);
|
||||
}
|
||||
|
||||
void ShellComponent::setSource(QString source) {
|
||||
if (this->mComponent != nullptr) {
|
||||
qWarning() << "cannot define ShellComponent.source while defining a component";
|
||||
return;
|
||||
}
|
||||
|
||||
this->mSource = std::move(source);
|
||||
}
|
||||
|
||||
void ShellComponent::setComponent(QQmlComponent* component) {
|
||||
if (this->mSource != nullptr) {
|
||||
qWarning() << "cannot define a component for ShellComponent while source is set";
|
||||
return;
|
||||
}
|
||||
|
||||
this->mComponent = component;
|
||||
}
|
||||
|
||||
void ShellComponent::prepare(const QDir& basePath) {
|
||||
if (this->mComponent == nullptr) {
|
||||
if (this->mSource == nullptr) {
|
||||
qWarning() << "neither source or a component was set for ShellComponent on prepare";
|
||||
return;
|
||||
}
|
||||
|
||||
auto path = basePath.filePath(this->mSource);
|
||||
qDebug() << "preparing ShellComponent from" << path;
|
||||
|
||||
auto* context = QQmlEngine::contextForObject(this);
|
||||
if (context == nullptr) {
|
||||
qWarning() << "ShellComponent was created without an associated QQmlEngine, cannot prepare";
|
||||
return;
|
||||
}
|
||||
|
||||
auto* engine = context->engine();
|
||||
|
||||
this->mComponent = new QQmlComponent(engine, path, this);
|
||||
|
||||
if (this->mComponent == nullptr) {
|
||||
qWarning() << "could not load ShellComponent source" << path;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
qDebug() << "Sending ready for ShellComponent";
|
||||
emit this->ready();
|
||||
}
|
59
src/cpp/shell.hpp
Normal file
59
src/cpp/shell.hpp
Normal file
|
@ -0,0 +1,59 @@
|
|||
#pragma once
|
||||
|
||||
#include <qdir.h>
|
||||
#include <qlogging.h>
|
||||
#include <qobject.h>
|
||||
#include <qqmlcomponent.h>
|
||||
#include <qqmlengine.h>
|
||||
#include <qqmlintegration.h>
|
||||
#include <qqmllist.h>
|
||||
#include <qqmlparserstatus.h>
|
||||
|
||||
extern QString CONFIG_PATH; // NOLINT
|
||||
|
||||
class ShellComponent;
|
||||
|
||||
class QtShell: public QObject, public QQmlParserStatus {
|
||||
Q_OBJECT;
|
||||
Q_INTERFACES(QQmlParserStatus);
|
||||
Q_PROPERTY(QQmlListProperty<ShellComponent> components READ components FINAL);
|
||||
Q_CLASSINFO("DefaultProperty", "components");
|
||||
QML_ELEMENT;
|
||||
|
||||
public:
|
||||
explicit QtShell();
|
||||
|
||||
void classBegin() override {};
|
||||
void componentComplete() override;
|
||||
|
||||
QQmlListProperty<ShellComponent> components();
|
||||
|
||||
private:
|
||||
static void appendComponent(QQmlListProperty<ShellComponent>* list, ShellComponent* component);
|
||||
|
||||
QList<ShellComponent*> mComponents;
|
||||
QString path;
|
||||
QDir dir;
|
||||
};
|
||||
|
||||
class ShellComponent: public QObject {
|
||||
Q_OBJECT;
|
||||
Q_PROPERTY(QString source WRITE setSource);
|
||||
Q_PROPERTY(QQmlComponent* component MEMBER mComponent WRITE setComponent);
|
||||
Q_CLASSINFO("DefaultProperty", "component");
|
||||
QML_ELEMENT;
|
||||
|
||||
public:
|
||||
explicit ShellComponent(QObject* parent = nullptr): QObject(parent) {}
|
||||
|
||||
void setSource(QString source);
|
||||
void setComponent(QQmlComponent* component);
|
||||
void prepare(const QDir& basePath);
|
||||
|
||||
signals:
|
||||
void ready();
|
||||
|
||||
private:
|
||||
QString mSource;
|
||||
QQmlComponent* mComponent = nullptr;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue