diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -68,3 +68,13 @@ Qt5::WebSockets libruqolacore ) + +#### +set (qmlFileTest_SRCS qmltest.cpp) +qt5_add_resources(qmlFileTestResources ../src/qml/qml.qrc) + +add_executable(qmlFileTest ${qmlFileTest_SRCS} ${qmlFileTestResources}) + +target_link_libraries(qmlFileTest + libruqolacore + ) diff --git a/tests/qmltest.h b/tests/qmltest.h new file mode 100644 --- /dev/null +++ b/tests/qmltest.h @@ -0,0 +1,54 @@ +/* + Copyright (c) 2018 Montel Laurent + + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Library General Public License as published + by the Free Software Foundation; either version 2 of the License or + ( at your option ) version 3 or, at the discretion of KDE e.V. + ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef QMLTEST_H +#define QMLTEST_H + +#include "ruqolaregisterengine.h" + +#include +#include +#include +#include + +class QmlTest : public QWidget +{ + Q_OBJECT + +public: + explicit QmlTest(QWidget *parent = nullptr); + ~QmlTest(); + void loadQmlFile(const QString &qmlPath); + void loadCombobox(); + +private Q_SLOTS: + void slotPushButtonClicked(); + +private: + void addFileToComboBox(); + bool isQmlFile(const QString &filename); + + QComboBox *combobox = nullptr; + QLabel *FileNameLabel = nullptr; + QPushButton *loadButton = nullptr; + RuqolaRegisterEngine *mEngine = nullptr; +}; + +#endif // QMLTEST_H diff --git a/tests/qmltest.cpp b/tests/qmltest.cpp new file mode 100644 --- /dev/null +++ b/tests/qmltest.cpp @@ -0,0 +1,125 @@ +/* + Copyright (c) 2018 Montel Laurent + + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Library General Public License as published + by the Free Software Foundation; either version 2 of the License or + ( at your option ) version 3 or, at the discretion of KDE e.V. + ( which shall act as a proxy as in section 14 of the GPLv3 ), any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "qmltest.h" +#include "ruqola_debug.h" + +#include +#include +#include +#include +#include +#include +#include + +QmlTest::QmlTest(QWidget *parent) + : QWidget(parent) +{ + combobox = new QComboBox(this); + FileNameLabel = new QLabel(QStringLiteral("File name: "), this); + loadButton = new QPushButton(QStringLiteral("load"), this); + mEngine = new RuqolaRegisterEngine; + + QHBoxLayout *layout = new QHBoxLayout(this); + layout->addWidget(FileNameLabel); + layout->addWidget(combobox); + layout->addWidget(loadButton); + + connect(loadButton, &QPushButton::clicked, this, &QmlTest::slotPushButtonClicked); + + loadCombobox();//mEngine initialized here +} + +QmlTest::~QmlTest() +{ + delete combobox; + delete FileNameLabel; + delete loadButton; + delete mEngine; +} + +bool QmlTest::isQmlFile(const QString &filename) +{ + return (filename.endsWith(QStringLiteral(".qml"))); +} + +void QmlTest::loadCombobox() +{ + if (mEngine->initialize()) { + combobox->addItem(QStringLiteral("Add file")); + //combobox->addItem(QStringLiteral(":/Desktop.qml"));//******test file******** + + QDir rsrc = QDir(QStringLiteral(":/")); + QDirIterator dirItr(rsrc); + while (dirItr.hasNext()) { + //qDebug() << "filename: " << dirItr.next() << endl; + const QString filename = dirItr.next(); + if (isQmlFile(filename)) combobox->addItem(filename); + } + }else { + //qCWarning(RUQOLA_LOG) << "Unable to initialize RuqolaRegisterEngine"; + } +} + +void QmlTest::addFileToComboBox() +{ + //take input from user + QDir rsrc = QDir(QStringLiteral("qrc:/")); + bool inValid = true; + QString text; + while(inValid) { + text = QInputDialog::getText(this, QStringLiteral("qml file path"), + QStringLiteral("Enter qml filename(example.qml):")); + inValid = !rsrc.exists(text); + } + const bool notFound = (combobox->findText(text) == -1); + if (notFound) { + combobox->addItem(text); + } + //else statment warn the user that comboBox already has that file +} + +void QmlTest::loadQmlFile(const QString &qmlPath) +{ + QQuickView *view = new QQuickView(QUrl::fromLocalFile(qmlPath)); + view->show(); +} + +void QmlTest::slotPushButtonClicked() +{ + QString msg = combobox->currentText(); + if (msg == QStringLiteral("Add file")) { + addFileToComboBox(); + }else { + //you can remove this else statment, to load the file directly after taking input from dialog box + loadQmlFile(msg); + } + +} + +int main(int argc, char *argv[]) +{ + Q_INIT_RESOURCE(qml); + + QApplication a(argc, argv); + QmlTest w; + w.show(); + return a.exec(); +}