Fixed potential crash and too much switching to Terrain Brush · mapeditor/tiled@70d41d0 (original) (raw)

`@@ -32,8 +32,10 @@ ToolManager::ToolManager(QObject *parent)

`

32

32

` : QObject(parent)

`

33

33

` , mActionGroup(new QActionGroup(this))

`

34

34

` , mSelectedTool(nullptr)

`

``

35

`+

, mDisabledTool(nullptr)

`

35

36

` , mPreviouslyDisabledTool(nullptr)

`

36

37

` , mMapDocument(nullptr)

`

``

38

`+

, mSelectEnabledToolPending(false)

`

37

39

`{

`

38

40

`mActionGroup->setExclusive(true);

`

39

41

`connect(mActionGroup, SIGNAL(triggered(QAction*)),

`

`@@ -97,6 +99,9 @@ QAction *ToolManager::registerTool(AbstractTool *tool)

`

97

99

`*/

`

98

100

`void ToolManager::selectTool(AbstractTool *tool)

`

99

101

`{

`

``

102

`+

if (mSelectedTool == tool)

`

``

103

`+

return;

`

``

104

+

100

105

`if (tool && !tool->isEnabled()) // Refuse to select disabled tools

`

101

106

`return;

`

102

107

``

`@@ -144,30 +149,38 @@ void ToolManager::toolEnabledChanged(bool enabled)

`

144

149

` }

`

145

150

` }

`

146

151

``

147

``

`-

// Switch to another tool when the current tool gets disabled. This is done

`

148

``

`-

// with a delayed call since we first want all the tools to update their

`

149

``

`-

// enabled state.

`

150

152

`if ((!enabled && tool == mSelectedTool) || (enabled && !mSelectedTool)) {

`

151

``

`-

QMetaObject::invokeMethod(this, "selectEnabledTool",

`

152

``

`-

Qt::QueuedConnection);

`

``

153

`+

if (mSelectedTool) {

`

``

154

`+

mDisabledTool = mSelectedTool;

`

``

155

`+

setSelectedTool(nullptr);

`

``

156

`+

}

`

``

157

+

``

158

`+

// Automatically switch to another enabled tool when the current tool

`

``

159

`+

// gets disabled. This is done with a delayed call since we first want

`

``

160

`+

// all the tools to update their enabled state.

`

``

161

`+

if (!mSelectEnabledToolPending) {

`

``

162

`+

mSelectEnabledToolPending = true;

`

``

163

`+

QMetaObject::invokeMethod(this, "selectEnabledTool",

`

``

164

`+

Qt::QueuedConnection);

`

``

165

`+

}

`

153

166

` }

`

154

167

`}

`

155

168

``

156

169

`void ToolManager::selectEnabledTool()

`

157

170

`{

`

``

171

`+

mSelectEnabledToolPending = false;

`

``

172

+

158

173

`// Avoid changing tools when it's no longer necessary

`

159

174

`if (mSelectedTool && mSelectedTool->isEnabled())

`

160

175

`return;

`

161

176

``

162

``

`-

AbstractTool *currentTool = mSelectedTool;

`

163

``

-

164

177

`// Prefer the tool we switched away from last time

`

165

178

`if (mPreviouslyDisabledTool && mPreviouslyDisabledTool->isEnabled())

`

166

179

`selectTool(mPreviouslyDisabledTool);

`

167

180

`else

`

168

181

`selectTool(firstEnabledTool());

`

169

182

``

170

``

`-

mPreviouslyDisabledTool = currentTool;

`

``

183

`+

mPreviouslyDisabledTool = mDisabledTool;

`

171

184

`}

`

172

185

``

173

186

`AbstractTool *ToolManager::firstEnabledTool() const

`