Skip to content

Commit

Permalink
Save splitter settings in project files
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchcurtis committed Mar 4, 2018
1 parent 8741540 commit d7b65c2
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 104 deletions.
50 changes: 0 additions & 50 deletions app/applicationsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,56 +177,6 @@ bool ApplicationSettings::defaultGuidesLocked() const
return false;
}

bool ApplicationSettings::defaultSplitScreen() const
{
return true;
}

bool ApplicationSettings::isSplitScreen() const
{
return contains("splitScreen") ? value("splitScreen").toBool() : defaultSplitScreen();
}

void ApplicationSettings::setSplitScreen(bool splitScreen)
{
QVariant existingValue = value("splitScreen");
bool existingBoolValue = defaultSplitScreen();
if (contains("splitScreen")) {
existingBoolValue = existingValue.toBool();
}

if (splitScreen == existingBoolValue)
return;

setValue("splitScreen", splitScreen);
emit splitScreenChanged();
}

bool ApplicationSettings::defaultSplitterLocked() const
{
return true;
}

bool ApplicationSettings::isSplitterLocked() const
{
return contains("splitterLocked") ? value("splitterLocked").toBool() : defaultSplitterLocked();
}

void ApplicationSettings::setSplitterLocked(bool splitterLocked)
{
QVariant existingValue = value("splitterLocked");
bool existingBoolValue = defaultSplitterLocked();
if (contains("splitterLocked")) {
existingBoolValue = existingValue.toBool();
}

if (splitterLocked == existingBoolValue)
return;

setValue("splitterLocked", splitterLocked);
emit splitterLockedChanged();
}

bool ApplicationSettings::defaultScrollZoom() const
{
return true;
Expand Down
12 changes: 0 additions & 12 deletions app/applicationsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class ApplicationSettings : public QSettings
Q_PROPERTY(bool rulersVisible READ areRulersVisible WRITE setRulersVisible NOTIFY rulersVisibleChanged)
Q_PROPERTY(bool guidesVisible READ areGuidesVisible WRITE setGuidesVisible NOTIFY guidesVisibleChanged)
Q_PROPERTY(bool guidesLocked READ areGuidesLocked WRITE setGuidesLocked NOTIFY guidesLockedChanged)
Q_PROPERTY(bool splitScreen READ isSplitScreen WRITE setSplitScreen NOTIFY splitScreenChanged)
Q_PROPERTY(bool splitterLocked READ isSplitterLocked WRITE setSplitterLocked NOTIFY splitterLockedChanged)
Q_PROPERTY(bool scrollZoom READ scrollZoom WRITE setScrollZoom NOTIFY scrollZoomChanged)

Q_PROPERTY(QString quitShortcut READ quitShortcut WRITE setQuitShortcut NOTIFY quitShortcutChanged)
Expand Down Expand Up @@ -105,14 +103,6 @@ class ApplicationSettings : public QSettings
void setGuidesLocked(bool guidesLocked);
bool defaultGuidesLocked() const;

bool defaultSplitScreen() const;
bool isSplitScreen() const;
void setSplitScreen(bool splitScreen);

bool defaultSplitterLocked() const;
bool isSplitterLocked() const;
void setSplitterLocked(bool splitterLocked);

bool defaultScrollZoom() const;
bool scrollZoom() const;
void setScrollZoom(bool scrollZoom);
Expand Down Expand Up @@ -266,8 +256,6 @@ class ApplicationSettings : public QSettings
void rulersVisibleChanged();
void guidesVisibleChanged();
void guidesLockedChanged();
void splitScreenChanged();
void splitterLockedChanged();
void scrollZoomChanged();

void quitShortcutChanged();
Expand Down
69 changes: 48 additions & 21 deletions app/imagecanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ ImageCanvas::ImageCanvas() :
mGridVisible(false),
mGridColour(Qt::black),
mSplitColour(Qt::black),
mSplitScreen(true),
mSplitScreen(false),
mSplitter(this),
mCurrentPane(&mFirstPane),
mFirstHorizontalRuler(new Ruler(Qt::Horizontal, this)),
Expand Down Expand Up @@ -160,11 +160,22 @@ void ImageCanvas::setProject(Project *project)
// New projects or projects that don't have their own Slate extension
// won't have any JSON data.
QJsonObject *cachedProjectJson = project->cachedProjectJson();
bool readPanes = false;
if (cachedProjectJson->contains("firstPane")) {
mFirstPane.read(cachedProjectJson->value("firstPane").toObject());
readPanes = true;
}
if (cachedProjectJson->contains("firstPane")) {
mSecondPane.read(cachedProjectJson->value("secondPane").toObject());
readPanes = true;
}
doSetSplitScreen(cachedProjectJson->value("splitScreen").toBool(false), DontResetPaneSizes);
mSplitter.setEnabled(cachedProjectJson->value("splitterLocked").toBool(false));
if (!readPanes) {
// If there were no panes stored, then the project hasn't been saved yet,
// so we can do what we want with the panes.
setDefaultPaneSizes();
centrePanes();
}

setAcceptedMouseButtons(Qt::AllButtons);
Expand Down Expand Up @@ -504,24 +515,7 @@ bool ImageCanvas::isSplitScreen() const

void ImageCanvas::setSplitScreen(bool splitScreen)
{
if (splitScreen == mSplitScreen)
return;

mSplitScreen = splitScreen;

if (mCurrentPane == &mSecondPane) {
setCurrentPane(&mFirstPane);
}

mFirstPane.setSize(splitScreen ? 0.5 : 1.0);
mSecondPane.setSize(splitScreen ? 0.5 : 0.0);

updateRulerVisibility();
resizeRulers();

update();

emit splitScreenChanged();
doSetSplitScreen(splitScreen, ResetPaneSizes);
}

bool ImageCanvas::scrollZoom() const
Expand Down Expand Up @@ -671,8 +665,6 @@ void ImageCanvas::connectSignals()
this, SLOT(onReadyForWritingToJson(QJsonObject*)));

