<commit_before>#include "StableHeaders.h"
#include "Foundation.h"
#include "QtModule.h"
#include "UICanvasManager.h"

#include <QtUiTools>
#include <QFile>
#include <QTime>

#include "QtGUI.h"

namespace CommunicationUI
{

	QtGUI::QtGUI(Foundation::Framework *framework)
		: framework_(framework)
	{
		LogInfo("Loading UIController to QtModule UICanvas...");
		// Get comm manager to check state
		communication_service_ = Communication::CommunicationService::GetInstance();
		if (communication_service_ == 0)
		{
			throw Core::Exception("Cannot get CommunicationService object.");
		}
		// Use QtModule to show our QtUI widget
		boost::shared_ptr<QtModule> qt_module = framework_->GetModuleManager()->GetModule<QtModule>(Foundation::Module::MT_Gui).lock();
		
		if ( qt_module.get() == 0)
		{	
			LogWarning("Could not aqquire QtModule and show Comm UI");
		}
		else 
		{
			// Set param to QtUI::UICanvas::Internal to put inside ogre window
			canvas_login_ = qt_module->CreateCanvas(UICanvas::Internal).lock();
            canvas_login_->Hide();
            canvas_chat_ = qt_module->CreateCanvas(UICanvas::Internal).lock();
            canvas_chat_->Hide();

			UIContainer *UIContainer_ = new UIContainer(0, framework_, canvas_login_, canvas_chat_);

			// Connect signal for resizing
			QObject::connect(UIContainer_, SIGNAL( ChangeToolBarButton(QString, QString) ), this, SLOT( ChangeToolBarButton(QString, QString) ));
			QObject::connect(UIContainer_, SIGNAL( DestroyCanvas() ), this, SLOT( DestroyThis() ));
			
            //QObject::connect(UIContainer_, SIGNAL( SetCanvasTitle(QString) ), canvas_.get(), SLOT( SetCanvasWindowTitle(QString) ));
			//QObject::connect(UIContainer_, SIGNAL( SetCanvasIcon(QIcon &) ), canvas_.get(), SLOT( SetCanvasWindowIcon(QIcon &) ));
			// Init title, icon and size
            //UIContainer_->resize(400, 185);
			//canvas_login_->SetCanvasWindowIcon(QIcon(":/images/iconUsers.png"));
			//canvas_login_->SetCanvasWindowTitle(QString("realXtend Naali Jabber IM Login"));

			// Add to control bar
			qt_module->AddCanvasToControlBar(canvas_login_->GetID(), QString("IM Login"));
			LogInfo("Loading succesfull");
		}		
	}

	QtGUI::~QtGUI(void)
	{
		DestroyThis();
	}

	void QtGUI::DestroyThis()
	{
		boost::shared_ptr<QtModule> qt_module = framework_->GetModuleManager()->GetModule<QtModule>(Foundation::Module::MT_Gui).lock();
		if (qt_module.get())
		{
            if (canvas_login_)
			    qt_module->DeleteCanvas(canvas_login_->GetID());
            if (canvas_chat_)
                qt_module->DeleteCanvas(canvas_chat_->GetID());
		}
	}

	void QtGUI::ChangeToolBarButton(QString oldID, QString newID)
	{
        boost::shared_ptr<QtModule> qt_module = framework_->GetModuleManager()->GetModule<QtModule>(Foundation::Module::MT_Gui).lock();
		if (qt_module.get())
        {
            qt_module->RemoveCanvasFromControlBar(oldID);
            qt_module->AddCanvasToControlBar(newID, QString("IM"));
        }
	}

	/////////////////////////////////////////////////////////////////////
	// UIContainer CLASS
	/////////////////////////////////////////////////////////////////////

