2022-01-10 01:43:41 +01:00
#include "timelinepropertiesdialog.h"
#include "ui_timelinepropertiesdialog.h"
2022-01-11 02:35:26 +01:00
#include <QDebug>
#include <QMessageBox>
2022-01-14 21:21:17 +01:00
#include <QInputDialog>
#include <QMenu>
2022-01-11 02:35:26 +01:00
2022-01-14 21:21:17 +01:00
#include <limits>
#include "models/projecttreemodel.h"
#include "models/timelinemomentsmodel.h"
#include "dialogs/deletemomentsdialog.h"
2022-01-11 02:35:26 +01:00
2022-01-10 01:43:41 +01:00
TimeLinePropertiesDialog :: TimeLinePropertiesDialog ( TimeLine & timeLine , ProjectTreeModel & projectModel , QWidget * parent ) :
QDialog { parent },
2022-01-11 02:35:26 +01:00
m_ui { std :: make_unique < Ui :: TimeLinePropertiesDialog > ()},
m_timeLine { timeLine },
m_projectModel { projectModel },
2022-01-14 21:21:17 +01:00
m_moments { timeLine . moments },
m_momentsModel { std :: make_unique < TimelineMomentsModel > ( m_moments , this )}
2022-01-10 01:43:41 +01:00
{
m_ui -> setupUi ( this );
2022-01-11 02:35:26 +01:00
updateTitle ();
if ( auto button = m_ui -> buttonBox -> button ( QDialogButtonBox :: Ok ))
button -> setIcon ( QIcon { ":/qtgameengine/icons/ok.png" });
if ( auto button = m_ui -> buttonBox -> button ( QDialogButtonBox :: Cancel ))
button -> setIcon ( QIcon { ":/qtgameengine/icons/delete.png" });
m_ui -> lineEditName -> setText ( m_timeLine . name );
m_ui -> listViewMoments -> setModel ( m_momentsModel . get ());
connect ( & m_projectModel , & ProjectTreeModel :: timeLineNameChanged ,
this , & TimeLinePropertiesDialog :: timeLineNameChanged );
2022-01-14 21:21:17 +01:00
connect ( m_momentsModel . get (), & QAbstractItemModel :: modelReset ,
this , & TimeLinePropertiesDialog :: changed );
connect ( m_momentsModel . get (), & QAbstractItemModel :: rowsInserted ,
this , & TimeLinePropertiesDialog :: changed );
connect ( m_momentsModel . get (), & QAbstractItemModel :: dataChanged ,
this , & TimeLinePropertiesDialog :: changed );
connect ( m_momentsModel . get (), & QAbstractItemModel :: rowsRemoved ,
this , & TimeLinePropertiesDialog :: changed );
2022-01-11 02:35:26 +01:00
connect ( m_ui -> lineEditName , & QLineEdit :: textChanged ,
this , & TimeLinePropertiesDialog :: changed );
connect ( m_ui -> pushButtonAdd , & QAbstractButton :: clicked ,
2022-01-14 21:21:17 +01:00
this , & TimeLinePropertiesDialog :: addMoment );
2022-01-11 02:35:26 +01:00
connect ( m_ui -> pushButtonChange , & QAbstractButton :: clicked ,
2022-01-14 21:21:17 +01:00
this , & TimeLinePropertiesDialog :: changeMoment );
2022-01-11 02:35:26 +01:00
connect ( m_ui -> pushButtonDelete , & QAbstractButton :: clicked ,
2022-01-14 21:21:17 +01:00
this , & TimeLinePropertiesDialog :: deleteMoments );
2022-01-11 02:35:26 +01:00
connect ( m_ui -> pushButtonClear , & QAbstractButton :: clicked ,
2022-01-14 21:21:17 +01:00
this , & TimeLinePropertiesDialog :: clearMoments );
2022-01-11 02:35:26 +01:00
connect ( m_ui -> pushButtonShift , & QAbstractButton :: clicked ,
2022-01-14 21:21:17 +01:00
this , & TimeLinePropertiesDialog :: shiftMoments );
2022-01-11 02:35:26 +01:00
connect ( m_ui -> pushButtonDuplicate , & QAbstractButton :: clicked ,
2022-01-14 21:21:17 +01:00
this , & TimeLinePropertiesDialog :: duplicateMoments );
2022-01-11 02:35:26 +01:00
connect ( m_ui -> pushButtonSpread , & QAbstractButton :: clicked ,
2022-01-14 21:21:17 +01:00
this , & TimeLinePropertiesDialog :: spreadMoments );
2022-01-11 02:35:26 +01:00
connect ( m_ui -> pushButtonMerge , & QAbstractButton :: clicked ,
2022-01-14 21:21:17 +01:00
this , & TimeLinePropertiesDialog :: mergeMoments );
2022-01-11 02:35:26 +01:00
connect ( m_ui -> pushButtonShowInformation , & QAbstractButton :: clicked ,
this , & TimeLinePropertiesDialog :: showInformation );
2022-01-14 21:21:17 +01:00
connect ( m_ui -> listViewMoments -> selectionModel (), & QItemSelectionModel :: currentChanged ,
this , & TimeLinePropertiesDialog :: currentMomentChanged );
connect ( m_momentsModel . get (), & QAbstractItemModel :: modelAboutToBeReset ,
m_ui -> listViewMoments , [ listView = m_ui -> listViewMoments ](){
listView -> setCurrentIndex ( QModelIndex {});
});
connect ( m_ui -> listViewMoments , & QListView :: customContextMenuRequested ,
this , & TimeLinePropertiesDialog :: momentsContextMenuRequested );
connect ( m_ui -> actionsWidget , & ActionsContainerWidget :: changed ,
this , & TimeLinePropertiesDialog :: changed );
2022-01-10 01:43:41 +01:00
}
TimeLinePropertiesDialog ::~ TimeLinePropertiesDialog () = default ;
2022-01-11 02:35:26 +01:00
void TimeLinePropertiesDialog :: accept ()
{
if ( ! m_unsavedChanges )
{
QDialog :: reject ();
return ;
}
if ( m_timeLine . name != m_ui -> lineEditName -> text ())
{
if ( ! m_projectModel . rename < TimeLine > ( m_timeLine , m_ui -> lineEditName -> text ()))
{
QMessageBox :: critical ( this , tr ( "Renaming Time Line failed!" ), tr ( "Renaming Time Line failed!" ));
return ;
}
}
2022-01-14 21:21:17 +01:00
m_timeLine . moments = m_moments ;
2022-01-11 02:35:26 +01:00
QDialog :: accept ();
}
void TimeLinePropertiesDialog :: reject ()
{
if ( ! m_unsavedChanges )
{
QDialog :: reject ();
return ;
}
const auto result = QMessageBox :: warning (
this ,
tr ( "The Time Line has been modified." ),
tr ( "Do you want to save your changes?" ),
QMessageBox :: Save | QMessageBox :: Discard | QMessageBox :: Cancel ,
QMessageBox :: Save
);
switch ( result )
{
case QMessageBox :: Save :
accept ();
return ;
case QMessageBox :: Discard :
QDialog :: reject ();
return ;
case QMessageBox :: Cancel :
return ;
default :
qWarning () << "unexpected dialog result" << result ;
}
}
void TimeLinePropertiesDialog :: changed ()
{
if ( ! m_unsavedChanges )
{
m_unsavedChanges = true ;
updateTitle ();
}
}
void TimeLinePropertiesDialog :: timeLineNameChanged ( const TimeLine & timeLine )
{
if ( & timeLine != & m_timeLine )
return ;
{
QSignalBlocker blocker { m_ui -> lineEditName };
m_ui -> lineEditName -> setText ( timeLine . name );
}
updateTitle ();
}
2022-01-14 21:21:17 +01:00
void TimeLinePropertiesDialog :: addMoment ()
{
TimeLine :: moment_key_t newMoment {};
for ( const auto & moment : m_moments )
if ( moment . first >= newMoment )
newMoment = moment . first + 1 ;
bool ok ;
const auto moment = QInputDialog :: getInt ( this , tr ( "Adding a Moment" ), tr ( "Indicate the moment:" ), newMoment , 0 , std :: numeric_limits < TimeLine :: moment_key_t >:: max (), 1 , & ok );
if ( ! ok )
return ;
if ( ! m_momentsModel -> addMoment ( moment ))
QMessageBox :: warning ( this , tr ( "Could not add Moment!" ), tr ( "Could not add Moment!" ));
}
void TimeLinePropertiesDialog :: changeMoment ()
{
const auto index = m_ui -> listViewMoments -> currentIndex ();
if ( ! index . isValid ())
return ;
const auto moment = m_momentsModel -> getMoment ( index );
if ( ! moment )
return ;
bool ok ;
const auto newMoment = QInputDialog :: getInt ( this , tr ( "Changing a Moment" ), tr ( "Indicate the moment:" ), moment -> first , 0 , std :: numeric_limits < TimeLine :: moment_key_t >:: max (), 1 , & ok );
if ( ! ok )
return ;
if ( ! m_momentsModel -> changeMoment ( moment -> first , newMoment ))
QMessageBox :: warning ( this , tr ( "Could not change Moment!" ), tr ( "Could not change Moment!" ));
}
void TimeLinePropertiesDialog :: deleteMoments ()
{
if ( m_moments . empty ())
return ;
std :: optional < TimeLine :: moment_key_t > momentKey ;
if ( const auto index = m_ui -> listViewMoments -> currentIndex (); index . isValid ())
if ( const auto moment = m_momentsModel -> getMoment ( index ); moment )
momentKey = moment -> first ;
DeleteMomentsDialog dialog = momentKey ? DeleteMomentsDialog { * momentKey , * momentKey , this } : DeleteMomentsDialog { this };
if ( dialog . exec () == QDialog :: Accepted )
{
if ( dialog . fromMoment () > dialog . tillMoment ())
{
QMessageBox :: warning ( this , tr ( "Invalid input!" ), tr ( "Invalid input!" ) + " \n\n " + tr ( "From moment cannot be greater than Till moment!" ));
return ;
}
if ( ! m_momentsModel -> removeRows ( dialog . fromMoment (), dialog . tillMoment () - dialog . fromMoment () + 1 ))
QMessageBox :: warning ( this , tr ( "Could not remove Moments!" ), tr ( "Could not remove Moments!" ));
}
}
void TimeLinePropertiesDialog :: clearMoments ()
{
if ( m_moments . empty ())
return ;
if ( QMessageBox :: question ( this , tr ( "Confirm" ), tr ( "Are you sure, that you really want to remove all Moments?" )) != QMessageBox :: Yes )
return ;
if ( ! m_momentsModel -> clearMoments ())
QMessageBox :: warning ( this , tr ( "Could not clear Moments!" ), tr ( "Could not clear Moments!" ));
}
void TimeLinePropertiesDialog :: shiftMoments ()
2022-01-11 02:35:26 +01:00
{
QMessageBox :: warning ( this , tr ( "Not yet implemented" ), tr ( "Not yet implemented" ));
}
2022-01-14 21:21:17 +01:00
void TimeLinePropertiesDialog :: duplicateMoments ()
2022-01-11 02:35:26 +01:00
{
QMessageBox :: warning ( this , tr ( "Not yet implemented" ), tr ( "Not yet implemented" ));
}
2022-01-14 21:21:17 +01:00
void TimeLinePropertiesDialog :: spreadMoments ()
2022-01-11 02:35:26 +01:00
{
QMessageBox :: warning ( this , tr ( "Not yet implemented" ), tr ( "Not yet implemented" ));
}
2022-01-14 21:21:17 +01:00
void TimeLinePropertiesDialog :: mergeMoments ()
2022-01-11 02:35:26 +01:00
{
QMessageBox :: warning ( this , tr ( "Not yet implemented" ), tr ( "Not yet implemented" ));
}
void TimeLinePropertiesDialog :: showInformation ()
{
QMessageBox :: warning ( this , tr ( "Not yet implemented" ), tr ( "Not yet implemented" ));
}
2022-01-14 21:21:17 +01:00
void TimeLinePropertiesDialog :: currentMomentChanged ( const QModelIndex & index )
{
qDebug () << index . isValid () << index . row ();
if ( index . isValid ())
{
if ( auto moment = m_momentsModel -> getMoment ( index ))
m_ui -> actionsWidget -> setActionsContainer ( & moment -> second );
else
goto none ;
}
else
{
none :
m_ui -> actionsWidget -> setActionsContainer ( nullptr );
}
}
void TimeLinePropertiesDialog :: momentsContextMenuRequested ( const QPoint & pos )
{
const auto index = m_ui -> listViewMoments -> indexAt ( pos );
auto moment = index . isValid () ? m_momentsModel -> getMoment ( index ) : nullptr ;
QMenu menu { this };
menu . addAction ( tr ( "&Add Moment" ), this , & TimeLinePropertiesDialog :: addMoment );
menu . addAction ( tr ( "&Change Moment" ), this , & TimeLinePropertiesDialog :: changeMoment ) -> setEnabled ( moment );
menu . addAction ( tr ( "&Delete Moment" ), this , & TimeLinePropertiesDialog :: deleteMoments ) -> setEnabled ( moment );
menu . addSeparator ();
menu . addAction ( tr ( "&Shift Moments" ), this , & TimeLinePropertiesDialog :: shiftMoments ) -> setEnabled ( moment );
menu . addAction ( tr ( "&Merge Moments" ), this , & TimeLinePropertiesDialog :: mergeMoments ) -> setEnabled ( moment );
menu . exec ( m_ui -> listViewMoments -> viewport () -> mapToGlobal ( pos ));
}
2022-01-11 02:35:26 +01:00
void TimeLinePropertiesDialog :: updateTitle ()
{
setWindowTitle ( tr ( "Time Line Properties: %0%1" )
. arg ( m_timeLine . name )
. arg ( m_unsavedChanges ? tr ( "*" ) : QString {})
);
}