New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Selection of underlying objects #1494
Changes from 16 commits
04678b5
c7c7863
19e5c2a
37481ce
e4a7d6a
85a4260
874fc7e
cf1117f
cea4d0a
9c5dcfb
ff0a8ed
5a0cf89
6eb96a3
344c0e9
250125d
5459383
f1c2fa2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ | |
#include <QKeyEvent> | ||
#include <QTransform> | ||
#include <QUndoStack> | ||
#include <QMenu> | ||
|
||
#include <cmath> | ||
|
||
|
@@ -533,6 +534,42 @@ void ObjectSelectionTool::mousePressed(QGraphicsSceneMouseEvent *event) | |
|
||
break; | ||
} | ||
case Qt::RightButton: | ||
if (event->modifiers() & Qt::AltModifier) { | ||
QList<MapObjectItem*> underlyingObjects = objectItemsAt(event->scenePos()); | ||
if (underlyingObjects.empty()) | ||
break; | ||
QMenu selectUnderlyingMenu; | ||
|
||
for (int levelNum = 0; levelNum < underlyingObjects.size(); ++levelNum) { | ||
const QString& objectName = underlyingObjects[levelNum]->mapObject()->name(); | ||
QString actionName = QLatin1String(levelNum < 9 ? "&" : "") + tr("%n) ", "", levelNum + 1) | ||
+ (objectName.isEmpty() ? tr("Unnamed object") : objectName); | ||
QAction *action = selectUnderlyingMenu.addAction(actionName); | ||
action->setData(QVariant::fromValue(underlyingObjects[levelNum])); | ||
} | ||
|
||
QAction *action = selectUnderlyingMenu.exec(event->screenPos()); | ||
|
||
if (!action) | ||
break; | ||
|
||
if (MapObjectItem* objectToBeSelected = action->data().value<MapObjectItem*>()) { | ||
auto selection = mapScene()->selectedObjectItems(); | ||
if (event->modifiers() & (Qt::ShiftModifier | Qt::ControlModifier)) { | ||
if (selection.contains(objectToBeSelected)) | ||
selection.remove(objectToBeSelected); | ||
else | ||
selection.insert(objectToBeSelected); | ||
} else { | ||
selection.clear(); | ||
selection.insert(objectToBeSelected); | ||
} | ||
mapScene()->setSelectedObjectItems(selection); | ||
} | ||
} else | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coding style: if some if/else chain has brackets somewhere, it should have them everywhere. Please add |
||
AbstractObjectTool::mousePressed(event); | ||
break; | ||
default: | ||
AbstractObjectTool::mousePressed(event); | ||
break; | ||
|
@@ -551,11 +588,24 @@ void ObjectSelectionTool::mouseReleased(QGraphicsSceneMouseEvent *event) | |
break; | ||
} | ||
const Qt::KeyboardModifiers modifiers = event->modifiers(); | ||
QSet<MapObjectItem*> selection = mapScene()->selectedObjectItems(); | ||
if (modifiers & Qt::AltModifier) { | ||
auto underlyingObjects = objectItemsAt(event->scenePos()); | ||
if (underlyingObjects.isEmpty()) | ||
break; | ||
|
||
int lastSelectedIndex = -1; | ||
for (auto selected : selection) | ||
lastSelectedIndex = std::max(lastSelectedIndex, underlyingObjects.indexOf(selected)); | ||
do lastSelectedIndex = (lastSelectedIndex + 1) % underlyingObjects.size(); | ||
while (selection.contains(underlyingObjects.at(lastSelectedIndex)) | ||
&& lastSelectedIndex != underlyingObjects.size() - 1); | ||
mClickedObjectItem = underlyingObjects.at(lastSelectedIndex); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was something wrong with the loop I wrote earlier? I think it was easier to understand and more efficient than this approach. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't support a last->first->second iteration. If the bottom part of the stack is Shift-selected, your code always point at the topmost element. |
||
} | ||
if (mClickedObjectItem) { | ||
QSet<MapObjectItem*> selection = mapScene()->selectedObjectItems(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't use the |
||
if (modifiers & (Qt::ShiftModifier | Qt::ControlModifier)) { | ||
if (selection.contains(mClickedObjectItem)) | ||
selection.remove(mClickedObjectItem); | ||
if (!(modifiers & Qt::AltModifier) && selection.contains(mClickedObjectItem)) | ||
selection.remove(mClickedObjectItem);// Removal is not supported in alt+click mode | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why you would block removal. I don't think there is a negative side-effect to it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Otherwise, if the whole stack is selected, it select and unselect the same object in a cycle. I think it looks buggy |
||
else | ||
selection.insert(mClickedObjectItem); | ||
mapScene()->setSelectedObjectItems(selection); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any better way to construct a
"%d) "
string in this case? I think it doesn't require any translation, so can be made withouttr
.And how to properly update a .ts file? I can provide a russian translation for strings in my code (now it is only "Unnamed object")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mostly just make it a translatable string anyway, because you never know (especially since some languages have different writing directions). But since this number does not relate to a plural form, you should not use the plural form override of
tr
.I would probably just do:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, but what about a .ts file update? Can it be done manually or only via lupdate?