File indexing completed on 2024-05-05 05:40:58
0001 #include "selectconnectionprofiledialog.h" 0002 #include "ui_selectconnectionprofiledialog.h" 0003 0004 #include <QColorDialog> 0005 #include <QDebug> 0006 #include <QFileDialog> 0007 #include <QImage> 0008 #include <QMessageBox> 0009 #include <QPixmap> 0010 #include <QQmlEngine> 0011 0012 #include "worker/iohelper.h" 0013 0014 #include "controller/gamecontroller.h" 0015 #include "controller/networkcontroller.h" 0016 #include "controller/view_controller/imageselectorcontroller.h" 0017 #include "controller/view_controller/selectconnprofilecontroller.h" 0018 #include "imageselectordialog.h" 0019 #include "model/profilemodel.h" 0020 0021 #include "qml_views/image_selector.h" 0022 0023 /// ConnectionProfile 0024 SelectConnectionProfileDialog::SelectConnectionProfileDialog(GameController* ctrl, QWidget* parent) 0025 : QDialog(parent), ui(new Ui::SelectConnectionProfileDialog), m_gameCtrl{ctrl} 0026 { 0027 if(ctrl && ctrl->networkController() && ctrl->networkController()->profileModel()) 0028 m_ctrl.reset(new SelectConnProfileController(ctrl->networkController()->profileModel(), ctrl)); 0029 0030 ui->setupUi(this); 0031 connect(ui->m_quickWidget, &QQuickWidget::sceneGraphError, this, 0032 [](QQuickWindow::SceneGraphError, const QString& message) { qDebug() << "ERROR" << message; }); 0033 connect(ui->m_quickWidget, &QQuickWidget::statusChanged, this, 0034 [this](QQuickWidget::Status status) 0035 { 0036 if(status == QQuickWidget::Error) 0037 qDebug() << "Errors: " << ui->m_quickWidget->errors(); 0038 }); 0039 0040 qRegisterMetaType<ProfileModel*>("ProfileModel*"); 0041 qRegisterMetaType<CharacterDataModel*>("CharacterDataModel*"); 0042 qmlRegisterSingletonInstance<SelectConnProfileController>("Profile", 1, 0, "ProfileController", m_ctrl.get()); 0043 // qmlRegisterSingletonInstance<SelectConnectionProfileDialog>("Profile", 1, 0, "ProfileView", this); 0044 0045 qmlRegisterType<ImageSelector>("Profile", 1, 0, "ImageSelector"); 0046 auto engine= ui->m_quickWidget->engine(); 0047 engine->setObjectOwnership(m_ctrl.get(), QQmlEngine::CppOwnership); 0048 engine->addImportPath(QStringLiteral("qrc:/qml")); 0049 0050 ui->m_quickWidget->setSource(QUrl("qrc:/qml/views/ConnectionForm.qml")); 0051 ui->m_quickWidget->setVisible(true); 0052 0053 // link between game controller and profile controller 0054 if(!m_gameCtrl) 0055 return; 0056 auto networkCtrl= m_gameCtrl->networkController(); 0057 connect(networkCtrl, &NetworkController::lastErrorChanged, m_ctrl.get(), &SelectConnProfileController::setErrorMsg); 0058 connect(networkCtrl, &NetworkController::infoMessage, m_ctrl.get(), &SelectConnProfileController::setInfoMsg); 0059 connect(networkCtrl, &NetworkController::connectingChanged, this, 0060 [this](bool connecting) 0061 { 0062 m_ctrl->setConnectionState(connecting ? SelectConnProfileController::ConnectionState::CONNECTING : 0063 SelectConnProfileController::ConnectionState::IDLE); 0064 }); 0065 0066 // actions from controller 0067 connect(m_ctrl.get(), &SelectConnProfileController::saveModels, networkCtrl, &NetworkController::saveData); 0068 connect(m_ctrl.get(), &SelectConnProfileController::connectionStarted, this, 0069 [this]() { m_gameCtrl->setDataFromProfile(m_ctrl->currentProfileIndex()); }); 0070 connect(m_ctrl.get(), &SelectConnProfileController::portChanged, this, 0071 [this]() 0072 { 0073 if(!m_gameCtrl) 0074 return; 0075 auto networkCtrl= m_gameCtrl->networkController(); 0076 if(!networkCtrl) 0077 return; 0078 auto profile= networkCtrl->currentProfile(); 0079 if(!profile) 0080 return; 0081 0082 profile->setPort(m_ctrl->port()); 0083 }); 0084 connect(m_ctrl.get(), &SelectConnProfileController::addressChanged, this, 0085 [this]() 0086 { 0087 if(!m_gameCtrl) 0088 return; 0089 auto networkCtrl= m_gameCtrl->networkController(); 0090 if(!networkCtrl) 0091 return; 0092 auto profile= networkCtrl->currentProfile(); 0093 if(!profile) 0094 return; 0095 0096 profile->setAddress(m_ctrl->address()); 0097 }); 0098 connect(m_ctrl.get(), &SelectConnProfileController::passwordChanged, this, 0099 [this]() 0100 { 0101 if(!m_gameCtrl) 0102 return; 0103 auto networkCtrl= m_gameCtrl->networkController(); 0104 if(!networkCtrl) 0105 return; 0106 auto profile= networkCtrl->currentProfile(); 0107 if(!profile) 0108 return; 0109 0110 profile->editPassword(m_ctrl->password()); 0111 }); 0112 connect(m_ctrl.get(), &SelectConnProfileController::rejected, this, &SelectConnectionProfileDialog::reject); 0113 connect(m_ctrl.get(), &SelectConnProfileController::changeCampaignPath, this, 0114 [this]() 0115 { 0116 auto result 0117 = QFileDialog::getExistingDirectory(this, tr("Select directory"), m_ctrl->campaignPath(), 0118 QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks); 0119 if(!result.isEmpty()) 0120 { 0121 m_ctrl->setCampaignPath(result); 0122 } 0123 }); 0124 connect(m_ctrl.get(), &SelectConnProfileController::changeCharacterAvatar, this, 0125 [this](int i) 0126 { 0127 auto characters= m_ctrl->characterModel(); 0128 auto character= characters->character(i); 0129 auto data= openImage(); // m_ctrl->playerAvatar() 0130 if(data.isEmpty()) 0131 return; 0132 characters->setAvatar(i, data); 0133 }); 0134 connect(m_ctrl.get(), &SelectConnProfileController::changePlayerAvatar, this, 0135 [this]() 0136 { 0137 auto data= openImage(); // m_ctrl->playerAvatar() 0138 0139 if(data.isEmpty()) 0140 return; 0141 0142 m_ctrl->setPlayerAvatar(data); 0143 ui->m_playerAvatarAct->setIcon(QIcon(IOHelper::dataToPixmap(data))); 0144 }); 0145 0146 connect(m_ctrl.get(), &SelectConnProfileController::startConnect, networkCtrl, &NetworkController::startConnection); 0147 connect(m_ctrl.get(), &SelectConnProfileController::stopConnecting, networkCtrl, 0148 &NetworkController::stopConnecting); 0149 connect(ctrl, &GameController::dataLoaded, this, 0150 [this]() { m_ctrl->setConnectionState(SelectConnProfileController::ConnectionState::LOADED); }); 0151 } 0152 0153 SelectConnectionProfileDialog::~SelectConnectionProfileDialog() 0154 { 0155 delete ui; 0156 } 0157 0158 void SelectConnectionProfileDialog::setArgumentProfile(QString host, int port, QByteArray password) 0159 { 0160 /* ConnectionProfile* fromURL= new ConnectionProfile(); 0161 fromURL->setTitle(tr("From URL")); 0162 fromURL->setName(tr("Unknown")); 0163 fromURL->setAddress(host); 0164 fromURL->setPort(port); 0165 fromURL->setHash(password); 0166 fromURL->setGm(false); 0167 fromURL->setServerMode(false); 0168 fromURL->setPlayer(new Player); 0169 m_model->appendProfile(fromURL); 0170 auto index= m_model->indexOf(fromURL); 0171 ui->m_profileList->setCurrentIndex(m_model->index(index, 0)); 0172 m_currentProfile= fromURL; 0173 updateGUI(); 0174 updateProfile();*/ 0175 } 0176 0177 QByteArray SelectConnectionProfileDialog::openImage(const QString& path) 0178 { 0179 qDebug() << "path" << path; 0180 QFileInfo info(path); 0181 ImageSelectorController ctrl(true, ImageSelectorController::All, ImageSelectorController::Square, 0182 info.absoluteFilePath()); 0183 ImageSelectorDialog dialog(&ctrl, this); 0184 0185 if(QDialog::Accepted != dialog.exec()) 0186 return {}; 0187 0188 ctrl.address(); 0189 0190 return ctrl.finalImageData(); 0191 }