How to use QLineEdit's built-in undo/redo functionality while implementing custom input logic?
I am implementing a custom editor for amounts of money for a cashier's efficient work. Basic features are:
The format is always ^\d+,\d{2}$;
When cursor is left to comma, it edits the integer part (e.g., when 0,00 is edited, the first digit input replaces the zero; i.e., 0,00 becomes 1,00 after '1' is entered);
Comma or period entered moves cursor to the fractional part;
Deletes and backspaces respect the format.
I can't use masks since they can't manage arbitrary length without unnecessary spaces. I can't use validators because of they are not enough flexible to handle logic of editing leading zero in place. That's why I sublassed QLineEdit and wrote the necessary logic myself.
The question is: is there a way to support undo functionality on QLineEdit's own logic? I use setText(), that resets undo/redo history. I could make two stacks of states and override undo() and redo() correspondingly, or make sequences of selections and insertions/deletions (what can cause unnecessary blinks), but I feel there may be even simpler way.
class MoneyLineEdit : public QLineEdit {
Q_OBJECT
public:
MoneyLineEdit(QWidget *parent = 0);
protected:
void keyPressEvent(QKeyEvent * event);
};
void MoneyLineEdit::keyPressEvent(QKeyEvent *event) {
if (event->key() == Qt::Key_Comma || event->key() == Qt::Key_Period) {
setCursorPosition(text().length() - 2);
} else {
QString text = this->text();
int pos = cursorPosition();
if (event->key() == Qt::Key_Backspace) {
if (pos == text.length()) {
text.replace(text.length() - 1, 1, QChar('0'));
--pos;
} else if (pos == text.length() - 1) {
text.replace(text.length() - 2, 1, text.at(text.length() - 1));
text.replace(text.length() - 1, 1, QChar('0'));
--pos;
} else if (pos == text.length() - 2) {
--pos;
} else if (text.length() == 4 && pos == 1) {
text.replace(0, 1, QChar('0'));
}
setText(text);
setCursorPosition(pos);
} else if ((pos == text.length() - 3 || pos == text.length() - 2) && event->key() == Qt::Key_Delete && selectionStart() == -1){
text.replace(text.length() - 2, 1, text.at(text.length() - 1));
text.replace(text.length() - 1, 1, QChar('0'));
setText(text);
setCursorPosition(pos);
} else if (pos >= text.length() - 2 && pos <= text.length() - 1 && !event->text().isEmpty()){
text.replace(cursorPosition(), 1, event->text());
++pos;
setText(text);
setCursorPosition(pos);
} else if ((pos == 0 || pos == 1) && !text.isEmpty() && text.length() == 4 && text.at(0) == QChar('0') && !event->text().isEmpty()) {
text.replace(0, 1, event->text());
setText(text);
setCursorPosition(1);
} else {
QLineEdit::keyPressEvent(event);
}
}
}
Obviously, two stacks of Pair(text,cursorPosition) for undo()/redo() overloaded are sufficient.
Related
I got some issue with useState and "if else" statement. I wanted to change displayed text with a click.
When I checking if counter icrement is working, it works, but when I add setText in "if else" statement there is a problem and just doesnt work properly. Icnrement strats to work strange when clicking and change a text is just imposible. Even in console increment looks wrong. Can someone help what I am doing wrong?
let counter = 0;
const [txt, setTxt] = useState("Text1");
const handleClick = (e) => {
if (
e.target.classList.contains("fa-angle-right") || e.target.classList.contains("fa-angle-left")
) {e.target.classList.contains("fa-angle-right") ? counter++ : counter--;
if (counter === 1) {
console.log(counter);
setTxt("Text2");
} else if (counter === 2) {
console.log(counter);
setTxt("Txt3")
} else if (counter > 2) {
counter = 0;
console.log(counter);
setTxt("Text1");
} else if (counter < 0) {
counter = 2;
console.log(counter);
setTxt("Text3");
} else if (counter === 0) {
console.log(counter);
setTxt("Txt1");
}
}
};
Every time setTxt('...') is executed, the component is re-rendered and the function executes again, so counter gets the value of 0.
If you want to preserve the value of your counter between renders, put it as a new state ( const [counter, setCounter] = useState(0) )
OK, #AdriánFernándezMartínez I did that way and its close. Just after first clik, in a first "if" (if (counter === 1)) console shows 0 and i have to click twice to chage it and change Txt (but is should be 1 after setCounter(counter + 1)). Next, in a third "if" (else if (counter > 2)) console shows 3 and again I need to click twice to change Txt (after 1st click its 0 and after 2nd it become 1). Still need help.
const [counter, setCounter] = useState(0);
const [txt, setTxt] = useState("Text1");
const handleClick = (e) => {
if (
e.target.classList.contains("fa-angle-right") ||
e.target.classList.contains("fa-angle-left")
) {
e.target.classList.contains("fa-angle-right")
? setCounter(counter + 1)
: setCounter(counter + 1);
console.log(counter);
if (counter === 1) {
console.log(counter);
setTxt("Text2");
} else if (counter === 2) {
console.log(counter);
setTxt("Text3");
} else if (counter > 2) {
setCounter(0);
console.log(counter);
setTxt("Text1");
} else if (counter < 0) {
setCounter(2);
console.log(counter);
setTxt("Text3");
} else if (counter === 0) {
console.log(counter);
setTxt("Text1");
}
}
};
I just created a countdown method in Java, but i have a problem when the countdowner broadcasts the messages: 60(and down) seconds until the game starts!
The broadcast gets sent *4. Does anyone know any solution to this?
Here is my code:
Main plugin;
public StartCountdown(Main pl) {
plugin = pl;
}
public static int timeUntilStart;
#Override
public void run() {
for(Player p1 : Bukkit.getOnlinePlayers()){
if(timeUntilStart == 0) {
if(!Game.canStart()) {
plugin.restartCountdown();
ChatUtilities.broadcast(ChatColor.RED + "Not enough players to start. Countdown will");
ChatUtilities.broadcast(ChatColor.RED + "restart.");
p1.playSound(p1.getLocation(), Sound.ENDERDRAGON_WINGS, 5, 1);
return;
}
Game.start();
}
for(Player p : Bukkit.getOnlinePlayers()){
p.setLevel(timeUntilStart);
if(timeUntilStart < 11 || timeUntilStart == 60 || timeUntilStart == 30) {
p.playSound(p.getLocation(), Sound.ORB_PICKUP, 5, 0);
if(timeUntilStart == 1) {
p.playSound(p.getLocation(), Sound.ORB_PICKUP, 5, 1);
}
ChatUtilities.broadcast(String.valueOf(timeUntilStart)
+ " §6Seconds until the game starts!");
}
}
}
timeUntilStart -= 1;
}
}
You are broadcasting for every player that is online. You need to move any code that you don't want to run for every player outside of the for loop.
#Override
public void run() {
if (timeUntilStart == 0) {
if (!Game.canStart()) {
plugin.restartCountdown();
ChatUtilities.broadcast(ChatColor.RED + "Not enough players to start. Countdown will");
ChatUtilities.broadcast(ChatColor.RED + "restart.");
for (Player p : Bukkit.getOnlinePlayers()) p.playSound(p.getLocation(), Sound.ENDERDRAGON_WINGS, 5, 1);
return;
}
Game.start();
}
boolean broadcast;
for (Player p : Bukkit.getOnlinePlayers()) {
p.setLevel(timeUntilStart);
if (timeUntilStart < 11 || timeUntilStart == 60 || timeUntilStart == 30) {
p.playSound(p.getLocation(), Sound.ORB_PICKUP, 5, 0);
if (timeUntilStart == 1) p.playSound(p.getLocation(), Sound.ORB_PICKUP, 5, 1);
broadcast = true;
}
}
if (broadcast) ChatUtilities.broadcast(String.valueOf(timeUntilStart) + " §6Seconds until the game starts!");
timeUntilStart -= 1;
}
as Tanner Little said above
You are broadcasting for every player that is online. You need to move any code that you don't want to run for every player outside of the for loop.
You need to make sure as well that you are cancelling the task. I would recommend using the inbuilt scheduler. You can access the scheduler in this way
private int countDownTimer
private int countDownTime
public void runCountDown() {
countDownTimer = Bukkit.getScheduler.scheduleSyncDelayedTask(plugin, new runnable() {
public void run {
if (countDownTime <= 0) {
//do your bradcasting here
for (Player ingame : Bukkit.getOnlinePlayers()) {
//Do your player specific stuff here
}
Bukkit.getScheduler.cancelTask(countDownTimer);
}
if (countDownTime % 10 == 0) { //You can pick whaterver times u want this is just for an example
//Do periodic broadcasting
}
countDownTime -= 1;
}
}, 0L, 20L); //This means that it would wait 0 ticks to start the countdown and do the task every 20 ticks ie) 1 second.
}
Hopefully this helps you.
I am encountering issues with both std::thread and std::mutex, unable to get both to play nicely with each other. I've been pulling my hair out over this for the past few hours and just cannot make any progress whatsoever. I believe it is out of my skill area. The code is just below:
void GekkoFyre::TuiHangouts::gui_userRosterListNav(std::shared_ptr<WINDOW> display,
std::vector<const char *> userList,
const short &menuItem)
{
while (rosterEnabled) {
for (short i = 0; i < totalMenuItems; ++i) {
short diff = (menuItem - i);
if (i < numMenuItems) {
if (i == menuItem) {
size_t msgLen = strlen(userList.at(i));
size_t l = 0;
if (msgLen > subMaxX) {
for (l = subMaxX; l < msgLen; ++l) {
// Scroll the message from left to right, then vice versa, so that it fits within
// the designated window.
std::this_thread::sleep_for(std::chrono::milliseconds(500));
rosterListMutex.lock();
wattron(usrSubWin.get(), A_REVERSE); // Highlight selection
const char *msg = userList.at(i);
mvwaddstr(usrSubWin.get(), i, 0, &msg[(msgLen - l)]);
wrefresh(usrSubWin.get());
touchwin(usrSubWin.get());
rosterListMutex.unlock();
}
} else {
rosterListMutex.lock();
wattron(usrSubWin.get(), A_REVERSE); // Highlight selection
mvwaddstr(usrSubWin.get(), i, 0, userList.at(i));
wrefresh(usrSubWin.get());
touchwin(usrSubWin.get());
rosterListMutex.unlock();
}
}
wattroff(usrSubWin.get(), A_REVERSE); // Remove highlight
if ((i + 1) < numMenuItems) {
mvwaddstr(usrSubWin.get(), (i + 1), 0, userList.at((i + 1)));
}
} else if (diff < (totalMenuItems - numMenuItems) && diff > 0) {
// Allow the scrolling of a username list, from downwards and then back upwards, so that
// the user may see the list in its entirety.
wclear(usrSubWin.get());
int pos = 0;
for (short c = diff; c < (numMenuItems + diff); ++c) {
++pos;
mvwaddstr(usrSubWin.get(), pos, 0, userList.at(c));
}
pos = 0;
break;
}
}
rosterListMutex.lock();
wattroff(usrSubWin.get(), A_REVERSE); // Remove highlight
touchwin(usrSubWin.get());
wrefresh(usrSubWin.get());
wrefresh(display.get());
rosterListMutex.unlock();
}
}
I am trying to display a list of users to the left of a chat window in NCurses, as can be seen in [ 1 ] below, albeit without any users present in said screenshot. I want to keep my privacy :) What happens is that as you scroll down the list, flickering begins to occur back and forth between two usernames after scrolling past just a few. They keep repeatedly selecting each other, just over and over. I believe this is reason of the threads not syncing up properly. The function is implemented as so:
[ 1 ] - http://imgur.com/ZZlFHg2
#define WIDGET_USERS_LIST 1
short menuItem = 0;
int ch = 0;
int curr_widget = 0;
std::thread rosterListNav1(&GekkoFyre::TuiHangouts::gui_userRosterListNav, this, userListWin, rosterFormatted, menuItem);
rosterListNav1.detach();
while ((ch = wgetch(display.get())) != KEY_F(12)) {
switch (ch) {
case KEY_DOWN:
if (curr_widget == WIDGET_USERS_LIST && rosterEnabled) {
++menuItem;
if (menuItem > totalMenuItems - 1) {
menuItem = 0;
}
}
break;
case KEY_UP:
if (curr_widget == WIDGET_USERS_LIST && rosterEnabled) {
--menuItem;
if (menuItem < 0) {
menuItem = totalMenuItems - 1;
}
}
}
std::thread rosterListNav2(&GekkoFyre::TuiHangouts::gui_userRosterListNav, this, userListWin, rosterFormatted, menuItem);
rosterListNav2.detach();
}
Any help on this issue would be dearly appreciated, and I believe I have placed the std::mutex's in the right areas. I am really stumped with this problem. Also beware that while I know a few tricks of the trade, I am entirely self-taught. Some of the nomenclature that is normal to programmers who have gone through university is completely unintelligible to me.
How can I call an if statement (or any other function) without calling it again, until it's finished executing? I have an update function that calls another function (in another class) but because it executes every update, the user doesn't get the time to actually complete the IF statements (the if statements rely on user input) so therefor it returns nothing or only the first part.
Code (where the update is):
public void Update(){
if(Mouse.isButtonDown(0)){
changeTile();
}
while(Keyboard.next()){
if(Keyboard.getEventKey() == Keyboard.KEY_RIGHT && Keyboard.getEventKeyState()){
moveIndex();
}
if(Keyboard.getEventKey() == Keyboard.KEY_LEFT && Keyboard.getEventKeyState()){
moveIndexBack();
}
}
}
changeTile function:
public void changeTile(){
boolean start = true;
if(start){
while(Mouse.next()){
if(Mouse.getEventButton() == 0 && Mouse.getEventButtonState()){
//system uses tileTypes because player textures are tiletypes itself, so in the end we can let them swim by changing tiletypes
int xCoord = (int) Math.floor(Mouse.getX() / 64);
int yCoord = (int) Math.floor((HEIGHT - Mouse.getY() - 1) / 64);
tileType tile1 = map[xCoord][yCoord].getType();
System.out.println("first tile is set to:" + tile1);
start = false;
}
}
}
if(!start){
while(Mouse.next()){
if(Mouse.getEventButton() == 0 && Mouse.getEventButtonState()){
int xCoord2 = (int) Math.floor(Mouse.getX() / 64);
int yCoord2 = (int) Math.floor((HEIGHT - Mouse.getY() - 1) / 64);
tileType tile2 = map[xCoord2][yCoord2].getType();
System.out.println("second tile is set to:" + tile2);
}
}
}
In this cryptography post it says
The chain can go as long as you want, until it hits the original input. When it hits that point, it will just repeat itself and it will be useless.
So my starting point is 12345 but I can't get the end point and having an endless loop because 12345 doesn't repeat. I'm using qt4.7 (lib version: 4.7.3) to achieve this. Here is my code
rainbowTable::rainbowTable(QWidget *parent) :
QWidget(parent),
ui(new Ui::rainbowTable)
{
ui->setupUi(this);
passwordLength = 5;
qDebug() << getLastReduction("12345",false);
}
QString rainbowTable::hashString(QString value)
{
QString dataToReturn = QString(QCryptographicHash::hash((value.toAscii()),QCryptographicHash::Md5).toHex());
return dataToReturn;
}
QString rainbowTable::reductionOfString(QString hash)
{
QString dataToReturn = "";
int iterator = 0;
while ( iterator < hash.count() )
{
if ( hash.at(iterator) == '0' ||
hash.at(iterator) == '1' ||
hash.at(iterator) == '2' ||
hash.at(iterator) == '3' ||
hash.at(iterator) == '4' ||
hash.at(iterator) == '5' ||
hash.at(iterator) == '6' ||
hash.at(iterator) == '7' ||
hash.at(iterator) == '8' ||
hash.at(iterator) == '9' )
{
dataToReturn += hash.at(iterator);
if( dataToReturn.count() == passwordLength )
break;
}
iterator++;
}
return dataToReturn;
}
QString rainbowTable::getLastReduction(QString value,bool isHash)
{
int flagToAvoidImmediateExit = 0;
if( isHash )
{
QString startPoint = value;
startPoint = reductionOfString(startPoint);
QString endPoint = "";
QString tempPoint = startPoint;
while( startPoint != tempPoint || flagToAvoidImmediateExit == 0 )
{
flagToAvoidImmediateExit = 1;
endPoint = tempPoint;
tempPoint = hashString(tempPoint);
tempPoint = reductionOfString(tempPoint);
qDebug() << tempPoint;
}
return endPoint;
}
else
{
QString startPoint = value;
QString endPoint = "";
QString tempPoint = startPoint;
while( startPoint != tempPoint || flagToAvoidImmediateExit == 0 )
{
flagToAvoidImmediateExit = 1;
endPoint = tempPoint;
tempPoint = hashString(tempPoint);
tempPoint = reductionOfString(tempPoint);
qDebug() << tempPoint;
}
return endPoint;
}
}
Here is the debug ouput for a few seconds:
"38064"
"37923"
"59636"
"14842"
"81105"
"83011"
"84978"
"72903"
"28301"
"59067"
"94222"
"35329"
"75907"
"52980"
"64297"
"36654"
"12207"
"83738"
"03523"
"79083"
"15597"
"32652"
"13934"
"88497"
"75435"
"79791"
"58265"
"09856"
"18041"
"43966"
"65978"
"64242"
"52739"
"55704"
"56811"
"58183"
"68597"
"84064"
"85717"
"46438"
"18042"
"71321"
"88067"
"70648"
"83580"
"11878"
"32297"
"52376"
"41289"
"07909"
"50439"
"03819"
"50325"
"82736"
"41621"
"05497"
"15546"
"64017"
"90503"
"13150"
"30287"
"01749"
"81308"
"12036"
"37241"
"35850"
"97225"
"80539"
"17472"
"63098"
"85818"
"18438"
"26139"
"09545"
"97042"
"63672"
"37406"
"41180"
"14910"
"28900"
"29729"
"56861"
"16208"
"83565"
"30912"
"95541"
"08468"
"29539"
"93679"
"42487"
"95833"
"42793"
"97064"
"18087"
"75623"
"13910"
"60404"
"52557"
"95932"
"65477"
"28304"
"08456"
"27849"
"11429"
"38896"
"08634"
"97107"
"96385"
"44159"
"32875"
"17063"
"86213"
"85052"
"46852"
"97541"
"81412"
"31199"
"96618"
"16178"
"56100"
"50394"
"42087"
"90552"
"51966"
"13598"
"28757"
"38715"
"71025"
"61334"
"43686"
"74633"
"50360"
"99883"
"01361"
"49662"
"62929"
"07280"
"59161"
"32509"
"93670"
"95649"
"15206"
"99927"
"93692"
"37748"
"23350"
"74680"
"68259"
"04819"
"26627"
"65968"
"06919"
"09194"
"50084"
"74452"
"23763"
"17953"
"35026"
"86691"
"67542"
"95634"
"00793"
"20270"
"24386"
"35606"
"76055"
"00010"
"00798"
"30867"
"20697"
"02143"
"12044"
"05098"
"52828"
"98446"
"54039"
"08778"
"98405"
"92267"
"71783"
"61953"
"87447"
"66505"
"66535"
"01776"
"90120"
"51497"
"56082"
"18253"
"15222"
"74769"
"19614"
"86376"
"65391"
"43365"
"90484"
"32717"
"75052"
"16186"
"89444"
"15439"
"65166"
"75785"
"72462"
"75920"
"91383"
"41678"
"94123"
"61751"
"47976"
"67798"
"59438"
"10180"
"65854"
"40218"
"77990"
"44843"
"84554"
"52350"
"73347"
"51901"
"61155"
"30316"
"83096"
"64946"
"05985"
"24208"
"28718"
"02241"
"22303"
"23331"
"18410"
"54868"
"51723"
"06401"
"49554"
"65577"
"28105"
"42319"
"34167"
"85036"
"98679"
"08594"
"31075"
"80514"
"11517"
"66780"
"33411"
"83180"
"61910"
"70423"
"16885"
"09107"
"83702"
"81842"
"88430"
"59146"
"29140"
"47236"
"29625"
"03078"
"26540"
"79321"
"41649"
"10210"
"75702"
"12020"
"36877"
"57307"
"03222"
"46603"
"58449"
"94709"
"01436"
"84975"
"39385"
"15952"
"67607"
"91666"
"34456"
"53385"
"21512"
"06712"
"42073"
"61343"
"66825"
"70199"
"73203"
"60216"
"39469"
"84324"
"47850"
"84825"
"52471"
"92397"
"86051"
"33676"
"04221"
"79740"
"11573"
"26304"
"52510"
"12679"
"05930"
"49607"
"10880"
"99174"
"53967"
"06397"
"25700"
"96721"
"94694"
"96566"
"31746"
"57359"
"84870"
"06236"
"10673"
"45914"
"19209"
"32478"
"38824"
"71178"
"22983"
"36320"
"46594"
"66538"
"80495"
"35645"
"38064"
"37923"
"59636"
"14842"
"81105"
"83011"
"84978"
"72903"
"28301"
"59067"
"94222"
"35329"
"75907"
"52980"
"64297"
"36654"
"12207"
"83738"
"03523"
"79083"
"15597"
"32652"
"13934"
"88497"
"75435"
"79791"
"58265"
"09856"
"18041"
"43966"
"65978"
"64242"
"52739"
"55704"
"56811"
"58183"
"68597"
"84064"
"85717"
"46438"
"18042"
"71321"
"88067"
"70648"
"83580"
"11878"
"32297"
"52376"
"41289"
"07909"
"50439"
"03819"
"50325"
"82736"
"41621"
"05497"
"15546"
"64017"
"90503"
"13150"
"30287"
"01749"
"81308"
"12036"
"37241"
"35850"
"97225"
"80539"
"17472"
"63098"
"85818"
"18438"
"26139"
"09545"
"97042"
"63672"
"37406"
"41180"
"14910"
"28900"
"29729"
"56861"
"16208"
"83565"
"30912"
"95541"
"08468"
"29539"
"93679"
"42487"
"95833"
"42793"
"97064"
"18087"
"75623"
"13910"
"60404"
"52557"
"95932"
"65477"
"28304"
"08456"
"27849"
"11429"
"38896"
"08634"
"97107"
"96385"
"44159"
"32875"
"17063"
"86213"
"85052"
"46852"
"97541"
"81412"
"31199"
"96618"
"16178"
"56100"
"50394"
"42087"
"90552"
"51966"
"13598"
"28757"
"38715"
"71025"
"61334"
"43686"
"74633"
"50360"
"99883"
"01361"
"49662"
"62929"
"07280"
"59161"
"32509"
"93670"
"95649"
"15206"
"99927"
"93692"
"37748"
"23350"
"74680"
"68259"
"04819"
"26627"
"65968"
"06919"
"09194"
"50084"
"74452"
"23763"
"17953"
"35026"
"86691"
"67542"
"95634"
"00793"
"20270"
"24386"
"35606"
"76055"
"00010"
"00798"
"30867"
"20697"
"02143"
"12044"
"05098"
"52828"
"98446"
"54039"
"08778"
"98405"
"92267"
"71783"
"61953"
"87447"
"66505"
"66535"
"01776"
"90120"
"51497"
"56082"
"18253"
"15222"
"74769"
"19614"
"86376"
"65391"
"43365"
"90484"
As you see 12345 doesn't repeat but other numbers are repeated and having endless loop. Is my starting point wrong?
The chain is not guaranteed to ever hit the initial value again. More often than not you'll probably find it entering a loop like this:
If the input is larger than the hash output, it is impossible by definition to ever hit the initial input value again. However, even if the input has an equal length to the output, it is not guaranteed that the hash will cover every single possible value in the output space before looping around. This actually depends on the characteristics and quality of the hash. A hash may have one big cycle, covering every single possible output value in its loop. Other hashes may enter a number of different possible loops, each covering a different subset of the output space. Other hashes may not ever cover every possible output value.