这是indexloc提供的服务,不要输入任何密码
Skip to content
126 changes: 104 additions & 22 deletions app/src/mainwindow2.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*

Pencil - Traditional Animation Software
Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
Expand Down Expand Up @@ -132,9 +132,6 @@ MainWindow2::MainWindow2(QWidget* parent) :
ui->background->init(mEditor->preference());

setWindowTitle(PENCIL_WINDOW_TITLE);

if (!loadMostRecent())
tryLoadPreset();
}

MainWindow2::~MainWindow2()
Expand Down Expand Up @@ -519,6 +516,19 @@ void MainWindow2::closeEvent(QCloseEvent* event)
}
}

void MainWindow2::showEvent(QShowEvent*)
{
static bool firstShowEvent = true;
if (firstShowEvent)
{
firstShowEvent = false;
if (tryRecoverUnsavedProject()) { return; }
if (loadMostRecent()) { return; }
if (tryLoadPreset()) { return; }
newObject();
}
}

void MainWindow2::tabletEvent(QTabletEvent* event)
{
event->ignore();
Expand All @@ -528,7 +538,10 @@ void MainWindow2::newDocument()
{
if (maybeSave())
{
tryLoadPreset();
if (!tryLoadPreset())
{
newObject();
}
}
}

Expand Down Expand Up @@ -641,7 +654,7 @@ bool MainWindow2::openObject(QString strFilePath)
dd.collect(error.details());
ErrorDialog errorDialog(error.title(), error.description(), dd.str());
errorDialog.exec();
newEmptyDocumentAfterErrorOccurred();
emptyDocumentWhenErrorOccurred();
return false;
}

Expand All @@ -651,7 +664,7 @@ bool MainWindow2::openObject(QString strFilePath)
tr("An unknown error occurred while trying to load the file and we are not able to load your file."),
QString("Raw file path: %1\nResolved file path: %2").arg(strFilePath, fullPath));
errorDialog.exec();
newEmptyDocumentAfterErrorOccurred();
emptyDocumentWhenErrorOccurred();
return false;
}

Expand Down Expand Up @@ -816,7 +829,7 @@ bool MainWindow2::autoSave()
return false;
}

void MainWindow2::newEmptyDocumentAfterErrorOccurred()
void MainWindow2::emptyDocumentWhenErrorOccurred()
{
newObject();

Expand Down Expand Up @@ -1062,19 +1075,21 @@ bool MainWindow2::newObject()
bool MainWindow2::newObjectFromPresets(int presetIndex)
{
Object* object = nullptr;
QString presetFilePath = (presetIndex > 0) ? PresetDialog::getPresetPath(presetIndex) : "";
if (!presetFilePath.isEmpty())
QString presetFilePath = PresetDialog::getPresetPath(presetIndex);

if (presetFilePath.isEmpty())
{
FileManager fm(this);
object = fm.load(presetFilePath);
if (fm.error().ok() == false) object = nullptr;
return false;
}
if (object == nullptr)

FileManager fm(this);
object = fm.load(presetFilePath);

if (fm.error().ok() == false || object == nullptr)
{
object = new Object();
object->init();
object->createDefaultLayers();
return false;
}

mEditor->setObject(object);
object->setFilePath(QString());

Expand All @@ -1099,7 +1114,7 @@ bool MainWindow2::loadMostRecent()
return false;
}

void MainWindow2::tryLoadPreset()
bool MainWindow2::tryLoadPreset()
{
if (mEditor->preference()->isOn(SETTING::ASK_FOR_PRESET))
{
Expand All @@ -1110,21 +1125,25 @@ void MainWindow2::tryLoadPreset()
if (result == QDialog::Accepted)
{
int presetIndex = presetDialog->getPresetIndex();
if (presetDialog->shouldAlwaysUse()) {
if (presetDialog->shouldAlwaysUse())
{
mEditor->preference()->set(SETTING::ASK_FOR_PRESET, false);
mEditor->preference()->set(SETTING::DEFAULT_PRESET, presetIndex);
}
newObjectFromPresets(presetIndex);
qDebug() << "Accepted!";
if (!newObjectFromPresets(presetIndex))
{
newObject();
}
}
});
presetDialog->open();
}
else
{
int defaultPreset = mEditor->preference()->getInt(SETTING::DEFAULT_PRESET);
newObjectFromPresets(defaultPreset);
return newObjectFromPresets(defaultPreset);
}
return true;
}

void MainWindow2::readSettings()
Expand Down Expand Up @@ -1541,3 +1560,66 @@ void MainWindow2::displayMessageBoxNoTitle(const QString& body)
{
QMessageBox::information(this, nullptr, tr(qPrintable(body)), QMessageBox::Ok);
}

