2022-01-14 08:43:42 +00:00
# include "ModDownloadDialog.h"
# include <BaseVersion.h>
# include <icons/IconList.h>
# include <InstanceList.h>
# include "ProgressDialog.h"
2022-03-14 13:50:49 +00:00
# include "ReviewMessageBox.h"
2022-01-14 08:43:42 +00:00
# include <QLayout>
# include <QPushButton>
# include <QValidator>
# include <QDialogButtonBox>
# include "ui/widgets/PageContainer.h"
# include "ui/pages/modplatform/modrinth/ModrinthPage.h"
# include "ModDownloadTask.h"
2022-01-14 19:22:15 +00:00
ModDownloadDialog : : ModDownloadDialog ( const std : : shared_ptr < ModFolderModel > & mods , QWidget * parent ,
BaseInstance * instance )
: QDialog ( parent ) , mods ( mods ) , m_instance ( instance )
2022-01-14 08:43:42 +00:00
{
setObjectName ( QStringLiteral ( " ModDownloadDialog " ) ) ;
resize ( 400 , 347 ) ;
m_verticalLayout = new QVBoxLayout ( this ) ;
m_verticalLayout - > setObjectName ( QStringLiteral ( " verticalLayout " ) ) ;
setWindowIcon ( APPLICATION - > getThemedIcon ( " new " ) ) ;
// NOTE: m_buttons must be initialized before PageContainer, because it indirectly accesses m_buttons through setSuggestedPack! Do not move this below.
m_buttons = new QDialogButtonBox ( QDialogButtonBox : : Help | QDialogButtonBox : : Ok | QDialogButtonBox : : Cancel ) ;
m_container = new PageContainer ( this ) ;
m_container - > setSizePolicy ( QSizePolicy : : Policy : : Preferred , QSizePolicy : : Policy : : Expanding ) ;
m_container - > layout ( ) - > setContentsMargins ( 0 , 0 , 0 , 0 ) ;
m_verticalLayout - > addWidget ( m_container ) ;
m_container - > addButtons ( m_buttons ) ;
// Bonk Qt over its stupid head and make sure it understands which button is the default one...
// See: https://stackoverflow.com/questions/24556831/qbuttonbox-set-default-button
auto OkButton = m_buttons - > button ( QDialogButtonBox : : Ok ) ;
2022-02-23 22:17:33 +00:00
OkButton - > setEnabled ( false ) ;
2022-01-14 08:43:42 +00:00
OkButton - > setDefault ( true ) ;
OkButton - > setAutoDefault ( true ) ;
2022-02-22 02:00:50 +00:00
connect ( OkButton , & QPushButton : : clicked , this , & ModDownloadDialog : : confirm ) ;
2022-01-14 08:43:42 +00:00
auto CancelButton = m_buttons - > button ( QDialogButtonBox : : Cancel ) ;
CancelButton - > setDefault ( false ) ;
CancelButton - > setAutoDefault ( false ) ;
connect ( CancelButton , & QPushButton : : clicked , this , & ModDownloadDialog : : reject ) ;
auto HelpButton = m_buttons - > button ( QDialogButtonBox : : Help ) ;
HelpButton - > setDefault ( false ) ;
HelpButton - > setAutoDefault ( false ) ;
connect ( HelpButton , & QPushButton : : clicked , m_container , & PageContainer : : help ) ;
2022-02-22 00:34:06 +00:00
2022-01-14 08:43:42 +00:00
QMetaObject : : connectSlotsByName ( this ) ;
setWindowModality ( Qt : : WindowModal ) ;
setWindowTitle ( " Download mods " ) ;
}
QString ModDownloadDialog : : dialogTitle ( )
{
return tr ( " Download mods " ) ;
}
void ModDownloadDialog : : reject ( )
{
QDialog : : reject ( ) ;
}
2022-02-22 02:00:50 +00:00
void ModDownloadDialog : : confirm ( )
{
2022-02-22 02:25:33 +00:00
auto keys = modTask . keys ( ) ;
keys . sort ( Qt : : CaseInsensitive ) ;
2022-03-14 13:50:49 +00:00
auto confirm_dialog = ReviewMessageBox : : create (
2022-02-22 02:00:50 +00:00
this ,
2022-03-14 13:50:49 +00:00
tr ( " Confirm mods to download " )
2022-02-22 02:00:50 +00:00
) ;
2022-03-14 13:50:49 +00:00
for ( auto task : keys ) {
confirm_dialog - > appendMod ( task , modTask . find ( task ) . value ( ) - > getFilename ( ) ) ;
}
connect ( confirm_dialog , & QDialog : : accepted , this , & ModDownloadDialog : : accept ) ;
2022-02-22 02:00:50 +00:00
confirm_dialog - > open ( ) ;
}
2022-01-14 08:43:42 +00:00
void ModDownloadDialog : : accept ( )
{
QDialog : : accept ( ) ;
}
QList < BasePage * > ModDownloadDialog : : getPages ( )
{
2022-01-14 19:22:15 +00:00
modrinthPage = new ModrinthPage ( this , m_instance ) ;
2022-01-16 10:20:21 +00:00
flameModPage = new FlameModPage ( this , m_instance ) ;
2022-01-14 08:43:42 +00:00
return
{
2022-01-16 10:20:21 +00:00
modrinthPage ,
flameModPage
2022-01-14 08:43:42 +00:00
} ;
}
2022-02-22 00:34:06 +00:00
void ModDownloadDialog : : addSelectedMod ( const QString & name , ModDownloadTask * task )
{
2022-02-23 22:17:33 +00:00
removeSelectedMod ( name ) ;
modTask . insert ( name , task ) ;
2022-02-22 00:34:06 +00:00
m_buttons - > button ( QDialogButtonBox : : Ok ) - > setEnabled ( ! modTask . isEmpty ( ) ) ;
}
void ModDownloadDialog : : removeSelectedMod ( const QString & name )
{
if ( modTask . contains ( name ) )
delete modTask . find ( name ) . value ( ) ;
modTask . remove ( name ) ;
2022-02-23 22:17:33 +00:00
m_buttons - > button ( QDialogButtonBox : : Ok ) - > setEnabled ( ! modTask . isEmpty ( ) ) ;
2022-02-22 00:34:06 +00:00
}
bool ModDownloadDialog : : isModSelected ( const QString & name , const QString & filename ) const
2022-01-14 08:43:42 +00:00
{
2022-02-22 00:34:06 +00:00
// FIXME: Is there a way to check for versions without checking the filename
// as a heuristic, other than adding such info to ModDownloadTask itself?
auto iter = modTask . find ( name ) ;
return iter ! = modTask . end ( ) & & ( iter . value ( ) - > getFilename ( ) = = filename ) ;
2022-01-14 08:43:42 +00:00
}
ModDownloadDialog : : ~ ModDownloadDialog ( )
{
}
2022-02-22 00:34:06 +00:00
const QList < ModDownloadTask * > ModDownloadDialog : : getTasks ( ) {
return modTask . values ( ) ;
2022-01-14 08:43:42 +00:00
}