Pencil2D Animation
Download Community News Docs Contribute
  • Overview
  • Articles
  • Code
  •  
  • Class List
  • Class Index
  • Class Hierarchy
  • Class Members
  • File List
Loading...
Searching...
No Matches
  • app
  • src
importlayersdialog.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 "importlayersdialog.h"
18#include "ui_importlayersdialog.h"
19
20#include <QProgressDialog>
21
22#include "app_util.h"
23#include "filemanager.h"
24#include "filedialog.h"
25#include "fileformat.h"
26#include "layermanager.h"
27#include "soundmanager.h"
28#include "layer.h"
29#include "layersound.h"
30#include "layervector.h"
31#include "soundclip.h"
32
33
34ImportLayersDialog::ImportLayersDialog(QWidget *parent) :
35 QDialog(parent),
36 ui(new Ui::ImportLayersDialog)
37{
38 ui->setupUi(this);
39 connect(ui->btnSelectFile, &QPushButton::clicked, this, &ImportLayersDialog::getFileName);
40 connect(ui->btnImportLayers, &QPushButton::clicked, this, &ImportLayersDialog::importLayers);
41 connect(ui->lwLayers, &QListWidget::itemSelectionChanged, this, &ImportLayersDialog::listWidgetChanged);
42 connect(ui->btnClose, &QPushButton::clicked, this, &ImportLayersDialog::cancel);
43 ui->lwLayers->setSelectionMode(QAbstractItemView::ExtendedSelection);
44 ui->btnImportLayers->setEnabled(false);
45
46 hideQuestionMark(*this);
47}
48
49ImportLayersDialog::~ImportLayersDialog()
50{
51 delete ui;
52}
53
54void ImportLayersDialog::setCore(Editor *editor)
55{
56 mEditor = editor;
57}
58
59void ImportLayersDialog::getFileName()
60{
61 mFileName.clear();
62 ui->lwLayers->clear();
63 mFileName = FileDialog::getOpenFileName(this, FileType::ANIMATION, tr("Choose file"));
64 if (mFileName.isEmpty()) { return; }
65 getLayers();
66}
67
68void ImportLayersDialog::listWidgetChanged()
69{
70 mItemsSelected.clear();
71 for (int i = 0; i < ui->lwLayers->count(); i++)
72 if (ui->lwLayers->item(i)->isSelected())
73 mItemsSelected.append(i);
74
75 if (!mItemsSelected.isEmpty())
76 ui->btnImportLayers->setEnabled(true);
77 else
78 ui->btnImportLayers->setEnabled(false);
79}
80
81void ImportLayersDialog::importLayers()
82{
83 Object* object = mEditor->object();
84 int currentFrame = mEditor->currentFrame();
85 Q_ASSERT(ui->lwLayers->count() == mImportObject->getLayerCount());
86
87 QMap<int, int> importedColors;
88
89 for (const QListWidgetItem* item : ui->lwLayers->selectedItems())
90 {
91 mImportLayer = mImportObject->takeLayer(item->data(Qt::UserRole).toInt());
92 mImportLayer->setName(mEditor->layers()->nameSuggestLayer(item->text()));
93 loadKeyFrames(mImportLayer); // all keyframes of this layer must be in memory
94
95 object->addLayer(mImportLayer);
96
97 if (mImportLayer->type() == Layer::VECTOR)
98 {
99 LayerVector* layerVector = static_cast<LayerVector*>(mImportLayer);
100 for (int i = 0; i < mImportObject->getColorCount(); i++) {
101 if (!layerVector->usesColor(i)) {
102 continue;
103 }
104
105 if (!importedColors.contains(i)) {
106 const ColorRef color = mImportObject->getColor(i);
107 object->addColor(color);
108 importedColors[i] = object->getColorCount() - 1;
109 }
110
111 layerVector->moveColor(i, importedColors[i]);
112 }
113 }
114
115 if (mImportLayer->type() == Layer::SOUND)
116 {
117 LayerSound* layerSound = static_cast<LayerSound*>(mImportLayer);
118 layerSound->foreachKeyFrame([this](KeyFrame* key)
119 {
120 SoundClip* clip = dynamic_cast<SoundClip*>(key);
121 Status st = mEditor->sound()->loadSound(clip, clip->fileName());
122 Q_ASSERT(st.ok());
123 });
124 }
125 }
126 mEditor->object()->modification();
127
128 mImportObject.reset();
129 getLayers();
130 mEditor->scrubTo(currentFrame);
131}
132
133void ImportLayersDialog::cancel()
134{
135 close();
136}
137
138void ImportLayersDialog::getLayers()
139{
140 QProgressDialog progress(tr("Opening document..."), tr("Abort"), 0, 100, this);
141
142 // Don't show progress bar if running without a GUI (aka. when rendering from command line)
143 if (isVisible())
144 {
145 hideQuestionMark(progress);
146 progress.setWindowModality(Qt::WindowModal);
147 progress.show();
148 }
149
150 FileManager fm;
151 connect(&fm, &FileManager::progressChanged, [&progress](int p)
152 {
153 progress.setValue(p);
154 QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
155 });
156 connect(&fm, &FileManager::progressRangeChanged, [&progress](int max)
157 {
158 progress.setRange(0, max + 3);
159 });
160 mImportObject.reset(fm.load(mFileName));
161
162 ui->lwLayers->clear();
163 for (int i = 0; i < mImportObject->getLayerCount(); i++)
164 {
165 const QString layerName = mImportObject->getLayer(i)->name();
166 const int layerId = mImportObject->getLayer(i)->id();
167
168 // Store the layer name as well as layer ID cuz two layers could have the same name
169 QListWidgetItem* item = new QListWidgetItem(layerName);
170 item->setData(Qt::UserRole, layerId);
171 ui->lwLayers->addItem(item);
172 }
173}
174
175void ImportLayersDialog::loadKeyFrames(Layer* importedLayer)
176{
177 // Pencil2D only keeps a small portion of keyframes in the memory initially
178 // Here we need to force load all the keyframes of this layer into memory
179 // Otherwise the keyframe data will lose after mImportObject is deleted
180 importedLayer->foreachKeyFrame([](KeyFrame* k)
181 {
182 k->loadFile();
183 });
184}
ColorRef
Definition: colorref.h:26
Editor
Definition: editor.h:71
FileDialog::getOpenFileName
static QString getOpenFileName(QWidget *parent, FileType fileType, const QString &caption=QString())
Shows a file dialog which allows the user to select a file to open.
Definition: filedialog.cpp:28
FileManager
Definition: filemanager.h:37
ImportLayersDialog
Definition: importlayersdialog.h:29
KeyFrame
Definition: keyframe.h:30
Layer
Definition: layer.h:33
LayerSound
Definition: layersound.h:26
LayerVector
Definition: layervector.h:26
Object
Definition: object.h:42
SoundClip
Definition: soundclip.h:27
Status
Definition: pencilerror.h:40
QAbstractButton::clicked
void clicked(bool checked)
QAbstractItemView::ExtendedSelection
ExtendedSelection
QCoreApplication::processEvents
void processEvents(QEventLoop::ProcessEventsFlags flags)
QDialog
QEventLoop::ExcludeUserInputEvents
ExcludeUserInputEvents
QList::append
void append(const T &value)
QList::clear
void clear()
QList::isEmpty
bool isEmpty() const const
QListWidget::itemSelectionChanged
void itemSelectionChanged()
QListWidgetItem
QListWidgetItem::setData
virtual void setData(int role, const QVariant &value)
QMap
QMap::contains
bool contains(const Key &key) const const
QObject::connect
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject::tr
QString tr(const char *sourceText, const char *disambiguation, int n)
QProgressDialog
QString
QString::clear
void clear()
QString::isEmpty
bool isEmpty() const const
Qt::UserRole
UserRole
Qt::WindowModal
WindowModal
QWidget
QWidget::close
bool close()
QWidget::setupUi
void setupUi(QWidget *widget)
QWidget::isVisible
bool isVisible() const const
Generated on Thu May 8 2025 04:47:53 for Pencil2D by doxygen 1.9.6 based on revision 4513250b1d5b1a3676ec0e67b06b7a885ceaae39