Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <a.out.h>
- #include <QDebug>
- #include <QInputDialog>
- #include <QMessageBox>
- #include <QString>
- #include <QStringList>
- #include <algorithm>
- #include "./ui_widget.h"
- #include "widget.h"
- Widget::Widget( QWidget *parent ) : QWidget( parent ), ui( new Ui::Widget )
- {
- ui->setupUi( this );
- }
- Widget::~Widget()
- {
- delete ui;
- }
- void Widget::on_addItemButton_clicked()
- {
- addNewRoom();
- }
- void Widget::on_newItemLineEdit_returnPressed()
- {
- addNewRoom();
- }
- void Widget::on_deleteItemButton_clicked()
- {
- if ( ui->listWidget->currentItem() )
- {
- QString room = ui->listWidget->currentItem()->text();
- QString str = "Are you sure you want to permanently delete this room?:\n\"" + room + "\"";
- if ( QMessageBox::critical( this, "Deleting room is permanent!", str, QMessageBox::Yes, QMessageBox::Cancel ) ==
- QMessageBox::Yes )
- {
- delete ui->listWidget->takeItem( ui->listWidget->currentRow() );
- }
- }
- }
- void Widget::addNewRoom()
- {
- if ( ui->newItemLineEdit->text().size() )
- {
- QString room = ui->newItemLineEdit->text();
- auto lastDoubleDotPos = room.lastIndexOf("..");
- /* Inside isNotAPossibleSequenceInput() all non-sequence inputs will be added normally and the
- * remainder of addNewRoom() becomes irrelevant. In likewise manner valid sequential inputs are
- * added inside of functions processedAsNumberSequence() and processedAsLetterSequence(). */
- if ( isNotAPossibleSequenceInput( lastDoubleDotPos, room ))
- {
- return;
- }
- QString seqBegin = room.first(lastDoubleDotPos); //left side of ".."
- QString seqEnd = room.mid(lastDoubleDotPos + 2); //right side of ".."
- if ( seqEndFormatIsInvalid(seqBegin, seqEnd, room) )
- {
- return;
- }
- if( processedAsNumberSequence(seqBegin, seqEnd, room) )
- {
- return;
- }
- if( processedAsLetterSequence(seqBegin, seqEnd, room) )
- {
- return;
- }
- else
- {
- return ambiguousFormatWarning(room);
- }
- }
- }
- bool Widget::isNotAPossibleSequenceInput(int pos, const QString& room)
- {
- if ( pos == -1 )
- {
- if(room.contains("."))
- {
- ambiguousFormatWarning(room);
- return true;
- }
- addLineEditText();
- return true;
- }
- if (room.lastIndexOf("...") == pos - 1 || pos == room.size() - 2 || pos == 0)
- {
- ambiguousFormatWarning(room);
- return true;
- }
- return false;
- }
- void Widget::ambiguousFormatWarning(const QString& room)
- {
- QString ambigMsgBoxTitle = "Ambiguous format detected...";
- QString ambigMsgBoxMsg = "Ambiguous format detected in inputted room:\n\"" + room +
- "\"\nIf you meant to convey a sequence of inputs, then click \'Cancel\'"
- " and resubmit placing at the end either number(s) or a letter (of matching case) on either side of "
- "two dots with the greater value on the right side. Examples:\n\'Suite 100..120\', \'Bungalow 2A..C\'\n"
- "Otherwise click \'Ignore\' and just one room will be submitted as what was written.";
- if (QMessageBox::warning( this, ambigMsgBoxTitle, ambigMsgBoxMsg, QMessageBox::Ignore, QMessageBox::Cancel )
- == QMessageBox::Cancel )
- {
- return;
- }
- else
- {
- addLineEditText();
- return;
- }
- }
- void Widget::addLineEditText()
- {
- ui->listWidget->addItem( ui->newItemLineEdit->text() );
- ui->newItemLineEdit->clear();
- }
- bool Widget::seqEndFormatIsInvalid(const QString& seqBegin, const QString& seqEnd, const QString& room)
- {
- if ( seqEnd.size() > 1 )
- {
- if (std::any_of( seqEnd.begin(), seqEnd.end(), []( auto c ) { return !c.isDigit(); } ) ||
- !seqBegin.back().isDigit() )
- {
- ambiguousFormatWarning(room);
- return true;
- }
- return false;
- }
- }
- bool Widget::processedAsNumberSequence(const QString& seqBegin, const QString& seqEnd, const QString& room)
- {
- if ( seqEnd.front().isDigit() )
- {
- if ( std::all_of( seqBegin.begin(), seqBegin.end(), []( auto c ) { return c.isDigit(); } ) )
- {
- if (seqBegin.toInt() >= seqEnd.toInt())
- {
- ambiguousFormatWarning(room);
- }
- else
- {
- addLineEditSequence("", seqBegin.toInt(), seqEnd.toInt(), RoomSequence::Number);
- }
- }
- else
- {
- int i = seqBegin.size() - 1;
- for ( ; seqBegin[ i ].isDigit() && i > -1; --i );
- auto startNumber = seqBegin.last( seqBegin.size() - (i + 1)).toInt();
- auto endNumber = seqEnd.toInt();
- if(startNumber < endNumber)
- {
- addLineEditSequence(seqBegin.first( i + 1 ), startNumber,
- endNumber, RoomSequence::Number );
- }
- else
- {
- ambiguousFormatWarning(room);
- }
- }
- return true;
- }
- return false;
- }
- void Widget::addLineEditSequence(const QString& base, int roomNumberStart, int roomNumberEnd, RoomSequence type)
- {
- int currentRoom = roomNumberStart;
- for ( ; currentRoom < roomNumberEnd + 1; ++currentRoom )
- {
- QString addMe;
- if(type == RoomSequence::Letter)
- {
- addMe = base + QChar(currentRoom);
- }
- else
- {
- addMe = base + QString::number(currentRoom);
- }
- ui->listWidget->addItem( addMe );
- }
- ui->newItemLineEdit->clear();
- }
- bool Widget::processedAsLetterSequence(const QString& seqBegin, const QString& seqEnd, const QString& room)
- {
- if ( seqEnd.front().isLetter() && seqBegin.back().isLetter() && seqBegin.back() < seqEnd.front() &&
- seqBegin.back().category() == seqEnd.front().category() ) //category refers to case
- {
- QString base = seqBegin.size() > 1? seqBegin.first(seqBegin.size() - 1) : "";
- addLineEditSequence(base, seqBegin.back().unicode(), seqEnd.front().unicode(), RoomSequence::Letter);
- return true;
- }
- else
- {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement