Pencil2D Animation
Download Community News Docs Contribute
  • Overview
  • Articles
  • Code
  •  
  • Class List
  • Class Index
  • Class Hierarchy
  • Class Members
  • File List
Loading...
Searching...
No Matches
  • core_lib
  • src
  • managers
clipboardmanager.cpp
1/*
2
3Pencil2D - Traditional Animation Software
4Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5Copyright (C) 2012-2020 Matthew Chiawen Chang
6
7This program is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public License
9as published by the Free Software Foundation; version 2 of the License.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16*/
17#include "clipboardmanager.h"
18
19#include <QClipboard>
20#include <QGuiApplication>
21
22#include <editor.h>
23
24ClipboardManager::ClipboardManager(Editor* editor) : BaseManager(editor, "ClipboardManager")
25{
26 resetStates();
27}
28
29ClipboardManager::~ClipboardManager()
30{
31 for (auto it : mFrames)
32 {
33 KeyFrame* frame = it.second;
34 delete frame;
35 }
36}
37
38void ClipboardManager::setFromSystemClipboard(const QPointF& pos, const Layer* layer)
39{
40 const QClipboard *clipboard = QGuiApplication::clipboard();
41
42 // We intentially do not call resetStates here because we can only store image changes to the clipboard
43 // otherwise we break pasting for vector.
44 // Only bitmap is supported currently...
45 // Only update clipboard data if it was stored by other applications
46 if (layer->type() != Layer::BITMAP || clipboard->ownsClipboard()) {
47 return;
48 }
49
50 QImage image = clipboard->image(QClipboard::Clipboard);
51 if (!image.isNull()) {
52 mBitmapImage = BitmapImage(pos.toPoint()-QPoint(image.size().width()/2, image.size().height()/2), image);
53 }
54}
55
56void ClipboardManager::copyBitmapImage(BitmapImage* bitmapImage, QRectF selectionRect)
57{
58 resetStates();
59 Q_ASSERT(bitmapImage != nullptr && bitmapImage->isLoaded());
60
61 if (!selectionRect.isEmpty())
62 {
63 mBitmapImage = bitmapImage->copy(selectionRect.toRect());
64 }
65 else
66 {
67 mBitmapImage = bitmapImage->copy();
68 }
69
70 QGuiApplication::clipboard()->setImage(*mBitmapImage.image());
71}
72
73void ClipboardManager::copyVectorImage(const VectorImage* vectorImage)
74{
75 resetStates();
76 if (vectorImage == nullptr || vectorImage->isEmpty()) { return; }
77
78 // FIXME: handle vector selections, ie. independent strokes...
79 mVectorImage = *vectorImage->clone();
80}
81
82void ClipboardManager::copySelectedFrames(const Layer* currentLayer)
83{
84 resetStates();
85
86 for (int pos : currentLayer->selectedKeyFramesPositions()) {
87 KeyFrame* keyframe = currentLayer->getKeyFrameAt(pos);
88 Q_ASSERT(keyframe != nullptr);
89
90 KeyFrame* newKeyframe = keyframe->clone();
91 // Unload unmodified keyframes now as they won't ever get unloaded
92 // by activeframepool while in clipboard manager.
93 newKeyframe->unloadFile();
94
95 mFrames.insert(std::make_pair(keyframe->pos(), newKeyframe));
96 }
97 mFramesType = currentLayer->type();
98}
99
100std::map<int, KeyFrame*> ClipboardManager::getClipboardFrames()
101{
102 std::map<int, KeyFrame*> resultMap;
103 for (auto it : mFrames)
104 {
105 resultMap.insert(std::make_pair(it.first, it.second->clone()));
106 }
107 return resultMap;
108}
109
110void ClipboardManager::resetStates()
111{
112 for (auto it : mFrames)
113 {
114 KeyFrame* frame = it.second;
115 delete frame;
116 }
117 mFrames.clear();
118
119 mBitmapImage = BitmapImage();
120 mVectorImage = VectorImage();
121 mFramesType = Layer::LAYER_TYPE::UNDEFINED;
122}
BaseManager
Definition: basemanager.h:29
BitmapImage
Definition: bitmapimage.h:28
ClipboardManager::getClipboardFrames
std::map< int, KeyFrame * > getClipboardFrames()
Return a copy of all clipboard frames keyed by their position.
Definition: clipboardmanager.cpp:100
ClipboardManager::copySelectedFrames
void copySelectedFrames(const Layer *currentLayer)
Copy selected keyframes of any given layer and remember its type.
Definition: clipboardmanager.cpp:82
ClipboardManager::copyVectorImage
void copyVectorImage(const VectorImage *vectorImage)
Copy the entire vector image to clipboard, this operation does not yet support partial selections.
Definition: clipboardmanager.cpp:73
ClipboardManager::resetStates
void resetStates()
This should be called before copying and updating the clipboard to ensure no previous state is saved.
Definition: clipboardmanager.cpp:110
ClipboardManager::copyBitmapImage
void copyBitmapImage(BitmapImage *image, QRectF selectionRect)
Copy bitmap image to clipboard and save its latest position Additionally only a part of the image wil...
Definition: clipboardmanager.cpp:56
Editor
Definition: editor.h:71
KeyFrame
Definition: keyframe.h:30
Layer
Definition: layer.h:33
Layer::selectedKeyFramesPositions
QList< int > selectedKeyFramesPositions() const
Get selected keyframe positions sorted by position.
Definition: layer.h:62
VectorImage
Definition: vectorimage.h:32
QClipboard
QClipboard::Clipboard
Clipboard
QClipboard::image
QImage image(QClipboard::Mode mode) const const
QClipboard::ownsClipboard
bool ownsClipboard() const const
QClipboard::setImage
void setImage(const QImage &image, QClipboard::Mode mode)
QGuiApplication::clipboard
QClipboard * clipboard()
QImage
QImage::isNull
bool isNull() const const
QImage::size
QSize size() const const
QPoint
QPointF
QPointF::toPoint
QPoint toPoint() const const
QRectF
QRectF::isEmpty
bool isEmpty() const const
QRectF::toRect
QRect toRect() const const
QSize::height
int height() const const
QSize::width
int width() const const
Generated on Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39