<commit_before>#include "card_selector.h"
#include <QLayout>
#include <QVBoxLayout>
#include <cards/standard-36-deck.h>
#include <QPushButton>
#include <type_traits>
#include <exception>

#include <QFrame>

template<class T>
typename std::enable_if<std::is_same<T, QComboBox>::value>::type SelectData(T* comboBox, int data) {
	int index = comboBox->findData(data);
	if (index == -1)
		throw std::runtime_error("Unknown data");
	comboBox->setCurrentIndex(index);
}

GuiCardSelector::GuiCardSelector(int suit, int rank, bool inversed, QWidget* parent) : QWidget(parent) {
	CreateElements();
	PopulateSuits();
	PopulateRanks();
	CreateLayout();

	SelectData(this->suit, suit);
	SelectData(this->rank, rank);
	inverse->setChecked(inversed);
}

void GuiCardSelector::CreateElements() {
	suit = new QComboBox();
	rank = new QComboBox();
	inverse = new QCheckBox(tr("Inverse"));
}

void GuiCardSelector::PopulateSuits() {
	suit->addItem(tr("Any suit"), (int)-1);
	suit->addItem(tr("Diamond"), (int)standard_36_deck::Suits::Diamonds);
	suit->addItem(tr("Spade"), (int)standard_36_deck::Suits::Spades);
	suit->addItem(tr("Heart"), (int)standard_36_deck::Suits::Hearts);
	suit->addItem(tr("Clubs"), (int)standard_36_deck::Suits::Clubs);
}

void GuiCardSelector::PopulateRanks() {
	rank->addItem(tr("Any rank"), (int)-1);
	rank->addItem(tr("Six"), (int)standard_36_deck::Ranks::Six);
	rank->addItem(tr("Seven"), (int)standard_36_deck::Ranks::Seven);
	rank->addItem(tr("Eight"), (int)standard_36_deck::Ranks::Eight);
	rank->addItem(tr("Nine"), (int)standard_36_deck::Ranks::Nine);
	rank->addItem(tr("Ten"), (int)standard_36_deck::Ranks::Ten);
	rank->addItem(tr("Jack"), (int)standard_36_deck::Ranks::Jack);
	rank->addItem(tr("Queen"), (int)standard_36_deck::Ranks::Queen);
	rank->addItem(tr("King"), (int)standard_36_deck::Ranks::King);
	rank->addItem(tr("Ace"), (int)standard_36_deck::Ranks::Ace);
}

void GuiCardSelector::CreateLayout() {
	auto layout = new QVBoxLayout();
	layout->addWidget(suit);
	layout->addWidget(rank);
	layout->addWidget(inverse);

	QPushButton *btn = new QPushButton(tr("Remove"));
	layout->addWidget(btn);
	QObject::connect(btn, &QPushButton::clicked, this, &GuiCardSelector::DeleteClicked);

	auto frame = new QFrame();
	frame->setLayout(layout);
	frame->setFrameStyle(QFrame::StyledPanel);
	
	setLayout(new QGridLayout());
	this->layout()->addWidget(frame);
}

CardSelector GuiCardSelector::GetSelector() const {
	int suitData = suit->currentData().toInt();
	int rankData = rank->currentData().toInt();

	bool suitSelected = suitData != -1;
	bool rankSelected = rankData != -1;
	bool isStraight = !inverse->isChecked();

	if (suitSelected && rankSelected) {
		return CardSelector(suitData, rankData, isStraight);
	} else if (suitSelected) {
		return CardSelector(Card::Suit(suitData), isStraight);
	} else if (rankSelected) {
		return CardSelector(Card::Rank(rankData), isStraight);
	} else {
		throw std::runtime_error("No suit or rank selected");
	}
}

<commit_msg>Adedd a helper for populating QComboBox'es<commit_after>#include "card_selector.h"
#include <QLayout>
#include <QVBoxLayout>
#include <cards/standard-36-deck.h>
#include <QPushButton>
#include <type_traits>
#include <exception>

