File indexing completed on 2024-04-28 05:38:13

0001 /***************************************************************************
0002  *  Copyright (C) 2020 by Renaud Guezennec                               *
0003  *   http://www.rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   This software is free software; you can redistribute it and/or modify *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #include "instantmessagingview.h"
0021 #include "ui_instantmessagingview.h"
0022 
0023 #include <QDebug>
0024 #include <QQmlContext>
0025 #include <QQmlEngine>
0026 #include <QQuickWidget>
0027 #include <QUrl>
0028 #include <QVBoxLayout>
0029 
0030 #include "qml_components/avatarprovider.h"
0031 
0032 InstantMessagingView::InstantMessagingView(InstantMessagingController* ctrl, QWidget* parent)
0033     : QWidget(parent), m_ui(new Ui::InstantMessagingView), m_qmlViewer(new QQuickWidget()), m_ctrl(ctrl)
0034 {
0035     m_ui->setupUi(this);
0036 
0037     auto engine= m_qmlViewer->engine();
0038 
0039     connect(engine, &QQmlEngine::warnings, this, [](const QList<QQmlError>& warnings) {
0040         for(const auto& warn : warnings)
0041         {
0042             qDebug() << warn.toString();
0043         }
0044     });
0045     qmlRegisterSingletonType<InstantMessagerManager>(
0046         "org.rolisteam.InstantMessaging", 1, 0, "InstantMessagerManager",
0047         [this, engine](QQmlEngine* qmlengine, QJSEngine* scriptEngine) -> QObject* {
0048             Q_UNUSED(scriptEngine)
0049             if(qmlengine != engine)
0050                 return {};
0051             static InstantMessagerManager manager;
0052             qmlengine->setObjectOwnership(&manager, QQmlEngine::CppOwnership);
0053             static bool initialized= false;
0054             if(!initialized)
0055             {
0056                 manager.setCtrl(m_ctrl);
0057                 initialized= true;
0058             }
0059             return &manager;
0060         });
0061 
0062     engine->addImageProvider("avatar", new AvatarProvider(m_ctrl->playerModel()));
0063     engine->addImportPath(QStringLiteral("qrc:/qml"));
0064     engine->addImportPath(QStringLiteral("qrc:/qml/rolistyle"));
0065 
0066     connect(m_qmlViewer.get(), &QQuickWidget::sceneGraphError, this,
0067             [](QQuickWindow::SceneGraphError, const QString& msg) { qDebug() << msg; });
0068     connect(engine, &QQmlEngine::warnings, this, [](const QList<QQmlError>& warnings) {
0069         for(const auto& error : warnings)
0070             qDebug() << error.toString();
0071     });
0072     setMinimumWidth(200);
0073 
0074     m_qmlViewer->setResizeMode(QQuickWidget::SizeRootObjectToView);
0075     m_qmlViewer->setSource(QUrl("qrc:/qml/InstantMessaging/InstantMessagingMain.qml"));
0076 
0077     for(const auto& error : m_qmlViewer->errors())
0078         qDebug() << error.toString();
0079 
0080     auto layout= new QVBoxLayout();
0081     layout->setContentsMargins(QMargins());
0082     setLayout(layout);
0083 
0084     layout->addWidget(m_qmlViewer.get());
0085 
0086     setWindowTitle(tr("Instant Messaging"));
0087 }
0088 
0089 InstantMessagingView::~InstantMessagingView()
0090 {
0091     delete m_ui;
0092 }
0093 
0094 void InstantMessagingView::closeEvent(QCloseEvent* event)
0095 {
0096     hide();
0097     event->accept();
0098 }
0099 
0100 InstantMessagerManager::InstantMessagerManager(QObject* object) : QObject(object) {}
0101 
0102 InstantMessagingController* InstantMessagerManager::ctrl() const
0103 {
0104     return m_ctrl;
0105 }
0106 
0107 void InstantMessagerManager::setCtrl(InstantMessagingController* ctrl)
0108 {
0109     if(ctrl == m_ctrl)
0110         return;
0111     m_ctrl= ctrl;
0112     emit ctrlChanged();
0113 }