mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Added shortcuts for actions of items in photo editor.
This commit is contained in:
parent
f936e484cc
commit
2a86ce596d
2 changed files with 93 additions and 26 deletions
|
@ -22,6 +22,10 @@ namespace {
|
||||||
|
|
||||||
constexpr auto kSnapAngle = 45.;
|
constexpr auto kSnapAngle = 45.;
|
||||||
|
|
||||||
|
const auto kDuplicateSequence = QKeySequence("ctrl+d");
|
||||||
|
const auto kFlipSequence = QKeySequence("ctrl+s");
|
||||||
|
const auto kDeleteSequence = QKeySequence("delete");
|
||||||
|
|
||||||
auto Normalized(float64 angle) {
|
auto Normalized(float64 angle) {
|
||||||
return angle
|
return angle
|
||||||
+ ((std::abs(angle) < 360) ? 0 : (-360 * (angle < 0 ? -1 : 1)));
|
+ ((std::abs(angle) < 360) ? 0 : (-360 * (angle < 0 ? -1 : 1)));
|
||||||
|
@ -175,41 +179,96 @@ void ItemBase::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void ItemBase::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
|
void ItemBase::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
|
||||||
if (!isSelected() && scene()) {
|
if (scene()) {
|
||||||
scene()->clearSelection();
|
scene()->clearSelection();
|
||||||
setSelected(true);
|
setSelected(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const auto add = [&](
|
||||||
|
auto base,
|
||||||
|
const QKeySequence &sequence,
|
||||||
|
Fn<void()> callback) {
|
||||||
|
// TODO: refactor.
|
||||||
|
const auto sequenceText = QChar('\t')
|
||||||
|
+ sequence.toString(QKeySequence::NativeText);
|
||||||
|
_menu->addAction(base(tr::now) + sequenceText, std::move(callback));
|
||||||
|
};
|
||||||
|
|
||||||
_menu = base::make_unique_q<Ui::PopupMenu>(nullptr);
|
_menu = base::make_unique_q<Ui::PopupMenu>(nullptr);
|
||||||
_menu->addAction(tr::lng_photo_editor_menu_delete(tr::now), [=] {
|
add(
|
||||||
if (const auto s = static_cast<Scene*>(scene())) {
|
tr::lng_photo_editor_menu_delete,
|
||||||
s->removeItem(this);
|
kDeleteSequence,
|
||||||
}
|
[=] { actionDelete(); });
|
||||||
});
|
add(
|
||||||
_menu->addAction(tr::lng_photo_editor_menu_flip(tr::now), [=] {
|
tr::lng_photo_editor_menu_flip,
|
||||||
setFlip(!flipped());
|
kFlipSequence,
|
||||||
});
|
[=] { actionFlip(); });
|
||||||
_menu->addAction(tr::lng_photo_editor_menu_duplicate(tr::now), [=] {
|
add(
|
||||||
if (const auto s = static_cast<Scene*>(scene())) {
|
tr::lng_photo_editor_menu_duplicate,
|
||||||
const auto newItem = duplicate(
|
kDuplicateSequence,
|
||||||
_zoom.value(),
|
[=] { actionDuplicate(); });
|
||||||
_lastZ,
|
|
||||||
_horizontalSize,
|
|
||||||
scenePos().x() + _horizontalSize / 3,
|
|
||||||
scenePos().y() + _verticalSize / 3);
|
|
||||||
newItem->setFlip(flipped());
|
|
||||||
newItem->setRotation(rotation());
|
|
||||||
s->clearSelection();
|
|
||||||
newItem->setSelected(true);
|
|
||||||
s->addItem(newItem);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
_menu->popup(event->screenPos());
|
_menu->popup(event->screenPos());
|
||||||
}
|
}
|
||||||
|
|
||||||
int ItemBase::type() const {
|
void ItemBase::performForSelectedItems(Action action) {
|
||||||
return Type;
|
if (const auto s = scene()) {
|
||||||
|
for (const auto item : s->selectedItems()) {
|
||||||
|
if (const auto base = static_cast<ItemBase*>(item)) {
|
||||||
|
(base->*action)();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ItemBase::actionFlip() {
|
||||||
|
setFlip(!flipped());
|
||||||
|
}
|
||||||
|
|
||||||
|
void ItemBase::actionDelete() {
|
||||||
|
if (const auto s = static_cast<Scene*>(scene())) {
|
||||||
|
s->removeItem(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ItemBase::actionDuplicate() {
|
||||||
|
if (const auto s = static_cast<Scene*>(scene())) {
|
||||||
|
const auto newItem = duplicate(
|
||||||
|
_zoom.value(),
|
||||||
|
_lastZ,
|
||||||
|
_horizontalSize,
|
||||||
|
scenePos().x() + _horizontalSize / 3,
|
||||||
|
scenePos().y() + _verticalSize / 3);
|
||||||
|
newItem->setFlip(flipped());
|
||||||
|
newItem->setRotation(rotation());
|
||||||
|
if (hasFocus()) {
|
||||||
|
newItem->setFocus();
|
||||||
|
}
|
||||||
|
const auto selected = isSelected();
|
||||||
|
newItem->setSelected(selected);
|
||||||
|
setSelected(false);
|
||||||
|
s->addItem(newItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ItemBase::keyPressEvent(QKeyEvent *event) {
|
||||||
|
handleActionKey(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ItemBase::handleActionKey(not_null<QKeyEvent*> e) {
|
||||||
|
const auto matches = [&](const QKeySequence &sequence) {
|
||||||
|
const auto searchKey = (e->modifiers() | e->key())
|
||||||
|
& ~(Qt::KeypadModifier | Qt::GroupSwitchModifier);
|
||||||
|
const auto events = QKeySequence(searchKey);
|
||||||
|
return sequence.matches(events) == QKeySequence::ExactMatch;
|
||||||
|
};
|
||||||
|
if (matches(kDuplicateSequence)) {
|
||||||
|
performForSelectedItems(&ItemBase::actionDuplicate);
|
||||||
|
} else if (matches(kDeleteSequence)) {
|
||||||
|
performForSelectedItems(&ItemBase::actionDelete);
|
||||||
|
} else if (matches(kFlipSequence)) {
|
||||||
|
performForSelectedItems(&ItemBase::actionFlip);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QRectF ItemBase::rightHandleRect() const {
|
QRectF ItemBase::rightHandleRect() const {
|
||||||
|
|
|
@ -61,6 +61,13 @@ protected:
|
||||||
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
|
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
|
||||||
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
|
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
|
||||||
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
|
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
|
||||||
|
void keyPressEvent(QKeyEvent *event) override;
|
||||||
|
|
||||||
|
using Action = void(ItemBase::*)();
|
||||||
|
void performForSelectedItems(Action action);
|
||||||
|
void actionFlip();
|
||||||
|
void actionDelete();
|
||||||
|
void actionDuplicate();
|
||||||
|
|
||||||
QRectF contentRect() const;
|
QRectF contentRect() const;
|
||||||
QRectF innerRect() const;
|
QRectF innerRect() const;
|
||||||
|
@ -83,6 +90,7 @@ private:
|
||||||
bool isHandling() const;
|
bool isHandling() const;
|
||||||
void updateVerticalSize();
|
void updateVerticalSize();
|
||||||
void updatePens(QPen pen);
|
void updatePens(QPen pen);
|
||||||
|
void handleActionKey(not_null<QKeyEvent*> e);
|
||||||
|
|
||||||
const std::shared_ptr<float64> _lastZ;
|
const std::shared_ptr<float64> _lastZ;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue