1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-22 16:53:54 +00:00
openmw/apps/opencs/view/doc/filedialog.cpp

225 lines
6 KiB
C++
Raw Normal View History

#include "filedialog.hpp"
#include <QDialogButtonBox>
2022-09-22 18:26:05 +00:00
#include <QPushButton>
2022-10-19 17:02:00 +00:00
#include <components/contentselector/model/contentmodel.hpp>
#include <components/contentselector/model/esmfile.hpp>
#include <components/contentselector/view/contentselector.hpp>
#include <components/files/qtconversion.hpp>
2022-10-19 17:02:00 +00:00
#include <string>
#include "adjusterwidget.hpp"
2022-09-22 18:26:05 +00:00
#include "filewidget.hpp"
2022-09-22 18:26:05 +00:00
CSVDoc::FileDialog::FileDialog(QWidget* parent)
: QDialog(parent)
, mSelector(nullptr)
, mAction(ContentAction_Undefined)
, mFileWidget(nullptr)
, mAdjusterWidget(nullptr)
, mDialogBuilt(false)
{
2022-09-22 18:26:05 +00:00
ui.setupUi(this);
resize(400, 400);
2022-09-22 18:26:05 +00:00
setObjectName("FileDialog");
mSelector = new ContentSelectorView::ContentSelector(ui.contentSelectorWidget, /*showOMWScripts=*/false);
mAdjusterWidget = new AdjusterWidget(this);
}
void CSVDoc::FileDialog::addFiles(const std::vector<std::filesystem::path>& dataDirs)
{
for (auto iter = dataDirs.rbegin(); iter != dataDirs.rend(); ++iter)
{
QString path = Files::pathToQString(*iter);
mSelector->addFiles(path);
}
mSelector->sortFiles();
}
2022-09-22 18:26:05 +00:00
void CSVDoc::FileDialog::setEncoding(const QString& encoding)
{
mSelector->setEncoding(encoding);
}
void CSVDoc::FileDialog::clearFiles()
{
mSelector->clearFiles();
}
QStringList CSVDoc::FileDialog::selectedFilePaths()
{
QStringList filePaths;
2022-09-22 18:26:05 +00:00
for (ContentSelectorModel::EsmFile* file : mSelector->selectedFiles())
filePaths.append(file->filePath());
return filePaths;
}
2022-09-22 18:26:05 +00:00
void CSVDoc::FileDialog::setLocalData(const std::filesystem::path& localData)
2013-10-27 03:55:44 +00:00
{
2022-09-22 18:26:05 +00:00
mAdjusterWidget->setLocalData(localData);
2013-10-27 03:55:44 +00:00
}
2022-09-22 18:26:05 +00:00
void CSVDoc::FileDialog::showDialog(ContentAction action)
{
mAction = action;
2022-09-22 18:26:05 +00:00
ui.projectGroupBoxLayout->insertWidget(0, mAdjusterWidget);
switch (mAction)
{
2022-09-22 18:26:05 +00:00
case ContentAction_New:
buildNewFileView();
break;
2022-09-22 18:26:05 +00:00
case ContentAction_Edit:
buildOpenFileView();
break;
2022-09-22 18:26:05 +00:00
default:
break;
}
2022-09-22 18:26:05 +00:00
mAdjusterWidget->setFilenameCheck(mAction == ContentAction_New);
2022-09-22 18:26:05 +00:00
if (!mDialogBuilt)
{
2022-09-22 18:26:05 +00:00
// connections common to both dialog view flavors
connect(mSelector, &ContentSelectorView::ContentSelector::signalCurrentGamefileIndexChanged, this,
qOverload<int>(&FileDialog::slotUpdateAcceptButton));
2022-09-22 18:26:05 +00:00
connect(ui.projectButtonBox, &QDialogButtonBox::rejected, this, &FileDialog::slotRejected);
mDialogBuilt = true;
}
show();
raise();
activateWindow();
}
void CSVDoc::FileDialog::buildNewFileView()
{
setWindowTitle(tr("Create a new addon"));
2022-09-22 18:26:05 +00:00
QPushButton* createButton = ui.projectButtonBox->button(QDialogButtonBox::Ok);
createButton->setText("Create");
createButton->setEnabled(false);
2022-09-22 18:26:05 +00:00
if (!mFileWidget)
{
2022-09-22 18:26:05 +00:00
mFileWidget = new FileWidget(this);
2022-09-22 18:26:05 +00:00
mFileWidget->setType(true);
mFileWidget->extensionLabelIsVisible(true);
2022-09-22 18:26:05 +00:00
connect(mFileWidget, &FileWidget::nameChanged, mAdjusterWidget, &AdjusterWidget::setName);
2013-10-27 03:55:44 +00:00
2022-09-22 18:26:05 +00:00
connect(mFileWidget, &FileWidget::nameChanged, this,
qOverload<const QString&, bool>(&FileDialog::slotUpdateAcceptButton));
}
2013-10-27 03:55:44 +00:00
2022-09-22 18:26:05 +00:00
ui.projectGroupBoxLayout->insertWidget(0, mFileWidget);
2022-09-22 18:26:05 +00:00
connect(ui.projectButtonBox, &QDialogButtonBox::accepted, this, &FileDialog::slotNewFile);
}
void CSVDoc::FileDialog::buildOpenFileView()
{
setWindowTitle(tr("Open"));
2022-09-22 18:26:05 +00:00
ui.projectGroupBox->setTitle(QString(""));
ui.projectButtonBox->button(QDialogButtonBox::Ok)->setText("Open");
if (mSelector->isGamefileSelected())
ui.projectButtonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
else
2022-09-22 18:26:05 +00:00
ui.projectButtonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
2013-10-27 03:55:44 +00:00
2022-09-22 18:26:05 +00:00
if (!mDialogBuilt)
{
2022-09-22 18:26:05 +00:00
connect(mSelector, &ContentSelectorView::ContentSelector::signalAddonDataChanged, this,
&FileDialog::slotAddonDataChanged);
}
2022-09-22 18:26:05 +00:00
connect(ui.projectButtonBox, &QDialogButtonBox::accepted, this, &FileDialog::slotOpenFile);
}
2022-09-22 18:26:05 +00:00
void CSVDoc::FileDialog::slotAddonDataChanged(const QModelIndex& topleft, const QModelIndex& bottomright)
{
slotUpdateAcceptButton(0);
}
void CSVDoc::FileDialog::slotUpdateAcceptButton(int)
{
QString name = "";
if (mFileWidget && mAction == ContentAction_New)
name = mFileWidget->getName();
2022-09-22 18:26:05 +00:00
slotUpdateAcceptButton(name, true);
}
2022-09-22 18:26:05 +00:00
void CSVDoc::FileDialog::slotUpdateAcceptButton(const QString& name, bool)
{
bool success = !mSelector->selectedFiles().empty();
bool isNew = (mAction == ContentAction_New);
if (isNew)
success = !name.isEmpty();
else if (success)
{
2022-09-22 18:26:05 +00:00
ContentSelectorModel::EsmFile* file = mSelector->selectedFiles().back();
mAdjusterWidget->setName(file->filePath(), !file->isGameFile());
}
else
2022-09-22 18:26:05 +00:00
mAdjusterWidget->setName("", true);
2022-09-22 18:26:05 +00:00
ui.projectButtonBox->button(QDialogButtonBox::Ok)->setEnabled(success);
}
QString CSVDoc::FileDialog::filename() const
{
if (mAction == ContentAction_New)
2013-10-27 03:55:44 +00:00
return "";
return mSelector->currentFile();
}
void CSVDoc::FileDialog::slotRejected()
{
emit rejected();
2022-09-22 18:26:05 +00:00
disconnect(ui.projectButtonBox, &QDialogButtonBox::accepted, this, &FileDialog::slotNewFile);
disconnect(ui.projectButtonBox, &QDialogButtonBox::accepted, this, &FileDialog::slotOpenFile);
if (mFileWidget)
{
delete mFileWidget;
2018-10-09 06:21:12 +00:00
mFileWidget = nullptr;
}
close();
}
2013-10-27 03:55:44 +00:00
void CSVDoc::FileDialog::slotNewFile()
{
2022-09-22 18:26:05 +00:00
emit signalCreateNewFile(mAdjusterWidget->getPath());
if (mFileWidget)
{
delete mFileWidget;
2018-10-09 06:21:12 +00:00
mFileWidget = nullptr;
}
2022-09-22 18:26:05 +00:00
disconnect(ui.projectButtonBox, &QDialogButtonBox::accepted, this, &FileDialog::slotNewFile);
2014-09-08 21:24:18 +00:00
close();
2013-10-27 03:55:44 +00:00
}
void CSVDoc::FileDialog::slotOpenFile()
{
2022-09-22 18:26:05 +00:00
ContentSelectorModel::EsmFile* file = mSelector->selectedFiles().back();
2013-10-27 03:55:44 +00:00
2022-09-22 18:26:05 +00:00
mAdjusterWidget->setName(file->filePath(), !file->isGameFile());
2013-10-27 03:55:44 +00:00
2022-09-22 18:26:05 +00:00
emit signalOpenFiles(mAdjusterWidget->getPath());
disconnect(ui.projectButtonBox, &QDialogButtonBox::accepted, this, &FileDialog::slotOpenFile);
2014-09-08 21:24:18 +00:00
close();
2013-10-27 03:55:44 +00:00
}