scene_item_base.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "editor/scene/scene_item_base.h"
  8. #include "editor/scene/scene.h"
  9. #include "lang/lang_keys.h"
  10. #include "ui/widgets/popup_menu.h"
  11. #include "ui/painter.h"
  12. #include "styles/style_editor.h"
  13. #include "styles/style_menu_icons.h"
  14. #include <QGraphicsScene>
  15. #include <QGraphicsSceneHoverEvent>
  16. #include <QGraphicsSceneMouseEvent>
  17. #include <QStyleOptionGraphicsItem>
  18. #include <QtMath>
  19. namespace Editor {
  20. namespace {
  21. constexpr auto kSnapAngle = 45.;
  22. const auto kDuplicateSequence = QKeySequence("ctrl+d");
  23. const auto kFlipSequence = QKeySequence("ctrl+s");
  24. const auto kDeleteSequence = QKeySequence("delete");
  25. constexpr auto kMinSizeRatio = 0.05;
  26. constexpr auto kMaxSizeRatio = 1.00;
  27. auto Normalized(float64 angle) {
  28. return angle
  29. + ((std::abs(angle) < 360) ? 0 : (-360 * (angle < 0 ? -1 : 1)));
  30. }
  31. } // namespace
  32. int NumberedItem::type() const {
  33. return NumberedItem::Type;
  34. }
  35. int NumberedItem::number() const {
  36. return _number;
  37. }
  38. void NumberedItem::setNumber(int number) {
  39. _number = number;
  40. }
  41. NumberedItem::Status NumberedItem::status() const {
  42. return _status;
  43. }
  44. bool NumberedItem::isNormalStatus() const {
  45. return _status == Status::Normal;
  46. }
  47. bool NumberedItem::isUndidStatus() const {
  48. return _status == Status::Undid;
  49. }
  50. bool NumberedItem::isRemovedStatus() const {
  51. return _status == Status::Removed;
  52. }
  53. void NumberedItem::save(SaveState state) {
  54. }
  55. void NumberedItem::restore(SaveState state) {
  56. }
  57. bool NumberedItem::hasState(SaveState state) const {
  58. return false;
  59. }
  60. void NumberedItem::setStatus(Status status) {
  61. if (status != _status) {
  62. _status = status;
  63. setVisible(status == Status::Normal);
  64. }
  65. }
  66. ItemBase::ItemBase(Data data)
  67. : _lastZ(data.zPtr)
  68. , _imageSize(data.imageSize)
  69. , _horizontalSize(data.size) {
  70. setFlags(QGraphicsItem::ItemIsMovable
  71. | QGraphicsItem::ItemIsSelectable
  72. | QGraphicsItem::ItemIsFocusable);
  73. setAcceptHoverEvents(true);
  74. applyData(data);
  75. }
  76. QRectF ItemBase::boundingRect() const {
  77. return innerRect() + _scaledInnerMargins;
  78. }
  79. QRectF ItemBase::contentRect() const {
  80. return innerRect() - _scaledInnerMargins;
  81. }
  82. QRectF ItemBase::innerRect() const {
  83. const auto &hSize = _horizontalSize;
  84. const auto &vSize = _verticalSize;
  85. return QRectF(-hSize / 2, -vSize / 2, hSize, vSize);
  86. }
  87. void ItemBase::paint(
  88. QPainter *p,
  89. const QStyleOptionGraphicsItem *option,
  90. QWidget *) {
  91. if (!(option->state & QStyle::State_Selected)) {
  92. return;
  93. }
  94. PainterHighQualityEnabler hq(*p);
  95. const auto hasFocus = (option->state & QStyle::State_HasFocus);
  96. p->setPen(hasFocus ? _pens.select : _pens.selectInactive);
  97. p->drawRect(innerRect());
  98. p->setPen(hasFocus ? _pens.handle : _pens.handleInactive);
  99. p->setBrush(st::photoEditorItemBaseHandleFg);
  100. p->drawEllipse(rightHandleRect());
  101. p->drawEllipse(leftHandleRect());
  102. }
  103. void ItemBase::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
  104. if (isHandling()) {
  105. const auto mousePos = event->pos();
  106. const auto shift = event->modifiers().testFlag(Qt::ShiftModifier);
  107. const auto isLeft = (_handle == HandleType::Left);
  108. if (!shift) {
  109. // Resize.
  110. const auto p = isLeft ? (mousePos * -1) : mousePos;
  111. const auto dx = int(2.0 * p.x());
  112. const auto dy = int(2.0 * p.y());
  113. prepareGeometryChange();
  114. _horizontalSize = std::clamp(
  115. (dx > dy ? dx : dy),
  116. _sizeLimits.min,
  117. _sizeLimits.max);
  118. updateVerticalSize();
  119. }
  120. // Rotate.
  121. const auto origin = mapToScene(boundingRect().center());
  122. const auto pos = mapToScene(mousePos);
  123. const auto diff = pos - origin;
  124. const auto angle = Normalized((isLeft ? 180 : 0)
  125. + (std::atan2(diff.y(), diff.x()) * 180 / M_PI));
  126. setRotation(shift
  127. ? (base::SafeRound(angle / kSnapAngle) * kSnapAngle)
  128. : angle);
  129. } else {
  130. QGraphicsItem::mouseMoveEvent(event);
  131. }
  132. }
  133. void ItemBase::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
  134. setCursor(isHandling()
  135. ? Qt::ClosedHandCursor
  136. : (handleType(event->pos()) != HandleType::None) && isSelected()
  137. ? Qt::OpenHandCursor
  138. : Qt::ArrowCursor);
  139. QGraphicsItem::hoverMoveEvent(event);
  140. }
  141. void ItemBase::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  142. setZValue((*_lastZ)++);
  143. if (event->button() == Qt::LeftButton) {
  144. _handle = handleType(event->pos());
  145. }
  146. if (isHandling()) {
  147. setCursor(Qt::ClosedHandCursor);
  148. } else {
  149. QGraphicsItem::mousePressEvent(event);
  150. }
  151. }
  152. void ItemBase::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  153. if ((event->button() == Qt::LeftButton) && isHandling()) {
  154. _handle = HandleType::None;
  155. } else {
  156. QGraphicsItem::mouseReleaseEvent(event);
  157. }
  158. }
  159. void ItemBase::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
  160. if (scene()) {
  161. scene()->clearSelection();
  162. setSelected(true);
  163. }
  164. const auto add = [&](
  165. auto base,
  166. const QKeySequence &sequence,
  167. Fn<void()> callback,
  168. const style::icon *icon) {
  169. // TODO: refactor.
  170. const auto sequenceText = QChar('\t')
  171. + sequence.toString(QKeySequence::NativeText);
  172. _menu->addAction(
  173. base(tr::now) + sequenceText,
  174. std::move(callback),
  175. icon);
  176. };
  177. _menu = base::make_unique_q<Ui::PopupMenu>(
  178. nullptr,
  179. st::popupMenuWithIcons);
  180. add(
  181. tr::lng_photo_editor_menu_delete,
  182. kDeleteSequence,
  183. [=] { actionDelete(); },
  184. &st::menuIconDelete);
  185. add(
  186. tr::lng_photo_editor_menu_flip,
  187. kFlipSequence,
  188. [=] { actionFlip(); },
  189. &st::menuIconFlip);
  190. add(
  191. tr::lng_photo_editor_menu_duplicate,
  192. kDuplicateSequence,
  193. [=] { actionDuplicate(); },
  194. &st::menuIconCopy);
  195. _menu->popup(event->screenPos());
  196. }
  197. void ItemBase::performForSelectedItems(Action action) {
  198. if (const auto s = scene()) {
  199. for (const auto item : s->selectedItems()) {
  200. if (const auto base = static_cast<ItemBase*>(item)) {
  201. (base->*action)();
  202. }
  203. }
  204. }
  205. }
  206. void ItemBase::actionFlip() {
  207. setFlip(!flipped());
  208. }
  209. void ItemBase::actionDelete() {
  210. if (const auto s = static_cast<Scene*>(scene())) {
  211. s->removeItem(this);
  212. }
  213. }
  214. void ItemBase::actionDuplicate() {
  215. if (const auto s = static_cast<Scene*>(scene())) {
  216. auto data = generateData();
  217. data.x += int(_horizontalSize / 3);
  218. data.y += int(_verticalSize / 3);
  219. const auto newItem = duplicate(std::move(data));
  220. if (hasFocus()) {
  221. newItem->setFocus();
  222. }
  223. const auto selected = isSelected();
  224. newItem->setSelected(selected);
  225. setSelected(false);
  226. s->addItem(newItem);
  227. }
  228. }
  229. void ItemBase::keyPressEvent(QKeyEvent *e) {
  230. if (e->key() == Qt::Key_Escape) {
  231. if (const auto s = scene()) {
  232. s->clearSelection();
  233. s->clearFocus();
  234. return;
  235. }
  236. }
  237. handleActionKey(e);
  238. }
  239. void ItemBase::handleActionKey(not_null<QKeyEvent*> e) {
  240. const auto matches = [&](const QKeySequence &sequence) {
  241. const auto searchKey = (e->modifiers() | e->key())
  242. & ~(Qt::KeypadModifier | Qt::GroupSwitchModifier);
  243. const auto events = QKeySequence(searchKey);
  244. return sequence.matches(events) == QKeySequence::ExactMatch;
  245. };
  246. if (matches(kDuplicateSequence)) {
  247. performForSelectedItems(&ItemBase::actionDuplicate);
  248. } else if (matches(kDeleteSequence)) {
  249. performForSelectedItems(&ItemBase::actionDelete);
  250. } else if (matches(kFlipSequence)) {
  251. performForSelectedItems(&ItemBase::actionFlip);
  252. }
  253. }
  254. QRectF ItemBase::rightHandleRect() const {
  255. return QRectF(
  256. (_horizontalSize / 2) - (_scaledHandleSize / 2),
  257. 0 - (_scaledHandleSize / 2),
  258. _scaledHandleSize,
  259. _scaledHandleSize);
  260. }
  261. QRectF ItemBase::leftHandleRect() const {
  262. return QRectF(
  263. (-_horizontalSize / 2) - (_scaledHandleSize / 2),
  264. 0 - (_scaledHandleSize / 2),
  265. _scaledHandleSize,
  266. _scaledHandleSize);
  267. }
  268. bool ItemBase::isHandling() const {
  269. return _handle != HandleType::None;
  270. }
  271. float64 ItemBase::size() const {
  272. return _horizontalSize;
  273. }
  274. void ItemBase::updateVerticalSize() {
  275. const auto verticalSize = _horizontalSize * _aspectRatio;
  276. _verticalSize = std::max(
  277. verticalSize,
  278. float64(_sizeLimits.min));
  279. if (verticalSize < _sizeLimits.min) {
  280. _horizontalSize = _verticalSize / _aspectRatio;
  281. }
  282. }
  283. void ItemBase::setAspectRatio(float64 aspectRatio) {
  284. _aspectRatio = aspectRatio;
  285. updateVerticalSize();
  286. }
  287. ItemBase::HandleType ItemBase::handleType(const QPointF &pos) const {
  288. return rightHandleRect().contains(pos)
  289. ? HandleType::Right
  290. : leftHandleRect().contains(pos)
  291. ? HandleType::Left
  292. : HandleType::None;
  293. }
  294. bool ItemBase::flipped() const {
  295. return _flipped;
  296. }
  297. void ItemBase::setFlip(bool value) {
  298. if (_flipped != value) {
  299. performFlip();
  300. _flipped = value;
  301. }
  302. }
  303. int ItemBase::type() const {
  304. return ItemBase::Type;
  305. }
  306. void ItemBase::updateZoom(float64 zoom) {
  307. _scaledHandleSize = st::photoEditorItemHandleSize / zoom;
  308. _scaledInnerMargins = QMarginsF(
  309. _scaledHandleSize,
  310. _scaledHandleSize,
  311. _scaledHandleSize,
  312. _scaledHandleSize) * 0.5;
  313. const auto maxSide = std::max(
  314. _imageSize.width(),
  315. _imageSize.height());
  316. _sizeLimits = {
  317. .min = int(maxSide * kMinSizeRatio),
  318. .max = int(maxSide * kMaxSizeRatio),
  319. };
  320. _horizontalSize = std::clamp(
  321. _horizontalSize,
  322. float64(_sizeLimits.min),
  323. float64(_sizeLimits.max));
  324. updateVerticalSize();
  325. updatePens(QPen(
  326. QBrush(),
  327. 1 / zoom,
  328. Qt::DashLine,
  329. Qt::SquareCap,
  330. Qt::RoundJoin));
  331. }
  332. void ItemBase::performFlip() {
  333. }
  334. void ItemBase::updatePens(QPen pen) {
  335. _pens = {
  336. .select = pen,
  337. .selectInactive = pen,
  338. .handle = pen,
  339. .handleInactive = pen,
  340. };
  341. _pens.select.setColor(Qt::white);
  342. _pens.selectInactive.setColor(Qt::gray);
  343. _pens.handle.setColor(Qt::white);
  344. _pens.handleInactive.setColor(Qt::gray);
  345. _pens.handle.setStyle(Qt::SolidLine);
  346. _pens.handleInactive.setStyle(Qt::SolidLine);
  347. }
  348. ItemBase::Data ItemBase::generateData() const {
  349. return {
  350. .initialZoom = (st::photoEditorItemHandleSize / _scaledHandleSize),
  351. .zPtr = _lastZ,
  352. .size = int(_horizontalSize),
  353. .x = int(scenePos().x()),
  354. .y = int(scenePos().y()),
  355. .flipped = flipped(),
  356. .rotation = int(rotation()),
  357. .imageSize = _imageSize,
  358. };
  359. }
  360. void ItemBase::applyData(const Data &data) {
  361. // _lastZ is const.
  362. // _imageSize is const.
  363. _horizontalSize = data.size;
  364. setPos(data.x, data.y);
  365. setZValue((*_lastZ)++);
  366. setFlip(data.flipped);
  367. setRotation(data.rotation);
  368. updateZoom(data.initialZoom);
  369. update();
  370. }
  371. void ItemBase::save(SaveState state) {
  372. const auto z = zValue();
  373. auto &saved = (state == SaveState::Keep) ? _keeped : _saved;
  374. saved = {
  375. .data = generateData(),
  376. .zValue = z,
  377. .status = status(),
  378. };
  379. }
  380. void ItemBase::restore(SaveState state) {
  381. if (!hasState(state)) {
  382. return;
  383. }
  384. const auto &saved = (state == SaveState::Keep) ? _keeped : _saved;
  385. applyData(saved.data);
  386. setZValue(saved.zValue);
  387. setStatus(saved.status);
  388. }
  389. bool ItemBase::hasState(SaveState state) const {
  390. const auto &saved = (state == SaveState::Keep) ? _keeped : _saved;
  391. return saved.zValue;
  392. }
  393. } // namespace Editor