2020-12-05 13:08:24 +01:00
# include "pkg_install_dialog.h"
2020-11-17 17:18:01 +01:00
# include "game_compatibility.h"
2021-01-12 13:15:14 +01:00
# include "numbered_widget_item.h"
2021-01-12 13:16:53 +01:00
# include "richtext_item_delegate.h"
2020-01-03 19:27:47 +01:00
# include <QDialogButtonBox>
# include <QPushButton>
# include <QFileInfo>
2020-02-22 20:42:49 +01:00
# include <QHBoxLayout>
2020-01-03 19:27:47 +01:00
# include <QLabel>
# include <QToolButton>
2020-11-17 17:18:01 +01:00
enum Roles
{
FullPathRole = Qt : : UserRole + 0 ,
2021-01-12 13:15:14 +01:00
ChangelogRole = Qt : : UserRole + 1 ,
TitleRole = Qt : : UserRole + 2 ,
TitleIdRole = Qt : : UserRole + 3 ,
VersionRole = Qt : : UserRole + 4 ,
2020-11-17 17:18:01 +01:00
} ;
pkg_install_dialog : : pkg_install_dialog ( const QStringList & paths , game_compatibility * compat , QWidget * parent )
2020-01-03 19:27:47 +01:00
: QDialog ( parent )
{
m_dir_list = new QListWidget ( this ) ;
2021-01-12 13:16:53 +01:00
m_dir_list - > setItemDelegate ( new richtext_item_delegate ( m_dir_list - > itemDelegate ( ) ) ) ;
2020-01-03 19:27:47 +01:00
for ( const QString & path : paths )
{
2020-11-17 17:18:01 +01:00
const compat : : package_info info = game_compatibility : : GetPkgInfo ( path , compat ) ;
2021-03-21 17:18:25 +01:00
if ( ! info . is_valid )
{
continue ;
}
2021-01-12 13:16:01 +01:00
const QFileInfo file_info ( path ) ;
2020-11-17 17:18:01 +01:00
2020-11-25 18:14:42 +01:00
// We have to build our complicated localized string in some annoying manner
QString accumulated_info ;
2020-11-17 17:18:01 +01:00
QString tooltip ;
2020-11-25 18:14:42 +01:00
2021-01-12 13:16:01 +01:00
const auto append_comma = [ & accumulated_info ] ( )
{
if ( ! accumulated_info . isEmpty ( ) )
{
accumulated_info + = " , " ;
}
} ;
2020-11-25 18:14:42 +01:00
if ( ! info . title_id . isEmpty ( ) )
{
accumulated_info = info . title_id ;
}
2020-11-25 19:04:12 +01:00
if ( info . type ! = compat : : package_type : : other )
2020-11-25 18:14:42 +01:00
{
2021-01-12 13:16:01 +01:00
append_comma ( ) ;
2020-11-25 18:14:42 +01:00
2020-11-25 19:04:12 +01:00
if ( info . type = = compat : : package_type : : dlc )
2020-11-25 18:14:42 +01:00
{
accumulated_info + = tr ( " DLC " , " Package type info (DLC) " ) ;
}
else
{
accumulated_info + = tr ( " Update " , " Package type info (Update) " ) ;
}
}
else if ( ! info . local_cat . isEmpty ( ) )
{
2021-01-12 13:16:01 +01:00
append_comma ( ) ;
2020-11-25 18:14:42 +01:00
accumulated_info + = tr ( " %0 " , " Package type info " ) . arg ( info . local_cat ) ;
}
if ( ! info . version . isEmpty ( ) )
{
2021-01-12 13:16:01 +01:00
append_comma ( ) ;
2020-11-25 18:14:42 +01:00
accumulated_info + = tr ( " v.%0 " , " Version info " ) . arg ( info . version ) ;
}
2020-11-17 17:18:01 +01:00
if ( info . changelog . isEmpty ( ) )
{
2020-11-25 18:14:42 +01:00
tooltip = tr ( " No info " , " Changelog info placeholder " ) ;
2020-11-17 17:18:01 +01:00
}
else
{
2020-11-25 18:14:42 +01:00
tooltip = tr ( " Changelog: \n \n %0 " , " Changelog info " ) . arg ( info . changelog ) ;
2020-11-17 17:18:01 +01:00
}
2021-01-12 13:16:01 +01:00
append_comma ( ) ;
accumulated_info + = file_info . fileName ( ) ;
2020-11-17 17:18:01 +01:00
2021-01-12 13:16:01 +01:00
const QString text = tr ( " <b>%0</b> (%2) " , " Package text " ) . arg ( info . title . simplified ( ) ) . arg ( accumulated_info ) ;
2020-11-17 17:18:01 +01:00
QListWidgetItem * item = new numbered_widget_item ( text , m_dir_list ) ;
item - > setData ( Roles : : FullPathRole , info . path ) ;
item - > setData ( Roles : : ChangelogRole , info . changelog ) ;
item - > setData ( Roles : : TitleRole , info . title ) ;
item - > setData ( Roles : : TitleIdRole , info . title_id ) ;
item - > setData ( Roles : : VersionRole , info . version ) ;
item - > setToolTip ( tooltip ) ;
2020-01-03 19:27:47 +01:00
item - > setFlags ( item - > flags ( ) | Qt : : ItemIsUserCheckable ) ;
item - > setCheckState ( Qt : : Checked ) ;
}
m_dir_list - > sortItems ( ) ;
m_dir_list - > setCurrentRow ( 0 ) ;
m_dir_list - > setMinimumWidth ( ( m_dir_list - > sizeHintForColumn ( 0 ) * 125 ) / 100 ) ;
// Create buttons
QDialogButtonBox * buttons = new QDialogButtonBox ( QDialogButtonBox : : Cancel | QDialogButtonBox : : Ok ) ;
buttons - > button ( QDialogButtonBox : : Ok ) - > setText ( tr ( " Install " ) ) ;
buttons - > button ( QDialogButtonBox : : Ok ) - > setDefault ( true ) ;
2020-11-17 17:18:01 +01:00
connect ( buttons , & QDialogButtonBox : : clicked , this , [ this , buttons ] ( QAbstractButton * button )
2020-01-03 19:27:47 +01:00
{
if ( button = = buttons - > button ( QDialogButtonBox : : Ok ) )
{
accept ( ) ;
}
else if ( button = = buttons - > button ( QDialogButtonBox : : Cancel ) )
{
reject ( ) ;
}
} ) ;
2020-11-17 17:18:01 +01:00
connect ( m_dir_list , & QListWidget : : itemChanged , this , [ this , buttons ] ( QListWidgetItem * )
2020-01-03 19:27:47 +01:00
{
bool any_checked = false ;
for ( int i = 0 ; i < m_dir_list - > count ( ) ; i + + )
{
if ( m_dir_list - > item ( i ) - > checkState ( ) = = Qt : : Checked )
{
any_checked = true ;
break ;
}
}
buttons - > button ( QDialogButtonBox : : Ok ) - > setEnabled ( any_checked ) ;
} ) ;
QToolButton * move_up = new QToolButton ;
move_up - > setArrowType ( Qt : : UpArrow ) ;
move_up - > setToolTip ( tr ( " Move selected item up " ) ) ;
2020-11-17 17:18:01 +01:00
connect ( move_up , & QToolButton : : clicked , this , [ this ] ( ) { MoveItem ( - 1 ) ; } ) ;
2020-01-03 19:27:47 +01:00
QToolButton * move_down = new QToolButton ;
move_down - > setArrowType ( Qt : : DownArrow ) ;
move_down - > setToolTip ( tr ( " Move selected item down " ) ) ;
2020-11-17 17:18:01 +01:00
connect ( move_down , & QToolButton : : clicked , this , [ this ] ( ) { MoveItem ( 1 ) ; } ) ;
2020-01-03 19:27:47 +01:00
QHBoxLayout * hbox = new QHBoxLayout ;
hbox - > addStretch ( ) ;
hbox - > addWidget ( move_up ) ;
hbox - > addWidget ( move_down ) ;
QLabel * description = new QLabel ( tr ( " You are about to install multiple packages. \n Reorder and/or exclude them if needed, then click \" Install \" to proceed. " ) ) ;
QVBoxLayout * vbox = new QVBoxLayout ;
vbox - > addWidget ( description ) ;
vbox - > addLayout ( hbox ) ;
vbox - > addWidget ( m_dir_list ) ;
vbox - > addWidget ( buttons ) ;
setLayout ( vbox ) ;
setWindowTitle ( tr ( " Batch PKG Installation " ) ) ;
setObjectName ( " pkg_install_dialog " ) ;
}
2021-04-07 23:05:18 +02:00
void pkg_install_dialog : : MoveItem ( int offset ) const
2020-01-03 19:27:47 +01:00
{
const int src_index = m_dir_list - > currentRow ( ) ;
const int dest_index = src_index + offset ;
if ( src_index > = 0 & & src_index < m_dir_list - > count ( ) & &
dest_index > = 0 & & dest_index < m_dir_list - > count ( ) )
{
QListWidgetItem * item = m_dir_list - > takeItem ( src_index ) ;
m_dir_list - > insertItem ( dest_index , item ) ;
m_dir_list - > setCurrentItem ( item ) ;
}
}
2020-11-17 17:18:01 +01:00
std : : vector < compat : : package_info > pkg_install_dialog : : GetPathsToInstall ( ) const
2020-01-03 19:27:47 +01:00
{
2020-11-17 17:18:01 +01:00
std : : vector < compat : : package_info > result ;
2020-01-03 19:27:47 +01:00
for ( int i = 0 ; i < m_dir_list - > count ( ) ; i + + )
{
const QListWidgetItem * item = m_dir_list - > item ( i ) ;
2020-11-17 17:18:01 +01:00
if ( item & & item - > checkState ( ) = = Qt : : Checked )
2020-01-03 19:27:47 +01:00
{
2020-11-17 17:18:01 +01:00
compat : : package_info info ;
info . path = item - > data ( Roles : : FullPathRole ) . toString ( ) ;
info . title = item - > data ( Roles : : TitleRole ) . toString ( ) ;
info . title_id = item - > data ( Roles : : TitleIdRole ) . toString ( ) ;
info . changelog = item - > data ( Roles : : ChangelogRole ) . toString ( ) ;
info . version = item - > data ( Roles : : VersionRole ) . toString ( ) ;
result . push_back ( info ) ;
2020-01-03 19:27:47 +01:00
}
}
return result ;
}