    UIContainer::UIContainer(QWidget *parent, Foundation::Framework *framework, boost::shared_ptr<UICanvas> canvas_login, boost::shared_ptr<UICanvas> canvas_chat)
		: QWidget(parent), chatWidget_(0), loginWidget_(0), framework_(framework), canvas_login_(canvas_login), canvas_chat_(canvas_chat)
	{
		communication_service_ = Communication::CommunicationService::GetInstance();

		LogInfo("Creating UIContainer, initializing with Login widget...");
		this->setLayout(new QVBoxLayout);
		this->layout()->setMargin(0);
		this->setObjectName(QString("containerWidget"));
        this->setStyleSheet("QWidget#containerWidget { background-color: rgba(0,0,0,0); padding: 0px; margin: 0px; }");
		LoadUserInterface(false);
		LogInfo("Loading successfull");
	}

	UIContainer::~UIContainer(void)
	{
		delete this->layout();
	}

	void UIContainer::LoadUserInterface(bool connected)
	{
		if (connected && chatWidget_ == 0)
		{
            // Init chat GUI
			QUiLoader loader;
			QFile uiFile("./data/ui/communications.ui");
			chatWidget_ = loader.load(&uiFile, 0);
			//chatWidget_->layout()->setMargin(0);
            chatWidget_->resize(720, 350);
			uiFile.close();

			// Insert custom tab widget
			tabWidgetCoversations_ = new ConversationsContainer(chatWidget_);
			tabWidgetCoversations_->clear();
			QHBoxLayout *friendsAndTabWidget = chatWidget_->findChild<QHBoxLayout *>("horizontalLayout_FriendsAndTabWidget");
			friendsAndTabWidget->insertWidget(1, tabWidgetCoversations_); 
			friendsAndTabWidget->setStretch(1,2);
			
			// Get widgets
			listWidgetFriends_ = chatWidget_->findChild<QListWidget *>("listWidget_Friends");
			listWidgetFriends_->setIconSize(QSize(10,10));
			labelUsername_ = chatWidget_->findChild<QLabel *>("label_UserName");
			lineEditStatus_ = chatWidget_->findChild<QLineEdit *>("lineEdit_Status");
			comboBoxStatus_ = chatWidget_->findChild<QComboBox *>("comboBox_Status");
			comboBoxStatus_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
			comboBoxStatus_->setIconSize(QSize(10,10));
			buttonAddFriend_ = chatWidget_->findChild<QPushButton *>("pushButton_AddFriend");
			buttonAddFriend_->setIcon(QIcon(":/images/iconAdd.png"));
			buttonAddFriend_->setLayoutDirection(Qt::RightToLeft);
			buttonAddFriend_->setIconSize(QSize(10,10));
			buttonRemoveFriend_ = chatWidget_->findChild<QPushButton *>("pushButton_RemoveFriend");
			buttonRemoveFriend_->setIcon(QIcon(":/images/iconRemove.png"));
			buttonRemoveFriend_->setIconSize(QSize(10,10));
            
			canvas_chat_->SetCanvasResizeLock(true);
            canvas_chat_->SetPosition(20,20);
		    canvas_chat_->SetCanvasSize(720, 350);
            canvas_chat_->AddWidget(chatWidget_);
            canvas_login_->Hide();
            canvas_chat_->Show();
            emit( ChangeToolBarButton(canvas_login_->GetID(), canvas_chat_->GetID()) );
			// Add widget to layout
            //this->layout()->addWidget(chatWidget_);
            //emit ( Resized(QSize(720, 350)) );
            //emit ( SetCanvasTitle(QString("realXtend Naali Jabber IM")) );
		} 
		else
		{
            if (loginWidget_ == 0)
			{			
			    // Init login GUI
			    loginWidget_ = new Login(0, currentMessage, framework_);
                loginWidget_->setObjectName(QString("containerMiddleChat"));
                loginWidget_->setStyleSheet("QWidget#containerMiddleChat { background-color: rgba(0,0,0,0); padding: 0px; margin: 0px; }");
			    // Get widgets
			    labelLoginConnectionStatus_ = loginWidget_->findChild<QLabel *>("label_Status");
			    // Connect signals
			    QObject::connect(loginWidget_, SIGNAL( UserdataSet(QString, int, QString, QString) ), this, SLOT( ConnectToServer(QString, int, QString, QString) ));
			    
    			canvas_login_->SetCanvasResizeLock(true);
                canvas_login_->SetPosition(20,20);
			    canvas_login_->SetCanvasSize(400, 185);
                canvas_login_->AddWidget(loginWidget_);
                // Add widget to layout
			    /*this->layout()->addWidget(loginWidget_);*/
			    //emit( SetCanvasTitle(QString("realXtend Naali Communications Login")) );
            }
		}

	}

