Skip to content

Commit

Permalink
Merge pull request #1261 from davidlamhauge/layer_import
Browse files Browse the repository at this point in the history
Layer import, from *.pclx to active project
  • Loading branch information
candyface authored Sep 28, 2019
2 parents 2c3a285 + 6090fd8 commit 6ff2580
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/app.pro
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ INCLUDEPATH += \
../core_lib/src/external

HEADERS += \
src/importlayersdialog.h \
src/mainwindow2.h \
src/pegbaralignmentdialog.h \
src/shortcutfilter.h \
Expand Down Expand Up @@ -64,6 +65,7 @@ HEADERS += \
src/checkupdatesdialog.h

SOURCES += \
src/importlayersdialog.cpp \
src/main.cpp \
src/mainwindow2.cpp \
src/pegbaralignmentdialog.cpp \
Expand Down Expand Up @@ -94,6 +96,7 @@ SOURCES += \
src/checkupdatesdialog.cpp

FORMS += \
ui/importlayersdialog.ui \
ui/mainwindow2.ui \
ui/pegbaralignmentdialog.ui \
ui/timeline2.ui \
Expand Down
122 changes: 122 additions & 0 deletions app/src/importlayersdialog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include "importlayersdialog.h"
#include "ui_importlayersdialog.h"

#include <QFileDialog>
#include <QProgressDialog>

#include "app_util.h"
#include "filemanager.h"
#include "filedialogex.h"
#include "layermanager.h"
#include "soundmanager.h"
#include "layer.h"
#include "layersound.h"
#include "soundclip.h"


ImportLayersDialog::ImportLayersDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::ImportLayersDialog)
{
ui->setupUi(this);
connect(ui->btnSelectFile, &QPushButton::clicked, this, &ImportLayersDialog::getFileName);
connect(ui->btnImportLayers, &QPushButton::clicked, this, &ImportLayersDialog::importLayers);
connect(ui->lwLayers, &QListWidget::itemSelectionChanged, this, &ImportLayersDialog::listWidgetChanged);
connect(ui->btnClose, &QPushButton::clicked, this, &ImportLayersDialog::cancel);
ui->lwLayers->setSelectionMode(QAbstractItemView::ExtendedSelection);
ui->btnImportLayers->setEnabled(false);
}

ImportLayersDialog::~ImportLayersDialog()
{
delete ui;
}

void ImportLayersDialog::setCore(Editor *editor)
{
mEditor = editor;
mObject = mEditor->object();
}

void ImportLayersDialog::getFileName()
{
mFileName.clear();
ui->lwLayers->clear();
FileDialog fd(this);
mFileName = QFileDialog::getOpenFileName(this, tr("Choose file"),
fd.getLastOpenPath(FileType::ANIMATION),
tr("Pencil Animation file (*.pclx)"));
if (mFileName.isEmpty()) { return; }
getLayers();
for (int i = 0; i < mImportObject->getLayerCount(); i++)
ui->lwLayers->addItem(mImportObject->getLayer(i)->name());
}

void ImportLayersDialog::listWidgetChanged()
{
if (ui->lwLayers->count() > 0)
ui->btnImportLayers->setEnabled(true);
else
ui->btnImportLayers->setEnabled(false);
}

void ImportLayersDialog::importLayers()
{
int currentFrame = mEditor->currentFrame();
for (int i = 0; i < mImportObject->getLayerCount(); i++ )
{
if (ui->lwLayers->item(i)->isSelected())
{
Layer *tmpLayer = mImportObject->findLayerByName(ui->lwLayers->item(i)->text());
if (tmpLayer->type() == Layer::SOUND)
{
LayerSound* layerSound = static_cast<LayerSound*>(tmpLayer);
int count = 0;
while (count < layerSound->getNextKeyFramePosition(count))
{
int newKeyPos = layerSound->getNextKeyFramePosition(count);
SoundClip* clip = new SoundClip;
clip = layerSound->getSoundClipWhichCovers(newKeyPos);
Status st = mEditor->sound()->loadSound(clip, clip->fileName());
count = newKeyPos;
}
mObject->addLayer(layerSound);
}
else
{
mObject->addLayer(tmpLayer);
}
}
}
mEditor->scrubTo(currentFrame);
}

void ImportLayersDialog::cancel()
{
close();
}

void ImportLayersDialog::getLayers()
{
QProgressDialog progress(tr("Opening document..."), tr("Abort"), 0, 100, this);

// Don't show progress bar if running without a GUI (aka. when rendering from command line)
if (isVisible())
{
hideQuestionMark(progress);
progress.setWindowModality(Qt::WindowModal);
progress.show();
}

FileManager fm;
connect(&fm, &FileManager::progressChanged, [&progress](int p)
{
progress.setValue(p);
QApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
});
connect(&fm, &FileManager::progressRangeChanged, [&progress](int max)
{
progress.setRange(0, max + 3);
});
mImportObject = fm.load(mFileName);
}
39 changes: 39 additions & 0 deletions app/src/importlayersdialog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef IMPORTLAYERSDIALOG_H
#define IMPORTLAYERSDIALOG_H