connect(window(), SIGNAL(activeFocusItemChanged()), this, SLOT(updateWindowCursorShape()));

centrePanes();
}

void ImageCanvas::disconnectSignals()
Expand Down Expand Up @@ -890,6 +882,36 @@ void ImageCanvas::centrePanes(bool respectSceneCentred)
update();
}

void ImageCanvas::doSetSplitScreen(bool splitScreen, ImageCanvas::ResetPaneSizePolicy resetPaneSizePolicy)
{
if (splitScreen == mSplitScreen)
return;

mSplitScreen = splitScreen;

if (mCurrentPane == &mSecondPane) {
setCurrentPane(&mFirstPane);
}

if (resetPaneSizePolicy == ResetPaneSizes) {
setDefaultPaneSizes();
centrePanes();
}

updateRulerVisibility();
resizeRulers();

update();

emit splitScreenChanged();
}

void ImageCanvas::setDefaultPaneSizes()
{
mFirstPane.setSize(mSplitScreen ? 0.5 : 1.0);
mSecondPane.setSize(mSplitScreen ? 0.5 : 0.0);
}

bool ImageCanvas::mouseOverSplitterHandle(const QPoint &mousePos)
{
const QRect splitterRegion(paneWidth(0) - mSplitter.width() / 2, 0, mSplitter.width(), height());
Expand Down Expand Up @@ -1015,6 +1037,11 @@ void ImageCanvas::onReadyForWritingToJson(QJsonObject *projectJson)
QJsonObject secondPaneJson;
mSecondPane.write(secondPaneJson);
(*projectJson)["secondPane"] = secondPaneJson;

if (mSplitScreen)
(*projectJson)["splitScreen"] = true;
if (mSplitter.isEnabled())
(*projectJson)["splitterLocked"] = true;
}

bool ImageCanvas::isPanning() const
Expand Down
6 changes: 6 additions & 0 deletions app/imagecanvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@ protected slots:
void drawGuide(QPainter *painter, const CanvasPane &pane, int paneIndex, const Guide &guide, int guideIndex);
int paneWidth(int index) const;
void centrePanes(bool respectSceneCentred = true);
enum ResetPaneSizePolicy {
DontResetPaneSizes,
ResetPaneSizes
};
void doSetSplitScreen(bool splitScreen, ResetPaneSizePolicy resetPaneSizePolicy);
void setDefaultPaneSizes();
bool mouseOverSplitterHandle(const QPoint &mousePos);