	void UIContainer::LoadConnectedUserData(Communication::ConnectionInterface *connection)
	{
		QString user_id = connection->GetUserID();;
		labelUsername_->setText(user_id); 
		
		// Update contacts
		ContactListChanged(connection->GetContacts().GetContacts());

		// Get available state options
		comboBoxStatus_->clear();
		comboBoxStatus_->addItems(im_connection_->GetPresenceStatusOptionsForUser());

		// Connect signals
		//QObject::connect(im_connection_->GetUser(), SIGNAL ( NAME_THIS(ContactVector) ), this, SLOT( ContactListChanged(ContactVector) ));
		QObject::connect(listWidgetFriends_, SIGNAL( itemDoubleClicked(QListWidgetItem *) ), this, SLOT( StartNewChat(QListWidgetItem *) )); 
		QObject::connect(comboBoxStatus_, SIGNAL( currentIndexChanged(const QString &) ), this, SLOT( StatusChanged(const QString &) ));
		QObject::connect(lineEditStatus_, SIGNAL( returnPressed() ), this, SLOT( StatusMessageChanged() ));
		QObject::connect(buttonAddFriend_, SIGNAL( clicked(bool) ), this, SLOT( AddNewFriend(bool) ));
		QObject::connect(buttonRemoveFriend_, SIGNAL( clicked(bool) ), this, SLOT( RemoveFriend(bool) ));
	}<commit_msg>* Moved init position Y to 30 pixels from old 20, so it wont overlap with ControlBar on first show.<commit_after>#include "StableHeaders.h"
#include "Foundation.h"
#include "QtModule.h"
#include "UICanvasManager.h"

#include <QtUiTools>
#include <QFile>
#include <QTime>

#include "QtGUI.h"

namespace CommunicationUI
{

	QtGUI::QtGUI(Foundation::Framework *framework)
		: framework_(framework)
	{
		LogInfo("Loading UIController to QtModule UICanvas...");
		// Get comm manager to check state
		communication_service_ = Communication::CommunicationService::GetInstance();
		if (communication_service_ == 0)
		{
			throw Core::Exception("Cannot get CommunicationService object.");
		}
		// Use QtModule to show our QtUI widget
		boost::shared_ptr<QtModule> qt_module = framework_->GetModuleManager()->GetModule<QtModule>(Foundation::Module::MT_Gui).lock();
		
		if ( qt_module.get() == 0)
		{	
			LogWarning("Could not aqquire QtModule and show Comm UI");
		}
		else 
		{
			// Set param to QtUI::UICanvas::Internal to put inside ogre window
			canvas_login_ = qt_module->CreateCanvas(UICanvas::Internal).lock();
            canvas_login_->Hide();
            canvas_chat_ = qt_module->CreateCanvas(UICanvas::Internal).lock();
            canvas_chat_->Hide();

			UIContainer *UIContainer_ = new UIContainer(0, framework_, canvas_login_, canvas_chat_);

			// Connect signal for resizing
			QObject::connect(UIContainer_, SIGNAL( ChangeToolBarButton(QString, QString) ), this, SLOT( ChangeToolBarButton(QString, QString) ));
			QObject::connect(UIContainer_, SIGNAL( DestroyCanvas() ), this, SLOT( DestroyThis() ));
			
            //QObject::connect(UIContainer_, SIGNAL( SetCanvasTitle(QString) ), canvas_.get(), SLOT( SetCanvasWindowTitle(QString) ));
			//QObject::connect(UIContainer_, SIGNAL( SetCanvasIcon(QIcon &) ), canvas_.get(), SLOT( SetCanvasWindowIcon(QIcon &) ));
			// Init title, icon and size
            //UIContainer_->resize(400, 185);
			//canvas_login_->SetCanvasWindowIcon(QIcon(":/images/iconUsers.png"));
			//canvas_login_->SetCanvasWindowTitle(QString("realXtend Naali Jabber IM Login"));

			// Add to control bar
			qt_module->AddCanvasToControlBar(canvas_login_->GetID(), QString("IM Login"));
			LogInfo("Loading succesfull");
		}		
	}

