Qt: Implement drag/drop file on main window

This commit is contained in:
Connor McLaughlin 2021-03-12 02:50:17 +10:00
parent eca0b51008
commit 4685d66a2b
3 changed files with 69 additions and 20 deletions

View File

@ -22,6 +22,7 @@
#include <QtCore/QDebug>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QMimeData>
#include <QtCore/QUrl>
#include <QtGui/QCursor>
#include <QtGui/QWindowStateChangeEvent>
@ -609,26 +610,7 @@ void MainWindow::onGameListEntrySelected(const GameListEntry* entry)
void MainWindow::onGameListEntryDoubleClicked(const GameListEntry* entry)
{
// if we're not running, boot the system, otherwise swap discs
QString path = QString::fromStdString(entry->path);
if (!m_emulation_running)
{
if (!entry->code.empty() && m_host_interface->GetBoolSettingValue("Main", "SaveStateOnExit", true) &&
!m_host_interface->IsCheevosChallengeModeActive())
{
m_host_interface->resumeSystemFromState(path, true);
}
else
{
m_host_interface->bootSystem(std::make_shared<const SystemBootParameters>(path.toStdString()));
}
}
else
{
m_host_interface->changeDisc(path);
m_host_interface->pauseSystem(false);
switchToEmulationView();
}
startGameOrChangeDiscs(entry->path);
}
void MainWindow::onGameListContextMenuRequested(const QPoint& point, const GameListEntry* entry)
@ -952,6 +934,34 @@ void MainWindow::switchToEmulationView()
m_display_widget->setFocus();
}
void MainWindow::startGameOrChangeDiscs(const std::string& path)
{
// if we're not running, boot the system, otherwise swap discs
if (!m_emulation_running)
{
if (m_host_interface->GetBoolSettingValue("Main", "SaveStateOnExit", true) &&
!m_host_interface->IsCheevosChallengeModeActive())
{
const GameListEntry* entry = m_host_interface->getGameList()->GetEntryForPath(path.c_str());
if ((entry && !entry->code.empty()) || !System::GetGameCodeForPath(path.c_str(), true).empty())
{
m_host_interface->resumeSystemFromState(QString::fromStdString(path), true);
return;
}
}
else
{
m_host_interface->bootSystem(std::make_shared<const SystemBootParameters>(path));
}
}
else
{
m_host_interface->changeDisc(QString::fromStdString(path));
m_host_interface->pauseSystem(false);
switchToEmulationView();
}
}
void MainWindow::connectSignals()
{
updateEmulationActions(false, false, m_host_interface->IsCheevosChallengeModeActive());
@ -1418,6 +1428,39 @@ void MainWindow::changeEvent(QEvent* event)
QMainWindow::changeEvent(event);
}
static std::string getFilenameFromMimeData(const QMimeData* md)
{
std::string filename;
if (md->hasUrls())
{
// only one url accepted
const QList<QUrl> urls(md->urls());
if (urls.size() == 1)
filename = urls.front().toLocalFile().toStdString();
}
return filename;
}
void MainWindow::dragEnterEvent(QDragEnterEvent* event)
{
const std::string filename(getFilenameFromMimeData(event->mimeData()));
if (!System::IsLoadableFilename(filename.c_str()))
return;
event->acceptProposedAction();
}
void MainWindow::dropEvent(QDropEvent* event)
{
const std::string filename(getFilenameFromMimeData(event->mimeData()));
if (!System::IsLoadableFilename(filename.c_str()))
return;
event->acceptProposedAction();
startGameOrChangeDiscs(filename);
}
void MainWindow::startupUpdateCheck()
{
if (!m_host_interface->GetBoolSettingValue("AutoUpdater", "CheckAtStartup", true))

View File

@ -111,6 +111,8 @@ private Q_SLOTS:
protected:
void closeEvent(QCloseEvent* event) override;
void changeEvent(QEvent* event) override;
void dragEnterEvent(QDragEnterEvent* event) override;
void dropEvent(QDropEvent* event) override;
private:
void setupAdditionalUi();
@ -120,6 +122,7 @@ private:
bool isShowingGameList() const;
void switchToGameListView();
void switchToEmulationView();
void startGameOrChangeDiscs(const std::string& path);
void saveStateToConfig();
void restoreStateFromConfig();
void saveDisplayWindowGeometryToConfig();

View File

@ -17,6 +17,9 @@
<iconset resource="resources/resources.qrc">
<normaloff>:/icons/duck.png</normaloff>:/icons/duck.png</iconset>
</property>
<property name="acceptDrops">
<bool>true</bool>
</property>
<widget class="QStackedWidget" name="mainContainer">
<property name="currentIndex">
<number>0</number>