17 #include "actioncommands.h"
19 #include <QInputDialog>
20 #include <QMessageBox>
21 #include <QProgressDialog>
22 #include <QApplication>
23 #include <QDesktopServices>
25 #include "pencildef.h"
28 #include "viewmanager.h"
29 #include "layermanager.h"
30 #include "soundmanager.h"
31 #include "playbackmanager.h"
32 #include "preferencemanager.h"
37 #include "layercamera.h"
38 #include "layersound.h"
39 #include "bitmapimage.h"
40 #include "vectorimage.h"
41 #include "soundclip.h"
43 #include "movieexporter.h"
44 #include "filedialogex.h"
45 #include "exportmoviedialog.h"
49 ActionCommands::ActionCommands( QWidget* parent ) : QObject( parent )
54 ActionCommands::~ActionCommands() {}
56 Status ActionCommands::importSound()
58 Layer* layer = mEditor->layers()->currentLayer();
60 NULLReturn( layer, Status::FAIL );
62 if ( layer->type() != Layer::SOUND )
65 msg.setText( tr(
"No sound layer exists as a destination for your import. Create a new sound layer?" ) );
66 msg.addButton( tr(
"Create sound layer" ), QMessageBox::AcceptRole );
67 msg.addButton( tr(
"Don't create layer" ), QMessageBox::RejectRole );
69 int buttonClicked = msg.exec();
70 if ( buttonClicked != QMessageBox::AcceptRole )
77 QString strLayerName = QInputDialog::getText( mParent, tr(
"Layer Properties" ),
78 tr(
"Layer name:" ), QLineEdit::Normal,
79 tr(
"Sound Layer" ), &ok );
80 if ( ok && !strLayerName.isEmpty() )
82 Layer* newLayer = mEditor->layers()->createSoundLayer( strLayerName );
83 mEditor->layers()->setCurrentLayer( newLayer );
92 layer = mEditor->layers()->currentLayer();
94 if ( layer->keyExists( mEditor->currentFrame() ) )
96 QMessageBox::warning(
nullptr,
98 tr(
"A sound clip already exists on this frame! Please select another frame or layer." ) );
103 QString strSoundFile = fileDialog.openFile( FileType::SOUND );
105 Status st = mEditor->sound()->loadSound( layer, mEditor->currentFrame(), strSoundFile );
110 Status ActionCommands::exportMovie()
114 std::vector< std::pair<QString, QSize > > camerasInfo;
115 auto cameraLayers = mEditor->object()->getLayersByType<
LayerCamera >();
118 camerasInfo.push_back( std::make_pair( i->name(), i->getViewSize() ) );
121 auto currLayer = mEditor->layers()->currentLayer();
122 if ( currLayer->type() == Layer::CAMERA )
124 QString strName = currLayer->name();
125 auto it = std::find_if( camerasInfo.begin(), camerasInfo.end(),
126 [strName] ( std::pair<QString, QSize> p )
128 return p.first == strName;
131 Q_ASSERT(it != camerasInfo.end());
133 std::swap( camerasInfo[ 0 ], *it );
136 exportDialog.setCamerasInfo( camerasInfo );
137 exportDialog.setDefaultRange( 1, mEditor->layers()->projectLength() );
139 if ( exportDialog.result() == QDialog::Rejected )
143 QString strMoviePath = exportDialog.getFilePath();
146 desc.strFileName = strMoviePath;
147 desc.startFrame = exportDialog.getStartFrame();
148 desc.endFrame = exportDialog.getEndFrame();
149 desc.fps = mEditor->playback()->fps();
150 desc.exportSize = exportDialog.getExportSize();
151 desc.strCameraName = exportDialog.getSelectedCameraName();
153 QProgressDialog progressDlg;
154 progressDlg.setWindowModality( Qt::WindowModal );
155 progressDlg.setLabelText( tr(
"Exporting movie...") );
156 Qt::WindowFlags eFlags = Qt::Dialog | Qt::WindowTitleHint;
157 progressDlg.setWindowFlags( eFlags );
162 connect( &progressDlg, &QProgressDialog::canceled, [&ex]
167 Status st = ex.run( mEditor->object(), desc, [ &progressDlg ](
float f )
169 progressDlg.setValue( (
int)(f * 100.f) );
170 QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
173 if ( st.ok() && QFile::exists( strMoviePath ) )
175 auto btn = QMessageBox::question( mParent,
177 tr(
"Finished. Open movie now?" ) );
178 if ( btn == QMessageBox::Yes )
180 QDesktopServices::openUrl( QUrl::fromLocalFile( strMoviePath ) );
187 void ActionCommands::ZoomIn()
189 mEditor->view()->scaleUp();
192 void ActionCommands::ZoomOut()
194 mEditor->view()->scaleDown();
197 void ActionCommands::flipSelectionX()
199 bool flipVertical =
false;
200 mEditor->flipSelection(flipVertical);
203 void ActionCommands::flipSelectionY()
205 bool flipVertical =
true;
206 mEditor->flipSelection(flipVertical);
209 void ActionCommands::rotateClockwise()
211 float currentRotation = mEditor->view()->rotation();
212 mEditor->view()->rotate(currentRotation + 15.f);
215 void ActionCommands::rotateCounterClockwise()
217 float currentRotation = mEditor->view()->rotation();
218 mEditor->view()->rotate(currentRotation - 15.f);
221 void ActionCommands::showGrid(
bool bShow )
223 auto prefs = mEditor->preference();
225 prefs->turnOn( SETTING::GRID );
227 prefs->turnOff( SETTING::GRID );
230 void ActionCommands::PlayStop()
233 if ( playback->isPlaying() )
243 void ActionCommands::GotoNextFrame()
245 mEditor->scrubForward();
248 void ActionCommands::GotoPrevFrame()
250 mEditor->scrubBackward();
253 void ActionCommands::GotoNextKeyFrame()
255 mEditor->scrubNextKeyFrame();
258 void ActionCommands::GotoPrevKeyFrame()
260 mEditor->scrubPreviousKeyFrame();
263 void ActionCommands::addNewKey()
265 KeyFrame* key = mEditor->addNewKey();
271 QString strSoundFile = fileDialog.openFile( FileType::SOUND );
273 if ( strSoundFile.isEmpty() )
275 mEditor->removeKey();
278 Status st = mEditor->sound()->loadSound( clip, strSoundFile );
283 void ActionCommands::removeKey()
285 mEditor->removeKey();
287 Layer* layer = mEditor->layers()->currentLayer();
288 if ( layer->keyFrameCount() == 0 )
290 switch ( layer->type() )
295 layer->addNewEmptyKeyAt( 1 );
303 Status ActionCommands::addNewBitmapLayer()
306 QString text = QInputDialog::getText(
nullptr, tr(
"Layer Properties" ),
307 tr(
"Layer name:" ), QLineEdit::Normal,
308 tr(
"Bitmap Layer" ), &ok );
309 if ( ok && !text.isEmpty() )
311 mEditor->layers()->createBitmapLayer( text );
316 Status ActionCommands::addNewVectorLayer()
319 QString text = QInputDialog::getText(
nullptr, tr(
"Layer Properties" ),
320 tr(
"Layer name:" ), QLineEdit::Normal,
321 tr(
"Vector Layer" ), &ok );
322 if ( ok && !text.isEmpty() )
324 mEditor->layers()->createVectorLayer( text );
330 Status ActionCommands::addNewCameraLayer()
333 QString text = QInputDialog::getText(
nullptr, tr(
"Layer Properties" ),
334 tr(
"Layer name:" ), QLineEdit::Normal,
335 tr(
"Camera Layer" ), &ok );
336 if ( ok && !text.isEmpty() )
338 mEditor->layers()->createCameraLayer( text );
345 Status ActionCommands::addNewSoundLayer()
348 QString strLayerName = QInputDialog::getText(
nullptr, tr(
"Layer Properties" ),
349 tr(
"Layer name:" ), QLineEdit::Normal,
350 tr(
"Sound Layer" ), &ok );
351 if ( ok && !strLayerName.isEmpty() )
353 Layer* layer = mEditor->layers()->createSoundLayer( strLayerName );
354 mEditor->layers()->setCurrentLayer( layer );