File indexing completed on 2024-04-14 03:59:03

0001 /*
0002     SPDX-FileCopyrightText: 2012 Mathias Kraus <k.hias@gmx.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "playerselectoritem.h"
0008 #include "playersettings.h"
0009 
0010 #include <QApplication>
0011 #include <QLabel>
0012 #include <QVBoxLayout>
0013 #include <QHBoxLayout>
0014 #include <QGridLayout>
0015 #include <QSpacerItem>
0016 #include <QCheckBox>
0017 
0018 #include <QLineEdit>
0019 #include <KKeySequenceWidget>
0020 #include <KLocalizedString>
0021 
0022 
0023 PlayerSelectorItem::PlayerSelectorItem(const QString& playerId, PlayerSettings* playerSettings, QWidget* parent) : QWidget(parent), m_playerId(playerId), m_playerSettings(playerSettings)
0024 {
0025     m_selectCheckBox = new QCheckBox;
0026     m_selectCheckBox->setChecked(m_playerSettings->enabled(playerId));
0027     m_playerName = new QLineEdit(m_playerSettings->playerName(playerId));
0028     m_playerName->setFixedWidth(200);
0029     const qreal dpr = qApp->devicePixelRatio();
0030     const int previewSize = 64 * dpr;
0031     m_playerPreviewPixmap = QPixmap(QSize(previewSize, previewSize));
0032     m_playerPreviewPixmap.setDevicePixelRatio(dpr);
0033     m_playerPreviewImageAlphaChannel = QImage(QSize(previewSize, previewSize), QImage::Format_ARGB32_Premultiplied);
0034     m_playerPreviewPixmapLabel = new QLabel;
0035     m_playerAuthor = new QLabel;
0036     
0037     auto* mainLayout = new QHBoxLayout(this);
0038     
0039     auto* gridLayoutPlayer = new QGridLayout();
0040     gridLayoutPlayer->setContentsMargins(0, 0, 0, 0);
0041     
0042     auto* verticalLayoutKeySequence = new QVBoxLayout();
0043     verticalLayoutKeySequence->setContentsMargins(0, 0, 0, 0);
0044     
0045     mainLayout->addLayout(gridLayoutPlayer);
0046     mainLayout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));
0047     mainLayout->addLayout(verticalLayoutKeySequence);
0048     mainLayout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Minimum));
0049     
0050     gridLayoutPlayer->addWidget(m_selectCheckBox, 0, 0);
0051     gridLayoutPlayer->addWidget(m_playerName, 0, 1, 1, 3);
0052     gridLayoutPlayer->addWidget(m_playerPreviewPixmapLabel, 1, 1, 2, 1);
0053     gridLayoutPlayer->addWidget(m_playerAuthor, 2, 2);//, 1, 1, Qt::AlignBottom);
0054     
0055     auto* gridLayoutKeySequence = new QGridLayout();
0056     gridLayoutKeySequence->setContentsMargins(0, 0, 0, 0);
0057     
0058     verticalLayoutKeySequence->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding));
0059     verticalLayoutKeySequence->addLayout(gridLayoutKeySequence);
0060     verticalLayoutKeySequence->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding));
0061     
0062     m_moveLeft = new KKeySequenceWidget(this);
0063     m_moveLeft->setMultiKeyShortcutsAllowed(false);
0064     m_moveLeft->setModifierlessAllowed(true);
0065     m_moveLeft->setClearButtonShown(false);
0066     m_moveLeft->setKeySequence(m_playerSettings->keyLeft(playerId));
0067     m_labelMoveLeft = new QLabel(i18nc("Label for the key sequence to move the player.", "Left:"));
0068     gridLayoutKeySequence->addWidget(m_labelMoveLeft, 1, 1, Qt::AlignRight);
0069     gridLayoutKeySequence->addWidget(m_moveLeft, 1, 2);
0070     
0071     m_moveUp = new KKeySequenceWidget(this);
0072     m_moveUp->setMultiKeyShortcutsAllowed(false);
0073     m_moveUp->setModifierlessAllowed(true);
0074     m_moveUp->setClearButtonShown(false);
0075     m_moveUp->setKeySequence(m_playerSettings->keyUp(playerId));
0076     m_labelMoveUp = new QLabel(i18nc("Label for the key sequence to move the player.", "Up:"));
0077     gridLayoutKeySequence->addWidget(m_labelMoveUp, 0, 3, Qt::AlignRight);
0078     gridLayoutKeySequence->addWidget(m_moveUp, 0, 4);
0079     
0080     m_moveRight = new KKeySequenceWidget(this);
0081     m_moveRight->setMultiKeyShortcutsAllowed(false);
0082     m_moveRight->setModifierlessAllowed(true);
0083     m_moveRight->setClearButtonShown(false);
0084     m_moveRight->setKeySequence(m_playerSettings->keyRight(playerId));
0085     m_labelMoveRight = new QLabel(i18nc("Label for the key sequence to move the player.", "Right:"));
0086     gridLayoutKeySequence->addWidget(m_labelMoveRight, 1, 5, Qt::AlignRight);
0087     gridLayoutKeySequence->addWidget(m_moveRight, 1, 6);
0088     
0089     m_moveDown = new KKeySequenceWidget(this);
0090     m_moveDown->setMultiKeyShortcutsAllowed(false);
0091     m_moveDown->setModifierlessAllowed(true);
0092     m_moveDown->setClearButtonShown(false);
0093     m_moveDown->setKeySequence(m_playerSettings->keyDown(playerId));
0094     m_labelMoveDown = new QLabel(i18nc("Label for the key sequence to move the player.", "Down:"));
0095     gridLayoutKeySequence->addWidget(m_labelMoveDown, 2, 3, Qt::AlignRight);
0096     gridLayoutKeySequence->addWidget(m_moveDown, 2, 4);
0097     
0098     m_dropBomb = new KKeySequenceWidget(this);
0099     m_dropBomb->setMultiKeyShortcutsAllowed(false);
0100     m_dropBomb->setModifierlessAllowed(true);
0101     m_dropBomb->setClearButtonShown(false);
0102     m_dropBomb->setKeySequence(m_playerSettings->keyPutBomb(playerId));
0103     m_labelDropBomb = new QLabel(i18nc("Label for the key sequence to move the player.", "Bomb:"));
0104     gridLayoutKeySequence->addWidget(m_labelDropBomb, 1, 3, Qt::AlignRight);
0105     gridLayoutKeySequence->addWidget(m_dropBomb, 1, 4);
0106     
0107     selectionChanged(m_selectCheckBox->isChecked());
0108     connect(m_selectCheckBox, &QCheckBox::toggled, this, &PlayerSelectorItem::selectionChanged);
0109     connect(m_selectCheckBox, &QCheckBox::toggled, this, &PlayerSelectorItem::settingsChanged);
0110     connect(m_playerName, &QLineEdit::textEdited, this, &PlayerSelectorItem::settingsChanged);
0111     connect(m_moveLeft, &KKeySequenceWidget::keySequenceChanged, this, &PlayerSelectorItem::settingsChanged);
0112     connect(m_moveUp, &KKeySequenceWidget::keySequenceChanged, this, &PlayerSelectorItem::settingsChanged);
0113     connect(m_moveRight, &KKeySequenceWidget::keySequenceChanged, this, &PlayerSelectorItem::settingsChanged);
0114     connect(m_moveDown, &KKeySequenceWidget::keySequenceChanged, this, &PlayerSelectorItem::settingsChanged);
0115     connect(m_dropBomb, &KKeySequenceWidget::keySequenceChanged, this, &PlayerSelectorItem::settingsChanged);
0116 }
0117 
0118 PlayerSelectorItem::~PlayerSelectorItem()
0119 {
0120     delete m_selectCheckBox;
0121     
0122     delete m_playerName;
0123     delete m_playerPreviewPixmapLabel;
0124     delete m_playerAuthor;
0125     
0126     delete m_moveLeft;
0127     delete m_moveUp;
0128     delete m_moveRight;
0129     delete m_moveDown;
0130     delete m_dropBomb;
0131     
0132     delete m_labelMoveLeft;
0133     delete m_labelMoveUp;
0134     delete m_labelMoveRight;
0135     delete m_labelMoveDown;
0136     delete m_labelDropBomb;
0137 }
0138 
0139 void PlayerSelectorItem::setPlayerPreviewPixmap(const QPixmap& pixmap)
0140 {
0141     m_playerPreviewPixmap = QPixmap(pixmap);
0142 
0143     QImage tempImage = m_playerPreviewPixmap.toImage();
0144     tempImage.setAlphaChannel(m_playerPreviewImageAlphaChannel);
0145     m_playerPreviewPixmapLabel->setPixmap(QPixmap::fromImage(tempImage));
0146 }
0147 
0148 void PlayerSelectorItem::setPlayerAuthor(const QString& name, const QString& mail)
0149 {
0150     QString mailString;
0151     if(!mail.isEmpty())
0152     {
0153         mailString = QStringLiteral("<br />") + mail;
0154     }
0155     m_playerAuthor->setText(QStringLiteral("<i>%1</i>%2").arg(i18nc("Author attribution, e.g. \"by Jack\"", "by %1", name), mailString));
0156 }
0157 
0158 void PlayerSelectorItem::selectionChanged(bool selectionState)
0159 {
0160     m_playerName->setEnabled(selectionState);
0161     m_playerAuthor->setEnabled(selectionState);
0162     
0163     if(selectionState == true)
0164     {
0165         m_playerPreviewImageAlphaChannel.fill(QColor(255, 255, 255, 255));
0166     }
0167     else
0168     {
0169         m_playerPreviewImageAlphaChannel.fill(QColor(64, 64, 64, 255));
0170     }
0171 
0172     QImage tempImage = m_playerPreviewPixmap.toImage();
0173     tempImage.setAlphaChannel(m_playerPreviewImageAlphaChannel);
0174     m_playerPreviewPixmapLabel->setPixmap(QPixmap::fromImage(tempImage));
0175     
0176     m_moveLeft->setEnabled(selectionState);
0177     m_moveUp->setEnabled(selectionState);
0178     m_moveRight->setEnabled(selectionState);
0179     m_moveDown->setEnabled(selectionState);
0180     m_dropBomb->setEnabled(selectionState);
0181     
0182     m_labelMoveLeft->setEnabled(selectionState);
0183     m_labelMoveUp->setEnabled(selectionState);
0184     m_labelMoveRight->setEnabled(selectionState);
0185     m_labelMoveDown->setEnabled(selectionState);
0186     m_labelDropBomb->setEnabled(selectionState);
0187 }
0188 
0189 void PlayerSelectorItem::settingsChanged()
0190 {
0191     m_playerSettings->setEnabled(m_playerId, m_selectCheckBox->isChecked());
0192     m_playerSettings->setPlayerName(m_playerId, m_playerName->text());
0193     m_playerSettings->setKeyLeft(m_playerId, m_moveLeft->keySequence());
0194     m_playerSettings->setKeyUp(m_playerId, m_moveUp->keySequence());
0195     m_playerSettings->setKeyRight(m_playerId, m_moveRight->keySequence());
0196     m_playerSettings->setKeyDown(m_playerId, m_moveDown->keySequence());
0197     m_playerSettings->setKeyPutBomb(m_playerId, m_dropBomb->keySequence());
0198 }
0199 
0200 #include "moc_playerselectoritem.cpp"