bool MainWindow2::tryRecoverUnsavedProject()
{
FileManager fm;
QStringList recoverables = fm.searchForUnsavedProjects();

if (recoverables.size() == 0)
{
return false;
}

QString caption = tr("Restore Project?");
QString text = tr("Pencil2D didn't close correctly. Would you like to restore the project?");

QString recoverPath = recoverables[0];

QMessageBox* msgBox = new QMessageBox(this);
msgBox->setWindowTitle(tr("Restore project"));
msgBox->setWindowModality(Qt::ApplicationModal);
msgBox->setAttribute(Qt::WA_DeleteOnClose);
msgBox->setIconPixmap(QPixmap(":/icons/logo.png"));
msgBox->setText(QString("<h4>%1</h4>%2").arg(caption).arg(text));
msgBox->setInformativeText(QString("<b>%1</b>").arg(retrieveProjectNameFromTempPath(recoverPath)));
msgBox->setStandardButtons(QMessageBox::Open | QMessageBox::Discard);
msgBox->setProperty("RecoverPath", recoverPath);
hideQuestionMark(*msgBox);

connect(msgBox, &QMessageBox::finished, this, &MainWindow2::startProjectRecovery);
msgBox->open();
return true;
}

void MainWindow2::startProjectRecovery(int result)
{
const QMessageBox* msgBox = dynamic_cast<QMessageBox*>(QObject::sender());
const QString recoverPath = msgBox->property("RecoverPath").toString();

if (result == QMessageBox::Discard)
{
// The user presses discard
QDir(recoverPath).removeRecursively();
tryLoadPreset();
return;
}
Q_ASSERT(result == QMessageBox::Open);

FileManager fm;
Object* o = fm.recoverUnsavedProject(recoverPath);
if (!fm.error().ok())
{
Q_ASSERT(o == nullptr);
const QString title = tr("Recovery Failed.");
const QString text = tr("Sorry! Pencil2D is unable to restore your project");
QMessageBox::information(this, title, QString("<h4>%1</h4>%2").arg(title).arg(text));
}

mEditor->setObject(o);
updateSaveState();

const QString title = tr("Recovery Succeeded!");
const QString text = tr("Please save your work immediately to prevent loss of data");
QMessageBox::information(this, title, QString("<h4>%1</h4>%2").arg(title).arg(text));
}
11 changes: 7 additions & 4 deletions app/src/mainwindow2.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public slots:
bool saveAsNewDocument();
bool maybeSave();
bool autoSave();
void newEmptyDocumentAfterErrorOccurred();
void emptyDocumentWhenErrorOccurred();

// import
void importImage();
Expand All @@ -96,7 +96,7 @@ public slots:
void setSoundScrubMsec(int msec);
void setOpacity(int opacity);
void preferences();

void openFile(QString filename);

PreferencesDialog* getPrefDialog() { return mPrefDialog; }
Expand All @@ -111,6 +111,7 @@ public slots:
protected:
void tabletEvent(QTabletEvent*) override;
void closeEvent(QCloseEvent*) override;
void showEvent(QShowEvent*) override;

private slots:
void resetAndDockAllSubWidgets();
Expand All @@ -128,7 +129,7 @@ private slots:
void clearKeyboardShortcuts();
void updateZoomLabel();
bool loadMostRecent();
void tryLoadPreset();
bool tryLoadPreset();

void openPalette();
void importPalette();
Expand All @@ -151,6 +152,9 @@ private slots:

void bindActionWithSetting(QAction*, const SETTING&);

bool tryRecoverUnsavedProject();
void startProjectRecovery(int result);

// UI: Dock widgets
ColorBox* mColorBox = nullptr;
ColorPaletteWidget* mColorPalette = nullptr;
Expand Down Expand Up @@ -182,7 +186,6 @@ private slots:

// whether we are currently importing an image sequence.
bool mIsImportingImageSequence = false;
// bool mLoadMostRecent = true;

Ui::MainWindow2* ui = nullptr;
};
Expand Down
14 changes: 11 additions & 3 deletions app/src/presetdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,19 @@ bool PresetDialog::shouldAlwaysUse()

QString PresetDialog::getPresetPath(int index)
{
QString filename = QString("%1.pclx").arg(index);
if (index == 0)
{
return QString();
}

const QString filename = QString("%1.pclx").arg(index);
QDir dataDir = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation));
if (dataDir.cd("presets"))
{
return dataDir.filePath(filename);
if (dataDir.exists(filename))
{
return dataDir.filePath(filename);
}
}
return QString();
}
Expand Down Expand Up @@ -77,7 +85,7 @@ void PresetDialog::initPresets()
return;
}
QSettings presets(dataDir.filePath("presets.ini"), QSettings::IniFormat, this);

bool ok = true;
for (const QString& key : presets.allKeys())
{
Expand Down
Loading