#include <QFrame>

template<class T>
typename std::enable_if<std::is_same<T, QComboBox>::value>::type SelectData(T* comboBox, int data) {
	int index = comboBox->findData(data);
	if (index == -1)
		throw std::runtime_error("Unknown data");
	comboBox->setCurrentIndex(index);
}

GuiCardSelector::GuiCardSelector(int suit, int rank, bool inversed, QWidget* parent) : QWidget(parent) {
	CreateElements();
	PopulateSuits();
	PopulateRanks();
	CreateLayout();

	SelectData(this->suit, suit);
	SelectData(this->rank, rank);
	inverse->setChecked(inversed);
}

void GuiCardSelector::CreateElements() {
	suit = new QComboBox();
	rank = new QComboBox();
	inverse = new QCheckBox(tr("Inverse"));
}

class Populater {
public:
	Populater(QComboBox* box) : box(box) {}
	void Add(const QString& title, int value) {
		box->addItem(title, value);
	}
private:
	QComboBox* box;
};

void GuiCardSelector::PopulateSuits() {
	Populater p(suit);
	p.Add(tr("Any suit"), -1);
	p.Add(tr("Diamond"), standard_36_deck::Suits::Diamonds);
	p.Add(tr("Spade"), standard_36_deck::Suits::Spades);
	p.Add(tr("Heart"), standard_36_deck::Suits::Hearts);
	p.Add(tr("Clubs"), standard_36_deck::Suits::Clubs);
}

void GuiCardSelector::PopulateRanks() {
	Populater p(rank);
	p.Add(tr("Any rank"), -1);
	p.Add(tr("Six"), standard_36_deck::Ranks::Six);
	p.Add(tr("Seven"), standard_36_deck::Ranks::Seven);
	p.Add(tr("Eight"), standard_36_deck::Ranks::Eight);
	p.Add(tr("Nine"), standard_36_deck::Ranks::Nine);
	p.Add(tr("Ten"), standard_36_deck::Ranks::Ten);
	p.Add(tr("Jack"), standard_36_deck::Ranks::Jack);
	p.Add(tr("Queen"), standard_36_deck::Ranks::Queen);
	p.Add(tr("King"), standard_36_deck::Ranks::King);
	p.Add(tr("Ace"), standard_36_deck::Ranks::Ace);
}

void GuiCardSelector::CreateLayout() {
	auto layout = new QVBoxLayout();
	layout->addWidget(suit);
	layout->addWidget(rank);
	layout->addWidget(inverse);

	QPushButton *btn = new QPushButton(tr("Remove"));
	layout->addWidget(btn);
	QObject::connect(btn, &QPushButton::clicked, this, &GuiCardSelector::DeleteClicked);

	auto frame = new QFrame();
	frame->setLayout(layout);
	frame->setFrameStyle(QFrame::StyledPanel);
	
	setLayout(new QGridLayout());
	this->layout()->addWidget(frame);
}

CardSelector GuiCardSelector::GetSelector() const {
	int suitData = suit->currentData().toInt();
	int rankData = rank->currentData().toInt();

	bool suitSelected = suitData != -1;
	bool rankSelected = rankData != -1;
	bool isStraight = !inverse->isChecked();

	if (suitSelected && rankSelected) {
		return CardSelector(suitData, rankData, isStraight);
	} else if (suitSelected) {
		return CardSelector(Card::Suit(suitData), isStraight);
	} else if (rankSelected) {
		return CardSelector(Card::Rank(rankData), isStraight);
	} else {
		throw std::runtime_error("No suit or rank selected");
	}
}


{'fraction_non_alphanumeric': 0.18231676915406347, 'fraction_numerical': 0.010968921389396709, 'mean_word_length': 7.536170212765957, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1837449', 'n_tokens_mistral': 2255, 'n_tokens_neox': 2146, 'n_words': 372}