Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <a.out.h>
- #include <sqlite3.h>
- #include <QDebug>
- #include <QInputDialog>
- #include <QMessageBox>
- #include <QString>
- #include <QStringList>
- #include <QRegularExpression>
- #include <QRegularExpressionMatch>
- #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
- if ( isNotAPossibleSequenceInput( lastDoubleDotPos, room ))
- {
- return;
- }
- static const QRegularExpression upperLetterSequence("^(?<base>.*)(?<start>[A-Z])\\.\\.(?<end>[A-Z])\\s*$");
- if ( upperLetterSequence.match(room).hasMatch() )
- {
- auto match = upperLetterSequence.match(room);
- addLineEditSequence(match.captured(1), match.captured(2).front().unicode(), match.captured(3).front().unicode(), RoomSequence::Letter);
- return;
- }
- static const QRegularExpression lowerLetterSequence("^(?<base>.*)(?<start>[a-z])\\.\\.(?<end>[a-z])\\s*$");
- if ( lowerLetterSequence.match(room).hasMatch() )
- {
- auto match = lowerLetterSequence.match(room);
- addLineEditSequence(match.captured(1), match.captured(2).front().unicode(), match.captured(3).front().unicode(), RoomSequence::Letter);
- return;
- }
- static const QRegularExpression baseToNumberSequence("^(?<base>.*\\D+)(?<start>[1-9]\\d*)\\.\\.(?<end>[1-9]\\d*)\\s*$");
- if( baseToNumberSequence.match(room).hasMatch())
- {
- auto match = baseToNumberSequence.match(room);
- addLineEditSequence(match.captured(1), match.captured(2).toInt(), match.captured(3).toInt(), RoomSequence::Number);
- qDebug() << "\"" << match.captured(1) << "\", \"" << match.captured(2).toInt() << "\", \""
- << match.captured(3).toInt() << "\"";
- return;
- }
- static const QRegularExpression pureNumberSequence("^(?<start>[1-9]\\d*)\\.\\.(?<end>[1-9]\\d*)\\s*$");
- if( pureNumberSequence.match(room).hasMatch())
- {
- auto match = pureNumberSequence.match(room);
- addLineEditSequence("", match.captured(1).toInt(), match.captured(2).toInt(), RoomSequence::Number);
- // qDebug() << "\"" << match.captured(1) << "\", \"" << match.captured(2).toInt() << "\", \""
- // << match.captured(3).toInt() << "\"";
- 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()
- {
- QString addMe = ui->newItemLineEdit->text().trimmed();
- auto existingItems = ui->listWidget->findItems(addMe, Qt::MatchExactly);
- if (existingItems.isEmpty()) {
- // Add the item to the list only if it doesn't exist already
- ui->listWidget->addItem(addMe);
- ui->newItemLineEdit->clear();
- }
- else
- {
- QString msg = "Room " + addMe + " could not be added as it already exists.";
- QMessageBox::information(this, "Could not add room", msg);
- }
- }
- void Widget::addLineEditSequence(const QString& base, int roomNumberStart, int roomNumberEnd, RoomSequence type)
- {
- for ( int currentRoom = roomNumberStart; currentRoom < roomNumberEnd + 1; ++currentRoom )
- {
- QString addMe;
- if(type == RoomSequence::Letter)
- {
- addMe = base + QChar(currentRoom);
- }
- else
- {
- addMe = base + QString::number(currentRoom);
- }
- auto trimmed = addMe.trimmed();
- auto existingItems = ui->listWidget->findItems(trimmed, Qt::MatchExactly);
- if (existingItems.isEmpty()) {
- // Add the item to the list only if it doesn't exist already
- ui->listWidget->addItem(addMe);
- ui->newItemLineEdit->clear();
- }
- else
- {
- QString msg = "Room " + trimmed + " could not be added as it already exists.";
- QMessageBox::information(this, "Could not add room", msg);
- }
- }
- ui->newItemLineEdit->clear();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement