All Classes Namespaces Functions Variables Enumerations Properties Pages
clipboardmanager.cpp
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2005-2007 Patrick Corrieri & Pascal Naidon
5 Copyright (C) 2012-2020 Matthew Chiawen Chang
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; version 2 of the License.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 */
17 #include "clipboardmanager.h"
18 
19 #include <QClipboard>
20 
21 #include <editor.h>
22 
23 ClipboardManager::ClipboardManager(Editor* editor) : BaseManager(editor, "ClipboardManager")
24 {
25  resetStates();
26 }
27 
28 ClipboardManager::~ClipboardManager()
29 {
30 
31 }
32 
33 void ClipboardManager::setFromSystemClipboard(const QPointF& pos, const Layer* layer)
34 {
35  // We intentially do not call resetStates here because we can only store image changes to the clipboard
36  // otherwise we break pasting for vector.
37 
38  QImage image = mClipboard->image(QClipboard::Clipboard);
39  // Only bitmap is supported currently...
40  if (layer->type() == Layer::BITMAP && !image.isNull()) {
41  mBitmapImage = BitmapImage(pos.toPoint()-QPoint(image.size().width()/2, image.size().height()/2), image);
42  }
43 }
44 
45 void ClipboardManager::copyBitmapImage(BitmapImage* bitmapImage, QRectF selectionRect)
46 {
47  resetStates();
48  Q_ASSERT(bitmapImage != nullptr && bitmapImage->isLoaded());
49 
50  if (!selectionRect.isEmpty())
51  {
52  mBitmapImage = bitmapImage->copy(selectionRect.toRect());
53  }
54  else
55  {
56  mBitmapImage = bitmapImage->copy();
57  }
58 
59  mClipboard->setImage(*mBitmapImage.image());
60 }
61 
63 {
64  resetStates();
65  if (vectorImage == nullptr || vectorImage->isEmpty()) { return; }
66 
67  // FIXME: handle vector selections, ie. independent strokes...
68  mVectorImage = *vectorImage->clone();
69 }
70 
71 void ClipboardManager::copySelectedFrames(const Layer* currentLayer) {
72  resetStates();
73 
74  for (int pos : currentLayer->selectedKeyFramesPositions()) {
75  KeyFrame* keyframe = currentLayer->getKeyFrameAt(pos);
76 
77  Q_ASSERT(keyframe != nullptr);
78 
79  keyframe->loadFile();
80 
81  mFrames.insert(std::make_pair(keyframe->pos(), keyframe->clone()));
82  }
83  mFramesType = currentLayer->type();
84 }
85 
87 {
88  mFrames.clear();
89  mBitmapImage = BitmapImage();
90  mVectorImage = VectorImage();
91  mFramesType = Layer::LAYER_TYPE::UNDEFINED;
92 }
int width() const const
QRect toRect() const const
bool isNull() const const
QList< int > selectedKeyFramesPositions() const
Get selected keyframe positions sorted by position.
Definition: layer.h:71
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: layer.h:38
void copySelectedFrames(const Layer *currentLayer)
Copy selected keyframes of any given layer and remember its type.
bool isEmpty() const const
void setImage(const QImage &image, QClipboard::Mode mode)
QPoint toPoint() const const
QImage image(QClipboard::Mode mode) const const
int height() const const
QSize size() const const
void resetStates()
This should be called before copying and updating the clipboard to ensure no previous state is saved...
Definition: editor.h:55
void copyVectorImage(const VectorImage *vectorImage)
Copy the entire vector image to clipboard, this operation does not yet support partial selections...