Skip to content

Commit

Permalink
Assorted fixes (pencil2d#1721)
Browse files Browse the repository at this point in the history
* Fix project recovery dialog not actually being modal

The dialog was being shown from within MW2's showEvent, but that event is called
just *before* the window is visible, hence why the dialog wasn't actually modal.

* Fix toolbar object name warning

* Fix undo/redo shortcuts sometimes being erroneously disabled

The enable/disable code was written under the assumption that undo/redo could
only be triggered from the menu, but did not take keyboard shortcuts or the
recently added toolbar into account.

* Fix incorrect translation context in app and layer classes

* Check for open instance after handling --help and --version

* Fix object data memory leak

* Note that the DOM string conversion bug in Qt was fixed in 5.14
  • Loading branch information
J5lx authored Jul 3, 2022
1 parent 514c672 commit 7b910c4
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 72 deletions.
13 changes: 0 additions & 13 deletions app/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ GNU General Public License for more details.
*/

#include <clocale>

#include "log.h"
#include "pencil2d.h"
#include "pencilerror.h"
Expand All @@ -28,23 +26,12 @@ GNU General Public License for more details.
*/
int main(int argc, char* argv[])
{
// iss #940
// Force dot separator on numbers because some localizations
// use comma as separator.
std::setlocale(LC_NUMERIC, "en_US.UTF-8");

Q_INIT_RESOURCE(core_lib);
PlatformHandler::initialise();
initCategoryLogging();

Pencil2D app(argc, argv);

#ifndef QT_DEBUG
if (app.isInstanceOpen()) {
return EXIT_SUCCESS;
}
#endif

switch (app.handleCommandLineOptions().code())
{
case Status::OK:
Expand Down
44 changes: 23 additions & 21 deletions app/src/mainwindow2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ void MainWindow2::createMenus()
connect(ui->actionImport_Replace_Palette, &QAction::triggered, this, &MainWindow2::openPalette);

//--- Edit Menu ---
connect(mEditor, &Editor::updateBackup, this, &MainWindow2::undoActSetText);
connect(ui->actionUndo, &QAction::triggered, mEditor, &Editor::undo);
connect(ui->actionRedo, &QAction::triggered, mEditor, &Editor::redo);
connect(ui->actionCut, &QAction::triggered, mEditor, &Editor::copyAndCut);
Expand Down Expand Up @@ -425,9 +426,6 @@ void MainWindow2::createMenus()
ui->menuFile->insertMenu(ui->actionSave, mRecentFileMenu);

connect(mRecentFileMenu, &RecentFileMenu::loadRecentFile, this, &MainWindow2::openFile);

connect(ui->menuEdit, &QMenu::aboutToShow, this, &MainWindow2::undoActSetText);
connect(ui->menuEdit, &QMenu::aboutToHide, this, &MainWindow2::undoActSetEnabled);
}

void MainWindow2::setOpacity(int opacity)
Expand Down Expand Up @@ -557,18 +555,6 @@ void MainWindow2::closeEvent(QCloseEvent* event)
m2ndCloseEvent = true;
}

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

void MainWindow2::tabletEvent(QTabletEvent* event)
{
event->ignore();
Expand Down Expand Up @@ -605,6 +591,21 @@ bool MainWindow2::saveAsNewDocument()
return saveObject(fileName);
}

void MainWindow2::openStartupFile(const QString& filename)
{
if (tryRecoverUnsavedProject())
{
return;
}

if (!filename.isEmpty() && openObject(filename))
{
return;
}

loadMostRecent() || tryLoadPreset();
}

void MainWindow2::openFile(const QString& filename)
{
if (maybeSave())
Expand Down Expand Up @@ -656,6 +657,7 @@ bool MainWindow2::openObject(const QString& strFilePath)
progress.setValue(progress.maximum());

updateSaveState();
undoActSetText();

if (!QFileInfo(strFilePath).isWritable())
{
Expand Down Expand Up @@ -1055,6 +1057,7 @@ void MainWindow2::newObject()
closeDialogs();

setWindowTitle(PENCIL_WINDOW_TITLE);
undoActSetText();
}

bool MainWindow2::newObjectFromPresets(int presetIndex)
Expand All @@ -1079,6 +1082,7 @@ bool MainWindow2::newObjectFromPresets(int presetIndex)

setWindowTitle(PENCIL_WINDOW_TITLE);
updateSaveState();
undoActSetText();

return true;
}
Expand Down Expand Up @@ -1331,12 +1335,6 @@ void MainWindow2::undoActSetText()
}
}

void MainWindow2::undoActSetEnabled()
{
ui->actionUndo->setEnabled(true);
ui->actionRedo->setEnabled(true);
}

void MainWindow2::exportPalette()
{
QString filePath = FileDialog::getSaveFileName(this, FileType::PALETTE);
Expand Down Expand Up @@ -1622,6 +1620,7 @@ void MainWindow2::startProjectRecovery(int result)
Q_ASSERT(o);
mEditor->setObject(o);
updateSaveState();
undoActSetText();

const QString title = tr("Recovery Succeeded!");
const QString text = tr("Please save your work immediately to prevent loss of data");
Expand All @@ -1631,6 +1630,7 @@ void MainWindow2::startProjectRecovery(int result)
void MainWindow2::createToolbars()
{
mMainToolbar = addToolBar(tr("Main Toolbar"));
mMainToolbar->setObjectName("mMainToolbar");
mMainToolbar->addAction(ui->actionNew);
mMainToolbar->addAction(ui->actionOpen);
mMainToolbar->addAction(ui->actionSave);
Expand All @@ -1643,13 +1643,15 @@ void MainWindow2::createToolbars()
mMainToolbar->addAction(ui->actionPaste);

mViewToolbar = addToolBar(tr("View Toolbar"));
mViewToolbar->setObjectName("mViewToolbar");
mViewToolbar->addAction(ui->actionZoom_In);
mViewToolbar->addAction(ui->actionZoom_Out);
mViewToolbar->addAction(ui->actionReset_View);
mViewToolbar->addAction(ui->actionHorizontal_Flip);
mViewToolbar->addAction(ui->actionVertical_Flip);

mOverlayToolbar = addToolBar(tr("Overlay Toolbar"));
mOverlayToolbar->setObjectName("mOverlayToolbar");
mOverlayToolbar->addAction(ui->actionGrid);

mToolbars = { mMainToolbar, mViewToolbar, mOverlayToolbar };
Expand Down
5 changes: 1 addition & 4 deletions app/src/mainwindow2.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class MainWindow2 : public QMainWindow

public slots:
void undoActSetText();
void undoActSetEnabled();
void updateSaveState();
void openPegAlignDialog();
void openRepositionDialog();
Expand Down Expand Up @@ -100,21 +99,19 @@ public slots:
void setOpacity(int opacity);
void preferences();

void openStartupFile(const QString& filename);
void openFile(const QString& filename);

void displayMessageBox(const QString& title, const QString& body);
void displayMessageBoxNoTitle(const QString& body);

signals:
void updateRecentFilesList(bool b);

/** Emitted when window regains focus */
void windowActivated();

protected:
void tabletEvent(QTabletEvent*) override;
void closeEvent(QCloseEvent*) override;
void showEvent(QShowEvent*) override;
bool event(QEvent*) override;

private slots:
Expand Down
21 changes: 16 additions & 5 deletions app/src/pencil2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ Status Pencil2D::handleCommandLineOptions()
CommandLineParser parser;
parser.process(arguments());

#ifndef QT_DEBUG
if (isInstanceOpen()) {
return Status::SAFE;
}
#endif

QString inputPath = parser.inputPath();
QStringList outputPaths = parser.outputPaths();

Expand Down Expand Up @@ -102,7 +108,7 @@ bool Pencil2D::isInstanceOpen()
mProcessLock.reset(new QLockFile(appDir.absoluteFilePath("pencil2d-process.lock")));
if (!mProcessLock->tryLock(10))
{
QMessageBox::StandardButton clickedButton = QMessageBox::warning(nullptr, QObject::tr("Warning"), QObject::tr("An instance of Pencil2D is already open. Running multiple instances of Pencil2D simultaneously is not recommended and could potentially result in data loss and other unexpected behavior."), QMessageBox::Close | QMessageBox::Open, QMessageBox::Close);
QMessageBox::StandardButton clickedButton = QMessageBox::warning(nullptr, tr("Warning"), tr("An instance of Pencil2D is already open. Running multiple instances of Pencil2D simultaneously is not recommended and could potentially result in data loss and other unexpected behavior."), QMessageBox::Close | QMessageBox::Open, QMessageBox::Close);
if (clickedButton != QMessageBox::Open)
{
return true;
Expand Down Expand Up @@ -130,6 +136,14 @@ void Pencil2D::installTranslators()
QLocale locale = userLocale.isEmpty() ? QLocale::system() : QLocale(userLocale);
QLocale::setDefault(locale);

#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
// In versions prior to 5.14, Qt's DOM implementation erroneously used
// locale-dependent string conversion for double attributes (QTBUG-80068).
// To work around this, we override the numeric locale category to use the
// C locale.
std::setlocale(LC_NUMERIC, "C");
#endif

std::unique_ptr<QTranslator> qtTranslator(new QTranslator(this));
if (qtTranslator->load(locale, "qt", "_", QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
{
Expand All @@ -151,8 +165,5 @@ void Pencil2D::prepareGuiStartup(const QString& inputPath)
connect(this, &Pencil2D::openFileRequested, mainWindow.get(), &MainWindow2::openFile);
mainWindow->show();

if (!inputPath.isEmpty())
{
mainWindow->openFile(inputPath);
}
mainWindow->openStartupFile(inputPath);
}
29 changes: 13 additions & 16 deletions core_lib/src/structure/filemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ bool FileManager::loadObject(Object* object, const QDomElement& root)
}
else if (element.tagName() == "editor" || element.tagName() == "projectdata")
{
ObjectData* projectData = loadProjectData(element);
object->setData(projectData);
object->setData(loadProjectData(element));
}
else if (element.tagName() == "version")
{
Expand Down Expand Up @@ -384,9 +383,9 @@ Status FileManager::writeToWorkingFolder(const Object* object)
return Status(errorCode, dd);
}

ObjectData* FileManager::loadProjectData(const QDomElement& docElem)
ObjectData FileManager::loadProjectData(const QDomElement& docElem)
{
ObjectData* data = new ObjectData;
ObjectData data;
if (docElem.isNull())
{
return data;
Expand Down Expand Up @@ -468,14 +467,12 @@ QDomElement FileManager::saveProjectData(const ObjectData* data, QDomDocument& x
return rootTag;
}

void FileManager::extractProjectData(const QDomElement& element, ObjectData* data)
void FileManager::extractProjectData(const QDomElement& element, ObjectData& data)
{
Q_ASSERT(data);

QString strName = element.tagName();
if (strName == "currentFrame")
{
data->setCurrentFrame(element.attribute("value").toInt());
data.setCurrentFrame(element.attribute("value").toInt());
}
else if (strName == "currentColor")
{
Expand All @@ -484,11 +481,11 @@ void FileManager::extractProjectData(const QDomElement& element, ObjectData* dat
int b = element.attribute("b", "255").toInt();
int a = element.attribute("a", "255").toInt();

data->setCurrentColor(QColor(r, g, b, a));
data.setCurrentColor(QColor(r, g, b, a));
}
else if (strName == "currentLayer")
{
data->setCurrentLayer(element.attribute("value", "0").toInt());
data.setCurrentLayer(element.attribute("value", "0").toInt());
}
else if (strName == "currentView")
{
Expand All @@ -499,27 +496,27 @@ void FileManager::extractProjectData(const QDomElement& element, ObjectData* dat
double dx = element.attribute("dx", "0").toDouble();
double dy = element.attribute("dy", "0").toDouble();

data->setCurrentView(QTransform(m11, m12, m21, m22, dx, dy));
data.setCurrentView(QTransform(m11, m12, m21, m22, dx, dy));
}
else if (strName == "fps" || strName == "currentFps")
{
data->setFrameRate(element.attribute("value", "12").toInt());
data.setFrameRate(element.attribute("value", "12").toInt());
}
else if (strName == "isLoop")
{
data->setLooping(element.attribute("value", "false") == "true");
data.setLooping(element.attribute("value", "false") == "true");
}
else if (strName == "isRangedPlayback")
{
data->setRangedPlayback((element.attribute("value", "false") == "true"));
data.setRangedPlayback((element.attribute("value", "false") == "true"));
}
else if (strName == "markInFrame")
{
data->setMarkInFrameNumber(element.attribute("value", "0").toInt());
data.setMarkInFrameNumber(element.attribute("value", "0").toInt());
}
else if (strName == "markOutFrame")
{
data->setMarkOutFrameNumber(element.attribute("value", "15").toInt());
data.setMarkOutFrameNumber(element.attribute("value", "15").toInt());
}
}

Expand Down
4 changes: 2 additions & 2 deletions core_lib/src/structure/filemanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ class FileManager : public QObject
Status writeMainXml(const Object* obj, const QString& mainXmlPath, QStringList& filesWritten);
Status writePalette(const Object* obj, const QString& dataFolder, QStringList& filesWritten);

ObjectData* loadProjectData(const QDomElement& element);
ObjectData loadProjectData(const QDomElement& element);
QDomElement saveProjectData(const ObjectData*, QDomDocument& xmlDoc);

void extractProjectData(const QDomElement& element, ObjectData* data);
void extractProjectData(const QDomElement& element, ObjectData& data);
void handleOpenProjectError(Status::ErrorCode, const DebugDetails&);

QString backupPreviousFile(const QString& fileName);
Expand Down
2 changes: 1 addition & 1 deletion core_lib/src/structure/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Layer::Layer(Object* object, LAYER_TYPE eType)

mObject = object;
meType = eType;
mName = QString(QObject::tr("Undefined Layer"));
mName = QString(tr("Undefined Layer"));

mId = object->getUniqueLayerID();
}
Expand Down
2 changes: 1 addition & 1 deletion core_lib/src/structure/layerbitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ GNU General Public License for more details.

LayerBitmap::LayerBitmap(Object* object) : Layer(object, Layer::BITMAP)
{
setName(QObject::tr("Bitmap Layer"));
setName(tr("Bitmap Layer"));
}

LayerBitmap::~LayerBitmap()
Expand Down
2 changes: 1 addition & 1 deletion core_lib/src/structure/layercamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ GNU General Public License for more details.

LayerCamera::LayerCamera(Object* object) : Layer(object, Layer::CAMERA)
{
setName(QObject::tr("Camera Layer"));
setName(tr("Camera Layer"));

QSettings settings(PENCIL2D, PENCIL2D);
mFieldW = settings.value("FieldW").toInt();
Expand Down
2 changes: 1 addition & 1 deletion core_lib/src/structure/layersound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ GNU General Public License for more details.

LayerSound::LayerSound(Object* object) : Layer(object, Layer::SOUND)
{
setName(QObject::tr("Sound Layer"));
setName(tr("Sound Layer"));
}

LayerSound::~LayerSound()
Expand Down
2 changes: 1 addition & 1 deletion core_lib/src/structure/layervector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ GNU General Public License for more details.

LayerVector::LayerVector(Object* object) : Layer(object, Layer::VECTOR)
{
setName(QObject::tr("Vector Layer"));
setName(tr("Vector Layer"));
}

LayerVector::~LayerVector()
Expand Down
Loading

0 comments on commit 7b910c4

Please sign in to comment.