2017-12-12 04:45:46 +01:00
/*****************************************************************************
2018-05-24 22:32:00 +02:00
* gta5view Grand Theft Auto V Profile Viewer
2021-02-15 23:12:44 +01:00
* Copyright ( C ) 2016 - 2021 Syping
2017-12-12 04:45:46 +01:00
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include "PlayerListDialog.h"
# include "ui_PlayerListDialog.h"
# include "AppEnv.h"
2021-04-02 19:51:05 +02:00
# include <QStringBuilder>
2017-12-12 04:45:46 +01:00
# include <QFontMetrics>
# include <QInputDialog>
# include <QMessageBox>
# include <QPainter>
# include <QPixmap>
# include <QDebug>
PlayerListDialog : : PlayerListDialog ( QStringList players , ProfileDatabase * profileDB , QWidget * parent ) :
QDialog ( parent ) , players ( players ) , profileDB ( profileDB ) ,
ui ( new Ui : : PlayerListDialog )
{
// Set Window Flags
2021-02-15 23:12:44 +01:00
# if QT_VERSION >= 0x050900
setWindowFlag ( Qt : : WindowContextHelpButtonHint , false ) ;
# else
2017-12-12 04:45:46 +01:00
setWindowFlags ( windowFlags ( ) ^ Qt : : WindowContextHelpButtonHint ) ;
2021-02-15 23:12:44 +01:00
# endif
2017-12-17 13:03:43 +01:00
listUpdated = false ;
2017-12-12 04:45:46 +01:00
ui - > setupUi ( this ) ;
2018-06-07 17:07:30 +02:00
ui - > cmdCancel - > setDefault ( true ) ;
2018-01-11 08:41:00 +01:00
ui - > cmdCancel - > setFocus ( ) ;
2017-12-12 04:45:46 +01:00
2018-01-11 08:41:00 +01:00
// Set Icon for Apply Button
2021-02-15 23:12:44 +01:00
if ( QIcon : : hasThemeIcon ( " dialog-ok-apply " ) ) {
2017-12-12 04:45:46 +01:00
ui - > cmdApply - > setIcon ( QIcon : : fromTheme ( " dialog-ok-apply " ) ) ;
}
2021-02-15 23:12:44 +01:00
else if ( QIcon : : hasThemeIcon ( " dialog-apply " ) ) {
2017-12-12 04:45:46 +01:00
ui - > cmdApply - > setIcon ( QIcon : : fromTheme ( " dialog-apply " ) ) ;
}
2021-02-15 23:12:44 +01:00
else if ( QIcon : : hasThemeIcon ( " gtk-apply " ) ) {
2018-01-11 08:41:00 +01:00
ui - > cmdApply - > setIcon ( QIcon : : fromTheme ( " gtk-apply " ) ) ;
}
2021-02-15 23:12:44 +01:00
else if ( QIcon : : hasThemeIcon ( " dialog-ok " ) ) {
2017-12-12 04:45:46 +01:00
ui - > cmdApply - > setIcon ( QIcon : : fromTheme ( " dialog-ok " ) ) ;
}
2021-02-15 23:12:44 +01:00
else if ( QIcon : : hasThemeIcon ( " gtk-ok " ) ) {
2018-01-11 08:41:00 +01:00
ui - > cmdApply - > setIcon ( QIcon : : fromTheme ( " dialog-ok " ) ) ;
}
// Set Icon for Cancel Button
2021-02-15 23:12:44 +01:00
if ( QIcon : : hasThemeIcon ( " dialog-cancel " ) ) {
2017-12-12 04:45:46 +01:00
ui - > cmdCancel - > setIcon ( QIcon : : fromTheme ( " dialog-cancel " ) ) ;
}
2021-02-15 23:12:44 +01:00
else if ( QIcon : : hasThemeIcon ( " gtk-cancel " ) ) {
2018-01-11 08:41:00 +01:00
ui - > cmdCancel - > setIcon ( QIcon : : fromTheme ( " gtk-cancel " ) ) ;
}
2017-12-12 04:45:46 +01:00
2018-01-11 08:41:00 +01:00
// Set Icon for Manage Buttons
2021-02-15 23:12:44 +01:00
if ( QIcon : : hasThemeIcon ( " go-previous " ) & & QIcon : : hasThemeIcon ( " go-next " ) & & QIcon : : hasThemeIcon ( " list-add " ) ) {
2020-08-25 12:53:21 +02:00
# if QT_VERSION < 0x050600
qreal screenRatio = AppEnv : : screenRatio ( ) ;
if ( screenRatio ! = 1 ) {
QSize iconSize = ui - > cmdMakeAv - > iconSize ( ) ;
iconSize = QSize ( iconSize . width ( ) * screenRatio , iconSize . height ( ) * screenRatio ) ;
ui - > cmdMakeAv - > setIconSize ( iconSize ) ;
ui - > cmdMakeSe - > setIconSize ( iconSize ) ;
ui - > cmdMakeAd - > setIconSize ( iconSize ) ;
}
# endif
2018-01-11 08:41:00 +01:00
ui - > cmdMakeAv - > setIcon ( QIcon : : fromTheme ( " go-previous " ) ) ;
ui - > cmdMakeSe - > setIcon ( QIcon : : fromTheme ( " go-next " ) ) ;
ui - > cmdMakeAd - > setIcon ( QIcon : : fromTheme ( " list-add " ) ) ;
}
2021-02-15 23:12:44 +01:00
else {
2020-08-25 12:53:21 +02:00
# if QT_VERSION < 0x050600
qreal screenRatio = AppEnv : : screenRatio ( ) ;
if ( screenRatio ! = 1 ) {
QSize iconSize = ui - > cmdMakeAv - > iconSize ( ) ;
iconSize = QSize ( iconSize . width ( ) * screenRatio , iconSize . height ( ) * screenRatio ) ;
ui - > cmdMakeAv - > setIconSize ( iconSize ) ;
ui - > cmdMakeSe - > setIconSize ( iconSize ) ;
ui - > cmdMakeAd - > setIconSize ( iconSize ) ;
}
# endif
2021-04-02 19:51:05 +02:00
ui - > cmdMakeAv - > setIcon ( QIcon ( AppEnv : : getImagesFolder ( ) % " /back.svgz " ) ) ;
ui - > cmdMakeSe - > setIcon ( QIcon ( AppEnv : : getImagesFolder ( ) % " /next.svgz " ) ) ;
ui - > cmdMakeAd - > setIcon ( QIcon ( AppEnv : : getImagesFolder ( ) % " /add.svgz " ) ) ;
2018-01-11 08:41:00 +01:00
}
2017-12-12 04:45:46 +01:00
buildInterface ( ) ;
// DPI calculation
qreal screenRatio = AppEnv : : screenRatio ( ) ;
resize ( 500 * screenRatio , 350 * screenRatio ) ;
}
PlayerListDialog : : ~ PlayerListDialog ( )
{
2021-02-15 23:12:44 +01:00
for ( QObject * object : ui - > listAvPlayers - > children ( ) ) {
2017-12-12 04:45:46 +01:00
delete object ;
}
2021-02-15 23:12:44 +01:00
for ( QObject * object : ui - > listSePlayers - > children ( ) ) {
2017-12-12 04:45:46 +01:00
delete object ;
}
delete ui ;
}
void PlayerListDialog : : on_cmdCancel_clicked ( )
{
close ( ) ;
}
void PlayerListDialog : : buildInterface ( )
{
const QStringList dbPlayers = profileDB - > getPlayers ( ) ;
2021-04-02 19:51:05 +02:00
for ( const QString & sePlayer : qAsConst ( players ) ) {
2018-01-16 00:13:08 +01:00
QListWidgetItem * playerItem = new QListWidgetItem ( profileDB - > getPlayerName ( sePlayer ) ) ;
playerItem - > setData ( Qt : : UserRole , sePlayer ) ;
ui - > listSePlayers - > addItem ( playerItem ) ;
2017-12-12 04:45:46 +01:00
}
2021-02-15 23:12:44 +01:00
for ( const QString & dbPlayer : dbPlayers ) {
if ( ! players . contains ( dbPlayer ) ) {
2018-01-16 00:13:08 +01:00
QListWidgetItem * playerItem = new QListWidgetItem ( profileDB - > getPlayerName ( dbPlayer ) ) ;
playerItem - > setData ( Qt : : UserRole , dbPlayer ) ;
ui - > listAvPlayers - > addItem ( playerItem ) ;
2017-12-12 04:45:46 +01:00
}
}
2018-01-16 00:13:08 +01:00
ui - > listAvPlayers - > sortItems ( Qt : : AscendingOrder ) ;
2017-12-12 04:45:46 +01:00
}
void PlayerListDialog : : on_cmdMakeAv_clicked ( )
{
2021-02-15 23:12:44 +01:00
for ( QListWidgetItem * item : ui - > listSePlayers - > selectedItems ( ) ) {
2018-01-16 00:13:08 +01:00
QString playerName = item - > text ( ) ;
int playerID = item - > data ( Qt : : UserRole ) . toInt ( ) ;
2017-12-12 04:45:46 +01:00
delete item ;
2018-01-16 00:13:08 +01:00
QListWidgetItem * playerItem = new QListWidgetItem ( playerName ) ;
playerItem - > setData ( Qt : : UserRole , playerID ) ;
ui - > listAvPlayers - > addItem ( playerItem ) ;
2017-12-12 04:45:46 +01:00
ui - > listAvPlayers - > sortItems ( Qt : : AscendingOrder ) ;
}
}
void PlayerListDialog : : on_cmdMakeSe_clicked ( )
{
int maxPlayers = 30 ;
2021-02-15 23:12:44 +01:00
if ( maxPlayers < ui - > listSePlayers - > count ( ) + ui - > listAvPlayers - > selectedItems ( ) . count ( ) ) {
2017-12-12 04:45:46 +01:00
QMessageBox : : warning ( this , tr ( " Add Players... " ) , tr ( " Failed to add more Players because the limit of Players are %1! " ) . arg ( QString : : number ( maxPlayers ) ) ) ;
return ;
}
2021-02-15 23:12:44 +01:00
for ( QListWidgetItem * item : ui - > listAvPlayers - > selectedItems ( ) ) {
2018-01-16 00:13:08 +01:00
QString playerName = item - > text ( ) ;
int playerID = item - > data ( Qt : : UserRole ) . toInt ( ) ;
2017-12-12 04:45:46 +01:00
delete item ;
2018-01-16 00:13:08 +01:00
QListWidgetItem * playerItem = new QListWidgetItem ( playerName ) ;
playerItem - > setData ( Qt : : UserRole , playerID ) ;
ui - > listSePlayers - > addItem ( playerItem ) ;
2017-12-12 04:45:46 +01:00
}
}
void PlayerListDialog : : on_cmdMakeAd_clicked ( )
{
bool playerOk ;
int playerID = QInputDialog : : getInt ( this , tr ( " Add Player... " ) , tr ( " Enter Social Club Player ID " ) , 1 , 1 , 214783647 , 1 , & playerOk , windowFlags ( ) ) ;
2021-02-15 23:12:44 +01:00
if ( playerOk ) {
for ( int i = 0 ; i < ui - > listAvPlayers - > count ( ) ; + + i ) {
2017-12-12 04:45:46 +01:00
QListWidgetItem * item = ui - > listAvPlayers - > item ( i ) ;
2018-01-16 00:13:08 +01:00
QString itemPlayerName = item - > text ( ) ;
int itemPlayerID = item - > data ( Qt : : UserRole ) . toInt ( ) ;
2021-02-15 23:12:44 +01:00
if ( itemPlayerID = = playerID ) {
2017-12-12 04:45:46 +01:00
delete item ;
2018-01-16 00:13:08 +01:00
QListWidgetItem * playerItem = new QListWidgetItem ( itemPlayerName ) ;
playerItem - > setData ( Qt : : UserRole , playerID ) ;
ui - > listSePlayers - > addItem ( playerItem ) ;
2017-12-12 04:45:46 +01:00
return ;
}
}
2021-02-15 23:12:44 +01:00
for ( int i = 0 ; i < ui - > listSePlayers - > count ( ) ; + + i ) {
2017-12-12 04:45:46 +01:00
QListWidgetItem * item = ui - > listSePlayers - > item ( i ) ;
2018-01-16 00:13:08 +01:00
int itemPlayerID = item - > data ( Qt : : UserRole ) . toInt ( ) ;
if ( itemPlayerID = = playerID )
2017-12-12 04:45:46 +01:00
{
QMessageBox : : warning ( this , tr ( " Add Player... " ) , tr ( " Failed to add Player %1 because Player %1 is already added! " ) . arg ( QString : : number ( playerID ) ) ) ;
return ;
}
}
2018-01-16 00:13:08 +01:00
QListWidgetItem * playerItem = new QListWidgetItem ( QString : : number ( playerID ) ) ;
playerItem - > setData ( Qt : : UserRole , playerID ) ;
ui - > listSePlayers - > addItem ( playerItem ) ;
2017-12-12 04:45:46 +01:00
}
}
void PlayerListDialog : : on_cmdApply_clicked ( )
{
players . clear ( ) ;
2021-02-15 23:12:44 +01:00
for ( int i = 0 ; i < ui - > listSePlayers - > count ( ) ; + + i ) {
2018-01-16 00:13:08 +01:00
players + = ui - > listSePlayers - > item ( i ) - > data ( Qt : : UserRole ) . toString ( ) ;
2017-12-12 04:45:46 +01:00
}
emit playerListUpdated ( players ) ;
2017-12-17 13:03:43 +01:00
listUpdated = true ;
2017-12-12 04:45:46 +01:00
close ( ) ;
}
2017-12-17 13:03:43 +01:00
QStringList PlayerListDialog : : getPlayerList ( ) const
{
return players ;
}
bool PlayerListDialog : : isListUpdated ( )
{
return listUpdated ;
}