void updateRulerVisibility();
Expand Down
2 changes: 1 addition & 1 deletion app/project.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public slots:
QUrl mUrl;
QTemporaryDir mTempDir;
bool mUsingTempImage;
// Caching the project's json object allows the project to save and shared
// Caching the project's json object allows the project to save and share
// e.g. pane info without having to know about the canvas. This member is
// written to once after loading, and then the readyForWritingToJson()
// signal is emitted when saving to allow the canvas (and anyone else
Expand Down
2 changes: 0 additions & 2 deletions app/qml/ui/ImageTypeCanvas.qml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ ImageCanvas {
rulersVisible: settings.rulersVisible
guidesVisible: settings.guidesVisible
guidesLocked: settings.guidesLocked
splitScreen: settings.splitScreen
splitColour: CanvasColours.splitColour
splitter.enabled: settings.splitScreen && !settings.splitterLocked
splitter.width: 32
scrollZoom: settings.scrollZoom
anchors.fill: parent
Expand Down
2 changes: 0 additions & 2 deletions app/qml/ui/LayeredImageTypeCanvas.qml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ LayeredImageCanvas {
rulersVisible: settings.rulersVisible
guidesVisible: settings.guidesVisible
guidesLocked: settings.guidesLocked
splitScreen: settings.splitScreen
splitColour: CanvasColours.splitColour
splitter.enabled: settings.splitScreen && !settings.splitterLocked
splitter.width: 32
rulerForegroundColour: CanvasColours.rulerForegroundColour
rulerBackgroundColour: CanvasColours.rulerBackgroundColour
Expand Down
10 changes: 5 additions & 5 deletions app/qml/ui/MenuBar.qml
Original file line number Diff line number Diff line change
Expand Up @@ -237,17 +237,17 @@ Platform.MenuBar {
text: qsTr("Split Screen")
enabled: canvas
checkable: true
checked: settings.splitScreen
onTriggered: settings.splitScreen = checked
checked: canvas && canvas.splitScreen
onTriggered: canvas.splitScreen = checked
}

Platform.MenuItem {
objectName: "splitterLockedMenuButton"
text: qsTr("Lock Splitter")
enabled: canvas && settings.splitScreen
enabled: canvas && canvas.splitScreen
checkable: true
checked: settings.splitterLocked
onTriggered: settings.splitterLocked = checked
checked: canvas && canvas.splitter.enabled
onTriggered: canvas.splitter.enabled = checked
}

Platform.MenuItem {
Expand Down
4 changes: 2 additions & 2 deletions app/qml/ui/Shortcuts.qml
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,14 @@ Item {
Shortcut {
objectName: "splitScreenShortcut"
sequence: settings.splitScreenShortcut
onActivated: settings.splitScreen = !settings.splitScreen
onActivated: canvas.splitScreen = !canvas.splitScreen
enabled: canvasHasActiveFocus
}

Shortcut {
objectName: "splitterLockedShortcut"
sequence: settings.splitterLockedShortcut
onActivated: settings.splitterLocked = !settings.splitterLocked
onActivated: canvas.splitter.enabled = !canvas.splitter.enabled
enabled: canvasHasActiveFocus
}

Expand Down
2 changes: 0 additions & 2 deletions app/qml/ui/TilesetTypeCanvas.qml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ TileCanvas {
rulersVisible: settings.rulersVisible
guidesVisible: settings.guidesVisible
guidesLocked: settings.guidesLocked
splitScreen: settings.splitScreen
splitColour: CanvasColours.splitColour
splitter.enabled: settings.splitScreen && !settings.splitterLocked
splitter.width: 32
rulerForegroundColour: CanvasColours.rulerForegroundColour
rulerBackgroundColour: CanvasColours.rulerBackgroundColour
Expand Down
9 changes: 6 additions & 3 deletions tests/testhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ void TestHelper::initTestCase()
// Tests could have failed on the last run, so just enforce the default settings.
app.settings()->setGridVisible(app.settings()->defaultGridVisible());
QVERIFY(app.settings()->isGridVisible());
app.settings()->setSplitScreen(app.settings()->defaultSplitScreen());
QVERIFY(app.settings()->isSplitScreen());
app.settings()->setRulersVisible(false);
QVERIFY(!app.settings()->areRulersVisible());

Expand Down Expand Up @@ -1133,6 +1131,11 @@ bool TestHelper::updateVariables(bool isNewProject, Project::Type newProjectType

canvas = window->findChild<ImageCanvas*>();
VERIFY(canvas);
// The old default was to split the screen,
// and so the tests might be depending on it to be split.
// Also, it's good to ensure that it's tested.
canvas->setSplitScreen(true);
VERIFY(canvas->isSplitScreen());

if (newProjectType == Project::TilesetType) {
tilesetProject = qobject_cast<TilesetProject*>(project);
Expand All @@ -1153,7 +1156,7 @@ bool TestHelper::updateVariables(bool isNewProject, Project::Type newProjectType
}

canvas->forceActiveFocus();
// QTRY_VERIFY(canvas->hasActiveFocus());
// TRY_VERIFY(canvas->hasActiveFocus());

VERIFY(project->hasLoaded());

Expand Down
2 changes: 1 addition & 1 deletion tests/testhelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ private Q_SLOTS:
void initTestCase();
void cleanup();

protected:
void resetCreationErrorSpy();

protected:
enum TestMouseEventType
{
MousePress,
Expand Down
6 changes: 3 additions & 3 deletions tests/tst_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,8 @@ void tst_App::saveAsAndLoad()
setCursorPosInPixels(QPoint(0, 0));
QVERIFY2(zoomTo(5), failureMessage);

// Ensure that the splitter is not locked.
QCOMPARE(canvas->isSplitScreen(), true);

QVERIFY2(setSplitterLocked(false), failureMessage);

// Resize the first pane to make it smaller.
Expand Down Expand Up @@ -1478,13 +1478,13 @@ void tst_App::panes()

// Remove split.
QVERIFY2(triggerSplitScreen(), failureMessage);
QVERIFY(!app.settings()->isSplitScreen());
QVERIFY(!canvas->isSplitScreen());
QCOMPARE(tileCanvas->firstPane()->size(), 1.0);
QCOMPARE(tileCanvas->secondPane()->size(), 0.0);

// Add it back again.
QVERIFY2(triggerSplitScreen(), failureMessage);
QVERIFY(app.settings()->isSplitScreen());
QVERIFY(canvas->isSplitScreen());
QCOMPARE(tileCanvas->firstPane()->size(), 0.5);
QCOMPARE(tileCanvas->secondPane()->size(), 0.5);
}
Expand Down

0 comments on commit d7b65c2

Please sign in to comment.