mirror of
git://git.savannah.nongnu.org/eliot.git
synced 2025-01-18 10:26:15 +01:00
Group all the custom validators in a factory class.
This will make it easier to factorize and reuse this code.
This commit is contained in:
parent
855718c6b8
commit
cfdf5a928d
7 changed files with 484 additions and 325 deletions
|
@ -59,6 +59,7 @@ eliot_SOURCES = \
|
|||
coord_model.h coord_model.cpp \
|
||||
tile_widget.cpp tile_widget.h \
|
||||
tile_layout.cpp tile_layout.h \
|
||||
validator_factory.h validator_factory.cpp \
|
||||
custom_popup.cpp custom_popup.h \
|
||||
bag_widget.cpp bag_widget.h \
|
||||
bag_widget2.cpp bag_widget2.h \
|
||||
|
@ -90,6 +91,7 @@ nodist_eliot_SOURCES = \
|
|||
coord_model.moc.cpp \
|
||||
tile_widget.moc.cpp \
|
||||
tile_layout.moc.cpp \
|
||||
validator_factory.moc.cpp \
|
||||
custom_popup.moc.cpp \
|
||||
new_game.moc.cpp \
|
||||
dic_tools_widget.moc.cpp \
|
||||
|
|
|
@ -26,15 +26,14 @@
|
|||
#include <fstream>
|
||||
#include <QtGui/QTreeView>
|
||||
#include <QtGui/QStandardItemModel>
|
||||
#include <QtGui/QVBoxLayout>
|
||||
#include <QtGui/QLineEdit>
|
||||
#include <QtGui/QToolTip>
|
||||
#include <QtGui/QFileDialog>
|
||||
#include <QtGui/QMessageBox>
|
||||
#include <QtCore/QString>
|
||||
|
||||
#include "dic_tools_widget.h"
|
||||
#include "custom_popup.h"
|
||||
#include "validator_factory.h"
|
||||
#include "qtcommon.h"
|
||||
#include "dic.h"
|
||||
#include "header.h"
|
||||
|
@ -44,34 +43,6 @@
|
|||
using namespace std;
|
||||
|
||||
|
||||
/// Validator used for the line edits accepting only dictionary characters
|
||||
class DicRackValidator: public QValidator
|
||||
{
|
||||
public:
|
||||
explicit DicRackValidator(QObject *parent,
|
||||
const Dictionary *iDic,
|
||||
bool acceptJoker = false);
|
||||
virtual State validate(QString &input, int &pos) const;
|
||||
|
||||
private:
|
||||
const Dictionary *m_dic;
|
||||
const bool m_acceptJoker;
|
||||
};
|
||||
|
||||
|
||||
/// Validator used for the regexp line edit
|
||||
class RegexpValidator: public QValidator
|
||||
{
|
||||
public:
|
||||
explicit RegexpValidator(QObject *parent,
|
||||
const Dictionary *iDic);
|
||||
virtual State validate(QString &input, int &pos) const;
|
||||
|
||||
private:
|
||||
const Dictionary *m_dic;
|
||||
};
|
||||
|
||||
|
||||
DicToolsWidget::DicToolsWidget(QWidget *parent)
|
||||
: QWidget(parent), m_dic(NULL)
|
||||
{
|
||||
|
@ -138,9 +109,9 @@ void DicToolsWidget::setDic(const Dictionary *iDic)
|
|||
lineEditPlus1->clear();
|
||||
lineEditRegexp->clear();
|
||||
// Create new validators
|
||||
lineEditCheck->setValidator(new DicRackValidator(this, m_dic));
|
||||
lineEditPlus1->setValidator(new DicRackValidator(this, m_dic, true));
|
||||
lineEditRegexp->setValidator(new RegexpValidator(this, m_dic));
|
||||
lineEditCheck->setValidator(ValidatorFactory::newDicRackValidator(this, m_dic));
|
||||
lineEditPlus1->setValidator(ValidatorFactory::newDicRackValidator(this, m_dic, true));
|
||||
lineEditRegexp->setValidator(ValidatorFactory::newRegexpValidator(this, m_dic));
|
||||
// Refresh
|
||||
refreshCheck();
|
||||
refreshPlus1();
|
||||
|
@ -414,76 +385,3 @@ void DicToolsWidget::populateMenuRegexp(QMenu &iMenu, const QPoint &iPoint)
|
|||
m_customPopupRegexp->addShowDefinitionEntry(iMenu, selectedWord);
|
||||
}
|
||||
|
||||
|
||||
|
||||
DicRackValidator::DicRackValidator(QObject *parent,
|
||||
const Dictionary *iDic,
|
||||
bool acceptJoker)
|
||||
: QValidator(parent), m_dic(iDic), m_acceptJoker(acceptJoker)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
QValidator::State DicRackValidator::validate(QString &input, int &) const
|
||||
{
|
||||
if (m_dic == NULL)
|
||||
return Invalid;
|
||||
|
||||
if (input == "")
|
||||
return Intermediate;
|
||||
|
||||
// The string is invalid if it contains invalid input characters
|
||||
const wistring &winput = wfq(input);
|
||||
if (!m_dic->validateInputChars(winput))
|
||||
return Invalid;
|
||||
|
||||
// Convert the string to internal letters
|
||||
const wstring &intInput = m_dic->convertFromInput(winput);
|
||||
// The string is invalid if it contains characters not present
|
||||
// in the dictionary
|
||||
if (!m_dic->validateLetters(intInput))
|
||||
return Intermediate;
|
||||
|
||||
// A '?' may not be acceptable
|
||||
if (!m_acceptJoker && input.contains('?'))
|
||||
return Invalid;
|
||||
// Do not accept more than 2 jokers
|
||||
if (input.count('?') > 2)
|
||||
return Invalid;
|
||||
return Acceptable;
|
||||
}
|
||||
|
||||
|
||||
|
||||
RegexpValidator::RegexpValidator(QObject *parent,
|
||||
const Dictionary *iDic)
|
||||
: QValidator(parent), m_dic(iDic)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
QValidator::State RegexpValidator::validate(QString &input, int &) const
|
||||
{
|
||||
if (m_dic == NULL)
|
||||
return Invalid;
|
||||
|
||||
if (input == "")
|
||||
return Intermediate;
|
||||
|
||||
wstring authorizedChars = L".[]()*+?:^";
|
||||
|
||||
// The string is invalid if it contains invalid input characters
|
||||
const wistring &winput = wfq(input);
|
||||
if (!m_dic->validateInputChars(winput, authorizedChars))
|
||||
return Invalid;
|
||||
|
||||
// Convert the string to internal letters
|
||||
const wstring &intInput = m_dic->convertFromInput(winput);
|
||||
// The string is invalid if it contains characters not present
|
||||
// in the dictionary
|
||||
if (!m_dic->validateLetters(intInput, authorizedChars))
|
||||
return Intermediate;
|
||||
|
||||
return Acceptable;
|
||||
}
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include <QtGui/QPushButton>
|
||||
|
||||
#include "play_word_mediator.h"
|
||||
#include "validator_factory.h"
|
||||
#include "coord_model.h"
|
||||
#include "qtcommon.h"
|
||||
|
||||
|
@ -31,29 +32,6 @@
|
|||
#include "debug.h"
|
||||
|
||||
|
||||
/// Validator used for the "play word" line edit
|
||||
class PlayWordValidator: public QValidator
|
||||
{
|
||||
public:
|
||||
explicit PlayWordValidator(QObject *parent,
|
||||
const Dictionary &iDic);
|
||||
virtual State validate(QString &input, int &pos) const;
|
||||
|
||||
private:
|
||||
const Dictionary &m_dic;
|
||||
};
|
||||
|
||||
|
||||
/// Validator used for the "coords" line edit
|
||||
class CoordsValidator: public QValidator
|
||||
{
|
||||
public:
|
||||
explicit CoordsValidator(QObject *parent);
|
||||
virtual State validate(QString &input, int &pos) const;
|
||||
};
|
||||
|
||||
|
||||
|
||||
PlayWordMediator::PlayWordMediator(QObject *parent, QLineEdit &iEditPlay,
|
||||
QLineEdit &iEditCoord, QLineEdit &iEditPoints,
|
||||
QPushButton &iButtonPlay,
|
||||
|
@ -75,8 +53,8 @@ PlayWordMediator::PlayWordMediator(QObject *parent, QLineEdit &iEditPlay,
|
|||
/// Set validators;
|
||||
if (m_game)
|
||||
{
|
||||
m_lineEditPlay.setValidator(new PlayWordValidator(this, m_game->getDic()));
|
||||
m_lineEditCoord.setValidator(new CoordsValidator(this));
|
||||
m_lineEditPlay.setValidator(ValidatorFactory::newPlayWordValidator(this, m_game->getDic()));
|
||||
m_lineEditCoord.setValidator(ValidatorFactory::newCoordsValidator(this));
|
||||
}
|
||||
|
||||
// Set all the connections
|
||||
|
@ -253,76 +231,3 @@ void PlayWordMediator::updateCoord(const Coord &, const Coord &iNewCoord)
|
|||
lineEditPlay_textChanged();
|
||||
}
|
||||
|
||||
|
||||
// ------ Validators ------
|
||||
|
||||
PlayWordValidator::PlayWordValidator(QObject *parent,
|
||||
const Dictionary &iDic)
|
||||
: QValidator(parent), m_dic(iDic)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
QValidator::State PlayWordValidator::validate(QString &input, int &) const
|
||||
{
|
||||
if (input == "")
|
||||
return Intermediate;
|
||||
|
||||
const wistring &winput = wfq(input);
|
||||
// The string is invalid if it contains invalid input characters
|
||||
if (!m_dic.validateInputChars(winput, L"()") || input.contains('?'))
|
||||
return Invalid;
|
||||
|
||||
// Convert the string to internal letters
|
||||
const wstring &intInput = m_dic.convertFromInput(winput);
|
||||
// The string is invalid if it contains characters not present
|
||||
// in the dictionary (ignoring parentheses)
|
||||
if (!m_dic.validateLetters(intInput, L"()"))
|
||||
return Intermediate;
|
||||
|
||||
// Check the parentheses pairs
|
||||
QString qintInput = qfw(intInput);
|
||||
int pos;
|
||||
while ((pos = qintInput.indexOf('(')) != -1)
|
||||
{
|
||||
if (qintInput.size() < pos + 3 || qintInput[pos + 2] != ')' ||
|
||||
!m_dic.validateLetters(wfq(QString(qintInput[pos + 1]))))
|
||||
{
|
||||
return Intermediate;
|
||||
}
|
||||
else
|
||||
{
|
||||
qintInput.remove(pos, 3);
|
||||
}
|
||||
}
|
||||
if (qintInput.indexOf(')') != -1)
|
||||
return Intermediate;
|
||||
|
||||
return Acceptable;
|
||||
}
|
||||
|
||||
|
||||
|
||||
CoordsValidator::CoordsValidator(QObject *parent)
|
||||
: QValidator(parent)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
QValidator::State CoordsValidator::validate(QString &input, int &) const
|
||||
{
|
||||
// Only authorize characters part of a valid coordinate
|
||||
wstring copy = wfq(input.toUpper());
|
||||
wstring authorized = L"ABCDEFGHIJKLMNO1234567890";
|
||||
if (copy.find_first_not_of(authorized) != wstring::npos)
|
||||
return Invalid;
|
||||
|
||||
// Check coordinates
|
||||
Coord c(wfq(input));
|
||||
if (!c.isValid())
|
||||
return Intermediate;
|
||||
|
||||
return Acceptable;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -18,13 +18,12 @@
|
|||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*****************************************************************************/
|
||||
|
||||
#include <QtGui/QLineEdit>
|
||||
#include <QtGui/QHBoxLayout>
|
||||
#include <QtGui/QValidator>
|
||||
#include <QtCore/QStringList>
|
||||
|
||||
#include "player_widget.h"
|
||||
#include "play_word_mediator.h"
|
||||
#include "validator_factory.h"
|
||||
#include "qtcommon.h"
|
||||
#include "public_game.h"
|
||||
#include "player.h"
|
||||
|
@ -37,21 +36,6 @@
|
|||
#include "encoding.h"
|
||||
|
||||
|
||||
/// Validator used for the "change letters" line edit
|
||||
class ChangeValidator: public QValidator
|
||||
{
|
||||
public:
|
||||
explicit ChangeValidator(QObject *parent,
|
||||
const QLineEdit &iLineEdit,
|
||||
const Dictionary &iDic);
|
||||
virtual State validate(QString &input, int &pos) const;
|
||||
|
||||
private:
|
||||
const QLineEdit &m_lineEdit;
|
||||
const Dictionary &m_dic;
|
||||
};
|
||||
|
||||
|
||||
PlayerWidget::PlayerWidget(QWidget *parent, CoordModel &iCoordModel,
|
||||
unsigned int iPlayerNb, PublicGame *iGame)
|
||||
: QWidget(parent), m_game(iGame), m_player(iPlayerNb)
|
||||
|
@ -85,8 +69,8 @@ PlayerWidget::PlayerWidget(QWidget *parent, CoordModel &iCoordModel,
|
|||
}
|
||||
else
|
||||
{
|
||||
lineEditChange->setValidator(new ChangeValidator(this, *lineEditRack,
|
||||
m_game->getDic()));
|
||||
QValidator * val = ValidatorFactory::newChangeValidator(this, *lineEditRack, m_game->getDic());
|
||||
lineEditChange->setValidator(val);
|
||||
}
|
||||
|
||||
refresh();
|
||||
|
@ -195,49 +179,6 @@ void PlayerWidget::pass(QString inputLetters)
|
|||
|
||||
|
||||
|
||||
ChangeValidator::ChangeValidator(QObject *parent,
|
||||
const QLineEdit &iLineEdit,
|
||||
const Dictionary &iDic)
|
||||
: QValidator(parent), m_lineEdit(iLineEdit), m_dic(iDic)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
QValidator::State ChangeValidator::validate(QString &input, int &) const
|
||||
{
|
||||
// The string is invalid if it contains invalid input characters
|
||||
const wistring &winput = wfq(input);
|
||||
if (!m_dic.validateInputChars(winput))
|
||||
return Invalid;
|
||||
|
||||
// Convert the string to internal letters
|
||||
const wstring &intInput = m_dic.convertFromInput(winput);
|
||||
// The string is invalid if it contains characters not present
|
||||
// in the dictionary
|
||||
if (!m_dic.validateLetters(intInput))
|
||||
return Intermediate;
|
||||
|
||||
const wstring &rack = m_dic.convertFromInput(wfq(m_lineEdit.text()));
|
||||
if (intInput.size() > rack.size())
|
||||
return Intermediate;
|
||||
// The letters to change must be in the rack
|
||||
// We convert back to QString objects, because their count() method is
|
||||
// very practical...
|
||||
QString qrack = qfw(rack);
|
||||
QString qinput = qfw(intInput);
|
||||
for (int i = 0; i < qinput.size(); ++i)
|
||||
{
|
||||
if (qinput.count(qinput[i], Qt::CaseInsensitive) >
|
||||
qrack.count(qinput[i], Qt::CaseInsensitive))
|
||||
{
|
||||
return Intermediate;
|
||||
}
|
||||
}
|
||||
return Acceptable;
|
||||
}
|
||||
|
||||
|
||||
|
||||
PlayerTabWidget::PlayerTabWidget(CoordModel &iCoordModel, QWidget *parent)
|
||||
: QTabWidget(parent), m_coordModel(iCoordModel)
|
||||
{
|
||||
|
|
|
@ -19,13 +19,12 @@
|
|||
*****************************************************************************/
|
||||
|
||||
#include <QtGui/QStandardItemModel>
|
||||
#include <QtGui/QValidator>
|
||||
#include <QtGui/QHeaderView>
|
||||
|
||||
#include "training_widget.h"
|
||||
#include "qtcommon.h"
|
||||
#include "play_word_mediator.h"
|
||||
#include "custom_popup.h"
|
||||
#include "validator_factory.h"
|
||||
|
||||
#include "dic.h"
|
||||
#include "bag.h"
|
||||
|
@ -40,17 +39,6 @@ using namespace std;
|
|||
|
||||
static const int HIDDEN_COLUMN = 6;
|
||||
|
||||
/// Validator used for the rack line edit
|
||||
class RackValidator: public QValidator
|
||||
{
|
||||
public:
|
||||
explicit RackValidator(QObject *parent, const Bag *iBag);
|
||||
virtual State validate(QString &input, int &pos) const;
|
||||
|
||||
private:
|
||||
const Bag *m_bag;
|
||||
};
|
||||
|
||||
|
||||
TrainingWidget::TrainingWidget(QWidget *parent, CoordModel &iCoordModel, PublicGame *iGame)
|
||||
: QWidget(parent), m_game(iGame), m_autoResizeColumns(true)
|
||||
|
@ -117,7 +105,10 @@ TrainingWidget::TrainingWidget(QWidget *parent, CoordModel &iCoordModel, PublicG
|
|||
SLOT(showPreview(const QItemSelection&, const QItemSelection&)));
|
||||
|
||||
if (m_game)
|
||||
lineEditRack->setValidator(new RackValidator(this, &m_game->getBag()));
|
||||
{
|
||||
QValidator * val = ValidatorFactory::newRackValidator(this, &m_game->getBag());
|
||||
lineEditRack->setValidator(val);
|
||||
}
|
||||
|
||||
// Notify that the rack changed
|
||||
QObject::connect(lineEditRack, SIGNAL(textChanged(const QString&)),
|
||||
|
@ -372,48 +363,3 @@ QSize TrainingWidget::sizeHint() const
|
|||
return QSize(160, 300);
|
||||
}
|
||||
|
||||
|
||||
|
||||
RackValidator::RackValidator(QObject *parent, const Bag *iBag)
|
||||
: QValidator(parent), m_bag(iBag)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
QValidator::State RackValidator::validate(QString &input, int &) const
|
||||
{
|
||||
// This should never happen, since the control should be disabled in
|
||||
// such a case, but checking doesn't hurt...
|
||||
if (m_bag == NULL)
|
||||
return Invalid;
|
||||
|
||||
input = input.toUpper();
|
||||
|
||||
const Dictionary &dic = m_bag->getDic();
|
||||
|
||||
// The string is invalid if it contains invalid input characters
|
||||
const wistring &winput = wfq(input);
|
||||
if (!dic.validateInputChars(winput))
|
||||
return Invalid;
|
||||
|
||||
// Convert the string to internal letters
|
||||
const wstring &intInput = dic.convertFromInput(winput);
|
||||
// The string is invalid if it contains characters not present
|
||||
// in the dictionary
|
||||
if (!dic.validateLetters(intInput))
|
||||
return Intermediate;
|
||||
|
||||
QString qinput = qfw(intInput);
|
||||
// The letters must be in the bag
|
||||
for (int i = 0; i < qinput.size(); ++i)
|
||||
{
|
||||
if ((unsigned int)qinput.count(qinput[i], Qt::CaseInsensitive) >
|
||||
m_bag->in(intInput[i]))
|
||||
{
|
||||
return Invalid;
|
||||
}
|
||||
}
|
||||
return Acceptable;
|
||||
}
|
||||
|
||||
|
||||
|
|
386
qt/validator_factory.cpp
Normal file
386
qt/validator_factory.cpp
Normal file
|
@ -0,0 +1,386 @@
|
|||
/*****************************************************************************
|
||||
* Eliot
|
||||
* Copyright (C) 2012 Olivier Teulière
|
||||
* Authors: Olivier Teulière <ipkiss @@ gmail.com>
|
||||
*
|
||||
* 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 2 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, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*****************************************************************************/
|
||||
|
||||
#include <QtGui/QLineEdit>
|
||||
#include <QtGui/QValidator>
|
||||
|
||||
#include "validator_factory.h"
|
||||
#include "qtcommon.h"
|
||||
|
||||
#include "dic.h"
|
||||
#include "bag.h"
|
||||
#include "coord.h"
|
||||
|
||||
// TODO: There is probably a good potential for code factorization in this file
|
||||
|
||||
|
||||
INIT_LOGGER(qt, ValidatorFactory);
|
||||
|
||||
// {{{ ChangeValidator: Validator used for the "change letters" line edit
|
||||
class ChangeValidator: public QValidator
|
||||
{
|
||||
public:
|
||||
explicit ChangeValidator(QObject *parent,
|
||||
const QLineEdit &iLineEdit,
|
||||
const Dictionary &iDic);
|
||||
virtual State validate(QString &input, int &pos) const;
|
||||
|
||||
private:
|
||||
const QLineEdit &m_lineEdit;
|
||||
const Dictionary &m_dic;
|
||||
};
|
||||
|
||||
|
||||
ChangeValidator::ChangeValidator(QObject *parent,
|
||||
const QLineEdit &iLineEdit,
|
||||
const Dictionary &iDic)
|
||||
: QValidator(parent), m_lineEdit(iLineEdit), m_dic(iDic)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
QValidator::State ChangeValidator::validate(QString &input, int &) const
|
||||
{
|
||||
// The string is invalid if it contains invalid input characters
|
||||
const wistring &winput = wfq(input);
|
||||
if (!m_dic.validateInputChars(winput))
|
||||
return Invalid;
|
||||
|
||||
// Convert the string to internal letters
|
||||
const wstring &intInput = m_dic.convertFromInput(winput);
|
||||
// The string is invalid if it contains characters not present
|
||||
// in the dictionary
|
||||
if (!m_dic.validateLetters(intInput))
|
||||
return Intermediate;
|
||||
|
||||
const wstring &rack = m_dic.convertFromInput(wfq(m_lineEdit.text()));
|
||||
if (intInput.size() > rack.size())
|
||||
return Intermediate;
|
||||
// The letters to change must be in the rack
|
||||
// We convert back to QString objects, because their count() method is
|
||||
// very practical...
|
||||
QString qrack = qfw(rack);
|
||||
QString qinput = qfw(intInput);
|
||||
for (int i = 0; i < qinput.size(); ++i)
|
||||
{
|
||||
if (qinput.count(qinput[i], Qt::CaseInsensitive) >
|
||||
qrack.count(qinput[i], Qt::CaseInsensitive))
|
||||
{
|
||||
return Intermediate;
|
||||
}
|
||||
}
|
||||
return Acceptable;
|
||||
}
|
||||
|
||||
|
||||
QValidator *ValidatorFactory::newChangeValidator(QObject *parent,
|
||||
const QLineEdit &iRackLineEdit,
|
||||
const Dictionary &iDic)
|
||||
{
|
||||
return new ChangeValidator(parent, iRackLineEdit, iDic);
|
||||
}
|
||||
// }}}
|
||||
|
||||
|
||||
// {{{ RackValidator: Validator used for the rack line edit
|
||||
class RackValidator: public QValidator
|
||||
{
|
||||
public:
|
||||
explicit RackValidator(QObject *parent, const Bag *iBag);
|
||||
virtual State validate(QString &input, int &pos) const;
|
||||
|
||||
private:
|
||||
const Bag *m_bag;
|
||||
};
|
||||
|
||||
|
||||
RackValidator::RackValidator(QObject *parent, const Bag *iBag)
|
||||
: QValidator(parent), m_bag(iBag)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
QValidator::State RackValidator::validate(QString &input, int &) const
|
||||
{
|
||||
// This should never happen, since the control should be disabled in
|
||||
// such a case, but checking doesn't hurt...
|
||||
if (m_bag == NULL)
|
||||
return Invalid;
|
||||
|
||||
input = input.toUpper();
|
||||
|
||||
const Dictionary &dic = m_bag->getDic();
|
||||
|
||||
// The string is invalid if it contains invalid input characters
|
||||
const wistring &winput = wfq(input);
|
||||
if (!dic.validateInputChars(winput))
|
||||
return Invalid;
|
||||
|
||||
// Convert the string to internal letters
|
||||
const wstring &intInput = dic.convertFromInput(winput);
|
||||
// The string is invalid if it contains characters not present
|
||||
// in the dictionary
|
||||
if (!dic.validateLetters(intInput))
|
||||
return Intermediate;
|
||||
|
||||
QString qinput = qfw(intInput);
|
||||
// The letters must be in the bag
|
||||
for (int i = 0; i < qinput.size(); ++i)
|
||||
{
|
||||
if ((unsigned int)qinput.count(qinput[i], Qt::CaseInsensitive) >
|
||||
m_bag->in(intInput[i]))
|
||||
{
|
||||
return Invalid;
|
||||
}
|
||||
}
|
||||
return Acceptable;
|
||||
}
|
||||
|
||||
|
||||
QValidator *ValidatorFactory::newRackValidator(QObject *parent,
|
||||
const Bag *iBag)
|
||||
{
|
||||
return new RackValidator(parent, iBag);
|
||||
}
|
||||
// }}}
|
||||
|
||||
|
||||
// {{{ DicRackValidator: Validator used for the line edits accepting only dictionary characters
|
||||
class DicRackValidator: public QValidator
|
||||
{
|
||||
public:
|
||||
explicit DicRackValidator(QObject *parent,
|
||||
const Dictionary *iDic,
|
||||
bool acceptJoker);
|
||||
virtual State validate(QString &input, int &pos) const;
|
||||
|
||||
private:
|
||||
const Dictionary *m_dic;
|
||||
const bool m_acceptJoker;
|
||||
};
|
||||
|
||||
|
||||
DicRackValidator::DicRackValidator(QObject *parent,
|
||||
const Dictionary *iDic,
|
||||
bool acceptJoker)
|
||||
: QValidator(parent), m_dic(iDic), m_acceptJoker(acceptJoker)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
QValidator::State DicRackValidator::validate(QString &input, int &) const
|
||||
{
|
||||
if (m_dic == NULL)
|
||||
return Invalid;
|
||||
|
||||
if (input == "")
|
||||
return Intermediate;
|
||||
|
||||
// The string is invalid if it contains invalid input characters
|
||||
const wistring &winput = wfq(input);
|
||||
if (!m_dic->validateInputChars(winput))
|
||||
return Invalid;
|
||||
|
||||
// Convert the string to internal letters
|
||||
const wstring &intInput = m_dic->convertFromInput(winput);
|
||||
// The string is invalid if it contains characters not present
|
||||
// in the dictionary
|
||||
if (!m_dic->validateLetters(intInput))
|
||||
return Intermediate;
|
||||
|
||||
// A '?' may not be acceptable
|
||||
if (!m_acceptJoker && input.contains('?'))
|
||||
return Invalid;
|
||||
// Do not accept more than 2 jokers
|
||||
if (input.count('?') > 2)
|
||||
return Invalid;
|
||||
return Acceptable;
|
||||
}
|
||||
|
||||
|
||||
QValidator *ValidatorFactory::newDicRackValidator(QObject *parent,
|
||||
const Dictionary *iDic,
|
||||
bool acceptJoker)
|
||||
{
|
||||
return new DicRackValidator(parent, iDic, acceptJoker);
|
||||
}
|
||||
// }}}
|
||||
|
||||
|
||||
// {{{ Validator used for the regexp line edit
|
||||
class RegexpValidator: public QValidator
|
||||
{
|
||||
public:
|
||||
explicit RegexpValidator(QObject *parent,
|
||||
const Dictionary *iDic);
|
||||
virtual State validate(QString &input, int &pos) const;
|
||||
|
||||
private:
|
||||
const Dictionary *m_dic;
|
||||
};
|
||||
|
||||
|
||||
RegexpValidator::RegexpValidator(QObject *parent,
|
||||
const Dictionary *iDic)
|
||||
: QValidator(parent), m_dic(iDic)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
QValidator::State RegexpValidator::validate(QString &input, int &) const
|
||||
{
|
||||
if (m_dic == NULL)
|
||||
return Invalid;
|
||||
|
||||
if (input == "")
|
||||
return Intermediate;
|
||||
|
||||
wstring authorizedChars = L".[]()*+?:^";
|
||||
|
||||
// The string is invalid if it contains invalid input characters
|
||||
const wistring &winput = wfq(input);
|
||||
if (!m_dic->validateInputChars(winput, authorizedChars))
|
||||
return Invalid;
|
||||
|
||||
// Convert the string to internal letters
|
||||
const wstring &intInput = m_dic->convertFromInput(winput);
|
||||
// The string is invalid if it contains characters not present
|
||||
// in the dictionary
|
||||
if (!m_dic->validateLetters(intInput, authorizedChars))
|
||||
return Intermediate;
|
||||
|
||||
return Acceptable;
|
||||
}
|
||||
|
||||
|
||||
QValidator *ValidatorFactory::newRegexpValidator(QObject *parent,
|
||||
const Dictionary *iDic)
|
||||
{
|
||||
return new RegexpValidator(parent, iDic);
|
||||
}
|
||||
// }}}
|
||||
|
||||
|
||||
// {{{ Validator used for the "play word" line edit
|
||||
class PlayWordValidator: public QValidator
|
||||
{
|
||||
public:
|
||||
explicit PlayWordValidator(QObject *parent,
|
||||
const Dictionary &iDic);
|
||||
virtual State validate(QString &input, int &pos) const;
|
||||
|
||||
private:
|
||||
const Dictionary &m_dic;
|
||||
};
|
||||
|
||||
|
||||
PlayWordValidator::PlayWordValidator(QObject *parent,
|
||||
const Dictionary &iDic)
|
||||
: QValidator(parent), m_dic(iDic)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
QValidator::State PlayWordValidator::validate(QString &input, int &) const
|
||||
{
|
||||
if (input == "")
|
||||
return Intermediate;
|
||||
|
||||
const wistring &winput = wfq(input);
|
||||
// The string is invalid if it contains invalid input characters
|
||||
if (!m_dic.validateInputChars(winput, L"()") || input.contains('?'))
|
||||
return Invalid;
|
||||
|
||||
// Convert the string to internal letters
|
||||
const wstring &intInput = m_dic.convertFromInput(winput);
|
||||
// The string is invalid if it contains characters not present
|
||||
// in the dictionary (ignoring parentheses)
|
||||
if (!m_dic.validateLetters(intInput, L"()"))
|
||||
return Intermediate;
|
||||
|
||||
// Check the parentheses pairs
|
||||
QString qintInput = qfw(intInput);
|
||||
int pos;
|
||||
while ((pos = qintInput.indexOf('(')) != -1)
|
||||
{
|
||||
if (qintInput.size() < pos + 3 || qintInput[pos + 2] != ')' ||
|
||||
!m_dic.validateLetters(wfq(QString(qintInput[pos + 1]))))
|
||||
{
|
||||
return Intermediate;
|
||||
}
|
||||
else
|
||||
{
|
||||
qintInput.remove(pos, 3);
|
||||
}
|
||||
}
|
||||
if (qintInput.indexOf(')') != -1)
|
||||
return Intermediate;
|
||||
|
||||
return Acceptable;
|
||||
}
|
||||
|
||||
|
||||
QValidator *ValidatorFactory::newPlayWordValidator(QObject *parent,
|
||||
const Dictionary &iDic)
|
||||
{
|
||||
return new PlayWordValidator(parent, iDic);
|
||||
}
|
||||
// }}}
|
||||
|
||||
|
||||
// {{{ CoordsValidator: Validator used to enter coordinates
|
||||
class CoordsValidator: public QValidator
|
||||
{
|
||||
public:
|
||||
explicit CoordsValidator(QObject *parent);
|
||||
virtual State validate(QString &input, int &pos) const;
|
||||
};
|
||||
|
||||
|
||||
CoordsValidator::CoordsValidator(QObject *parent)
|
||||
: QValidator(parent)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
QValidator::State CoordsValidator::validate(QString &input, int &) const
|
||||
{
|
||||
// Only authorize characters part of a valid coordinate
|
||||
wstring copy = wfq(input.toUpper());
|
||||
wstring authorized = L"ABCDEFGHIJKLMNO1234567890";
|
||||
if (copy.find_first_not_of(authorized) != wstring::npos)
|
||||
return Invalid;
|
||||
|
||||
// Check coordinates
|
||||
Coord c(wfq(input));
|
||||
if (!c.isValid())
|
||||
return Intermediate;
|
||||
|
||||
return Acceptable;
|
||||
}
|
||||
|
||||
|
||||
QValidator *ValidatorFactory::newCoordsValidator(QObject *parent)
|
||||
{
|
||||
return new CoordsValidator(parent);
|
||||
}
|
||||
// }}}
|
||||
|
||||
// vim:fdm=marker fdl=0
|
81
qt/validator_factory.h
Normal file
81
qt/validator_factory.h
Normal file
|
@ -0,0 +1,81 @@
|
|||
/*****************************************************************************
|
||||
* Eliot
|
||||
* Copyright (C) 2012 Olivier Teulière
|
||||
* Authors: Olivier Teulière <ipkiss @@ gmail.com>
|
||||
*
|
||||
* 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 2 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, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*****************************************************************************/
|
||||
|
||||
#ifndef VALIDATOR_FACTORY_H_
|
||||
#define VALIDATOR_FACTORY_H_
|
||||
|
||||
#include <QObject>
|
||||
#include "logging.h"
|
||||
|
||||
class QValidator;
|
||||
class QLineEdit;
|
||||
class Dictionary;
|
||||
class Bag;
|
||||
|
||||
|
||||
class ValidatorFactory: public QObject
|
||||
{
|
||||
Q_OBJECT;
|
||||
DEFINE_LOGGER();
|
||||
|
||||
public:
|
||||
/**
|
||||
* Create a validator suitable for changing letters.
|
||||
* The given QLineEdit contains the current rack.
|
||||
*/
|
||||
static QValidator *newChangeValidator(QObject *parent,
|
||||
const QLineEdit &iRackLineEdit,
|
||||
const Dictionary &iDic);
|
||||
|
||||
/**
|
||||
* Create a validator suitable for setting rack letters.
|
||||
*/
|
||||
static QValidator *newRackValidator(QObject *parent,
|
||||
const Bag *iBag);
|
||||
|
||||
/**
|
||||
* Create a validator suitable for setting rack letters
|
||||
* in the dictionary window.
|
||||
*/
|
||||
static QValidator *newDicRackValidator(QObject *parent,
|
||||
const Dictionary *iDic,
|
||||
bool acceptJoker = false);
|
||||
|
||||
/**
|
||||
* Create a validator suitable for entering a regular expression
|
||||
* in the dictionary window.
|
||||
*/
|
||||
static QValidator *newRegexpValidator(QObject *parent,
|
||||
const Dictionary *iDic);
|
||||
|
||||
/**
|
||||
* Create a validator suitable for playing a word.
|
||||
*/
|
||||
static QValidator *newPlayWordValidator(QObject *parent,
|
||||
const Dictionary &iDic);
|
||||
|
||||
/**
|
||||
* Create a validator suitable for entering board coordinates.
|
||||
*/
|
||||
static QValidator *newCoordsValidator(QObject *parent);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in a new issue