	QtGUI::~QtGUI(void)
	{
		DestroyThis();
	}

	void QtGUI::DestroyThis()
	{
		boost::shared_ptr<QtModule> qt_module = framework_->GetModuleManager()->GetModule<QtModule>(Foundation::Module::MT_Gui).lock();
		if (qt_module.get())
		{
            if (canvas_login_)
			    qt_module->DeleteCanvas(canvas_login_->GetID());
            if (canvas_chat_)
                qt_module->DeleteCanvas(canvas_chat_->GetID());
		}
	}

	void QtGUI::ChangeToolBarButton(QString oldID, QString newID)
	{
        boost::shared_ptr<QtModule> qt_module = framework_->GetModuleManager()->GetModule<QtModule>(Foundation::Module::MT_Gui).lock();
		if (qt_module.get())
        {
            qt_module->RemoveCanvasFromControlBar(oldID);
            qt_module->AddCanvasToControlBar(newID, QString("IM"));
        }
	}

	/////////////////////////////////////////////////////////////////////
	// UIContainer CLASS
	/////////////////////////////////////////////////////////////////////

    UIContainer::UIContainer(QWidget *parent, Foundation::Framework *framework, boost::shared_ptr<UICanvas> canvas_login, boost::shared_ptr<UICanvas> canvas_chat)
		: QWidget(parent), chatWidget_(0), loginWidget_(0), framework_(framework), canvas_login_(canvas_login), canvas_chat_(canvas_chat)
	{
		communication_service_ = Communication::CommunicationService::GetInstance();

		LogInfo("Creating UIContainer, initializing with Login widget...");
		this->setLayout(new QVBoxLayout);
		this->layout()->setMargin(0);
		this->setObjectName(QString("containerWidget"));
        this->setStyleSheet("QWidget#containerWidget { background-color: rgba(0,0,0,0); padding: 0px; margin: 0px; }");
		LoadUserInterface(false);
		LogInfo("Loading successfull");
	}

	UIContainer::~UIContainer(void)
	{
		delete this->layout();
	}

