Fix InputBox in Qt

This commit is contained in:
Henrik Rydgård committed 2023-03-22 14:39:57 +01:00
1 parent 604f3b4500
commit cc7b9569f2
2 files changed
+49 -28

No files matched your search

+48 -28
View File
@@ -58,6 +58,8 @@ MainUI *emugl = nullptr;
static float refreshRate = 60.f;
static int browseFileEvent = -1;
static int browseFolderEvent = -1;
static int inputBoxEvent = -1;
QTCamera *qtcamera = nullptr;
MainWindow *g_mainWindow;
@@ -273,22 +275,54 @@ void System_Notify(SystemNotification notification) {
}
}
// TODO: Find a better version to pass parameters to HandleCustomEvent.
static std::string g_param1;
static std::string g_param2;
static int g_requestId;
bool MainUI::HandleCustomEvent(QEvent *e) {
if (e->type() == browseFileEvent) {
QString fileName = QFileDialog::getOpenFileName(nullptr, "Load ROM", g_Config.currentDirectory.c_str(), "PSP ROMs (*.iso *.cso *.pbp *.elf *.zip *.ppdmp)");
if (QFile::exists(fileName)) {
QDir newPath;
g_Config.currentDirectory = Path(newPath.filePath(fileName).toStdString());
g_Config.Save("browseFileEvent");
NativeMessageReceived("boot", fileName.toStdString().c_str());
}
} else if (e->type() == browseFolderEvent) {
auto mm = GetI18NCategory("MainMenu");
QString fileName = QFileDialog::getExistingDirectory(nullptr, mm->T("Choose folder"), g_Config.currentDirectory.c_str());
if (QDir(fileName).exists()) {
NativeMessageReceived("browse_folderSelect", fileName.toStdString().c_str());
}
} else if (e->type() == inputBoxEvent) {
QString title = QString::fromStdString(g_param1);
QString defaultValue = QString::fromStdString(g_param2);
QString text = emugl->InputBoxGetQString(title, defaultValue);
if (text.isEmpty()) {
g_requestManager.PostSystemFailure(g_requestId);
} else {
g_requestManager.PostSystemSuccess(g_requestId, text.toStdString().c_str());
}
} else {
return false;
}
return true;
}
bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2) {
switch (type) {
case SystemRequestType::INPUT_TEXT_MODAL:
{
// NOTE: This doesn't actually work, we're on the wrong thread!
// Was broken even previously to the conversion to System_MakeRequest.
QString title = QString::fromStdString(param1);
QString defaultValue = QString::fromStdString(param2);
QString text = emugl->InputBoxGetQString(title, defaultValue);
if (text.isEmpty()) {
g_requestManager.PostSystemFailure(requestId);
} else {
g_requestManager.PostSystemSuccess(requestId, text.toStdString().c_str());
}
break;
g_requestId = requestId;
g_param1 = param1;
g_param2 = param2;
QCoreApplication::postEvent(emugl, new QEvent((QEvent::Type)inputBoxEvent));
return true;
}
default:
return false;
}
}
@@ -365,6 +399,7 @@ static int mainInternal(QApplication &a) {
browseFileEvent = QEvent::registerEventType();
browseFolderEvent = QEvent::registerEventType();
inputBoxEvent = QEvent::registerEventType();
int retval = a.exec();
delete emugl;
@@ -576,23 +611,8 @@ bool MainUI::event(QEvent *e) {
break;
default:
if (e->type() == browseFileEvent) {
QString fileName = QFileDialog::getOpenFileName(nullptr, "Load ROM", g_Config.currentDirectory.c_str(), "PSP ROMs (*.iso *.cso *.pbp *.elf *.zip *.ppdmp)");
if (QFile::exists(fileName)) {
QDir newPath;
g_Config.currentDirectory = Path(newPath.filePath(fileName).toStdString());
g_Config.Save("browseFileEvent");
NativeMessageReceived("boot", fileName.toStdString().c_str());
}
break;
} else if (e->type() == browseFolderEvent) {
auto mm = GetI18NCategory("MainMenu");
QString fileName = QFileDialog::getExistingDirectory(nullptr, mm->T("Choose folder"), g_Config.currentDirectory.c_str());
if (QDir(fileName).exists()) {
NativeMessageReceived("browse_folderSelect", fileName.toStdString().c_str());
}
} else {
// Can't switch on dynamic event types.
if (!HandleCustomEvent(e)) {
return QWidget::event(e);
}
}
+1
View File
@@ -140,6 +140,7 @@ protected:
void EmuThreadJoin();
private:
bool HandleCustomEvent(QEvent *e);
QtGLGraphicsContext *graphicsContext;
float xscale, yscale;