Qt: Unified file opening and saving with last location

This commit is contained in:
Jeffrey Pfau 2015-05-30 00:45:53 -07:00
parent 0378fa229d
commit bbac206364
9 changed files with 72 additions and 63 deletions

View File

@ -47,6 +47,7 @@ Misc:
- All: Enable static linking for Windows
- All: Enable static linking for OS X
- Qt: Migrate multiplayer window handling into GBAApp
- Qt: Unified file opening and saving with last location
0.2.1: (2015-05-13)
Bugfixes:

View File

@ -5,10 +5,10 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "CheatsView.h"
#include "GBAApp.h"
#include "GameController.h"
#include <QClipboard>
#include <QFileDialog>
extern "C" {
#include "gba/cheats.h"
@ -64,14 +64,14 @@ bool CheatsView::eventFilter(QObject* object, QEvent* event) {
}
void CheatsView::load() {
QString filename = QFileDialog::getOpenFileName(this, tr("Select cheats file"));
QString filename = GBAApp::app()->getOpenFileName(this, tr("Select cheats file"));
if (!filename.isEmpty()) {
m_model.loadFile(filename);
}
}
void CheatsView::save() {
QString filename = QFileDialog::getSaveFileName(this, tr("Select cheats file"));
QString filename = GBAApp::app()->getSaveFileName(this, tr("Select cheats file"));
if (!filename.isEmpty()) {
m_model.saveFile(filename);
}

View File

@ -9,6 +9,8 @@
#include "GameController.h"
#include "Window.h"
#include <QFileDialog>
#include <QFileInfo>
#include <QFileOpenEvent>
extern "C" {
@ -22,6 +24,7 @@ GBAApp* g_app = nullptr;
GBAApp::GBAApp(int& argc, char* argv[])
: QApplication(argc, argv)
, m_windows{}
{
g_app = this;
@ -85,6 +88,44 @@ Window* GBAApp::newWindow() {
return w;
}
Window* GBAApp::newWindow() {
return g_app->newWindowInternal();
GBAApp* GBAApp::app() {
return g_app;
}
void GBAApp::interruptAll() {
for (int i = 0; i < MAX_GBAS; ++i) {
if (!m_windows[i] || !m_windows[i]->controller()->isLoaded()) {
continue;
}
m_windows[i]->controller()->threadInterrupt();
}
}
void GBAApp::continueAll() {
for (int i = 0; i < MAX_GBAS; ++i) {
if (!m_windows[i] || !m_windows[i]->controller()->isLoaded()) {
continue;
}
m_windows[i]->controller()->threadContinue();
}
}
QString GBAApp::getOpenFileName(QWidget* owner, const QString& title, const QString& filter) {
interruptAll();
QString filename = QFileDialog::getOpenFileName(owner, title, m_configController.getQtOption("lastDirectory").toString(), filter);
continueAll();
if (!filename.isEmpty()) {
m_configController.setQtOption("lastDirectory", QFileInfo(filename).dir().path());
}
return filename;
}
QString GBAApp::getSaveFileName(QWidget* owner, const QString& title, const QString& filter) {
interruptAll();
QString filename = QFileDialog::getSaveFileName(owner, title, m_configController.getQtOption("lastDirectory").toString(), filter);
continueAll();
if (!filename.isEmpty()) {
m_configController.setQtOption("lastDirectory", QFileInfo(filename).dir().path());
}
return filename;
}

View File

@ -25,8 +25,16 @@ Q_OBJECT
public:
GBAApp(int& argc, char* argv[]);
static GBAApp* app();
static Window* newWindow();
Window* newWindow();
QString getOpenFileName(QWidget* owner, const QString& title, const QString& filter = QString());
QString getSaveFileName(QWidget* owner, const QString& title, const QString& filter = QString());
public slots:
void interruptAll();
void continueAll();
protected:
bool event(QEvent*);

View File

@ -7,7 +7,8 @@
#ifdef USE_MAGICK
#include <QFileDialog>
#include "GBAApp.h"
#include <QMap>
using namespace QGBA;
@ -48,7 +49,7 @@ void GIFView::stopRecording() {
}
void GIFView::selectFile() {
QString filename = QFileDialog::getSaveFileName(this, tr("Select output file"));
QString filename = GBAApp::app()->getSaveFileName(this, tr("Select output file"), tr("Graphics Interchange Format (*.gif)"));
if (!filename.isEmpty()) {
m_ui.filename->setText(filename);
if (!ImageMagickGIFEncoderIsOpen(&m_encoder)) {

View File

@ -5,9 +5,9 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "PaletteView.h"
#include "GBAApp.h"
#include "VFileDevice.h"
#include <QFileDialog>
#include <QFontDatabase>
extern "C" {
@ -83,7 +83,7 @@ void PaletteView::exportPalette(int start, int length) {
length = 512 - start;
}
m_controller->threadInterrupt();
QString filename = QFileDialog::getSaveFileName(this, tr("Export palette"), QString(), tr("Windows PAL (*.pal);;Adobe Color Table (*.act)"));
QString filename = GBAApp::app()->getSaveFileName(this, tr("Export palette"), tr("Windows PAL (*.pal)"));
if (filename.isNull()) {
m_controller->threadContinue();
return;

View File

@ -7,8 +7,7 @@
#include "AudioProcessor.h"
#include "ConfigController.h"
#include <QFileDialog>
#include "GBAApp.h"
using namespace QGBA;
@ -65,7 +64,7 @@ SettingsView::SettingsView(ConfigController* controller, QWidget* parent)
}
void SettingsView::selectBios() {
QString filename = QFileDialog::getOpenFileName(this, tr("Select BIOS"));
QString filename = GBAApp::app()->getOpenFileName(this, tr("Select BIOS"));
if (!filename.isEmpty()) {
m_ui.bios->setText(filename);
}

View File

@ -7,7 +7,8 @@
#ifdef USE_FFMPEG
#include <QFileDialog>
#include "GBAApp.h"
#include <QMap>
using namespace QGBA;
@ -212,7 +213,7 @@ void VideoView::stopRecording() {
}
void VideoView::selectFile() {
QString filename = QFileDialog::getSaveFileName(this, tr("Select output file"));
QString filename = GBAApp::app()->getSaveFileName(this, tr("Select output file"));
if (!filename.isEmpty()) {
m_ui.filename->setText(filename);
}

View File

@ -5,8 +5,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "Window.h"
#include <QFileDialog>
#include <QFileInfo>
#include <QKeyEvent>
#include <QKeySequence>
#include <QMenuBar>
@ -216,10 +214,6 @@ void Window::saveConfig() {
}
void Window::selectROM() {
bool doPause = m_controller->isLoaded() && !m_controller->isPaused();
if (doPause) {
m_controller->setPaused(true);
}
QStringList formats{
"*.gba",
#ifdef USE_LIBZIP
@ -231,27 +225,15 @@ void Window::selectROM() {
"*.rom",
"*.bin"};
QString filter = tr("Game Boy Advance ROMs (%1)").arg(formats.join(QChar(' ')));
QString filename = QFileDialog::getOpenFileName(this, tr("Select ROM"), m_config->getQtOption("lastDirectory").toString(), filter);
if (doPause) {
m_controller->setPaused(false);
}
QString filename = GBAApp::app()->getOpenFileName(this, tr("Select ROM"), filter);
if (!filename.isEmpty()) {
m_config->setQtOption("lastDirectory", QFileInfo(filename).dir().path());
m_controller->loadGame(filename);
}
}
void Window::selectBIOS() {
bool doPause = m_controller->isLoaded() && !m_controller->isPaused();
if (doPause) {
m_controller->setPaused(true);
}
QString filename = QFileDialog::getOpenFileName(this, tr("Select BIOS"), m_config->getQtOption("lastDirectory").toString());
if (doPause) {
m_controller->setPaused(false);
}
QString filename = GBAApp::app()->getOpenFileName(this, tr("Select BIOS"));
if (!filename.isEmpty()) {
m_config->setQtOption("lastDirectory", QFileInfo(filename).dir().path());
m_config->setOption("bios", filename);
m_config->updateOption("bios");
m_config->setOption("useBios", true);
@ -261,16 +243,8 @@ void Window::selectBIOS() {
}
void Window::selectPatch() {
bool doPause = m_controller->isLoaded() && !m_controller->isPaused();
if (doPause) {
m_controller->setPaused(true);
}
QString filename = QFileDialog::getOpenFileName(this, tr("Select patch"), m_config->getQtOption("lastDirectory").toString(), tr("Patches (*.ips *.ups *.bps)"));
if (doPause) {
m_controller->setPaused(false);
}
QString filename = GBAApp::app()->getOpenFileName(this, tr("Select patch"), tr("Patches (*.ips *.ups *.bps)"));
if (!filename.isEmpty()) {
m_config->setQtOption("lastDirectory", QFileInfo(filename).dir().path());
m_controller->loadPatch(filename);
}
}
@ -282,31 +256,15 @@ void Window::openView(QWidget* widget) {
}
void Window::importSharkport() {
bool doPause = m_controller->isLoaded() && !m_controller->isPaused();
if (doPause) {
m_controller->setPaused(true);
}
QString filename = QFileDialog::getOpenFileName(this, tr("Select save"), m_config->getQtOption("lastDirectory").toString(), tr("GameShark saves (*.sps *.xps)"));
if (doPause) {
m_controller->setPaused(false);
}
QString filename = GBAApp::app()->getOpenFileName(this, tr("Select save"), tr("GameShark saves (*.sps *.xps)"));
if (!filename.isEmpty()) {
m_config->setQtOption("lastDirectory", QFileInfo(filename).dir().path());
m_controller->importSharkport(filename);
}
}
void Window::exportSharkport() {
bool doPause = m_controller->isLoaded() && !m_controller->isPaused();
if (doPause) {
m_controller->setPaused(true);
}
QString filename = QFileDialog::getSaveFileName(this, tr("Select save"), m_config->getQtOption("lastDirectory").toString(), tr("GameShark saves (*.sps *.xps)"));
if (doPause) {
m_controller->setPaused(false);
}
QString filename = GBAApp::app()->getSaveFileName(this, tr("Select save"), tr("GameShark saves (*.sps *.xps)"));
if (!filename.isEmpty()) {
m_config->setQtOption("lastDirectory", QFileInfo(filename).dir().path());
m_controller->exportSharkport(filename);
}
}
@ -713,7 +671,7 @@ void Window::setupMenu(QMenuBar* menubar) {
fileMenu->addSeparator();
QAction* multiWindow = new QAction(tr("New multiplayer window"), fileMenu);
connect(multiWindow, &QAction::triggered, [this]() {
GBAApp::newWindow();
GBAApp::app()->newWindow();
});
addControlledAction(fileMenu, multiWindow, "multiWindow");