	void UIContainer::LoadUserInterface(bool connected)
	{
		if (connected && chatWidget_ == 0)
		{
            // Init chat GUI
			QUiLoader loader;
			QFile uiFile("./data/ui/communications.ui");
			chatWidget_ = loader.load(&uiFile, 0);
			//chatWidget_->layout()->setMargin(0);
            chatWidget_->resize(720, 350);
			uiFile.close();

			// Insert custom tab widget
			tabWidgetCoversations_ = new ConversationsContainer(chatWidget_);
			tabWidgetCoversations_->clear();
			QHBoxLayout *friendsAndTabWidget = chatWidget_->findChild<QHBoxLayout *>("horizontalLayout_FriendsAndTabWidget");
			friendsAndTabWidget->insertWidget(1, tabWidgetCoversations_); 
			friendsAndTabWidget->setStretch(1,2);
			
			// Get widgets
			listWidgetFriends_ = chatWidget_->findChild<QListWidget *>("listWidget_Friends");
			listWidgetFriends_->setIconSize(QSize(10,10));
			labelUsername_ = chatWidget_->findChild<QLabel *>("label_UserName");
			lineEditStatus_ = chatWidget_->findChild<QLineEdit *>("lineEdit_Status");
			comboBoxStatus_ = chatWidget_->findChild<QComboBox *>("comboBox_Status");
			comboBoxStatus_->setSizeAdjustPolicy(QComboBox::AdjustToContents);
			comboBoxStatus_->setIconSize(QSize(10,10));
			buttonAddFriend_ = chatWidget_->findChild<QPushButton *>("pushButton_AddFriend");
			buttonAddFriend_->setIcon(QIcon(":/images/iconAdd.png"));
			buttonAddFriend_->setLayoutDirection(Qt::RightToLeft);
			buttonAddFriend_->setIconSize(QSize(10,10));
			buttonRemoveFriend_ = chatWidget_->findChild<QPushButton *>("pushButton_RemoveFriend");
			buttonRemoveFriend_->setIcon(QIcon(":/images/iconRemove.png"));
			buttonRemoveFriend_->setIconSize(QSize(10,10));
            
			canvas_chat_->SetCanvasResizeLock(true);
            canvas_chat_->SetPosition(20,30);
		    canvas_chat_->SetCanvasSize(720, 350);
            canvas_chat_->AddWidget(chatWidget_);
            canvas_login_->Hide();
            canvas_chat_->Show();
            emit( ChangeToolBarButton(canvas_login_->GetID(), canvas_chat_->GetID()) );
			// Add widget to layout
            //this->layout()->addWidget(chatWidget_);
            //emit ( Resized(QSize(720, 350)) );
            //emit ( SetCanvasTitle(QString("realXtend Naali Jabber IM")) );
		} 
		else
		{
            if (loginWidget_ == 0)
			{			
			    // Init login GUI
			    loginWidget_ = new Login(0, currentMessage, framework_);
                loginWidget_->setObjectName(QString("containerMiddleChat"));
                loginWidget_->setStyleSheet("QWidget#containerMiddleChat { background-color: rgba(0,0,0,0); padding: 0px; margin: 0px; }");
			    // Get widgets
			    labelLoginConnectionStatus_ = loginWidget_->findChild<QLabel *>("label_Status");
			    // Connect signals
			    QObject::connect(loginWidget_, SIGNAL( UserdataSet(QString, int, QString, QString) ), this, SLOT( ConnectToServer(QString, int, QString, QString) ));
			    
    			canvas_login_->SetCanvasResizeLock(true);
                canvas_login_->SetPosition(20,30);
			    canvas_login_->SetCanvasSize(400, 185);
                canvas_login_->AddWidget(loginWidget_);
                // Add widget to layout
			    /*this->layout()->addWidget(loginWidget_);*/
			    //emit( SetCanvasTitle(QString("realXtend Naali Communications Login")) );
            }
		}

	}

	void UIContainer::LoadConnectedUserData(Communication::ConnectionInterface *connection)
	{
		QString user_id = connection->GetUserID();;
		labelUsername_->setText(user_id); 
		
		// Update contacts
		ContactListChanged(connection->GetContacts().GetContacts());

		// Get available state options
		comboBoxStatus_->clear();
		comboBoxStatus_->addItems(im_connection_->GetPresenceStatusOptionsForUser());

		// Connect signals
		//QObject::connect(im_connection_->GetUser(), SIGNAL ( NAME_THIS(ContactVector) ), this, SLOT( ContactListChanged(ContactVector) ));
		QObject::connect(listWidgetFriends_, SIGNAL( itemDoubleClicked(QListWidgetItem *) ), this, SLOT( StartNewChat(QListWidgetItem *) )); 
		QObject::connect(comboBoxStatus_, SIGNAL( currentIndexChanged(const QString &) ), this, SLOT( StatusChanged(const QString &) ));
		QObject::connect(lineEditStatus_, SIGNAL( returnPressed() ), this, SLOT( StatusMessageChanged() ));
		QObject::connect(buttonAddFriend_, SIGNAL( clicked(bool) ), this, SLOT( AddNewFriend(bool) ));
		QObject::connect(buttonRemoveFriend_, SIGNAL( clicked(bool) ), this, SLOT( RemoveFriend(bool) ));
	}
{'fraction_non_alphanumeric': 0.15232613339662948, 'fraction_numerical': 0.009731782577735581, 'mean_word_length': 4.145954198473283, 'pattern_counts': {'":': 6, '<': 41, '<?xml version=': 0, '>': 189, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1877948', 'n_tokens_mistral': 5538, 'n_tokens_neox': 4973, 'n_words': 827}