#include <QDialog>
#include "object.h"
#include "editor.h"

namespace Ui {
class ImportLayersDialog;
}

class ImportLayersDialog : public QDialog
{
Q_OBJECT

public:
explicit ImportLayersDialog(QWidget *parent = nullptr);
~ImportLayersDialog();

void setCore(Editor *editor);

public slots:
void getFileName();
void listWidgetChanged();
void importLayers();
void cancel();

private:
Ui::ImportLayersDialog *ui;

void getLayers();

Object *mObject = nullptr;
Object *mImportObject = nullptr;
Editor *mEditor = nullptr;
QString mFileName = "";
};

#endif // IMPORTLAYERSDIALOG_H
9 changes: 9 additions & 0 deletions app/src/mainwindow2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ GNU General Public License for more details.
#include "timeline2.h"
#include "errordialog.h"
#include "importimageseqdialog.h"
#include "importlayersdialog.h"
#include "recentfilemenu.h"
#include "shortcutfilter.h"
#include "app_util.h"
Expand Down Expand Up @@ -243,6 +244,7 @@ void MainWindow2::createMenus()
connect(ui->actionImport_Image, &QAction::triggered, this, &MainWindow2::importImage);
connect(ui->actionImport_ImageSeq, &QAction::triggered, this, &MainWindow2::importImageSequence);
connect(ui->actionImport_ImageSeqNum, &QAction::triggered, this, &MainWindow2::importImageSequenceNumbered);
connect(ui->actionImportLayers_from_pclx, &QAction::triggered, this, &MainWindow2::importLayers);
connect(ui->actionImport_Gif, &QAction::triggered, this, &MainWindow2::importGIF);
connect(ui->actionImport_Movie, &QAction::triggered, this, &MainWindow2::importMovie);

Expand Down Expand Up @@ -972,6 +974,13 @@ void MainWindow2::addLayerByFilename(QString strFilePath)
mTimeLine->updateContent();
}

void MainWindow2::importLayers()
{
ImportLayersDialog *importLayers = new ImportLayersDialog(this);
importLayers->setCore(mEditor);
importLayers->exec();
}

void MainWindow2::importGIF()
{
auto gifDialog = new ImportImageSeqDialog(this, ImportExportDialog::Import, FileType::GIF);
Expand Down
1 change: 1 addition & 0 deletions app/src/mainwindow2.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class MainWindow2 : public QMainWindow
void importImageSequence();
void importImageSequenceNumbered();
void addLayerByFilename(QString strFilePath);
void importLayers();
void importMovie();
void importGIF();

Expand Down
93 changes: 93 additions & 0 deletions app/ui/importlayersdialog.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ImportLayersDialog</class>
<widget class="QDialog" name="ImportLayersDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>267</height>
</rect>
</property>
<property name="windowTitle">
<string>Import Layers from other *.pclx files</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="labSelectFile">
<property name="text">
<string>1. Select PCLX file:</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="btnSelectFile">
<property name="text">
<string>Select File</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="labSelectLayers">
<property name="text">
<string>2. Select layers from file:</string>
</property>
</widget>
</item>
<item>
<widget class="QListWidget" name="lwLayers"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="btnClose">
<property name="text">
<string>Close</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnImportLayers">
<property name="text">
<string>Import layers</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
8 changes: 7 additions & 1 deletion app/ui/mainwindow2.ui
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<x>0</x>
<y>0</y>
<width>800</width>
<height>22</height>
<height>26</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
Expand All @@ -66,6 +66,7 @@
<addaction name="actionImport_Movie"/>
<addaction name="actionImport_ImageSeqNum"/>
<addaction name="actionImport_Gif"/>
<addaction name="actionImportLayers_from_pclx"/>
<addaction name="actionImport_Sound"/>
<addaction name="separator"/>
<addaction name="actionImport_Palette"/>
Expand Down Expand Up @@ -981,6 +982,11 @@
<string>Peg bar Alignment</string>
</property>
</action>
<action name="actionImportLayers_from_pclx">
<property name="text">
<string>Layers from PCLX...</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
Expand Down
8 changes: 8 additions & 0 deletions core_lib/src/structure/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,14 @@ void Object::deleteLayer(Layer* layer)
}
}

void Object::addLayer(Layer *layer)
{
if (layer != nullptr)
{
mLayers.append(layer);
}
}

ColourRef Object::getColour(int index) const
{
ColourRef result(Qt::white, "error");
Expand Down
1 change: 1 addition & 0 deletions core_lib/src/structure/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class Object : public QObject
bool swapLayers(int i, int j);
void deleteLayer(int i);
void deleteLayer(Layer*);
void addLayer(Layer* layer);

template<typename T>
std::vector<T*> getLayersByType() const
Expand Down

0 comments on commit 6ff2580

Please sign in to comment.